Skip to content

Commit ba1c418

Browse files
committed
Address review comments on test naming, companion keys
1 parent e7a6d70 commit ba1c418

3 files changed

Lines changed: 32 additions & 28 deletions

File tree

.idea/ktfmt.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/kotlin/io/homeassistant/companion/android/sensors/DynamicColorSensorManager.kt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ class DynamicColorSensorManager @Inject constructor(
2929
companion object {
3030
// See https://source.android.com/docs/core/display/dynamic-color#dynamic-13
3131
private val TONAL_PALETTE_STYLES = listOf(
32-
"TONAL_SPOT",
33-
"SPRITZ",
34-
"MONOCHROMATIC",
35-
"VIBRANT",
3632
"EXPRESSIVE",
3733
"FRUIT_SALAD",
34+
"MONOCHROMATIC",
3835
"RAINBOW",
39-
).sorted()
36+
"SPRITZ",
37+
"TONAL_SPOT",
38+
"VIBRANT",
39+
)
40+
private const val THEME_OVERLAY_JSON = "theme_customization_overlay_packages"
41+
private const val THEME_STYLE = "android.theme.customization.theme_style"
4042

4143
@ProvidesSensor
4244
val accentColorSensor = SensorManager.BasicSensor(
@@ -122,36 +124,32 @@ class DynamicColorSensorManager @Inject constructor(
122124
)
123125
}
124126

125-
// See https://source.android.com/docs/core/display/dynamic-color#dynamic-13
126-
val jsonKey = "theme_customization_overlay_packages"
127-
val styleKey = "android.theme.customization.theme_style"
128-
129127
val jsonString = try {
130-
Settings.Secure.getString(applicationContext.contentResolver, jsonKey)
128+
Settings.Secure.getString(applicationContext.contentResolver, THEME_OVERLAY_JSON)
131129
} catch (ex: Exception) {
132-
Timber.w(ex, "Exception reading %s", jsonKey)
130+
Timber.w(ex, "Exception reading %s", THEME_OVERLAY_JSON)
133131
updateState(STATE_UNAVAILABLE)
134132
return
135133
}
136134

137135
if (jsonString == null) {
138-
Timber.w("No value found for %s", jsonKey)
136+
Timber.w("No value found for %s", THEME_OVERLAY_JSON)
139137
updateState(STATE_UNAVAILABLE)
140138
return
141139
}
142140

143141
val jsonObject = try {
144142
JSONObject(jsonString)
145143
} catch (ex: JSONException) {
146-
Timber.w(ex, "Exception parsing JSON for %s", jsonKey)
144+
Timber.w(ex, "Exception parsing JSON for %s", THEME_OVERLAY_JSON)
147145
updateState(STATE_UNKNOWN)
148146
return
149147
}
150148

151149
val themeStyle = try {
152-
jsonObject.getString(styleKey)
150+
jsonObject.getString(THEME_STYLE)
153151
} catch (ex: JSONException) {
154-
Timber.w(ex, "Missing %s in JSON for %s", styleKey, jsonKey)
152+
Timber.w(ex, "Missing %s in JSON for %s", THEME_STYLE, THEME_OVERLAY_JSON)
155153
updateState(STATE_UNKNOWN)
156154
return
157155
}

app/src/test/kotlin/io/homeassistant/companion/android/sensors/DynamicColorSensorManagerTest.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,46 +48,46 @@ class DynamicColorSensorManagerTest {
4848

4949
@Config(maxSdk = Build.VERSION_CODES.R)
5050
@Test
51-
fun `Given SDK is lower than Android 12 sensor is absent`() {
51+
fun `Given SDK is lower than Android 12 then sensor is absent`() {
5252
assertFalse(dynamicColorSensor.hasSensor())
5353
}
5454

5555
@Config(minSdk = Build.VERSION_CODES.S)
5656
@Test
57-
fun `Given SDK is at least Android 12 sensor is present`() {
57+
fun `Given SDK is at least Android 12 then sensor is present`() {
5858
assertTrue(dynamicColorSensor.hasSensor())
5959
}
6060

6161
@Test
62-
fun `Available sensors includes color sensor`() = runTest {
62+
fun `Given dynamic color sensor when available sensors then includes color sensor`() = runTest {
6363
val availableSensors = dynamicColorSensor.getAvailableSensors()
6464
assertTrue(availableSensors.contains(DynamicColorSensorManager.accentColorSensor))
6565
}
6666

6767
@Test
68-
fun `Available sensors includes palette sensor`() = runTest {
68+
fun `Given dynamic color sensor when available sensors then includes palette sensor`() = runTest {
6969
val availableSensors = dynamicColorSensor.getAvailableSensors()
7070
assertTrue(availableSensors.contains(DynamicColorSensorManager.tonalPaletteSensor))
7171
}
7272

7373
@Test
74-
fun `Color sensor does not require any special permissions`() {
74+
fun `Given accent color sensor when required permissions then none specified`() {
7575
assertArrayEquals(
7676
emptyArray<String>(),
7777
dynamicColorSensor.requiredPermissions(DynamicColorSensorManager.accentColorSensor.id),
7878
)
7979
}
8080

8181
@Test
82-
fun `Palette sensor does not require any special permissions`() {
82+
fun `Given tonal palette sensor when required permissions then none specified`() {
8383
assertArrayEquals(
8484
emptyArray<String>(),
8585
dynamicColorSensor.requiredPermissions(DynamicColorSensorManager.tonalPaletteSensor.id),
8686
)
8787
}
8888

8989
@Test
90-
fun `Given color sensor is enabled then request update sets theme color`() = runTest {
90+
fun `Given enabled color sensor when request update then sets theme color`() = runTest {
9191
val id = DynamicColorSensorManager.accentColorSensor.id
9292
sensorRepository.setSensorEnabled(id, listOf(1), true)
9393

@@ -100,7 +100,7 @@ class DynamicColorSensorManagerTest {
100100
}
101101

102102
@Test
103-
fun `Given color sensor is enabled then request update sets rgb color attribute`() = runTest {
103+
fun `Given enabled accent color sensor when request update then sets rgb color attribute`() = runTest {
104104
val id = DynamicColorSensorManager.accentColorSensor.id
105105
sensorRepository.setSensorEnabled(id, listOf(1), true)
106106

@@ -115,7 +115,7 @@ class DynamicColorSensorManagerTest {
115115
}
116116

117117
@Test
118-
fun `Given color sensor is disabled then request update does not update state`() = runTest {
118+
fun `Given disabled color sensor when request update then does not update state`() = runTest {
119119
val id = DynamicColorSensorManager.accentColorSensor.id
120120
sensorRepository.setSensorEnabled(id, listOf(1), false)
121121

@@ -125,7 +125,7 @@ class DynamicColorSensorManagerTest {
125125
}
126126

127127
@Test
128-
fun `Given palette sensor is enabled then request update sets palette variant`() = runTest {
128+
fun `Given palette sensor when request update then sets palette variant`() = runTest {
129129
val id = DynamicColorSensorManager.tonalPaletteSensor.id
130130
sensorRepository.setSensorEnabled(id, listOf(1), true)
131131

@@ -141,7 +141,7 @@ class DynamicColorSensorManagerTest {
141141
}
142142

143143
@Test
144-
fun `Given palette sensor receives malformed input then state unknown`() = runTest {
144+
fun `Given palette sensor when receives malformed input then state is unknown`() = runTest {
145145
val id = DynamicColorSensorManager.tonalPaletteSensor.id
146146
sensorRepository.setSensorEnabled(id, listOf(1), true)
147147

@@ -157,7 +157,7 @@ class DynamicColorSensorManagerTest {
157157
}
158158

159159
@Test
160-
fun `Given palette sensor receives null input then state is unavailable`() = runTest {
160+
fun `Given palette sensor when receives null input then state is unavailable`() = runTest {
161161
val id = DynamicColorSensorManager.tonalPaletteSensor.id
162162
sensorRepository.setSensorEnabled(id, listOf(1), true)
163163

@@ -173,7 +173,7 @@ class DynamicColorSensorManagerTest {
173173
}
174174

175175
@Test
176-
fun `Given palette sensor is updated then options are exhaustive`() = runTest {
176+
fun `Given palette sensor when updated then options are exhaustive`() = runTest {
177177
val id = DynamicColorSensorManager.tonalPaletteSensor.id
178178
sensorRepository.setSensorEnabled(id, listOf(1), true)
179179

0 commit comments

Comments
 (0)