Chromium Code Reviews| Index: chrome/android/javatests/src/org/chromium/chrome/browser/photo_picker/PhotoPickerDialogTest.java |
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/photo_picker/PhotoPickerDialogTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/photo_picker/PhotoPickerDialogTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..097c8f4a1307804bc54671db12790131d2a2816c |
| --- /dev/null |
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/photo_picker/PhotoPickerDialogTest.java |
| @@ -0,0 +1,265 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.photo_picker; |
| + |
| +import android.support.test.filters.LargeTest; |
| +import android.support.v7.widget.RecyclerView; |
| +import android.view.View; |
| +import android.widget.Button; |
| + |
| +import org.chromium.base.ThreadUtils; |
| +import org.chromium.base.test.util.RetryOnFailure; |
| +import org.chromium.chrome.R; |
| +import org.chromium.chrome.browser.ChromeActivity; |
| +import org.chromium.chrome.browser.widget.selection.SelectionDelegate; |
| +import org.chromium.chrome.browser.widget.selection.SelectionDelegate.SelectionObserver; |
| +import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
| +import org.chromium.chrome.test.util.browser.RecyclerViewTestUtils; |
| +import org.chromium.content.browser.test.util.Criteria; |
| +import org.chromium.content.browser.test.util.CriteriaHelper; |
| +import org.chromium.content.browser.test.util.TouchCommon; |
| +import org.chromium.ui.PhotoPickerListener; |
| + |
| +import java.util.ArrayList; |
| +import java.util.Arrays; |
| +import java.util.List; |
| +import java.util.concurrent.Callable; |
| + |
| +/** |
| + * Tests for the PhotoPickerDialog class. |
| + */ |
| +@RetryOnFailure |
| +public class PhotoPickerDialogTest extends ChromeActivityTestCaseBase<ChromeActivity> |
| + implements PhotoPickerListener, SelectionObserver<PickerBitmap> { |
| + // The dialog we are testing. |
| + private PhotoPickerDialog mDialog; |
| + |
| + // The data to show in the dialog (A map of filepath to last-modified time). |
| + // Map<String, Long> mTestFiles; |
| + private List<PickerBitmap> mTestFiles; |
| + |
| + // The last action recorded in the dialog (e.g. photo selected). |
| + private Action mLastActionRecorded; |
| + |
| + // The final set of photos picked by the dialog. Can be an empty array, if |
| + // nothing was selected. |
| + private String[] mLastSelectedPhotos; |
| + |
| + // The list of currently selected photos (built piecemeal). |
| + private List<PickerBitmap> mCurrentPhotoSelection; |
| + |
| + public PhotoPickerDialogTest() { |
| + super(ChromeActivity.class); |
| + } |
| + |
| + // ChromeActivityTestCaseBase: |
| + |
| + @Override |
| + protected void setUp() throws Exception { |
| + super.setUp(); |
| + |
| + mTestFiles = new ArrayList<>(); |
| + mTestFiles.add(new PickerBitmap("a", 5L, PickerBitmap.PICTURE)); |
| + mTestFiles.add(new PickerBitmap("b", 4L, PickerBitmap.PICTURE)); |
| + mTestFiles.add(new PickerBitmap("c", 3L, PickerBitmap.PICTURE)); |
| + mTestFiles.add(new PickerBitmap("d", 2L, PickerBitmap.PICTURE)); |
| + mTestFiles.add(new PickerBitmap("e", 1L, PickerBitmap.PICTURE)); |
| + mTestFiles.add(new PickerBitmap("f", 0L, PickerBitmap.PICTURE)); |
| + |
| + /* |
| + mTestFiles.add(new PickerBitmap("/storage/emulated/0/DCIM/Camera/IMG_20170201_154701.jpg", |
| + 5L, PickerBitmap.PICTURE)); mTestFiles.add(new |
|
Theresa
2017/05/19 16:52:42
nit: remove this comment block
Finnur
2017/05/23 13:19:50
Oops. :) Done.
|
| + PickerBitmap("/storage/emulated/0/DCIM/Camera/IMG_20170201_154704.jpg", 4L, |
| + PickerBitmap.PICTURE)); mTestFiles.add(new |
| + PickerBitmap("/storage/emulated/0/DCIM/Camera/IMG_20170201_154707.jpg", 3L, |
| + PickerBitmap.PICTURE)); mTestFiles.add(new |
| + PickerBitmap("/storage/emulated/0/DCIM/Camera/IMG_20170201_154709.jpg", 2L, |
| + PickerBitmap.PICTURE)); mTestFiles.add(new |
| + PickerBitmap("/storage/emulated/0/DCIM/Camera/IMG_20170201_154711.jpg", 1L, |
| + PickerBitmap.PICTURE)); mTestFiles.add(new |
| + PickerBitmap("/storage/emulated/0/DCIM/Camera/IMG_20170201_154714.jpg", 0L, |
| + PickerBitmap.PICTURE)); |
| + */ |
| + } |
| + |
| + @Override |
| + public void startMainActivity() throws InterruptedException { |
| + startMainActivityOnBlankPage(); |
| + } |
| + |
| + // PhotoPickerDialog.PhotoPickerListener: |
| + |
| + @Override |
| + public void onPickerUserAction(Action action, String[] photos) { |
| + mLastActionRecorded = action; |
| + mLastSelectedPhotos = photos; |
| + if (mLastSelectedPhotos != null) Arrays.sort(mLastSelectedPhotos); |
| + } |
| + |
| + // SelectionObserver: |
| + |
| + @Override |
| + public void onSelectionStateChange(List<PickerBitmap> photosSelected) { |
| + mCurrentPhotoSelection = new ArrayList<>(photosSelected); |
| + } |
| + |
| + private RecyclerView recyclerView() { |
| + return (RecyclerView) mDialog.findViewById(R.id.recycler_view); |
| + } |
| + |
| + private PhotoPickerDialog createDialog(final boolean multiselect) { |
| + final PhotoPickerDialog dialog = |
| + ThreadUtils.runOnUiThreadBlockingNoException(new Callable<PhotoPickerDialog>() { |
| + @Override |
| + public PhotoPickerDialog call() { |
| + final PhotoPickerDialog dialog = new PhotoPickerDialog( |
| + getActivity(), PhotoPickerDialogTest.this, multiselect, mTestFiles); |
| + dialog.show(); |
| + return dialog; |
| + } |
| + }); |
| + |
| + SelectionDelegate<PickerBitmap> selectionDelegate = |
| + dialog.getCategoryViewForTesting().getSelectionDelegateForTesting(); |
| + if (!multiselect) selectionDelegate.setSingleSelectionMode(); |
| + selectionDelegate.addObserver(this); |
| + |
| + return dialog; |
| + } |
| + |
| + public void waitForRecyclerToStart() { |
| + CriteriaHelper.pollUiThread(new Criteria() { |
|
Theresa
2017/05/19 16:52:42
Where possible, it's preferable to rely on specifi
Finnur
2017/05/23 13:19:51
Not all of the polling turned out to be necessary;
|
| + @Override |
| + public boolean isSatisfied() { |
| + RecyclerView recyclerView = recyclerView(); |
| + return recyclerView != null && recyclerView.getChildCount() > 0; |
| + } |
| + }); |
| + } |
| + |
| + public void waitForViewToStartLoading(int position) { |
| + final PickerBitmapViewHolder holder = |
| + (PickerBitmapViewHolder) RecyclerViewTestUtils.waitForView( |
| + recyclerView(), position); |
| + |
| + CriteriaHelper.pollUiThread(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + return holder.getImageLoadingForTesting(); |
| + } |
| + }); |
| + } |
| + |
| + public void clickView(final int position, final int expectedSelectionCount) { |
| + waitForViewToStartLoading(position); |
| + |
| + RecyclerView recyclerView = recyclerView(); |
| + TouchCommon.singleClickView( |
| + recyclerView.findViewHolderForAdapterPosition(position).itemView); |
| + |
| + CriteriaHelper.pollUiThread(new Criteria() { |
|
Theresa
2017/05/19 16:52:42
This is waiting for the view to be selected, corre
Finnur
2017/05/23 13:19:50
Done.
|
| + @Override |
| + public boolean isSatisfied() { |
| + if (mCurrentPhotoSelection == null |
| + || mCurrentPhotoSelection.size() != expectedSelectionCount) { |
| + return false; |
| + } |
| + |
| + for (int i = 0; i < mCurrentPhotoSelection.size(); ++i) { |
| + if (mCurrentPhotoSelection.get(i).getFilePath().equals( |
| + mTestFiles.get(position).getFilePath())) { |
| + return true; |
| + } |
| + } |
| + return false; |
| + } |
| + }); |
| + } |
| + |
| + public void clickDone() { |
| + mLastActionRecorded = null; |
| + |
| + PhotoPickerToolbar toolbar = (PhotoPickerToolbar) mDialog.findViewById(R.id.action_bar); |
| + Button done = (Button) toolbar.findViewById(R.id.done); |
| + TouchCommon.singleClickView(done); |
| + |
| + CriteriaHelper.pollUiThread(new Criteria() { |
|
Theresa
2017/05/19 16:52:42
Instead of polling the UI thread, you should be ab
Finnur
2017/05/23 13:19:51
Done.
|
| + @Override |
| + public boolean isSatisfied() { |
| + return mLastActionRecorded != null; |
| + } |
| + }); |
| + } |
| + |
| + public void clickCancel() { |
| + mLastActionRecorded = null; |
| + |
| + PickerCategoryView categoryView = mDialog.getCategoryViewForTesting(); |
| + View cancel = new View(getActivity()); |
| + categoryView.onClick(cancel); |
| + |
| + CriteriaHelper.pollUiThread(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + return mLastActionRecorded != null; |
| + } |
| + }); |
| + } |
| + |
| + @LargeTest |
| + public void testNoSelection() throws Throwable { |
| + mDialog = createDialog(false); // Multi-select = false. |
| + waitForRecyclerToStart(); |
| + assertTrue(mDialog.isShowing()); |
| + |
| + clickCancel(); |
| + |
| + assertEquals(null, mLastSelectedPhotos); |
| + assertEquals(PhotoPickerListener.Action.CANCEL, mLastActionRecorded); |
| + |
| + mDialog.dismiss(); |
| + } |
| + |
| + @LargeTest |
| + public void testSingleSelectionPhoto() throws Throwable { |
| + mDialog = createDialog(false); // Multi-select = false. |
| + waitForRecyclerToStart(); |
| + assertTrue(mDialog.isShowing()); |
| + |
| + // Expected selection count is 1 because clicking on a new view unselects other. |
| + int expectedSelectionCount = 1; |
| + clickView(0, expectedSelectionCount); |
| + clickView(1, expectedSelectionCount); |
| + clickDone(); |
| + |
| + assertEquals(1, mLastSelectedPhotos.length); |
| + assertEquals(PhotoPickerListener.Action.PHOTOS_SELECTED, mLastActionRecorded); |
| + assertEquals(mTestFiles.get(1).getFilePath(), mLastSelectedPhotos[0]); |
| + |
| + mDialog.dismiss(); |
| + } |
| + |
| + @LargeTest |
| + public void testMultiSelectionPhoto() throws Throwable { |
| + mDialog = createDialog(true); // Multi-select = true. |
| + waitForRecyclerToStart(); |
| + assertTrue(mDialog.isShowing()); |
| + |
| + // Multi-selection is enabled, so each click is counted. |
| + int expectedSelectionCount = 1; |
| + clickView(0, expectedSelectionCount++); |
| + clickView(2, expectedSelectionCount++); |
| + clickView(4, expectedSelectionCount++); |
| + clickDone(); |
| + |
| + assertEquals(3, mLastSelectedPhotos.length); |
| + assertEquals(PhotoPickerListener.Action.PHOTOS_SELECTED, mLastActionRecorded); |
| + assertEquals(mTestFiles.get(0).getFilePath(), mLastSelectedPhotos[0]); |
| + assertEquals(mTestFiles.get(2).getFilePath(), mLastSelectedPhotos[1]); |
| + assertEquals(mTestFiles.get(4).getFilePath(), mLastSelectedPhotos[2]); |
| + |
| + mDialog.dismiss(); |
| + } |
| +} |