|
1 | 1 | package com.maxmind.device.collector |
2 | 2 |
|
3 | 3 | 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 |
4 | 15 | import io.mockk.mockk |
| 16 | +import io.mockk.verify |
5 | 17 | import org.junit.jupiter.api.Assertions.assertNotNull |
| 18 | +import org.junit.jupiter.api.BeforeEach |
6 | 19 | import org.junit.jupiter.api.Test |
7 | 20 |
|
8 | 21 | /** |
9 | | - * Tests for DeviceDataCollector. |
| 22 | + * Tests for [DeviceDataCollector]. |
10 | 23 | * |
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.). |
13 | 27 | */ |
14 | 28 | 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 | + } |
15 | 90 |
|
16 | 91 | @Test |
17 | 92 | internal fun `collector can be instantiated with context`() { |
18 | | - val mockContext = mockk<Context>(relaxed = true) |
19 | 93 | val collector = DeviceDataCollector(mockContext) |
20 | 94 | assertNotNull(collector) |
21 | 95 | } |
| 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 | + } |
22 | 136 | } |
0 commit comments