1
0
Fork 0

Closes mozilla-mobile#5458 Tests for IntentReceiverActivity to verify outcoming intent (#6130)

master
Pawel Obarzanek 2019-11-22 01:44:08 +01:00 committed by Jeff Boek
parent ee0ec6a8f8
commit 667602f02a
1 changed files with 97 additions and 24 deletions

View File

@ -5,9 +5,11 @@
package org.mozilla.fenix
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.`when`
@ -15,8 +17,10 @@ import org.mockito.Mockito.never
import org.mockito.Mockito.verify
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.shortcut.NewTabShortcutIntentProcessor
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
@ExperimentalCoroutinesApi
@ -25,37 +29,106 @@ import org.robolectric.annotation.Config
class IntentReceiverActivityTest {
@Test
fun `process intent with launchLinksInPrivateTab set to true`() {
runBlockingTest {
testContext.settings().openLinksInAPrivateTab = true
fun `process intent with flag launched from history`() = runBlockingTest {
testContext.settings().openLinksInAPrivateTab = false
val intent = Intent()
`when`(testContext.components.intentProcessors.privateIntentProcessor.process(intent)).thenReturn(true)
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
activity.processIntent(intent)
val intent = Intent()
intent.flags = FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
// Not using mockk here because process is a suspend function
// and mockito makes this easier to read.
verify(testContext.components.intentProcessors.intentProcessor, never()).process(intent)
verify(testContext.components.intentProcessors.privateIntentProcessor).process(intent)
}
`when`(testContext.components.intentProcessors.intentProcessor.process(intent)).thenReturn(true)
`when`(testContext.components.intentProcessors.intentProcessor.matches(intent)).thenReturn(true)
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
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 launchLinksInPrivateTab set to false`() {
runBlockingTest {
testContext.settings().openLinksInAPrivateTab = false
fun `process intent with action OPEN_PRIVATE_TAB`() = runBlockingTest {
testContext.settings().openLinksInAPrivateTab = false
val intent = Intent()
`when`(testContext.components.intentProcessors.intentProcessor.process(intent)).thenReturn(true)
val intent = Intent()
intent.action = NewTabShortcutIntentProcessor.ACTION_OPEN_PRIVATE_TAB
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
activity.processIntent(intent)
`when`(testContext.components.intentProcessors.intentProcessor.process(intent)).thenReturn(false)
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
activity.processIntent(intent)
// Not using mockk here because process is a suspend function
// and mockito makes this easier to read.
verify(testContext.components.intentProcessors.privateIntentProcessor, never()).process(intent)
verify(testContext.components.intentProcessors.intentProcessor).process(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 {
testContext.settings().openLinksInAPrivateTab = false
val intent = Intent()
intent.action = NewTabShortcutIntentProcessor.ACTION_OPEN_TAB
`when`(testContext.components.intentProcessors.intentProcessor.process(intent)).thenReturn(true)
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
activity.processIntent(intent)
val shadow = shadowOf(activity)
val actualIntent = shadow.peekNextStartedActivity()
assertEquals(HomeActivity::class.java.name, actualIntent.component?.className)
assertEquals(false, actualIntent.hasExtra(HomeActivity.PRIVATE_BROWSING_MODE))
assertEquals(false, actualIntent.getBooleanExtra(HomeActivity.OPEN_TO_BROWSER, true))
}
@Test
fun `process intent starts Activity`() = runBlockingTest {
testContext.settings().openLinksInAPrivateTab = false
val intent = Intent()
`when`(testContext.components.intentProcessors.intentProcessor.process(intent)).thenReturn(true)
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
activity.processIntent(intent)
val shadow = shadowOf(activity)
val actualIntent = shadow.peekNextStartedActivity()
assertEquals(HomeActivity::class.java.name, actualIntent.component?.className)
assertEquals(false, actualIntent.getBooleanExtra(HomeActivity.OPEN_TO_BROWSER, true))
}
@Test
fun `process intent with launchLinksInPrivateTab set to true`() = runBlockingTest {
testContext.settings().openLinksInAPrivateTab = true
val intent = Intent()
`when`(testContext.components.intentProcessors.privateIntentProcessor.process(intent)).thenReturn(true)
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
activity.processIntent(intent)
// Not using mockk here because process is a suspend function
// and mockito makes this easier to read.
verify(testContext.components.intentProcessors.intentProcessor, never()).process(intent)
verify(testContext.components.intentProcessors.privateIntentProcessor).process(intent)
}
@Test
fun `process intent with launchLinksInPrivateTab set to false`() = runBlockingTest {
testContext.settings().openLinksInAPrivateTab = false
val intent = Intent()
`when`(testContext.components.intentProcessors.intentProcessor.process(intent)).thenReturn(true)
val activity = Robolectric.buildActivity(IntentReceiverActivity::class.java, intent).get()
activity.processIntent(intent)
// Not using mockk here because process is a suspend function
// and mockito makes this easier to read.
verify(testContext.components.intentProcessors.privateIntentProcessor, never()).process(intent)
verify(testContext.components.intentProcessors.intentProcessor).process(intent)
}
}