Android测试startActivityForResult for Contact

时间:2014-09-24 05:21:18

标签: android unit-testing android-contacts robotium

我有一个Activity,它通过ACTION_PICK使用startActivityForResult进行联系。首先,我的测试是选择联系人,并在检查后选择联系人。

  public class ListaMensagemActivity extends ListActivity implements Transacao{

      private List<Mensagem> mensagens; 
      private static final int CONTATO_SELECIONADO=1;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);       
          TransacaoTask task = new TransacaoTask(this, this, R.string.aguarde);
          task.execute();
      }

      @Override
      public void executar() throws Exception {
          // Busca as mensagens em uma thread
          this.mensagens = new MensagemService(this).getMensagem();

      }

      @Override
      public void atualizarView() {
          // Atualiza as mensagens na thread principal
          if (this.mensagens != null) {         
              this.setListAdapter(new MensagemAdapter(this, mensagens));
          }
      }

      @Override
      public void onListItemClick(ListView parent, View view, int posicao,
              long id) {
          super.onListItemClick(parent, view, posicao, id);
          Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
          startActivityForResult(contactIntent,CONTATO_SELECIONADO);
      }

  }

2 个答案:

答案 0 :(得分:1)

至少发送您要测试的代码。这是机器人中单元测试的结构。

你的问题也不清楚

public class SimpleActivityTest extends
    ActivityInstrumentationTestCase2<SimpleActivity> {

  private Solo solo;

  public SimpleActivityTest() {
    super(SimpleActivity.class);
  }

  public void setUp() throws Exception {
   solo = new Solo(getInstrumentation(), getActivity());
  }

  @Override
  public void tearDown() throws Exception {
    solo.finishOpenedActivities();
  }

  public void testListItemClickShouldDisplayToast() throws Exception {
    // check that we have the right activity
    solo.assertCurrentActivity("wrong activity", SimpleActivity.class);

    // Click a button which will start a new Activity
    // Here we use the ID of the string to find the right button
    solo.clickOnButton(solo
        .getString(de.vogella.android.test.target.R.string.button1));
    // assert that the current activity is the SimpleListActivity.class
    solo.assertCurrentActivity("wrong activity", SimpleListActivity.class);
    solo.clickInList(1);
    // searchForText has a timeout of 5 seconds
    assertTrue(solo.waitForText("Android")); // Assertion
    solo.clickInList(2);
    assertTrue(solo.waitForText("iPhone")); // Assertion
    solo.clickInList(3);
    assertTrue(solo.waitForText("Blackberry")); // Assertion
    solo.goBack();
    solo.clickOnButton("Button2");
    solo.clickOnButton("Button3");
  }

  public void testListItemClickShouldDisplayToast() throws Exception {
    // open the menu
    solo.sendKey(Solo.MENU);
    solo.clickOnText("Preferences");
    solo.clickOnText("User");
    solo.clearEditText(0);
    Assert.assertTrue(solo.searchText(""));
    solo.enterText(0, "http//:www.vogella.com");
    Assert.assertTrue(solo.searchText("http//:www.vogella.com"));
    solo.goBack();

  }

} 

答案 1 :(得分:0)

我的问题是对Intent.ACTION_PICK的测试。如果我有一个动作选择,可以根据android选择联系人的号码,并获取联系人的号码。