2012-02-07 35 views
2

我正在爲啓動設備的默認郵件客戶端的活動編寫JUnit測試。我想驗證「發送到」活動已啓動,然後將單擊事件發送到「發送」按鈕。ActivityMonitor on Intent過濾器ACTION_SENDTO未命中

我確實設置了一個帶有意圖過濾器的ActivityMonitor,以便獲取「Send To」活動的參考。運行測試時可以看到發送郵件活動,但不幸的是監視器永遠不會被擊中。

下面是單元測試代碼,試圖找到「發送到」活動:

// register activity monitor for the send mail activity 
Instrumentation instrumentation = getInstrumentation(); 
IntentFilter filter = new IntentFilter(Intent.ACTION_SENDTO); 
ActivityMonitor monitor = instrumentation.addMonitor(filter, null, false); 

// click on the "Send Feedback" button (use Robotium here) 
solo.clickOnButton(0); 

// wait for the send mail activity to start 
Activity currentActivity = instrumentation.waitForMonitorWithTimeout(monitor, 5000); 
assertNotNull(currentActivity); 

這裏是如何「發送到」活動是在應用程序啓動:

Uri uri = Uri.parse("mailto:[email protected]"); 
Intent i = new Intent(Intent.ACTION_SENDTO, uri);         
i.putExtra(Intent.EXTRA_SUBJECT, "Message Title");   
i.putExtra(Intent.EXTRA_TEXT, "Hello"); 
startActivity(i); 

意圖過濾器設置不正確?還是不可能監測項目中未定義的活動?

感謝您的幫助。

+0

我有同樣的問題,我想這是不可能的,因爲沒有「真正的」活動開始,事實上趕上意圖的問題,實際上啓動你的意圖後活動活動列表是空的,但你有沒有設法解決這個問題? – Shilaghae 2012-11-23 15:57:45

+0

不幸沒有。我從未解決過這個問題。 – Georges 2012-11-23 16:37:49

回答

0

我有我的測試相同的問題,這對我工作。

此時您的測試工作正常,但您的顯示器不知道要預期哪些數據,因此您的意圖過濾器沒有捕獲任何東西。

// register activity monitor for the send mail activity  
Instrumentation instrumentation = getInstrumentation();  
IntentFilter filter = new IntentFilter(Intent.ACTION_SENDTO); 


//Here you just need to say to your intent filter what exactly to look for 
filter.addDataType(CommonDataKinds.Email.CONTENT_TYPE); 



ActivityMonitor monitor = instrumentation.addMonitor(filter, null, true); 
// click on the "Send Feedback" button (use Robotium here) 
solo.clickOnButton(0);  
// wait for the send mail activity to start 
Activity currentActivity = instrumentation.waitForMonitorWithTimeout(monitor, 5000); 
assertNotNull(currentActivity); 

如果你這樣做了發送短信,這條線將是:

filter.addDataScheme(Constants.SCHEME_SMS); 
2

我有這個問題,也和原來的活動監視器不工作,因爲Robotium的獨奏安裝其自己的監控。該監視器捕捉所有意圖,因此您的自定義監視器永遠不會被調用。

我最終做的是首先刪除獨奏顯示器,插入我自己的,然後重新添加獨奏顯示器,以便您的顯示器首先匹配。然後,您的代碼將是這個樣子:

// register activity monitor for the send mail activity 
Instrumentation instrumentation = getInstrumentation(); 
IntentFilter filter = new IntentFilter(Intent.ACTION_SENDTO); 
ActivityMonitor monitor = new ActivityMonitor(filter, null, false); 
ActivityMonitor soloMonitor = solo.getActivityMonitor(); 

// Remove the solo monitor, so your monitor is first on the list. 
instrumentation.removeMonitor(soloMonitor); 
// add your own monitor. 
instrumentation.addMonitor(monitor); 
// Re-add the solo monitor 
instrumentation.addMonitor(soloMonitor); 

// click on the "Send Feedback" button (use Robotium here) 
solo.clickOnButton(0); 

// wait for the send mail activity to start 
Activity currentActivity = instrumentation.waitForMonitorWithTimeout(monitor, 5000); 
assertNotNull(currentActivity); 

補充說明:啓動另一項活動(郵件應用程序)時,你失去的測試環境的控制,所以這將是使監控阻止有用(即new ActivityMonitor(filter, null, true);