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.os.Build;
30import android.support.test.InstrumentationRegistry;
31import android.support.test.filters.MediumTest;
32import android.support.test.rule.ActivityTestRule;
33import android.support.test.runner.AndroidJUnit4;
34import android.support.v17.leanback.testutils.PollingCheck;
35import android.support.v17.leanback.widget.ItemBridgeAdapter;
36import android.support.v17.leanback.widget.ListRowPresenter;
37import android.support.v17.leanback.widget.Presenter;
38import android.support.v7.widget.RecyclerView;
39import android.view.KeyEvent;
40import android.view.View;
41
42import org.junit.After;
43import org.junit.Rule;
44import org.junit.Test;
45import org.junit.runner.RunWith;
46import org.mockito.Mockito;
47
48@MediumTest
49@RunWith(AndroidJUnit4.class)
50public class BrowseSupportFragmentTest {
51
52    static final String TAG = "BrowseSupportFragmentTest";
53    static final long WAIT_TRANSIITON_TIMEOUT = 10000;
54
55    @Rule
56    public ActivityTestRule<BrowseSupportFragmentTestActivity> activityTestRule =
57            new ActivityTestRule<>(BrowseSupportFragmentTestActivity.class, false, false);
58    private BrowseSupportFragmentTestActivity mActivity;
59
60    @After
61    public void afterTest() throws Throwable {
62        activityTestRule.runOnUiThread(new Runnable() {
63            @Override
64            public void run() {
65                if (mActivity != null) {
66                    mActivity.finish();
67                    mActivity = null;
68                }
69            }
70        });
71    }
72
73    void waitForEntranceTransitionFinished() {
74        PollingCheck.waitFor(WAIT_TRANSIITON_TIMEOUT, new PollingCheck.PollingCheckCondition() {
75            @Override
76            public boolean canProceed() {
77                if (Build.VERSION.SDK_INT >= 21) {
78                    return mActivity.getBrowseTestSupportFragment() != null
79                            && mActivity.getBrowseTestSupportFragment().mEntranceTransitionEnded;
80                } else {
81                    // when entrance transition not supported, wait main fragment loaded.
82                    return mActivity.getBrowseTestSupportFragment() != null
83                            && mActivity.getBrowseTestSupportFragment().getMainFragment() != null;
84                }
85            }
86        });
87    }
88
89    void waitForHeaderTransitionFinished() {
90        View row = mActivity.getBrowseTestSupportFragment().getRowsSupportFragment().getRowViewHolder(
91                mActivity.getBrowseTestSupportFragment().getSelectedPosition()).view;
92        PollingCheck.waitFor(WAIT_TRANSIITON_TIMEOUT, new PollingCheck.ViewStableOnScreen(row));
93    }
94
95    @Test
96    public void testTwoBackKeysWithBackStack() throws Throwable {
97        final long dataLoadingDelay = 1000;
98        Intent intent = new Intent();
99        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay);
100        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , true);
101        mActivity = activityTestRule.launchActivity(intent);
102
103        waitForEntranceTransitionFinished();
104
105        assertNotNull(mActivity.getBrowseTestSupportFragment().getMainFragment());
106        sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
107        waitForHeaderTransitionFinished();
108        sendKeys(KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_BACK);
109    }
110
111    @Test
112    public void testTwoBackKeysWithoutBackStack() throws Throwable {
113        final long dataLoadingDelay = 1000;
114        Intent intent = new Intent();
115        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay);
116        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , false);
117        mActivity = activityTestRule.launchActivity(intent);
118
119        waitForEntranceTransitionFinished();
120
121        assertNotNull(mActivity.getBrowseTestSupportFragment().getMainFragment());
122        sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
123        waitForHeaderTransitionFinished();
124        sendKeys(KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_BACK);
125    }
126
127    @Test
128    public void testPressRightBeforeMainFragmentCreated() throws Throwable {
129        final long dataLoadingDelay = 1000;
130        Intent intent = new Intent();
131        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay);
132        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , false);
133        mActivity = activityTestRule.launchActivity(intent);
134
135        assertNull(mActivity.getBrowseTestSupportFragment().getMainFragment());
136        sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
137    }
138
139    @Test
140    public void testSelectCardOnARow() throws Throwable {
141        final int selectRow = 10;
142        final int selectItem = 20;
143        Intent intent = new Intent();
144        final long dataLoadingDelay = 1000;
145        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay);
146        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , true);
147        mActivity = activityTestRule.launchActivity(intent);
148
149        waitForEntranceTransitionFinished();
150
151        Presenter.ViewHolderTask itemTask = Mockito.spy(
152                new ItemSelectionTask(mActivity, selectRow));
153
154        final ListRowPresenter.SelectItemViewHolderTask task =
155                new ListRowPresenter.SelectItemViewHolderTask(selectItem);
156        task.setItemTask(itemTask);
157
158        mActivity.runOnUiThread(new Runnable() {
159            @Override
160            public void run() {
161                mActivity.getBrowseTestSupportFragment().setSelectedPosition(selectRow, true, task);
162            }
163        });
164
165        verify(itemTask, timeout(5000).times(1)).run(any(Presenter.ViewHolder.class));
166
167        activityTestRule.runOnUiThread(new Runnable() {
168            @Override
169            public void run() {
170                ListRowPresenter.ViewHolder row = (ListRowPresenter.ViewHolder) mActivity
171                        .getBrowseTestSupportFragment().getRowsSupportFragment().getRowViewHolder(selectRow);
172                assertNotNull(dumpRecyclerView(mActivity.getBrowseTestSupportFragment().getGridView()), row);
173                assertNotNull(row.getGridView());
174                assertEquals(selectItem, row.getGridView().getSelectedPosition());
175            }
176        });
177    }
178
179    @Test
180    public void activityRecreate_notCrash() throws Throwable {
181        final long dataLoadingDelay = 1000;
182        Intent intent = new Intent();
183        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay);
184        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_ADD_TO_BACKSTACK , false);
185        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_SET_ADAPTER_AFTER_DATA_LOAD, true);
186        mActivity = activityTestRule.launchActivity(intent);
187
188        waitForEntranceTransitionFinished();
189
190        InstrumentationRegistry.getInstrumentation().callActivityOnRestart(mActivity);
191        activityTestRule.runOnUiThread(new Runnable() {
192            @Override
193            public void run() {
194                mActivity.recreate();
195            }
196        });
197    }
198
199
200    @Test
201    public void lateLoadingHeaderDisabled() throws Throwable {
202        final long dataLoadingDelay = 1000;
203        Intent intent = new Intent();
204        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_LOAD_DATA_DELAY, dataLoadingDelay);
205        intent.putExtra(BrowseSupportFragmentTestActivity.EXTRA_HEADERS_STATE,
206                BrowseSupportFragment.HEADERS_DISABLED);
207        mActivity = activityTestRule.launchActivity(intent);
208        waitForEntranceTransitionFinished();
209        PollingCheck.waitFor(new PollingCheck.PollingCheckCondition() {
210            @Override
211            public boolean canProceed() {
212                return mActivity.getBrowseTestSupportFragment().getGridView() != null
213                        && mActivity.getBrowseTestSupportFragment().getGridView().getChildCount() > 0;
214            }
215        });
216    }
217
218    private void sendKeys(int ...keys) {
219        for (int i = 0; i < keys.length; i++) {
220            InstrumentationRegistry.getInstrumentation().sendKeyDownUpSync(keys[i]);
221        }
222    }
223
224    public static class ItemSelectionTask extends Presenter.ViewHolderTask {
225
226        private final BrowseSupportFragmentTestActivity activity;
227        private final int expectedRow;
228
229        public ItemSelectionTask(BrowseSupportFragmentTestActivity activity, int expectedRow) {
230            this.activity = activity;
231            this.expectedRow = expectedRow;
232        }
233
234        @Override
235        public void run(Presenter.ViewHolder holder) {
236            android.util.Log.d(TAG, dumpRecyclerView(activity.getBrowseTestSupportFragment()
237                    .getGridView()));
238            android.util.Log.d(TAG, "Row " + expectedRow + " " + activity.getBrowseTestSupportFragment()
239                    .getRowsSupportFragment().getRowViewHolder(expectedRow), new Exception());
240        }
241    }
242
243    static String dumpRecyclerView(RecyclerView recyclerView) {
244        StringBuffer b = new StringBuffer();
245        for (int i = 0; i < recyclerView.getChildCount(); i++) {
246            View child = recyclerView.getChildAt(i);
247            ItemBridgeAdapter.ViewHolder vh = (ItemBridgeAdapter.ViewHolder)
248                    recyclerView.getChildViewHolder(child);
249            b.append("child").append(i).append(":").append(vh);
250            if (vh != null) {
251                b.append(",").append(vh.getViewHolder());
252            }
253            b.append(";");
254        }
255        return b.toString();
256    }
257}
258