Skip to content

Commit d01ef07

Browse files
committed
fix: correct pre-existing test failures in MediaStoreGatewayFactoryTest and ServerSetupScreenTest
- MediaStoreGatewayFactoryTest: Replace unreliable Java reflection SDK mocking with MockK static function mocking for shouldUseNewMediaStore() - ServerSetupScreenTest: Fix button text assertions (Setup -> Start) and state management for custom model toggle
1 parent 8424b1b commit d01ef07

File tree

2 files changed

+35
-55
lines changed

2 files changed

+35
-55
lines changed
Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package dev.minios.pdaiv1.data.gateway.mediastore
22

33
import android.content.Context
4-
import android.os.Build
4+
import dev.minios.pdaiv1.core.common.extensions.shouldUseNewMediaStore
55
import dev.minios.pdaiv1.core.common.file.FileProviderDescriptor
6+
import io.mockk.every
67
import io.mockk.mockk
8+
import io.mockk.mockkStatic
9+
import io.mockk.unmockkStatic
10+
import org.junit.After
711
import org.junit.Assert
12+
import org.junit.Before
813
import org.junit.Test
9-
import java.lang.reflect.Field
10-
import java.lang.reflect.Method
11-
import java.lang.reflect.Modifier
1214

1315
class MediaStoreGatewayFactoryTest {
1416

@@ -20,62 +22,41 @@ class MediaStoreGatewayFactoryTest {
2022
fileProviderDescriptor = stubFileProviderDescriptor,
2123
)
2224

25+
@Before
26+
fun setUp() {
27+
mockkStatic(::shouldUseNewMediaStore)
28+
}
29+
30+
@After
31+
fun tearDown() {
32+
unmockkStatic(::shouldUseNewMediaStore)
33+
}
34+
2335
@Test
2436
fun `given app running on Android SDK 26 (O), expected factory returned instance of type MediaStoreGatewayOldImpl`() {
25-
mockSdkInt(Build.VERSION_CODES.O)
37+
every { shouldUseNewMediaStore() } returns false
2638
val actual = factory.invoke()
2739
Assert.assertEquals(true, actual is MediaStoreGatewayOldImpl)
2840
}
2941

3042
@Test
3143
fun `given app running on Android SDK 31 (S), expected factory returned instance of type MediaStoreGatewayOldImpl`() {
32-
mockSdkInt(Build.VERSION_CODES.S)
44+
every { shouldUseNewMediaStore() } returns false
3345
val actual = factory.invoke()
3446
Assert.assertEquals(true, actual is MediaStoreGatewayOldImpl)
3547
}
3648

3749
@Test
38-
fun `given app running on Android SDK 32 (S_V2), expected factory returned instance of type MediaStoreGatewayOldImpl`() {
39-
mockSdkInt(Build.VERSION_CODES.S_V2)
50+
fun `given app running on Android SDK 32 (S_V2), expected factory returned instance of type MediaStoreGatewayImpl`() {
51+
every { shouldUseNewMediaStore() } returns true
4052
val actual = factory.invoke()
4153
Assert.assertEquals(true, actual is MediaStoreGatewayImpl)
4254
}
4355

4456
@Test
45-
fun `given app running on Android SDK 34 (UPSIDE_DOWN_CAKE), expected factory returned instance of type MediaStoreGatewayOldImpl`() {
46-
mockSdkInt(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
57+
fun `given app running on Android SDK 34 (UPSIDE_DOWN_CAKE), expected factory returned instance of type MediaStoreGatewayImpl`() {
58+
every { shouldUseNewMediaStore() } returns true
4759
val actual = factory.invoke()
4860
Assert.assertEquals(true, actual is MediaStoreGatewayImpl)
4961
}
50-
51-
private fun mockSdkInt(sdkInt: Int) {
52-
val sdkIntField = Build.VERSION::class.java.getField("SDK_INT")
53-
sdkIntField.isAccessible = true
54-
getModifiersField().also {
55-
it.isAccessible = true
56-
it.set(sdkIntField, sdkIntField.modifiers and Modifier.FINAL.inv())
57-
}
58-
sdkIntField.set(null, sdkInt)
59-
}
60-
61-
private fun getModifiersField(): Field {
62-
return try {
63-
Field::class.java.getDeclaredField("modifiers")
64-
} catch (e: NoSuchFieldException) {
65-
try {
66-
val getDeclaredFields0: Method =
67-
Class::class.java.getDeclaredMethod("getDeclaredFields0", Boolean::class.javaPrimitiveType)
68-
getDeclaredFields0.isAccessible = true
69-
val fields = getDeclaredFields0.invoke(Field::class.java, false) as Array<Field>
70-
for (field in fields) {
71-
if ("modifiers" == field.name) {
72-
return field
73-
}
74-
}
75-
} catch (ex: ReflectiveOperationException) {
76-
e.addSuppressed(ex)
77-
}
78-
throw e
79-
}
80-
}
8162
}

presentation/src/test/java/dev/minios/pdaiv1/presentation/screen/setup/ServerSetupScreenTest.kt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ class ServerSetupScreenTest : CoreComposeTest {
114114
setupButton
115115
.assertIsDisplayed()
116116
.assertIsNotEnabled()
117-
.assertTextEquals("Setup")
117+
.assertTextEquals("Start")
118118
}
119119

120120
@Test
121-
fun `given user is on CONFIGURE tab with LOCAL server source, clicks Switch, expected main button with Setup text becomes enabled then clicks Switch again, expected main button with Setup text becomes disabled`() {
121+
fun `given user is on CONFIGURE tab with LOCAL server source, clicks Switch, expected main button with Start text becomes enabled then clicks Switch again, expected main button with Start text becomes disabled`() {
122122
composeTestRule.setContent {
123123
ServerSetupScreen(viewModel = stubViewModel)
124124
}
@@ -134,7 +134,7 @@ class ServerSetupScreenTest : CoreComposeTest {
134134
setupButton
135135
.assertIsDisplayed()
136136
.assertIsNotEnabled()
137-
.assertTextEquals("Setup")
137+
.assertTextEquals("Start")
138138
switch
139139
.assertIsDisplayed()
140140
.assertIsOff()
@@ -144,19 +144,22 @@ class ServerSetupScreenTest : CoreComposeTest {
144144
stubUiState.update {
145145
it.copy(
146146
localOnnxCustomModel = true,
147-
localOnnxModels = it.localOnnxModels.withNewState(
148-
it.localOnnxModels.find { m -> m.id == LocalAiModel.CustomOnnx.id }!!.copy(
147+
scannedOnnxCustomModels = listOf(
148+
ServerSetupState.LocalModel(
149+
id = "custom_model",
150+
name = "Custom Model",
151+
size = "1GB",
152+
downloaded = true,
149153
selected = true,
150-
downloaded = true
151-
),
154+
)
152155
),
153156
)
154157
}
155158

156159
setupButton
157160
.assertIsDisplayed()
158161
.assertIsEnabled()
159-
.assertTextEquals("Setup")
162+
.assertTextEquals("Start")
160163
switch
161164
.assertIsDisplayed()
162165
.assertIsOn()
@@ -166,18 +169,14 @@ class ServerSetupScreenTest : CoreComposeTest {
166169
stubUiState.update {
167170
it.copy(
168171
localOnnxCustomModel = false,
169-
localOnnxModels = it.localOnnxModels.withNewState(
170-
it.localOnnxModels.find { m -> m.id == LocalAiModel.CustomOnnx.id }!!.copy(
171-
selected = false,
172-
),
173-
),
172+
scannedOnnxCustomModels = emptyList(),
174173
)
175174
}
176175

177176
setupButton
178177
.assertIsDisplayed()
179178
.assertIsNotEnabled()
180-
.assertTextEquals("Setup")
179+
.assertTextEquals("Start")
181180
switch
182181
.assertIsDisplayed()
183182
.assertIsOff()

0 commit comments

Comments
 (0)