|
|
@ -7,12 +7,14 @@ package org.mozilla.fenix.widget
|
|
|
|
import android.app.Activity
|
|
|
|
import android.app.Activity
|
|
|
|
import android.content.ComponentName
|
|
|
|
import android.content.ComponentName
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.Intent
|
|
|
|
|
|
|
|
import android.content.IntentFilter
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.speech.RecognizerIntent.ACTION_RECOGNIZE_SPEECH
|
|
|
|
import android.speech.RecognizerIntent.ACTION_RECOGNIZE_SPEECH
|
|
|
|
import android.speech.RecognizerIntent.EXTRA_LANGUAGE_MODEL
|
|
|
|
import android.speech.RecognizerIntent.EXTRA_LANGUAGE_MODEL
|
|
|
|
import android.speech.RecognizerIntent.EXTRA_RESULTS
|
|
|
|
import android.speech.RecognizerIntent.EXTRA_RESULTS
|
|
|
|
import android.speech.RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
|
|
|
|
import android.speech.RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
|
|
|
|
import androidx.appcompat.app.AppCompatActivity.RESULT_OK
|
|
|
|
import androidx.appcompat.app.AppCompatActivity.RESULT_OK
|
|
|
|
|
|
|
|
import androidx.test.core.app.ApplicationProvider
|
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
|
|
import org.junit.Assert.assertEquals
|
|
|
|
import org.junit.Assert.assertEquals
|
|
|
|
import org.junit.Assert.assertFalse
|
|
|
|
import org.junit.Assert.assertFalse
|
|
|
@ -21,6 +23,7 @@ import org.junit.Assert.assertTrue
|
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.runner.RunWith
|
|
|
|
import org.junit.runner.RunWith
|
|
|
|
|
|
|
|
import org.mozilla.fenix.FenixApplication
|
|
|
|
import org.mozilla.fenix.HomeActivity.Companion.OPEN_TO_BROWSER_AND_LOAD
|
|
|
|
import org.mozilla.fenix.HomeActivity.Companion.OPEN_TO_BROWSER_AND_LOAD
|
|
|
|
import org.mozilla.fenix.IntentReceiverActivity
|
|
|
|
import org.mozilla.fenix.IntentReceiverActivity
|
|
|
|
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
|
|
|
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
|
|
@ -28,7 +31,7 @@ import org.mozilla.fenix.widget.VoiceSearchActivity.Companion.PREVIOUS_INTENT
|
|
|
|
import org.mozilla.fenix.widget.VoiceSearchActivity.Companion.SPEECH_PROCESSING
|
|
|
|
import org.mozilla.fenix.widget.VoiceSearchActivity.Companion.SPEECH_PROCESSING
|
|
|
|
import org.mozilla.fenix.widget.VoiceSearchActivity.Companion.SPEECH_REQUEST_CODE
|
|
|
|
import org.mozilla.fenix.widget.VoiceSearchActivity.Companion.SPEECH_REQUEST_CODE
|
|
|
|
import org.robolectric.Robolectric
|
|
|
|
import org.robolectric.Robolectric
|
|
|
|
import org.robolectric.Shadows
|
|
|
|
import org.robolectric.Shadows.shadowOf
|
|
|
|
import org.robolectric.android.controller.ActivityController
|
|
|
|
import org.robolectric.android.controller.ActivityController
|
|
|
|
import org.robolectric.shadows.ShadowActivity
|
|
|
|
import org.robolectric.shadows.ShadowActivity
|
|
|
|
|
|
|
|
|
|
|
@ -37,7 +40,7 @@ import org.robolectric.shadows.ShadowActivity
|
|
|
|
class VoiceSearchActivityTest {
|
|
|
|
class VoiceSearchActivityTest {
|
|
|
|
|
|
|
|
|
|
|
|
private lateinit var controller: ActivityController<VoiceSearchActivity>
|
|
|
|
private lateinit var controller: ActivityController<VoiceSearchActivity>
|
|
|
|
private lateinit var activity: Activity
|
|
|
|
private lateinit var activity: VoiceSearchActivity
|
|
|
|
private lateinit var shadow: ShadowActivity
|
|
|
|
private lateinit var shadow: ShadowActivity
|
|
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
@Before
|
|
|
@ -47,21 +50,36 @@ class VoiceSearchActivityTest {
|
|
|
|
|
|
|
|
|
|
|
|
controller = Robolectric.buildActivity(VoiceSearchActivity::class.java, intent)
|
|
|
|
controller = Robolectric.buildActivity(VoiceSearchActivity::class.java, intent)
|
|
|
|
activity = controller.get()
|
|
|
|
activity = controller.get()
|
|
|
|
shadow = Shadows.shadowOf(activity)
|
|
|
|
shadow = shadowOf(activity)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun allowVoiceIntentToResolveActivity() {
|
|
|
|
|
|
|
|
val context = ApplicationProvider.getApplicationContext<FenixApplication>()
|
|
|
|
|
|
|
|
val shadowPackageManager = shadowOf(context.packageManager)
|
|
|
|
|
|
|
|
val component = ComponentName("com.test", "Test")
|
|
|
|
|
|
|
|
shadowPackageManager.addActivityIfNotPresent(component)
|
|
|
|
|
|
|
|
shadowPackageManager.addIntentFilterForActivity(
|
|
|
|
|
|
|
|
component,
|
|
|
|
|
|
|
|
IntentFilter(ACTION_RECOGNIZE_SPEECH).apply { addCategory(Intent.CATEGORY_DEFAULT) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun `process intent with speech processing set to true`() {
|
|
|
|
fun `process intent with speech processing set to true`() {
|
|
|
|
|
|
|
|
allowVoiceIntentToResolveActivity()
|
|
|
|
controller.create()
|
|
|
|
controller.create()
|
|
|
|
|
|
|
|
|
|
|
|
val intentForResult = shadow.peekNextStartedActivityForResult()
|
|
|
|
val intentForResult = shadow.peekNextStartedActivityForResult()
|
|
|
|
assertEquals(SPEECH_REQUEST_CODE, intentForResult.requestCode)
|
|
|
|
assertEquals(SPEECH_REQUEST_CODE, intentForResult.requestCode)
|
|
|
|
assertEquals(ACTION_RECOGNIZE_SPEECH, intentForResult.intent.action)
|
|
|
|
assertEquals(ACTION_RECOGNIZE_SPEECH, intentForResult.intent.action)
|
|
|
|
assertEquals(LANGUAGE_MODEL_FREE_FORM, intentForResult.intent.getStringExtra(EXTRA_LANGUAGE_MODEL))
|
|
|
|
assertEquals(
|
|
|
|
|
|
|
|
LANGUAGE_MODEL_FREE_FORM,
|
|
|
|
|
|
|
|
intentForResult.intent.getStringExtra(EXTRA_LANGUAGE_MODEL)
|
|
|
|
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun `process intent with speech processing set to false`() {
|
|
|
|
fun `process intent with speech processing set to false`() {
|
|
|
|
|
|
|
|
allowVoiceIntentToResolveActivity()
|
|
|
|
val intent = Intent()
|
|
|
|
val intent = Intent()
|
|
|
|
intent.putExtra(SPEECH_PROCESSING, false)
|
|
|
|
intent.putExtra(SPEECH_PROCESSING, false)
|
|
|
|
|
|
|
|
|
|
|
@ -75,6 +93,7 @@ class VoiceSearchActivityTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun `process null intent`() {
|
|
|
|
fun `process null intent`() {
|
|
|
|
|
|
|
|
allowVoiceIntentToResolveActivity()
|
|
|
|
val controller = Robolectric.buildActivity(VoiceSearchActivity::class.java, null)
|
|
|
|
val controller = Robolectric.buildActivity(VoiceSearchActivity::class.java, null)
|
|
|
|
val activity = controller.get()
|
|
|
|
val activity = controller.get()
|
|
|
|
|
|
|
|
|
|
|
@ -85,6 +104,7 @@ class VoiceSearchActivityTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun `save previous intent to instance state`() {
|
|
|
|
fun `save previous intent to instance state`() {
|
|
|
|
|
|
|
|
allowVoiceIntentToResolveActivity()
|
|
|
|
val previousIntent = Intent().apply {
|
|
|
|
val previousIntent = Intent().apply {
|
|
|
|
putExtra(SPEECH_PROCESSING, true)
|
|
|
|
putExtra(SPEECH_PROCESSING, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -101,6 +121,7 @@ class VoiceSearchActivityTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun `process intent with speech processing in previous intent set to true`() {
|
|
|
|
fun `process intent with speech processing in previous intent set to true`() {
|
|
|
|
|
|
|
|
allowVoiceIntentToResolveActivity()
|
|
|
|
val savedInstanceState = Bundle()
|
|
|
|
val savedInstanceState = Bundle()
|
|
|
|
val previousIntent = Intent().apply {
|
|
|
|
val previousIntent = Intent().apply {
|
|
|
|
putExtra(SPEECH_PROCESSING, true)
|
|
|
|
putExtra(SPEECH_PROCESSING, true)
|
|
|
@ -115,6 +136,7 @@ class VoiceSearchActivityTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun `handle speech result`() {
|
|
|
|
fun `handle speech result`() {
|
|
|
|
|
|
|
|
allowVoiceIntentToResolveActivity()
|
|
|
|
controller.create()
|
|
|
|
controller.create()
|
|
|
|
|
|
|
|
|
|
|
|
val resultIntent = Intent().apply {
|
|
|
|
val resultIntent = Intent().apply {
|
|
|
@ -129,13 +151,17 @@ class VoiceSearchActivityTest {
|
|
|
|
val browserIntent = shadow.peekNextStartedActivity()
|
|
|
|
val browserIntent = shadow.peekNextStartedActivity()
|
|
|
|
|
|
|
|
|
|
|
|
assertTrue(activity.isFinishing)
|
|
|
|
assertTrue(activity.isFinishing)
|
|
|
|
assertEquals(ComponentName(activity, IntentReceiverActivity::class.java), browserIntent.component)
|
|
|
|
assertEquals(
|
|
|
|
|
|
|
|
ComponentName(activity, IntentReceiverActivity::class.java),
|
|
|
|
|
|
|
|
browserIntent.component
|
|
|
|
|
|
|
|
)
|
|
|
|
assertEquals("hello world", browserIntent.getStringExtra(SPEECH_PROCESSING))
|
|
|
|
assertEquals("hello world", browserIntent.getStringExtra(SPEECH_PROCESSING))
|
|
|
|
assertTrue(browserIntent.getBooleanExtra(OPEN_TO_BROWSER_AND_LOAD, false))
|
|
|
|
assertTrue(browserIntent.getBooleanExtra(OPEN_TO_BROWSER_AND_LOAD, false))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun `handle invalid result code`() {
|
|
|
|
fun `handle invalid result code`() {
|
|
|
|
|
|
|
|
allowVoiceIntentToResolveActivity()
|
|
|
|
controller.create()
|
|
|
|
controller.create()
|
|
|
|
|
|
|
|
|
|
|
|
val resultIntent = Intent()
|
|
|
|
val resultIntent = Intent()
|
|
|
@ -147,4 +173,10 @@ class VoiceSearchActivityTest {
|
|
|
|
|
|
|
|
|
|
|
|
assertTrue(activity.isFinishing)
|
|
|
|
assertTrue(activity.isFinishing)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
fun `handle no activity able to resolve voice intent`() {
|
|
|
|
|
|
|
|
controller.create()
|
|
|
|
|
|
|
|
assertTrue(activity.isFinishing)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|