diff --git a/app/src/main/java/me/hufman/androidautoidrive/carapp/music/TextScroller.kt b/app/src/main/java/me/hufman/androidautoidrive/carapp/music/TextScroller.kt index 79baaf96a..2d4d8a759 100644 --- a/app/src/main/java/me/hufman/androidautoidrive/carapp/music/TextScroller.kt +++ b/app/src/main/java/me/hufman/androidautoidrive/carapp/music/TextScroller.kt @@ -3,7 +3,7 @@ package me.hufman.androidautoidrive.carapp.music /** * Wrapper around a string that handles scrolling when it is too long to fit on a line. */ -class TextScroller(private val originalText: String, private val maxLineLength: Int) { +class TextScroller(private val originalText: String, private val maxLineLength: Int, private val _getTime: () -> Long = {System.currentTimeMillis()}) { companion object { const val SCROLL_COOLDOWN_SECONDS = 2 const val INDEX_JUMP_VALUE = 3 @@ -16,7 +16,7 @@ class TextScroller(private val originalText: String, private val maxLineLength: private var previousTimestamp: Long init { - previousTimestamp = System.currentTimeMillis() + previousTimestamp = _getTime() shouldScroll = originalText.length > maxLineLength } @@ -41,9 +41,9 @@ class TextScroller(private val originalText: String, private val maxLineLength: } else { scrollText = false startIndex = 0 - previousTimestamp = System.currentTimeMillis() + previousTimestamp = _getTime() } - } else if ((System.currentTimeMillis() - previousTimestamp) / 1000 >= SCROLL_COOLDOWN_SECONDS) { + } else if ((_getTime() - previousTimestamp) / 1000 >= SCROLL_COOLDOWN_SECONDS) { scrollText = true } return displayText diff --git a/app/src/test/java/me/hufman/androidautoidrive/music/SpotifyAuthStateManagerTest.kt b/app/src/test/java/me/hufman/androidautoidrive/music/SpotifyAuthStateManagerTest.kt index e4fb9b4ee..d08996906 100644 --- a/app/src/test/java/me/hufman/androidautoidrive/music/SpotifyAuthStateManagerTest.kt +++ b/app/src/test/java/me/hufman/androidautoidrive/music/SpotifyAuthStateManagerTest.kt @@ -13,6 +13,7 @@ import org.junit.Assert.* import org.junit.Before import org.junit.Test import org.junit.runner.RunWith +import org.mockito.Mockito import org.powermock.api.mockito.PowerMockito import org.powermock.core.classloader.annotations.PrepareForTest import org.powermock.modules.junit4.PowerMockRunner @@ -42,10 +43,11 @@ class SpotifyAuthStateManagerTest { appSettings[AppSettings.KEYS.SPOTIFY_AUTH_STATE_JSON] = state val authState = AuthState() - PowerMockito.mockStatic(AuthState::class.java) - PowerMockito.`when`(AuthState.jsonDeserialize(state)).thenAnswer { authState } + Mockito.mockStatic(AuthState::class.java).use { + it.`when` {AuthState.jsonDeserialize(state)}.thenReturn(authState) - spotifyAuthStateManager = Whitebox.invokeConstructor(SpotifyAuthStateManager::class.java, appSettings) + spotifyAuthStateManager = Whitebox.invokeConstructor(SpotifyAuthStateManager::class.java, appSettings) + } assertEquals(authState, spotifyAuthStateManager.currentState) } @@ -55,10 +57,11 @@ class SpotifyAuthStateManagerTest { val state = "authStateJson" appSettings[AppSettings.KEYS.SPOTIFY_AUTH_STATE_JSON] = state - PowerMockito.mockStatic(AuthState::class.java) - PowerMockito.`when`(AuthState.jsonDeserialize(state)).thenAnswer { throw JSONException("message") } + Mockito.mockStatic(AuthState::class.java).use { + it.`when` {AuthState.jsonDeserialize(state)}.thenThrow(JSONException("message")) - spotifyAuthStateManager = Whitebox.invokeConstructor(SpotifyAuthStateManager::class.java, appSettings) + spotifyAuthStateManager = Whitebox.invokeConstructor(SpotifyAuthStateManager::class.java, appSettings) + } assertTrue(isEmptyAuthState(spotifyAuthStateManager.currentState)) } @@ -106,15 +109,10 @@ class SpotifyAuthStateManagerTest { @Test fun testGetAccessTokenExpirationIn_ExistingAccessTokenExpirationTime() { - val expirationTime = 5000L - val currentTime = 3000L val currentState: AuthState = mock() - whenever(currentState.accessTokenExpirationTime).thenReturn(expirationTime) + whenever(currentState.accessTokenExpirationTime).thenReturn(System.currentTimeMillis() + 2999) Whitebox.setInternalState(spotifyAuthStateManager, "currentState", currentState) - PowerMockito.mockStatic(System::class.java) - PowerMockito.`when`(System.currentTimeMillis()).doAnswer { currentTime } - assertEquals(2, spotifyAuthStateManager.getAccessTokenExpirationIn()) } diff --git a/app/src/test/java/me/hufman/androidautoidrive/music/SpotifyWebApiTest.kt b/app/src/test/java/me/hufman/androidautoidrive/music/SpotifyWebApiTest.kt index 314b5a5a6..c32bc95b4 100644 --- a/app/src/test/java/me/hufman/androidautoidrive/music/SpotifyWebApiTest.kt +++ b/app/src/test/java/me/hufman/androidautoidrive/music/SpotifyWebApiTest.kt @@ -33,6 +33,7 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.ArgumentMatchers.anyInt +import org.mockito.Mockito import org.powermock.api.mockito.PowerMockito import org.powermock.core.classloader.annotations.PrepareForTest import org.powermock.modules.junit4.PowerMockRunner @@ -428,8 +429,9 @@ class SpotifyWebApiTest { whenever(context.getString(any())).thenReturn(getStringText) val contentIntent: PendingIntent = mock() - PowerMockito.mockStatic(PendingIntent::class.java) - PowerMockito.`when`(PendingIntent.getActivity(context, SpotifyWebApi.NOTIFICATION_REQ_ID, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)).thenAnswer { contentIntent } + Mockito.mockStatic(PendingIntent::class.java).`when` { + PendingIntent.getActivity(context, SpotifyWebApi.NOTIFICATION_REQ_ID, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) + }.doReturn(contentIntent) val notification: Notification = mock() val notificationBuilder: NotificationCompat.Builder = mock() diff --git a/app/src/test/java/me/hufman/androidautoidrive/music/TextScrollerTest.kt b/app/src/test/java/me/hufman/androidautoidrive/music/TextScrollerTest.kt index b8da777d9..6903df432 100644 --- a/app/src/test/java/me/hufman/androidautoidrive/music/TextScrollerTest.kt +++ b/app/src/test/java/me/hufman/androidautoidrive/music/TextScrollerTest.kt @@ -1,27 +1,17 @@ package me.hufman.androidautoidrive.music -import org.mockito.kotlin.* import me.hufman.androidautoidrive.carapp.music.TextScroller import org.junit.Assert.* import org.junit.Test -import org.junit.runner.RunWith -import org.powermock.api.mockito.PowerMockito -import org.powermock.core.classloader.annotations.PrepareForTest -import org.powermock.modules.junit4.PowerMockRunner -@RunWith(PowerMockRunner::class) -@PrepareForTest(System::class, TextScroller::class) class TextScrollerTest { val longText = "This is a long text segment that will scroll" @Test fun testGetText_ShouldNotScroll() { val shortText = "some text" - PowerMockito.mockStatic(System::class.java) - PowerMockito.`when`(System.currentTimeMillis()) - .doAnswer { 0L } // initialization - val textScroller = TextScroller(shortText, 30) + val textScroller = TextScroller(shortText, 30) { 0 } assertEquals(shortText, textScroller.getText()) assertEquals(shortText, textScroller.getText()) @@ -29,12 +19,8 @@ class TextScrollerTest { @Test fun testGetText_TimeElapsedLessThanScrollCooldown() { - PowerMockito.mockStatic(System::class.java) - PowerMockito.`when`(System.currentTimeMillis()) - .doAnswer { 0L } // initialization - .doAnswer { 1000L } // elapsed time - .doAnswer { 1001L } // elapsed time - val textScroller = TextScroller(longText, 40) + val times = listOf(0L, 1000L, 1001L).iterator() + val textScroller = TextScroller(longText, 40) { times.next() } assertEquals(longText, textScroller.getText()) assertEquals(longText, textScroller.getText()) @@ -42,12 +28,8 @@ class TextScrollerTest { @Test fun testGetText_TimeElapsedAfterScrollCooldown() { - PowerMockito.mockStatic(System::class.java) - PowerMockito.`when`(System.currentTimeMillis()) - .doAnswer { 0L } // initialization - .doAnswer { 10000L } // elapsed time - .doAnswer { 10001L } // elapsed time - val textScroller = TextScroller(longText, 40) + val times = listOf(0L, 10000L, 10001L).iterator() + val textScroller = TextScroller(longText, 40) { times.next() } textScroller.getText() assertEquals(longText, textScroller.getText()) @@ -57,11 +39,8 @@ class TextScrollerTest { @Test fun testGetText_TextScroll_SliceHasNotReachedEndOfOriginalText() { - PowerMockito.mockStatic(System::class.java) - PowerMockito.`when`(System.currentTimeMillis()) - .doAnswer { 0L } // initialization - .doAnswer { 10000L } // elapsed time - val textScroller = TextScroller(longText, 40) + val times = listOf(0L, 10000L).iterator() + val textScroller = TextScroller(longText, 40) { times.next() } textScroller.getText() assertEquals(longText, textScroller.getText()) @@ -71,13 +50,12 @@ class TextScrollerTest { @Test fun testGetText_TextScroll_SliceReachedEndOfOriginalText() { - PowerMockito.mockStatic(System::class.java) - PowerMockito.`when`(System.currentTimeMillis()) - .doAnswer { 0L } // initialization - .doAnswer { 10000L } // elapsed time - .doAnswer { 20000L } // new previous timestamp - .doAnswer { 21000L } // elapsed time - val textScroller = TextScroller(longText, 40) + val times = listOf(0L, // initialization + 10000L, // elapsed time + 20000L, // new previous timestamp + 21000L // elapsed time + ).iterator() + val textScroller = TextScroller(longText, 40) { times.next() } textScroller.getText() textScroller.getText() textScroller.getText() diff --git a/build.gradle b/build.gradle index 614ecbce9..4c2d66031 100644 --- a/build.gradle +++ b/build.gradle @@ -49,7 +49,7 @@ allprojects { // https://stackoverflow.com/a/76267308/169035 subprojects{ tasks.withType(Test).configureEach{ - jvmArgs = jvmArgs + ['--add-opens=java.base/java.lang=ALL-UNNAMED', '--add-opens=java.base/java.lang.reflect=ALL-UNNAMED'] + jvmArgs = jvmArgs + ['--add-opens=java.base/java.lang=ALL-UNNAMED', '--add-opens=java.base/java.lang.reflect=ALL-UNNAMED', '--add-opens=java.base/java.util=ALL-UNNAMED'] } }