安卓 Espresso Intent

标签 android android-intent intentfilter android-espresso

我的测试文件如下所示:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class CreateNewSessionActivityTest {
    @Rule
    public IntentsTestRule<CreateNewSessionActivity> mActivityRule = new IntentsTestRule<>(CreateNewSessionActivity.class);

    @Test
    public void test() {
        final Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, "this is my auth token");

        final Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, intent);

        intending(hasComponent(hasShortClassName(".CreateNewSessionActivity"))).respondWith(result);

        onView(withId(R.id.create_new_session_auth_token_edit_text)).check(matches(isDisplayed()));
        onView(withId(R.id.create_new_session_auth_token_edit_text)).check(matches(withText("this is my auth token")));
    }
}

CreateNewSessionActivity.onCreate 方法中,我执行以下操作:

final Intent intent = this.getIntent();

if (intent != null) {
    final String action = intent.getAction();
    final String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            final String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);

            if (sharedText != null) {
                authTokenEditText.setText(sharedText);
            }
        }
    }
}

我还在 Activity 下方的 list 中注册了 Intent Filter。

<intent-filter>
    <action android:name="android.intent.action.SEND"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="text/plain"/>
</intent-filter>

Activity 中的代码也有效。如果我使用任何应用程序并向其共享文本,它将显示在 EditText 中。

但是这条线在测试中失败了:

onView(withId(R.id.create_new_session_auth_token_edit_text)).check(matches(withText("this is my auth token")));

文本根本不会显示在 EditText 中。我想要测试 Intent 的方式有什么问题吗?

最佳答案

intending 用于测试传出 Intent 。要测试传入的 Intent ,请使用第三个参数为 falseActivityRule

@Rule
public ActivityTestRule activityRule = new ActivityTestRule<>(
    CreateNewSessionActivity.class,
    true,    // initialTouchMode
    false);  // launchActivity. False to set intent.

然后以这种方式启动您的 Activity:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "this is my auth token");
activityRule.launchActivity(intent);

参见 http://blog.sqisland.com/2015/04/espresso-21-activitytestrule.html了解更多信息。

关于安卓 Espresso Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34617411/

相关文章:

android - 为旧版SDK准备Android应用

Android startActivityForResult 在使用相机时向 onActivityResult 返回 null

android - 尝试从 URI 启动时出现 "Exported activity does not require permission"

android - 如何从下往上向 LinearLayout 添加 View ?

android - 如何通过wifi在电脑和智能手机之间进行通信

php - android类和php之间的 session 处理

java - Google Analytics 方法 Tracker.getReferer() 是否支持 android 应用 URI?

android - 简单跳转到另一个页面

android - 使用 Intent URL 启动 Android 浏览器的私有(private) Activity

android - 如何从 ACTION_SEND 获取 URL?