Android 意式浓缩咖啡
Android Espresso Intent
我的测试文件如下所示:
@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 下的清单中注册了 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
用于测试传出意图。要测试传入的意图,请使用带有第三个参数的 ActivityRule
作为 false
.
@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。
我的测试文件如下所示:
@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 下的清单中注册了 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
用于测试传出意图。要测试传入的意图,请使用带有第三个参数的 ActivityRule
作为 false
.
@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。