2013-07-08 33 views
5

Tôi đang cố gắng thử nghiệm một số AlertDialog với ActivityInstrumentationTestCase2.Làm cách nào để kiểm tra AlertDialog trong Android?

Đây là mã gốc:

this.setmBtAppelerFixe(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      AlertDialog.Builder dialog = new AlertDialog.Builder(InterventionImmobiliereDetailsActivity.this); 
      dialog.setTitle("Appel"); 
      dialog.setMessage("Appeler le contact ?"); 
      dialog.setCancelable(true); 
      dialog.setNegativeButton("Non", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.dismiss(); 
       } 
      }); 
      dialog.setPositiveButton("Oui", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        InterventionImmobiliereDetailsActivity.this.lancerIntentAppel(mIntervention.getTelContact()); 
       } 
      }); 

      mAdAppelerFixe = dialog.create(); 
      mAdAppelerFixe.show(); 
     } 
    }); 

Bây giờ tôi không thể quản lý để nhấp vào nút tích cực. Mã này dường như không hoạt động:

mActivity.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      assertTrue(mLLAppelerFixe.performClick()); 

      AlertDialog mDialog = mActivity.getAdAppelerFixe(); 
      assertTrue(mDialog.isShowing()); 

      Button okButton = mDialog.getButton(AlertDialog.BUTTON_POSITIVE); 

      assertTrue(okButton.performClick()); 
      assertTrue(mActivity.isNumeroValide()); 
     } 
    }); 

Trước tiên, tôi nhấp vào bố cục để mở AlertDialog. Sau đó, tôi nhận được OK_BUTTON và tôi thực hiện một cú nhấp chuột vào nó. Nó phải đặt boolean numeroValide thành sự thật. Nhưng không có gì.

Tôi có thể chỉ cần thử nghiệm một nút AlertDialog bằng cách nào?

Trả lời

3

này đang làm việc một cách hoàn hảo trong thiết bị Nexus 4 của tôi:

@MediumTest 
public void testStartMyActivity() { 
    monitor = getInstrumentation().addMonitor(MyActivity.class.getName(), null, false); 

    TouchUtils.clickView(this, startMyActivityButton); 

    MyActivity myActivity = (MyActivity) monitor.waitForActivityWithTimeout(2000); 
    assertNotNull("MyActivity activity not started, activity is null", myActivity); 

    AlertDialog dialog = myActivity.getLastDialog(); // I create getLastDialog method in MyActivity class. Its return last created AlertDialog 
    if (dialog.isShowing()) { 
     try { 
      performClick(dialog.getButton(DialogInterface.BUTTON_POSITIVE)); 
     } catch (Throwable e) { 
      e.printStackTrace(); 
     } 
    } 

    myActivity.finish(); 
    getInstrumentation().removeMonitor(monitor); 
} 

private void performClick(final Button button) throws Throwable { 
    runTestOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      button.performClick(); 
     } 
    }); 
    getInstrumentation().waitForIdleSync(); 
} 

Ở đây ví dụ thử nghiệm AlertDialog (từ nguồn google android): AlertDialogTest.java

0

Bạn có thể sử dụng Espresso cho bây giờ với mã

onView(withText("South China Sea")) 
    .inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView())))) 
    .perform(click()); 

Xem EspressoSamples

2

Trong hoạt động ban đầu của bạn, bạn có thể tạo một phương thức đơn giản để trả về cá thể của AlertDialog cuối cùng.

public AlertDialog getDialog(){ 
    return alertDialog; 
} 

Trong hoạt động kiểm tra, bạn có thể truy cập hộp thoại cảnh báo và nhấp vào nút bằng mã sau.

ActivityMonitor monitor = getInstrumentation().addMonitor(MyActivity.class.getName(), null, false); 
MyActivity myActivity = (MyActivity) monitor.waitForActivity(); 
getInstrumentation().waitForIdleSync();  

// access the alert dialog using the getDialog() method created in the activity 
AlertDialog dialog = myActivity.getDialog(); 

// access the button 
Button okBtn = (Button) dialog.findViewById(R.id.button_ok); 
TouchUtils.clickView(this, okBtn); 
getInstrumentation().removeMonitor(monitor); 
Các vấn đề liên quan