测试对 Intent.ACTION_SEND 的调用
test the call to Intent.ACTION_SEND
我有一个菜单项,单击后会调用 ACTION_SEND
,就像这样
public static void shareToGMail(Context context, String subject, String content) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, Constants.FEEDBACK_EMAIL);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, content);
// Attach Debug log:
File dataDirectory = context.getExternalFilesDir(null);
File logFile = new File(dataDirectory, Constants.LOG_FILENAME);
if (logFile.exists() && logFile.canRead()) {
Uri uri = Uri.parse("file://" + logFile);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
} else {
Timber.e("Could not attach Debug file, file " + Constants.BAMBAM_LOG_FILENAME + " does not exist or cannot be read");
}
final PackageManager pm = context.getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail"))
best = info;
if (best != null)
emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
context.startActivity(emailIntent);
}
我正在尝试使用此代码进行测试
@Override
@Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
getActivity();
}
@Test
public void testFeedback(){
try {
openDrawer(R.id.drawer_layout);
onView(withId(R.id.drawer_layout)).check(matches(isOpen()));
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SEND);
// intentFilter.addDataType("text/plain");
Instrumentation.ActivityMonitor receiverActivityMonitor = getInstrumentation().addMonitor(intentFilter, null, false);
onView(allOf(withText(R.string.report_issues))).perform(click());
Activity sendActivity = receiverActivityMonitor.waitForActivityWithTimeout(1000);
assertNotNull(sendActivity);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
但我得到 NoActivityResumedException: No activities in stage RESUMED. Did you forget to launch the activity
,但我正在调用 getActivity()
我的设置方法...缺少什么?
Espresso Intents (here, here) 专为此而设计:
@Rule
public final ActivityRule<MyActivity> rule = new ActivityRule<>(MyActivity.class);
@Test
public void testFeedback() {
openDrawer(R.id.drawer_layout);
onView(withId(R.id.drawer_layout)).check(matches(isOpen()));
onView(allOf(withText(R.string.report_issues))).perform(click());
intended(hasAction(Intent.ACTION_SEND));
}
加上静态导入,减去语法错误。
我有一个菜单项,单击后会调用 ACTION_SEND
,就像这样
public static void shareToGMail(Context context, String subject, String content) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, Constants.FEEDBACK_EMAIL);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, content);
// Attach Debug log:
File dataDirectory = context.getExternalFilesDir(null);
File logFile = new File(dataDirectory, Constants.LOG_FILENAME);
if (logFile.exists() && logFile.canRead()) {
Uri uri = Uri.parse("file://" + logFile);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
} else {
Timber.e("Could not attach Debug file, file " + Constants.BAMBAM_LOG_FILENAME + " does not exist or cannot be read");
}
final PackageManager pm = context.getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail"))
best = info;
if (best != null)
emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
context.startActivity(emailIntent);
}
我正在尝试使用此代码进行测试
@Override
@Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
getActivity();
}
@Test
public void testFeedback(){
try {
openDrawer(R.id.drawer_layout);
onView(withId(R.id.drawer_layout)).check(matches(isOpen()));
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SEND);
// intentFilter.addDataType("text/plain");
Instrumentation.ActivityMonitor receiverActivityMonitor = getInstrumentation().addMonitor(intentFilter, null, false);
onView(allOf(withText(R.string.report_issues))).perform(click());
Activity sendActivity = receiverActivityMonitor.waitForActivityWithTimeout(1000);
assertNotNull(sendActivity);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
但我得到 NoActivityResumedException: No activities in stage RESUMED. Did you forget to launch the activity
,但我正在调用 getActivity()
我的设置方法...缺少什么?
Espresso Intents (here, here) 专为此而设计:
@Rule
public final ActivityRule<MyActivity> rule = new ActivityRule<>(MyActivity.class);
@Test
public void testFeedback() {
openDrawer(R.id.drawer_layout);
onView(withId(R.id.drawer_layout)).check(matches(isOpen()));
onView(allOf(withText(R.string.report_issues))).perform(click());
intended(hasAction(Intent.ACTION_SEND));
}
加上静态导入,减去语法错误。