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.suggestions; | |
| 6 | |
| 7 import android.support.test.filters.MediumTest; | |
| 8 import android.view.View; | |
| 9 import android.view.ViewGroup; | |
| 10 | |
| 11 import org.chromium.base.ThreadUtils; | |
| 12 import org.chromium.base.test.util.Feature; | |
| 13 import org.chromium.base.test.util.RetryOnFailure; | |
| 14 import org.chromium.chrome.R; | |
| 15 import org.chromium.chrome.browser.ChromeActivity; | |
| 16 import org.chromium.chrome.browser.UrlConstants; | |
| 17 import org.chromium.chrome.browser.ntp.ContextMenuManager; | |
| 18 import org.chromium.chrome.browser.ntp.NewTabPage; | |
| 19 import org.chromium.chrome.browser.ntp.cards.NewTabPageRecyclerView; | |
| 20 import org.chromium.chrome.browser.tab.Tab; | |
| 21 import org.chromium.chrome.test.ChromeTabbedActivityTestBase; | |
| 22 import org.chromium.chrome.test.util.NewTabPageTestUtils; | |
| 23 import org.chromium.chrome.test.util.browser.RecyclerViewTestUtils; | |
| 24 import org.chromium.chrome.test.util.browser.suggestions.FakeSuggestionsSource; | |
| 25 import org.chromium.content.browser.test.util.Criteria; | |
| 26 import org.chromium.content.browser.test.util.CriteriaHelper; | |
| 27 import org.chromium.content.browser.test.util.TestTouchUtils; | |
| 28 import org.chromium.net.test.EmbeddedTestServer; | |
| 29 | |
| 30 import java.util.concurrent.TimeoutException; | |
| 31 | |
| 32 /** | |
| 33 * Instrumentation tests for {@link TileGroup} on the New Tab Page. | |
| 
 
Michael van Ouwerkerk
2017/03/20 11:10:18
Is this actually a test for TileGridLayout? It see
 
dgn
2017/03/20 16:06:45
TileGridLayout is mentioned just because that is h
 
 | |
| 34 */ | |
| 35 @RetryOnFailure | |
| 36 public class TileGroupTest extends ChromeTabbedActivityTestBase { | |
| 37 private static final String[] FAKE_MOST_VISITED_URLS = | |
| 38 new String[] {"/chrome/test/data/android/navigate/one.html", | |
| 39 "/chrome/test/data/android/navigate/two.html", | |
| 40 "/chrome/test/data/android/navigate/three.html"}; | |
| 41 | |
| 42 private NewTabPage mNtp; | |
| 43 private String[] mSiteSuggestionUrls; | |
| 44 private FakeMostVisitedSites mMostVisitedSites; | |
| 45 private EmbeddedTestServer mTestServer; | |
| 46 | |
| 47 @Override | |
| 48 protected void setUp() throws Exception { | |
| 49 mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation ().getContext()); | |
| 50 | |
| 51 mSiteSuggestionUrls = new String[] {mTestServer.getURL(FAKE_MOST_VISITED _URLS[0]), | |
| 52 mTestServer.getURL(FAKE_MOST_VISITED_URLS[1]), | |
| 53 mTestServer.getURL(FAKE_MOST_VISITED_URLS[2])}; | |
| 54 | |
| 55 FakeSuggestionsSource mSource = new FakeSuggestionsSource(); | |
| 56 NewTabPage.setSuggestionsSourceForTests(mSource); | |
| 57 | |
| 58 super.setUp(); | |
| 59 } | |
| 60 | |
| 61 @Override | |
| 62 protected void tearDown() throws Exception { | |
| 63 TileGroupDelegateImpl.setMostVisitedSitesForTests(null); | |
| 64 NewTabPage.setSuggestionsSourceForTests(null); | |
| 65 mTestServer.stopAndDestroyServer(); | |
| 66 | |
| 67 super.tearDown(); | |
| 68 } | |
| 69 | |
| 70 @Override | |
| 71 public void startMainActivity() throws InterruptedException { | |
| 72 startMainActivityOnBlankPage(); | |
| 73 Tab mTab = getActivity().getActivityTab(); | |
| 74 | |
| 75 mMostVisitedSites = new FakeMostVisitedSites(); | |
| 
 
Michael van Ouwerkerk
2017/03/20 11:10:19
Can this block be in setUp?
 
dgn
2017/03/20 16:06:45
Done.
 
 | |
| 76 mMostVisitedSites.setTileSuggestions(mSiteSuggestionUrls); | |
| 77 TileGroupDelegateImpl.setMostVisitedSitesForTests(mMostVisitedSites); | |
| 78 | |
| 79 loadUrl(UrlConstants.NTP_URL); | |
| 80 NewTabPageTestUtils.waitForNtpLoaded(mTab); | |
| 81 | |
| 82 assertTrue(mTab.getNativePage() instanceof NewTabPage); | |
| 83 mNtp = (NewTabPage) mTab.getNativePage(); | |
| 84 | |
| 85 RecyclerViewTestUtils.waitForStableRecyclerView(getRecyclerView()); | |
| 86 } | |
| 87 | |
| 88 @MediumTest | |
| 89 @Feature({"NewTabPage"}) | |
| 90 public void testDismissTileWithContextMenu() throws InterruptedException, Ti meoutException { | |
| 91 final View tileView = getTileViewForUrl(mSiteSuggestionUrls[0]); | |
| 92 | |
| 93 // Dismiss the tile using the context menu. | |
| 94 invokeContextMenu(tileView, ContextMenuManager.ID_REMOVE); | |
| 95 assertTrue(mMostVisitedSites.isUrlBlacklisted(mSiteSuggestionUrls[0])); | |
| 96 | |
| 97 // Ensure that the removal update goes through. | |
| 98 mMostVisitedSites.setTileSuggestions(mSiteSuggestionUrls[1], mSiteSugges tionUrls[2]); | |
| 99 waitForViewDetached(tileView); | |
| 100 waitForChildCountChanged(getTileGridLayout(), 2); | |
| 
 
Michael van Ouwerkerk
2017/03/20 11:10:19
Please assert earlier in the test that it has 3 ch
 
dgn
2017/03/20 16:06:44
Done.
 
 | |
| 101 } | |
| 102 | |
| 103 @MediumTest | |
| 104 @Feature({"NewTabPage"}) | |
| 105 public void testDismissTileUndo() throws InterruptedException, TimeoutExcept ion { | |
| 106 final TileGridLayout tileGridLayout = getTileGridLayout(); | |
| 107 final View tileView = getTileViewForUrl(mSiteSuggestionUrls[0]); | |
| 108 assertEquals(mSiteSuggestionUrls.length, tileGridLayout.getChildCount()) ; | |
| 109 | |
| 110 // Dismiss the tile using the context menu. | |
| 111 invokeContextMenu(tileView, ContextMenuManager.ID_REMOVE); | |
| 112 | |
| 113 // Ensure that the removal update goes through. | |
| 114 mMostVisitedSites.setTileSuggestions(mSiteSuggestionUrls[1], mSiteSugges tionUrls[2]); | |
| 115 waitForChildCountChanged(tileGridLayout, 2); | |
| 116 waitForSnackbar(getActivity()); | |
| 117 | |
| 118 assertTrue(mMostVisitedSites.isUrlBlacklisted(mSiteSuggestionUrls[0])); | |
| 119 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
| 120 @Override | |
| 121 public void run() { | |
| 122 getActivity().getSnackbarManager().onClick(null); | |
| 123 } | |
| 124 }); | |
| 125 | |
| 126 assertFalse(mMostVisitedSites.isUrlBlacklisted(mSiteSuggestionUrls[0])); | |
| 127 | |
| 128 // Ensure that the removal of the update goes through. | |
| 
 
