| Index: chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtilsTest.java
|
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtilsTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtilsTest.java
|
| index fa4c93ea64c6530b21af1be72cfcf730de6e13f8..307e7e94198a8a63dfa62f4efed70387be731171 100644
|
| --- a/chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtilsTest.java
|
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtilsTest.java
|
| @@ -5,29 +5,19 @@
|
| package org.chromium.chrome.browser.offlinepages;
|
|
|
| import android.content.Context;
|
| -import android.support.test.InstrumentationRegistry;
|
| import android.support.test.filters.SmallTest;
|
| -
|
| -import org.junit.After;
|
| -import org.junit.Assert;
|
| -import org.junit.Before;
|
| -import org.junit.Rule;
|
| -import org.junit.Test;
|
| -import org.junit.runner.RunWith;
|
|
|
| import org.chromium.base.Callback;
|
| import org.chromium.base.ThreadUtils;
|
| import org.chromium.base.test.util.CommandLineFlags;
|
| import org.chromium.base.test.util.RetryOnFailure;
|
| import org.chromium.chrome.browser.ChromeActivity;
|
| -import org.chromium.chrome.browser.ChromeSwitches;
|
| import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.OfflinePageModelObserver;
|
| import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.SavePageCallback;
|
| import org.chromium.chrome.browser.profiles.Profile;
|
| import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController;
|
| import org.chromium.chrome.browser.tab.Tab;
|
| -import org.chromium.chrome.test.ChromeActivityTestRule;
|
| -import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
|
| +import org.chromium.chrome.test.ChromeActivityTestCaseBase;
|
| import org.chromium.components.offlinepages.SavePageResult;
|
| import org.chromium.content.browser.test.util.Criteria;
|
| import org.chromium.content.browser.test.util.CriteriaHelper;
|
| @@ -42,15 +32,8 @@
|
| import java.util.concurrent.TimeUnit;
|
|
|
| /** Unit tests for {@link OfflinePageUtils}. */
|
| -@RunWith(ChromeJUnit4ClassRunner.class)
|
| -@CommandLineFlags.Add({"enable-features=OfflineBookmarks", "enable-features=OfflinePagesSharing",
|
| - ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE,
|
| - ChromeActivityTestRule.DISABLE_NETWORK_PREDICTION_FLAG})
|
| -public class OfflinePageUtilsTest {
|
| - @Rule
|
| - public ChromeActivityTestRule<ChromeActivity> mActivityTestRule =
|
| - new ChromeActivityTestRule<>(ChromeActivity.class);
|
| -
|
| +@CommandLineFlags.Add({"enable-features=OfflineBookmarks", "enable-features=OfflinePagesSharing"})
|
| +public class OfflinePageUtilsTest extends ChromeActivityTestCaseBase<ChromeActivity> {
|
| private static final String TEST_PAGE = "/chrome/test/data/android/about.html";
|
| private static final int TIMEOUT_MS = 5000;
|
| private static final ClientId BOOKMARK_ID =
|
| @@ -59,9 +42,13 @@
|
| private OfflinePageBridge mOfflinePageBridge;
|
| private EmbeddedTestServer mTestServer;
|
|
|
| - @Before
|
| - public void setUp() throws Exception {
|
| - mActivityTestRule.startMainActivityOnBlankPage();
|
| + public OfflinePageUtilsTest() {
|
| + super(ChromeActivity.class);
|
| + }
|
| +
|
| + @Override
|
| + protected void setUp() throws Exception {
|
| + super.setUp();
|
| final Semaphore semaphore = new Semaphore(0);
|
| ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| @Override
|
| @@ -87,15 +74,15 @@
|
| }
|
| }
|
| });
|
| - Assert.assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
|
| -
|
| - mTestServer = EmbeddedTestServer.createAndStartServer(
|
| - InstrumentationRegistry.getInstrumentation().getContext());
|
| - }
|
| -
|
| - @After
|
| - public void tearDown() throws Exception {
|
| + assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
|
| +
|
| + mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation().getContext());
|
| + }
|
| +
|
| + @Override
|
| + protected void tearDown() throws Exception {
|
| mTestServer.stopAndDestroyServer();
|
| + super.tearDown();
|
| }
|
|
|
| /**
|
| @@ -145,7 +132,11 @@
|
| }
|
| }
|
|
|
| - @Test
|
| + @Override
|
| + public void startMainActivity() throws InterruptedException {
|
| + startMainActivityOnBlankPage();
|
| + }
|
| +
|
| @SmallTest
|
| @RetryOnFailure
|
| public void testShowOfflineSnackbarIfNecessary() throws Exception {
|
| @@ -167,22 +158,19 @@
|
| }
|
| });
|
| String testUrl = mTestServer.getURL(TEST_PAGE);
|
| - mActivityTestRule.loadUrl(testUrl);
|
| -
|
| - int tabId = mActivityTestRule.getActivity().getActivityTab().getId();
|
| + loadUrl(testUrl);
|
| +
|
| + int tabId = getActivity().getActivityTab().getId();
|
|
|
| // Act. This needs to be called from the UI thread.
|
| ThreadUtils.runOnUiThread(new Runnable() {
|
| @Override
|
| public void run() {
|
| - OfflinePageTabObserver offlineObserver = new OfflinePageTabObserver(
|
| - mActivityTestRule.getActivity().getTabModelSelector(),
|
| - mActivityTestRule.getActivity().getSnackbarManager(),
|
| - mockSnackbarController);
|
| - OfflinePageTabObserver.setObserverForTesting(
|
| - mActivityTestRule.getActivity(), offlineObserver);
|
| - OfflinePageUtils.showOfflineSnackbarIfNecessary(
|
| - mActivityTestRule.getActivity().getActivityTab());
|
| + OfflinePageTabObserver offlineObserver =
|
| + new OfflinePageTabObserver(getActivity().getTabModelSelector(),
|
| + getActivity().getSnackbarManager(), mockSnackbarController);
|
| + OfflinePageTabObserver.setObserverForTesting(getActivity(), offlineObserver);
|
| + OfflinePageUtils.showOfflineSnackbarIfNecessary(getActivity().getActivityTab());
|
|
|
| // Pretend that we went online, this should cause the snackbar to show.
|
| // This call will set the isConnected call to return true.
|
| @@ -197,13 +185,13 @@
|
| mockSnackbarController.waitForSnackbarControllerToFinish();
|
|
|
| // Assert snackbar was shown.
|
| - Assert.assertEquals(tabId, mockSnackbarController.getLastTabId());
|
| - Assert.assertTrue(mockSnackbarController.getDismissed());
|
| + assertEquals(tabId, mockSnackbarController.getLastTabId());
|
| + assertTrue(mockSnackbarController.getDismissed());
|
| }
|
|
|
| private void loadPageAndSave() throws Exception {
|
| String testUrl = mTestServer.getURL(TEST_PAGE);
|
| - mActivityTestRule.loadUrl(testUrl);
|
| + loadUrl(testUrl);
|
| savePage(SavePageResult.SUCCESS, testUrl);
|
| }
|
|
|
| @@ -215,22 +203,21 @@
|
| ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| @Override
|
| public void run() {
|
| - mOfflinePageBridge.savePage(
|
| - mActivityTestRule.getActivity().getActivityTab().getWebContents(),
|
| + mOfflinePageBridge.savePage(getActivity().getActivityTab().getWebContents(),
|
| BOOKMARK_ID, new SavePageCallback() {
|
| @Override
|
| public void onSavePageDone(
|
| int savePageResult, String url, long offlineId) {
|
| - Assert.assertEquals(
|
| + assertEquals(
|
| "Requested and returned URLs differ.", expectedUrl, url);
|
| - Assert.assertEquals(
|
| + assertEquals(
|
| "Save result incorrect.", expectedResult, savePageResult);
|
| semaphore.release();
|
| }
|
| });
|
| }
|
| });
|
| - Assert.assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
|
| + assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
|
| }
|
|
|
| private List<OfflinePageItem> getAllPages() throws InterruptedException {
|
| @@ -248,11 +235,10 @@
|
| });
|
| }
|
| });
|
| - Assert.assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
|
| + assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
|
| return result;
|
| }
|
|
|
| - @Test
|
| @SmallTest
|
| @RetryOnFailure
|
| public void testCopyToShareableLocation() throws Exception {
|
| @@ -269,23 +255,21 @@
|
| // Clear the directory before copying the file.
|
| clearSharedOfflineFilesAndWait();
|
|
|
| - File offlineSharingDir = OfflinePageUtils.getDirectoryForOfflineSharing(
|
| - mActivityTestRule.getActivity().getBaseContext());
|
| - Assert.assertTrue("Offline sharing directory should exist.", offlineSharingDir != null);
|
| + File offlineSharingDir =
|
| + OfflinePageUtils.getDirectoryForOfflineSharing(getActivity().getBaseContext());
|
| + assertTrue("Offline sharing directory should exist.", offlineSharingDir != null);
|
|
|
| File offlinePageShareable = new File(offlineSharingDir, offlinePageOriginal.getName());
|
| - Assert.assertFalse(
|
| - "File with the same name should not exist.", offlinePageShareable.exists());
|
| -
|
| - Assert.assertTrue("Should be able to copy file to shareable location.",
|
| + assertFalse("File with the same name should not exist.", offlinePageShareable.exists());
|
| +
|
| + assertTrue("Should be able to copy file to shareable location.",
|
| OfflinePageUtils.copyToShareableLocation(
|
| offlinePageOriginal, offlinePageShareable));
|
|
|
| - Assert.assertEquals("File copy result incorrect", offlinePageOriginal.length(),
|
| + assertEquals("File copy result incorrect", offlinePageOriginal.length(),
|
| offlinePageShareable.length());
|
| }
|
|
|
| - @Test
|
| @SmallTest
|
| public void testDeleteSharedOfflineFiles() throws Exception {
|
| // Save an offline page.
|
| @@ -298,15 +282,15 @@
|
|
|
| File offlinePageOriginal = new File(offlinePageFilePath);
|
|
|
| - final Context context = mActivityTestRule.getActivity().getBaseContext();
|
| + final Context context = getActivity().getBaseContext();
|
| final File offlineSharingDir = OfflinePageUtils.getDirectoryForOfflineSharing(context);
|
|
|
| - Assert.assertTrue("Should be able to create subdirectory in shareable directory.",
|
| + assertTrue("Should be able to create subdirectory in shareable directory.",
|
| (offlineSharingDir != null));
|
|
|
| File offlinePageShareable = new File(offlineSharingDir, offlinePageOriginal.getName());
|
| if (!offlinePageShareable.exists()) {
|
| - Assert.assertTrue("Should be able to copy file to shareable location.",
|
| + assertTrue("Should be able to copy file to shareable location.",
|
| OfflinePageUtils.copyToShareableLocation(
|
| offlinePageOriginal, offlinePageShareable));
|
| }
|
| @@ -316,7 +300,7 @@
|
| }
|
|
|
| private void clearSharedOfflineFilesAndWait() {
|
| - final Context context = mActivityTestRule.getActivity().getBaseContext();
|
| + final Context context = getActivity().getBaseContext();
|
| final File offlineSharingDir = OfflinePageUtils.getDirectoryForOfflineSharing(context);
|
| ThreadUtils.runOnUiThread(new Runnable() {
|
| @Override
|
|
|