| /* |
| * Copyright (C) 2015 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package android.support.v17.leanback.app; |
| |
| import android.support.v17.leanback.test.R; |
| import android.view.KeyEvent; |
| |
| import android.support.v17.leanback.widget.Presenter; |
| import android.support.v17.leanback.widget.ListRowPresenter; |
| import android.content.Intent; |
| import org.junit.Rule; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| |
| import android.support.test.InstrumentationRegistry; |
| import android.support.test.runner.AndroidJUnit4; |
| import android.support.test.rule.ActivityTestRule; |
| import android.support.test.espresso.action.ViewActions; |
| import org.mockito.Mockito; |
| |
| import static org.junit.Assert.assertEquals; |
| import static org.junit.Assert.assertNull; |
| import static org.junit.Assert.assertNotNull; |
| import static org.junit.Assert.assertTrue; |
| import static org.mockito.Matchers.any; |
| import static org.mockito.Mockito.timeout; |
| import static org.mockito.Mockito.verify; |
| |
| /** |
| * @hide from javadoc |
| */ |
| @RunWith(AndroidJUnit4.class) |
| public class BrowseFragmentTest { |
| |
| static final long TRANSITION_LENGTH = 1000; |
| static final long HORIZONTAL_SCROLL_WAIT = 2000; |
| |
| @Rule |
| public ActivityTestRule<BrowseFragmentTestActivity> activityTestRule |
| = new ActivityTestRule<>(BrowseFragmentTestActivity.class, false, false); |
| private BrowseFragmentTestActivity mActivity; |
| |
| @Test |
| public void testTwoBackKeysWithBackStack() throws Throwable { |
| final long dataLoadingDelay = 1000; |
| Intent intent = new Intent(); |
| intent.putExtra(BrowseFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay); |
| intent.putExtra(BrowseFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , true); |
| mActivity = activityTestRule.launchActivity(intent); |
| |
| Thread.sleep(dataLoadingDelay + TRANSITION_LENGTH); |
| |
| assertNotNull(mActivity.getBrowseTestFragment().getMainFragment()); |
| sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT); |
| Thread.sleep(TRANSITION_LENGTH); |
| sendKeys(KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_BACK); |
| } |
| |
| @Test |
| public void testTwoBackKeysWithoutBackStack() throws Throwable { |
| final long dataLoadingDelay = 1000; |
| Intent intent = new Intent(); |
| intent.putExtra(BrowseFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay); |
| intent.putExtra(BrowseFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , false); |
| mActivity = activityTestRule.launchActivity(intent); |
| |
| Thread.sleep(dataLoadingDelay + TRANSITION_LENGTH); |
| |
| assertNotNull(mActivity.getBrowseTestFragment().getMainFragment()); |
| sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT); |
| Thread.sleep(TRANSITION_LENGTH); |
| sendKeys(KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_BACK); |
| } |
| |
| @Test |
| public void testPressRightBeforeMainFragmentCreated() throws Throwable { |
| final long dataLoadingDelay = 1000; |
| Intent intent = new Intent(); |
| intent.putExtra(BrowseFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay); |
| intent.putExtra(BrowseFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , false); |
| mActivity = activityTestRule.launchActivity(intent); |
| |
| assertNull(mActivity.getBrowseTestFragment().getMainFragment()); |
| sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT); |
| } |
| |
| @Test |
| public void testSelectCardOnARow() throws Throwable { |
| final int selectRow = 10; |
| final int selectItem = 20; |
| Intent intent = new Intent(); |
| final long dataLoadingDelay = 1000; |
| intent.putExtra(BrowseFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay); |
| intent.putExtra(BrowseFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , true); |
| mActivity = activityTestRule.launchActivity(intent); |
| |
| Thread.sleep(dataLoadingDelay + TRANSITION_LENGTH); |
| |
| Presenter.ViewHolderTask itemTask = Mockito.spy( |
| new ItemSelectionTask(mActivity, selectRow)); |
| |
| final ListRowPresenter.SelectItemViewHolderTask task = |
| new ListRowPresenter.SelectItemViewHolderTask(selectItem); |
| task.setItemTask(itemTask); |
| |
| mActivity.runOnUiThread(new Runnable() { |
| @Override |
| public void run() { |
| mActivity.getBrowseTestFragment().setSelectedPosition(selectRow, true, task); |
| } |
| }); |
| |
| verify(itemTask, timeout(5000).times(1)).run(any(Presenter.ViewHolder.class)); |
| |
| ListRowPresenter.ViewHolder row = (ListRowPresenter.ViewHolder) mActivity |
| .getBrowseTestFragment().getRowsFragment().getRowViewHolder(selectRow); |
| assertEquals(selectItem, row.getGridView().getSelectedPosition()); |
| } |
| |
| private void sendKeys(int ...keys) { |
| for (int i = 0; i < keys.length; i++) { |
| InstrumentationRegistry.getInstrumentation().sendKeyDownUpSync(keys[i]); |
| } |
| } |
| |
| public static class ItemSelectionTask extends Presenter.ViewHolderTask { |
| |
| private final BrowseFragmentTestActivity activity; |
| private final int expectedRow; |
| |
| ItemSelectionTask(BrowseFragmentTestActivity activity, int expectedRow) { |
| this.activity = activity; |
| this.expectedRow = expectedRow; |
| } |
| |
| public void run(Presenter.ViewHolder holder) { |
| assertEquals(expectedRow, activity.getBrowseTestFragment().getSelectedPosition()); |
| } |
| } |
| } |