| OLD | NEW |
| 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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 | 129 |
| 130 @Test | 130 @Test |
| 131 @MediumTest | 131 @MediumTest |
| 132 @Feature({"NewTabPage"}) | 132 @Feature({"NewTabPage"}) |
| 133 public void testClickSuggestion() throws InterruptedException { | 133 public void testClickSuggestion() throws InterruptedException { |
| 134 setSuggestionsAndWaitForUpdate(10); | 134 setSuggestionsAndWaitForUpdate(10); |
| 135 List<SnippetArticle> suggestions = mSource.getSuggestionsForCategory(TES
T_CATEGORY); | 135 List<SnippetArticle> suggestions = mSource.getSuggestionsForCategory(TES
T_CATEGORY); |
| 136 | 136 |
| 137 // Scroll the last suggestion into view and click it. | 137 // Scroll the last suggestion into view and click it. |
| 138 SnippetArticle suggestion = suggestions.get(suggestions.size() - 1); | 138 SnippetArticle suggestion = suggestions.get(suggestions.size() - 1); |
| 139 int suggestionPosition = getSuggestionPosition(suggestion); | 139 int suggestionPosition = getLastCardPosition(); |
| 140 final View suggestionView = getViewHolderAtPosition(suggestionPosition).
itemView; | 140 final View suggestionView = getViewHolderAtPosition(suggestionPosition).
itemView; |
| 141 ChromeTabUtils.waitForTabPageLoaded(mTab, new Runnable() { | 141 ChromeTabUtils.waitForTabPageLoaded(mTab, new Runnable() { |
| 142 @Override | 142 @Override |
| 143 public void run() { | 143 public void run() { |
| 144 TouchCommon.singleClickView(suggestionView); | 144 TouchCommon.singleClickView(suggestionView); |
| 145 } | 145 } |
| 146 }); | 146 }); |
| 147 Assert.assertEquals(suggestion.mUrl, mTab.getUrl()); | 147 Assert.assertEquals(suggestion.mUrl, mTab.getUrl()); |
| 148 } | 148 } |
| 149 | 149 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 182 |
| 183 @Test | 183 @Test |
| 184 @MediumTest | 184 @MediumTest |
| 185 @Feature({"NewTabPage"}) | 185 @Feature({"NewTabPage"}) |
| 186 public void testDismissArticleWithContextMenu() throws InterruptedException,
TimeoutException { | 186 public void testDismissArticleWithContextMenu() throws InterruptedException,
TimeoutException { |
| 187 setSuggestionsAndWaitForUpdate(10); | 187 setSuggestionsAndWaitForUpdate(10); |
| 188 List<SnippetArticle> suggestions = mSource.getSuggestionsForCategory(TES
T_CATEGORY); | 188 List<SnippetArticle> suggestions = mSource.getSuggestionsForCategory(TES
T_CATEGORY); |
| 189 Assert.assertEquals(10, suggestions.size()); | 189 Assert.assertEquals(10, suggestions.size()); |
| 190 | 190 |
| 191 // Scroll a suggestion into view. | 191 // Scroll a suggestion into view. |
| 192 int suggestionPosition = getSuggestionPosition(suggestions.get(suggestio
ns.size() - 1)); | 192 int suggestionPosition = getLastCardPosition(); |
| 193 View suggestionView = getViewHolderAtPosition(suggestionPosition).itemVi
ew; | 193 View suggestionView = getViewHolderAtPosition(suggestionPosition).itemVi
ew; |
| 194 | 194 |
| 195 // Dismiss the suggestion using the context menu. | 195 // Dismiss the suggestion using the context menu. |
| 196 invokeContextMenu(suggestionView, ContextMenuManager.ID_REMOVE); | 196 invokeContextMenu(suggestionView, ContextMenuManager.ID_REMOVE); |
| 197 RecyclerViewTestUtils.waitForViewToDetach(getRecyclerView(), suggestionV
iew); | 197 RecyclerViewTestUtils.waitForViewToDetach(getRecyclerView(), suggestionV
iew); |
| 198 | 198 |
| 199 suggestions = mSource.getSuggestionsForCategory(TEST_CATEGORY); | 199 suggestions = mSource.getSuggestionsForCategory(TEST_CATEGORY); |
| 200 Assert.assertEquals(9, suggestions.size()); | 200 Assert.assertEquals(9, suggestions.size()); |
| 201 } | 201 } |
| 202 | 202 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 } | 355 } |
| 356 | 356 |
| 357 private NewTabPageRecyclerView getRecyclerView() { | 357 private NewTabPageRecyclerView getRecyclerView() { |
| 358 return getNtpView().getRecyclerView(); | 358 return getNtpView().getRecyclerView(); |
| 359 } | 359 } |
| 360 | 360 |
| 361 private NewTabPageAdapter getAdapter() { | 361 private NewTabPageAdapter getAdapter() { |
| 362 return getRecyclerView().getNewTabPageAdapter(); | 362 return getRecyclerView().getNewTabPageAdapter(); |
| 363 } | 363 } |
| 364 | 364 |
| 365 private int getSuggestionPosition(SnippetArticle article) { | 365 private int getLastCardPosition() { |
| 366 NewTabPageAdapter adapter = getAdapter(); | 366 int count = getAdapter().getItemCount(); |
| 367 for (int i = 0; i < adapter.getItemCount(); i++) { | 367 for (int i = count - 1; i >= 0; i--) { |
| 368 SnippetArticle articleToCheck = adapter.getSuggestionAt(i); | 368 if (getAdapter().getItemViewType(i) == ItemViewType.SNIPPET) return
i; |
| 369 if (articleToCheck != null && articleToCheck.equals(article)) return
i; | |
| 370 } | 369 } |
| 371 return RecyclerView.NO_POSITION; | 370 return RecyclerView.NO_POSITION; |
| 372 } | 371 } |
| 373 | 372 |
| 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 */ |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 } |
| OLD | NEW |