1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.support.v4.testutils; 18 19 import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 20 21 import android.support.test.espresso.NoMatchingViewException; 22 import android.support.test.espresso.ViewAssertion; 23 import android.view.View; 24 import android.view.ViewGroup; 25 26 public class TestUtilsAssertions { 27 28 /** 29 * Returns an assertion that asserts that there is specified number of fully displayed 30 * children. 31 */ hasDisplayedChildren(final int expectedCount)32 public static ViewAssertion hasDisplayedChildren(final int expectedCount) { 33 return new ViewAssertion() { 34 @Override 35 public void check(final View foundView, NoMatchingViewException noViewException) { 36 if (noViewException != null) { 37 throw noViewException; 38 } else { 39 if (!(foundView instanceof ViewGroup)) { 40 throw new AssertionError("View " 41 + foundView.getClass().getSimpleName() + " is not a ViewGroup"); 42 } 43 final ViewGroup foundGroup = (ViewGroup) foundView; 44 45 final int childrenCount = foundGroup.getChildCount(); 46 int childrenDisplayedCount = 0; 47 for (int i = 0; i < childrenCount; i++) { 48 if (isDisplayed().matches(foundGroup.getChildAt(i))) { 49 childrenDisplayedCount++; 50 } 51 } 52 53 if (childrenDisplayedCount != expectedCount) { 54 throw new AssertionError("Expected " + expectedCount 55 + " displayed children, but found " + childrenDisplayedCount); 56 } 57 } 58 } 59 }; 60 } 61 }