Skip to content

Commit 8c21286

Browse files
committed
Add tests
1 parent 8d1b90b commit 8c21286

5 files changed

Lines changed: 619 additions & 4 deletions

File tree

docs/TESTING_INSTRUMENTED.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This document explains instrumented testing for OpenMapView, including setup and
88

99
**Instrumented tests** run on an Android emulator or physical device. They provide access to real Android framework APIs and hardware.
1010

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.
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 (107 tests) cover logic, calculations, and vector shape management.
1212

1313
**CI Integration:** Instrumented tests run automatically daily at 09:45 UTC on GitHub Actions using the free Ubuntu runner with Android emulator.
1414

docs/TESTING_UNIT.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,17 @@ The `--continue` flag ensures all tests run even if some fail, useful for gettin
5050

5151
## Test Structure
5252

53-
### Current Test Coverage (76 tests)
53+
### Current Test Coverage (107 tests)
5454

5555
| Test Class | Tests | Description |
5656
|------------|-------|-------------|
5757
| **AttributionOverlayTest** | 4 | Attribution rendering and touch detection |
5858
| **BitmapDescriptorFactoryTest** | 7 | Marker icon generation with colors |
59-
| **DiskTileCacheTest** | - | Persistent disk cache with DiskLruCache |
60-
| **MapControllerTest** | 28 | Zoom, pan, marker management, touch detection |
59+
| **DiskTileCacheTest** | 4 | Persistent disk cache with DiskLruCache |
60+
| **MapControllerTest** | 40 | Zoom, pan, marker/polyline/polygon management, touch detection |
6161
| **MarkerTest** | 8 | Marker creation, equality, and properties |
62+
| **PolygonTest** | 12 | Polygon creation, validation, holes, and styling |
63+
| **PolylineTest** | 10 | Polyline creation, validation, and styling |
6264
| **ProjectionTest** | 12 | Web Mercator projection calculations |
6365
| **TileCacheTest** | 6 | Memory LRU cache behavior |
6466
| **TileDownloaderTest** | 2 | HTTP tile downloading with mocked Ktor client |
@@ -413,6 +415,8 @@ The test suite currently covers:
413415
- Core projection math (Web Mercator)
414416
- Tile coordinate calculations
415417
- Marker API and bitmap generation
418+
- Polyline and polygon data classes with validation
419+
- Vector shape management (add, remove, clear operations)
416420
- Memory cache (LRU) behavior
417421
- Disk cache (persistent storage)
418422
- Viewport calculation

