diff --git a/app/src/main/java/org/mozilla/fenix/session/NotificationSessionObserver.kt b/app/src/main/java/org/mozilla/fenix/session/NotificationSessionObserver.kt index 0826e4ac7..2df2cdb88 100644 --- a/app/src/main/java/org/mozilla/fenix/session/NotificationSessionObserver.kt +++ b/app/src/main/java/org/mozilla/fenix/session/NotificationSessionObserver.kt @@ -16,26 +16,27 @@ import org.mozilla.fenix.ext.sessionsOfType */ class NotificationSessionObserver( - private val context: Context + private val context: Context, + private val notificationService: SessionNotificationService.Companion = SessionNotificationService ) : SessionManager.Observer { override fun onSessionRemoved(session: Session) { val privateTabsEmpty = context.components.core.sessionManager.sessionsOfType(private = true).none() if (privateTabsEmpty) { - SessionNotificationService.stop(context) + notificationService.stop(context) } } override fun onAllSessionsRemoved() { - SessionNotificationService.stop(context) + notificationService.stop(context) } override fun onSessionAdded(session: Session) { // 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 if (session.private && !session.isCustomTabSession()) { - SessionNotificationService.start(context) + notificationService.start(context) } } } diff --git a/app/src/test/java/org/mozilla/fenix/session/NotificationSessionObserverTest.kt b/app/src/test/java/org/mozilla/fenix/session/NotificationSessionObserverTest.kt new file mode 100644 index 000000000..ba0424bdd --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/session/NotificationSessionObserverTest.kt @@ -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 { + every { private } returns isPrivate + every { isCustomTabSession() } returns isCustom +}