gpt4 book ai didi

android - 测试对 Intent.ACTION_SEND 的调用

转载 作者:行者123 更新时间:2023-11-30 02:00:46 25 4
gpt4 key购买 nike

我有一个菜单项,单击时会调用 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:阶段中没有 Activity 已恢复。您是不是忘记启动 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));
}

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

关于android - 测试对 Intent.ACTION_SEND 的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31523932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com