2015-05-20 10 views
6

का निरीक्षण किया और दावा है कि सूचीदृश्य एंड्रॉयड एस्प्रेसो के साथ की उम्मीद आकार है करने के लिए सबसे अच्छा तरीका क्या है के साथ इस सूची में वस्तुओं का उचित संख्या?जोर एस्प्रेसो

मैं इस मेल खाने लिखा था, लेकिन काफी कैसे परीक्षण में एकीकृत करने के लिए पता नहीं है।

public static Matcher<View> withListSize (final int size) { 
    return new TypeSafeMatcher<View>() { 
     @Override public boolean matchesSafely (final View view) { 
      return ((ListView) view).getChildCount() == size; 
     } 

     @Override public void describeTo (final Description description) { 
      description.appendText ("ListView should have " + size + " items"); 
     } 
    }; 
} 
+0

तो एस्प्रेसो अच्छा है, एक सूची में चीजों को कोई फर्क नहीं पड़ता। अब एक के लिए जा रहा है। –

उत्तर

13

यह पता लगाया। यदि सूची के एक आइटम उम्मीद

class Matchers { 
    public static Matcher<View> withListSize (final int size) { 
    return new TypeSafeMatcher<View>() { 
     @Override public boolean matchesSafely (final View view) { 
     return ((ListView) view).getCount() == size; 
     } 

     @Override public void describeTo (final Description description) { 
     description.appendText ("ListView should have " + size + " items"); 
     } 
    }; 
    } 
} 

, वास्तविक परीक्षा लिपि में इस डाल दिया।

onView (withId (android.R.id.list)).check (ViewAssertions.matches (Matchers.withListSize (1))); 
+0

मैं विधि Matchers.withListSize (1) ... क्या आप मुझे पूरे classpath और matchers की शायद प्रासंगिक संस्करण बता सकते हैं खोजने के लिए प्रतीत नहीं कर सकते हैं? – gleenn

+0

विधि प्रश्न में है और मेरे द्वारा लिखा गया था। इसे अपने खुद के Matchhers वर्ग में रखो। –

+0

विधि सवाल में है और मेरे द्वारा लिखा गया था। इसे अपने खुद के Matchhers वर्ग में रखो। मैंने इसे अभी जवाब में जोड़ा है। आपको अपनी एस्प्रेसो टेस्ट स्क्रिप्ट के संबंध में कहां रखा गया है, इसके आधार पर आपको कक्षा को सार्वजनिक करने की आवश्यकता हो सकती है। –

4

एस्प्रेसो के साथ सूची में गिनती हो रही वस्तुओं की दो अलग-अलग दृष्टिकोण हैं: के रूप में @CoryRoy ऊपर उल्लेख किया पहले एक है - TypeSafeMatcher, एक दूसरे BoundedMatcher उपयोग करने के लिए है का उपयोग करते हुए

और क्योंकि @CoryRoy पहले से ही पता चला है कि यह कैसे बात पर जोर देना है, यहाँ मैं कैसे (वापसी) प्राप्त करने के लिए बताने के लिए अलग matchers का उपयोग कर संख्या चाहते हैं।

public class CountHelper { 

    private static int count; 

    public static int getCountFromListUsingTypeSafeMatcher(@IdRes int listViewId) { 
     count = 0; 

     Matcher matcher = new TypeSafeMatcher<View>() { 
      @Override 
      protected boolean matchesSafely(View item) { 
       count = ((ListView) item).getCount(); 
       return true; 
      } 

      @Override 
      public void describeTo(Description description) { 
      } 
     }; 

     onView(withId(listViewId)).check(matches(matcher)); 

     int result = count; 
     count = 0; 
     return result; 
    } 

    public static int getCountFromListUsingBoundedMatcher(@IdRes int listViewId) { 
     count = 0; 

     Matcher<Object> matcher = new BoundedMatcher<Object, String>(String.class) { 
      @Override 
      protected boolean matchesSafely(String item) { 
       count += 1; 
       return true; 
      } 

      @Override 
      public void describeTo(Description description) { 
      } 
     }; 

     try { 
      // do a nonsense operation with no impact 
      // because ViewMatchers would only start matching when action is performed on DataInteraction 
      onData(matcher).inAdapterView(withId(listViewId)).perform(typeText("")); 
     } catch (Exception e) { 
     } 

     int result = count; 
     count = 0; 
     return result; 
    } 

} 

भी है कि आप ListView#getChildCount() के बजाय ListView#getCount() का उपयोग करना चाहिए उल्लेख करना चाहते हैं:

  • getCount() - एडाप्टर के स्वामित्व वाला डेटा आइटम, जो दृष्टिगोचर दृश्यों की संख्या से अधिक हो सकता है की संख्या।
  • getChildCount() - में देखें बच्चों की संख्या ViewGroup, जिसे व्यू ग्रुप द्वारा पुन: उपयोग किया जा सकता है।
संबंधित मुद्दे