测试对Intent.ACTION_SEND的调用

时间:2015-07-20 18:39:38

标签: android android-intent android-espresso

我有一个菜单项,点击后,调用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() ...缺少什么?

1 个答案:

答案 0 :(得分:1)

意式浓缩咖啡意图(herehere)就是这样做的:

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

加静态导入,减去语法错误。