SingleFragmentTestBase.java revision e2104f4b5c8e3ad63570306a25e61502dfe4c418
1/*
2 * Copyright (C) 2016 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.v17.leanback.app;
17
18import android.content.Intent;
19import android.os.SystemClock;
20import android.support.test.InstrumentationRegistry;
21import android.support.test.rule.ActivityTestRule;
22import android.support.v17.leanback.testutils.PollingCheck;
23
24import org.junit.After;
25import org.junit.Rule;
26import org.junit.rules.TestName;
27
28public class SingleFragmentTestBase {
29
30    @Rule
31    public TestName mUnitTestName = new TestName();
32
33    @Rule
34    public ActivityTestRule<SingleFragmentTestActivity> activityTestRule =
35            new ActivityTestRule<>(SingleFragmentTestActivity.class, false, false);
36
37    protected SingleFragmentTestActivity mActivity;
38
39    @After
40    public void afterTest() throws Throwable {
41        final SingleFragmentTestActivity activity = mActivity;
42        if (activity != null) {
43            mActivity = null;
44            activityTestRule.runOnUiThread(new Runnable() {
45                @Override
46                public void run() {
47                    activity.finish();
48                }
49            });
50            PollingCheck.waitFor(new PollingCheck.ActivityDestroy(activity));
51        }
52    }
53
54    public void sendKeys(int ...keys) {
55        for (int i = 0; i < keys.length; i++) {
56            InstrumentationRegistry.getInstrumentation().sendKeyDownUpSync(keys[i]);
57        }
58    }
59
60    /**
61     * Options that will be passed throught Intent to SingleFragmentTestActivity
62     */
63    public static class Options {
64        int mActivityLayoutId;
65        int mUiVisibility;
66
67        public Options() {
68        }
69
70        public Options activityLayoutId(int activityLayoutId) {
71            mActivityLayoutId = activityLayoutId;
72            return this;
73        }
74
75        public Options uiVisibility(int uiVisibility) {
76            mUiVisibility = uiVisibility;
77            return this;
78        }
79
80        public void collect(Intent intent) {
81            if (mActivityLayoutId != 0) {
82                intent.putExtra(SingleFragmentTestActivity.EXTRA_ACTIVITY_LAYOUT,
83                        mActivityLayoutId);
84            }
85            if (mUiVisibility != 0) {
86                intent.putExtra(SingleFragmentTestActivity.EXTRA_UI_VISIBILITY, mUiVisibility);
87            }
88        }
89    }
90
91    public void launchAndWaitActivity(Class fragmentClass, long waitTimeMs) {
92        launchAndWaitActivity(fragmentClass.getName(), null, waitTimeMs);
93    }
94
95    public void launchAndWaitActivity(Class fragmentClass, Options options, long waitTimeMs) {
96        launchAndWaitActivity(fragmentClass.getName(), options, waitTimeMs);
97    }
98
99    public void launchAndWaitActivity(String firstFragmentName, Options options, long waitTimeMs) {
100        Intent intent = new Intent();
101        intent.putExtra(SingleFragmentTestActivity.EXTRA_FRAGMENT_NAME, firstFragmentName);
102        if (options != null) {
103            options.collect(intent);
104        }
105        mActivity = activityTestRule.launchActivity(intent);
106        SystemClock.sleep(waitTimeMs);
107    }
108}
109