|
2 | 2 |
|
3 | 3 | [Back to README](../README.md) |
4 | 4 |
|
5 | | -This document explains instrumented testing for OpenMapView. Currently, the project does not have instrumented tests, but this guide describes when and how to add them. |
| 5 | +This document explains instrumented testing for OpenMapView, including setup and the existing test suite. |
6 | 6 |
|
7 | 7 | ## Overview |
8 | 8 |
|
9 | 9 | **Instrumented tests** (also called **instrumentation tests** or **on-device tests**) run on an Android emulator or physical device. They provide access to real Android framework APIs and hardware. |
10 | 10 |
|
11 | | -**Current Status:** OpenMapView has **no instrumented tests** yet. All testing is done via [unit tests with Robolectric](TESTING_UNIT.md). |
| 11 | +**Current Status:** OpenMapView has **9 instrumented tests** (2 for TileDownloader, 7 for MapController) that test real rendering, network operations, and Canvas drawing. Unit tests with Robolectric (72 tests) cover logic and calculations. |
12 | 12 |
|
13 | 13 | ## Unit Tests vs Instrumented Tests |
14 | 14 |
|
@@ -49,60 +49,84 @@ Continue using unit tests (with Robolectric) for: |
49 | 49 | mkdir -p openmapview/src/androidTest/kotlin/de/afarber/openmapview |
50 | 50 | ``` |
51 | 51 |
|
52 | | -### 2. Add Dependencies |
| 52 | +### 2. Dependencies (Already Configured) |
53 | 53 |
|
54 | | -Update `openmapview/build.gradle.kts`: |
| 54 | +The project has instrumented testing dependencies configured in `openmapview/build.gradle.kts`: |
55 | 55 |
|
56 | 56 | ```kotlin |
57 | 57 | dependencies { |
58 | | - // Existing dependencies... |
59 | | - |
60 | | - // Instrumented testing |
61 | | - androidTestImplementation("androidx.test.ext:junit:1.1.5") |
62 | | - androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") |
63 | | - androidTestImplementation("androidx.test:runner:1.5.2") |
64 | | - androidTestImplementation("androidx.test:rules:1.5.0") |
| 58 | + // Instrumentation testing |
| 59 | + androidTestImplementation("androidx.test:core-ktx:1.6.1") |
| 60 | + androidTestImplementation("androidx.test:runner:1.6.2") |
| 61 | + androidTestImplementation("androidx.test:rules:1.6.1") |
| 62 | + androidTestImplementation("androidx.test.ext:junit-ktx:1.2.1") |
| 63 | + androidTestImplementation("junit:junit:4.13.2") |
| 64 | + androidTestImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.9.0") |
65 | 65 | } |
66 | 66 | ``` |
67 | 67 |
|
68 | | -### 3. Example Instrumented Test |
| 68 | +### 3. Current Instrumented Tests |
69 | 69 |
|
70 | | -Create `openmapview/src/androidTest/kotlin/de/afarber/openmapview/OpenMapViewInstrumentedTest.kt`: |
| 70 | +The project has instrumented tests in `openmapview/src/androidTest/kotlin/de/afarber/openmapview/`: |
71 | 71 |
|
| 72 | +**TileDownloaderInstrumentationTest.kt** (2 tests): |
72 | 73 | ```kotlin |
73 | | -package de.afarber.openmapview |
74 | | - |
75 | | -import android.content.Context |
76 | | -import androidx.test.core.app.ApplicationProvider |
77 | | -import androidx.test.ext.junit.runners.AndroidJUnit4 |
78 | | -import org.junit.Assert.assertEquals |
79 | | -import org.junit.Assert.assertNotNull |
80 | | -import org.junit.Test |
81 | | -import org.junit.runner.RunWith |
82 | | - |
83 | 74 | @RunWith(AndroidJUnit4::class) |
84 | | -class OpenMapViewInstrumentedTest { |
| 75 | +class TileDownloaderInstrumentationTest { |
85 | 76 | @Test |
86 | | - fun testOpenMapViewCreation() { |
87 | | - val context = ApplicationProvider.getApplicationContext<Context>() |
88 | | - val mapView = OpenMapView(context) |
| 77 | + fun testDownloadRealOsmTile() = runTest { |
| 78 | + val downloader = TileDownloader() |
| 79 | + val tileUrl = TileSource.STANDARD.getTileUrl(TileCoordinate(x = 0, y = 0, zoom = 0)) |
| 80 | + val result = downloader.downloadTile(tileUrl) |
| 81 | + |
| 82 | + assertNotNull("Should successfully download a real OSM tile", result) |
| 83 | + result?.let { |
| 84 | + assert(it.width > 0) { "Downloaded bitmap should have width > 0" } |
| 85 | + assert(it.height > 0) { "Downloaded bitmap should have height > 0" } |
| 86 | + } |
| 87 | + downloader.close() |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
89 | 91 |
|
90 | | - assertNotNull(mapView) |
91 | | - assertEquals(0, mapView.childCount) // FrameLayout with no children initially |
| 92 | +**MapControllerInstrumentationTest.kt** (7 tests): |
| 93 | +```kotlin |
| 94 | +@RunWith(AndroidJUnit4::class) |
| 95 | +class MapControllerInstrumentationTest { |
| 96 | + private lateinit var controller: MapController |
| 97 | + |
| 98 | + @Before |
| 99 | + fun setUp() { |
| 100 | + val context = InstrumentationRegistry.getInstrumentation().targetContext |
| 101 | + controller = MapController(context) |
| 102 | + controller.setViewSize(1080, 1920) |
92 | 103 | } |
93 | 104 |
|
94 | 105 | @Test |
95 | | - fun testBitmapRendering() { |
96 | | - // Test actual bitmap rendering with real Android framework |
97 | | - val bitmap = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED) |
| 106 | + fun testDraw_WithRealCanvas() { |
| 107 | + val bitmap = Bitmap.createBitmap(1080, 1920, Bitmap.Config.ARGB_8888) |
| 108 | + val canvas = Canvas(bitmap) |
| 109 | + |
| 110 | + controller.setCenter(LatLng(51.4661, 7.2491)) |
| 111 | + controller.setZoom(14.0) |
| 112 | + controller.draw(canvas) |
98 | 113 |
|
99 | 114 | assertNotNull(bitmap) |
100 | | - assertEquals(48, bitmap.width) |
101 | | - assertEquals(72, bitmap.height) |
| 115 | + assertTrue(bitmap.width == 1080) |
| 116 | + assertTrue(bitmap.height == 1920) |
| 117 | + } |
102 | 118 |
|
103 | | - // Verify actual pixel colors (not possible with Robolectric) |
104 | | - val centerPixel = bitmap.getPixel(24, 36) |
105 | | - // Assert red-ish color (exact value depends on marker design) |
| 119 | + @Test |
| 120 | + fun testDraw_WithMarkers() { |
| 121 | + val bitmap = Bitmap.createBitmap(1080, 1920, Bitmap.Config.ARGB_8888) |
| 122 | + val canvas = Canvas(bitmap) |
| 123 | + |
| 124 | + controller.setCenter(LatLng(51.4661, 7.2491)) |
| 125 | + controller.setZoom(14.0) |
| 126 | + controller.addMarker(Marker(LatLng(51.4661, 7.2491))) |
| 127 | + controller.draw(canvas) |
| 128 | + |
| 129 | + assertNotNull(bitmap) |
106 | 130 | } |
107 | 131 | } |
108 | 132 | ``` |
@@ -136,24 +160,28 @@ adb devices |
136 | 160 |
|
137 | 161 | ## Test Categories for OpenMapView |
138 | 162 |
|
139 | | -### Recommended Instrumented Tests |
| 163 | +### Current Instrumented Tests |
| 164 | + |
| 165 | +1. **Rendering Tests** (Implemented) |
| 166 | + - Real Canvas drawing with actual Bitmap |
| 167 | + - Marker rendering with real Android graphics |
| 168 | + - Zoom and pan rendering validation |
140 | 169 |
|
141 | | -1. **Rendering Tests** |
142 | | - - Verify tile rendering produces non-null bitmaps |
143 | | - - Check marker icon pixel colors |
144 | | - - Test canvas drawing operations |
| 170 | +2. **Network Tests** (Implemented) |
| 171 | + - Real OSM tile downloads |
| 172 | + - Network error handling |
145 | 173 |
|
146 | | -2. **Touch Gesture Tests** |
| 174 | +3. **Lifecycle Tests** (Implemented) |
| 175 | + - MapController lifecycle integration |
| 176 | + |
| 177 | +### Future Instrumented Tests |
| 178 | + |
| 179 | +1. **Touch Gesture Tests** |
147 | 180 | - Pan gesture recognition |
148 | 181 | - Pinch-to-zoom gesture |
149 | 182 | - Double-tap zoom |
150 | 183 |
|
151 | | -3. **Integration Tests** |
152 | | - - Full map initialization |
153 | | - - Tile downloading and caching |
154 | | - - Marker addition and rendering |
155 | | - |
156 | | -4. **Performance Tests** |
| 184 | +2. **Performance Tests** |
157 | 185 | - Measure frame rate during panning |
158 | 186 | - Memory usage with many markers |
159 | 187 | - Tile cache eviction behavior |
@@ -210,16 +238,16 @@ jobs: |
210 | 238 |
|
211 | 239 | ## Test Structure |
212 | 240 |
|
213 | | -Typical instrumented test structure: |
| 241 | +Current instrumented test structure: |
214 | 242 |
|
215 | 243 | ``` |
216 | 244 | openmapview/src/androidTest/kotlin/de/afarber/openmapview/ |
217 | | -├── OpenMapViewTest.kt # View creation and basic functionality |
218 | | -├── MapControllerRenderTest.kt # Rendering verification |
219 | | -├── GestureHandlingTest.kt # Touch gestures |
220 | | -└── MarkerIntegrationTest.kt # Marker display and interaction |
| 245 | +├── TileDownloaderInstrumentationTest.kt # Real OSM tile downloads (2 tests) |
| 246 | +└── MapControllerInstrumentationTest.kt # Canvas rendering and markers (7 tests) |
221 | 247 | ``` |
222 | 248 |
|
| 249 | +Total: 9 instrumented tests covering real Android framework behavior. |
| 250 | + |
223 | 251 | ## Espresso UI Testing |
224 | 252 |
|
225 | 253 | For testing UI interactions, use Espresso: |
@@ -255,16 +283,17 @@ class MapInteractionTest { |
255 | 283 | | **Instrumented** | Real Android, hardware access, accurate | Slow, requires device, CI complexity | |
256 | 284 | | **Hybrid** | Best of both worlds | More test code to maintain | |
257 | 285 |
|
258 | | -**Current OpenMapView approach:** Pure unit tests with Robolectric provide sufficient coverage for the current feature set. |
| 286 | +**Current OpenMapView approach:** Hybrid approach with 72 unit tests (Robolectric) for logic and 9 instrumented tests for real Android framework validation. |
259 | 287 |
|
260 | | -## Future Considerations |
| 288 | +## Expanding Instrumented Tests |
261 | 289 |
|
262 | | -Add instrumented tests when: |
| 290 | +Consider adding more instrumented tests when: |
263 | 291 |
|
264 | 292 | - Users report device-specific rendering issues |
265 | | -- Adding complex gesture handling |
266 | | -- Implementing hardware-dependent features |
| 293 | +- Adding complex gesture handling (currently tested via unit tests) |
| 294 | +- Implementing hardware-dependent features (GPS, sensors) |
267 | 295 | - Validating performance on low-end devices |
| 296 | +- Testing pixel-perfect rendering accuracy |
268 | 297 |
|
269 | 298 | ## References |
270 | 299 |
|
|
0 commit comments