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 androidx.leanback.app;
17
18import static org.mockito.Mockito.mock;
19import static org.mockito.Mockito.timeout;
20import static org.mockito.Mockito.verify;
21import static org.mockito.Mockito.when;
22
23import android.content.Intent;
24import android.support.test.InstrumentationRegistry;
25import android.support.test.rule.ActivityTestRule;
26import android.view.View;
27
28import androidx.leanback.R;
29import androidx.leanback.testutils.PollingCheck;
30
31import org.junit.Before;
32import org.junit.Rule;
33import org.junit.rules.TestName;
34
35public class GuidedStepSupportFragmentTestBase {
36
37    private static final long TIMEOUT = 5000;
38
39    @Rule public TestName mUnitTestName = new TestName();
40
41    @Rule
42    public ActivityTestRule<GuidedStepSupportFragmentTestActivity> activityTestRule =
43            new ActivityTestRule<>(GuidedStepSupportFragmentTestActivity.class, false, false);
44
45    @Before
46    public void clearTests() {
47        GuidedStepTestSupportFragment.clearTests();
48    }
49
50    public static class ExpandTransitionFinish extends PollingCheck.PollingCheckCondition {
51        GuidedStepTestSupportFragment.Provider mProvider;
52
53        public ExpandTransitionFinish(GuidedStepTestSupportFragment.Provider provider) {
54            mProvider = provider;
55        }
56
57        @Override
58        public boolean canPreProceed() {
59            return false;
60        }
61
62        @Override
63        public boolean canProceed() {
64            GuidedStepTestSupportFragment fragment = mProvider.getFragment();
65            if (fragment != null && fragment.getView() != null) {
66                if (!fragment.getGuidedActionsStylist().isInExpandTransition()) {
67                    // expand transition finishes
68                    return true;
69                }
70            }
71            return false;
72        }
73    }
74
75    public static void waitOnDestroy(GuidedStepTestSupportFragment.Provider provider,
76            int times) {
77        verify(provider, timeout((int)TIMEOUT).times(times)).onDestroy();
78    }
79
80    public static class EnterTransitionFinish extends PollingCheck.PollingCheckCondition {
81        PollingCheck.ViewScreenPositionDetector mDector =
82                new PollingCheck.ViewScreenPositionDetector();
83
84        GuidedStepTestSupportFragment.Provider mProvider;
85
86        public EnterTransitionFinish(GuidedStepTestSupportFragment.Provider provider) {
87            mProvider = provider;
88        }
89        @Override
90        public boolean canProceed() {
91            GuidedStepTestSupportFragment fragment = mProvider.getFragment();
92            if (fragment != null && fragment.getView() != null) {
93                View view = fragment.getView().findViewById(R.id.guidance_title);
94                if (view != null) {
95                    if (mDector.isViewStableOnScreen(view)) {
96                        return true;
97                    }
98                }
99            }
100            return false;
101        }
102    }
103
104    public static void sendKey(int keyCode) {
105        InstrumentationRegistry.getInstrumentation().sendKeyDownUpSync(keyCode);
106    }
107
108    public String generateMethodTestName(String testName) {
109        return mUnitTestName.getMethodName() + "_" + testName;
110    }
111
112    public GuidedStepSupportFragmentTestActivity launchTestActivity(String firstTestName) {
113        Intent intent = new Intent();
114        intent.putExtra(GuidedStepSupportFragmentTestActivity.EXTRA_TEST_NAME, firstTestName);
115        return activityTestRule.launchActivity(intent);
116    }
117
118    public GuidedStepSupportFragmentTestActivity launchTestActivity(String firstTestName,
119            boolean addAsRoot) {
120        Intent intent = new Intent();
121        intent.putExtra(GuidedStepSupportFragmentTestActivity.EXTRA_TEST_NAME, firstTestName);
122        intent.putExtra(GuidedStepSupportFragmentTestActivity.EXTRA_ADD_AS_ROOT, addAsRoot);
123        return activityTestRule.launchActivity(intent);
124    }
125
126    public GuidedStepSupportFragmentTestActivity launchTestActivity(String firstTestName,
127            boolean addAsRoot, int layoutDirection) {
128        Intent intent = new Intent();
129        intent.putExtra(GuidedStepSupportFragmentTestActivity.EXTRA_TEST_NAME, firstTestName);
130        intent.putExtra(GuidedStepSupportFragmentTestActivity.EXTRA_ADD_AS_ROOT, addAsRoot);
131        intent.putExtra(GuidedStepSupportFragmentTestActivity.EXTRA_LAYOUT_DIRECTION, layoutDirection);
132        return activityTestRule.launchActivity(intent);
133    }
134
135    public GuidedStepTestSupportFragment.Provider mockProvider(String testName) {
136        GuidedStepTestSupportFragment.Provider test = mock(GuidedStepTestSupportFragment.Provider.class);
137        when(test.getActivity()).thenCallRealMethod();
138        when(test.getFragmentManager()).thenCallRealMethod();
139        when(test.getFragment()).thenCallRealMethod();
140        GuidedStepTestSupportFragment.setupTest(testName, test);
141        return test;
142    }
143}
144
145