|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.maps.android |
| 18 | + |
| 19 | +import com.google.android.gms.maps.model.LatLng |
| 20 | +import com.google.android.gms.maps.model.Marker |
| 21 | +import com.google.maps.android.ui.AnimationUtil |
| 22 | +import io.mockk.* |
| 23 | +import org.junit.After |
| 24 | +import org.junit.Before |
| 25 | +import org.junit.Test |
| 26 | +import org.junit.runner.RunWith |
| 27 | +import org.robolectric.RobolectricTestRunner |
| 28 | +import org.robolectric.Shadows |
| 29 | +import java.util.concurrent.TimeUnit |
| 30 | +import kotlin.test.assertEquals |
| 31 | + |
| 32 | +@RunWith(RobolectricTestRunner::class) |
| 33 | +class AnimationUtilTest { |
| 34 | + |
| 35 | + private lateinit var marker: Marker |
| 36 | + private lateinit var currentPosition: LatLng |
| 37 | + |
| 38 | + @Before |
| 39 | + fun setUp() { |
| 40 | + marker = mockk(relaxed = true) |
| 41 | + |
| 42 | + // Initial position |
| 43 | + currentPosition = LatLng(0.0, 0.0) |
| 44 | + |
| 45 | + // Mock the marker position getter and setter |
| 46 | + every { marker.position } answers { currentPosition } |
| 47 | + every { marker.setPosition(any()) } answers { |
| 48 | + currentPosition = firstArg() |
| 49 | + Unit |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @After |
| 54 | + fun tearDown() { |
| 55 | + unmockkAll() |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `animateMarkerTo moves marker to final position with a buffer tolerance`() { |
| 60 | + val finalPosition = LatLng(10.0, 10.0) |
| 61 | + val durationMs = 100L |
| 62 | + |
| 63 | + // Start the animation |
| 64 | + AnimationUtil.animateMarkerTo(marker, finalPosition, durationMs) |
| 65 | + |
| 66 | + val mainLooper = Shadows.shadowOf(android.os.Looper.getMainLooper()) |
| 67 | + |
| 68 | + // Simulate time passing in 16ms increments until we exceed the animation duration |
| 69 | + var timePassed = 0L |
| 70 | + while (timePassed <= durationMs + 100) { // Allowing a little buffer for completion |
| 71 | + mainLooper.idleFor(16, TimeUnit.MILLISECONDS) |
| 72 | + timePassed += 16 |
| 73 | + } |
| 74 | + |
| 75 | + // Check the final position — allowing a reasonable tolerance (0.5 or more) |
| 76 | + assertEquals(10.0, currentPosition.latitude, 0.5) // 0.5 tolerance |
| 77 | + assertEquals(10.0, currentPosition.longitude, 0.5) // 0.5 tolerance |
| 78 | + } |
| 79 | +} |
0 commit comments