2019-09-20 18:33:55 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.fenix
|
|
|
|
|
2020-05-29 00:43:40 +00:00
|
|
|
import android.app.Activity
|
2019-09-20 18:33:55 +00:00
|
|
|
import android.content.Intent
|
2019-11-22 00:44:08 +00:00
|
|
|
import android.content.Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
|
2020-05-29 00:43:40 +00:00
|
|
|
import io.mockk.coEvery
|
|
|
|
import io.mockk.coVerify
|
|
|
|
import io.mockk.every
|
|
|
|
import io.mockk.mockk
|
|
|
|
import io.mockk.mockkStatic
|
2020-07-17 20:07:01 +00:00
|
|
|
import io.mockk.unmockkStatic
|
2020-08-05 23:24:47 +00:00
|
|
|
import io.mockk.verify
|
2019-09-20 18:33:55 +00:00
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
|
|
import kotlinx.coroutines.test.runBlockingTest
|
2020-05-29 00:43:40 +00:00
|
|
|
import mozilla.components.feature.intent.processing.IntentProcessor
|
2020-10-07 21:22:02 +00:00
|
|
|
import mozilla.components.support.test.robolectric.testContext
|
2020-07-17 20:07:01 +00:00
|
|
|
import org.junit.After
|
2019-11-22 00:44:08 +00:00
|
|
|
import org.junit.Assert.assertEquals
|
2019-12-06 18:57:54 +00:00
|
|
|
import org.junit.Assert.assertTrue
|
2020-05-29 00:43:40 +00:00
|
|
|
import org.junit.Before
|
2019-09-20 18:33:55 +00:00
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.runner.RunWith
|
2020-05-29 00:43:40 +00:00
|
|
|
import org.mozilla.fenix.components.IntentProcessors
|
2019-12-06 18:57:54 +00:00
|
|
|
import org.mozilla.fenix.customtabs.ExternalAppBrowserActivity
|
2019-09-20 18:33:55 +00:00
|
|
|
import org.mozilla.fenix.ext.components
|
|
|
|
import org.mozilla.fenix.ext.settings
|
2020-05-29 00:43:40 +00:00
|
|
|
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
2019-11-22 00:44:08 +00:00
|
|
|
import org.mozilla.fenix.shortcut.NewTabShortcutIntentProcessor
|
2020-05-29 00:43:40 +00:00
|
|
|
import org.mozilla.fenix.utils.Settings
|
2019-09-20 18:33:55 +00:00
|
|
|
import org.robolectric.Robolectric
|
2019-11-22 00:44:08 +00:00
|
|
|
import org.robolectric.Shadows.shadowOf
|
2019-09-20 18:33:55 +00:00
|
|
|
|
|
|
|
@ExperimentalCoroutinesApi
|
2020-04-01 21:00:32 +00:00
|
|
|
@RunWith(FenixRobolectricTestRunner::class)
|
2019-09-20 18:33:55 +00:00
|
|
|
class IntentReceiverActivityTest {
|
|
|
|
|
2020-05-29 00:43:40 +00:00
|
|
|
private lateinit var settings: Settings
|
|
|
|
private lateinit var intentProcessors: IntentProcessors
|
|
|
|
|
|
|
|
@Before
|
|
|
|
fun setup() {
|
2020-07-17 20:07:01 +00:00
|
|
|
mockkStatic("org.mozilla.fenix.ext.ContextKt")
|
2020-05-29 00:43:40 +00:00
|
|
|
settings = mockk()
|
|
|
|
intentProcessors = mockk()
|
|
|
|
|
|
|
|
every { settings.openLinksInAPrivateTab } returns false
|
|
|
|
every { intentProcessors.intentProcessor } returns mockIntentProcessor()
|
|
|
|
every { intentProcessors.privateIntentProcessor } returns mockIntentProcessor()
|
|
|
|
every { intentProcessors.customTabIntentProcessor } returns mockIntentProcessor()
|
|
|
|
every { intentProcessors.privateCustomTabIntentProcessor } returns mockIntentProcessor()
|
|
|
|
every { intentProcessors.externalAppIntentProcessors } returns emptyList()
|
|
|
|
every { intentProcessors.fennecPageShortcutIntentProcessor } returns mockIntentProcessor()
|
|
|
|
every { intentProcessors.migrationIntentProcessor } returns mockIntentProcessor()
|
|
|
|
|
|
|
|
coEvery { intentProcessors.intentProcessor.process(any()) } returns true
|
|
|
|
}
|
|
|
|
|
2020-07-17 20:07:01 +00:00
|
|
|
@After
|
|
|
|
fun teardown() {
|
|
|
|
unmockkStatic("org.mozilla.fenix.ext.ContextKt")
|
|
|
|
}
|
|
|
|
|
2019-09-20 18:33:55 +00:00
|
|
|
@Test
|
2019-11-22 00:44:08 +00:00
|
|
|
fun `process intent with flag launched from history`() = runBlockingTest {
|
|
|
|
val intent = Intent()
|
|
|
|
intent.flags = FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
|
|
|
|
|
|
|
|
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
|
2020-05-29 00:43:40 +00:00
|
|
|
attachMocks(activity)
|
2019-11-22 00:44:08 +00:00
|
|
|
activity.processIntent(intent)
|
|
|
|
|
|
|
|
val shadow = shadowOf(activity)
|
|
|
|
val actualIntent = shadow.peekNextStartedActivity()
|
|
|
|
|
|
|
|
assertEquals(HomeActivity::class.java.name, actualIntent.component?.className)
|
|
|
|
assertEquals(true, actualIntent.flags == FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `process intent with action OPEN_PRIVATE_TAB`() = runBlockingTest {
|
|
|
|
val intent = Intent()
|
|
|
|
intent.action = NewTabShortcutIntentProcessor.ACTION_OPEN_PRIVATE_TAB
|
|
|
|
|
2020-05-29 00:43:40 +00:00
|
|
|
coEvery { intentProcessors.intentProcessor.process(intent) } returns false
|
|
|
|
coEvery { intentProcessors.customTabIntentProcessor.process(intent) } returns false
|
2019-11-22 00:44:08 +00:00
|
|
|
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
|
2020-05-29 00:43:40 +00:00
|
|
|
attachMocks(activity)
|
2019-11-22 00:44:08 +00:00
|
|
|
activity.processIntent(intent)
|
|
|
|
|
|
|
|
val shadow = shadowOf(activity)
|
|
|
|
val actualIntent = shadow.peekNextStartedActivity()
|
|
|
|
|
|
|
|
assertEquals(HomeActivity::class.java.name, actualIntent.component?.className)
|
|
|
|
assertEquals(true, actualIntent.getBooleanExtra(HomeActivity.PRIVATE_BROWSING_MODE, false))
|
|
|
|
assertEquals(false, actualIntent.getBooleanExtra(HomeActivity.OPEN_TO_BROWSER, true))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `process intent with action OPEN_TAB`() = runBlockingTest {
|
|
|
|
val intent = Intent()
|
|
|
|
intent.action = NewTabShortcutIntentProcessor.ACTION_OPEN_TAB
|
|
|
|
|
|
|
|
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
|
2020-05-29 00:43:40 +00:00
|
|
|
attachMocks(activity)
|
2019-11-22 00:44:08 +00:00
|
|
|
activity.processIntent(intent)
|
|
|
|
|
|
|
|
val shadow = shadowOf(activity)
|
|
|
|
val actualIntent = shadow.peekNextStartedActivity()
|
|
|
|
|
|
|
|
assertEquals(HomeActivity::class.java.name, actualIntent.component?.className)
|
2020-04-18 01:34:14 +00:00
|
|
|
assertEquals(false, actualIntent.getBooleanExtra(HomeActivity.PRIVATE_BROWSING_MODE, false))
|
2019-11-22 00:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `process intent starts Activity`() = runBlockingTest {
|
|
|
|
val intent = Intent()
|
|
|
|
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
|
2020-05-29 00:43:40 +00:00
|
|
|
attachMocks(activity)
|
2019-11-22 00:44:08 +00:00
|
|
|
activity.processIntent(intent)
|
|
|
|
|
|
|
|
val shadow = shadowOf(activity)
|
|
|
|
val actualIntent = shadow.peekNextStartedActivity()
|
|
|
|
|
|
|
|
assertEquals(HomeActivity::class.java.name, actualIntent.component?.className)
|
2020-04-18 01:34:14 +00:00
|
|
|
assertEquals(true, actualIntent.getBooleanExtra(HomeActivity.OPEN_TO_BROWSER, true))
|
2019-11-22 00:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `process intent with launchLinksInPrivateTab set to true`() = runBlockingTest {
|
2020-05-29 00:43:40 +00:00
|
|
|
every { settings.openLinksInAPrivateTab } returns true
|
|
|
|
|
|
|
|
coEvery { intentProcessors.intentProcessor.process(any()) } returns false
|
|
|
|
coEvery { intentProcessors.privateIntentProcessor.process(any()) } returns true
|
2019-11-22 00:44:08 +00:00
|
|
|
|
|
|
|
val intent = Intent()
|
|
|
|
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
|
2020-05-29 00:43:40 +00:00
|
|
|
attachMocks(activity)
|
2019-11-22 00:44:08 +00:00
|
|
|
activity.processIntent(intent)
|
|
|
|
|
2020-08-05 23:24:47 +00:00
|
|
|
val shadow = shadowOf(activity)
|
|
|
|
val actualIntent = shadow.peekNextStartedActivity()
|
|
|
|
|
2020-05-29 00:43:40 +00:00
|
|
|
val normalProcessor = intentProcessors.intentProcessor
|
2020-08-05 23:24:47 +00:00
|
|
|
verify(exactly = 0) { normalProcessor.process(intent) }
|
|
|
|
verify { intentProcessors.privateIntentProcessor.process(intent) }
|
|
|
|
assertEquals(HomeActivity::class.java.name, actualIntent.component?.className)
|
|
|
|
assertTrue(actualIntent.getBooleanExtra(HomeActivity.PRIVATE_BROWSING_MODE, false))
|
2019-09-20 18:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-11-22 00:44:08 +00:00
|
|
|
fun `process intent with launchLinksInPrivateTab set to false`() = runBlockingTest {
|
|
|
|
val intent = Intent()
|
2019-09-20 18:33:55 +00:00
|
|
|
|
2019-11-22 00:44:08 +00:00
|
|
|
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
|
2020-05-29 00:43:40 +00:00
|
|
|
attachMocks(activity)
|
2019-11-22 00:44:08 +00:00
|
|
|
activity.processIntent(intent)
|
2019-09-20 18:33:55 +00:00
|
|
|
|
2020-05-29 00:43:40 +00:00
|
|
|
coVerify(exactly = 0) { intentProcessors.privateIntentProcessor.process(intent) }
|
|
|
|
coVerify { intentProcessors.intentProcessor.process(intent) }
|
2019-09-20 18:33:55 +00:00
|
|
|
}
|
2019-12-06 18:57:54 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `process custom tab intent`() = runBlockingTest {
|
|
|
|
val intent = Intent()
|
2020-05-29 00:43:40 +00:00
|
|
|
coEvery { intentProcessors.intentProcessor.process(intent) } returns false
|
|
|
|
coEvery { intentProcessors.customTabIntentProcessor.process(intent) } returns true
|
2019-12-06 18:57:54 +00:00
|
|
|
|
|
|
|
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
|
2020-05-29 00:43:40 +00:00
|
|
|
attachMocks(activity)
|
2019-12-06 18:57:54 +00:00
|
|
|
activity.processIntent(intent)
|
|
|
|
|
2020-05-29 00:43:40 +00:00
|
|
|
coVerify(exactly = 0) { intentProcessors.privateCustomTabIntentProcessor.process(intent) }
|
|
|
|
coVerify { intentProcessors.customTabIntentProcessor.process(intent) }
|
2019-12-06 18:57:54 +00:00
|
|
|
|
|
|
|
assertEquals(ExternalAppBrowserActivity::class.java.name, intent.component!!.className)
|
|
|
|
assertTrue(intent.getBooleanExtra(HomeActivity.OPEN_TO_BROWSER, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `process private custom tab intent`() = runBlockingTest {
|
2020-05-29 00:43:40 +00:00
|
|
|
every { settings.openLinksInAPrivateTab } returns true
|
2019-12-06 18:57:54 +00:00
|
|
|
|
|
|
|
val intent = Intent()
|
2020-05-29 00:43:40 +00:00
|
|
|
coEvery { intentProcessors.privateCustomTabIntentProcessor.process(intent) } returns true
|
2019-12-06 18:57:54 +00:00
|
|
|
|
|
|
|
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
|
2020-05-29 00:43:40 +00:00
|
|
|
attachMocks(activity)
|
2019-12-06 18:57:54 +00:00
|
|
|
activity.processIntent(intent)
|
|
|
|
|
2020-05-29 00:43:40 +00:00
|
|
|
val normalProcessor = intentProcessors.customTabIntentProcessor
|
|
|
|
coVerify(exactly = 0) { normalProcessor.process(intent) }
|
|
|
|
coVerify { intentProcessors.privateCustomTabIntentProcessor.process(intent) }
|
2019-12-06 18:57:54 +00:00
|
|
|
|
|
|
|
assertEquals(ExternalAppBrowserActivity::class.java.name, intent.component!!.className)
|
|
|
|
assertTrue(intent.getBooleanExtra(HomeActivity.OPEN_TO_BROWSER, false))
|
|
|
|
}
|
2020-05-29 00:43:40 +00:00
|
|
|
|
|
|
|
private fun attachMocks(activity: Activity) {
|
|
|
|
every { activity.settings() } returns settings
|
|
|
|
every { activity.components.analytics } returns mockk(relaxed = true)
|
|
|
|
every { activity.components.intentProcessors } returns intentProcessors
|
2020-10-07 21:22:02 +00:00
|
|
|
|
|
|
|
// For some reason, activity.components doesn't return application.components, which is the
|
|
|
|
// globally defined TestComponents, so we redirect it.
|
|
|
|
every { activity.components.strictMode } returns testContext.components.strictMode
|
2020-05-29 00:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private inline fun <reified T : IntentProcessor> mockIntentProcessor(): T {
|
|
|
|
return mockk {
|
|
|
|
coEvery { process(any()) } returns false
|
|
|
|
}
|
|
|
|
}
|
2019-09-20 18:33:55 +00:00
|
|
|
}
|