This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #12565 Implement the common part of search widget in Android Comp…
…onents
- Loading branch information
Showing
4 changed files
with
181 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
.../test/java/mozilla/components/feature/searchwidget/VoiceSearchActivityExtendedForTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* 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 mozilla.components.feature.searchwidget | ||
|
||
import java.util.Locale | ||
|
||
class VoiceSearchActivityExtendedForTests : VoiceSearchActivity() { | ||
|
||
override fun getCurrentLocale(): Locale { | ||
return Locale.getDefault() | ||
} | ||
|
||
override fun recordVoiceButtonTelemetry() { | ||
} | ||
|
||
override fun startIntentAfterVoiceSearch(spokenText: String?) { | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
...chwidget/src/test/java/mozilla/components/feature/searchwidget/VoiceSearchActivityTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* 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 mozilla.components.feature.searchwidget | ||
|
||
import android.app.Activity | ||
import android.app.Activity.RESULT_OK | ||
import android.content.ComponentName | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.os.Bundle | ||
import android.speech.RecognizerIntent.ACTION_RECOGNIZE_SPEECH | ||
import android.speech.RecognizerIntent.EXTRA_RESULTS | ||
import androidx.activity.result.ActivityResult | ||
import mozilla.components.feature.searchwidget.VoiceSearchActivity.Companion.PREVIOUS_INTENT | ||
import mozilla.components.feature.searchwidget.VoiceSearchActivity.Companion.SPEECH_PROCESSING | ||
import mozilla.components.support.test.robolectric.testContext | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertNull | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.Robolectric | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.Shadows.shadowOf | ||
import org.robolectric.android.controller.ActivityController | ||
import org.robolectric.shadows.ShadowActivity | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class VoiceSearchActivityTest { | ||
|
||
private lateinit var controller: ActivityController<VoiceSearchActivityExtendedForTests> | ||
private lateinit var activity: VoiceSearchActivityExtendedForTests | ||
private lateinit var shadow: ShadowActivity | ||
|
||
@Before | ||
fun setup() { | ||
val intent = Intent() | ||
intent.putExtra(SPEECH_PROCESSING, true) | ||
|
||
controller = Robolectric.buildActivity(VoiceSearchActivityExtendedForTests::class.java, intent) | ||
activity = controller.get() | ||
shadow = shadowOf(activity) | ||
} | ||
|
||
private fun allowVoiceIntentToResolveActivity() { | ||
val shadowPackageManager = shadowOf(testContext.packageManager) | ||
val component = ComponentName("com.test", "Test") | ||
shadowPackageManager.addActivityIfNotPresent(component) | ||
shadowPackageManager.addIntentFilterForActivity( | ||
component, | ||
IntentFilter(ACTION_RECOGNIZE_SPEECH).apply { addCategory(Intent.CATEGORY_DEFAULT) } | ||
) | ||
} | ||
|
||
@Test | ||
fun `process intent with speech processing set to true`() { | ||
val intent = Intent() | ||
intent.putStringArrayListExtra(EXTRA_RESULTS, ArrayList<String>(listOf("hello world"))) | ||
val activityResult = ActivityResult(RESULT_OK, intent) | ||
controller.get().activityResultImplementation(activityResult) | ||
|
||
assertTrue(activity.isFinishing) | ||
} | ||
|
||
@Test | ||
fun `process intent with speech processing set to false`() { | ||
allowVoiceIntentToResolveActivity() | ||
val intent = Intent() | ||
intent.putExtra(SPEECH_PROCESSING, false) | ||
|
||
val controller = Robolectric.buildActivity(VoiceSearchActivityExtendedForTests::class.java, intent) | ||
val activity = controller.get() | ||
|
||
controller.create() | ||
|
||
assertTrue(activity.isFinishing) | ||
} | ||
|
||
@Test | ||
fun `process null intent`() { | ||
allowVoiceIntentToResolveActivity() | ||
val controller = Robolectric.buildActivity(VoiceSearchActivityExtendedForTests::class.java, null) | ||
val activity = controller.get() | ||
|
||
controller.create() | ||
|
||
assertTrue(activity.isFinishing) | ||
} | ||
|
||
@Test | ||
fun `save previous intent to instance state`() { | ||
allowVoiceIntentToResolveActivity() | ||
val previousIntent = Intent().apply { | ||
putExtra(SPEECH_PROCESSING, true) | ||
} | ||
val savedInstanceState = Bundle().apply { | ||
putParcelable(PREVIOUS_INTENT, previousIntent) | ||
} | ||
val outState = Bundle() | ||
|
||
controller.create(savedInstanceState) | ||
controller.saveInstanceState(outState) | ||
|
||
assertEquals(previousIntent, outState.getParcelable<Intent>(PREVIOUS_INTENT)) | ||
} | ||
|
||
@Test | ||
fun `process intent with speech processing in previous intent set to true`() { | ||
allowVoiceIntentToResolveActivity() | ||
val savedInstanceState = Bundle() | ||
val previousIntent = Intent().apply { | ||
putExtra(SPEECH_PROCESSING, true) | ||
} | ||
savedInstanceState.putParcelable(PREVIOUS_INTENT, previousIntent) | ||
|
||
controller.create(savedInstanceState) | ||
|
||
assertFalse(activity.isFinishing) | ||
assertNull(shadow.peekNextStartedActivityForResult()) | ||
} | ||
|
||
@Test | ||
fun `handle invalid result code`() { | ||
val activityResult = ActivityResult(Activity.RESULT_CANCELED, Intent()) | ||
controller.get().activityResultImplementation(activityResult) | ||
|
||
assertTrue(activity.isFinishing) | ||
} | ||
|
||
@Test | ||
fun `handle no activity able to resolve voice intent`() { | ||
controller.create() | ||
assertTrue(activity.isFinishing) | ||
} | ||
} |