DetailsSupportFragmentTest.java revision a87809f9d13a3d39bf2202303b912019e8107c1b
1// CHECKSTYLE:OFF Generated code
2/* This file is auto-generated from DetailsFragmentTest.java.  DO NOT MODIFY. */
3
4/*
5 * Copyright (C) 2016 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 junit.framework.TestCase.assertFalse;
22
23import static org.junit.Assert.assertEquals;
24import static org.junit.Assert.assertTrue;
25
26import android.animation.PropertyValuesHolder;
27import android.graphics.Bitmap;
28import android.graphics.BitmapFactory;
29import android.graphics.Rect;
30import android.graphics.drawable.ColorDrawable;
31import android.graphics.drawable.Drawable;
32import android.net.Uri;
33import android.os.Bundle;
34import android.support.test.InstrumentationRegistry;
35import android.support.test.filters.MediumTest;
36import android.support.v17.leanback.R;
37import android.support.v17.leanback.graphics.FitWidthBitmapDrawable;
38import android.support.v17.leanback.media.MediaPlayerGlue;
39import android.support.v17.leanback.testutils.PollingCheck;
40import android.support.v17.leanback.widget.DetailsParallax;
41import android.support.v17.leanback.widget.DetailsParallaxDrawable;
42import android.support.v17.leanback.widget.ParallaxTarget;
43import android.support.v17.leanback.widget.RecyclerViewParallax;
44import android.support.v17.leanback.widget.VerticalGridView;
45import android.view.KeyEvent;
46import android.view.View;
47
48import org.junit.Test;
49import org.junit.runner.RunWith;
50import org.junit.runners.JUnit4;
51
52/**
53 * Unit tests for {@link DetailsSupportFragment}.
54 */
55@RunWith(JUnit4.class)
56@MediumTest
57public class DetailsSupportFragmentTest extends SingleSupportFragmentTestBase {
58
59    static final int PARALLAX_VERTICAL_OFFSET = -300;
60
61    public static class DetailsSupportFragmentParallax extends DetailsTestSupportFragment {
62
63        private DetailsParallaxDrawable mParallaxDrawable;
64
65        public DetailsSupportFragmentParallax() {
66            super();
67            mMinVerticalOffset = PARALLAX_VERTICAL_OFFSET;
68        }
69
70        @Override
71        public void onCreate(Bundle savedInstanceState) {
72            super.onCreate(savedInstanceState);
73            Drawable coverDrawable = new FitWidthBitmapDrawable();
74            mParallaxDrawable = new DetailsParallaxDrawable(
75                    getActivity(),
76                    getParallax(),
77                    coverDrawable,
78                    new ParallaxTarget.PropertyValuesHolderTarget(
79                            coverDrawable,
80                            PropertyValuesHolder.ofInt("verticalOffset", 0, mMinVerticalOffset)
81                    )
82            );
83
84            BackgroundManager backgroundManager = BackgroundManager.getInstance(getActivity());
85            backgroundManager.attach(getActivity().getWindow());
86            backgroundManager.setDrawable(mParallaxDrawable);
87        }
88
89        @Override
90        public void onStart() {
91            super.onStart();
92            setItem(new PhotoItem("Hello world", "Fake content goes here",
93                    android.support.v17.leanback.test.R.drawable.spiderman));
94        }
95
96        @Override
97        public void onResume() {
98            super.onResume();
99            Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(),
100                    android.support.v17.leanback.test.R.drawable.spiderman);
101            ((FitWidthBitmapDrawable) mParallaxDrawable.getCoverDrawable()).setBitmap(bitmap);
102        }
103
104        DetailsParallaxDrawable getParallaxDrawable() {
105            return mParallaxDrawable;
106        }
107    }
108
109    @Test
110    public void parallaxSetupTest() {
111        launchAndWaitActivity(DetailsSupportFragmentTest.DetailsSupportFragmentParallax.class,
112                new SingleSupportFragmentTestBase.Options().uiVisibility(
113                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
114
115        double delta = 0.0002;
116        DetailsParallax dpm = ((DetailsSupportFragment) mActivity.getTestFragment()).getParallax();
117
118        RecyclerViewParallax.ChildPositionProperty frameTop =
119                (RecyclerViewParallax.ChildPositionProperty) dpm.getOverviewRowTop();
120        assertEquals(0f, frameTop.getFraction(), delta);
121        assertEquals(0f, frameTop.getAdapterPosition(), delta);
122
123
124        RecyclerViewParallax.ChildPositionProperty frameBottom =
125                (RecyclerViewParallax.ChildPositionProperty) dpm.getOverviewRowBottom();
126        assertEquals(1f, frameBottom.getFraction(), delta);
127        assertEquals(0f, frameBottom.getAdapterPosition(), delta);
128    }
129
130    @Test
131    public void parallaxTest() throws Throwable {
132        launchAndWaitActivity(DetailsSupportFragmentParallax.class,
133                new Options().uiVisibility(
134                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
135
136        final DetailsSupportFragmentParallax detailsFragment =
137                (DetailsSupportFragmentParallax) mActivity.getTestFragment();
138        DetailsParallaxDrawable drawable =
139                detailsFragment.getParallaxDrawable();
140        final FitWidthBitmapDrawable bitmapDrawable = (FitWidthBitmapDrawable)
141                drawable.getCoverDrawable();
142
143        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
144            @Override
145            public boolean canProceed() {
146                return detailsFragment.getRowsSupportFragment().getAdapter() != null
147                        && detailsFragment.getRowsSupportFragment().getAdapter().size() > 1;
148            }
149        });
150
151        final VerticalGridView verticalGridView = detailsFragment.getRowsSupportFragment()
152                .getVerticalGridView();
153        final int windowHeight = verticalGridView.getHeight();
154        final int windowWidth = verticalGridView.getWidth();
155        // make sure background manager attached to window is same size as VerticalGridView
156        // i.e. no status bar.
157        assertEquals(windowHeight, mActivity.getWindow().getDecorView().getHeight());
158        assertEquals(windowWidth, mActivity.getWindow().getDecorView().getWidth());
159
160        final View detailsFrame = verticalGridView.findViewById(R.id.details_frame);
161
162        assertEquals(windowWidth, bitmapDrawable.getBounds().width());
163
164        final Rect detailsFrameRect = new Rect();
165        detailsFrameRect.set(0, 0, detailsFrame.getWidth(), detailsFrame.getHeight());
166        verticalGridView.offsetDescendantRectToMyCoords(detailsFrame, detailsFrameRect);
167
168        assertEquals(Math.min(windowHeight, detailsFrameRect.top),
169                bitmapDrawable.getBounds().height());
170        assertEquals(0, bitmapDrawable.getVerticalOffset());
171
172        assertTrue("TitleView is visible", detailsFragment.getView()
173                .findViewById(R.id.browse_title_group).getVisibility() == View.VISIBLE);
174
175        activityTestRule.runOnUiThread(new Runnable() {
176            @Override
177            public void run() {
178                verticalGridView.scrollToPosition(1);
179            }
180        });
181
182        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
183            @Override
184            public boolean canProceed() {
185                return bitmapDrawable.getVerticalOffset() == PARALLAX_VERTICAL_OFFSET
186                        && detailsFragment.getView()
187                        .findViewById(R.id.browse_title_group).getVisibility() != View.VISIBLE;
188            }
189        });
190
191        detailsFrameRect.set(0, 0, detailsFrame.getWidth(), detailsFrame.getHeight());
192        verticalGridView.offsetDescendantRectToMyCoords(detailsFrame, detailsFrameRect);
193
194        assertEquals(0, bitmapDrawable.getBounds().top);
195        assertEquals(Math.max(detailsFrameRect.top, 0), bitmapDrawable.getBounds().bottom);
196        assertEquals(windowWidth, bitmapDrawable.getBounds().width());
197
198        ColorDrawable colorDrawable = (ColorDrawable) (drawable.getChildAt(1).getDrawable());
199        assertEquals(windowWidth, colorDrawable.getBounds().width());
200        assertEquals(detailsFrameRect.bottom, colorDrawable.getBounds().top);
201        assertEquals(windowHeight, colorDrawable.getBounds().bottom);
202    }
203
204    public static class DetailsSupportFragmentWithVideo extends DetailsTestSupportFragment {
205
206        final DetailsSupportFragmentBackgroundController mDetailsBackground =
207                new DetailsSupportFragmentBackgroundController(this);
208        MediaPlayerGlue mGlue;
209
210        public DetailsSupportFragmentWithVideo() {
211            mTimeToLoadOverviewRow = mTimeToLoadRelatedRow = 100;
212        }
213
214        @Override
215        public void onCreate(Bundle savedInstanceState) {
216            super.onCreate(savedInstanceState);
217            mDetailsBackground.enableParallax();
218            mGlue = new MediaPlayerGlue(getActivity());
219            mDetailsBackground.setupVideoPlayback(mGlue);
220
221            mGlue.setMode(MediaPlayerGlue.REPEAT_ALL);
222            mGlue.setArtist("A Googleer");
223            mGlue.setTitle("Diving with Sharks");
224            mGlue.setMediaSource(
225                    Uri.parse("android.resource://android.support.v17.leanback.test/raw/video"));
226        }
227
228        @Override
229        public void onStart() {
230            super.onStart();
231            setItem(new PhotoItem("Hello world", "Fake content goes here",
232                    android.support.v17.leanback.test.R.drawable.spiderman));
233            Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(),
234                    android.support.v17.leanback.test.R.drawable.spiderman);
235            mDetailsBackground.setCoverBitmap(bitmap);
236        }
237
238        @Override
239        public void onStop() {
240            mDetailsBackground.setCoverBitmap(null);
241            super.onStop();
242        }
243    }
244
245    @Test
246    public void navigateBetweenRowsAndVideoUsingRequestFocus() throws Throwable {
247        launchAndWaitActivity(DetailsSupportFragmentWithVideo.class,
248                new Options().uiVisibility(
249                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
250
251        final DetailsSupportFragmentWithVideo detailsFragment =
252                (DetailsSupportFragmentWithVideo) mActivity.getTestFragment();
253        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
254            @Override
255            public boolean canProceed() {
256                return detailsFragment.mVideoSupportFragment != null
257                        && detailsFragment.mVideoSupportFragment.getView() != null
258                        && detailsFragment.mGlue.isMediaPlaying();
259            }
260        });
261
262        final int screenHeight = detailsFragment.getRowsSupportFragment().getVerticalGridView()
263                .getHeight();
264        final View firstRow = detailsFragment.getRowsSupportFragment().getVerticalGridView().getChildAt(0);
265        final int originalFirstRowTop = firstRow.getTop();
266        assertTrue(firstRow.hasFocus());
267        assertTrue(firstRow.getTop() < screenHeight);
268        assertTrue(detailsFragment.isShowingTitle());
269
270        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
271            @Override
272            public void run() {
273                detailsFragment.mVideoSupportFragment.getView().requestFocus();
274            }
275        });
276        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
277            @Override
278            public boolean canProceed() {
279                return firstRow.getTop() >= screenHeight;
280            }
281        });
282        assertFalse(detailsFragment.isShowingTitle());
283
284        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
285            @Override
286            public void run() {
287                detailsFragment.getRowsSupportFragment().getVerticalGridView().requestFocus();
288            }
289        });
290        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
291            @Override
292            public boolean canProceed() {
293                return firstRow.getTop() == originalFirstRowTop;
294            }
295        });
296        assertTrue(detailsFragment.isShowingTitle());
297    }
298
299    @Test
300    public void navigateBetweenRowsAndVideoUsingDPAD() throws Throwable {
301        launchAndWaitActivity(DetailsSupportFragmentWithVideo.class,
302                new Options().uiVisibility(
303                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
304
305        final DetailsSupportFragmentWithVideo detailsFragment =
306                (DetailsSupportFragmentWithVideo) mActivity.getTestFragment();
307        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
308            @Override
309            public boolean canProceed() {
310                return detailsFragment.mVideoSupportFragment != null
311                        && detailsFragment.mVideoSupportFragment.getView() != null
312                        && detailsFragment.mGlue.isMediaPlaying();
313            }
314        });
315
316        final int screenHeight = detailsFragment.getRowsSupportFragment().getVerticalGridView()
317                .getHeight();
318        final View firstRow = detailsFragment.getRowsSupportFragment().getVerticalGridView().getChildAt(0);
319        final int originalFirstRowTop = firstRow.getTop();
320        assertTrue(firstRow.hasFocus());
321        assertTrue(firstRow.getTop() < screenHeight);
322        assertTrue(detailsFragment.isShowingTitle());
323
324        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
325        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
326            @Override
327            public boolean canProceed() {
328                return firstRow.getTop() >= screenHeight;
329            }
330        });
331        assertFalse(detailsFragment.isShowingTitle());
332
333        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
334        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
335            @Override
336            public boolean canProceed() {
337                return firstRow.getTop() == originalFirstRowTop;
338            }
339        });
340        assertTrue(detailsFragment.isShowingTitle());
341    }
342
343    @Test
344    public void navigateBetweenRowsAndTitle() throws Throwable {
345        launchAndWaitActivity(DetailsTestSupportFragment.class, new Options().uiVisibility(
346                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
347        final DetailsTestSupportFragment detailsFragment =
348                (DetailsTestSupportFragment) mActivity.getTestFragment();
349
350        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
351            @Override
352            public void run() {
353                detailsFragment.setOnSearchClickedListener(new View.OnClickListener() {
354                    @Override
355                    public void onClick(View view) {
356                    }
357                });
358                detailsFragment.setItem(new PhotoItem("Hello world", "Fake content goes here",
359                        android.support.v17.leanback.test.R.drawable.spiderman));
360            }
361        });
362
363        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
364            @Override
365            public boolean canProceed() {
366                return detailsFragment.getRowsSupportFragment().getVerticalGridView().getChildCount() > 0;
367            }
368        });
369        final View firstRow = detailsFragment.getRowsSupportFragment().getVerticalGridView().getChildAt(0);
370        final int originalFirstRowTop = firstRow.getTop();
371        final int screenHeight = detailsFragment.getRowsSupportFragment().getVerticalGridView()
372                .getHeight();
373
374        assertTrue(firstRow.hasFocus());
375        assertTrue(detailsFragment.isShowingTitle());
376        assertTrue(firstRow.getTop() < screenHeight);
377
378        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
379        PollingCheck.waitFor(new PollingCheck.ViewStableOnScreen(firstRow));
380        assertTrue(detailsFragment.isShowingTitle());
381        assertTrue(detailsFragment.getTitleView().hasFocus());
382        assertEquals(originalFirstRowTop, firstRow.getTop());
383
384        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
385        PollingCheck.waitFor(new PollingCheck.ViewStableOnScreen(firstRow));
386        assertTrue(detailsFragment.isShowingTitle());
387        assertTrue(firstRow.hasFocus());
388        assertEquals(originalFirstRowTop, firstRow.getTop());
389    }
390
391}
392