summaryrefslogtreecommitdiff
path: root/tests/PhotoPicker/src/android/photopicker/cts/PhotoPickerTest.java
blob: 3d0bcd3378f4dbebbfde0b9ee17329304e305246 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.photopicker.cts;

import static android.photopicker.cts.util.GetContentActivityAliasUtils.clearPackageData;
import static android.photopicker.cts.util.GetContentActivityAliasUtils.getDocumentsUiPackageName;
import static android.photopicker.cts.util.PhotoPickerAssertionsUtils.assertMimeType;
import static android.photopicker.cts.util.PhotoPickerAssertionsUtils.assertPersistedGrant;
import static android.photopicker.cts.util.PhotoPickerAssertionsUtils.assertPickerUriFormat;
import static android.photopicker.cts.util.PhotoPickerAssertionsUtils.assertRedactedReadOnlyAccess;
import static android.photopicker.cts.util.PhotoPickerFilesUtils.createDNGVideosAndGetUris;
import static android.photopicker.cts.util.PhotoPickerFilesUtils.createImagesAndGetUris;
import static android.photopicker.cts.util.PhotoPickerFilesUtils.createVideosAndGetUris;
import static android.photopicker.cts.util.PhotoPickerFilesUtils.deleteMedia;
import static android.photopicker.cts.util.PhotoPickerUiUtils.REGEX_PACKAGE_NAME;
import static android.photopicker.cts.util.PhotoPickerUiUtils.SHORT_TIMEOUT;
import static android.photopicker.cts.util.PhotoPickerUiUtils.clickAndWait;
import static android.photopicker.cts.util.PhotoPickerUiUtils.findAddButton;
import static android.photopicker.cts.util.PhotoPickerUiUtils.findItemList;
import static android.photopicker.cts.util.PhotoPickerUiUtils.findPreviewAddButton;
import static android.photopicker.cts.util.PhotoPickerUiUtils.findPreviewAddOrSelectButton;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import android.content.ClipData;
import android.content.Intent;
import android.media.AudioAttributes;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
import android.net.Uri;
import android.photopicker.cts.util.GetContentActivityAliasUtils;
import android.provider.MediaStore;

import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Photo Picker Device only tests for common flows.
 */
@RunWith(Parameterized.class)
public class PhotoPickerTest extends PhotoPickerBaseTest {

    @Parameter(0)
    public String mAction;

    @Parameters(name = "intent={0}")
    public static Iterable<? extends Object> data() {
        return getTestParameters();
    }

    private List<Uri> mUriList = new ArrayList<>();

