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