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