Migrate some SessionManager usage to BrowserStore (#10789)
parent
9dd5988071
commit
a7e74e308b
@ -1,63 +1,82 @@
|
|||||||
package org.mozilla.fenix.session
|
package org.mozilla.fenix.session
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import io.mockk.Called
|
||||||
import io.mockk.MockKAnnotations
|
import io.mockk.MockKAnnotations
|
||||||
|
import io.mockk.confirmVerified
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.impl.annotations.MockK
|
import io.mockk.impl.annotations.MockK
|
||||||
import io.mockk.mockk
|
|
||||||
import io.mockk.verify
|
import io.mockk.verify
|
||||||
import mozilla.components.support.test.robolectric.testContext
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import mozilla.components.browser.state.action.CustomTabListAction
|
||||||
|
import mozilla.components.browser.state.action.TabListAction
|
||||||
|
import mozilla.components.browser.state.state.createCustomTab
|
||||||
|
import mozilla.components.browser.state.state.createTab
|
||||||
|
import mozilla.components.browser.state.store.BrowserStore
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
|
import org.mozilla.fenix.ext.components
|
||||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||||
import mozilla.components.browser.session.Session
|
|
||||||
|
|
||||||
|
@ExperimentalCoroutinesApi
|
||||||
@RunWith(FenixRobolectricTestRunner::class)
|
@RunWith(FenixRobolectricTestRunner::class)
|
||||||
class NotificationSessionObserverTest {
|
class NotificationSessionObserverTest {
|
||||||
|
|
||||||
private lateinit var observer: NotificationSessionObserver
|
private lateinit var observer: NotificationSessionObserver
|
||||||
|
private lateinit var store: BrowserStore
|
||||||
|
@MockK private lateinit var context: Context
|
||||||
@MockK(relaxed = true) private lateinit var notificationService: SessionNotificationService.Companion
|
@MockK(relaxed = true) private lateinit var notificationService: SessionNotificationService.Companion
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
fun before() {
|
fun before() {
|
||||||
MockKAnnotations.init(this)
|
MockKAnnotations.init(this)
|
||||||
observer = NotificationSessionObserver(testContext, notificationService)
|
store = BrowserStore()
|
||||||
|
every { context.components.core.store } returns store
|
||||||
|
observer = NotificationSessionObserver(context, notificationService)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `GIVEN session is private and non-custom WHEN it is added THEN notification service should be started`() {
|
fun `GIVEN session is private and non-custom WHEN it is added THEN notification service should be started`() = runBlocking {
|
||||||
val privateSession = mockSession(true, false)
|
val privateSession = createTab("https://firefox.com", private = true)
|
||||||
|
|
||||||
observer.onSessionAdded(privateSession)
|
store.dispatch(TabListAction.AddTabAction(privateSession)).join()
|
||||||
verify(exactly = 1) { notificationService.start(any()) }
|
|
||||||
|
observer.start()
|
||||||
|
verify(exactly = 1) { notificationService.start(context) }
|
||||||
|
confirmVerified(notificationService)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `GIVEN session is not private WHEN it is added THEN notification service should not be started`() {
|
fun `GIVEN session is not private WHEN it is added THEN notification service should not be started`() = runBlocking {
|
||||||
val normalSession = mockSession(false, true)
|
val normalSession = createTab("https://firefox.com")
|
||||||
val customSession = mockSession(false, false)
|
val customSession = createCustomTab("https://firefox.com")
|
||||||
|
|
||||||
|
observer.start()
|
||||||
|
verify { notificationService wasNot Called }
|
||||||
|
|
||||||
observer.onSessionAdded(normalSession)
|
store.dispatch(TabListAction.AddTabAction(normalSession)).join()
|
||||||
verify(exactly = 0) { notificationService.start(any()) }
|
verify(exactly = 0) { notificationService.start(context) }
|
||||||
|
|
||||||
observer.onSessionAdded(customSession)
|
store.dispatch(CustomTabListAction.AddCustomTabAction(customSession)).join()
|
||||||
verify(exactly = 0) { notificationService.start(any()) }
|
verify(exactly = 0) { notificationService.start(context) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `GIVEN session is custom tab WHEN it is added THEN notification service should not be started`() {
|
fun `GIVEN session is custom tab WHEN it is added THEN notification service should not be started`() = runBlocking {
|
||||||
val privateCustomSession = mockSession(true, true)
|
val privateCustomSession = createCustomTab("https://firefox.com").let {
|
||||||
val customSession = mockSession(false, true)
|
it.copy(content = it.content.copy(private = true))
|
||||||
|
}
|
||||||
|
val customSession = createCustomTab("https://firefox.com")
|
||||||
|
|
||||||
observer.onSessionAdded(privateCustomSession)
|
observer.start()
|
||||||
verify(exactly = 0) { notificationService.start(any()) }
|
verify { notificationService wasNot Called }
|
||||||
|
|
||||||
observer.onSessionAdded(customSession)
|
store.dispatch(CustomTabListAction.AddCustomTabAction(privateCustomSession)).join()
|
||||||
verify(exactly = 0) { notificationService.start(any()) }
|
verify(exactly = 0) { notificationService.start(context) }
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun mockSession(isPrivate: Boolean, isCustom: Boolean) = mockk<Session> {
|
store.dispatch(CustomTabListAction.AddCustomTabAction(customSession)).join()
|
||||||
every { private } returns isPrivate
|
verify(exactly = 0) { notificationService.start(context) }
|
||||||
every { isCustomTabSession() } returns isCustom
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue