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

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/RecentTabsTest.java

Issue 2655273003: Adds a first integration test for the Recent Tabs feature. (Closed)
Patch Set: Created 3 years, 11 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
(Empty)
1 // Copyright 2015 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.offlinepages;
6
7 import android.content.Context;
8 import android.support.test.filters.MediumTest;
9
10 import org.chromium.base.Callback;
11 import org.chromium.base.ThreadUtils;
12 import org.chromium.base.test.util.CommandLineFlags;
13 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.OfflinePageMod elObserver;
14 import org.chromium.chrome.browser.profiles.Profile;
15 import org.chromium.chrome.browser.tab.Tab;
16 import org.chromium.chrome.test.ChromeTabbedActivityTestBase;
17 import org.chromium.content.browser.test.util.Criteria;
18 import org.chromium.content.browser.test.util.CriteriaHelper;
19 import org.chromium.net.NetworkChangeNotifier;
20 import org.chromium.net.test.EmbeddedTestServer;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.concurrent.Semaphore;
25 import java.util.concurrent.TimeUnit;
26
27 /** Integration tests for the Last 1 feature of Offline Pages. */
28 public class RecentTabsTest extends ChromeTabbedActivityTestBase {
29 private static final String TEST_PAGE = "/chrome/test/data/android/about.htm l";
30 private static final int TIMEOUT_MS = 5000;
31 private static final long POLLING_INTERVAL = 100;
32 private static final ClientId LAST_N_ID =
carlosk 2017/01/27 02:28:14 Both POLLING_INTERVAL and LAST_N_ID are not being
dewittj 2017/01/30 17:32:37 Done.
33 new ClientId(OfflinePageBridge.LAST_N_NAMESPACE, "1234");
34
35 private OfflinePageBridge mOfflinePageBridge;
36 private EmbeddedTestServer mTestServer;
37 private String mTestPage;
38
39 private void initializeBridgeForProfile(final boolean incognitoProfile)
40 throws InterruptedException {
41 final Semaphore semaphore = new Semaphore(0);
42 ThreadUtils.runOnUiThread(new Runnable() {
43 @Override
44 public void run() {
45 Profile profile = Profile.getLastUsedProfile();
46 if (incognitoProfile) {
47 profile = profile.getOffTheRecordProfile();
48 }
49 // Ensure we start in an offline state.
50 mOfflinePageBridge = OfflinePageBridge.getForProfile(profile);
51 if (mOfflinePageBridge == null || mOfflinePageBridge.isOfflinePa geModelLoaded()) {
52 semaphore.release();
53 return;
54 }
55 mOfflinePageBridge.addObserver(new OfflinePageModelObserver() {
56 @Override
57 public void offlinePageModelLoaded() {
58 semaphore.release();
59 mOfflinePageBridge.removeObserver(this);
60 }
61 });
62 }
63 });
64 assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
65 }
66
67 @Override
68 protected void setUp() throws Exception {
69 super.setUp();
70
71 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
72 @Override
73 public void run() {
74 // Ensure we start in an offline state.
75 NetworkChangeNotifier.forceConnectivityState(false);
76 Context context = getActivity().getBaseContext();
77 if (!NetworkChangeNotifier.isInitialized()) {
78 NetworkChangeNotifier.init(context);
79 }
80 }
81 });
82
83 initializeBridgeForProfile(false);
84
85 mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation ().getContext());
86 mTestPage = mTestServer.getURL(TEST_PAGE);
87 }
88
89 @Override
90 protected void tearDown() throws Exception {
91 mTestServer.stopAndDestroyServer();
92 super.tearDown();
93 }
94
95 @Override
96 public void startMainActivity() throws InterruptedException {
97 startMainActivityOnBlankPage();
98 }
99
100 @CommandLineFlags.Add({
101 "enable-features=OfflineRecentPages", "short-offline-page-snapshot-d elay-for-test"})
102 @MediumTest
dewittj 2017/01/30 17:32:37 Now that sleep is removed, I think it should run i
103 public void testLastNPageSavedWhenTabSwitchedAfterLoadAndDelay() throws Exce ption {
fgorski 2017/01/27 17:47:13 Can you add a bit of documentation to this test? (
dewittj 2017/01/30 17:32:37 Added more docs within the test, and simplified th
104 // The tab of interest.
105 final Tab tab = loadUrlInNewTab(mTestPage);
106 final ClientId clientId =
107 new ClientId(OfflinePageBridge.LAST_N_NAMESPACE, Integer.toStrin g(tab.getId()));
108
109 // After load, with the testing flag we need to sleep a small amount to make sure that
110 // ShapshotController has a chance to fire StartSnapshot.
111 Thread.sleep(20);
carlosk 2017/01/27 02:28:14 Sleeping in tests always makes me cringe but I don
Dmitry Titov 2017/01/27 03:10:25 Yes, is it possible to wait for a notification or
fgorski 2017/01/27 17:47:13 do we inform NTP of new last_n pages?
dewittj 2017/01/30 17:32:37 To reply to everyone :) The issue here was not to
112
113 // Switch to a new tab to cause the hidden event.
114 loadUrlInNewTab(mTestPage);
115
fgorski 2017/01/27 17:47:13 nit: You can probably move the clientId line here,
dewittj 2017/01/30 17:32:37 Acknowledged.
116 CriteriaHelper.pollInstrumentationThread(new Criteria() {
117 @Override
118 public boolean isSatisfied() {
119 try {
120 return getPageByClientId(clientId) != null;
121 } catch (InterruptedException e) {
122 return false;
123 }
124 }
125 });
126 }
127
128 private OfflinePageItem getPageByClientId(ClientId clientId) throws Interrup tedException {
129 final OfflinePageItem[] result = {null};
130 final Semaphore semaphore = new Semaphore(0);
131 final List<ClientId> clientIdList = new ArrayList<>();
132 clientIdList.add(clientId);
133
134 ThreadUtils.runOnUiThread(new Runnable() {
135 @Override
136 public void run() {
137 mOfflinePageBridge.getPagesByClientIds(
138 clientIdList, new Callback<List<OfflinePageItem>>() {
139 @Override
140 public void onResult(List<OfflinePageItem> items) {
141 if (!items.isEmpty()) {
142 result[0] = items.get(0);
143 }
144 semaphore.release();
145 }
146 });
147 }
148 });
149 assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
150 return result[0];
151 }
152 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698