SearchView触发两个Intent到SearchActivity

时间:2018-05-03 08:43:35

标签: android searchview

目前我正在使用espresso测试来测试我的应用程序,但是SearchView会为SearchResultActivity(执行搜索并显示结果的Activity)触发两个相同的Intent。为什么有两个意图被解雇而不仅仅是一个?

在我的测试中,我收到以下错误消息:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 2 intents.

    IntentMatcher: (has component: has component with: class name: is "my.first.application.search.SearchResultActivity" package name: an instance of java.lang.String short class name: an instance of java.lang.String and has action: is "android.intent.action.SEARCH" and has extras: has bundle with: key: is "query" value: is "Baecker Neuss")

    Matched intents:
    -Intent { act=android.intent.action.SEARCH flg=0x10000000 cmp=my.first.application.MyApplication.lite.cpa/my.first.application.search.SearchResultActivity launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) } handling packages:[[my.first.application.MyApplication.lite.cpa]], extras:[Bundle[{query=Baecker Neuss, user_query=Baecker Neuss}]])
    -Intent { act=android.intent.action.SEARCH flg=0x10000000 cmp=my.first.application.MyApplication.lite.cpa/my.first.application.search.SearchResultActivity launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) } handling packages:[[my.first.application.MyApplication.lite.cpa]], extras:[Bundle[{query=Baecker Neuss, user_query=Baecker Neuss}]])

    Recorded intents:
    -Intent { act=android.intent.action.SEARCH flg=0x10000000 cmp=my.first.application.MyApplication.lite.cpa/my.first.application.search.SearchResultActivity launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) } handling packages:[[my.first.application.MyApplication.lite.cpa]], extras:[Bundle[{query=Baecker Neuss, user_query=Baecker Neuss}]])
    -Intent { act=android.intent.action.SEARCH flg=0x10000000 cmp=my.first.application.MyApplication.lite.cpa/my.first.application.search.SearchResultActivity launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) } handling packages:[[my.first.application.MyApplication.lite.cpa]], extras:[Bundle[{query=Baecker Neuss, user_query=Baecker Neuss}]])
    at dalvik.system.VMStack.getThreadStackTrace(Native Method)
    at java.lang.Thread.getStackTrace(Thread.java:1567)
    at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:90)
    at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52)
    at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:314)
    at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:291)
    at android.support.test.espresso.intent.Intents.intended(Intents.java:185)
    at android.support.test.espresso.intent.Intents.intended(Intents.java:167)
    at my.first.application.MyApplicationTest.testOSMSearch(SGJMobileTest.java:48)
    at java.lang.reflect.Method.invoke(Native Method)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:433)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
    at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1967)

这是失败的测试:

@Test
    public void testOSMSearch() {
        String searchQuery = "Baecker Neuss";
        Intents.init();
        onView(withId(R.id.toolbar_search))
                .perform(click(), typeSearchView(searchQuery), pressKey(KeyEvent.KEYCODE_ENTER));
        intended(allOf(
                hasComponent(SearchResultActivity.class.getName()),
                hasAction(Intent.ACTION_SEARCH),
                hasExtra(SearchManager.QUERY, searchQuery)));
        Intents.release();
    }

Android Manifest:

<activity
            android:name=".MyApplication"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="android.app.default_searchable"
                android:value=".search.SearchResultActivity" />
        </activity>
        <activity
            android:name=".search.SearchResultActivity"
            android:launchMode="singleTop"
            android:windowSoftInputMode="stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="@string/osmSearch"
    android:label="@string/app_name" />

SearchView的初始化:

 public boolean onCreateOptionsMenu(Menu menu, Activity activity) {
        activity.getMenuInflater().inflate(R.menu.main_toolbar_menu, menu);
        SearchManager searchManager = (SearchManager) activity.getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.toolbar_search).getActionView();
        ComponentName cn = new ComponentName(activity, SearchResultActivity.class);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
        return true;
    }

应用程序中的工作流程是正确的,但是由于两个Intent,活动必须处理两个Intent。有没有人建议为什么有两个Intent被解雇或解决方法如何解决这个问题?

0 个答案:

没有答案