mirror of
https://github.com/fork-maintainers/iceraven-browser
synced 2024-11-19 09:25:34 +00:00
[fenix] For 5334: test NotificationSessionObserver
This commit is contained in:
parent
5d33e79c59
commit
484d552c60
@ -16,26 +16,27 @@ import org.mozilla.fenix.ext.sessionsOfType
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class NotificationSessionObserver(
|
class NotificationSessionObserver(
|
||||||
private val context: Context
|
private val context: Context,
|
||||||
|
private val notificationService: SessionNotificationService.Companion = SessionNotificationService
|
||||||
) : SessionManager.Observer {
|
) : SessionManager.Observer {
|
||||||
|
|
||||||
override fun onSessionRemoved(session: Session) {
|
override fun onSessionRemoved(session: Session) {
|
||||||
val privateTabsEmpty = context.components.core.sessionManager.sessionsOfType(private = true).none()
|
val privateTabsEmpty = context.components.core.sessionManager.sessionsOfType(private = true).none()
|
||||||
|
|
||||||
if (privateTabsEmpty) {
|
if (privateTabsEmpty) {
|
||||||
SessionNotificationService.stop(context)
|
notificationService.stop(context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onAllSessionsRemoved() {
|
override fun onAllSessionsRemoved() {
|
||||||
SessionNotificationService.stop(context)
|
notificationService.stop(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onSessionAdded(session: Session) {
|
override fun onSessionAdded(session: Session) {
|
||||||
// Custom tabs are meant to feel like part of the app that opened them, not Fenix, so we
|
// Custom tabs are meant to feel like part of the app that opened them, not Fenix, so we
|
||||||
// don't need to show a 'close tab' notification for them
|
// don't need to show a 'close tab' notification for them
|
||||||
if (session.private && !session.isCustomTabSession()) {
|
if (session.private && !session.isCustomTabSession()) {
|
||||||
SessionNotificationService.start(context)
|
notificationService.start(context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,66 @@
|
|||||||
|
package org.mozilla.fenix.session
|
||||||
|
|
||||||
|
import io.mockk.MockKAnnotations
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.impl.annotations.MockK
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.verify
|
||||||
|
import mozilla.components.support.test.robolectric.testContext
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mozilla.fenix.TestApplication
|
||||||
|
import org.robolectric.RobolectricTestRunner
|
||||||
|
import org.robolectric.annotation.Config
|
||||||
|
import mozilla.components.browser.session.Session
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner::class)
|
||||||
|
@Config(application = TestApplication::class)
|
||||||
|
class NotificationSessionObserverTest {
|
||||||
|
|
||||||
|
private lateinit var observer: NotificationSessionObserver
|
||||||
|
@MockK(relaxed = true) private lateinit var notificationService: SessionNotificationService.Companion
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun before() {
|
||||||
|
MockKAnnotations.init(this)
|
||||||
|
observer = NotificationSessionObserver(testContext, notificationService)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `GIVEN session is private and non-custom WHEN it is added THEN notification service should be started`() {
|
||||||
|
val privateSession = mockSession(true, false)
|
||||||
|
|
||||||
|
observer.onSessionAdded(privateSession)
|
||||||
|
verify(exactly = 1) { notificationService.start(any()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `GIVEN session is not private WHEN it is added THEN notification service should not be started`() {
|
||||||
|
val normalSession = mockSession(false, true)
|
||||||
|
val customSession = mockSession(false, false)
|
||||||
|
|
||||||
|
observer.onSessionAdded(normalSession)
|
||||||
|
verify(exactly = 0) { notificationService.start(any()) }
|
||||||
|
|
||||||
|
observer.onSessionAdded(customSession)
|
||||||
|
verify(exactly = 0) { notificationService.start(any()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `GIVEN session is custom tab WHEN it is added THEN notification service should not be started`() {
|
||||||
|
val privateCustomSession = mockSession(true, true)
|
||||||
|
val customSession = mockSession(false, true)
|
||||||
|
|
||||||
|
observer.onSessionAdded(privateCustomSession)
|
||||||
|
verify(exactly = 0) { notificationService.start(any()) }
|
||||||
|
|
||||||
|
observer.onSessionAdded(customSession)
|
||||||
|
verify(exactly = 0) { notificationService.start(any()) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun mockSession(isPrivate: Boolean, isCustom: Boolean) = mockk<Session> {
|
||||||
|
every { private } returns isPrivate
|
||||||
|
every { isCustomTabSession() } returns isCustom
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user