Example usage for android.app Instrumentation addMonitor

List of usage examples for android.app Instrumentation addMonitor

Introduction

In this page you can find the example usage for android.app Instrumentation addMonitor.

Prototype

public ActivityMonitor addMonitor(String cls, ActivityResult result, boolean block) 

Source Link

Document

A convenience wrapper for #addMonitor(ActivityMonitor) that creates a class matching ActivityMonitor for you and returns it.

Usage

From source file:au.com.cybersearch2.classyfy.MainActivityTest.java

public void test_search() throws Throwable {
    final MainActivity mainActivity = getActivity();
    Instrumentation instrumentation = getInstrumentation();
    ActivityMonitor am = instrumentation.addMonitor(TitleSearchResultsActivity.class.getName(), null, false);
    assertThat(instrumentation.invokeMenuActionSync(mainActivity, R.id.action_search, 0)).isTrue();
    ActionBar actionBar = mainActivity.getSupportActionBar();
    assertThat(actionBar).isNotNull();/*from   w w w  . j  a v  a  2  s . c  o m*/
    final FragmentManager sfm = mainActivity.getSupportFragmentManager();
    runTestOnUiThread(new Runnable() {
        public void run() {
            sfm.executePendingTransactions();
        }
    });
    instrumentation.sendCharacterSync(KeyEvent.KEYCODE_I);
    instrumentation.sendCharacterSync(KeyEvent.KEYCODE_N);
    instrumentation.sendCharacterSync(KeyEvent.KEYCODE_F);
    instrumentation.sendCharacterSync(KeyEvent.KEYCODE_ENTER);
    runTestOnUiThread(new Runnable() {
        public void run() {
            sfm.executePendingTransactions();
        }
    });
    TitleSearchResultsActivity titleSearchResultsActivity = (TitleSearchResultsActivity) getInstrumentation()
            .waitForMonitorWithTimeout(am, 10000);
    assertThat(titleSearchResultsActivity).isNotNull();
    assertThat(titleSearchResultsActivity.taskHandle).isNotNull();
    synchronized (titleSearchResultsActivity.taskHandle) {
        titleSearchResultsActivity.taskHandle.wait(10000);
    }
    assertThat(titleSearchResultsActivity.taskHandle.getStatus()).isEqualTo(WorkStatus.FINISHED);
    SimpleCursorAdapter adapter = titleSearchResultsActivity.adapter;
    for (int i = 0; i < adapter.getCount(); i++) {
        Cursor cursor = (Cursor) adapter.getItem(i);
        int column = cursor.getColumnIndexOrThrow(SearchManager.SUGGEST_COLUMN_TEXT_1);
        assertThat(INF_LIST[i]).isEqualTo(cursor.getString(column));
    }
}