2014-09-24 95 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的角度來選擇一個聯繫人的號碼,並獲得聯繫人的號碼。