2015-07-20 7 views
0

У меня есть пункт меню, при нажатии, вызовите ACTION_SEND, как этоттест вызов Intent.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() по моему методу настройки ... чего не хватает?

+0

Да, к сожалению, –

+0

попробует! спасибо –

+0

Android-эмулятор поставляется с почтовым клиентом. Можете ли вы поделиться своим кодом настройки, пожалуйста? И правило, которое вы используете. –

ответ

1

Эспрессо 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()); 

    intented(hasAction(Intent.ACTION_SEND)); 
} 

плюс статический импорт, минус синтаксических ошибок.

Смежные вопросы