Skip to content

Commit 97cce0a

Browse files
oschwaldclaude
andcommitted
test: Improve DeviceDataCollectorTest with helper injection tests
Expand DeviceDataCollectorTest to verify: - Instantiation with context - Instantiation with custom helper classes - enableLogging parameter acceptance - Helper injection without immediate invocation Note: Full collect() method testing requires instrumented tests due to Android framework dependencies in dedicated collectors (GPU, Camera, etc.). The helper classes enable isolated testing of the orchestration logic. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 932a8d7 commit 97cce0a

File tree

1 file changed

+118
-4
lines changed

1 file changed

+118
-4
lines changed
Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,136 @@
11
package com.maxmind.device.collector
22

33
import android.content.Context
4+
import com.maxmind.device.collector.helper.BuildInfoHelper
5+
import com.maxmind.device.collector.helper.DisplayInfoHelper
6+
import com.maxmind.device.collector.helper.HardwareInfoHelper
7+
import com.maxmind.device.collector.helper.InstallationInfoHelper
8+
import com.maxmind.device.collector.helper.LocaleInfoHelper
9+
import com.maxmind.device.model.BuildInfo
10+
import com.maxmind.device.model.DisplayInfo
11+
import com.maxmind.device.model.HardwareInfo
12+
import com.maxmind.device.model.InstallationInfo
13+
import com.maxmind.device.model.LocaleInfo
14+
import io.mockk.every
415
import io.mockk.mockk
16+
import io.mockk.verify
517
import org.junit.jupiter.api.Assertions.assertNotNull
18+
import org.junit.jupiter.api.BeforeEach
619
import org.junit.jupiter.api.Test
720

821
/**
9-
* Tests for DeviceDataCollector.
22+
* Tests for [DeviceDataCollector].
1023
*
11-
* Full integration testing of collect() requires instrumented tests
12-
* due to Android framework static fields (Build.*, etc.).
24+
* These tests verify the helper injection and instantiation.
25+
* Full integration tests require instrumented tests due to
26+
* Android framework dependencies in dedicated collectors (GPU, Camera, etc.).
1327
*/
1428
internal class DeviceDataCollectorTest {
29+
private lateinit var mockContext: Context
30+
private lateinit var mockBuildInfoHelper: BuildInfoHelper
31+
private lateinit var mockDisplayInfoHelper: DisplayInfoHelper
32+
private lateinit var mockHardwareInfoHelper: HardwareInfoHelper
33+
private lateinit var mockInstallationInfoHelper: InstallationInfoHelper
34+
private lateinit var mockLocaleInfoHelper: LocaleInfoHelper
35+
36+
private val testBuildInfo = BuildInfo(
37+
fingerprint = "test-fingerprint",
38+
manufacturer = "Test",
39+
model = "TestModel",
40+
brand = "TestBrand",
41+
device = "testdevice",
42+
product = "testproduct",
43+
board = "testboard",
44+
hardware = "testhardware",
45+
osVersion = "14",
46+
sdkVersion = 34,
47+
)
48+
49+
private val testDisplayInfo = DisplayInfo(
50+
widthPixels = 1080,
51+
heightPixels = 2400,
52+
densityDpi = 440,
53+
density = 2.75f,
54+
)
55+
56+
private val testHardwareInfo = HardwareInfo(
57+
cpuCores = 8,
58+
totalMemoryBytes = 8_000_000_000L,
59+
totalStorageBytes = 128_000_000_000L,
60+
)
61+
62+
private val testInstallationInfo = InstallationInfo(
63+
firstInstallTime = 1700000000000L,
64+
lastUpdateTime = 1700100000000L,
65+
versionCode = 10,
66+
)
67+
68+
private val testLocaleInfo = LocaleInfo(
69+
language = "en",
70+
country = "US",
71+
timezone = "America/New_York",
72+
)
73+
74+
@BeforeEach
75+
internal fun setUp() {
76+
mockContext = mockk(relaxed = true)
77+
mockBuildInfoHelper = mockk(relaxed = true)
78+
mockDisplayInfoHelper = mockk(relaxed = true)
79+
mockHardwareInfoHelper = mockk(relaxed = true)
80+
mockInstallationInfoHelper = mockk(relaxed = true)
81+
mockLocaleInfoHelper = mockk(relaxed = true)
82+
83+
// Setup default returns
84+
every { mockBuildInfoHelper.collect() } returns testBuildInfo
85+
every { mockDisplayInfoHelper.collect() } returns testDisplayInfo
86+
every { mockHardwareInfoHelper.collect() } returns testHardwareInfo
87+
every { mockInstallationInfoHelper.collect() } returns testInstallationInfo
88+
every { mockLocaleInfoHelper.collect() } returns testLocaleInfo
89+
}
1590

1691
@Test
1792
internal fun `collector can be instantiated with context`() {
18-
val mockContext = mockk<Context>(relaxed = true)
1993
val collector = DeviceDataCollector(mockContext)
2094
assertNotNull(collector)
2195
}
96+
97+
@Test
98+
internal fun `collector can be instantiated with custom helpers`() {
99+
val collector = DeviceDataCollector(
100+
context = mockContext,
101+
buildInfoHelper = mockBuildInfoHelper,
102+
displayInfoHelper = mockDisplayInfoHelper,
103+
hardwareInfoHelper = mockHardwareInfoHelper,
104+
installationInfoHelper = mockInstallationInfoHelper,
105+
localeInfoHelper = mockLocaleInfoHelper,
106+
)
107+
assertNotNull(collector)
108+
}
109+
110+
@Test
111+
internal fun `collector accepts enableLogging parameter`() {
112+
val collector = DeviceDataCollector(
113+
context = mockContext,
114+
enableLogging = true,
115+
)
116+
assertNotNull(collector)
117+
}
118+
119+
@Test
120+
internal fun `helpers are called during instantiation verification`() {
121+
// Just verify the helpers can be set up - they don't get called until collect()
122+
val collector = DeviceDataCollector(
123+
context = mockContext,
124+
buildInfoHelper = mockBuildInfoHelper,
125+
displayInfoHelper = mockDisplayInfoHelper,
126+
hardwareInfoHelper = mockHardwareInfoHelper,
127+
installationInfoHelper = mockInstallationInfoHelper,
128+
localeInfoHelper = mockLocaleInfoHelper,
129+
)
130+
131+
// Verify helpers were injected (not yet called)
132+
assertNotNull(collector)
133+
verify(exactly = 0) { mockBuildInfoHelper.collect() }
134+
verify(exactly = 0) { mockDisplayInfoHelper.collect() }
135+
}
22136
}

0 commit comments

Comments
 (0)