Michael van Ouwerkerk
2017/03/20 11:10:18
"Ensure that undoing the removal is reflected in t
 
dgn
2017/03/20 16:06:44
Done.
 
 | |
| 129 mMostVisitedSites.setTileSuggestions(mSiteSuggestionUrls); | |
| 130 waitForChildCountChanged(tileGridLayout, mSiteSuggestionUrls.length); | |
| 131 } | |
| 132 | |
| 133 private NewTabPageRecyclerView getRecyclerView() { | |
| 134 return mNtp.getNewTabPageView().getRecyclerView(); | |
| 135 } | |
| 136 | |
| 137 private TileGridLayout getTileGridLayout() { | |
| 138 ViewGroup aboveTheFoldView = getRecyclerView().getAboveTheFoldView(); | |
| 139 assertNotNull("Unable to retrieve the AboveTheFold view.", aboveTheFoldV iew); | |
| 140 | |
| 141 TileGridLayout tileGridLayout = | |
| 142 (TileGridLayout) aboveTheFoldView.findViewById(R.id.tile_grid_la yout); | |
| 143 assertNotNull("Unable to retrieve the TileGridLayout.", tileGridLayout); | |
| 144 return tileGridLayout; | |
| 145 } | |
| 146 | |
| 147 private View getTileViewForUrl(String url) { | |
| 148 View tileView = getTileGridLayout().getTileView(url); | |
| 149 assertNotNull("Tile not found for url " + url, tileView); | |
| 150 | |
| 151 return tileView; | |
| 152 } | |
| 153 | |
| 154 private void invokeContextMenu(View view, int contextMenuItemId) { | |
| 155 TestTouchUtils.longClickView(getInstrumentation(), view); | |
| 156 assertTrue( | |
| 157 getInstrumentation().invokeContextMenuAction(getActivity(), cont extMenuItemId, 0)); | |
| 158 } | |
| 159 | |
| 160 private static void waitForSnackbar(final ChromeActivity activity) { | |
| 
 
Michael van Ouwerkerk
2017/03/20 11:10:19
Other instrumentation tests seems to rely on getCu
 
dgn
2017/03/20 16:06:44
As explained below, Snackbar is not super usable o
 
 | |
| 161 CriteriaHelper.pollUiThread(new Criteria("The snackbar was not shown.") { | |
| 162 @Override | |
| 163 public boolean isSatisfied() { | |
| 164 return activity.findViewById(R.id.snackbar) != null; | |
| 
 
Michael van Ouwerkerk
2017/03/20 11:10:19
DataReductionPromoSnackbarControllerTest also conf
 
dgn
2017/03/20 16:06:44
That one has access to package private methods fro
 
 | |
| 165 } | |
| 166 }); | |
| 167 } | |
| 168 | |
| 169 private static void waitForViewDetached(final View view) { | |
| 
 
Michael van Ouwerkerk
2017/03/20 11:10:18
This seems like a very generic method, is there a
 
dgn
2017/03/20 16:06:45
Removed
 
 | |
| 170 CriteriaHelper.pollUiThread(new Criteria("The view did not detach.") { | |
| 171 @Override | |
| 172 public boolean isSatisfied() { | |
| 173 return view.getParent() == null; | |
| 174 } | |
| 175 }); | |
| 176 } | |
| 177 | |
| 178 private static void waitForChildCountChanged(final ViewGroup parent, final i nt expectedCount) { | |
| 
 
Michael van Ouwerkerk
2017/03/20 11:10:19
nit: rename to waitForChildCount, it's shorter. An
 
Michael van Ouwerkerk
2017/03/20 11:10:19
This might also be fine in a generic test util.
 
dgn
2017/03/20 16:06:44
Done.
 
dgn
2017/03/20 16:06:44
Removed the polling, but because for the case of t
 
 | |
| 179 CriteriaHelper.pollUiThread(new Criteria("The child count did not change .") { | |
| 180 @Override | |
| 181 public boolean isSatisfied() { | |
| 182 return parent.getChildCount() == expectedCount; | |
| 183 } | |
| 184 }); | |
| 185 } | |
| 186 } | |
| OLD | NEW |