DetailsSupportFragmentTest.java revision 3bcad88cbf4488e747d84893c35f2351b8f84afe
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 org.junit.Assert.assertEquals;
22import static org.junit.Assert.assertFalse;
23import static org.junit.Assert.assertNull;
24import static org.junit.Assert.assertTrue;
25
26import android.animation.PropertyValuesHolder;
27import android.support.v4.app.Fragment;
28import android.content.Intent;
29import android.graphics.Bitmap;
30import android.graphics.BitmapFactory;
31import android.graphics.Rect;
32import android.graphics.drawable.ColorDrawable;
33import android.graphics.drawable.Drawable;
34import android.net.Uri;
35import android.os.Build;
36import android.os.Bundle;
37import android.os.SystemClock;
38import android.support.test.InstrumentationRegistry;
39import android.support.test.filters.LargeTest;
40import android.support.v17.leanback.R;
41import android.support.v17.leanback.graphics.FitWidthBitmapDrawable;
42import android.support.v17.leanback.media.MediaPlayerGlue;
43import android.support.v17.leanback.media.PlaybackGlueHost;
44import android.support.v17.leanback.testutils.PollingCheck;
45import android.support.v17.leanback.transition.TransitionHelper;
46import android.support.v17.leanback.util.StateMachine;
47import android.support.v17.leanback.widget.DetailsParallax;
48import android.support.v17.leanback.widget.DetailsParallaxDrawable;
49import android.support.v17.leanback.widget.ParallaxTarget;
50import android.support.v17.leanback.widget.RecyclerViewParallax;
51import android.support.v17.leanback.widget.VerticalGridView;
52import android.view.KeyEvent;
53import android.view.View;
54
55import org.junit.Test;
56import org.junit.runner.RunWith;
57import org.junit.runners.JUnit4;
58
59/**
60 * Unit tests for {@link DetailsSupportFragment}.
61 */
62@RunWith(JUnit4.class)
63@LargeTest
64public class DetailsSupportFragmentTest extends SingleSupportFragmentTestBase {
65
66    static final int PARALLAX_VERTICAL_OFFSET = -300;
67
68    static int getCoverDrawableAlpha(DetailsSupportFragmentBackgroundController controller) {
69        return ((FitWidthBitmapDrawable) controller.mParallaxDrawable.getCoverDrawable())
70                .getAlpha();
71    }
72
73    public static class DetailsSupportFragmentParallax extends DetailsTestSupportFragment {
74
75        private DetailsParallaxDrawable mParallaxDrawable;
76
77        public DetailsSupportFragmentParallax() {
78            super();
79            mMinVerticalOffset = PARALLAX_VERTICAL_OFFSET;
80        }
81
82        @Override
83        public void onCreate(Bundle savedInstanceState) {
84            super.onCreate(savedInstanceState);
85            Drawable coverDrawable = new FitWidthBitmapDrawable();
86            mParallaxDrawable = new DetailsParallaxDrawable(
87                    getActivity(),
88                    getParallax(),
89                    coverDrawable,
90                    new ParallaxTarget.PropertyValuesHolderTarget(
91                            coverDrawable,
92                            PropertyValuesHolder.ofInt("verticalOffset", 0, mMinVerticalOffset)
93                    )
94            );
95
96            BackgroundManager backgroundManager = BackgroundManager.getInstance(getActivity());
97            backgroundManager.attach(getActivity().getWindow());
98            backgroundManager.setDrawable(mParallaxDrawable);
99        }
100
101        @Override
102        public void onStart() {
103            super.onStart();
104            setItem(new PhotoItem("Hello world", "Fake content goes here",
105                    android.support.v17.leanback.test.R.drawable.spiderman));
106        }
107
108        @Override
109        public void onResume() {
110            super.onResume();
111            Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(),
112                    android.support.v17.leanback.test.R.drawable.spiderman);
113            ((FitWidthBitmapDrawable) mParallaxDrawable.getCoverDrawable()).setBitmap(bitmap);
114        }
115
116        DetailsParallaxDrawable getParallaxDrawable() {
117            return mParallaxDrawable;
118        }
119    }
120
121    @Test
122    public void parallaxSetupTest() {
123        SingleSupportFragmentTestActivity activity =
124                launchAndWaitActivity(DetailsSupportFragmentTest.DetailsSupportFragmentParallax.class,
125                new SingleSupportFragmentTestBase.Options().uiVisibility(
126                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
127
128        double delta = 0.0002;
129        DetailsParallax dpm = ((DetailsSupportFragment) activity.getTestFragment()).getParallax();
130
131        RecyclerViewParallax.ChildPositionProperty frameTop =
132                (RecyclerViewParallax.ChildPositionProperty) dpm.getOverviewRowTop();
133        assertEquals(0f, frameTop.getFraction(), delta);
134        assertEquals(0f, frameTop.getAdapterPosition(), delta);
135
136
137        RecyclerViewParallax.ChildPositionProperty frameBottom =
138                (RecyclerViewParallax.ChildPositionProperty) dpm.getOverviewRowBottom();
139        assertEquals(1f, frameBottom.getFraction(), delta);
140        assertEquals(0f, frameBottom.getAdapterPosition(), delta);
141    }
142
143    @Test
144    public void parallaxTest() throws Throwable {
145        SingleSupportFragmentTestActivity activity = launchAndWaitActivity(DetailsSupportFragmentParallax.class,
146                new Options().uiVisibility(
147                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
148
149        final DetailsSupportFragmentParallax detailsFragment =
150                (DetailsSupportFragmentParallax) activity.getTestFragment();
151        DetailsParallaxDrawable drawable =
152                detailsFragment.getParallaxDrawable();
153        final FitWidthBitmapDrawable bitmapDrawable = (FitWidthBitmapDrawable)
154                drawable.getCoverDrawable();
155
156        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
157            @Override
158            public boolean canProceed() {
159                return detailsFragment.getRowsSupportFragment().getAdapter() != null
160                        && detailsFragment.getRowsSupportFragment().getAdapter().size() > 1;
161            }
162        });
163
164        final VerticalGridView verticalGridView = detailsFragment.getRowsSupportFragment()
165                .getVerticalGridView();
166        final int windowHeight = verticalGridView.getHeight();
167        final int windowWidth = verticalGridView.getWidth();
168        // make sure background manager attached to window is same size as VerticalGridView
169        // i.e. no status bar.
170        assertEquals(windowHeight, activity.getWindow().getDecorView().getHeight());
171        assertEquals(windowWidth, activity.getWindow().getDecorView().getWidth());
172
173        final View detailsFrame = verticalGridView.findViewById(R.id.details_frame);
174
175        assertEquals(windowWidth, bitmapDrawable.getBounds().width());
176
177        final Rect detailsFrameRect = new Rect();
178        detailsFrameRect.set(0, 0, detailsFrame.getWidth(), detailsFrame.getHeight());
179        verticalGridView.offsetDescendantRectToMyCoords(detailsFrame, detailsFrameRect);
180
181        assertEquals(Math.min(windowHeight, detailsFrameRect.top),
182                bitmapDrawable.getBounds().height());
183        assertEquals(0, bitmapDrawable.getVerticalOffset());
184
185        assertTrue("TitleView is visible", detailsFragment.getView()
186                .findViewById(R.id.browse_title_group).getVisibility() == View.VISIBLE);
187
188        activityTestRule.runOnUiThread(new Runnable() {
189            @Override
190            public void run() {
191                verticalGridView.scrollToPosition(1);
192            }
193        });
194
195        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
196            @Override
197            public boolean canProceed() {
198                return bitmapDrawable.getVerticalOffset() == PARALLAX_VERTICAL_OFFSET
199                        && detailsFragment.getView()
200                        .findViewById(R.id.browse_title_group).getVisibility() != View.VISIBLE;
201            }
202        });
203
204        detailsFrameRect.set(0, 0, detailsFrame.getWidth(), detailsFrame.getHeight());
205        verticalGridView.offsetDescendantRectToMyCoords(detailsFrame, detailsFrameRect);
206
207        assertEquals(0, bitmapDrawable.getBounds().top);
208        assertEquals(Math.max(detailsFrameRect.top, 0), bitmapDrawable.getBounds().bottom);
209        assertEquals(windowWidth, bitmapDrawable.getBounds().width());
210
211        ColorDrawable colorDrawable = (ColorDrawable) (drawable.getChildAt(1).getDrawable());
212        assertEquals(windowWidth, colorDrawable.getBounds().width());
213        assertEquals(detailsFrameRect.bottom, colorDrawable.getBounds().top);
214        assertEquals(windowHeight, colorDrawable.getBounds().bottom);
215    }
216
217    public static class DetailsSupportFragmentWithVideo extends DetailsTestSupportFragment {
218
219        final DetailsSupportFragmentBackgroundController mDetailsBackground =
220                new DetailsSupportFragmentBackgroundController(this);
221        MediaPlayerGlue mGlue;
222
223        public DetailsSupportFragmentWithVideo() {
224            mTimeToLoadOverviewRow = mTimeToLoadRelatedRow = 100;
225        }
226
227        @Override
228        public void onCreate(Bundle savedInstanceState) {
229            super.onCreate(savedInstanceState);
230            mDetailsBackground.enableParallax();
231            mGlue = new MediaPlayerGlue(getActivity());
232            mDetailsBackground.setupVideoPlayback(mGlue);
233
234            mGlue.setMode(MediaPlayerGlue.REPEAT_ALL);
235            mGlue.setArtist("A Googleer");
236            mGlue.setTitle("Diving with Sharks");
237            mGlue.setMediaSource(
238                    Uri.parse("android.resource://android.support.v17.leanback.test/raw/video"));
239        }
240
241        @Override
242        public void onStart() {
243            super.onStart();
244            Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(),
245                    android.support.v17.leanback.test.R.drawable.spiderman);
246            mDetailsBackground.setCoverBitmap(bitmap);
247        }
248
249        @Override
250        public void onStop() {
251            mDetailsBackground.setCoverBitmap(null);
252            super.onStop();
253        }
254    }
255
256    public static class DetailsSupportFragmentWithVideo1 extends DetailsSupportFragmentWithVideo {
257
258        @Override
259        public void onCreate(Bundle savedInstanceState) {
260            super.onCreate(savedInstanceState);
261            setItem(new PhotoItem("Hello world", "Fake content goes here",
262                    android.support.v17.leanback.test.R.drawable.spiderman));
263        }
264    }
265
266    public static class DetailsSupportFragmentWithVideo2 extends DetailsSupportFragmentWithVideo {
267
268        @Override
269        public void onStart() {
270            super.onStart();
271            setItem(new PhotoItem("Hello world", "Fake content goes here",
272                    android.support.v17.leanback.test.R.drawable.spiderman));
273        }
274    }
275
276    private void navigateBetweenRowsAndVideoUsingRequestFocusInternal(Class cls)
277            throws Throwable {
278        SingleSupportFragmentTestActivity activity = launchAndWaitActivity(cls,
279                new Options().uiVisibility(
280                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
281
282        final DetailsSupportFragmentWithVideo detailsFragment =
283                (DetailsSupportFragmentWithVideo) activity.getTestFragment();
284        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
285            @Override
286            public boolean canProceed() {
287                return detailsFragment.mVideoSupportFragment != null
288                        && detailsFragment.mVideoSupportFragment.getView() != null
289                        && detailsFragment.mGlue.isMediaPlaying();
290            }
291        });
292
293        final int screenHeight = detailsFragment.getRowsSupportFragment().getVerticalGridView()
294                .getHeight();
295        final View firstRow = detailsFragment.getRowsSupportFragment().getVerticalGridView().getChildAt(0);
296        final int originalFirstRowTop = firstRow.getTop();
297        assertTrue(firstRow.hasFocus());
298        assertTrue(firstRow.getTop() > 0 && firstRow.getTop() < screenHeight);
299        assertTrue(detailsFragment.isShowingTitle());
300
301        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
302            @Override
303            public void run() {
304                detailsFragment.mVideoSupportFragment.getView().requestFocus();
305            }
306        });
307        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
308            @Override
309            public boolean canProceed() {
310                return firstRow.getTop() >= screenHeight;
311            }
312        });
313        assertFalse(detailsFragment.isShowingTitle());
314
315        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
316            @Override
317            public void run() {
318                detailsFragment.getRowsSupportFragment().getVerticalGridView().requestFocus();
319            }
320        });
321        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
322            @Override
323            public boolean canProceed() {
324                return firstRow.getTop() == originalFirstRowTop;
325            }
326        });
327        assertTrue(detailsFragment.isShowingTitle());
328    }
329
330    @Test
331    public void navigateBetweenRowsAndVideoUsingRequestFocus1() throws Throwable {
332        navigateBetweenRowsAndVideoUsingRequestFocusInternal(DetailsSupportFragmentWithVideo1.class);
333    }
334
335    @Test
336    public void navigateBetweenRowsAndVideoUsingRequestFocus2() throws Throwable {
337        navigateBetweenRowsAndVideoUsingRequestFocusInternal(DetailsSupportFragmentWithVideo2.class);
338    }
339
340    private void navigateBetweenRowsAndVideoUsingDPADInternal(Class cls) throws Throwable {
341        SingleSupportFragmentTestActivity activity = launchAndWaitActivity(cls,
342                new Options().uiVisibility(
343                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
344
345        final DetailsSupportFragmentWithVideo detailsFragment =
346                (DetailsSupportFragmentWithVideo) activity.getTestFragment();
347        // wait video playing
348        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
349            @Override
350            public boolean canProceed() {
351                return detailsFragment.mVideoSupportFragment != null
352                        && detailsFragment.mVideoSupportFragment.getView() != null
353                        && detailsFragment.mGlue.isMediaPlaying();
354            }
355        });
356
357        final int screenHeight = detailsFragment.getRowsSupportFragment().getVerticalGridView()
358                .getHeight();
359        final View firstRow = detailsFragment.getRowsSupportFragment().getVerticalGridView().getChildAt(0);
360        final int originalFirstRowTop = firstRow.getTop();
361        assertTrue(firstRow.hasFocus());
362        assertTrue(firstRow.getTop() > 0 && firstRow.getTop() < screenHeight);
363        assertTrue(detailsFragment.isShowingTitle());
364
365        // navigate to video
366        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
367        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
368            @Override
369            public boolean canProceed() {
370                return firstRow.getTop() >= screenHeight;
371            }
372        });
373
374        // wait auto hide play controls done:
375        PollingCheck.waitFor(8000, new PollingCheck.PollingCheckCondition() {
376            @Override
377            public boolean canProceed() {
378                return ((PlaybackSupportFragment) detailsFragment.mVideoSupportFragment).mBgAlpha == 0;
379            }
380        });
381
382        // navigate to details
383        sendKeys(KeyEvent.KEYCODE_BACK);
384        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
385            @Override
386            public boolean canProceed() {
387                return firstRow.getTop() == originalFirstRowTop;
388            }
389        });
390        assertTrue(detailsFragment.isShowingTitle());
391    }
392
393    @Test
394    public void navigateBetweenRowsAndVideoUsingDPAD1() throws Throwable {
395        navigateBetweenRowsAndVideoUsingDPADInternal(DetailsSupportFragmentWithVideo1.class);
396    }
397
398    @Test
399    public void navigateBetweenRowsAndVideoUsingDPAD2() throws Throwable {
400        navigateBetweenRowsAndVideoUsingDPADInternal(DetailsSupportFragmentWithVideo2.class);
401    }
402
403    public static class EmptyFragmentClass extends Fragment {
404        @Override
405        public void onStart() {
406            super.onStart();
407            getActivity().finish();
408        }
409    }
410
411    private void fragmentOnStartWithVideoInternal(Class cls) throws Throwable {
412        final SingleSupportFragmentTestActivity activity = launchAndWaitActivity(cls,
413                new Options().uiVisibility(
414                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
415
416        final DetailsSupportFragmentWithVideo detailsFragment =
417                (DetailsSupportFragmentWithVideo) activity.getTestFragment();
418        // wait video playing
419        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
420            @Override
421            public boolean canProceed() {
422                return detailsFragment.mVideoSupportFragment != null
423                        && detailsFragment.mVideoSupportFragment.getView() != null
424                        && detailsFragment.mGlue.isMediaPlaying();
425            }
426        });
427
428        final int screenHeight = detailsFragment.getRowsSupportFragment().getVerticalGridView()
429                .getHeight();
430        final View firstRow = detailsFragment.getRowsSupportFragment().getVerticalGridView().getChildAt(0);
431        final int originalFirstRowTop = firstRow.getTop();
432        assertTrue(firstRow.hasFocus());
433        assertTrue(firstRow.getTop() > 0 && firstRow.getTop() < screenHeight);
434        assertTrue(detailsFragment.isShowingTitle());
435
436        // navigate to video
437        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
438        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
439            @Override
440            public boolean canProceed() {
441                return firstRow.getTop() >= screenHeight;
442            }
443        });
444
445        // start an empty activity
446        InstrumentationRegistry.getInstrumentation().runOnMainSync(
447                new Runnable() {
448                    @Override
449                    public void run() {
450                        Intent intent = new Intent(activity, SingleSupportFragmentTestActivity.class);
451                        intent.putExtra(SingleSupportFragmentTestActivity.EXTRA_FRAGMENT_NAME,
452                                EmptyFragmentClass.class.getName());
453                        activity.startActivity(intent);
454                    }
455                }
456        );
457        PollingCheck.waitFor(2000, new PollingCheck.PollingCheckCondition() {
458            @Override
459            public boolean canProceed() {
460                return detailsFragment.isResumed();
461            }
462        });
463        assertTrue(detailsFragment.mVideoSupportFragment.getView().hasFocus());
464    }
465
466    @Test
467    public void fragmentOnStartWithVideo1() throws Throwable {
468        fragmentOnStartWithVideoInternal(DetailsSupportFragmentWithVideo1.class);
469    }
470
471    @Test
472    public void fragmentOnStartWithVideo2() throws Throwable {
473        fragmentOnStartWithVideoInternal(DetailsSupportFragmentWithVideo2.class);
474    }
475
476    @Test
477    public void navigateBetweenRowsAndTitle() throws Throwable {
478        SingleSupportFragmentTestActivity activity =
479                launchAndWaitActivity(DetailsTestSupportFragment.class, new Options().uiVisibility(
480                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
481        final DetailsTestSupportFragment detailsFragment =
482                (DetailsTestSupportFragment) activity.getTestFragment();
483
484        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
485            @Override
486            public void run() {
487                detailsFragment.setOnSearchClickedListener(new View.OnClickListener() {
488                    @Override
489                    public void onClick(View view) {
490                    }
491                });
492                detailsFragment.setItem(new PhotoItem("Hello world", "Fake content goes here",
493                        android.support.v17.leanback.test.R.drawable.spiderman));
494            }
495        });
496
497        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
498            @Override
499            public boolean canProceed() {
500                return detailsFragment.getRowsSupportFragment().getVerticalGridView().getChildCount() > 0;
501            }
502        });
503        final View firstRow = detailsFragment.getRowsSupportFragment().getVerticalGridView().getChildAt(0);
504        final int originalFirstRowTop = firstRow.getTop();
505        final int screenHeight = detailsFragment.getRowsSupportFragment().getVerticalGridView()
506                .getHeight();
507
508        assertTrue(firstRow.hasFocus());
509        assertTrue(detailsFragment.isShowingTitle());
510        assertTrue(firstRow.getTop() > 0 && firstRow.getTop() < screenHeight);
511
512        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
513        PollingCheck.waitFor(new PollingCheck.ViewStableOnScreen(firstRow));
514        assertTrue(detailsFragment.isShowingTitle());
515        assertTrue(detailsFragment.getTitleView().hasFocus());
516        assertEquals(originalFirstRowTop, firstRow.getTop());
517
518        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
519        PollingCheck.waitFor(new PollingCheck.ViewStableOnScreen(firstRow));
520        assertTrue(detailsFragment.isShowingTitle());
521        assertTrue(firstRow.hasFocus());
522        assertEquals(originalFirstRowTop, firstRow.getTop());
523    }
524
525    public static class DetailsSupportFragmentWithNoVideo extends DetailsTestSupportFragment {
526
527        final DetailsSupportFragmentBackgroundController mDetailsBackground =
528                new DetailsSupportFragmentBackgroundController(this);
529
530        public DetailsSupportFragmentWithNoVideo() {
531            mTimeToLoadOverviewRow = mTimeToLoadRelatedRow = 100;
532        }
533
534        @Override
535        public void onCreate(Bundle savedInstanceState) {
536            super.onCreate(savedInstanceState);
537            mDetailsBackground.enableParallax();
538
539            setItem(new PhotoItem("Hello world", "Fake content goes here",
540                    android.support.v17.leanback.test.R.drawable.spiderman));
541        }
542
543        @Override
544        public void onStart() {
545            super.onStart();
546            Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(),
547                    android.support.v17.leanback.test.R.drawable.spiderman);
548            mDetailsBackground.setCoverBitmap(bitmap);
549        }
550
551        @Override
552        public void onStop() {
553            mDetailsBackground.setCoverBitmap(null);
554            super.onStop();
555        }
556    }
557
558    @Test
559    public void lateSetupVideo() {
560        final SingleSupportFragmentTestActivity activity =
561                launchAndWaitActivity(DetailsSupportFragmentWithNoVideo.class, new Options().uiVisibility(
562                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
563        final DetailsSupportFragmentWithNoVideo detailsFragment =
564                (DetailsSupportFragmentWithNoVideo) activity.getTestFragment();
565
566        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
567            @Override
568            public void run() {
569                detailsFragment.setItem(new PhotoItem("Hello world", "Fake content goes here",
570                        android.support.v17.leanback.test.R.drawable.spiderman));
571            }
572        });
573
574        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
575            @Override
576            public boolean canProceed() {
577                return detailsFragment.getRowsSupportFragment().getVerticalGridView().getChildCount() > 0;
578            }
579        });
580        final View firstRow = detailsFragment.getRowsSupportFragment().getVerticalGridView().getChildAt(0);
581        final int screenHeight = detailsFragment.getRowsSupportFragment().getVerticalGridView()
582                .getHeight();
583
584        assertTrue(firstRow.hasFocus());
585        assertTrue(detailsFragment.isShowingTitle());
586        assertTrue(firstRow.getTop() > 0 && firstRow.getTop() < screenHeight);
587
588        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
589        assertTrue(firstRow.hasFocus());
590
591        SystemClock.sleep(1000);
592        InstrumentationRegistry.getInstrumentation().runOnMainSync(
593                new Runnable() {
594                    @Override
595                    public void run() {
596                        final MediaPlayerGlue glue = new MediaPlayerGlue(activity);
597                        detailsFragment.mDetailsBackgroundController.setupVideoPlayback(glue);
598                        glue.setMode(MediaPlayerGlue.REPEAT_ALL);
599                        glue.setArtist("A Googleer");
600                        glue.setTitle("Diving with Sharks");
601                        glue.setMediaSource(Uri.parse(
602                                "android.resource://android.support.v17.leanback.test/raw/video"));
603                    }
604                }
605        );
606
607        // after setup Video Playback the DPAD up will navigate to Video Fragment.
608        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
609                @Override
610                    public boolean canProceed() {
611                        return detailsFragment.mVideoSupportFragment != null
612                                && detailsFragment.mVideoSupportFragment.getView() != null;
613                }
614        });
615        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
616        assertTrue(detailsFragment.mVideoSupportFragment.getView().hasFocus());
617        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
618            @Override
619            public boolean canProceed() {
620                return ((MediaPlayerGlue) detailsFragment.mDetailsBackgroundController
621                        .getPlaybackGlue()).isMediaPlaying();
622            }
623        });
624        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
625            @Override
626            public boolean canProceed() {
627                return 0 == getCoverDrawableAlpha(detailsFragment.mDetailsBackgroundController);
628            }
629        });
630
631        // wait a little bit to replace with new Glue
632        SystemClock.sleep(1000);
633        InstrumentationRegistry.getInstrumentation().runOnMainSync(
634                new Runnable() {
635                    @Override
636                    public void run() {
637                        final MediaPlayerGlue glue2 = new MediaPlayerGlue(activity);
638                        detailsFragment.mDetailsBackgroundController.setupVideoPlayback(glue2);
639                        glue2.setMode(MediaPlayerGlue.REPEAT_ALL);
640                        glue2.setArtist("A Googleer");
641                        glue2.setTitle("Diving with Sharks");
642                        glue2.setMediaSource(Uri.parse(
643                                "android.resource://android.support.v17.leanback.test/raw/video"));
644                    }
645                }
646        );
647
648        // test switchToRows() and switchToVideo()
649        InstrumentationRegistry.getInstrumentation().runOnMainSync(
650                new Runnable() {
651                    @Override
652                    public void run() {
653                        detailsFragment.mDetailsBackgroundController.switchToRows();
654                    }
655                }
656        );
657        assertTrue(detailsFragment.mRowsSupportFragment.getView().hasFocus());
658        PollingCheck.waitFor(new PollingCheck.ViewStableOnScreen(firstRow));
659        InstrumentationRegistry.getInstrumentation().runOnMainSync(
660                new Runnable() {
661                    @Override
662                    public void run() {
663                        detailsFragment.mDetailsBackgroundController.switchToVideo();
664                    }
665                }
666        );
667        assertTrue(detailsFragment.mVideoSupportFragment.getView().hasFocus());
668        PollingCheck.waitFor(new PollingCheck.ViewStableOnScreen(firstRow));
669    }
670
671    @Test
672    public void sharedGlueHost() {
673        final SingleSupportFragmentTestActivity activity =
674                launchAndWaitActivity(DetailsSupportFragmentWithNoVideo.class, new Options().uiVisibility(
675                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
676        final DetailsSupportFragmentWithNoVideo detailsFragment =
677                (DetailsSupportFragmentWithNoVideo) activity.getTestFragment();
678
679        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
680            @Override
681            public void run() {
682                detailsFragment.setItem(new PhotoItem("Hello world", "Fake content goes here",
683                        android.support.v17.leanback.test.R.drawable.spiderman));
684            }
685        });
686
687        SystemClock.sleep(1000);
688        InstrumentationRegistry.getInstrumentation().runOnMainSync(
689                new Runnable() {
690                    @Override
691                    public void run() {
692                        final MediaPlayerGlue glue1 = new MediaPlayerGlue(activity);
693                        detailsFragment.mDetailsBackgroundController.setupVideoPlayback(glue1);
694                        glue1.setArtist("A Googleer");
695                        glue1.setTitle("Diving with Sharks");
696                        glue1.setMediaSource(Uri.parse(
697                                "android.resource://android.support.v17.leanback.test/raw/video"));
698                    }
699                }
700        );
701
702        // after setup Video Playback the DPAD up will navigate to Video Fragment.
703        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
704            @Override
705            public boolean canProceed() {
706                return detailsFragment.mVideoSupportFragment != null
707                        && detailsFragment.mVideoSupportFragment.getView() != null;
708            }
709        });
710
711        final MediaPlayerGlue glue1 = (MediaPlayerGlue) detailsFragment
712                .mDetailsBackgroundController
713                .getPlaybackGlue();
714        PlaybackGlueHost playbackGlueHost = glue1.getHost();
715
716        // wait a little bit to replace with new Glue
717        SystemClock.sleep(1000);
718        InstrumentationRegistry.getInstrumentation().runOnMainSync(
719                new Runnable() {
720                    @Override
721                    public void run() {
722                        final MediaPlayerGlue glue2 = new MediaPlayerGlue(activity);
723                        detailsFragment.mDetailsBackgroundController.setupVideoPlayback(glue2);
724                        glue2.setArtist("A Googleer");
725                        glue2.setTitle("Diving with Sharks");
726                        glue2.setMediaSource(Uri.parse(
727                                "android.resource://android.support.v17.leanback.test/raw/video"));
728                    }
729                }
730        );
731
732        // wait for new glue to get its glue host
733        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
734            @Override
735            public boolean canProceed() {
736                MediaPlayerGlue mediaPlayerGlue = (MediaPlayerGlue) detailsFragment
737                        .mDetailsBackgroundController
738                        .getPlaybackGlue();
739                return mediaPlayerGlue != null && mediaPlayerGlue != glue1
740                        && mediaPlayerGlue.getHost() != null;
741            }
742        });
743
744        final MediaPlayerGlue glue2 = (MediaPlayerGlue) detailsFragment
745                .mDetailsBackgroundController
746                .getPlaybackGlue();
747
748        assertTrue(glue1.getHost() == null);
749        assertTrue(glue2.getHost() == playbackGlueHost);
750    }
751
752    @Test
753    public void clearVideo() {
754        final SingleSupportFragmentTestActivity activity =
755                launchAndWaitActivity(DetailsSupportFragmentWithNoVideo.class, new Options().uiVisibility(
756                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
757        final DetailsSupportFragmentWithNoVideo detailsFragment =
758                (DetailsSupportFragmentWithNoVideo) activity.getTestFragment();
759
760        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
761            @Override
762            public void run() {
763                detailsFragment.setItem(new PhotoItem("Hello world", "Fake content goes here",
764                        android.support.v17.leanback.test.R.drawable.spiderman));
765            }
766        });
767
768        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
769            @Override
770            public boolean canProceed() {
771                return detailsFragment.getRowsSupportFragment().getVerticalGridView().getChildCount() > 0;
772            }
773        });
774        final View firstRow = detailsFragment.getRowsSupportFragment().getVerticalGridView().getChildAt(0);
775        final int screenHeight = detailsFragment.getRowsSupportFragment().getVerticalGridView()
776                .getHeight();
777
778        assertTrue(firstRow.hasFocus());
779        assertTrue(detailsFragment.isShowingTitle());
780        assertTrue(firstRow.getTop() > 0 && firstRow.getTop() < screenHeight);
781
782        SystemClock.sleep(1000);
783        InstrumentationRegistry.getInstrumentation().runOnMainSync(
784                new Runnable() {
785                    @Override
786                    public void run() {
787                        final MediaPlayerGlue glue = new MediaPlayerGlue(activity);
788                        detailsFragment.mDetailsBackgroundController.setupVideoPlayback(glue);
789                        glue.setMode(MediaPlayerGlue.REPEAT_ALL);
790                        glue.setArtist("A Googleer");
791                        glue.setTitle("Diving with Sharks");
792                        glue.setMediaSource(Uri.parse(
793                                "android.resource://android.support.v17.leanback.test/raw/video"));
794                    }
795                }
796        );
797
798        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
799            @Override
800            public boolean canProceed() {
801                return ((MediaPlayerGlue) detailsFragment.mDetailsBackgroundController
802                        .getPlaybackGlue()).isMediaPlaying();
803            }
804        });
805        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
806            @Override
807            public boolean canProceed() {
808                return 0 == getCoverDrawableAlpha(detailsFragment.mDetailsBackgroundController);
809            }
810        });
811
812        // wait a little bit then reset glue
813        SystemClock.sleep(1000);
814        InstrumentationRegistry.getInstrumentation().runOnMainSync(
815                new Runnable() {
816                    @Override
817                    public void run() {
818                        detailsFragment.mDetailsBackgroundController.setupVideoPlayback(null);
819                    }
820                }
821        );
822        // background should fade in upon reset playback
823        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
824            @Override
825            public boolean canProceed() {
826                return 255 == getCoverDrawableAlpha(detailsFragment.mDetailsBackgroundController);
827            }
828        });
829    }
830
831    public static class DetailsSupportFragmentWithNoItem extends DetailsTestSupportFragment {
832
833        final DetailsSupportFragmentBackgroundController mDetailsBackground =
834                new DetailsSupportFragmentBackgroundController(this);
835
836        public DetailsSupportFragmentWithNoItem() {
837            mTimeToLoadOverviewRow = mTimeToLoadRelatedRow = 100;
838        }
839
840        @Override
841        public void onCreate(Bundle savedInstanceState) {
842            super.onCreate(savedInstanceState);
843            mDetailsBackground.enableParallax();
844        }
845
846        @Override
847        public void onStart() {
848            super.onStart();
849            Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(),
850                    android.support.v17.leanback.test.R.drawable.spiderman);
851            mDetailsBackground.setCoverBitmap(bitmap);
852        }
853
854        @Override
855        public void onStop() {
856            mDetailsBackground.setCoverBitmap(null);
857            super.onStop();
858        }
859    }
860
861    @Test
862    public void noInitialItem() {
863        SingleSupportFragmentTestActivity activity =
864                launchAndWaitActivity(DetailsSupportFragmentWithNoItem.class, new Options().uiVisibility(
865                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
866        final DetailsSupportFragmentWithNoItem detailsFragment =
867                (DetailsSupportFragmentWithNoItem) activity.getTestFragment();
868
869        final int recyclerViewHeight = detailsFragment.getRowsSupportFragment().getVerticalGridView()
870                .getHeight();
871        assertTrue(recyclerViewHeight > 0);
872
873        assertEquals(255, getCoverDrawableAlpha(detailsFragment.mDetailsBackgroundController));
874        Drawable coverDrawable = detailsFragment.mDetailsBackgroundController.getCoverDrawable();
875        assertEquals(0, coverDrawable.getBounds().top);
876        assertEquals(recyclerViewHeight, coverDrawable.getBounds().bottom);
877        Drawable bottomDrawable = detailsFragment.mDetailsBackgroundController.getBottomDrawable();
878        assertEquals(recyclerViewHeight, bottomDrawable.getBounds().top);
879        assertEquals(recyclerViewHeight, bottomDrawable.getBounds().bottom);
880    }
881
882    public static class DetailsSupportFragmentSwitchToVideoInOnCreate extends DetailsTestSupportFragment {
883
884        final DetailsSupportFragmentBackgroundController mDetailsBackground =
885                new DetailsSupportFragmentBackgroundController(this);
886
887        public DetailsSupportFragmentSwitchToVideoInOnCreate() {
888            mTimeToLoadOverviewRow = mTimeToLoadRelatedRow = 100;
889        }
890
891        @Override
892        public void onCreate(Bundle savedInstanceState) {
893            super.onCreate(savedInstanceState);
894            mDetailsBackground.enableParallax();
895            mDetailsBackground.switchToVideo();
896        }
897
898        @Override
899        public void onStart() {
900            super.onStart();
901            Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(),
902                    android.support.v17.leanback.test.R.drawable.spiderman);
903            mDetailsBackground.setCoverBitmap(bitmap);
904        }
905
906        @Override
907        public void onStop() {
908            mDetailsBackground.setCoverBitmap(null);
909            super.onStop();
910        }
911    }
912
913    @Test
914    public void switchToVideoInOnCreate() {
915        final SingleSupportFragmentTestActivity activity =
916                launchAndWaitActivity(DetailsSupportFragmentSwitchToVideoInOnCreate.class,
917                new Options().uiVisibility(
918                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
919        final DetailsSupportFragmentSwitchToVideoInOnCreate detailsFragment =
920                (DetailsSupportFragmentSwitchToVideoInOnCreate) activity.getTestFragment();
921
922        // the pending enter transition flag should be automatically cleared
923        assertEquals(StateMachine.STATUS_INVOKED,
924                detailsFragment.STATE_ENTER_TRANSITION_COMPLETE.getStatus());
925        assertNull(TransitionHelper.getEnterTransition(activity.getWindow()));
926        assertEquals(0, getCoverDrawableAlpha(detailsFragment.mDetailsBackgroundController));
927        assertTrue(detailsFragment.getRowsSupportFragment().getView().hasFocus());
928        //SystemClock.sleep(5000);
929        assertFalse(detailsFragment.isShowingTitle());
930
931        SystemClock.sleep(1000);
932        assertNull(detailsFragment.mVideoSupportFragment);
933        InstrumentationRegistry.getInstrumentation().runOnMainSync(
934                new Runnable() {
935                    @Override
936                    public void run() {
937                        final MediaPlayerGlue glue = new MediaPlayerGlue(activity);
938                        detailsFragment.mDetailsBackgroundController.setupVideoPlayback(glue);
939                        glue.setMode(MediaPlayerGlue.REPEAT_ALL);
940                        glue.setArtist("A Googleer");
941                        glue.setTitle("Diving with Sharks");
942                        glue.setMediaSource(Uri.parse(
943                                "android.resource://android.support.v17.leanback.test/raw/video"));
944                    }
945                }
946        );
947        // once the video fragment is created it would be immediately assigned focus
948        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
949            @Override
950            public boolean canProceed() {
951                return detailsFragment.mVideoSupportFragment != null
952                        && detailsFragment.mVideoSupportFragment.getView() != null
953                        && detailsFragment.mVideoSupportFragment.getView().hasFocus();
954            }
955        });
956        // wait auto hide play controls done:
957        PollingCheck.waitFor(8000, new PollingCheck.PollingCheckCondition() {
958            @Override
959            public boolean canProceed() {
960                return ((PlaybackSupportFragment) detailsFragment.mVideoSupportFragment).mBgAlpha == 0;
961            }
962        });
963
964        // switchToRows does nothing if there is no row
965        InstrumentationRegistry.getInstrumentation().runOnMainSync(
966                new Runnable() {
967                    @Override
968                    public void run() {
969                        detailsFragment.mDetailsBackgroundController.switchToRows();
970                    }
971                }
972        );
973        assertTrue(detailsFragment.mVideoSupportFragment.getView().hasFocus());
974
975        // create item, it should be layout outside screen
976        InstrumentationRegistry.getInstrumentation().runOnMainSync(
977                new Runnable() {
978                    @Override
979                    public void run() {
980                        detailsFragment.setItem(new PhotoItem("Hello world",
981                                "Fake content goes here",
982                                android.support.v17.leanback.test.R.drawable.spiderman));
983                    }
984                }
985        );
986        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
987            @Override
988            public boolean canProceed() {
989                return detailsFragment.getVerticalGridView().getChildCount() > 0
990                        && detailsFragment.getVerticalGridView().getChildAt(0).getTop()
991                        >= detailsFragment.getVerticalGridView().getHeight();
992            }
993        });
994
995        // pressing BACK will return to details row
996        sendKeys(KeyEvent.KEYCODE_BACK);
997        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
998            @Override
999            public boolean canProceed() {
1000                return detailsFragment.getVerticalGridView().getChildAt(0).getTop()
1001                        < (detailsFragment.getVerticalGridView().getHeight() * 0.7f);
1002            }
1003        });
1004        assertTrue(detailsFragment.getVerticalGridView().getChildAt(0).hasFocus());
1005    }
1006
1007    @Test
1008    public void switchToVideoBackToQuit() {
1009        final SingleSupportFragmentTestActivity activity =
1010                launchAndWaitActivity(DetailsSupportFragmentSwitchToVideoInOnCreate.class,
1011                new Options().uiVisibility(
1012                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
1013        final DetailsSupportFragmentSwitchToVideoInOnCreate detailsFragment =
1014                (DetailsSupportFragmentSwitchToVideoInOnCreate) activity.getTestFragment();
1015
1016        // the pending enter transition flag should be automatically cleared
1017        assertEquals(StateMachine.STATUS_INVOKED,
1018                detailsFragment.STATE_ENTER_TRANSITION_COMPLETE.getStatus());
1019        assertNull(TransitionHelper.getEnterTransition(activity.getWindow()));
1020        assertEquals(0, getCoverDrawableAlpha(detailsFragment.mDetailsBackgroundController));
1021        assertTrue(detailsFragment.getRowsSupportFragment().getView().hasFocus());
1022        assertFalse(detailsFragment.isShowingTitle());
1023
1024        SystemClock.sleep(1000);
1025        assertNull(detailsFragment.mVideoSupportFragment);
1026        InstrumentationRegistry.getInstrumentation().runOnMainSync(
1027                new Runnable() {
1028                    @Override
1029                    public void run() {
1030                        final MediaPlayerGlue glue = new MediaPlayerGlue(activity);
1031                        detailsFragment.mDetailsBackgroundController.setupVideoPlayback(glue);
1032                        glue.setMode(MediaPlayerGlue.REPEAT_ALL);
1033                        glue.setArtist("A Googleer");
1034                        glue.setTitle("Diving with Sharks");
1035                        glue.setMediaSource(Uri.parse(
1036                                "android.resource://android.support.v17.leanback.test/raw/video"));
1037                    }
1038                }
1039        );
1040        // once the video fragment is created it would be immediately assigned focus
1041        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
1042            @Override
1043            public boolean canProceed() {
1044                return detailsFragment.mVideoSupportFragment != null
1045                        && detailsFragment.mVideoSupportFragment.getView() != null
1046                        && detailsFragment.mVideoSupportFragment.getView().hasFocus();
1047            }
1048        });
1049        // wait auto hide play controls done:
1050        PollingCheck.waitFor(8000, new PollingCheck.PollingCheckCondition() {
1051            @Override
1052            public boolean canProceed() {
1053                return ((PlaybackSupportFragment) detailsFragment.mVideoSupportFragment).mBgAlpha == 0;
1054            }
1055        });
1056
1057        // before any details row is presented, pressing BACK will quit the activity
1058        sendKeys(KeyEvent.KEYCODE_BACK);
1059        PollingCheck.waitFor(4000, new PollingCheck.ActivityDestroy(activity));
1060    }
1061
1062    public static class DetailsSupportFragmentSwitchToVideoAndPrepareEntranceTransition
1063            extends DetailsTestSupportFragment {
1064
1065        final DetailsSupportFragmentBackgroundController mDetailsBackground =
1066                new DetailsSupportFragmentBackgroundController(this);
1067
1068        public DetailsSupportFragmentSwitchToVideoAndPrepareEntranceTransition() {
1069            mTimeToLoadOverviewRow = mTimeToLoadRelatedRow = 100;
1070        }
1071
1072        @Override
1073        public void onCreate(Bundle savedInstanceState) {
1074            super.onCreate(savedInstanceState);
1075            mDetailsBackground.enableParallax();
1076            mDetailsBackground.switchToVideo();
1077            prepareEntranceTransition();
1078        }
1079
1080        @Override
1081        public void onViewCreated(View view, Bundle savedInstanceState) {
1082            super.onViewCreated(view, savedInstanceState);
1083        }
1084
1085        @Override
1086        public void onStart() {
1087            super.onStart();
1088            Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(),
1089                    android.support.v17.leanback.test.R.drawable.spiderman);
1090            mDetailsBackground.setCoverBitmap(bitmap);
1091        }
1092
1093        @Override
1094        public void onStop() {
1095            mDetailsBackground.setCoverBitmap(null);
1096            super.onStop();
1097        }
1098    }
1099
1100    @Test
1101    public void switchToVideoInOnCreateAndPrepareEntranceTransition() {
1102        SingleSupportFragmentTestActivity activity = launchAndWaitActivity(
1103                DetailsSupportFragmentSwitchToVideoAndPrepareEntranceTransition.class,
1104                new Options().uiVisibility(
1105                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
1106        final DetailsSupportFragmentSwitchToVideoAndPrepareEntranceTransition detailsFragment =
1107                (DetailsSupportFragmentSwitchToVideoAndPrepareEntranceTransition)
1108                        activity.getTestFragment();
1109
1110        assertEquals(StateMachine.STATUS_INVOKED,
1111                detailsFragment.STATE_ENTRANCE_COMPLETE.getStatus());
1112    }
1113
1114    public static class DetailsSupportFragmentEntranceTransition
1115            extends DetailsTestSupportFragment {
1116
1117        final DetailsSupportFragmentBackgroundController mDetailsBackground =
1118                new DetailsSupportFragmentBackgroundController(this);
1119
1120        public DetailsSupportFragmentEntranceTransition() {
1121            mTimeToLoadOverviewRow = mTimeToLoadRelatedRow = 100;
1122        }
1123
1124        @Override
1125        public void onCreate(Bundle savedInstanceState) {
1126            super.onCreate(savedInstanceState);
1127            mDetailsBackground.enableParallax();
1128            prepareEntranceTransition();
1129        }
1130
1131        @Override
1132        public void onStart() {
1133            super.onStart();
1134            Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(),
1135                    android.support.v17.leanback.test.R.drawable.spiderman);
1136            mDetailsBackground.setCoverBitmap(bitmap);
1137        }
1138
1139        @Override
1140        public void onStop() {
1141            mDetailsBackground.setCoverBitmap(null);
1142            super.onStop();
1143        }
1144    }
1145
1146    @Test
1147    public void entranceTransitionBlocksSwitchToVideo() {
1148        SingleSupportFragmentTestActivity activity =
1149                launchAndWaitActivity(DetailsSupportFragmentEntranceTransition.class,
1150                new Options().uiVisibility(
1151                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN), 0);
1152        final DetailsSupportFragmentEntranceTransition detailsFragment =
1153                (DetailsSupportFragmentEntranceTransition)
1154                        activity.getTestFragment();
1155
1156        if (Build.VERSION.SDK_INT < 21) {
1157            // when enter transition is not supported, mCanUseHost is immmediately true
1158            assertTrue(detailsFragment.mDetailsBackgroundController.mCanUseHost);
1159        } else {
1160            // calling switchToVideo() between prepareEntranceTransition and entrance transition
1161            // finishes will be ignored.
1162            InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
1163                @Override
1164                public void run() {
1165                    detailsFragment.mDetailsBackgroundController.switchToVideo();
1166                }
1167            });
1168            assertFalse(detailsFragment.mDetailsBackgroundController.mCanUseHost);
1169        }
1170        assertEquals(255, getCoverDrawableAlpha(detailsFragment.mDetailsBackgroundController));
1171        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
1172            @Override
1173            public void run() {
1174                detailsFragment.setItem(new PhotoItem("Hello world", "Fake content goes here",
1175                        android.support.v17.leanback.test.R.drawable.spiderman));
1176                detailsFragment.startEntranceTransition();
1177            }
1178        });
1179        // once Entrance transition is finished, mCanUseHost will be true
1180        // and we can switchToVideo and fade out the background.
1181        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
1182            @Override
1183            public boolean canProceed() {
1184                return detailsFragment.mDetailsBackgroundController.mCanUseHost;
1185            }
1186        });
1187        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
1188            @Override
1189            public void run() {
1190                detailsFragment.mDetailsBackgroundController.switchToVideo();
1191            }
1192        });
1193        PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() {
1194            @Override
1195            public boolean canProceed() {
1196                return 0 == getCoverDrawableAlpha(detailsFragment.mDetailsBackgroundController);
1197            }
1198        });
1199    }
1200
1201}
1202