BrowseSupportFragmentTest.java revision 3bd47bb01a2654f8399e89d227d40f99393b1844
1/* This file is auto-generated from BrowseFragmentTest.java.  DO NOT MODIFY. */
2
3/*
4 * Copyright (C) 2015 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *      http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package android.support.v17.leanback.app;
19
20import static org.junit.Assert.assertEquals;
21import static org.junit.Assert.assertNotNull;
22import static org.junit.Assert.assertNull;
23import static org.mockito.Matchers.any;
24import static org.mockito.Mockito.timeout;
25import static org.mockito.Mockito.verify;
26
27import android.content.Intent;
28import android.support.test.InstrumentationRegistry;
29import android.support.test.filters.MediumTest;
30import android.support.test.rule.ActivityTestRule;
31import android.support.test.runner.AndroidJUnit4;
32import android.support.v17.leanback.widget.ListRowPresenter;
33import android.support.v17.leanback.widget.Presenter;
34import android.view.KeyEvent;
35
36import org.junit.After;
37import org.junit.Rule;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40import org.mockito.Mockito;
41
42@MediumTest
43@RunWith(AndroidJUnit4.class)
44public class BrowseSupportFragmentTest {
45
46    static final long TRANSITION_LENGTH = 1000;
47    static final long HORIZONTAL_SCROLL_WAIT = 2000;
48
49    @Rule
50    public ActivityTestRule<BrowseSupportFragmentTestActivity> activityTestRule =
51            new ActivityTestRule<>(BrowseSupportFragmentTestActivity.class, false, false);
52    private BrowseSupportFragmentTestActivity mActivity;
53
54    @After
55    public void afterTest() throws Throwable {
56        activityTestRule.runOnUiThread(new Runnable() {
57            public void run() {
58                if (mActivity != null) {
59                    mActivity.finish();
60                    mActivity = null;
61                }
62            }
63        });
64    }
65
66    @Test
67    public void testTwoBackKeysWithBackStack() throws Throwable {
68        final long dataLoadingDelay = 1000;
69        Intent intent = new Intent();
70        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay);
71        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , true);
72        mActivity = activityTestRule.launchActivity(intent);
73
74        Thread.sleep(dataLoadingDelay + TRANSITION_LENGTH);
75
76        assertNotNull(mActivity.getBrowseTestSupportFragment().getMainFragment());
77        sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
78        Thread.sleep(TRANSITION_LENGTH);
79        sendKeys(KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_BACK);
80    }
81
82    @Test
83    public void testTwoBackKeysWithoutBackStack() throws Throwable {
84        final long dataLoadingDelay = 1000;
85        Intent intent = new Intent();
86        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay);
87        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , false);
88        mActivity = activityTestRule.launchActivity(intent);
89
90        Thread.sleep(dataLoadingDelay + TRANSITION_LENGTH);
91
92        assertNotNull(mActivity.getBrowseTestSupportFragment().getMainFragment());
93        sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
94        Thread.sleep(TRANSITION_LENGTH);
95        sendKeys(KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_BACK);
96    }
97
98    @Test
99    public void testPressRightBeforeMainFragmentCreated() throws Throwable {
100        final long dataLoadingDelay = 1000;
101        Intent intent = new Intent();
102        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay);
103        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , false);
104        mActivity = activityTestRule.launchActivity(intent);
105
106        assertNull(mActivity.getBrowseTestSupportFragment().getMainFragment());
107        sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
108    }
109
110    @Test
111    public void testSelectCardOnARow() throws Throwable {
112        final int selectRow = 10;
113        final int selectItem = 20;
114        Intent intent = new Intent();
115        final long dataLoadingDelay = 1000;
116        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay);
117        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , true);
118        mActivity = activityTestRule.launchActivity(intent);
119
120        Thread.sleep(dataLoadingDelay + TRANSITION_LENGTH);
121
122        Presenter.ViewHolderTask itemTask = Mockito.spy(
123                new ItemSelectionTask(mActivity, selectRow));
124
125        final ListRowPresenter.SelectItemViewHolderTask task =
126                new ListRowPresenter.SelectItemViewHolderTask(selectItem);
127        task.setItemTask(itemTask);
128
129        mActivity.runOnUiThread(new Runnable() {
130            @Override
131            public void run() {
132                mActivity.getBrowseTestSupportFragment().setSelectedPosition(selectRow, true, task);
133            }
134        });
135
136        verify(itemTask, timeout(5000).times(1)).run(any(Presenter.ViewHolder.class));
137
138        ListRowPresenter.ViewHolder row = (ListRowPresenter.ViewHolder) mActivity
139                .getBrowseTestSupportFragment().getRowsSupportFragment().getRowViewHolder(selectRow);
140        assertNotNull(row);
141        assertNotNull(row.getGridView());
142        assertEquals(selectItem, row.getGridView().getSelectedPosition());
143    }
144
145    @Test
146    public void activityRecreate_notCrash() throws Throwable {
147        final long dataLoadingDelay = 1000;
148        Intent intent = new Intent();
149        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay);
150        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , false);
151        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_SET_ADAPTER_AFTER_DATA_LOAD, true);
152        mActivity = activityTestRule.launchActivity(intent);
153
154        Thread.sleep(dataLoadingDelay + TRANSITION_LENGTH);
155
156        InstrumentationRegistry.getInstrumentation().callActivityOnRestart(mActivity);
157        activityTestRule.runOnUiThread(new Runnable() {
158            @Override
159            public void run() {
160                mActivity.recreate();
161            }
162        });
163    }
164
165    private void sendKeys(int ...keys) {
166        for (int i = 0; i < keys.length; i++) {
167            InstrumentationRegistry.getInstrumentation().sendKeyDownUpSync(keys[i]);
168        }
169    }
170
171    public static class ItemSelectionTask extends Presenter.ViewHolderTask {
172
173        private final BrowseSupportFragmentTestActivity activity;
174        private final int expectedRow;
175
176        public ItemSelectionTask(BrowseSupportFragmentTestActivity activity, int expectedRow) {
177            this.activity = activity;
178            this.expectedRow = expectedRow;
179        }
180
181        public void run(Presenter.ViewHolder holder) {
182            assertEquals(expectedRow, activity.getBrowseTestSupportFragment().getSelectedPosition());
183        }
184    }
185}
186