@ -10,7 +10,6 @@ import androidx.core.view.isVisible
import io.mockk.Called
import io.mockk.Runs
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
@ -18,10 +17,8 @@ import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.cancel
import mozilla.components.support.test.libstate.ext.waitUntilIdle
import mozilla.components.support.test.rule.MainCoroutineRule
import mozilla.components.support.test.rule.runTestOnMain
import org.junit.Assert.assertNotNull
import org.junit. Rul e
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.components.AppStore
@ -32,9 +29,6 @@ import org.mozilla.fenix.wallpapers.WallpapersUseCases
@RunWith ( FenixRobolectricTestRunner :: class )
class WallpapersObserverTest {
@get : Rule
val coroutinesTestRule = MainCoroutineRule ( )
@Test
fun `WHEN the observer is created THEN start observing the store` ( ) {
val appStore : AppStore = mockk ( relaxed = true ) {
@ -47,15 +41,15 @@ class WallpapersObserverTest {
}
@Test
fun `WHEN asked to apply the wallpaper THEN show it` ( ) = runTestOnMain {
fun `WHEN asked to apply the wallpaper THEN show it` ( ) {
val appStore = AppStore ( )
val observer = spyk ( WallpapersObserver ( appStore , mockk ( ) , mockk ( ) ) ) {
coE very { showWallpaper ( any ( ) ) } just Runs
e very { showWallpaper ( any ( ) ) } just Runs
}
observer . applyCurrentWallpaper ( )
coV erify { observer . showWallpaper ( any ( ) ) }
v erify { observer . showWallpaper ( any ( ) ) }
}
@Test
@ -73,10 +67,10 @@ class WallpapersObserverTest {
}
@Test
fun `WHEN the wallpaper is updated THEN show the wallpaper` ( ) = runTestOnMain {
fun `WHEN the wallpaper is updated THEN show the wallpaper` ( ) {
val appStore = AppStore ( )
val observer = spyk ( WallpapersObserver ( appStore , mockk ( relaxed = true ) , mockk ( relaxed = true ) ) ) {
coE very { showWallpaper ( any ( ) ) } just Runs
e very { showWallpaper ( any ( ) ) } just Runs
}
// Ignore the call on the real instance and call again "observeWallpaperUpdates"
@ -86,38 +80,38 @@ class WallpapersObserverTest {
val newWallpaper : Wallpaper = mockk ( relaxed = true )
appStore . dispatch ( UpdateCurrentWallpaper ( newWallpaper ) )
appStore . waitUntilIdle ( )
coV erify { observer . showWallpaper ( newWallpaper ) }
v erify { observer . showWallpaper ( newWallpaper ) }
}
@Test
fun `WHEN the wallpaper is updated to a new one THEN show the wallpaper` ( ) = runTestOnMain {
fun `WHEN the wallpaper is updated to a new one THEN show the wallpaper` ( ) {
val appStore = AppStore ( )
val wallpapersUseCases : WallpapersUseCases = mockk {
coEvery { loadBitmap ( any ( ) ) } returns null
}
val observer = spyk ( WallpapersObserver ( appStore , wallpapersUseCases , mockk ( relaxed = true ) ) ) {
coE very { showWallpaper ( any ( ) ) } just Runs
e very { showWallpaper ( any ( ) ) } just Runs
}
// Ignore the call on the real instance and call again "observeWallpaperUpdates"
// on the spy to be able to verify the "showWallpaper" call in the spy.
observer . observeWallpaperUpdates ( )
coV erify { observer . showWallpaper ( Wallpaper . Default ) }
v erify { observer . showWallpaper ( Wallpaper . Default ) }
val wallpaper : Wallpaper = mockk ( relaxed = true )
appStore . dispatch ( UpdateCurrentWallpaper ( wallpaper ) )
appStore . waitUntilIdle ( )
coV erify { observer . showWallpaper ( wallpaper ) }
v erify { observer . showWallpaper ( wallpaper ) }
}
@Test
fun `WHEN the wallpaper is updated to the current one THEN don't try showing the same wallpaper again` ( ) = runTestOnMain {
fun `WHEN the wallpaper is updated to the current one THEN don't try showing the same wallpaper again` ( ) {
val appStore = AppStore ( )
val wallpapersUseCases : WallpapersUseCases = mockk {
coEvery { loadBitmap ( any ( ) ) } returns null
}
val observer = spyk ( WallpapersObserver ( appStore , wallpapersUseCases , mockk ( relaxed = true ) ) ) {
coE very { showWallpaper ( any ( ) ) } just Runs
e very { showWallpaper ( any ( ) ) } just Runs
}
// Ignore the call on the real instance and call again "observeWallpaperUpdates"
// on the spy to be able to verify the "showWallpaper" call in the spy.
@ -126,15 +120,15 @@ class WallpapersObserverTest {
val wallpaper : Wallpaper = mockk ( relaxed = true )
appStore . dispatch ( UpdateCurrentWallpaper ( wallpaper ) )
appStore . waitUntilIdle ( )
coV erify { observer . showWallpaper ( wallpaper ) }
v erify { observer . showWallpaper ( wallpaper ) }
appStore . dispatch ( UpdateCurrentWallpaper ( wallpaper ) )
appStore . waitUntilIdle ( )
coV erify( exactly = 1 ) { observer . showWallpaper ( wallpaper ) }
v erify( exactly = 1 ) { observer . showWallpaper ( wallpaper ) }
}
@Test
fun `GIVEN no wallpaper is provided WHEN asked to show the wallpaper THEN show the current one` ( ) = runTestOnMain {
fun `GIVEN no wallpaper is provided WHEN asked to show the wallpaper THEN show the current one` ( ) {
val wallpaper : Wallpaper = mockk ( )
val appStore : AppStore = mockk ( relaxed = true ) {
every { state . wallpaperState . currentWallpaper } returns wallpaper
@ -143,10 +137,10 @@ class WallpapersObserverTest {
observer . showWallpaper ( )
coV erify { observer . showWallpaper ( wallpaper ) }
v erify { observer . showWallpaper ( wallpaper ) }
}
fun `GiVEN the current wallpaper is the default one WHEN showing it THEN hide the wallpaper view` ( ) = runTestOnMain {
fun `GiVEN the current wallpaper is the default one WHEN showing it THEN hide the wallpaper view` ( ) {
val wallpapersUseCases : WallpapersUseCases = mockk ( )
val wallpaperView : ImageView = mockk ( relaxed = true )
val observer = WallpapersObserver ( mockk ( relaxed = true ) , wallpapersUseCases , wallpaperView )
@ -158,7 +152,7 @@ class WallpapersObserverTest {
}
@Test
fun `GiVEN the current wallpaper is different than the default one WHEN showing it THEN load it's bitmap in the visible wallpaper view` ( ) = runTestOnMain {
fun `GiVEN the current wallpaper is different than the default one WHEN showing it THEN load it's bitmap in the visible wallpaper view` ( ) {
val wallpaper : Wallpaper = mockk ( )
val bitmap : Bitmap = mockk ( )
val wallpapersUseCases : WallpapersUseCases = mockk {
@ -172,4 +166,16 @@ class WallpapersObserverTest {
verify { wallpaperView . isVisible = true }
verify { wallpaperView . setImageBitmap ( bitmap ) }
}
@Test
fun `GIVEN the observer THEN use the main thread for showing the wallpaper` ( ) {
val wallpapersUseCases : WallpapersUseCases = mockk ( )
val wallpaperView : ImageView = mockk ( relaxed = true )
val observer = WallpapersObserver ( mockk ( relaxed = true ) , wallpapersUseCases , wallpaperView )
// Check that the context that would be used is Dispatchers.Main.immediate
// Unfortunately I could not also test that this is actually used when "showWallpaper" is called.
assertTrue ( observer . wallpapersScope . toString ( ) . contains ( " Dispatchers.Main.immediate " ) )
}
}