blob: 5f362a3d776a21c2730a4cb03d9f2744303fdfaf [file] [log] [blame]
/* This file is auto-generated from BrowseFrgamentTest.java. DO NOT MODIFY. */
/*
* 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.test.suitebuilder.annotation.MediumTest;
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.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.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
*/
@MediumTest
@RunWith(AndroidJUnit4.class)
public class BrowseSupportFragmentTest {
static final long TRANSITION_LENGTH = 1000;
static final long HORIZONTAL_SCROLL_WAIT = 2000;
@Rule
public ActivityTestRule<BrowseSupportFragmentTestActivity> activityTestRule
= new ActivityTestRule<>(BrowseSupportFragmentTestActivity.class, false, false);
private BrowseSupportFragmentTestActivity mActivity;
@Test
public void testTwoBackKeysWithBackStack() throws Throwable {
Intent intent = new Intent();
intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, (long) 1000);
intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , true);
activityTestRule.launchActivity(intent);
sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
Thread.sleep(TRANSITION_LENGTH);
sendKeys(KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_BACK);
}
@Test
public void testTwoBackKeysWithoutBackStack() throws Throwable {
Intent intent = new Intent();
intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, (long) 1000);
intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , false);
activityTestRule.launchActivity(intent);
sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
Thread.sleep(TRANSITION_LENGTH);
sendKeys(KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_BACK);
}
@Test
public void testSelectCardOnARow() throws Throwable {
final int selectRow = 10;
final int selectItem = 20;
Intent intent = new Intent();
final long dataLoadingDelay = 1000;
intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay);
intent.putExtra(BrowseSupportFragmentTestActivity.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.getBrowseTestSupportFragment().setSelectedPosition(selectRow, true, task);
}
});
verify(itemTask, timeout(5000).times(1)).run(any(Presenter.ViewHolder.class));
ListRowPresenter.ViewHolder row = (ListRowPresenter.ViewHolder) mActivity
.getBrowseTestSupportFragment().getRowsSupportFragment().getRowViewHolder(selectRow);
assertEquals(selectItem, row.getGridView().getSelectedPosition());
}
private void sendKeys(int ...keys) {
for (int i = 0; i < keys.length; i++) {
ViewActions.pressKey(keys[i]);
}
}
public static class ItemSelectionTask extends Presenter.ViewHolderTask {
private final BrowseSupportFragmentTestActivity activity;
private final int expectedRow;
ItemSelectionTask(BrowseSupportFragmentTestActivity activity, int expectedRow) {
this.activity = activity;
this.expectedRow = expectedRow;
}
public void run(Presenter.ViewHolder holder) {
assertEquals(expectedRow, activity.getBrowseTestSupportFragment().getSelectedPosition());
}
}
}