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

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerViewTest.java

Issue 2860463002: [Suggestions] Remove TreeNode.getSuggestionAt() in favor of a visitor. (Closed)
Patch Set: fix test Created 3 years, 7 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 android.content.res.Resources; 7 import android.content.res.Resources;
8 import android.support.test.InstrumentationRegistry; 8 import android.support.test.InstrumentationRegistry;
9 import android.support.test.filters.MediumTest; 9 import android.support.test.filters.MediumTest;
10 import android.support.v7.widget.RecyclerView; 10 import android.support.v7.widget.RecyclerView;
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 } 120 }
121 121
122 @After 122 @After
123 public void tearDown() throws Exception { 123 public void tearDown() throws Exception {
124 TileGroupDelegateImpl.setMostVisitedSitesForTests(null); 124 TileGroupDelegateImpl.setMostVisitedSitesForTests(null);
125 NewTabPage.setSuggestionsSourceForTests(null); 125 NewTabPage.setSuggestionsSourceForTests(null);
126 mTestServer.stopAndDestroyServer(); 126 mTestServer.stopAndDestroyServer();
127 127
128 } 128 }
129 129
130 private int getLastCardPosition() {
dgn 2017/05/24 11:30:31 nit: move at the end with the other utility functi
Bernhard Bauer 2017/05/25 12:27:54 Done.
131 int count = getAdapter().getItemCount();
132 for (int i = count - 1; i >= 0; i--) {
133 if (getAdapter().getItemViewType(i) == ItemViewType.SNIPPET) return i;
134 }
135 return RecyclerView.NO_POSITION;
136 }
137
130 @Test 138 @Test
131 @MediumTest 139 @MediumTest
132 @Feature({"NewTabPage"}) 140 @Feature({"NewTabPage"})
133 public void testClickSuggestion() throws InterruptedException { 141 public void testClickSuggestion() throws InterruptedException {
134 setSuggestionsAndWaitForUpdate(10); 142 setSuggestionsAndWaitForUpdate(10);
135 List<SnippetArticle> suggestions = mSource.getSuggestionsForCategory(TES T_CATEGORY); 143 List<SnippetArticle> suggestions = mSource.getSuggestionsForCategory(TES T_CATEGORY);
136 144
137 // Scroll the last suggestion into view and click it. 145 // Scroll the last suggestion into view and click it.
138 SnippetArticle suggestion = suggestions.get(suggestions.size() - 1); 146 SnippetArticle suggestion = suggestions.get(suggestions.size() - 1);
139 int suggestionPosition = getSuggestionPosition(suggestion); 147 int suggestionPosition = getLastCardPosition();
140 final View suggestionView = getViewHolderAtPosition(suggestionPosition). itemView; 148 final View suggestionView = getViewHolderAtPosition(suggestionPosition). itemView;
141 ChromeTabUtils.waitForTabPageLoaded(mTab, new Runnable() { 149 ChromeTabUtils.waitForTabPageLoaded(mTab, new Runnable() {
142 @Override 150 @Override
143 public void run() { 151 public void run() {
144 TouchCommon.singleClickView(suggestionView); 152 TouchCommon.singleClickView(suggestionView);
145 } 153 }
146 }); 154 });
147 Assert.assertEquals(suggestion.mUrl, mTab.getUrl()); 155 Assert.assertEquals(suggestion.mUrl, mTab.getUrl());
148 } 156 }
149 157
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 190
183 @Test 191 @Test
184 @MediumTest 192 @MediumTest
185 @Feature({"NewTabPage"}) 193 @Feature({"NewTabPage"})
186 public void testDismissArticleWithContextMenu() throws InterruptedException, TimeoutException { 194 public void testDismissArticleWithContextMenu() throws InterruptedException, TimeoutException {
187 setSuggestionsAndWaitForUpdate(10); 195 setSuggestionsAndWaitForUpdate(10);
188 List<SnippetArticle> suggestions = mSource.getSuggestionsForCategory(TES T_CATEGORY); 196 List<SnippetArticle> suggestions = mSource.getSuggestionsForCategory(TES T_CATEGORY);
189 Assert.assertEquals(10, suggestions.size()); 197 Assert.assertEquals(10, suggestions.size());
190 198
191 // Scroll a suggestion into view. 199 // Scroll a suggestion into view.
192 int suggestionPosition = getSuggestionPosition(suggestions.get(suggestio ns.size() - 1)); 200 int suggestionPosition = getLastCardPosition();
193 View suggestionView = getViewHolderAtPosition(suggestionPosition).itemVi ew; 201 View suggestionView = getViewHolderAtPosition(suggestionPosition).itemVi ew;
194 202
195 // Dismiss the suggestion using the context menu. 203 // Dismiss the suggestion using the context menu.
196 invokeContextMenu(suggestionView, ContextMenuManager.ID_REMOVE); 204 invokeContextMenu(suggestionView, ContextMenuManager.ID_REMOVE);
197 RecyclerViewTestUtils.waitForViewToDetach(getRecyclerView(), suggestionV iew); 205 RecyclerViewTestUtils.waitForViewToDetach(getRecyclerView(), suggestionV iew);
198 206
199 suggestions = mSource.getSuggestionsForCategory(TEST_CATEGORY); 207 suggestions = mSource.getSuggestionsForCategory(TEST_CATEGORY);
200 Assert.assertEquals(9, suggestions.size()); 208 Assert.assertEquals(9, suggestions.size());
201 } 209 }
202 210
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 } 363 }
356 364
357 private NewTabPageRecyclerView getRecyclerView() { 365 private NewTabPageRecyclerView getRecyclerView() {
358 return getNtpView().getRecyclerView(); 366 return getNtpView().getRecyclerView();
359 } 367 }
360 368
361 private NewTabPageAdapter getAdapter() { 369 private NewTabPageAdapter getAdapter() {
362 return getRecyclerView().getNewTabPageAdapter(); 370 return getRecyclerView().getNewTabPageAdapter();
363 } 371 }
364 372
365 private int getSuggestionPosition(SnippetArticle article) {
366 NewTabPageAdapter adapter = getAdapter();
367 for (int i = 0; i < adapter.getItemCount(); i++) {
368 SnippetArticle articleToCheck = adapter.getSuggestionAt(i);
369 if (articleToCheck != null && articleToCheck.equals(article)) return i;
370 }
371 return RecyclerView.NO_POSITION;
372 }
373
374 /** 373 /**
375 * Scroll the {@link View} at the given adapter position into view and retur ns 374 * Scroll the {@link View} at the given adapter position into view and retur ns
376 * its {@link ViewHolder}. 375 * its {@link ViewHolder}.
377 * @param position the adapter position for which to return the {@link ViewH older}. 376 * @param position the adapter position for which to return the {@link ViewH older}.
378 * @return the ViewHolder for the given {@code position}. 377 * @return the ViewHolder for the given {@code position}.
379 */ 378 */
380 private ViewHolder getViewHolderAtPosition(final int position) { 379 private ViewHolder getViewHolderAtPosition(final int position) {
381 final NewTabPageRecyclerView recyclerView = getRecyclerView(); 380 final NewTabPageRecyclerView recyclerView = getRecyclerView();
382 381
383 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 382 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 TestTouchUtils.longClickView(InstrumentationRegistry.getInstrumentation( ), view); 436 TestTouchUtils.longClickView(InstrumentationRegistry.getInstrumentation( ), view);
438 Assert.assertTrue(InstrumentationRegistry.getInstrumentation().invokeCon textMenuAction( 437 Assert.assertTrue(InstrumentationRegistry.getInstrumentation().invokeCon textMenuAction(
439 mActivityTestRule.getActivity(), contextMenuItemId, 0)); 438 mActivityTestRule.getActivity(), contextMenuItemId, 0));
440 } 439 }
441 440
442 441
443 private static void assertArrayEquals(int[] expected, int[] actual) { 442 private static void assertArrayEquals(int[] expected, int[] actual) {
444 Assert.assertEquals(Arrays.toString(expected), Arrays.toString(actual)); 443 Assert.assertEquals(Arrays.toString(expected), Arrays.toString(actual));
445 } 444 }
446 } 445 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698