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