Skip to content

Commit

Permalink
test: add PickerViewModelImplTest
Browse files Browse the repository at this point in the history
  • Loading branch information
ksnd committed May 11, 2023
1 parent e2b4581 commit 05b0d6e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/mock/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ dependencies {
implementation(project(":core:data"))
implementation(project(":core:model"))
implementation(project(":core:util"))
implementation(libs.compose.color.picker)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kosenda.makecolor.core.mock

import com.godaddy.android.colorpicker.HsvColor

class MockHsvColor {
val item = HsvColor(hue = 180f, saturation = 0.9f, value = 0.9f, alpha = 1f)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kosenda.makecolor.feature.viewmodel

import android.content.Context
import android.graphics.Bitmap
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package kosenda.makecolor.feature.viewmodel

import com.godaddy.android.colorpicker.harmony.ColorHarmonyMode
import com.google.common.truth.Truth.assertThat
import kosenda.makecolor.core.mock.MockHsvColor
import kosenda.makecolor.core.model.data.ColorData
import kosenda.makecolor.core.ui.data.PickerType
import kosenda.makecolor.core.util.hsvColorToHsv
import kosenda.makecolor.core.util.hsvToRGB
import kosenda.makecolor.core.util.rgbToCmyk
import kosenda.makecolor.core.util.rgbToHex
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class PickerViewModelImplTest {

private val viewModel = PickerViewModelImpl()

@Test
fun updateColorData_hsvColor_updatedUiState() {
// hsvColorを渡したときにColorDataが作成されUiStateに反映されることを確認する
val hsv = hsvColorToHsv(hsvColor = MockHsvColor().item)
val rgb = hsvToRGB(hsv = hsv)
val cmyk = rgbToCmyk(rgb)
val hex = rgbToHex(rgb)
viewModel.updateColorData(MockHsvColor().item)
assertThat(viewModel.uiState.value.colorData).isEqualTo(ColorData(rgb, cmyk, hsv, hex))
}

@Test
fun updateHsvColor_hsvColor_updatedUiState() {
// hsvColorを渡したときにUiStateに反映されることを確認する
val originColorData = viewModel.uiState.value.colorData
val originHsvColorData = viewModel.uiState.value.hsvColorData
viewModel.updateHsvColor(MockHsvColor().item)
assertThat(viewModel.uiState.value.colorData).isNotEqualTo(originColorData)
assertThat(viewModel.uiState.value.hsvColorData).isNotEqualTo(originHsvColorData)
}

@Test
fun updatePickerType_pickerType_updatedUiState() {
// pickerTypeを渡したときにUiStateに反映されることを確認する
viewModel.updatePickerType(PickerType.CIRCLE)
assertThat(viewModel.uiState.value.pickerType).isEqualTo(PickerType.CIRCLE)
viewModel.updatePickerType(PickerType.SQUARE)
assertThat(viewModel.uiState.value.pickerType).isEqualTo(PickerType.SQUARE)
}

@Test
fun updateHarmonyMode_colorHarmonyMode_updatedUiState() {
// colorHarmonyModeを渡したときにUiStateに反映されることを確認する
viewModel.updateHarmonyMode(ColorHarmonyMode.COMPLEMENTARY)
assertThat(viewModel.uiState.value.harmonyMode).isEqualTo(ColorHarmonyMode.COMPLEMENTARY)
viewModel.updateHarmonyMode(ColorHarmonyMode.ANALOGOUS)
assertThat(viewModel.uiState.value.harmonyMode).isEqualTo(ColorHarmonyMode.ANALOGOUS)
}
}

0 comments on commit 05b0d6e

Please sign in to comment.