Android的长者preSSO意向 [英] Android Espresso Intent

查看:138
本文介绍了Android的长者preSSO意向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的测试文件看起来像这样:

  @RunWith(AndroidJUnit4.class)
@LargeTest
公共类CreateNewSessionActivityTest {
    @规则
    公共IntentsTestRule< CreateNewSessionActivity> mActivityRule =新IntentsTestRule<>(CreateNewSessionActivity.class);    @测试
    公共无效测试(){
        最终意向意图=新的Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType(text / plain的);
        intent.putExtra(Intent.EXTRA_TEXT,这是我的身份验证令牌);        最终的结果Instrumentation.ActivityResult =新Instrumentation.ActivityResult(Activity.RESULT_OK,意向);        打算(hasComponent(hasShortClassName(CreateNewSessionActivity。)))respondWith(结果)。        。OnView由(withId(R.id.create_new_session_auth_token_edit_text))检查(匹配(isDisplayed()));
        。OnView由(withId(R.id.create_new_session_auth_token_edit_text))检查(匹配(withText(这是我的身份验证令牌)));
    }
}

CreateNewSessionActivity.onCreate 方法我执行以下命令:

 最终意向意图= this.getIntent();如果(意向!= NULL){
    最后弦乐行动= intent.getAction();
    最终字符串类型= intent.getType();    如果(Intent.ACTION_SEND.equals(动作)及&放大器;键入!= NULL){
        如果(text / plain的.equals(类型)){
            最后弦乐sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);            如果(sharedText!= NULL){
                authTokenEditText.setText(sharedText);
            }
        }
    }
}

我也注册在清单的意图过滤活动的下方。

 <意向滤光器>
    <作用机器人:名字=android.intent.action.SEND/>
    <类机器人:名字=android.intent.category.DEFAULT/>
    <数据机器人:mime类型=text / plain的/>
&所述; /意图滤光器>

在活动中的code也没有工作。如果我使用任何应用程序,并共享文本给它,它会显示在EditText上。

但是这条线在测试失败:

<$p$p><$c$c>onView(withId(R.id.create_new_session_auth_token_edit_text)).check(matches(withText(\"this是我的身份验证令牌)));

该文本将不会在EditText上显示在所有。有什么错我要测试的意图的方式?


解决方案

有意是检验传出的意图。为了测试传入意图,使用 ActivityRule 的第三个参数为

  @rule
公共ActivityTestRule activityRule =新ActivityTestRule&LT;&GT;(
    CreateNewSessionActivity.class,
    真的,// initialTouchMode
    假); // launchActivity。假设置意图。

然后启动你的活动是这样的:

 意向意图=新的Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType(text / plain的);
intent.putExtra(Intent.EXTRA_TEXT,这是我的身份验证令牌);
activityRule.launchActivity(意向);

请参阅 http://blog.sqisland.com /2015/04/es$p$psso-21-activitytestrule.html 获取更多信息。

My test file looks like this:

@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")));
    }
}

In the CreateNewSessionActivity.onCreate method I execute the following:

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);
            }
        }
    }
}

I also registered the Intent Filter in the Manifest underneath the activity.

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

The code in the activity also does work. If I use any app and share text to it it'll be displayed in the EditText.

However this line fails in the test:

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

The text won't be displayed in the EditText at all. Is there anything wrong with the way I want to test the Intent?

解决方案

intending is for testing outgoing intents. To test incoming intent, use an ActivityRule with the third argument as false.

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

Then launch your activity this way:

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);

See http://blog.sqisland.com/2015/04/espresso-21-activitytestrule.html for more info.

这篇关于Android的长者preSSO意向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