openmapview/src/test/kotlin/de/afarber/openmapview/MapControllerTest.kt

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import android.graphics.Canvas
1212
import io.mockk.mockk
1313
import io.mockk.verify
1414
import org.junit.Assert.assertEquals
15+
import org.junit.Assert.assertFalse
1516
import org.junit.Assert.assertNotNull
1617
import org.junit.Assert.assertNull
1718
import org.junit.Assert.assertTrue
@@ -304,4 +305,181 @@ class MapControllerTest {
304305
controller.onPause()
305306
// Should not crash
306307
}
308+
309+
@Test
310+
fun testAddPolyline() {
311+
val points =
312+
listOf(
313+
LatLng(51.4661, 7.2491),
314+
LatLng(51.4700, 7.2550),
315+
)
316+
val polyline = Polyline(points = points)
317+
318+
val result = controller.addPolyline(polyline)
319+
320+
assertEquals(polyline, result)
321+
assertEquals(1, controller.getPolylines().size)
322+
assertEquals(polyline, controller.getPolylines()[0])
323+
}
324+
325+
@Test
326+
fun testRemovePolyline() {
327+
val points =
328+
listOf(
329+
LatLng(51.4661, 7.2491),
330+
LatLng(51.4700, 7.2550),
331+
)
332+
val polyline = Polyline(points = points)
333+
controller.addPolyline(polyline)
334+
335+
val result = controller.removePolyline(polyline)
336+
337+
assertTrue(result)
338+
assertEquals(0, controller.getPolylines().size)
339+
}
340+
341+
@Test
342+
fun testRemoveNonExistentPolyline() {
343+
val points =
344+
listOf(
345+
LatLng(51.4661, 7.2491),
346+
LatLng(51.4700, 7.2550),
347+
)
348+
val polyline = Polyline(points = points)
349+
350+
val result = controller.removePolyline(polyline)
351+
352+
assertFalse(result)
353+
}
354+
355+
@Test
356+
fun testClearPolylines() {
357+
val polyline1 = Polyline(listOf(LatLng(51.4661, 7.2491), LatLng(51.4700, 7.2550)))
358+
val polyline2 = Polyline(listOf(LatLng(51.4620, 7.2430), LatLng(51.4640, 7.2460)))
359+
controller.addPolyline(polyline1)
360+
controller.addPolyline(polyline2)
361+
362+
controller.clearPolylines()
363+
364+
assertEquals(0, controller.getPolylines().size)
365+
}
366+
367+
@Test
368+
fun testGetPolylines_ReturnsImmutableCopy() {
369+
val polyline = Polyline(listOf(LatLng(51.4661, 7.2491), LatLng(51.4700, 7.2550)))
370+
controller.addPolyline(polyline)
371+
372+
val polylines = controller.getPolylines()
373+
assertEquals(1, polylines.size)
374+
375+
// Original list should remain unchanged even if returned list is modified
376+
// (though returned list is immutable)
377+
}
378+
379+
@Test
380+
fun testAddMultiplePolylines() {
381+
val polyline1 = Polyline(listOf(LatLng(51.4661, 7.2491), LatLng(51.4700, 7.2550)))
382+
val polyline2 = Polyline(listOf(LatLng(51.4620, 7.2430), LatLng(51.4640, 7.2460)))
383+
val polyline3 = Polyline(listOf(LatLng(51.4680, 7.2520), LatLng(51.4690, 7.2530)))
384+
385+
controller.addPolyline(polyline1)
386+
controller.addPolyline(polyline2)
387+
controller.addPolyline(polyline3)
388+
389+
val polylines = controller.getPolylines()
390+
assertEquals(3, polylines.size)
391+
assertEquals(polyline1, polylines[0])
392+
assertEquals(polyline2, polylines[1])
393+
assertEquals(polyline3, polylines[2])
394+
}
395+
396+
@Test
397+
fun testAddPolygon() {
398+
val points =
399+
listOf(
400+
LatLng(51.4661, 7.2491),
401+
LatLng(51.4700, 7.2550),
402+
LatLng(51.4620, 7.2430),
403+
)
404+
val polygon = Polygon(points = points)
405+
406+
val result = controller.addPolygon(polygon)
407+
408+
assertEquals(polygon, result)
409+
assertEquals(1, controller.getPolygons().size)
410+
assertEquals(polygon, controller.getPolygons()[0])
411+
}
412+
413+
@Test
414+
fun testRemovePolygon() {
415+
val points =
416+
listOf(
417+
LatLng(51.4661, 7.2491),
418+
LatLng(51.4700, 7.2550),
419+
LatLng(51.4620, 7.2430),
420+
)
421+
val polygon = Polygon(points = points)
422+
controller.addPolygon(polygon)
423+
424+
val result = controller.removePolygon(polygon)
425+
426+
assertTrue(result)
427+
assertEquals(0, controller.getPolygons().size)
428+
}
429+
430+
@Test
431+
fun testRemoveNonExistentPolygon() {
432+
val points =
433+
listOf(
434+
LatLng(51.4661, 7.2491),
435+
LatLng(51.4700, 7.2550),
436+
LatLng(51.4620, 7.2430),
437+
)
438+
val polygon = Polygon(points = points)
439+
440+
val result = controller.removePolygon(polygon)
441+
442+
assertFalse(result)
443+
}
444+
445+
@Test
446+
fun testClearPolygons() {
447+
val polygon1 = Polygon(listOf(LatLng(51.4661, 7.2491), LatLng(51.4700, 7.2550), LatLng(51.4620, 7.2430)))
448+
val polygon2 = Polygon(listOf(LatLng(51.4640, 7.2420), LatLng(51.4660, 7.2440), LatLng(51.4650, 7.2450)))
449+
controller.addPolygon(polygon1)
450+
controller.addPolygon(polygon2)
451+
452+
controller.clearPolygons()
453+
454+
assertEquals(0, controller.getPolygons().size)
455+
}
456+
457+
@Test
458+
fun testGetPolygons_ReturnsImmutableCopy() {
459+
val polygon = Polygon(listOf(LatLng(51.4661, 7.2491), LatLng(51.4700, 7.2550), LatLng(51.4620, 7.2430)))
460+
controller.addPolygon(polygon)
461+
462+
val polygons = controller.getPolygons()
463+
assertEquals(1, polygons.size)
464+
465+
// Original list should remain unchanged even if returned list is modified
466+
// (though returned list is immutable)
467+
}
468+
469+
@Test
470+
fun testAddMultiplePolygons() {
471+
val polygon1 = Polygon(listOf(LatLng(51.4661, 7.2491), LatLng(51.4700, 7.2550), LatLng(51.4620, 7.2430)))
472+
val polygon2 = Polygon(listOf(LatLng(51.4640, 7.2420), LatLng(51.4660, 7.2440), LatLng(51.4650, 7.2450)))
473+
val polygon3 = Polygon(listOf(LatLng(51.4680, 7.2520), LatLng(51.4690, 7.2530), LatLng(51.4685, 7.2540)))
474+
475+
controller.addPolygon(polygon1)
476+
controller.addPolygon(polygon2)
477+
controller.addPolygon(polygon3)
478+
479+
val polygons = controller.getPolygons()
480+
assertEquals(3, polygons.size)
481+
assertEquals(polygon1, polygons[0])
482+
assertEquals(polygon2, polygons[1])
483+
assertEquals(polygon3, polygons[2])
484+
}
307485
}

0 commit comments

Comments
 (0)