1// CHECKSTYLE:OFF Generated code
2/* This file is auto-generated from SingleFrgamentTestBase.java.  DO NOT MODIFY. */
3
4/*
5 * Copyright (C) 2016 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19package android.support.v17.leanback.app;
20
21import android.content.Intent;
22import android.os.SystemClock;
23import android.support.test.InstrumentationRegistry;
24import android.support.test.rule.ActivityTestRule;
25import android.support.v7.widget.RecyclerView;
26
27import org.junit.Rule;
28import org.junit.rules.TestName;
29
30public class SingleSupportFragmentTestBase {
31
32    private static final long WAIT_FOR_SCROLL_IDLE_TIMEOUT_MS = 60000;
33
34    @Rule
35    public TestName mUnitTestName = new TestName();
36
37    @Rule
38    public ActivityTestRule<SingleSupportFragmentTestActivity> activityTestRule =
39            new ActivityTestRule<>(SingleSupportFragmentTestActivity.class, false, false);
40
41    public void sendKeys(int ...keys) {
42        for (int i = 0; i < keys.length; i++) {
43            InstrumentationRegistry.getInstrumentation().sendKeyDownUpSync(keys[i]);
44        }
45    }
46
47    /**
48     * Options that will be passed throught Intent to SingleSupportFragmentTestActivity
49     */
50    public static class Options {
51        int mActivityLayoutId;
52        int mUiVisibility;
53
54        public Options() {
55        }
56
57        public Options activityLayoutId(int activityLayoutId) {
58            mActivityLayoutId = activityLayoutId;
59            return this;
60        }
61
62        public Options uiVisibility(int uiVisibility) {
63            mUiVisibility = uiVisibility;
64            return this;
65        }
66
67        public void collect(Intent intent) {
68            if (mActivityLayoutId != 0) {
69                intent.putExtra(SingleSupportFragmentTestActivity.EXTRA_ACTIVITY_LAYOUT,
70                        mActivityLayoutId);
71            }
72            if (mUiVisibility != 0) {
73                intent.putExtra(SingleSupportFragmentTestActivity.EXTRA_UI_VISIBILITY, mUiVisibility);
74            }
75        }
76    }
77
78    public SingleSupportFragmentTestActivity launchAndWaitActivity(Class fragmentClass, long waitTimeMs) {
79        return launchAndWaitActivity(fragmentClass.getName(), null, waitTimeMs);
80    }
81
82    public SingleSupportFragmentTestActivity launchAndWaitActivity(Class fragmentClass, Options options,
83            long waitTimeMs) {
84        return launchAndWaitActivity(fragmentClass.getName(), options, waitTimeMs);
85    }
86
87    public SingleSupportFragmentTestActivity launchAndWaitActivity(String firstFragmentName,
88            Options options, long waitTimeMs) {
89        Intent intent = new Intent();
90        intent.putExtra(SingleSupportFragmentTestActivity.EXTRA_FRAGMENT_NAME, firstFragmentName);
91        if (options != null) {
92            options.collect(intent);
93        }
94        SingleSupportFragmentTestActivity activity = activityTestRule.launchActivity(intent);
95        SystemClock.sleep(waitTimeMs);
96        return activity;
97    }
98
99    protected void waitForScrollIdle(RecyclerView recyclerView) throws Throwable {
100        waitForScrollIdle(recyclerView, null);
101    }
102
103    protected void waitForScrollIdle(RecyclerView recyclerView, Runnable verify) throws Throwable {
104        Thread.sleep(100);
105        int total = 0;
106        while (recyclerView.getLayoutManager().isSmoothScrolling()
107                || recyclerView.getScrollState() != recyclerView.SCROLL_STATE_IDLE) {
108            if ((total += 100) >= WAIT_FOR_SCROLL_IDLE_TIMEOUT_MS) {
109                throw new RuntimeException("waitForScrollIdle Timeout");
110            }
111            try {
112                Thread.sleep(100);
113            } catch (InterruptedException ex) {
114                break;
115            }
116            if (verify != null) {
117                activityTestRule.runOnUiThread(verify);
118            }
119        }
120    }
121
122}
123