|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Alexander Farber |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + * |
| 5 | + * This file is part of the OpenMapView project (https://github.com/afarber/OpenMapView) |
| 6 | + */ |
| 7 | + |
| 8 | +package de.afarber.openmapview |
| 9 | + |
| 10 | +import android.view.MotionEvent |
| 11 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 12 | +import androidx.test.platform.app.InstrumentationRegistry |
| 13 | +import org.junit.Assert.assertFalse |
| 14 | +import org.junit.Assert.assertTrue |
| 15 | +import org.junit.Before |
| 16 | +import org.junit.Test |
| 17 | +import org.junit.runner.RunWith |
| 18 | + |
| 19 | +@RunWith(AndroidJUnit4::class) |
| 20 | +class TouchGestureInstrumentationTest { |
| 21 | + private lateinit var openMapView: OpenMapView |
| 22 | + private lateinit var controller: MapController |
| 23 | + |
| 24 | + @Before |
| 25 | + fun setUp() { |
| 26 | + val context = InstrumentationRegistry.getInstrumentation().targetContext |
| 27 | + |
| 28 | + // Create and setup view on main thread to avoid Handler creation issues |
| 29 | + InstrumentationRegistry.getInstrumentation().runOnMainSync { |
| 30 | + openMapView = OpenMapView(context) |
| 31 | + openMapView.layout(0, 0, 1080, 1920) |
| 32 | + } |
| 33 | + |
| 34 | + controller = openMapView.getMapControllerForTesting() |
| 35 | + controller.setCenter(LatLng(51.4661, 7.2491)) |
| 36 | + controller.setZoom(10.0f) |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun testLongDrag_ShouldNotRegisterAsClick() { |
| 41 | + var clickReceived = false |
| 42 | + openMapView.setOnMapClickListener { clickReceived = true } |
| 43 | + |
| 44 | + val downTime = System.currentTimeMillis() |
| 45 | + |
| 46 | + // ACTION_DOWN at (100, 100) |
| 47 | + val down = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, 100f, 100f, 0) |
| 48 | + openMapView.onTouchEvent(down) |
| 49 | + down.recycle() |
| 50 | + |
| 51 | + // Simulate multiple MOVE events (drag 200 pixels) |
| 52 | + for (i in 1..10) { |
| 53 | + val move = |
| 54 | + MotionEvent.obtain( |
| 55 | + downTime, |
| 56 | + downTime + i * 10L, |
| 57 | + MotionEvent.ACTION_MOVE, |
| 58 | + 100f + i * 20f, |
| 59 | + 100f + i * 20f, |
| 60 | + 0, |
| 61 | + ) |
| 62 | + openMapView.onTouchEvent(move) |
| 63 | + move.recycle() |
| 64 | + } |
| 65 | + |
| 66 | + // ACTION_UP at (300, 300) - 200 pixels away from initial position |
| 67 | + val up = MotionEvent.obtain(downTime, downTime + 150, MotionEvent.ACTION_UP, 300f, 300f, 0) |
| 68 | + openMapView.onTouchEvent(up) |
| 69 | + up.recycle() |
| 70 | + |
| 71 | + // Should NOT have registered as a click (distance 200px > 10px threshold) |
| 72 | + assertFalse("Long drag should not register as click", clickReceived) |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun testSmallMovement_ShouldRegisterAsClick() { |
| 77 | + var clickReceived = false |
| 78 | + openMapView.setOnMapClickListener { clickReceived = true } |
| 79 | + |
| 80 | + val downTime = System.currentTimeMillis() |
| 81 | + |
| 82 | + // ACTION_DOWN at (100, 100) |
| 83 | + val down = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, 100f, 100f, 0) |
| 84 | + openMapView.onTouchEvent(down) |
| 85 | + down.recycle() |
| 86 | + |
| 87 | + // Small movement (3 pixels) |
| 88 | + val move = MotionEvent.obtain(downTime, downTime + 50, MotionEvent.ACTION_MOVE, 103f, 103f, 0) |
| 89 | + openMapView.onTouchEvent(move) |
| 90 | + move.recycle() |
| 91 | + |
| 92 | + // ACTION_UP at (103, 103) - only 3 pixels away |
| 93 | + val up = MotionEvent.obtain(downTime, downTime + 100, MotionEvent.ACTION_UP, 103f, 103f, 0) |
| 94 | + openMapView.onTouchEvent(up) |
| 95 | + up.recycle() |
| 96 | + |
| 97 | + // Should have registered as a click (distance 3px < 10px threshold) |
| 98 | + assertTrue("Small movement should register as click", clickReceived) |
| 99 | + } |
| 100 | + |
| 101 | + private fun OpenMapView.getMapControllerForTesting(): MapController { |
| 102 | + val field = OpenMapView::class.java.getDeclaredField("controller") |
| 103 | + field.isAccessible = true |
| 104 | + return field.get(this) as MapController |
| 105 | + } |
| 106 | +} |
0 commit comments