From 8454f208af5e0cd142755bfca7e23a32d6ff68b9 Mon Sep 17 00:00:00 2001 From: Mugurell Date: Tue, 7 Jan 2020 19:07:51 +0200 Subject: [PATCH] For #4977: Migrate fennec shortcuts (#7251) * For #4977 - Support opening Fennec pinned website shortcuts in Fenix Fennec's pinned website shortcuts are set to open the BrowserApp activity. So we need a new activity alias to actually catch such Intents. Otherwise they would open "org.mozilla.firefox/.App" without any way to inform that this is the result of the user clicking on a pinned shortcut. For actually checking if the newly received Intent is of a Fennec pinned shortcut we introduce a new FennecBookmarkShortcutsIntentProcessor which will prepare the Intent to open the shortcut's URL in a new tab. * For #4977 - Don't keep IntentReceiverActivity on the back stack For successive Fennec pinned shortcuts to create a new IntentReceiverActivity and be processed as normal we need to not keep this as our task root. * For #4977 - Test the FennecBookmarkShortcutsIntentProcessor --- app/src/main/AndroidManifest.xml | 8 ++ .../mozilla/fenix/IntentReceiverActivity.kt | 3 +- .../fenix/components/IntentProcessors.kt | 4 +- .../FennecBookmarkShortcutsIntentProcessor.kt | 63 +++++++++++++ ...necBookmarkShortcutsIntentProcessorTest.kt | 89 +++++++++++++++++++ 5 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/mozilla/fenix/home/intent/FennecBookmarkShortcutsIntentProcessor.kt create mode 100644 app/src/test/java/org/mozilla/fenix/home/intent/FennecBookmarkShortcutsIntentProcessorTest.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0fe9fc110..e5613f747 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -48,6 +48,14 @@ android:resource="@xml/shortcuts" /> + + + + (relaxed = true) + private val loadUrlUseCase = mockk(relaxed = true) + + @Test + fun `only match Fennec pinned shortcut Intents`() { + val processor = FennecBookmarkShortcutsIntentProcessor(sessionManager, loadUrlUseCase) + + assertAll { + assertThat(processor.matches(Intent(ACTION_FENNEC_HOMESCREEN_SHORTCUT))).isTrue() + assertThat(processor.matches(Intent("ShouldNotMatch"))).isFalse() + assertThat(processor.matches(Intent())).isFalse() + } + } + + @Test + fun `do not process blank Intents`() = runBlocking { + val processor = FennecBookmarkShortcutsIntentProcessor(sessionManager, loadUrlUseCase) + val fennecShortcutsIntent = Intent(ACTION_FENNEC_HOMESCREEN_SHORTCUT) + fennecShortcutsIntent.data = Uri.parse("http://mozilla.org") + + val wasEmptyIntentProcessed = processor.matches(Intent()) + + assertThat(wasEmptyIntentProcessed).isFalse() + verify { + sessionManager wasNot Called + loadUrlUseCase wasNot Called + } + } + + @Test + fun `processing a Fennec shortcut Intent results in loading it's URL in a new Session`() = runBlocking { + mockkStatic(UUID::class) + // The Session constructor uses UUID.randomUUID().toString() as the default value for it's id field + every { UUID.randomUUID().toString() } returns "test" + val processor = FennecBookmarkShortcutsIntentProcessor(sessionManager, loadUrlUseCase) + val fennecShortcutsIntent = Intent(ACTION_FENNEC_HOMESCREEN_SHORTCUT) + val testUrl = "http://mozilla.org" + fennecShortcutsIntent.data = Uri.parse(testUrl) + val expectedSession = Session(testUrl, private = false, source = Session.Source.HOME_SCREEN) + + val wasIntentProcessed = processor.process(fennecShortcutsIntent) + + assertAll { + assertThat(wasIntentProcessed).isTrue() + assertThat(fennecShortcutsIntent.action).isEqualTo(Intent.ACTION_VIEW) + assertThat(fennecShortcutsIntent.getSessionId()).isEqualTo(expectedSession.id) + } + verifyAll { + sessionManager.add(expectedSession) + loadUrlUseCase(testUrl, expectedSession, EngineSession.LoadUrlFlags.external()) + } + } +}