如何使用Robotium测试AutoCompleteTextView ItemClick事件

时间:2013-11-08 13:57:29

标签: android robotium

我有以下示例robotium测试用例,

public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity> {
private static Class launcherActivityClass ;    
private Solo solo;
public MyActivityTest() {
    super(launcherActivityClass);
}
public void setUp() throws Exception {
    super.setUp();
    solo=new Solo(getInstrumentation(),getActivity());
}
 public void test() throws InterruptedException {
    solo.enterText(0,"Hel");
    solo.goBack();
    solo.sendKey(solo.DOWN);
    solo.enterText(0,"Hello, Android - Ed Burnette");

    Thread.sleep(5000);
 }
}

这是样本受试者,

public class MyActivity extends Activity {
AutoCompleteTextView acTextView;
/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new AsyncTask(this,acTextView).execute("");
    acTextView = (AutoCompleteTextView)findViewById(R.id.androidbooks);

    acTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Log.i("Auto Test", adapterView.getAdapter().getItem(i).toString());

        }
    });

private class AsyncTask extends android.os.AsyncTask<String, String, String[]> {

    public AsyncTask(Context ctxt, AutoCompleteTextView fdtv) {

    }
    @Override
    protected String[] doInBackground(String... strings) {
        return new String[0];
    }

    @Override
    protected void onPostExecute(String[] strings) {
        String[] androidBooks =
                {
                        "Hello, Android - Ed Burnette",
                        "Professional Android 2 App Dev - Reto Meier",
                        "Unlocking Android - Frank Ableson",
                        "Android App Development - Blake Meike",
                        "Pro Android 2 - Dave MacLean",
                        "Beginning Android 2 - Mark Murphy",
                        "Android Programming Tutorials - Mark Murphy",
                        "Android Wireless App Development - Lauren Darcey",
                        "Pro Android Games - Vladimir Silva",
                        "Hello, Android - Ed Burnette",
                        "Professional Android 2 App Dev - Reto Meier",
                        "Unlocking Android - Frank Ableson",
                        "Android App Development - Blake Meike",
                        "Pro Android 2 - Dave MacLean",
                        "Beginning Android 2 - Mark Murphy",
                        "Android Programming Tutorials - Mark Murphy",
                        "Android Wireless App Development - Lauren Darcey",
                        "Pro Android Games - Vladimir Silva",

                };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyActivity.this,android.R.layout.simple_dropdown_item_1line,androidBooks);
        acTextView = (AutoCompleteTextView)findViewById(R.id.androidbooks);
        acTextView.setThreshold(3);
        acTextView.setAdapter(adapter);
    }
}
}

当我运行测试时,AutoCompleteTextView显示列表,文本填充数据,但我无法捕获textview的onItemClick事件。

我怎样才能在机器人中做到这一点?

...谢谢

1 个答案:

答案 0 :(得分:3)

// Enter the first few characters of the book.
solo.enterText(0,"Hello, Andr");

// Wait for the book name to appear, then click on it.
solo.waitForText("Hello, Android - Ed Burnette");
solo.clickOnText("Hello, Android - Ed Burnette");