From 5964b07ccab0d1b133dafe14475a44c2aa777a71 Mon Sep 17 00:00:00 2001 From: Christian Sadilek Date: Mon, 18 Jan 2021 16:35:25 -0500 Subject: [PATCH] [fenix] Refactor OpenSpecificTabIntentProcessor to use browser store --- .../intent/OpenSpecificTabIntentProcessor.kt | 12 ++++++--- .../OpenSpecificTabIntentProcessorTest.kt | 25 ++++++++++--------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/home/intent/OpenSpecificTabIntentProcessor.kt b/app/src/main/java/org/mozilla/fenix/home/intent/OpenSpecificTabIntentProcessor.kt index 4e91e5ef40..dec4cac01d 100644 --- a/app/src/main/java/org/mozilla/fenix/home/intent/OpenSpecificTabIntentProcessor.kt +++ b/app/src/main/java/org/mozilla/fenix/home/intent/OpenSpecificTabIntentProcessor.kt @@ -6,6 +6,7 @@ package org.mozilla.fenix.home.intent import android.content.Intent import androidx.navigation.NavController +import mozilla.components.browser.state.selector.findTab import mozilla.components.feature.media.service.AbstractMediaService import mozilla.components.feature.media.service.AbstractMediaSessionService import org.mozilla.fenix.BrowserDirection @@ -25,11 +26,14 @@ class OpenSpecificTabIntentProcessor( override fun process(intent: Intent, navController: NavController, out: Intent): Boolean { if (intent.action == getAction()) { - val sessionManager = activity.components.core.sessionManager - val sessionId = intent.extras?.getString(getTabId()) - val session = sessionId?.let { sessionManager.findSessionById(it) } + val browserStore = activity.components.core.store + val tabId = intent.extras?.getString(getTabId()) + + // Technically the additional lookup here isn't necessary, but this way we + // can make sure that we never try and select a custom tab by mistake. + val session = tabId?.let { browserStore.state.findTab(tabId) } if (session != null) { - sessionManager.select(session) + activity.components.useCases.tabsUseCases.selectTab(tabId) activity.openToBrowser(BrowserDirection.FromGlobal) return true } diff --git a/app/src/test/java/org/mozilla/fenix/home/intent/OpenSpecificTabIntentProcessorTest.kt b/app/src/test/java/org/mozilla/fenix/home/intent/OpenSpecificTabIntentProcessorTest.kt index 7dc14cc991..ea155c2fcb 100644 --- a/app/src/test/java/org/mozilla/fenix/home/intent/OpenSpecificTabIntentProcessorTest.kt +++ b/app/src/test/java/org/mozilla/fenix/home/intent/OpenSpecificTabIntentProcessorTest.kt @@ -11,10 +11,12 @@ import io.mockk.every import io.mockk.mockk import io.mockk.verify import io.mockk.verifyOrder -import mozilla.components.browser.session.Session -import mozilla.components.browser.session.SessionManager +import mozilla.components.browser.state.state.BrowserState +import mozilla.components.browser.state.state.createTab +import mozilla.components.browser.state.store.BrowserStore import mozilla.components.feature.media.service.AbstractMediaService import mozilla.components.feature.media.service.AbstractMediaSessionService +import mozilla.components.feature.tabs.TabsUseCases import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Before @@ -42,7 +44,7 @@ class OpenSpecificTabIntentProcessorTest { } @Test - fun `GIVEN a blank intent, WHEN it is processed, THEN nothing should happen`() { + fun `GIVEN a blank intent WHEN it is processed THEN nothing should happen`() { assertFalse(processor.process(Intent(), navController, out)) verify { activity wasNot Called } @@ -51,7 +53,7 @@ class OpenSpecificTabIntentProcessorTest { } @Test - fun `GIVEN an intent with wrong action, WHEN it is processed, THEN nothing should happen`() { + fun `GIVEN an intent with wrong action WHEN it is processed THEN nothing should happen`() { val intent = Intent().apply { action = TEST_WRONG_ACTION } @@ -64,11 +66,10 @@ class OpenSpecificTabIntentProcessorTest { } @Test - fun `GIVEN an intent with null extra string, WHEN it is processed, THEN openToBrowser should not be called`() { + fun `GIVEN an intent with null extra string WHEN it is processed THEN openToBrowser should not be called`() { val intent = Intent().apply { action = AbstractMediaService.Companion.ACTION_SWITCH_TAB } - every { activity.components.core.sessionManager } returns mockk(relaxed = true) assertFalse(processor.process(intent, navController, out)) @@ -78,7 +79,7 @@ class OpenSpecificTabIntentProcessorTest { } @Test - fun `GIVEN an intent with correct action and extra string, WHEN it is processed, THEN session should be selected and openToBrowser should be called`() { + fun `GIVEN an intent with correct action and extra string WHEN it is processed THEN session should be selected and openToBrowser should be called`() { val intent = Intent().apply { if (newMediaSessionApi) { action = AbstractMediaSessionService.Companion.ACTION_SWITCH_TAB @@ -88,15 +89,15 @@ class OpenSpecificTabIntentProcessorTest { putExtra(AbstractMediaService.Companion.EXTRA_TAB_ID, TEST_SESSION_ID) } } - val sessionManager: SessionManager = mockk(relaxed = true) - val session: Session = mockk(relaxed = true) - every { activity.components.core.sessionManager } returns sessionManager - every { sessionManager.findSessionById(TEST_SESSION_ID) } returns session + val store = BrowserStore(BrowserState(tabs = listOf(createTab(id = TEST_SESSION_ID, url = "https:mozilla.org")))) + val tabUseCases: TabsUseCases = mockk(relaxed = true) + every { activity.components.core.store } returns store + every { activity.components.useCases.tabsUseCases } returns tabUseCases assertTrue(processor.process(intent, navController, out)) verifyOrder { - sessionManager.select(session) + tabUseCases.selectTab(TEST_SESSION_ID) activity.openToBrowser(BrowserDirection.FromGlobal) } verify { navController wasNot Called }