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 */
16package android.support.design.widget;
17
18import android.support.annotation.LayoutRes;
19import android.support.design.test.R;
20import android.support.test.InstrumentationRegistry;
21import android.support.test.espresso.UiController;
22import android.support.test.espresso.ViewAction;
23import android.support.v4.view.ViewCompat;
24import android.view.View;
25import android.view.ViewStub;
26import org.hamcrest.Description;
27import org.hamcrest.Matcher;
28import org.hamcrest.TypeSafeMatcher;
29import org.junit.After;
30
31import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
32import static org.hamcrest.Matchers.allOf;
33import static org.hamcrest.Matchers.any;
34
35/**
36 * Base class for tests that are exercising various aspects of {@link CoordinatorLayout}.
37 */
38public abstract class BaseDynamicCoordinatorLayoutTest
39        extends BaseInstrumentationTestCase<DynamicCoordinatorLayoutActivity> {
40    protected CoordinatorLayout mCoordinatorLayout;
41
42    public BaseDynamicCoordinatorLayoutTest() {
43        super(DynamicCoordinatorLayoutActivity.class);
44    }
45
46    @After
47    public void tearDown() throws Exception {
48        // Now that the test is done, replace the activity content view with ViewStub so
49        // that it's ready to be replaced for the next test.
50        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
51            @Override
52            public void run() {
53                final DynamicCoordinatorLayoutActivity activity = mActivityTestRule.getActivity();
54                activity.setContentView(R.layout.dynamic_coordinator_layout);
55                mCoordinatorLayout = null;
56            }
57        });
58    }
59
60    /**
61     * Matches views that have parents.
62     */
63    private Matcher<View> hasParent() {
64        return new TypeSafeMatcher<View>() {
65            @Override
66            public void describeTo(Description description) {
67                description.appendText("has parent");
68            }
69
70            @Override
71            public boolean matchesSafely(View view) {
72                return view.getParent() != null;
73            }
74        };
75    }
76
77    /**
78     * Inflates the <code>ViewStub</code> with the passed layout resource.
79     */
80    protected ViewAction inflateViewStub(final @LayoutRes int layoutResId) {
81        return new ViewAction() {
82            @Override
83            public Matcher<View> getConstraints() {
84                return allOf(isAssignableFrom(ViewStub.class), hasParent());
85            }
86
87            @Override
88            public String getDescription() {
89                return "Inflates view stub";
90            }
91
92            @Override
93            public void perform(UiController uiController, View view) {
94                uiController.loopMainThreadUntilIdle();
95
96                ViewStub viewStub = (ViewStub) view;
97                viewStub.setLayoutResource(layoutResId);
98                viewStub.inflate();
99
100                mCoordinatorLayout = (CoordinatorLayout) mActivityTestRule.getActivity()
101                        .findViewById(viewStub.getInflatedId());
102
103                uiController.loopMainThreadUntilIdle();
104            }
105        };
106    }
107
108    protected ViewAction setLayoutDirection(final int layoutDir) {
109        return new ViewAction() {
110            @Override
111            public Matcher<View> getConstraints() {
112                return any(View.class);
113            }
114
115            @Override
116            public String getDescription() {
117                return "Sets layout direction";
118            }
119
120            @Override
121            public void perform(UiController uiController, View view) {
122                uiController.loopMainThreadUntilIdle();
123
124                ViewCompat.setLayoutDirection(view, layoutDir);
125
126                uiController.loopMainThreadUntilIdle();
127            }
128        };
129    }
130}
131