2014-05-22 2 views
5

Ich habe einen Alarmdialog, der eine Reihe von Elementen anzeigt.Verwenden von Espresso Wie überprüfe ich die Anzahl der Elemente in meinem Alarm Dialog

private String[] choices; // populated externally 

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder.setItems(choices, new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      _callback.onChoiceSelected(which); 
      dialog.dismiss(); 
     } 
    }); 

    _dialog = builder.create(); 

Unter anderen Umständen (zum Beispiel, wenn Sie einen admin/Gast sind) werden Sie eine andere Anzahl von „Auswahl“ haben. In meinem Espresso-Test versuche ich im Grunde, die Anzahl der vorhandenen Gegenstände zu testen.

public void test_ViewsSetupCorrectly() { 

    onView(withId(R.id.settings)).perform(click()); 
    onView(ViewMatchers.withChild(withClassName(Matchers.equalToIgnoringCase("TextView")))).check(matches(isDisplayed())); 

    /*// What I would ideally like ... but doesn't work as well :(: 
     onView(withClassName(equalToIgnoringWhiteSpace("DialogTitle"))).check(matches(hasProperty("child-count", 
                             is(new Integer(1)) 
                            )));*/ 
} 

werfen Diese im Grunde eine Ausnahme wie so:

nehme ich die onData Anruf Aktionen nur für die Durchführung ist? Ich mache nicht unbedingt eine Aktion. Könnte mir jemand helfen, das tatsächlich zu testen?

com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: No views in hierarchy found matching: has child: with class name: eqIgnoringCase("TextView") 
    If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:[email protected]c 

    View Hierarchy: 
    +>DecorView{id=-1, visibility=VISIBLE, width=684, height=226, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} 
    | 
    +->FrameLayout{id=-1, visibility=VISIBLE, width=684, height=226, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} 
    | 
    +-->FrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=684, height=226, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} 
    | 
    +--->LinearLayout{id=16908874, res-name=parentPanel, visibility=VISIBLE, width=652, height=226, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=16.0, y=0.0, child-count=4} 
    | 
    +---->LinearLayout{id=16908875, res-name=topPanel, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3} 
    | 
    +----->View{id=16908885, res-name=titleDividerTop, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} 
    | 
    +----->LinearLayout{id=16908876, res-name=title_template, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} 
    | 
    +------>ImageView{id=16908294, res-name=icon, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} 
    | 
    +------>DialogTitle{id=16908877, res-name=alertTitle, visibility=VISIBLE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false} 
    | 
    +----->View{id=16908878, res-name=titleDivider, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} 
    | 
    +---->LinearLayout{id=16908882, res-name=contentPanel, visibility=VISIBLE, width=652, height=226, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} 
    | 
    +----->RecycleListView{id=16909101, res-name=select_dialog_listview, visibility=VISIBLE, width=620, height=194, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=true, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=16.0, y=16.0, child-count=2} 
    | 
    +------>TextView{id=16908308, res-name=text1, visibility=VISIBLE, width=620, height=96, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Edit Info, input-type=0, ime-target=false} 
    | 
    +------>TextView{id=16908308, res-name=text1, visibility=VISIBLE, width=620, height=96, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=98.0, text=Use gallery photo, input-type=0, ime-target=false} 
    | 
    +---->FrameLayout{id=16908884, res-name=customPanel, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} 
    | 
    +----->FrameLayout{id=16908331, res-name=custom, visibility=VISIBLE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
    | 
    +---->LinearLayout{id=16908879, res-name=buttonPanel, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} 
    | 
    +----->LinearLayout{id=-1, visibility=VISIBLE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3} 
    | 
    +------>Button{id=16908314, res-name=button2, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false} 
    | 
    +------>Button{id=16908315, res-name=button3, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false} 
    | 
    +------>Button{id=16908313, res-name=button1, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false} 
    | 
    +-->ViewStub{id=16909076, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} 
    | 
    at dalvik.system.VMStack.getThreadStackTrace(Native Method) 
    at java.lang.Thread.getStackTrace(Thread.java:591) 
    at com.google.android.apps.common.testing.ui.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:69) 
    at com.google.android.apps.common.testing.ui.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:40) 
    at com.google.android.apps.common.testing.ui.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:159) 
    at com.google.android.apps.common.testing.ui.espresso.ViewInteraction.check(ViewInteraction.java:133) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214) 
    at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199) 
    at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192) 
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190) 
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175) 
    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) 
    at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167) 
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584) 
+0

OnData sollten auch Kontrollen ermöglichen. Haben Sie versucht, etwas wie: onData (allOf (ist (instanceOf (String.class)), equalTo (string))). AtPosition (lastExpectedIndex) .check (<überprüfen Sie hier>); – Maxwell

Antwort

0

Ich habe ein ähnliches Problem in der Vergangenheit hatte, aber ich war das Hinzufügen meiner eigenen EditText Steuerung an den Alarm-Dialog. Auf diese Weise können Sie dem Ansichtsobjekt, das dem Dialog hinzugefügt wird, eine ID zuweisen.

AlertDialog.Builder myDialog = new AlertDialog.Builder(this); 

myDialog.setTitle(R.string.add_comment); 

// Set an EditText view to get user input 
final EditText input = new EditText(this); 
input.setId(R.id.myEditText); 
myDialog.setView(input); 

Die add_comment_text id wird in einer ids.xml Datei in res/Werte angegeben und dann können Sie diese mit Hilfe wählen.

onView(withId(R.id.myEditText) 
0

Ich weiß, das ist eine ganz alte Post, aber es sieht aus wie das, was Sie wollen so etwas wie ist das, was diese blog post here beschrieben. Sie könnten dann auf den Dialog mit einem Matcher zugreifen, der mit der Dialogansicht übereinstimmt und etwas wie getChildCount() oder eine ähnliche Methode aufrufen, die mit dem Dialog zusammenhängt? Hoffentlich hilft das jemandem!

+0

Dieser Link scheint tot zu sein. – ThomasW

7

Dies ist, was ich mit add-Methode zu was auch immer benutzerdefinierte Matcher-Bibliothek, die Sie verwenden.

public static Matcher<View> hasChildren(final Matcher<Integer> numChildrenMatcher) { 
     return new TypeSafeMatcher<View>() { 

      /** 
      * matching with viewgroup.getChildCount() 
      */ 
      @Override 
      public boolean matchesSafely(View view) { 
       return view instanceof ViewGroup && numChildrenMatcher.matches(((ViewGroup)view).getChildCount()); 
      } 

      /** 
      * gets the description 
      */ 
      @Override 
      public void describeTo(Description description) { 
       description.appendText(" a view with # children is "); 
       numChildrenMatcher.describeTo(description); 
      } 
     }; 
    } 

Verbrauch ist:

int numKids = 3; 
onView(withId(R.id.view_pager)).check(matches(allOf(
        isDisplayed(), 
        hasChildren(is(numKids)) 
      ))); 
Verwandte Themen