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 | |
Ted C
2017/05/23 20:57:39
we shouldn't add RetryOnFailure for new test class
Finnur
2017/05/23 21:50:35
Ah, good catch. Copy paste error. Removed.
| |
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 selection delegate for the dialog. | |
43 private SelectionDelegate<PickerBitmap> mSelectionDelegate; | |
44 | |
45 // The last action recorded in the dialog (e.g. photo selected). | |
46 private Action mLastActionRecorded; | |
47 | |
48 // The final set of photos picked by the dialog. Can be an empty array, if | |
49 // nothing was selected. | |
50 private String[] mLastSelectedPhotos; | |
51 | |
52 // The list of currently selected photos (built piecemeal). | |
53 private List<PickerBitmap> mCurrentPhotoSelection; | |
54 | |
55 // A callback that fires when something is selected in the dialog. | |
56 public final CallbackHelper onSelectionCallback = new CallbackHelper(); | |
57 | |
58 // A callback that fires when an action is taken in the dialog (cancel/done etc). | |
59 public final CallbackHelper onActionCallback = new CallbackHelper(); | |
60 | |
61 public PhotoPickerDialogTest() { | |
62 super(ChromeActivity.class); | |
63 } | |
64 | |
65 // ChromeActivityTestCaseBase: | |
66 | |
67 @Override | |
68 protected void setUp() throws Exception { | |
69 super.setUp(); | |
70 | |
71 mTestFiles = new ArrayList<>(); | |
72 mTestFiles.add(new PickerBitmap("a", 5L, PickerBitmap.PICTURE)); | |
73 mTestFiles.add(new PickerBitmap("b", 4L, PickerBitmap.PICTURE)); | |
74 mTestFiles.add(new PickerBitmap("c", 3L, PickerBitmap.PICTURE)); | |
75 mTestFiles.add(new PickerBitmap("d", 2L, PickerBitmap.PICTURE)); | |
76 mTestFiles.add(new PickerBitmap("e", 1L, PickerBitmap.PICTURE)); | |
77 mTestFiles.add(new PickerBitmap("f", 0L, PickerBitmap.PICTURE)); | |
78 PickerCategoryView.setTestFiles(mTestFiles); | |
79 } | |
80 | |
81 @Override | |
82 public void startMainActivity() throws InterruptedException { | |
83 startMainActivityOnBlankPage(); | |
84 } | |
85 | |
86 // PhotoPickerDialog.PhotoPickerListener: | |
87 | |
88 @Override | |
89 public void onPickerUserAction(Action action, String[] photos) { | |
90 mLastActionRecorded = action; | |
91 mLastSelectedPhotos = photos != null ? photos.clone() : null; | |
92 if (mLastSelectedPhotos != null) Arrays.sort(mLastSelectedPhotos); | |
93 onActionCallback.notifyCalled(); | |
94 } | |
95 | |
96 // SelectionObserver: | |
97 | |
98 @Override | |
99 public void onSelectionStateChange(List<PickerBitmap> photosSelected) { | |
100 mCurrentPhotoSelection = new ArrayList<>(photosSelected); | |
101 onSelectionCallback.notifyCalled(); | |
102 } | |
103 | |
104 private RecyclerView getRecyclerView() { | |
105 return (RecyclerView) mDialog.findViewById(R.id.recycler_view); | |
106 } | |
107 | |
108 private PhotoPickerDialog createDialog(final boolean multiselect) throws Exc eption { | |
109 final PhotoPickerDialog dialog = | |
110 ThreadUtils.runOnUiThreadBlocking(new Callable<PhotoPickerDialog >() { | |
111 @Override | |
112 public PhotoPickerDialog call() { | |
113 final PhotoPickerDialog dialog = new PhotoPickerDialog( | |
114 getActivity(), PhotoPickerDialogTest.this, multi select); | |
115 dialog.show(); | |
116 return dialog; | |
117 } | |
118 }); | |
119 | |
120 mSelectionDelegate = dialog.getCategoryViewForTesting().getSelectionDele gateForTesting(); | |
121 if (!multiselect) mSelectionDelegate.setSingleSelectionMode(); | |
122 mSelectionDelegate.addObserver(this); | |
123 mDialog = dialog; | |
124 | |
125 return dialog; | |
126 } | |
127 | |
128 private void clickView(final int position, final int expectedSelectionCount) throws Exception { | |
129 RecyclerView recyclerView = getRecyclerView(); | |
130 RecyclerViewTestUtils.waitForView(recyclerView, position); | |
131 | |
132 int callCount = onSelectionCallback.getCallCount(); | |
133 TouchCommon.singleClickView( | |
134 recyclerView.findViewHolderForAdapterPosition(position).itemView ); | |
135 onSelectionCallback.waitForCallback(callCount, 1); | |
136 | |
137 // Validate the correct selection took place. | |
138 assertEquals(expectedSelectionCount, mCurrentPhotoSelection.size()); | |
139 assertTrue(mSelectionDelegate.isItemSelected(mTestFiles.get(position))); | |
140 } | |
141 | |
142 private void clickDone() throws Exception { | |
143 mLastActionRecorded = null; | |
144 | |
145 PhotoPickerToolbar toolbar = (PhotoPickerToolbar) mDialog.findViewById(R .id.action_bar); | |
146 Button done = (Button) toolbar.findViewById(R.id.done); | |
147 int callCount = onActionCallback.getCallCount(); | |
148 TouchCommon.singleClickView(done); | |
149 onActionCallback.waitForCallback(callCount, 1); | |
150 assertEquals(PhotoPickerListener.Action.PHOTOS_SELECTED, mLastActionReco rded); | |
151 } | |
152 | |
153 public void clickCancel() throws Exception { | |
154 mLastActionRecorded = null; | |
155 | |
156 PickerCategoryView categoryView = mDialog.getCategoryViewForTesting(); | |
157 View cancel = new View(getActivity()); | |
158 int callCount = onActionCallback.getCallCount(); | |
159 categoryView.onClick(cancel); | |
160 onActionCallback.waitForCallback(callCount, 1); | |
161 assertEquals(PhotoPickerListener.Action.CANCEL, mLastActionRecorded); | |
162 } | |
163 | |
164 @LargeTest | |
165 public void testNoSelection() throws Throwable { | |
166 createDialog(false); // Multi-select = false. | |
167 assertTrue(mDialog.isShowing()); | |
168 | |
169 int expectedSelectionCount = 1; | |
170 clickView(0, expectedSelectionCount); | |
171 clickCancel(); | |
172 | |
173 assertEquals(null, mLastSelectedPhotos); | |
174 assertEquals(PhotoPickerListener.Action.CANCEL, mLastActionRecorded); | |
175 | |
176 mDialog.dismiss(); | |
Ted C
2017/05/23 20:57:39
I would wrap these in ThreadUtils.runOnUiThreadBlo
Finnur
2017/05/23 21:50:35
Done.
| |
177 } | |
178 | |
179 @LargeTest | |
180 public void testSingleSelectionPhoto() throws Throwable { | |
181 createDialog(false); // Multi-select = false. | |
182 assertTrue(mDialog.isShowing()); | |
183 | |
184 // Expected selection count is 1 because clicking on a new view unselect s other. | |
185 int expectedSelectionCount = 1; | |
186 clickView(0, expectedSelectionCount); | |
187 clickView(1, expectedSelectionCount); | |
188 clickDone(); | |
189 | |
190 assertEquals(1, mLastSelectedPhotos.length); | |
191 assertEquals(PhotoPickerListener.Action.PHOTOS_SELECTED, mLastActionReco rded); | |
192 assertEquals(mTestFiles.get(1).getFilePath(), mLastSelectedPhotos[0]); | |
193 | |
194 mDialog.dismiss(); | |
195 } | |
196 | |
197 @LargeTest | |
198 public void testMultiSelectionPhoto() throws Throwable { | |
199 createDialog(true); // Multi-select = true. | |
200 assertTrue(mDialog.isShowing()); | |
201 | |
202 // Multi-selection is enabled, so each click is counted. | |
203 int expectedSelectionCount = 1; | |
204 clickView(0, expectedSelectionCount++); | |
205 clickView(2, expectedSelectionCount++); | |
206 clickView(4, expectedSelectionCount++); | |
207 clickDone(); | |
208 | |
209 assertEquals(3, mLastSelectedPhotos.length); | |
210 assertEquals(PhotoPickerListener.Action.PHOTOS_SELECTED, mLastActionReco rded); | |
211 assertEquals(mTestFiles.get(0).getFilePath(), mLastSelectedPhotos[0]); | |
212 assertEquals(mTestFiles.get(2).getFilePath(), mLastSelectedPhotos[1]); | |
213 assertEquals(mTestFiles.get(4).getFilePath(), mLastSelectedPhotos[2]); | |
214 | |
215 mDialog.dismiss(); | |
216 } | |
217 } | |
OLD | NEW |