|
| 1 | +@file:Suppress("PrivatePropertyName", "MaxLineLength") |
| 2 | + |
| 3 | +package com.mapbox.navigation.dropin.component.camera |
| 4 | + |
| 5 | +import android.app.Service |
| 6 | +import android.content.Context |
| 7 | +import android.view.LayoutInflater |
| 8 | +import android.view.View |
| 9 | +import android.view.View.OnLayoutChangeListener |
| 10 | +import android.widget.FrameLayout |
| 11 | +import androidx.test.core.app.ApplicationProvider |
| 12 | +import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI |
| 13 | +import com.mapbox.navigation.dropin.R |
| 14 | +import com.mapbox.navigation.dropin.databinding.MapboxNavigationViewLayoutBinding |
| 15 | +import com.mapbox.navigation.dropin.util.TestStore |
| 16 | +import com.mapbox.navigation.testing.MainCoroutineRule |
| 17 | +import com.mapbox.navigation.ui.app.internal.camera.CameraAction |
| 18 | +import com.mapbox.navigation.ui.app.internal.navigation.NavigationState |
| 19 | +import io.mockk.mockk |
| 20 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 21 | +import kotlinx.coroutines.coroutineScope |
| 22 | +import kotlinx.coroutines.launch |
| 23 | +import kotlinx.coroutines.suspendCancellableCoroutine |
| 24 | +import org.junit.Assert.assertEquals |
| 25 | +import org.junit.Assert.assertNotEquals |
| 26 | +import org.junit.Before |
| 27 | +import org.junit.Rule |
| 28 | +import org.junit.Test |
| 29 | +import org.junit.runner.RunWith |
| 30 | +import org.robolectric.RobolectricTestRunner |
| 31 | +import org.robolectric.annotation.Config |
| 32 | +import kotlin.coroutines.resume |
| 33 | + |
| 34 | +@OptIn(ExperimentalPreviewMapboxNavigationAPI::class, ExperimentalCoroutinesApi::class) |
| 35 | +@RunWith(RobolectricTestRunner::class) |
| 36 | +class CameraLayoutObserverTest { |
| 37 | + |
| 38 | + @get:Rule |
| 39 | + val coroutineRule = MainCoroutineRule() |
| 40 | + |
| 41 | + private lateinit var store: TestStore |
| 42 | + private lateinit var binding: MapboxNavigationViewLayoutBinding |
| 43 | + private lateinit var mapView: View |
| 44 | + private lateinit var sut: CameraLayoutObserver |
| 45 | + |
| 46 | + @Before |
| 47 | + fun setUp() { |
| 48 | + val context: Context = ApplicationProvider.getApplicationContext() |
| 49 | + binding = MapboxNavigationViewLayoutBinding.inflate( |
| 50 | + context.getSystemService(Service.LAYOUT_INFLATER_SERVICE) as LayoutInflater, |
| 51 | + FrameLayout(context) |
| 52 | + ).apply { |
| 53 | + // slightly modifying default layout to simulate layout changes |
| 54 | + guidanceLayout.bottom = 100 |
| 55 | + roadNameLayout.top = 100 |
| 56 | + actionListLayout.left = 100 |
| 57 | + infoPanelLayout.right = 100 |
| 58 | + } |
| 59 | + mapView = View(context) |
| 60 | + store = TestStore() |
| 61 | + sut = CameraLayoutObserver(store, mapView, binding) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @Config(qualifiers = "port") |
| 66 | + fun `portrait - should update bottom padding for DestinationPreview, FreeDrive and RoutePreview state`() = |
| 67 | + coroutineRule.runBlockingTest { |
| 68 | + sut.onAttached(mockk()) |
| 69 | + |
| 70 | + triggerLayoutChangesAndVerifyDispatchedActions( |
| 71 | + givenNavigationStates = listOf( |
| 72 | + NavigationState.DestinationPreview, |
| 73 | + NavigationState.FreeDrive, |
| 74 | + NavigationState.RoutePreview, |
| 75 | + ) |
| 76 | + ) { state, action -> |
| 77 | + assertEquals("$state|top", action.padding.top, PADDING_V_PORT, 0.001) |
| 78 | + assertNotEquals("$state|bottom", action.padding.bottom, PADDING_V_PORT, 0.001) |
| 79 | + assertEquals("$state|left", action.padding.left, PADDING_H_PORT, 0.001) |
| 80 | + assertEquals("$state|right", action.padding.right, PADDING_H_PORT, 0.001) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + @Config(qualifiers = "port") |
| 86 | + fun `portrait - should update top and bottom padding for ActiveNavigation and Arrival state`() = |
| 87 | + coroutineRule.runBlockingTest { |
| 88 | + sut.onAttached(mockk()) |
| 89 | + |
| 90 | + triggerLayoutChangesAndVerifyDispatchedActions( |
| 91 | + givenNavigationStates = listOf( |
| 92 | + NavigationState.ActiveNavigation, |
| 93 | + NavigationState.Arrival, |
| 94 | + ) |
| 95 | + ) { s, action -> |
| 96 | + assertNotEquals("$s|top", action.padding.top, PADDING_V_PORT, 0.001) |
| 97 | + assertNotEquals("$s|bottom", action.padding.bottom, PADDING_V_PORT, 0.001) |
| 98 | + assertEquals("$s|left", action.padding.left, PADDING_H_PORT, 0.001) |
| 99 | + assertEquals("$s|right", action.padding.right, PADDING_H_PORT, 0.001) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @Config(qualifiers = "land") |
| 105 | + fun `landscape - should update bottom padding for FreeDrive state`() = |
| 106 | + coroutineRule.runBlockingTest { |
| 107 | + sut.onAttached(mockk()) |
| 108 | + |
| 109 | + triggerLayoutChangesAndVerifyDispatchedActions( |
| 110 | + givenNavigationStates = listOf( |
| 111 | + NavigationState.FreeDrive |
| 112 | + ) |
| 113 | + ) { s, action -> |
| 114 | + assertEquals("$s|top", action.padding.top, PADDING_V_LAND, 0.001) |
| 115 | + assertNotEquals("$s|bottom", action.padding.bottom, PADDING_V_LAND, 0.001) |
| 116 | + assertEquals("$s|left", action.padding.left, PADDING_H_LAND, 0.001) |
| 117 | + assertEquals("$s|right", action.padding.right, PADDING_H_LAND, 0.001) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + @Config(qualifiers = "land") |
| 123 | + fun `landscape - should update left, bottom and right padding for all states except FreeDrive`() = |
| 124 | + coroutineRule.runBlockingTest { |
| 125 | + sut.onAttached(mockk()) |
| 126 | + |
| 127 | + triggerLayoutChangesAndVerifyDispatchedActions( |
| 128 | + givenNavigationStates = listOf( |
| 129 | + NavigationState.DestinationPreview, |
| 130 | + NavigationState.RoutePreview, |
| 131 | + NavigationState.ActiveNavigation, |
| 132 | + NavigationState.Arrival, |
| 133 | + ) |
| 134 | + ) { s, action -> |
| 135 | + assertEquals("$s|top", action.padding.top, PADDING_V_LAND, 0.001) |
| 136 | + assertNotEquals("$s|bottom", action.padding.bottom, PADDING_V_LAND, 0.001) |
| 137 | + assertNotEquals("$s|left", action.padding.left, PADDING_H_LAND, 0.001) |
| 138 | + assertNotEquals("$s|right", action.padding.right, PADDING_H_LAND, 0.001) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private suspend fun triggerLayoutChangesAndVerifyDispatchedActions( |
| 143 | + givenNavigationStates: List<NavigationState>, |
| 144 | + assertAction: (state: NavigationState, action: CameraAction.UpdatePadding) -> Unit |
| 145 | + ) { |
| 146 | + givenNavigationStates.forEachIndexed { index, navState -> |
| 147 | + store.updateState { it.copy(navigation = navState) } |
| 148 | + binding.coordinatorLayout.triggerLayoutChange() |
| 149 | + assertAction(navState, store.actions[index] as CameraAction.UpdatePadding) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private suspend fun View.triggerLayoutChange() = coroutineScope { |
| 154 | + launch { |
| 155 | + waitForLayoutChange() |
| 156 | + } |
| 157 | + layout(left + 1, 0, 0, 0) |
| 158 | + // this coroutine scope will wait for all launched coroutines to finish before returning |
| 159 | + } |
| 160 | + |
| 161 | + private suspend fun View.waitForLayoutChange() = suspendCancellableCoroutine<Unit> { cont -> |
| 162 | + addOnLayoutChangeListener(object : OnLayoutChangeListener { |
| 163 | + override fun onLayoutChange( |
| 164 | + v: View?, |
| 165 | + left: Int, |
| 166 | + top: Int, |
| 167 | + right: Int, |
| 168 | + bottom: Int, |
| 169 | + oldLeft: Int, |
| 170 | + oldTop: Int, |
| 171 | + oldRight: Int, |
| 172 | + oldBottom: Int |
| 173 | + ) { |
| 174 | + cont.resume(Unit) |
| 175 | + removeOnLayoutChangeListener(this) |
| 176 | + } |
| 177 | + }) |
| 178 | + } |
| 179 | + |
| 180 | + private val PADDING_V_PORT: Double |
| 181 | + get() = binding.root.resources |
| 182 | + .getDimensionPixelSize(R.dimen.mapbox_camera_overview_padding_v).toDouble() |
| 183 | + private val PADDING_H_PORT: Double |
| 184 | + get() = binding.root.resources |
| 185 | + .getDimensionPixelSize(R.dimen.mapbox_camera_overview_padding_h).toDouble() |
| 186 | + private val PADDING_V_LAND: Double |
| 187 | + get() = binding.root.resources |
| 188 | + .getDimensionPixelSize(R.dimen.mapbox_camera_overview_padding_landscape_v).toDouble() |
| 189 | + private val PADDING_H_LAND: Double |
| 190 | + get() = binding.root.resources |
| 191 | + .getDimensionPixelSize(R.dimen.mapbox_camera_overview_padding_landscape_h).toDouble() |
| 192 | +} |
0 commit comments