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