Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/SuggestionsSectionTest.java

Issue 2663313002: 📰 Merge the has_fetch_more and has_reload actions (Closed)
Patch Set: fix tests pt 2 Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.ntp.cards; 5 package org.chromium.chrome.browser.ntp.cards;
6 6
7 import static org.hamcrest.Matchers.is; 7 import static org.hamcrest.Matchers.is;
8 import static org.hamcrest.collection.IsIterableContainingInOrder.contains; 8 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
9 import static org.junit.Assert.assertEquals; 9 import static org.junit.Assert.assertEquals;
10 import static org.junit.Assert.assertFalse; 10 import static org.junit.Assert.assertFalse;
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 88
89 // Set empty variation params for the test. 89 // Set empty variation params for the test.
90 CardsVariationParameters.setTestVariationParams(new HashMap<String, Stri ng>()); 90 CardsVariationParameters.setTestVariationParams(new HashMap<String, Stri ng>());
91 } 91 }
92 92
93 @Test 93 @Test
94 @Feature({"Ntp"}) 94 @Feature({"Ntp"})
95 @EnableFeatures(ChromeFeatureList.NTP_SUGGESTIONS_SECTION_DISMISSAL) 95 @EnableFeatures(ChromeFeatureList.NTP_SUGGESTIONS_SECTION_DISMISSAL)
96 public void testDismissSibling() { 96 public void testDismissSibling() {
97 List<SnippetArticle> snippets = createDummySuggestions(3, TEST_CATEGORY_ ID); 97 List<SnippetArticle> snippets = createDummySuggestions(3, TEST_CATEGORY_ ID);
98 SuggestionsSection section = createSectionWithReloadAction(true); 98 SuggestionsSection section = createSectionWithFetchAction(true);
99 99
100 section.setStatus(CategoryStatus.AVAILABLE); 100 section.setStatus(CategoryStatus.AVAILABLE);
101 assertNotNull(section.getActionItemForTesting()); 101 assertNotNull(section.getActionItemForTesting());
102 102
103 // Without snippets. 103 // Without snippets.
104 assertEquals(ItemViewType.HEADER, section.getItemViewType(0)); 104 assertEquals(ItemViewType.HEADER, section.getItemViewType(0));
105 assertEquals(Collections.emptySet(), section.getItemDismissalGroup(0)); 105 assertEquals(Collections.emptySet(), section.getItemDismissalGroup(0));
106 assertEquals(ItemViewType.STATUS, section.getItemViewType(1)); 106 assertEquals(ItemViewType.STATUS, section.getItemViewType(1));
107 assertEquals(setOf(1, 2), section.getItemDismissalGroup(1)); 107 assertEquals(setOf(1, 2), section.getItemDismissalGroup(1));
108 assertEquals(ItemViewType.ACTION, section.getItemViewType(2)); 108 assertEquals(ItemViewType.ACTION, section.getItemViewType(2));
109 assertEquals(setOf(1, 2), section.getItemDismissalGroup(2)); 109 assertEquals(setOf(1, 2), section.getItemDismissalGroup(2));
110 110
111 // With snippets. 111 // With snippets.
112 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true); 112 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true);
113 assertEquals(ItemViewType.HEADER, section.getItemViewType(0)); 113 assertEquals(ItemViewType.HEADER, section.getItemViewType(0));
114 assertEquals(Collections.emptySet(), section.getItemDismissalGroup(0)); 114 assertEquals(Collections.emptySet(), section.getItemDismissalGroup(0));
115 assertEquals(ItemViewType.SNIPPET, section.getItemViewType(1)); 115 assertEquals(ItemViewType.SNIPPET, section.getItemViewType(1));
116 assertEquals(Collections.singleton(1), section.getItemDismissalGroup(1)) ; 116 assertEquals(Collections.singleton(1), section.getItemDismissalGroup(1)) ;
117 } 117 }
118 118
119 @Test 119 @Test
120 @Feature({"Ntp"}) 120 @Feature({"Ntp"})
121 @EnableFeatures({}) 121 @EnableFeatures({})
122 public void testDismissSiblingWithSectionDismissalDisabled() { 122 public void testDismissSiblingWithSectionDismissalDisabled() {
123 List<SnippetArticle> snippets = createDummySuggestions(3, TEST_CATEGORY_ ID); 123 List<SnippetArticle> snippets = createDummySuggestions(3, TEST_CATEGORY_ ID);
124 SuggestionsSection section = createSectionWithReloadAction(true); 124 SuggestionsSection section = createSectionWithFetchAction(true);
125 125
126 section.setStatus(CategoryStatus.AVAILABLE); 126 section.setStatus(CategoryStatus.AVAILABLE);
127 assertNotNull(section.getActionItemForTesting()); 127 assertNotNull(section.getActionItemForTesting());
128 128
129 // Without snippets. 129 // Without snippets.
130 assertEquals(ItemViewType.HEADER, section.getItemViewType(0)); 130 assertEquals(ItemViewType.HEADER, section.getItemViewType(0));
131 assertEquals(Collections.emptySet(), section.getItemDismissalGroup(0)); 131 assertEquals(Collections.emptySet(), section.getItemDismissalGroup(0));
132 assertEquals(ItemViewType.STATUS, section.getItemViewType(1)); 132 assertEquals(ItemViewType.STATUS, section.getItemViewType(1));
133 assertEquals(Collections.emptySet(), section.getItemDismissalGroup(1)); 133 assertEquals(Collections.emptySet(), section.getItemDismissalGroup(1));
134 assertEquals(ItemViewType.ACTION, section.getItemViewType(2)); 134 assertEquals(ItemViewType.ACTION, section.getItemViewType(2));
135 assertEquals(Collections.emptySet(), section.getItemDismissalGroup(2)); 135 assertEquals(Collections.emptySet(), section.getItemDismissalGroup(2));
136 136
137 // With snippets. 137 // With snippets.
138 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true); 138 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true);
139 assertEquals(ItemViewType.HEADER, section.getItemViewType(0)); 139 assertEquals(ItemViewType.HEADER, section.getItemViewType(0));
140 assertEquals(Collections.emptySet(), section.getItemDismissalGroup(0)); 140 assertEquals(Collections.emptySet(), section.getItemDismissalGroup(0));
141 assertEquals(ItemViewType.SNIPPET, section.getItemViewType(1)); 141 assertEquals(ItemViewType.SNIPPET, section.getItemViewType(1));
142 assertEquals(Collections.singleton(1), section.getItemDismissalGroup(1)) ; 142 assertEquals(Collections.singleton(1), section.getItemDismissalGroup(1)) ;
143 } 143 }
144 144
145 @Test 145 @Test
146 @Feature({"Ntp"}) 146 @Feature({"Ntp"})
147 public void testAddSuggestionsNotification() { 147 public void testAddSuggestionsNotification() {
148 final int suggestionCount = 5; 148 final int suggestionCount = 5;
149 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount, 149 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount,
150 TEST_CATEGORY_ID); 150 TEST_CATEGORY_ID);
151 151
152 SuggestionsSection section = createSectionWithReloadAction(false); 152 SuggestionsSection section = createSectionWithFetchAction(false);
153 // Simulate initialisation by the adapter. Here we don't care about the notifications, since 153 // Simulate initialisation by the adapter. Here we don't care about the notifications, since
154 // the RecyclerView will be updated through notifyDataSetChanged. 154 // the RecyclerView will be updated through notifyDataSetChanged.
155 section.setStatus(CategoryStatus.AVAILABLE); 155 section.setStatus(CategoryStatus.AVAILABLE);
156 reset(mParent); 156 reset(mParent);
157 157
158 assertEquals(2, section.getItemCount()); // When empty, we have the head er and status card. 158 assertEquals(2, section.getItemCount()); // When empty, we have the head er and status card.
159 assertEquals(ItemViewType.STATUS, section.getItemViewType(1)); 159 assertEquals(ItemViewType.STATUS, section.getItemViewType(1));
160 160
161 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true); 161 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true);
162 verify(mParent).onItemRangeInserted(section, 1, suggestionCount); 162 verify(mParent).onItemRangeInserted(section, 1, suggestionCount);
163 verify(mParent).onItemRangeRemoved(section, 1 + suggestionCount, 1); 163 verify(mParent).onItemRangeRemoved(section, 1 + suggestionCount, 1);
164 } 164 }
165 165
166 @Test 166 @Test
167 @Feature({"Ntp"}) 167 @Feature({"Ntp"})
168 public void testSetStatusNotification() { 168 public void testSetStatusNotification() {
169 final int suggestionCount = 5; 169 final int suggestionCount = 5;
170 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount, 170 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount,
171 TEST_CATEGORY_ID); 171 TEST_CATEGORY_ID);
172 SuggestionsSection section = createSectionWithReloadAction(false); 172 SuggestionsSection section = createSectionWithFetchAction(false);
173 173
174 // Simulate initialisation by the adapter. Here we don't care about the notifications, since 174 // Simulate initialisation by the adapter. Here we don't care about the notifications, since
175 // the RecyclerView will be updated through notifyDataSetChanged. 175 // the RecyclerView will be updated through notifyDataSetChanged.
176 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true); 176 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true);
177 reset(mParent); 177 reset(mParent);
178 178
179 // We don't clear suggestions when the status is AVAILABLE. 179 // We don't clear suggestions when the status is AVAILABLE.
180 section.setStatus(CategoryStatus.AVAILABLE); 180 section.setStatus(CategoryStatus.AVAILABLE);
181 verifyNoMoreInteractions(mParent); 181 verifyNoMoreInteractions(mParent);
182 182
183 // We clear existing suggestions when the status is not AVAILABLE, and s how the status card. 183 // We clear existing suggestions when the status is not AVAILABLE, and s how the status card.
184 section.setStatus(CategoryStatus.SIGNED_OUT); 184 section.setStatus(CategoryStatus.SIGNED_OUT);
185 verify(mParent).onItemRangeRemoved(section, 1, suggestionCount); 185 verify(mParent).onItemRangeRemoved(section, 1, suggestionCount);
186 verify(mParent).onItemRangeInserted(section, 1, 1); 186 verify(mParent).onItemRangeInserted(section, 1, 1);
187 187
188 // A loading state item triggers showing the loading item. 188 // A loading state item triggers showing the loading item.
189 section.setStatus(CategoryStatus.AVAILABLE_LOADING); 189 section.setStatus(CategoryStatus.AVAILABLE_LOADING);
190 verify(mParent).onItemRangeInserted(section, 2, 1); 190 verify(mParent).onItemRangeInserted(section, 2, 1);
191 191
192 section.setStatus(CategoryStatus.AVAILABLE); 192 section.setStatus(CategoryStatus.AVAILABLE);
193 verify(mParent).onItemRangeRemoved(section, 2, 1); 193 verify(mParent).onItemRangeRemoved(section, 2, 1);
194 } 194 }
195 195
196 @Test 196 @Test
197 @Feature({"Ntp"}) 197 @Feature({"Ntp"})
198 public void testRemoveUnknownSuggestion() { 198 public void testRemoveUnknownSuggestion() {
199 SuggestionsSection section = createSectionWithReloadAction(false); 199 SuggestionsSection section = createSectionWithFetchAction(false);
200 section.setStatus(CategoryStatus.AVAILABLE); 200 section.setStatus(CategoryStatus.AVAILABLE);
201 section.removeSuggestionById("foobar"); 201 section.removeSuggestionById("foobar");
202 } 202 }
203 203
204 @Test 204 @Test
205 @Feature({"Ntp"}) 205 @Feature({"Ntp"})
206 public void testRemoveSuggestionNotification() { 206 public void testRemoveSuggestionNotification() {
207 final int suggestionCount = 2; 207 final int suggestionCount = 2;
208 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount, 208 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount,
209 TEST_CATEGORY_ID); 209 TEST_CATEGORY_ID);
210 210
211 SuggestionsSection section = createSectionWithReloadAction(false); 211 SuggestionsSection section = createSectionWithFetchAction(false);
212 section.setStatus(CategoryStatus.AVAILABLE); 212 section.setStatus(CategoryStatus.AVAILABLE);
213 reset(mParent); 213 reset(mParent);
214 214
215 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true); 215 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true);
216 216
217 section.removeSuggestionById(snippets.get(1).mIdWithinCategory); 217 section.removeSuggestionById(snippets.get(1).mIdWithinCategory);
218 verify(mParent).onItemRangeRemoved(section, 2, 1); 218 verify(mParent).onItemRangeRemoved(section, 2, 1);
219 219
220 section.removeSuggestionById(snippets.get(0).mIdWithinCategory); 220 section.removeSuggestionById(snippets.get(0).mIdWithinCategory);
221 verify(mParent).onItemRangeRemoved(section, 1, 1); 221 verify(mParent).onItemRangeRemoved(section, 1, 1);
222 verify(mParent).onItemRangeInserted(section, 1, 1); 222 verify(mParent).onItemRangeInserted(section, 1, 1);
223 223
224 assertEquals(2, section.getItemCount()); 224 assertEquals(2, section.getItemCount());
225 assertEquals(ItemViewType.STATUS, section.getItemViewType(1)); 225 assertEquals(ItemViewType.STATUS, section.getItemViewType(1));
226 } 226 }
227 227
228 @Test 228 @Test
229 @Feature({"Ntp"}) 229 @Feature({"Ntp"})
230 public void testRemoveSuggestionNotificationWithButton() { 230 public void testRemoveSuggestionNotificationWithButton() {
231 final int suggestionCount = 2; 231 final int suggestionCount = 2;
232 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount, 232 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount,
233 TEST_CATEGORY_ID); 233 TEST_CATEGORY_ID);
234 234
235 SuggestionsCategoryInfo info = 235 SuggestionsCategoryInfo info =
236 new CategoryInfoBuilder(TEST_CATEGORY_ID) 236 new CategoryInfoBuilder(TEST_CATEGORY_ID).withFetchAction().show IfEmpty().build();
237 .withMoreAction()
238 .withReloadAction()
239 .showIfEmpty()
240 .build();
241 SuggestionsSection section = createSection(info); 237 SuggestionsSection section = createSection(info);
242 section.setStatus(CategoryStatus.AVAILABLE); 238 section.setStatus(CategoryStatus.AVAILABLE);
243 reset(mParent); 239 reset(mParent);
244 assertEquals(3, section.getItemCount()); // We have the header and statu s card and a button. 240 assertEquals(3, section.getItemCount()); // We have the header and statu s card and a button.
245 241
246 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true); 242 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true);
247 assertEquals(4, section.getItemCount()); 243 assertEquals(4, section.getItemCount());
248 244
249 section.removeSuggestionById(snippets.get(0).mIdWithinCategory); 245 section.removeSuggestionById(snippets.get(0).mIdWithinCategory);
250 verify(mParent).onItemRangeRemoved(section, 1, 1); 246 verify(mParent).onItemRangeRemoved(section, 1, 1);
251 247
252 section.removeSuggestionById(snippets.get(1).mIdWithinCategory); 248 section.removeSuggestionById(snippets.get(1).mIdWithinCategory);
253 verify(mParent, times(2)).onItemRangeRemoved(section, 1, 1); 249 verify(mParent, times(2)).onItemRangeRemoved(section, 1, 1);
254 verify(mParent).onItemRangeInserted(section, 1, 1); // Only the status c ard is added. 250 verify(mParent).onItemRangeInserted(section, 1, 1); // Only the status c ard is added.
255 assertEquals(3, section.getItemCount()); 251 assertEquals(3, section.getItemCount());
256 assertEquals(ItemViewType.STATUS, section.getItemViewType(1)); 252 assertEquals(ItemViewType.STATUS, section.getItemViewType(1));
257 assertEquals(ItemViewType.ACTION, section.getItemViewType(2)); 253 assertEquals(ItemViewType.ACTION, section.getItemViewType(2));
258 } 254 }
259 255
260 @Test 256 @Test
261 @Feature({"Ntp"}) 257 @Feature({"Ntp"})
262 @EnableFeatures({ChromeFeatureList.NTP_SUGGESTIONS_SECTION_DISMISSAL}) 258 @EnableFeatures({ChromeFeatureList.NTP_SUGGESTIONS_SECTION_DISMISSAL})
263 public void testDismissSection() { 259 public void testDismissSection() {
264 SuggestionsSection section = createSectionWithReloadAction(false); 260 SuggestionsSection section = createSectionWithFetchAction(false);
265 section.setStatus(CategoryStatus.AVAILABLE); 261 section.setStatus(CategoryStatus.AVAILABLE);
266 reset(mParent); 262 reset(mParent);
267 assertEquals(2, section.getItemCount()); 263 assertEquals(2, section.getItemCount());
268 264
269 @SuppressWarnings("unchecked") 265 @SuppressWarnings("unchecked")
270 Callback<String> callback = mock(Callback.class); 266 Callback<String> callback = mock(Callback.class);
271 section.dismissItem(1, callback); 267 section.dismissItem(1, callback);
272 verify(mDelegate).dismissSection(section); 268 verify(mDelegate).dismissSection(section);
273 verify(callback).onResult(section.getHeaderText()); 269 verify(callback).onResult(section.getHeaderText());
274 } 270 }
275 271
276 @Test 272 @Test
277 @Feature({"Ntp"}) 273 @Feature({"Ntp"})
278 public void testOfflineStatus() { 274 public void testOfflineStatus() {
279 final int suggestionCount = 3; 275 final int suggestionCount = 3;
280 final List<SnippetArticle> snippets = createDummySuggestions(suggestionC ount, 276 final List<SnippetArticle> snippets = createDummySuggestions(suggestionC ount,
281 TEST_CATEGORY_ID); 277 TEST_CATEGORY_ID);
282 assertNull(snippets.get(0).getOfflinePageOfflineId()); 278 assertNull(snippets.get(0).getOfflinePageOfflineId());
283 assertNull(snippets.get(1).getOfflinePageOfflineId()); 279 assertNull(snippets.get(1).getOfflinePageOfflineId());
284 assertNull(snippets.get(2).getOfflinePageOfflineId()); 280 assertNull(snippets.get(2).getOfflinePageOfflineId());
285 281
286 final OfflinePageItem item0 = createOfflinePageItem(snippets.get(0).mUrl , 0L); 282 final OfflinePageItem item0 = createOfflinePageItem(snippets.get(0).mUrl , 0L);
287 final OfflinePageItem item1 = createOfflinePageItem(snippets.get(1).mUrl , 1L); 283 final OfflinePageItem item1 = createOfflinePageItem(snippets.get(1).mUrl , 1L);
288 284
289 mBridge.setIsOfflinePageModelLoaded(true); 285 mBridge.setIsOfflinePageModelLoaded(true);
290 mBridge.setItems(Arrays.asList(item0, item1)); 286 mBridge.setItems(Arrays.asList(item0, item1));
291 287
292 SuggestionsSection section = createSectionWithReloadAction(true); 288 SuggestionsSection section = createSectionWithFetchAction(true);
293 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true); 289 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true);
294 290
295 // Check that we pick up the correct information. 291 // Check that we pick up the correct information.
296 assertEquals(Long.valueOf(0L), snippets.get(0).getOfflinePageOfflineId() ); 292 assertEquals(Long.valueOf(0L), snippets.get(0).getOfflinePageOfflineId() );
297 assertEquals(Long.valueOf(1L), snippets.get(1).getOfflinePageOfflineId() ); 293 assertEquals(Long.valueOf(1L), snippets.get(1).getOfflinePageOfflineId() );
298 assertNull(snippets.get(2).getOfflinePageOfflineId()); 294 assertNull(snippets.get(2).getOfflinePageOfflineId());
299 295
300 final OfflinePageItem item2 = createOfflinePageItem(snippets.get(2).mUrl , 2L); 296 final OfflinePageItem item2 = createOfflinePageItem(snippets.get(2).mUrl , 2L);
301 297
302 mBridge.setItems(Arrays.asList(item1, item2)); 298 mBridge.setItems(Arrays.asList(item1, item2));
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 assertEquals(Long.valueOf(0L), suggestions.get(0).getOfflinePageOfflineI d()); 334 assertEquals(Long.valueOf(0L), suggestions.get(0).getOfflinePageOfflineI d());
339 assertNull(suggestions.get(1).getOfflinePageOfflineId()); 335 assertNull(suggestions.get(1).getOfflinePageOfflineId());
340 } 336 }
341 337
342 @Test 338 @Test
343 @Feature({"Ntp"}) 339 @Feature({"Ntp"})
344 public void testViewAllActionPriority() { 340 public void testViewAllActionPriority() {
345 // When all the actions are enabled, ViewAll always has the priority and is shown. 341 // When all the actions are enabled, ViewAll always has the priority and is shown.
346 342
347 // Spy so that VerifyAction can check methods being called. 343 // Spy so that VerifyAction can check methods being called.
348 SuggestionsCategoryInfo info = 344 SuggestionsCategoryInfo info = spy(new CategoryInfoBuilder(TEST_CATEGORY _ID)
349 spy(new CategoryInfoBuilder(TEST_CATEGORY_ID) 345 .withFetchAction()
350 .withMoreAction() 346 .withViewAllAction()
351 .withReloadAction() 347 .showIfEmpty()
352 .withViewAllAction() 348 .build());
353 .showIfEmpty()
354 .build());
355 SuggestionsSection section = createSection(info); 349 SuggestionsSection section = createSection(info);
356 350
357 assertTrue(section.getActionItemForTesting().isVisible()); 351 assertTrue(section.getActionItemForTesting().isVisible());
358 verifyAction(section, ActionItem.ACTION_VIEW_ALL); 352 verifyAction(section, ActionItem.ACTION_VIEW_ALL);
359 353
360 section.setSuggestions(createDummySuggestions(3, TEST_CATEGORY_ID), 354 section.setSuggestions(createDummySuggestions(3, TEST_CATEGORY_ID),
361 CategoryStatus.AVAILABLE, /* replaceExisting = */ true); 355 CategoryStatus.AVAILABLE, /* replaceExisting = */ true);
362 356
363 assertTrue(section.getActionItemForTesting().isVisible()); 357 assertTrue(section.getActionItemForTesting().isVisible());
364 verifyAction(section, ActionItem.ACTION_VIEW_ALL); 358 verifyAction(section, ActionItem.ACTION_VIEW_ALL);
365 } 359 }
366 360
367 @Test 361 @Test
368 @Feature({"Ntp"}) 362 @Feature({"Ntp"})
369 public void testReloadAndFetchMoreActionPriority() { 363 public void testFetchActionPriority() {
dgn 2017/02/01 13:27:58 Not sure if I should remove the suggestion count d
Bernhard Bauer 2017/02/01 14:11:06 Yeah, I think that would make sense… It is intenti
dgn 2017/02/01 18:01:52 Done.
370 // When both Reload and FetchMore are enabled, FetchMore runs when we ha ve suggestions, and 364 // When only FetchMore is enabled, it shows whether we have suggestions or not.
371 // Reload when we don't.
372 365
373 // Spy so that VerifyAction can check methods being called. 366 // Spy so that VerifyAction can check methods being called.
374 SuggestionsCategoryInfo info = 367 SuggestionsCategoryInfo info = spy(
375 spy(new CategoryInfoBuilder(TEST_CATEGORY_ID) 368 new CategoryInfoBuilder(TEST_CATEGORY_ID).withFetchAction().show IfEmpty().build());
376 .withMoreAction()
377 .withReloadAction()
378 .showIfEmpty()
379 .build());
380 SuggestionsSection section = createSection(info); 369 SuggestionsSection section = createSection(info);
381 370
382 assertTrue(section.getActionItemForTesting().isVisible()); 371 assertTrue(section.getActionItemForTesting().isVisible());
383 verifyAction(section, ActionItem.ACTION_RELOAD); 372 verifyAction(section, ActionItem.ACTION_FETCH);
384 373
385 section.setSuggestions(createDummySuggestions(3, TEST_CATEGORY_ID), 374 section.setSuggestions(createDummySuggestions(3, TEST_CATEGORY_ID),
386 CategoryStatus.AVAILABLE, /* replaceExisting = */ true); 375 CategoryStatus.AVAILABLE, /* replaceExisting = */ true);
387 376
388 assertTrue(section.getActionItemForTesting().isVisible()); 377 assertTrue(section.getActionItemForTesting().isVisible());
389 verifyAction(section, ActionItem.ACTION_FETCH_MORE); 378 verifyAction(section, ActionItem.ACTION_FETCH);
390 } 379 }
391 380
392 @Test 381 @Test
393 @Feature({"Ntp"})
394 public void testReloadActionPriority() {
395 // When only Reload is enabled, it only shows when we have no suggestion s.
396
397 // Spy so that VerifyAction can check methods being called.
398 SuggestionsCategoryInfo info = spy(
399 new CategoryInfoBuilder(TEST_CATEGORY_ID).withReloadAction().sho wIfEmpty().build());
400 SuggestionsSection section = createSection(info);
401
402 assertTrue(section.getActionItemForTesting().isVisible());
403 verifyAction(section, ActionItem.ACTION_RELOAD);
404
405 section.setSuggestions(createDummySuggestions(3, TEST_CATEGORY_ID),
406 CategoryStatus.AVAILABLE, /* replaceExisting = */ true);
407
408 assertFalse(section.getActionItemForTesting().isVisible());
409 verifyAction(section, ActionItem.ACTION_NONE);
410 }
411
412 @Test
413 @Feature({"Ntp"})
414 public void testFetchMoreActionPriority() {
415 // When only FetchMore is enabled, it only shows when we have suggestion s.
416
417 // Spy so that VerifyAction can check methods being called.
418 SuggestionsCategoryInfo info = spy(
419 new CategoryInfoBuilder(TEST_CATEGORY_ID).withMoreAction().showI fEmpty().build());
420 SuggestionsSection section = createSection(info);
421
422 assertFalse(section.getActionItemForTesting().isVisible());
423 verifyAction(section, ActionItem.ACTION_NONE);
424
425 section.setSuggestions(createDummySuggestions(3, TEST_CATEGORY_ID),
426 CategoryStatus.AVAILABLE, /* replaceExisting = */ true);
427
428 assertTrue(section.getActionItemForTesting().isVisible());
429 verifyAction(section, ActionItem.ACTION_FETCH_MORE);
430 }
431
432 @Test
433 @Feature({"Ntp"}) 382 @Feature({"Ntp"})
434 public void testNoAction() { 383 public void testNoAction() {
435 // Test where no action is enabled. 384 // Test where no action is enabled.
436 385
437 // Spy so that VerifyAction can check methods being called. 386 // Spy so that VerifyAction can check methods being called.
438 SuggestionsCategoryInfo info = 387 SuggestionsCategoryInfo info =
439 spy(new CategoryInfoBuilder(TEST_CATEGORY_ID).showIfEmpty().buil d()); 388 spy(new CategoryInfoBuilder(TEST_CATEGORY_ID).showIfEmpty().buil d());
440 SuggestionsSection section = createSection(info); 389 SuggestionsSection section = createSection(info);
441 390
442 assertFalse(section.getActionItemForTesting().isVisible()); 391 assertFalse(section.getActionItemForTesting().isVisible());
443 verifyAction(section, ActionItem.ACTION_NONE); 392 verifyAction(section, ActionItem.ACTION_NONE);
444 393
445 section.setSuggestions(createDummySuggestions(3, TEST_CATEGORY_ID), 394 section.setSuggestions(createDummySuggestions(3, TEST_CATEGORY_ID),
446 CategoryStatus.AVAILABLE, /* replaceExisting = */ true); 395 CategoryStatus.AVAILABLE, /* replaceExisting = */ true);
447 396
448 assertFalse(section.getActionItemForTesting().isVisible()); 397 assertFalse(section.getActionItemForTesting().isVisible());
449 verifyAction(section, ActionItem.ACTION_NONE); 398 verifyAction(section, ActionItem.ACTION_NONE);
450 } 399 }
451 400
452 @Test 401 @Test
453 @Feature({"Ntp"}) 402 @Feature({"Ntp"})
454 public void testFetchMoreProgressDisplay() { 403 public void testFetchMoreProgressDisplay() {
455 final int suggestionCount = 3; 404 final int suggestionCount = 3;
456 SuggestionsCategoryInfo info = spy( 405 SuggestionsCategoryInfo info = spy(
457 new CategoryInfoBuilder(TEST_CATEGORY_ID).withMoreAction().showI fEmpty().build()); 406 new CategoryInfoBuilder(TEST_CATEGORY_ID).withFetchAction().show IfEmpty().build());
458 SuggestionsSection section = createSection(info); 407 SuggestionsSection section = createSection(info);
459 section.setSuggestions(createDummySuggestions(suggestionCount, TEST_CATE GORY_ID), 408 section.setSuggestions(createDummySuggestions(suggestionCount, TEST_CATE GORY_ID),
460 CategoryStatus.AVAILABLE, /* replaceExisting = */ true); 409 CategoryStatus.AVAILABLE, /* replaceExisting = */ true);
461 assertFalse(section.getProgressItemForTesting().isVisible()); 410 assertFalse(section.getProgressItemForTesting().isVisible());
462 411
463 // Tap the button 412 // Tap the button
464 verifyAction(section, ActionItem.ACTION_FETCH_MORE); 413 verifyAction(section, ActionItem.ACTION_FETCH);
465 assertTrue(section.getProgressItemForTesting().isVisible()); 414 assertTrue(section.getProgressItemForTesting().isVisible());
466 415
467 // Simulate receiving suggestions. 416 // Simulate receiving suggestions.
468 section.setSuggestions(createDummySuggestions(suggestionCount, TEST_CATE GORY_ID), 417 section.setSuggestions(createDummySuggestions(suggestionCount, TEST_CATE GORY_ID),
469 CategoryStatus.AVAILABLE, /* replaceExisting = */ false); 418 CategoryStatus.AVAILABLE, /* replaceExisting = */ false);
470 assertFalse(section.getProgressItemForTesting().isVisible()); 419 assertFalse(section.getProgressItemForTesting().isVisible());
471 } 420 }
472 421
473 /** 422 /**
474 * Tests that the UI updates on updated suggestions. 423 * Tests that the UI updates on updated suggestions.
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
680 /* replaceExisting = */ true); 629 /* replaceExisting = */ true);
681 630
682 // All previous snippets should be in place. 631 // All previous snippets should be in place.
683 verifySnippets(section, snippets); 632 verifySnippets(section, snippets);
684 } 633 }
685 634
686 @Test 635 @Test
687 @Feature({"Ntp"}) 636 @Feature({"Ntp"})
688 public void testCardIsNotifiedWhenBecomingFirst() { 637 public void testCardIsNotifiedWhenBecomingFirst() {
689 List<SnippetArticle> suggestions = createDummySuggestions(5, /* category Id = */ 42); 638 List<SnippetArticle> suggestions = createDummySuggestions(5, /* category Id = */ 42);
690 SuggestionsSection section = createSectionWithReloadAction(false); 639 SuggestionsSection section = createSectionWithFetchAction(false);
691 section.setSuggestions(suggestions, CategoryStatus.AVAILABLE, /* replace Existing = */ true); 640 section.setSuggestions(suggestions, CategoryStatus.AVAILABLE, /* replace Existing = */ true);
692 reset(mParent); 641 reset(mParent);
693 642
694 // Remove the first card. The second one should get the update. 643 // Remove the first card. The second one should get the update.
695 section.removeSuggestionById(suggestions.get(0).mIdWithinCategory); 644 section.removeSuggestionById(suggestions.get(0).mIdWithinCategory);
696 verify(mParent).onItemRangeChanged(section, 1, 1, PartialUpdateId.CARD_B ACKGROUND); 645 verify(mParent).onItemRangeChanged(section, 1, 1, PartialUpdateId.CARD_B ACKGROUND);
697 } 646 }
698 647
699 @Test 648 @Test
700 @Feature({"Ntp"}) 649 @Feature({"Ntp"})
701 public void testCardIsNotifiedWhenBecomingLast() { 650 public void testCardIsNotifiedWhenBecomingLast() {
702 List<SnippetArticle> suggestions = createDummySuggestions(5, /* category Id = */ 42); 651 List<SnippetArticle> suggestions = createDummySuggestions(5, /* category Id = */ 42);
703 SuggestionsSection section = createSectionWithReloadAction(false); 652 SuggestionsSection section = createSectionWithFetchAction(false);
704 section.setSuggestions(suggestions, CategoryStatus.AVAILABLE, /* replace Existing = */ true); 653 section.setSuggestions(suggestions, CategoryStatus.AVAILABLE, /* replace Existing = */ true);
705 reset(mParent); 654 reset(mParent);
706 655
707 // Remove the last card. The penultimate one should get the update. 656 // Remove the last card. The penultimate one should get the update.
708 section.removeSuggestionById(suggestions.get(4).mIdWithinCategory); 657 section.removeSuggestionById(suggestions.get(4).mIdWithinCategory);
709 verify(mParent).onItemRangeChanged(section, 4, 1, PartialUpdateId.CARD_B ACKGROUND); 658 verify(mParent).onItemRangeChanged(section, 4, 1, PartialUpdateId.CARD_B ACKGROUND);
710 } 659 }
711 660
712 @Test 661 @Test
713 @Feature({"Ntp"}) 662 @Feature({"Ntp"})
714 public void testCardIsNotifiedWhenBecomingSoleCard() { 663 public void testCardIsNotifiedWhenBecomingSoleCard() {
715 List<SnippetArticle> suggestions = createDummySuggestions(2, /* category Id = */ 42); 664 List<SnippetArticle> suggestions = createDummySuggestions(2, /* category Id = */ 42);
716 SuggestionsSection section = createSectionWithReloadAction(false); 665 SuggestionsSection section = createSectionWithFetchAction(false);
717 section.setSuggestions(suggestions, CategoryStatus.AVAILABLE, /* replace Existing = */ true); 666 section.setSuggestions(suggestions, CategoryStatus.AVAILABLE, /* replace Existing = */ true);
718 reset(mParent); 667 reset(mParent);
719 668
720 // Remove the last card. The penultimate one should get the update. 669 // Remove the last card. The penultimate one should get the update.
721 section.removeSuggestionById(suggestions.get(1).mIdWithinCategory); 670 section.removeSuggestionById(suggestions.get(1).mIdWithinCategory);
722 verify(mParent).onItemRangeChanged(section, 1, 1, PartialUpdateId.CARD_B ACKGROUND); 671 verify(mParent).onItemRangeChanged(section, 1, 1, PartialUpdateId.CARD_B ACKGROUND);
723 } 672 }
724 673
725 @Test 674 @Test
726 @Feature({"Ntp"}) 675 @Feature({"Ntp"})
727 @EnableFeatures(ChromeFeatureList.NTP_SUGGESTIONS_SECTION_DISMISSAL) 676 @EnableFeatures(ChromeFeatureList.NTP_SUGGESTIONS_SECTION_DISMISSAL)
728 public void testGetItemDismissalGroupWithSuggestions() { 677 public void testGetItemDismissalGroupWithSuggestions() {
729 List<SnippetArticle> suggestions = createDummySuggestions(5, TEST_CATEGO RY_ID); 678 List<SnippetArticle> suggestions = createDummySuggestions(5, TEST_CATEGO RY_ID);
730 SuggestionsSection section = createSectionWithReloadAction(false); 679 SuggestionsSection section = createSectionWithFetchAction(false);
731 section.setSuggestions(suggestions, CategoryStatus.AVAILABLE, /* replace Existing = */ true); 680 section.setSuggestions(suggestions, CategoryStatus.AVAILABLE, /* replace Existing = */ true);
732 681
733 assertThat(section.getItemDismissalGroup(1).size(), is(1)); 682 assertThat(section.getItemDismissalGroup(1).size(), is(1));
734 assertThat(section.getItemDismissalGroup(1), contains(1)); 683 assertThat(section.getItemDismissalGroup(1), contains(1));
735 } 684 }
736 685
737 @Test 686 @Test
738 @Feature({"Ntp"}) 687 @Feature({"Ntp"})
739 @EnableFeatures(ChromeFeatureList.NTP_SUGGESTIONS_SECTION_DISMISSAL) 688 @EnableFeatures(ChromeFeatureList.NTP_SUGGESTIONS_SECTION_DISMISSAL)
740 public void testGetItemDismissalGroupWithActionItem() { 689 public void testGetItemDismissalGroupWithActionItem() {
741 SuggestionsSection section = createSectionWithReloadAction(true); 690 SuggestionsSection section = createSectionWithFetchAction(true);
742 assertThat(section.getItemDismissalGroup(1).size(), is(2)); 691 assertThat(section.getItemDismissalGroup(1).size(), is(2));
743 assertThat(section.getItemDismissalGroup(1), contains(1, 2)); 692 assertThat(section.getItemDismissalGroup(1), contains(1, 2));
744 } 693 }
745 694
746 @Test 695 @Test
747 @Feature({"Ntp"}) 696 @Feature({"Ntp"})
748 @EnableFeatures(ChromeFeatureList.NTP_SUGGESTIONS_SECTION_DISMISSAL) 697 @EnableFeatures(ChromeFeatureList.NTP_SUGGESTIONS_SECTION_DISMISSAL)
749 public void testGetItemDismissalGroupWithoutActionItem() { 698 public void testGetItemDismissalGroupWithoutActionItem() {
750 SuggestionsSection section = createSectionWithReloadAction(false); 699 SuggestionsSection section = createSectionWithFetchAction(false);
751 assertThat(section.getItemDismissalGroup(1).size(), is(1)); 700 assertThat(section.getItemDismissalGroup(1).size(), is(1));
752 assertThat(section.getItemDismissalGroup(1), contains(1)); 701 assertThat(section.getItemDismissalGroup(1), contains(1));
753 } 702 }
754 703
755 @Test 704 @Test
756 @Feature({"Ntp"}) 705 @Feature({"Ntp"})
757 public void testCardIsNotifiedWhenNotTheLastAnymore() { 706 public void testCardIsNotifiedWhenNotTheLastAnymore() {
758 List<SnippetArticle> suggestions = createDummySuggestions(5, /* category Id = */ 42); 707 List<SnippetArticle> suggestions = createDummySuggestions(5, /* category Id = */ 42);
759 SuggestionsSection section = createSectionWithReloadAction(false); 708 SuggestionsSection section = createSectionWithFetchAction(false);
760 709
761 section.setSuggestions(suggestions, CategoryStatus.AVAILABLE, /* replace Existing = */ true); 710 section.setSuggestions(suggestions, CategoryStatus.AVAILABLE, /* replace Existing = */ true);
762 reset(mParent); 711 reset(mParent);
763 712
764 section.setSuggestions(createDummySuggestions(2, /* categoryId = */ 42, "new"), 713 section.setSuggestions(createDummySuggestions(2, /* categoryId = */ 42, "new"),
765 CategoryStatus.AVAILABLE, /* replaceExisting = */ false); 714 CategoryStatus.AVAILABLE, /* replaceExisting = */ false);
766 verify(mParent).onItemRangeChanged(section, 5, 1, PartialUpdateId.CARD_B ACKGROUND); 715 verify(mParent).onItemRangeChanged(section, 5, 1, PartialUpdateId.CARD_B ACKGROUND);
767 } 716 }
768 717
769 private SuggestionsSection createSectionWithSuggestions(List<SnippetArticle> snippets) { 718 private SuggestionsSection createSectionWithSuggestions(List<SnippetArticle> snippets) {
770 SuggestionsSection section = createSectionWithReloadAction(true); 719 SuggestionsSection section = createSectionWithFetchAction(true);
771 section.setStatus(CategoryStatus.AVAILABLE); 720 section.setStatus(CategoryStatus.AVAILABLE);
772 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true); 721 section.setSuggestions(snippets, CategoryStatus.AVAILABLE, /* replaceExi sting = */ true);
773 722
774 // Reset any notification invocations on the parent from setting the ini tial list 723 // Reset any notification invocations on the parent from setting the ini tial list
775 // of suggestions. 724 // of suggestions.
776 reset(mParent); 725 reset(mParent);
777 return section; 726 return section;
778 } 727 }
779 728
780 @SafeVarargs 729 @SafeVarargs
781 private static <T> Set<T> setOf(T... elements) { 730 private static <T> Set<T> setOf(T... elements) {
782 Set<T> set = new TreeSet<T>(); 731 Set<T> set = new TreeSet<T>();
783 set.addAll(Arrays.asList(elements)); 732 set.addAll(Arrays.asList(elements));
784 return set; 733 return set;
785 } 734 }
786 735
787 private SuggestionsSection createSectionWithReloadAction(boolean hasReloadAc tion) { 736 private SuggestionsSection createSectionWithFetchAction(boolean hasReloadAct ion) {
788 CategoryInfoBuilder builder = new CategoryInfoBuilder(TEST_CATEGORY_ID). showIfEmpty(); 737 CategoryInfoBuilder builder = new CategoryInfoBuilder(TEST_CATEGORY_ID). showIfEmpty();
789 if (hasReloadAction) builder.withReloadAction(); 738 if (hasReloadAction) builder.withFetchAction();
790 return createSection(builder.build()); 739 return createSection(builder.build());
791 } 740 }
792 741
793 private SuggestionsSection createSection(SuggestionsCategoryInfo info) { 742 private SuggestionsSection createSection(SuggestionsCategoryInfo info) {
794 SuggestionsSection section = new SuggestionsSection( 743 SuggestionsSection section = new SuggestionsSection(
795 mDelegate, mUiDelegate, mock(SuggestionsRanker.class), mBridge, info); 744 mDelegate, mUiDelegate, mock(SuggestionsRanker.class), mBridge, info);
796 section.setParent(mParent); 745 section.setParent(mParent);
797 return section; 746 return section;
798 } 747 }
799 748
800 private OfflinePageItem createOfflinePageItem(String url, long offlineId) { 749 private OfflinePageItem createOfflinePageItem(String url, long offlineId) {
801 return new OfflinePageItem(url, offlineId, "", "", "", 0, 0, 0, 0); 750 return new OfflinePageItem(url, offlineId, "", "", "", 0, 0, 0, 0);
802 } 751 }
803 752
804 private static void verifyAction(SuggestionsSection section, @ActionItem.Act ion int action) { 753 private static void verifyAction(SuggestionsSection section, @ActionItem.Act ion int action) {
805 SuggestionsSource suggestionsSource = mock(SuggestionsSource.class); 754 SuggestionsSource suggestionsSource = mock(SuggestionsSource.class);
806 SuggestionsUiDelegate manager = mock(SuggestionsUiDelegate.class); 755 SuggestionsUiDelegate manager = mock(SuggestionsUiDelegate.class);
807 SuggestionsNavigationDelegate navDelegate = mock(SuggestionsNavigationDe legate.class); 756 SuggestionsNavigationDelegate navDelegate = mock(SuggestionsNavigationDe legate.class);
808 when(manager.getSuggestionsSource()).thenReturn(suggestionsSource); 757 when(manager.getSuggestionsSource()).thenReturn(suggestionsSource);
809 when(manager.getNavigationDelegate()).thenReturn(navDelegate); 758 when(manager.getNavigationDelegate()).thenReturn(navDelegate);
810 when(manager.getMetricsReporter()).thenReturn(mock(SuggestionsMetricsRep orter.class)); 759 when(manager.getMetricsReporter()).thenReturn(mock(SuggestionsMetricsRep orter.class));
811 760
812 if (action != ActionItem.ACTION_NONE) { 761 if (action != ActionItem.ACTION_NONE) {
813 section.getActionItemForTesting().performAction(manager); 762 section.getActionItemForTesting().performAction(manager);
814 } 763 }
815 764
816 verify(section.getCategoryInfo(), 765 verify(section.getCategoryInfo(),
817 (action == ActionItem.ACTION_VIEW_ALL ? times(1) : never())) 766 (action == ActionItem.ACTION_VIEW_ALL ? times(1) : never()))
818 .performViewAllAction(navDelegate); 767 .performViewAllAction(navDelegate);
819 verify(suggestionsSource, 768 verify(suggestionsSource, (action == ActionItem.ACTION_FETCH ? times(1) : never()))
820 action == ActionItem.ACTION_RELOAD || action == ActionItem.ACTIO N_FETCH_MORE
821 ? times(1)
822 : never())
823 .fetchSuggestions(anyInt(), any(String[].class)); 769 .fetchSuggestions(anyInt(), any(String[].class));
824 } 770 }
825 771
826 private static void verifySnippets(SuggestionsSection section, List<SnippetA rticle> snippets) { 772 private static void verifySnippets(SuggestionsSection section, List<SnippetA rticle> snippets) {
827 assertEquals(snippets.size(), section.getSuggestionsCount()); 773 assertEquals(snippets.size(), section.getSuggestionsCount());
828 // Indices in section are off-by-one (index 0 is the header). 774 // Indices in section are off-by-one (index 0 is the header).
829 int index = 1; 775 int index = 1;
830 for (SnippetArticle snippet : snippets) { 776 for (SnippetArticle snippet : snippets) {
831 assertEquals(snippet, section.getSuggestionAt(index++)); 777 assertEquals(snippet, section.getSuggestionAt(index++));
832 } 778 }
833 } 779 }
834 } 780 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698