Skip to content

Commit

Permalink
Coaxes PowerMock to keep working with new Mockito
Browse files Browse the repository at this point in the history
  • Loading branch information
hufman committed Feb 19, 2024
1 parent 133da7a commit 62f76ac
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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> {AuthState.jsonDeserialize(state)}.thenReturn(authState)

spotifyAuthStateManager = Whitebox.invokeConstructor(SpotifyAuthStateManager::class.java, appSettings)
spotifyAuthStateManager = Whitebox.invokeConstructor(SpotifyAuthStateManager::class.java, appSettings)
}

assertEquals(authState, spotifyAuthStateManager.currentState)
}
Expand All @@ -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> {AuthState.jsonDeserialize(state)}.thenThrow(JSONException("message"))

spotifyAuthStateManager = Whitebox.invokeConstructor(SpotifyAuthStateManager::class.java, appSettings)
spotifyAuthStateManager = Whitebox.invokeConstructor(SpotifyAuthStateManager::class.java, appSettings)
}

assertTrue(isEmptyAuthState(spotifyAuthStateManager.currentState))
}
Expand Down Expand Up @@ -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())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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> {
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()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,35 @@
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())
}

@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())
}

@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())
Expand All @@ -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())
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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']
}
}

Expand Down

0 comments on commit 62f76ac

Please sign in to comment.