Chromium Code Reviews| Index: chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/RecentTabsTest.java |
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/RecentTabsTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/RecentTabsTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..30c10181b21a497d466b9c07e76aae26ba473327 |
| --- /dev/null |
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/RecentTabsTest.java |
| @@ -0,0 +1,152 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.offlinepages; |
| + |
| +import android.content.Context; |
| +import android.support.test.filters.MediumTest; |
| + |
| +import org.chromium.base.Callback; |
| +import org.chromium.base.ThreadUtils; |
| +import org.chromium.base.test.util.CommandLineFlags; |
| +import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.OfflinePageModelObserver; |
| +import org.chromium.chrome.browser.profiles.Profile; |
| +import org.chromium.chrome.browser.tab.Tab; |
| +import org.chromium.chrome.test.ChromeTabbedActivityTestBase; |
| +import org.chromium.content.browser.test.util.Criteria; |
| +import org.chromium.content.browser.test.util.CriteriaHelper; |
| +import org.chromium.net.NetworkChangeNotifier; |
| +import org.chromium.net.test.EmbeddedTestServer; |
| + |
| +import java.util.ArrayList; |
| +import java.util.List; |
| +import java.util.concurrent.Semaphore; |
| +import java.util.concurrent.TimeUnit; |
| + |
| +/** Integration tests for the Last 1 feature of Offline Pages. */ |
| +public class RecentTabsTest extends ChromeTabbedActivityTestBase { |
| + private static final String TEST_PAGE = "/chrome/test/data/android/about.html"; |
| + private static final int TIMEOUT_MS = 5000; |
| + private static final long POLLING_INTERVAL = 100; |
| + 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.
|
| + new ClientId(OfflinePageBridge.LAST_N_NAMESPACE, "1234"); |
| + |
| + private OfflinePageBridge mOfflinePageBridge; |
| + private EmbeddedTestServer mTestServer; |
| + private String mTestPage; |
| + |
| + private void initializeBridgeForProfile(final boolean incognitoProfile) |
| + throws InterruptedException { |
| + final Semaphore semaphore = new Semaphore(0); |
| + ThreadUtils.runOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + Profile profile = Profile.getLastUsedProfile(); |
| + if (incognitoProfile) { |
| + profile = profile.getOffTheRecordProfile(); |
| + } |
| + // Ensure we start in an offline state. |
| + mOfflinePageBridge = OfflinePageBridge.getForProfile(profile); |
| + if (mOfflinePageBridge == null || mOfflinePageBridge.isOfflinePageModelLoaded()) { |
| + semaphore.release(); |
| + return; |
| + } |
| + mOfflinePageBridge.addObserver(new OfflinePageModelObserver() { |
| + @Override |
| + public void offlinePageModelLoaded() { |
| + semaphore.release(); |
| + mOfflinePageBridge.removeObserver(this); |
| + } |
| + }); |
| + } |
| + }); |
| + assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
| + } |
| + |
| + @Override |
| + protected void setUp() throws Exception { |
| + super.setUp(); |
| + |
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| + @Override |
| + public void run() { |
| + // Ensure we start in an offline state. |
| + NetworkChangeNotifier.forceConnectivityState(false); |
| + Context context = getActivity().getBaseContext(); |
| + if (!NetworkChangeNotifier.isInitialized()) { |
| + NetworkChangeNotifier.init(context); |
| + } |
| + } |
| + }); |
| + |
| + initializeBridgeForProfile(false); |
| + |
| + mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation().getContext()); |
| + mTestPage = mTestServer.getURL(TEST_PAGE); |
| + } |
| + |
| + @Override |
| + protected void tearDown() throws Exception { |
| + mTestServer.stopAndDestroyServer(); |
| + super.tearDown(); |
| + } |
| + |
| + @Override |
| + public void startMainActivity() throws InterruptedException { |
| + startMainActivityOnBlankPage(); |
| + } |
| + |
| + @CommandLineFlags.Add({ |
| + "enable-features=OfflineRecentPages", "short-offline-page-snapshot-delay-for-test"}) |
| + @MediumTest |
|
fgorski
2017/01/27 17:47:13
https://developer.android.com/reference/android/su
dewittj
2017/01/30 17:32:37
Now that sleep is removed, I think it should run i
|
| + public void testLastNPageSavedWhenTabSwitchedAfterLoadAndDelay() throws Exception { |
|
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
|
| + // The tab of interest. |
| + final Tab tab = loadUrlInNewTab(mTestPage); |
| + final ClientId clientId = |
| + new ClientId(OfflinePageBridge.LAST_N_NAMESPACE, Integer.toString(tab.getId())); |
| + |
| + // After load, with the testing flag we need to sleep a small amount to make sure that |
| + // ShapshotController has a chance to fire StartSnapshot. |
| + 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
|
| + |
| + // Switch to a new tab to cause the hidden event. |
| + loadUrlInNewTab(mTestPage); |
| + |
|
fgorski
2017/01/27 17:47:13
nit: You can probably move the clientId line here,
dewittj
2017/01/30 17:32:37
Acknowledged.
|
| + CriteriaHelper.pollInstrumentationThread(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + try { |
| + return getPageByClientId(clientId) != null; |
| + } catch (InterruptedException e) { |
| + return false; |
| + } |
| + } |
| + }); |
| + } |
| + |
| + private OfflinePageItem getPageByClientId(ClientId clientId) throws InterruptedException { |
| + final OfflinePageItem[] result = {null}; |
| + final Semaphore semaphore = new Semaphore(0); |
| + final List<ClientId> clientIdList = new ArrayList<>(); |
| + clientIdList.add(clientId); |
| + |
| + ThreadUtils.runOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + mOfflinePageBridge.getPagesByClientIds( |
| + clientIdList, new Callback<List<OfflinePageItem>>() { |
| + @Override |
| + public void onResult(List<OfflinePageItem> items) { |
| + if (!items.isEmpty()) { |
| + result[0] = items.get(0); |
| + } |
| + semaphore.release(); |
| + } |
| + }); |
| + } |
| + }); |
| + assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
| + return result[0]; |
| + } |
| +} |