BrowseSupportFragmentTest.java revision cc671dd3caac1d0cbf3f6999ab063c9ff1f297b2
1// CHECKSTYLE:OFF Generated code 2/* This file is auto-generated from BrowseFragmentTest.java. DO NOT MODIFY. */ 3 4/* 5 * Copyright (C) 2015 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 static org.junit.Assert.assertEquals; 22import static org.junit.Assert.assertNotNull; 23import static org.junit.Assert.assertNull; 24import static org.mockito.Matchers.any; 25import static org.mockito.Mockito.timeout; 26import static org.mockito.Mockito.verify; 27 28import android.content.Intent; 29import android.support.test.InstrumentationRegistry; 30import android.support.test.filters.MediumTest; 31import android.support.test.rule.ActivityTestRule; 32import android.support.test.runner.AndroidJUnit4; 33import android.support.v17.leanback.widget.ListRowPresenter; 34import android.support.v17.leanback.widget.Presenter; 35import android.view.KeyEvent; 36 37import org.junit.After; 38import org.junit.Rule; 39import org.junit.Test; 40import org.junit.runner.RunWith; 41import org.mockito.Mockito; 42 43@MediumTest 44@RunWith(AndroidJUnit4.class) 45public class BrowseSupportFragmentTest { 46 47 static final long TRANSITION_LENGTH = 1000; 48 static final long HORIZONTAL_SCROLL_WAIT = 2000; 49 50 @Rule 51 public ActivityTestRule<BrowseSupportFragmentTestActivity> activityTestRule = 52 new ActivityTestRule<>(BrowseSupportFragmentTestActivity.class, false, false); 53 private BrowseSupportFragmentTestActivity mActivity; 54 55 @After 56 public void afterTest() throws Throwable { 57 activityTestRule.runOnUiThread(new Runnable() { 58 public void run() { 59 if (mActivity != null) { 60 mActivity.finish(); 61 mActivity = null; 62 } 63 } 64 }); 65 } 66 67 @Test 68 public void testTwoBackKeysWithBackStack() throws Throwable { 69 final long dataLoadingDelay = 1000; 70 Intent intent = new Intent(); 71 intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay); 72 intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , true); 73 mActivity = activityTestRule.launchActivity(intent); 74 75 Thread.sleep(dataLoadingDelay + TRANSITION_LENGTH); 76 77 assertNotNull(mActivity.getBrowseTestSupportFragment().getMainFragment()); 78 sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT); 79 Thread.sleep(TRANSITION_LENGTH); 80 sendKeys(KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_BACK); 81 } 82 83 @Test 84 public void testTwoBackKeysWithoutBackStack() throws Throwable { 85 final long dataLoadingDelay = 1000; 86 Intent intent = new Intent(); 87 intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay); 88 intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , false); 89 mActivity = activityTestRule.launchActivity(intent); 90 91 Thread.sleep(dataLoadingDelay + TRANSITION_LENGTH); 92 93 assertNotNull(mActivity.getBrowseTestSupportFragment().getMainFragment()); 94 sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT); 95 Thread.sleep(TRANSITION_LENGTH); 96 sendKeys(KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_BACK); 97 } 98 99 @Test 100 public void testPressRightBeforeMainFragmentCreated() throws Throwable { 101 final long dataLoadingDelay = 1000; 102 Intent intent = new Intent(); 103 intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay); 104 intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , false); 105 mActivity = activityTestRule.launchActivity(intent); 106 107 assertNull(mActivity.getBrowseTestSupportFragment().getMainFragment()); 108 sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT); 109 } 110 111 @Test 112 public void testSelectCardOnARow() throws Throwable { 113 final int selectRow = 10; 114 final int selectItem = 20; 115 Intent intent = new Intent(); 116 final long dataLoadingDelay = 1000; 117 intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay); 118 intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , true); 119 mActivity = activityTestRule.launchActivity(intent); 120 121 Thread.sleep(dataLoadingDelay + TRANSITION_LENGTH); 122 123 Presenter.ViewHolderTask itemTask = Mockito.spy( 124 new ItemSelectionTask(mActivity, selectRow)); 125 126 final ListRowPresenter.SelectItemViewHolderTask task = 127 new ListRowPresenter.SelectItemViewHolderTask(selectItem); 128 task.setItemTask(itemTask); 129 130 mActivity.runOnUiThread(new Runnable() { 131 @Override 132 public void run() { 133 mActivity.getBrowseTestSupportFragment().setSelectedPosition(selectRow, true, task); 134 } 135 }); 136 137 verify(itemTask, timeout(5000).times(1)).run(any(Presenter.ViewHolder.class)); 138 139 ListRowPresenter.ViewHolder row = (ListRowPresenter.ViewHolder) mActivity 140 .getBrowseTestSupportFragment().getRowsSupportFragment().getRowViewHolder(selectRow); 141 assertEquals(selectItem, row.getGridView().getSelectedPosition()); 142 } 143 144 @Test 145 public void activityRecreate_notCrash() throws Throwable { 146 final long dataLoadingDelay = 1000; 147 Intent intent = new Intent(); 148 intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay); 149 intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , false); 150 intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_SET_ADAPTER_AFTER_DATA_LOAD, true); 151 mActivity = activityTestRule.launchActivity(intent); 152 153 Thread.sleep(dataLoadingDelay + TRANSITION_LENGTH); 154 155 InstrumentationRegistry.getInstrumentation().callActivityOnRestart(mActivity); 156 activityTestRule.runOnUiThread(new Runnable() { 157 @Override 158 public void run() { 159 mActivity.recreate(); 160 } 161 }); 162 } 163 164 private void sendKeys(int ...keys) { 165 for (int i = 0; i < keys.length; i++) { 166 InstrumentationRegistry.getInstrumentation().sendKeyDownUpSync(keys[i]); 167 } 168 } 169 170 public static class ItemSelectionTask extends Presenter.ViewHolderTask { 171 172 private final BrowseSupportFragmentTestActivity activity; 173 private final int expectedRow; 174 175 ItemSelectionTask(BrowseSupportFragmentTestActivity activity, int expectedRow) { 176 this.activity = activity; 177 this.expectedRow = expectedRow; 178 } 179 180 public void run(Presenter.ViewHolder holder) { 181 assertEquals(expectedRow, activity.getBrowseTestSupportFragment().getSelectedPosition()); 182 } 183 } 184} 185