OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.photo_picker; |
| 6 |
| 7 import android.support.test.filters.LargeTest; |
| 8 import android.support.v7.widget.RecyclerView; |
| 9 import android.view.View; |
| 10 import android.widget.Button; |
| 11 |
| 12 import org.chromium.base.ThreadUtils; |
| 13 import org.chromium.base.test.util.CallbackHelper; |
| 14 import org.chromium.chrome.R; |
| 15 import org.chromium.chrome.browser.ChromeActivity; |
| 16 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; |
| 17 import org.chromium.chrome.browser.widget.selection.SelectionDelegate.SelectionO
bserver; |
| 18 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
| 19 import org.chromium.chrome.test.util.browser.RecyclerViewTestUtils; |
| 20 import org.chromium.content.browser.test.util.TouchCommon; |
| 21 import org.chromium.ui.PhotoPickerListener; |
| 22 |
| 23 import java.util.ArrayList; |
| 24 import java.util.Arrays; |
| 25 import java.util.List; |
| 26 import java.util.concurrent.Callable; |
| 27 |
| 28 /** |
| 29 * Tests for the PhotoPickerDialog class. |
| 30 */ |
| 31 public class PhotoPickerDialogTest extends ChromeActivityTestCaseBase<ChromeActi
vity> |
| 32 implements PhotoPickerListener, SelectionObserver<PickerBitmap> { |
| 33 // The dialog we are testing. |
| 34 private PhotoPickerDialog mDialog; |
| 35 |
| 36 // The data to show in the dialog (A map of filepath to last-modified time). |
| 37 // Map<String, Long> mTestFiles; |
| 38 private List<PickerBitmap> mTestFiles; |
| 39 |
| 40 // The selection delegate for the dialog. |
| 41 private SelectionDelegate<PickerBitmap> mSelectionDelegate; |
| 42 |
| 43 // The last action recorded in the dialog (e.g. photo selected). |
| 44 private Action mLastActionRecorded; |
| 45 |
| 46 // The final set of photos picked by the dialog. Can be an empty array, if |
| 47 // nothing was selected. |
| 48 private String[] mLastSelectedPhotos; |
| 49 |
| 50 // The list of currently selected photos (built piecemeal). |
| 51 private List<PickerBitmap> mCurrentPhotoSelection; |
| 52 |
| 53 // A callback that fires when something is selected in the dialog. |
| 54 public final CallbackHelper onSelectionCallback = new CallbackHelper(); |
| 55 |
| 56 // A callback that fires when an action is taken in the dialog (cancel/done
etc). |
| 57 public final CallbackHelper onActionCallback = new CallbackHelper(); |
| 58 |
| 59 public PhotoPickerDialogTest() { |
| 60 super(ChromeActivity.class); |
| 61 } |
| 62 |
| 63 // ChromeActivityTestCaseBase: |
| 64 |
| 65 @Override |
| 66 protected void setUp() throws Exception { |
| 67 super.setUp(); |
| 68 |
| 69 mTestFiles = new ArrayList<>(); |
| 70 mTestFiles.add(new PickerBitmap("a", 5L, PickerBitmap.PICTURE)); |
| 71 mTestFiles.add(new PickerBitmap("b", 4L, PickerBitmap.PICTURE)); |
| 72 mTestFiles.add(new PickerBitmap("c", 3L, PickerBitmap.PICTURE)); |
| 73 mTestFiles.add(new PickerBitmap("d", 2L, PickerBitmap.PICTURE)); |
| 74 mTestFiles.add(new PickerBitmap("e", 1L, PickerBitmap.PICTURE)); |
| 75 mTestFiles.add(new PickerBitmap("f", 0L, PickerBitmap.PICTURE)); |
| 76 PickerCategoryView.setTestFiles(mTestFiles); |
| 77 } |
| 78 |
| 79 @Override |
| 80 public void startMainActivity() throws InterruptedException { |
| 81 startMainActivityOnBlankPage(); |
| 82 } |
| 83 |
| 84 // PhotoPickerDialog.PhotoPickerListener: |
| 85 |
| 86 @Override |
| 87 public void onPickerUserAction(Action action, String[] photos) { |
| 88 mLastActionRecorded = action; |
| 89 mLastSelectedPhotos = photos != null ? photos.clone() : null; |
| 90 if (mLastSelectedPhotos != null) Arrays.sort(mLastSelectedPhotos); |
| 91 onActionCallback.notifyCalled(); |
| 92 } |
| 93 |
| 94 // SelectionObserver: |
| 95 |
| 96 @Override |
| 97 public void onSelectionStateChange(List<PickerBitmap> photosSelected) { |
| 98 mCurrentPhotoSelection = new ArrayList<>(photosSelected); |
| 99 onSelectionCallback.notifyCalled(); |
| 100 } |
| 101 |
| 102 private RecyclerView getRecyclerView() { |
| 103 return (RecyclerView) mDialog.findViewById(R.id.recycler_view); |
| 104 } |
| 105 |
| 106 private PhotoPickerDialog createDialog(final boolean multiselect) throws Exc
eption { |
| 107 final PhotoPickerDialog dialog = |
| 108 ThreadUtils.runOnUiThreadBlocking(new Callable<PhotoPickerDialog
>() { |
| 109 @Override |
| 110 public PhotoPickerDialog call() { |
| 111 final PhotoPickerDialog dialog = new PhotoPickerDialog( |
| 112 getActivity(), PhotoPickerDialogTest.this, multi
select); |
| 113 dialog.show(); |
| 114 return dialog; |
| 115 } |
| 116 }); |
| 117 |
| 118 mSelectionDelegate = dialog.getCategoryViewForTesting().getSelectionDele
gateForTesting(); |
| 119 if (!multiselect) mSelectionDelegate.setSingleSelectionMode(); |
| 120 mSelectionDelegate.addObserver(this); |
| 121 mDialog = dialog; |
| 122 |
| 123 return dialog; |
| 124 } |
| 125 |
| 126 private void clickView(final int position, final int expectedSelectionCount)
throws Exception { |
| 127 RecyclerView recyclerView = getRecyclerView(); |
| 128 RecyclerViewTestUtils.waitForView(recyclerView, position); |
| 129 |
| 130 int callCount = onSelectionCallback.getCallCount(); |
| 131 TouchCommon.singleClickView( |
| 132 recyclerView.findViewHolderForAdapterPosition(position).itemView
); |
| 133 onSelectionCallback.waitForCallback(callCount, 1); |
| 134 |
| 135 // Validate the correct selection took place. |
| 136 assertEquals(expectedSelectionCount, mCurrentPhotoSelection.size()); |
| 137 assertTrue(mSelectionDelegate.isItemSelected(mTestFiles.get(position))); |
| 138 } |
| 139 |
| 140 private void clickDone() throws Exception { |
| 141 mLastActionRecorded = null; |
| 142 |
| 143 PhotoPickerToolbar toolbar = (PhotoPickerToolbar) mDialog.findViewById(R
.id.action_bar); |
| 144 Button done = (Button) toolbar.findViewById(R.id.done); |
| 145 int callCount = onActionCallback.getCallCount(); |
| 146 TouchCommon.singleClickView(done); |
| 147 onActionCallback.waitForCallback(callCount, 1); |
| 148 assertEquals(PhotoPickerListener.Action.PHOTOS_SELECTED, mLastActionReco
rded); |
| 149 } |
| 150 |
| 151 public void clickCancel() throws Exception { |
| 152 mLastActionRecorded = null; |
| 153 |
| 154 PickerCategoryView categoryView = mDialog.getCategoryViewForTesting(); |
| 155 View cancel = new View(getActivity()); |
| 156 int callCount = onActionCallback.getCallCount(); |
| 157 categoryView.onClick(cancel); |
| 158 onActionCallback.waitForCallback(callCount, 1); |
| 159 assertEquals(PhotoPickerListener.Action.CANCEL, mLastActionRecorded); |
| 160 } |
| 161 |
| 162 private void dismissDialog() { |
| 163 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 164 @Override |
| 165 public void run() { |
| 166 mDialog.dismiss(); |
| 167 } |
| 168 }); |
| 169 } |
| 170 |
| 171 @LargeTest |
| 172 public void testNoSelection() throws Throwable { |
| 173 createDialog(false); // Multi-select = false. |
| 174 assertTrue(mDialog.isShowing()); |
| 175 |
| 176 int expectedSelectionCount = 1; |
| 177 clickView(0, expectedSelectionCount); |
| 178 clickCancel(); |
| 179 |
| 180 assertEquals(null, mLastSelectedPhotos); |
| 181 assertEquals(PhotoPickerListener.Action.CANCEL, mLastActionRecorded); |
| 182 |
| 183 dismissDialog(); |
| 184 } |
| 185 |
| 186 @LargeTest |
| 187 public void testSingleSelectionPhoto() throws Throwable { |
| 188 createDialog(false); // Multi-select = false. |
| 189 assertTrue(mDialog.isShowing()); |
| 190 |
| 191 // Expected selection count is 1 because clicking on a new view unselect
s other. |
| 192 int expectedSelectionCount = 1; |
| 193 clickView(0, expectedSelectionCount); |
| 194 clickView(1, expectedSelectionCount); |
| 195 clickDone(); |
| 196 |
| 197 assertEquals(1, mLastSelectedPhotos.length); |
| 198 assertEquals(PhotoPickerListener.Action.PHOTOS_SELECTED, mLastActionReco
rded); |
| 199 assertEquals(mTestFiles.get(1).getFilePath(), mLastSelectedPhotos[0]); |
| 200 |
| 201 dismissDialog(); |
| 202 } |
| 203 |
| 204 @LargeTest |
| 205 public void testMultiSelectionPhoto() throws Throwable { |
| 206 createDialog(true); // Multi-select = true. |
| 207 assertTrue(mDialog.isShowing()); |
| 208 |
| 209 // Multi-selection is enabled, so each click is counted. |
| 210 int expectedSelectionCount = 1; |
| 211 clickView(0, expectedSelectionCount++); |
| 212 clickView(2, expectedSelectionCount++); |
| 213 clickView(4, expectedSelectionCount++); |
| 214 clickDone(); |
| 215 |
| 216 assertEquals(3, mLastSelectedPhotos.length); |
| 217 assertEquals(PhotoPickerListener.Action.PHOTOS_SELECTED, mLastActionReco
rded); |
| 218 assertEquals(mTestFiles.get(0).getFilePath(), mLastSelectedPhotos[0]); |
| 219 assertEquals(mTestFiles.get(2).getFilePath(), mLastSelectedPhotos[1]); |
| 220 assertEquals(mTestFiles.get(4).getFilePath(), mLastSelectedPhotos[2]); |
| 221 |
| 222 dismissDialog(); |
| 223 } |
| 224 } |
OLD | NEW |