    private static int sGetContentTakeOverActivityAliasState;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        sGetContentTakeOverActivityAliasState = GetContentActivityAliasUtils.enableAndGetOldState();
        clearPackageData(getDocumentsUiPackageName());
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        GetContentActivityAliasUtils.restoreState(sGetContentTakeOverActivityAliasState);
    }

    @After
    public void tearDown() throws Exception {
        for (Uri uri : mUriList) {
            deleteMedia(uri, mContext);
        }
        mUriList.clear();

        if (mActivity != null) {
            mActivity.finish();
        }
    }

    @Test
    public void testSingleSelect() throws Exception {
        final int itemCount = 1;
        mUriList.addAll(createImagesAndGetUris(itemCount, mContext.getUserId()));

        final Intent intent = new Intent(mAction);
        launchPhotoPickerForIntent(intent);

        final UiObject item = findItemList(itemCount).get(0);
        clickAndWait(mDevice, item);

        final Uri uri = mActivity.getResult().data.getData();
        assertPickerUriFormat(uri, mContext.getUserId());
        assertPersistedGrant(uri, mContext.getContentResolver());
        assertRedactedReadOnlyAccess(uri);
    }

    @Test
    public void testSingleSelectForFavoritesAlbum() throws Exception {
        final int itemCount = 1;
        mUriList.addAll(createImagesAndGetUris(itemCount, mContext.getUserId(),
                /* isFavorite */ true));

        final Intent intent = new Intent(mAction);
        launchPhotoPickerForIntent(intent);

        UiObject albumsTab = mDevice.findObject(new UiSelector().text(
                "Albums"));
        clickAndWait(mDevice, albumsTab);
        final UiObject album = findItemList(1).get(0);
        clickAndWait(mDevice, album);

        final UiObject item = findItemList(itemCount).get(0);
        clickAndWait(mDevice, item);

        final Uri uri = mActivity.getResult().data.getData();
        assertPickerUriFormat(uri, mContext.getUserId());
        assertRedactedReadOnlyAccess(uri);
    }

    @Test
    public void testLaunchPreviewMultipleForVideoAlbum() throws Exception {
        final int videoCount = 2;
        mUriList.addAll(createVideosAndGetUris(videoCount, mContext.getUserId()));

        Intent intent = new Intent(mAction);
        intent.setType("video/*");
        addMultipleSelectionFlag(intent);
        launchPhotoPickerForIntent(intent);

        UiObject albumsTab = mDevice.findObject(new UiSelector().text(
                "Albums"));
        clickAndWait(mDevice, albumsTab);
        final UiObject album = findItemList(1).get(0);
        clickAndWait(mDevice, album);

        final List<UiObject> itemList = findItemList(videoCount);
        final int itemCount = itemList.size();

        assertThat(itemCount).isEqualTo(videoCount);

        for (int i = 0; i < itemCount; i++) {
            clickAndWait(mDevice, itemList.get(i));
        }

        clickAndWait(mDevice, findViewSelectedButton());

        // Wait for playback to start. This is needed in some devices where playback
        // buffering -> ready state takes around 10s.
        final long playbackStartTimeout = 10000;
        (findPreviewVideoImageView()).waitUntilGone(playbackStartTimeout);
    }

    @Test
    public void testSingleSelectWithPreview() throws Exception {
        final int itemCount = 1;
        mUriList.addAll(createImagesAndGetUris(itemCount, mContext.getUserId()));

        final Intent intent = new Intent(mAction);
        launchPhotoPickerForIntent(intent);

        final UiObject item = findItemList(itemCount).get(0);
        item.longClick();
        mDevice.waitForIdle();

        final UiObject addButton = findPreviewAddOrSelectButton();
        assertThat(addButton.waitForExists(1000)).isTrue();
        clickAndWait(mDevice, addButton);

        final Uri uri = mActivity.getResult().data.getData();
        assertPickerUriFormat(uri, mContext.getUserId());
        assertRedactedReadOnlyAccess(uri);
    }

    @Test
    public void testMultiSelect() throws Exception {
        final int imageCount = 4;
        mUriList.addAll(createImagesAndGetUris(imageCount, mContext.getUserId()));
        Intent intent = new Intent(mAction);
        addMultipleSelectionFlag(intent);
        launchPhotoPickerForIntent(intent);

        final List<UiObject> itemList = findItemList(imageCount);
        final int itemCount = itemList.size();
        assertThat(itemCount).isEqualTo(imageCount);
        for (int i = 0; i < itemCount; i++) {
            clickAndWait(mDevice, itemList.get(i));
        }

        clickAndWait(mDevice, findAddButton());

        final ClipData clipData = mActivity.getResult().data.getClipData();
        final int count = clipData.getItemCount();
        assertThat(count).isEqualTo(itemCount);
        for (int i = 0; i < count; i++) {
            final Uri uri = clipData.getItemAt(i).getUri();
            assertPickerUriFormat(uri, mContext.getUserId());
            assertPersistedGrant(uri, mContext.getContentResolver());
            assertRedactedReadOnlyAccess(uri);
        }
    }

    @Test
    public void testMultiSelect_longPress() throws Exception {
        final int videoCount = 3;
        mUriList.addAll(createDNGVideosAndGetUris(videoCount, mContext.getUserId()));

        Intent intent = new Intent(mAction);
        intent.setType("video/*");
        addMultipleSelectionFlag(intent);
        launchPhotoPickerForIntent(intent);

        final List<UiObject> itemList = findItemList(videoCount);
        final int itemCount = itemList.size();
        assertThat(itemCount).isEqualTo(videoCount);

        // Select one item from Photo grid
        clickAndWait(mDevice, itemList.get(0));

        // Preview the item
        UiObject item = itemList.get(1);
        item.longClick();
        mDevice.waitForIdle();

        final UiObject addOrSelectButton = findPreviewAddOrSelectButton();
        assertWithMessage("Timed out waiting for AddOrSelectButton to appear")
                .that(addOrSelectButton.waitForExists(1000)).isTrue();

        // Select the item from Preview
        clickAndWait(mDevice, addOrSelectButton);

        mDevice.pressBack();

        // Select one more item from Photo grid
        clickAndWait(mDevice, itemList.get(2));

        clickAndWait(mDevice, findAddButton());

        // Verify that all 3 items are returned
        final ClipData clipData = mActivity.getResult().data.getClipData();
        final int count = clipData.getItemCount();
        assertThat(count).isEqualTo(3);
        for (int i = 0; i < count; i++) {
            final Uri uri = clipData.getItemAt(i).getUri();
            assertPickerUriFormat(uri, mContext.getUserId());
            assertPersistedGrant(uri, mContext.getContentResolver());
            assertRedactedReadOnlyAccess(uri);
        }
    }

    @Test
    public void testMultiSelect_preview() throws Exception {
        final int imageCount = 4;
        mUriList.addAll(createImagesAndGetUris(imageCount, mContext.getUserId()));

        Intent intent = new Intent(mAction);
        addMultipleSelectionFlag(intent);
        launchPhotoPickerForIntent(intent);

        final List<UiObject> itemList = findItemList(imageCount);
        final int itemCount = itemList.size();
        assertThat(itemCount).isEqualTo(imageCount);
        for (int i = 0; i < itemCount; i++) {
            clickAndWait(mDevice, itemList.get(i));
        }

        clickAndWait(mDevice, findViewSelectedButton());

        // Swipe left three times
        swipeLeftAndWait();
        swipeLeftAndWait();
        swipeLeftAndWait();

        // Deselect one item
        clickAndWait(mDevice, findPreviewSelectedCheckButton());

        // Return selected items
        clickAndWait(mDevice, findPreviewAddButton());

        final ClipData clipData = mActivity.getResult().data.getClipData();
        final int count = clipData.getItemCount();
        assertThat(count).isEqualTo(itemCount - 1);
        for (int i = 0; i < count; i++) {
            final Uri uri = clipData.getItemAt(i).getUri();
            assertPickerUriFormat(uri, mContext.getUserId());
            assertPersistedGrant(uri, mContext.getContentResolver());
            assertRedactedReadOnlyAccess(uri);
        }
    }

    @Test
    @Ignore("Re-enable once we find work around for b/226318844")
    public void testMultiSelect_previewVideoPlayPause() throws Exception {
        launchPreviewMultipleWithVideos(/* videoCount */ 3);

        // Check Play/Pause in first video
        testVideoPreviewPlayPause();

        // Move to third video
        swipeLeftAndWait();
        swipeLeftAndWait();
        // Check Play/Pause in third video
        testVideoPreviewPlayPause();

        // We don't test the result of the picker here because the intention of the test is only to
        // test the video controls
    }

    @Test
    public void testMultiSelect_previewVideoMuteButtonInitial() throws Exception {
        launchPreviewMultipleWithVideos(/* videoCount */ 1);

        final UiObject playPauseButton = findPlayPauseButton();
        final UiObject muteButton = findMuteButton();

        // check that player controls are visible
        assertPlayerControlsVisible(playPauseButton, muteButton);

        // Test 1: Initial state of the mute Button
        // Check that initial state of mute button is mute, i.e., volume off
        assertMuteButtonState(muteButton, /* isMuted */ true);

        // Test 2: Click Mute Button
        // Click to unmute the audio
        clickAndWait(mDevice, muteButton);

        waitForBinderCallsToComplete();

        // Check that mute button state is unmute, i.e., it shows `volume up` icon
        assertMuteButtonState(muteButton, /* isMuted */ false);
        // Click on the muteButton and check that mute button status is now 'mute'
        clickAndWait(mDevice, muteButton);

        waitForBinderCallsToComplete();

        assertMuteButtonState(muteButton, /* isMuted */ true);
        // Click on the muteButton and check that mute button status is now unmute
        clickAndWait(mDevice, muteButton);

        waitForBinderCallsToComplete();

        assertMuteButtonState(muteButton, /* isMuted */ false);

        // Test 3: Next preview resumes mute state
        // Go back and launch preview again
        mDevice.pressBack();
        clickAndWait(mDevice, findViewSelectedButton());

        waitForBinderCallsToComplete();

        // check that player controls are visible
        assertPlayerControlsVisible(playPauseButton, muteButton);
        assertMuteButtonState(muteButton, /* isMuted */ false);

        // We don't test the result of the picker here because the intention of the test is only to
        // test the video controls
    }

    @Test
    public void testMultiSelect_previewVideoMuteButtonOnSwipe() throws Exception {
        launchPreviewMultipleWithVideos(/* videoCount */ 3);

        final UiObject playPauseButton = findPlayPauseButton();
        final UiObject muteButton = findMuteButton();
        final UiObject playerView = findPlayerView();

        // check that player controls are visible
        assertPlayerControlsVisible(playPauseButton, muteButton);

        // Test 1: Swipe resumes mute state, with state of the button is 'volume off' / 'mute'
        assertMuteButtonState(muteButton, /* isMuted */ true);
        // Swipe to next page and check that muteButton is in mute state.
        swipeLeftAndWait();

        waitForBinderCallsToComplete();

        // set-up and wait for player controls to be sticky
        setUpAndAssertStickyPlayerControls(playerView, playPauseButton, muteButton);
        assertMuteButtonState(muteButton, /* isMuted */ true);

        // Test 2: Swipe resumes mute state, with state of mute button 'volume up' / 'unmute'
        // Click muteButton again to check the next video resumes the previous video's mute state
        clickAndWait(mDevice, muteButton);

        waitForBinderCallsToComplete();

        assertMuteButtonState(muteButton, /* isMuted */ false);
        // check that next video resumed previous video's mute state
        swipeLeftAndWait();

        waitForBinderCallsToComplete();

        // Wait for 1s before checking Play/Pause button's visibility
        playPauseButton.waitForExists(1000);
        // check that player controls are visible
        assertPlayerControlsVisible(playPauseButton, muteButton);
        assertMuteButtonState(muteButton, /* isMuted */ false);

        // We don't test the result of the picker here because the intention of the test is only to
        // test the video controls
    }

    @Test
    public void testVideoPreviewAudioFocus() throws Exception {
        final int[] focusStateForTest = new int[1];
        final AudioManager audioManager = mContext.getSystemService(AudioManager.class);
        AudioFocusRequest audioFocusRequest =
                new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
                .setAudioAttributes(new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.USAGE_MEDIA)
                        .setUsage(AudioAttributes.CONTENT_TYPE_MOVIE)
                        .build())
                .setWillPauseWhenDucked(true)
                .setAcceptsDelayedFocusGain(false)
                .setOnAudioFocusChangeListener(focusChange -> {
                    if (focusChange == AudioManager.AUDIOFOCUS_LOSS
                            || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
                            || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
                        focusStateForTest[0] = focusChange;
                    }
                })
                .build();

        // Request AudioFocus
        assertWithMessage("Expected requestAudioFocus result")
                .that(audioManager.requestAudioFocus(audioFocusRequest))
                .isEqualTo(AudioManager.AUDIOFOCUS_REQUEST_GRANTED);

        // Launch Preview
        launchPreviewMultipleWithVideos(/* videoCount */ 2);
        // Video preview launches in mute mode, hence, test's audio focus shouldn't be lost when
        // video preview starts
        assertThat(focusStateForTest[0]).isEqualTo(0);

        final UiObject muteButton = findMuteButton();
        // unmute the audio of video preview
        clickAndWait(mDevice, muteButton);

        // Remote video preview involves binder calls
        // Wait for Binder calls to complete and device to be idle
        MediaStore.waitForIdle(mContext.getContentResolver());
        mDevice.waitForIdle();

        assertMuteButtonState(muteButton, /* isMuted */ false);

        // Verify that test lost the audio focus because PhotoPicker has requested audio focus now.
        assertThat(focusStateForTest[0]).isEqualTo(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT);

        // Reset the focusStateForTest to verify test loses audio focus when video preview is
        // launched with unmute state
        focusStateForTest[0] = 0;
        // Abandon the audio focus before requesting again. This is necessary to reduce test flakes
        audioManager.abandonAudioFocusRequest(audioFocusRequest);
        // Request AudioFocus from test again
        assertWithMessage("Expected requestAudioFocus result")
                .that(audioManager.requestAudioFocus(audioFocusRequest))
                        .isEqualTo(AudioManager.AUDIOFOCUS_REQUEST_GRANTED);

        // Wait for PhotoPicker to lose Audio Focus
        findPlayButton().waitForExists(SHORT_TIMEOUT);
        // Test requesting audio focus will make PhotoPicker lose audio focus, Verify video is
        // paused when PhotoPicker loses audio focus.
        assertWithMessage("PlayPause button's content description")
                .that(findPlayPauseButton().getContentDescription())
                .isEqualTo("Play");

        // Swipe to next video and verify preview gains audio focus
        swipeLeftAndWait();
        findPauseButton().waitForExists(SHORT_TIMEOUT);
        // Video preview is now in unmute mode. Hence, PhotoPicker will request audio focus. Verify
        // that test lost the audio focus.
        assertThat(focusStateForTest[0]).isEqualTo(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT);
    }

    @Test
    @Ignore("Re-enable once we find work around for b/226318844")
    public void testMultiSelect_previewVideoControlsVisibility() throws Exception {
        launchPreviewMultipleWithVideos(/* videoCount */ 3);

        final UiObject playPauseButton = findPlayPauseButton();
        final UiObject muteButton = findMuteButton();
        // Check that buttons auto hide.
        assertPlayerControlsAutoHide(playPauseButton, muteButton);

        final UiObject playerView = findPlayerView();
        // Click on StyledPlayerView to make the video controls visible
        clickAndWait(mDevice, playerView);
        assertPlayerControlsVisible(playPauseButton, muteButton);

        // Wait for 1s and check that controls are still visible
        assertPlayerControlsDontAutoHide(playPauseButton, muteButton);

        // Click on StyledPlayerView and check that controls are no longer visible. Don't click in
        // the center, clicking in the center may pause the video.
        playerView.clickBottomRight();
        mDevice.waitForIdle();
        assertPlayerControlsHidden(playPauseButton, muteButton);

        // Swipe left and check that controls are not visible
        swipeLeftAndWait();
        assertPlayerControlsHidden(playPauseButton, muteButton);

        // Click on the StyledPlayerView and check that controls appear
        clickAndWait(mDevice, playerView);
        assertPlayerControlsVisible(playPauseButton, muteButton);

        // Swipe left to check that controls are now visible on swipe
        swipeLeftAndWait();
        assertPlayerControlsVisible(playPauseButton, muteButton);

        // Check that the player controls are auto hidden in 1s
        assertPlayerControlsAutoHide(playPauseButton, muteButton);

        // We don't test the result of the picker here because the intention of the test is only to
        // test the video controls
    }

    @Test
    public void testMimeTypeFilter() throws Exception {
        final int videoCount = 2;
        mUriList.addAll(createDNGVideosAndGetUris(videoCount, mContext.getUserId()));
        final int imageCount = 1;
        mUriList.addAll(createImagesAndGetUris(imageCount, mContext.getUserId()));

        final String mimeType = "video/dng";

        Intent intent = new Intent(mAction);
        addMultipleSelectionFlag(intent);
        intent.setType(mimeType);
        launchPhotoPickerForIntent(intent);

        // find all items
        final List<UiObject> itemList = findItemList(-1);
        final int itemCount = itemList.size();
        assertThat(itemCount).isAtLeast(videoCount);
        for (int i = 0; i < itemCount; i++) {
            clickAndWait(mDevice, itemList.get(i));
        }

        clickAndWait(mDevice, findAddButton());

        final ClipData clipData = mActivity.getResult().data.getClipData();
        final int count = clipData.getItemCount();
        assertThat(count).isEqualTo(itemCount);
        for (int i = 0; i < count; i++) {
            final Uri uri = clipData.getItemAt(i).getUri();
            assertPickerUriFormat(uri, mContext.getUserId());
            assertPersistedGrant(uri, mContext.getContentResolver());
            assertRedactedReadOnlyAccess(uri);
            assertMimeType(uri, mimeType);
        }
    }

    private void assertMuteButtonState(UiObject muteButton, boolean isMuted)
            throws UiObjectNotFoundException {
        // We use content description to assert the state of the mute button, there is no other way
        // to test this.
        final String expectedContentDescription = isMuted ? "Unmute video" : "Mute video";
        final String assertMessage =
                "Expected mute button content description to be " + expectedContentDescription;
        assertWithMessage(assertMessage).that(muteButton.getContentDescription())
                .isEqualTo(expectedContentDescription);
    }

    private void testVideoPreviewPlayPause() throws Exception {
        final UiObject playPauseButton = findPlayPauseButton();
        final UiObject muteButton = findMuteButton();

        // Wait for buttons to auto hide.
        assertPlayerControlsAutoHide(playPauseButton, muteButton);

        // Click on StyledPlayerView to make the video controls visible
        clickAndWait(mDevice, findPlayerView());

        // PlayPause button is now pause button, click the button to pause the video.
        clickAndWait(mDevice, playPauseButton);

        // Wait for 1s and check that play button is not auto hidden
        assertPlayerControlsDontAutoHide(playPauseButton, muteButton);

        // PlayPause button is now play button, click the button to play the video.
        clickAndWait(mDevice, playPauseButton);
        // Check that pause button auto-hides in 1s.
        assertPlayerControlsAutoHide(playPauseButton, muteButton);
    }

    private void launchPreviewMultipleWithVideos(int videoCount) throws  Exception {
        mUriList.addAll(createVideosAndGetUris(videoCount, mContext.getUserId()));

        Intent intent = new Intent(mAction);
        intent.setType("video/*");
        addMultipleSelectionFlag(intent);
        launchPhotoPickerForIntent(intent);

        final List<UiObject> itemList = findItemList(videoCount);
        final int itemCount = itemList.size();

        assertThat(itemCount).isEqualTo(videoCount);

        for (int i = 0; i < itemCount; i++) {
            clickAndWait(mDevice, itemList.get(i));
        }

        clickAndWait(mDevice, findViewSelectedButton());

        // Wait for playback to start. This is needed in some devices where playback
        // buffering -> ready state takes around 10s.
        final long playbackStartTimeout = 10000;
        (findPreviewVideoImageView()).waitUntilGone(playbackStartTimeout);

        waitForBinderCallsToComplete();
    }

    private void waitForBinderCallsToComplete() {
        // Wait for Binder calls to complete and device to be idle
        MediaStore.waitForIdle(mContext.getContentResolver());
        mDevice.waitForIdle();
    }

    private void setUpAndAssertStickyPlayerControls(UiObject playerView, UiObject playPauseButton,
            UiObject muteButton) throws Exception {
        // Wait for 1s for player view to exist
        playerView.waitForExists(1000);
        // Wait for 1s or Play/Pause button to hide
        playPauseButton.waitUntilGone(1000);
        // Click on StyledPlayerView to make the video controls visible
        clickAndWait(mDevice, playerView);
        assertPlayerControlsVisible(playPauseButton, muteButton);
    }

    private void assertPlayerControlsVisible(UiObject playPauseButton, UiObject muteButton) {
        assertVisible(playPauseButton, "Expected play/pause button to be visible");
        assertVisible(muteButton, "Expected mute button to be visible");
    }

    private void assertPlayerControlsHidden(UiObject playPauseButton, UiObject muteButton) {
        assertHidden(playPauseButton, "Expected play/pause button to be hidden");
        assertHidden(muteButton, "Expected mute button to be hidden");
    }

    private void assertPlayerControlsAutoHide(UiObject playPauseButton, UiObject muteButton) {
        // These buttons should auto hide in 1 second after the video playback start. Since we can't
        // identify the video playback start time, we wait for 2 seconds instead.
        assertWithMessage("Expected play/pause button to auto hide in 2s")
                .that(playPauseButton.waitUntilGone(2000)).isTrue();
        assertHidden(muteButton, "Expected mute button to hide after 2s");
    }

    private void assertPlayerControlsDontAutoHide(UiObject playPauseButton, UiObject muteButton) {
        assertWithMessage("Expected play/pause button to not auto hide in 1s")
                .that(playPauseButton.waitUntilGone(1100)).isFalse();
        assertVisible(muteButton, "Expected mute button to be still visible after 1s");
    }

    private void assertVisible(UiObject button, String message) {
        assertWithMessage(message).that(button.exists()).isTrue();
    }

    private void assertHidden(UiObject button, String message) {
        assertWithMessage(message).that(button.exists()).isFalse();
    }

    private static UiObject findViewSelectedButton() {
        return new UiObject(new UiSelector().resourceIdMatches(
                REGEX_PACKAGE_NAME + ":id/button_view_selected"));
    }

    private static UiObject findPreviewSelectedCheckButton() {
        return new UiObject(new UiSelector().resourceIdMatches(
                REGEX_PACKAGE_NAME + ":id/preview_selected_check_button"));
    }


    private static UiObject findPlayerView() {
        return new UiObject(new UiSelector().resourceIdMatches(
                REGEX_PACKAGE_NAME + ":id/preview_player_view"));
    }

    private static UiObject findMuteButton() {
        return new UiObject(new UiSelector().resourceIdMatches(
                REGEX_PACKAGE_NAME + ":id/preview_mute"));
    }

    private static UiObject findPlayPauseButton() {
        return new UiObject(new UiSelector().resourceIdMatches(
                REGEX_PACKAGE_NAME + ":id/exo_play_pause"));
    }

    private static UiObject findPauseButton() {
        return new UiObject(new UiSelector().descriptionContains("Pause"));
    }

    private static UiObject findPlayButton() {
        return new UiObject(new UiSelector().descriptionContains("Play"));
    }

    private static UiObject findPreviewVideoImageView() {
        return new UiObject(new UiSelector().resourceIdMatches(
                REGEX_PACKAGE_NAME + ":id/preview_video_image"));
    }

    private void swipeLeftAndWait() {
        final int width = mDevice.getDisplayWidth();
        final int height = mDevice.getDisplayHeight();
        mDevice.swipe(15 * width / 20, height / 2, width / 20, height / 2, 10);
        mDevice.waitForIdle();
    }

    private static List<String> getTestParameters() {
        return Arrays.asList(
                MediaStore.ACTION_PICK_IMAGES,
                Intent.ACTION_GET_CONTENT
        );
    }

    private void addMultipleSelectionFlag(Intent intent) {
        switch (intent.getAction()) {
            case MediaStore.ACTION_PICK_IMAGES:
                intent.putExtra(MediaStore.EXTRA_PICK_IMAGES_MAX,
                        MediaStore.getPickImagesMaxLimit());
                break;
            case Intent.ACTION_GET_CONTENT:
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                break;
            default:
                // do nothing
        }
    }

    private void launchPhotoPickerForIntent(Intent intent) throws Exception {
        // GET_CONTENT needs to have setType
        if (Intent.ACTION_GET_CONTENT.equals(intent.getAction()) && intent.getType() == null) {
            intent.setType("*/*");
            intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/*", "video/*"});
        }

        mActivity.startActivityForResult(intent, REQUEST_CODE);
    }
}