2014-05-22 7 views
5

Ho una finestra di avviso che visualizza un numero di elementi.Utilizzo di Espresso come controllo il numero di elementi nel mio avviso Finestra di dialogo

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

In circostanze diverse (per esempio se sei un admin/guest) si avrà un diverso numero di "scelte". Nel mio test del caffè, sto praticamente cercando di testare il numero di articoli presenti.

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)) 
                            )));*/ 
} 

Si tratta in sostanza generano un'eccezione in questo modo:

presumo la chiamata onData è solo per l'esecuzione di azioni? Non sto eseguendo un'azione necessariamente. Qualcuno potrebbe aiutarmi a testare questo?

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 dovrebbe consentire controlli pure. Hai provato qualcosa del tipo: onData (allOf (is (instanceOf (String.class)), equalTo (string))). AtPosition (lastExpectedIndex) .check (); – Maxwell

risposta

0

ho avuto un problema simile in passato, ma mi è stato di aggiungere il mio controllo EditText alla finestra di avviso. Quando lo fai in questo modo puoi assegnare un ID all'oggetto vista che viene aggiunto alla finestra di dialogo.

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

L'id add_comment_text è specificato in un file ids.xml in res/valori e quindi è possibile selezionare questa utilizzando.

onView(withId(R.id.myEditText) 
0

So che questo è piuttosto un vecchio post, ma sembra che quello che vuoi è qualcosa di simile a ciò che viene descritto da questo blog post here. È quindi possibile accedere alla finestra di dialogo con un verificatore che corrisponde alla vista della finestra di dialogo e chiamare qualcosa come getChildCount() o simile metodo correlato alla finestra di dialogo? Speriamo che questo possa aiutare qualcuno!

+0

Questo link sembra essere morto. – ThomasW

7

questo è quello che è venuto fuori con l'aggiunta di un metodo a qualsiasi libreria di abbinamenti personalizzati che si utilizza.

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

L'uso è:

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