Chromium Code Reviews| 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.base.test.util.RetryOnFailure; | |
| 15 import org.chromium.chrome.R; | |
| 16 import org.chromium.chrome.browser.ChromeActivity; | |
| 17 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; | |
| 18 import org.chromium.chrome.browser.widget.selection.SelectionDelegate.SelectionO bserver; | |
| 19 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | |
| 20 import org.chromium.chrome.test.util.browser.RecyclerViewTestUtils; | |
| 21 import org.chromium.content.browser.test.util.TouchCommon; | |
| 22 import org.chromium.ui.PhotoPickerListener; | |
| 23 | |
| 24 import java.util.ArrayList; | |
| 25 import java.util.Arrays; | |
| 26 import java.util.List; | |
| 27 import java.util.concurrent.Callable; | |
| 28 | |
| 29 /** | |
| 30 * Tests for the PhotoPickerDialog class. | |
| 31 */ | |
| 32 @RetryOnFailure | |
| 33 public class PhotoPickerDialogTest extends ChromeActivityTestCaseBase<ChromeActi vity> | |
| 34 implements PhotoPickerListener, SelectionObserver<PickerBitmap> { | |
| 35 // The dialog we are testing. | |
| 36 private PhotoPickerDialog mDialog; | |
| 37 | |
| 38 // The data to show in the dialog (A map of filepath to last-modified time). | |
| 39 // Map<String, Long> mTestFiles; | |
| 40 private List<PickerBitmap> mTestFiles; | |
| 41 | |
| 42 // The last action recorded in the dialog (e.g. photo selected). | |
| 43 private Action mLastActionRecorded; | |
| 44 | |
| 45 // The final set of photos picked by the dialog. Can be an empty array, if | |
| 46 // nothing was selected. | |
| 47 private String[] mLastSelectedPhotos; | |
| 48 | |
| 49 // The list of currently selected photos (built piecemeal). | |
| 50 private List<PickerBitmap> mCurrentPhotoSelection; | |
| 51 | |
| 52 // A callback that fires when something is selected in the dialog. | |
| 53 public final CallbackHelper onSelectionCallback = new CallbackHelper(); | |
| 54 | |
| 55 // A callback that fires when an action is taken in the dialog (cancel/done etc). | |
| 56 public final CallbackHelper onActionCallback = new CallbackHelper(); | |
| 57 | |
| 58 public PhotoPickerDialogTest() { | |
| 59 super(ChromeActivity.class); | |
| 60 } | |
| 61 | |
| 62 // ChromeActivityTestCaseBase: | |
| 63 | |
| 64 @Override | |
| 65 protected void setUp() throws Exception { | |
| 66 super.setUp(); | |
| 67 | |
| 68 mTestFiles = new ArrayList<>(); | |
| 69 mTestFiles.add(new PickerBitmap("a", 5L, PickerBitmap.PICTURE)); | |
| 70 mTestFiles.add(new PickerBitmap("b", 4L, PickerBitmap.PICTURE)); | |
| 71 mTestFiles.add(new PickerBitmap("c", 3L, PickerBitmap.PICTURE)); | |
| 72 mTestFiles.add(new PickerBitmap("d", 2L, PickerBitmap.PICTURE)); | |
| 73 mTestFiles.add(new PickerBitmap("e", 1L, PickerBitmap.PICTURE)); | |
| 74 mTestFiles.add(new PickerBitmap("f", 0L, PickerBitmap.PICTURE)); | |
| 75 PickerCategoryView.setTestFiles(mTestFiles); | |
| 76 } | |
| 77 | |
| 78 @Override | |
| 79 public void startMainActivity() throws InterruptedException { | |
| 80 startMainActivityOnBlankPage(); | |
| 81 } | |
| 82 | |
| 83 // PhotoPickerDialog.PhotoPickerListener: | |
| 84 | |
| 85 @Override | |
| 86 public void onPickerUserAction(Action action, String[] photos) { | |
| 87 mLastActionRecorded = action; | |
| 88 mLastSelectedPhotos = photos != null ? photos.clone() : null; | |
| 89 if (mLastSelectedPhotos != null) Arrays.sort(mLastSelectedPhotos); | |
| 90 onActionCallback.notifyCalled(); | |
| 91 } | |
| 92 | |
| 93 // SelectionObserver: | |
| 94 | |
| 95 @Override | |
| 96 public void onSelectionStateChange(List<PickerBitmap> photosSelected) { | |
| 97 mCurrentPhotoSelection = new ArrayList<>(photosSelected); | |
| 98 onSelectionCallback.notifyCalled(); | |
| 99 } | |
| 100 | |
| 101 private RecyclerView recyclerView() { | |
|
Theresa
2017/05/23 15:10:29
nit: getRecyclerView()?
Finnur
2017/05/23 15:57:46
Done.
| |
| 102 return (RecyclerView) mDialog.findViewById(R.id.recycler_view); | |
| 103 } | |
| 104 | |
| 105 private PhotoPickerDialog createDialog(final boolean multiselect) { | |
| 106 final PhotoPickerDialog dialog = | |
| 107 ThreadUtils.runOnUiThreadBlockingNoException(new Callable<PhotoP ickerDialog>() { | |
|
Theresa
2017/05/23 15:10:30
Typically I see ThreadUtils.runOnUiThreadBlocking(
Finnur
2017/05/23 15:57:46
Hmm... no. Probably just copied from somewhere.
| |
| 108 @Override | |
| 109 public PhotoPickerDialog call() { | |
| 110 final PhotoPickerDialog dialog = new PhotoPickerDialog( | |
| 111 getActivity(), PhotoPickerDialogTest.this, multi select); | |
| 112 dialog.show(); | |
| 113 return dialog; | |
| 114 } | |
| 115 }); | |
| 116 | |
| 117 SelectionDelegate<PickerBitmap> selectionDelegate = | |
| 118 dialog.getCategoryViewForTesting().getSelectionDelegateForTestin g(); | |
| 119 if (!multiselect) selectionDelegate.setSingleSelectionMode(); | |
| 120 selectionDelegate.addObserver(this); | |
| 121 mDialog = dialog; | |
| 122 | |
| 123 return dialog; | |
| 124 } | |
| 125 | |
| 126 public void clickView(final int position, final int expectedSelectionCount) throws Exception { | |
|
Theresa
2017/05/23 15:10:29
nit: can this method be private? Same for the othe
Finnur
2017/05/23 15:57:46
Done.
| |
| 127 RecyclerView recyclerView = recyclerView(); | |
| 128 final PickerBitmapViewHolder holder = | |
|
Theresa
2017/05/23 15:10:29
I believe findbugs is complaining about this line
Finnur
2017/05/23 15:57:46
Done.
| |
| 129 (PickerBitmapViewHolder) RecyclerViewTestUtils.waitForView(recyc lerView, position); | |
| 130 | |
| 131 int callCount = onSelectionCallback.getCallCount(); | |
| 132 TouchCommon.singleClickView( | |
| 133 recyclerView.findViewHolderForAdapterPosition(position).itemView ); | |
| 134 onSelectionCallback.waitForCallback(callCount, 1); | |
| 135 | |
| 136 // Validate the correct selection took place. | |
| 137 assertEquals(expectedSelectionCount, mCurrentPhotoSelection.size()); | |
| 138 int i = 0; | |
|
Theresa
2017/05/23 15:10:29
nit: put the i = 0 inside the for loop since it's
Finnur
2017/05/23 15:57:46
Yes, I moved it out because it was being used, but
| |
| 139 for (; i < mCurrentPhotoSelection.size(); ++i) { | |
| 140 if (mCurrentPhotoSelection.get(i).getFilePath().equals( | |
| 141 mTestFiles.get(position).getFilePath())) { | |
|
Theresa
2017/05/23 15:10:29
SelectionDelegate has an #isItemSelected() method
Finnur
2017/05/23 15:57:46
Ah, right! Of course. Done.
| |
| 142 return; | |
| 143 } | |
| 144 } | |
| 145 // NOTREACHED. | |
| 146 assertTrue(false); | |
| 147 } | |
| 148 | |
| 149 public void clickDone() throws Exception { | |
| 150 mLastActionRecorded = null; | |
| 151 | |
| 152 PhotoPickerToolbar toolbar = (PhotoPickerToolbar) mDialog.findViewById(R .id.action_bar); | |
| 153 Button done = (Button) toolbar.findViewById(R.id.done); | |
| 154 int callCount = onActionCallback.getCallCount(); | |
| 155 TouchCommon.singleClickView(done); | |
| 156 onActionCallback.waitForCallback(callCount, 1); | |
| 157 assertEquals(PhotoPickerListener.Action.PHOTOS_SELECTED, mLastActionReco rded); | |
| 158 } | |
| 159 | |
| 160 public void clickCancel() throws Exception { | |
| 161 mLastActionRecorded = null; | |
| 162 | |
| 163 PickerCategoryView categoryView = mDialog.getCategoryViewForTesting(); | |
| 164 View cancel = new View(getActivity()); | |
| 165 int callCount = onActionCallback.getCallCount(); | |
| 166 categoryView.onClick(cancel); | |
| 167 onActionCallback.waitForCallback(callCount, 1); | |
| 168 assertEquals(PhotoPickerListener.Action.CANCEL, mLastActionRecorded); | |
| 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 mDialog.dismiss(); | |
| 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 mDialog.dismiss(); | |
| 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 mDialog.dismiss(); | |
| 223 } | |
| 224 } | |
| OLD | NEW |