2014-09-24 103 views
0

我有一个Activity使用startActivityForResult进行联系ACTION_PICK。首先,我的测试是选择联系人,并在选中联系后进行检查。Android测试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); 
     } 

    } 

回答

1

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

你的问题也不清楚

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(); 

    } 

} 
+0

我把代码举例在后。 – 2014-09-24 20:08:30

0

的,我的问题是一个测试,Intent.ACTION_PICK。在这种情况下,我有一个动作选择从Android的角度来选择一个联系人的号码,并获得联系人的号码。