Commit 33fb07fa authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Android Download Location: Adds end to end tests.

This CL adds Java end to end test for download location change feature.

Bug: 896873
Change-Id: I0dd89dab0f1a4c36726f0795231a3116c00955c2
Reviewed-on: https://chromium-review.googlesource.com/c/1294670
Commit-Queue: Xing Liu <xingliu@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#602427}
parent b5f825b9
......@@ -29,7 +29,7 @@ import java.util.ArrayList;
*
* This class uses an asynchronous task to retrieve the directories, and guarantee only one task
* can execute at any time. Multiple tasks may cause certain device fail to retrieve download
* directories.
* directories. Should be used on main thread.
*
* Also, this class listens to SD card insertion and removal events to update the directory
* options accordingly.
......@@ -117,7 +117,7 @@ public class DownloadDirectoryProvider {
// Singleton instance.
private static class LazyHolder {
private static final DownloadDirectoryProvider INSTANCE = new DownloadDirectoryProvider();
private static DownloadDirectoryProvider sInstance = new DownloadDirectoryProvider();
}
/**
......@@ -125,7 +125,15 @@ public class DownloadDirectoryProvider {
* @return The singleton directory provider instance.
*/
public static DownloadDirectoryProvider getInstance() {
return LazyHolder.INSTANCE;
return LazyHolder.sInstance;
}
/**
* Sets the directory provider for testing.
* @param provider The directory provider used in tests.
*/
public void setDirectoryProviderForTesting(DownloadDirectoryProvider provider) {
LazyHolder.sInstance = provider;
}
/**
......@@ -154,9 +162,9 @@ public class DownloadDirectoryProvider {
private ArrayList < Callback < ArrayList<DirectoryOption>>> mCallbacks = new ArrayList<>();
// Should be bounded to UI thread.
private final Handler mHandler = new Handler(ThreadUtils.getUiThreadLooper());
protected final Handler mHandler = new Handler(ThreadUtils.getUiThreadLooper());
private DownloadDirectoryProvider() {
protected DownloadDirectoryProvider() {
registerSDCardReceiver();
}
......
......@@ -1869,6 +1869,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/download/DownloadNotificationService2Test.java",
"javatests/src/org/chromium/chrome/browser/download/DownloadForegroundServiceManagerTest.java",
"javatests/src/org/chromium/chrome/browser/download/DownloadForegroundServiceTest.java",
"javatests/src/org/chromium/chrome/browser/download/DownloadLocationChangeTest.java",
"javatests/src/org/chromium/chrome/browser/download/DownloadTest.java",
"javatests/src/org/chromium/chrome/browser/download/DownloadTestRule.java",
"javatests/src/org/chromium/chrome/browser/download/DownloadUtilsTest.java",
......@@ -1876,6 +1877,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/download/MockDownloadNotificationService2.java",
"javatests/src/org/chromium/chrome/browser/download/OMADownloadHandlerTest.java",
"javatests/src/org/chromium/chrome/browser/download/SystemDownloadNotifier2Test.java",
"javatests/src/org/chromium/chrome/browser/download/TestDownloadDirectoryProvider.java",
"javatests/src/org/chromium/chrome/browser/download/ui/DownloadHistoryAdapterTest.java",
"javatests/src/org/chromium/chrome/browser/download/ui/StubbedProvider.java",
"javatests/src/org/chromium/chrome/browser/engagement/SiteEngagementServiceTest.java",
......
// Copyright 2018 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.download;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.Espresso;
import android.support.test.filters.MediumTest;
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.PathUtils;
import org.chromium.base.StrictModeContext;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.download.DownloadTestRule.CustomMainActivityStart;
import org.chromium.chrome.browser.preferences.PrefServiceBridge;
import org.chromium.chrome.download.R;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.net.test.EmbeddedTestServer;
import java.util.ArrayList;
/**
* Test to verify download location change feature behaviors.
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
public class DownloadLocationChangeTest implements CustomMainActivityStart {
@Rule
public DownloadTestRule mDownloadTestRule = new DownloadTestRule(this);
private EmbeddedTestServer mTestServer;
private static final String TEST_DATA_DIRECTORY = "/chrome/test/data/android/download/";
private static final String TEST_FILE = "test.gzip";
private static final long STORAGE_SIZE = 1024000;
@Before
public void setUp() throws Exception {
mTestServer = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
// Show the location dialog for the first time.
promptDownloadLocationDialog(DownloadPromptStatus.SHOW_INITIAL);
}
@After
public void tearDown() throws Exception {
mTestServer.stopAndDestroyServer();
}
// CustomMainActivityStart implementation.
@Override
public void customMainActivityStart() throws InterruptedException {
mDownloadTestRule.startMainActivityOnBlankPage();
}
/**
* Ensures the default download location dialog is shown to the user with SD card inserted.
* @throws Exception
*/
@Test
@MediumTest
@Feature({"Downloads"})
@Features.EnableFeatures(ChromeFeatureList.DOWNLOADS_LOCATION_CHANGE)
public void testDefaultDialogPositiveButtonClickThrough() throws Exception {
ThreadUtils.runOnUiThreadBlocking(() -> {
Assert.assertEquals(DownloadPromptStatus.SHOW_INITIAL,
PrefServiceBridge.getInstance().getPromptForDownloadAndroid());
simulateDownloadDirectories(true /* hasSDCard */);
// Trigger the download through navigation.
LoadUrlParams params =
new LoadUrlParams(mTestServer.getURL(TEST_DATA_DIRECTORY + TEST_FILE));
mDownloadTestRule.getActivity().getActivityTab().loadUrl(params);
});
// Ensure the dialog is being shown.
CriteriaHelper.pollUiThread(Criteria.equals(
true, () -> mDownloadTestRule.getActivity().getModalDialogManager().isShowing()));
int currentCallCount = mDownloadTestRule.getChromeDownloadCallCount();
// Click the button to start download.
Espresso.onView(withId(R.id.positive_button)).perform(click());
// Ensure download is done.
Assert.assertTrue(mDownloadTestRule.waitForChromeDownloadToFinish(currentCallCount));
mDownloadTestRule.deleteFilesInDownloadDirectory(new String[] {TEST_FILE});
}
/**
* Ensures no default download location dialog is shown to the user without SD card inserted.
* @throws Exception
*/
@Test
@MediumTest
@Feature({"Downloads"})
@Features.EnableFeatures(ChromeFeatureList.DOWNLOADS_LOCATION_CHANGE)
public void testNoDialogWithoutSDCard() throws Exception {
int currentCallCount = mDownloadTestRule.getChromeDownloadCallCount();
ThreadUtils.runOnUiThreadBlocking(() -> {
Assert.assertEquals(DownloadPromptStatus.SHOW_INITIAL,
PrefServiceBridge.getInstance().getPromptForDownloadAndroid());
simulateDownloadDirectories(false /* hasSDCard */);
// Trigger the download through navigation.
LoadUrlParams params =
new LoadUrlParams(mTestServer.getURL(TEST_DATA_DIRECTORY + TEST_FILE));
mDownloadTestRule.getActivity().getActivityTab().loadUrl(params);
});
// Ensure download is done, no download location dialog should show to interact with user.
Assert.assertTrue(mDownloadTestRule.waitForChromeDownloadToFinish(currentCallCount));
mDownloadTestRule.deleteFilesInDownloadDirectory(new String[] {TEST_FILE});
}
/**
* Provides default download directory and SD card directory.
* @param hasSDCard Whether to simulate SD card inserted.
*/
private void simulateDownloadDirectories(boolean hasSDCard) {
ArrayList<DirectoryOption> dirs = new ArrayList<>();
try (StrictModeContext unused = StrictModeContext.allowDiskReads()) {
dirs.add(buildDirectoryOption(DirectoryOption.DownloadLocationDirectoryType.DEFAULT,
PathUtils.getExternalStorageDirectory()));
if (hasSDCard) {
dirs.add(buildDirectoryOption(
DirectoryOption.DownloadLocationDirectoryType.ADDITIONAL,
PathUtils.getDataDirectory()));
}
}
DownloadDirectoryProvider.getInstance().setDirectoryProviderForTesting(
new TestDownloadDirectoryProvider(dirs));
}
private DirectoryOption buildDirectoryOption(
@DirectoryOption.DownloadLocationDirectoryType int type, String directoryPath) {
return new DirectoryOption("Download", directoryPath, STORAGE_SIZE, STORAGE_SIZE, type);
}
private void promptDownloadLocationDialog(@DownloadPromptStatus int promptStatus) {
ThreadUtils.runOnUiThreadBlocking(() -> {
PrefServiceBridge.getInstance().setPromptForDownloadAndroid(promptStatus);
});
}
}
......@@ -170,10 +170,10 @@ public class DownloadTestRule extends ChromeActivityTestRule<ChromeActivity> {
return mHttpDownloadFinished.getCallCount();
}
public boolean waitForChromeDownloadToFinish(int callCount) throws InterruptedException {
public boolean waitForChromeDownloadToFinish(int currentCallCount) throws InterruptedException {
boolean eventReceived = true;
try {
mHttpDownloadFinished.waitForCallback(callCount, 1, 5, TimeUnit.SECONDS);
mHttpDownloadFinished.waitForCallback(currentCallCount, 1, 5, TimeUnit.SECONDS);
} catch (TimeoutException e) {
eventReceived = false;
}
......
// Copyright 2018 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.download;
import org.chromium.base.Callback;
import java.util.ArrayList;
/**
* Used to provide arbitary number of download directories in tests.
*/
public class TestDownloadDirectoryProvider extends DownloadDirectoryProvider {
private ArrayList<DirectoryOption> mDirectoryOptions;
public TestDownloadDirectoryProvider(ArrayList<DirectoryOption> dirs) {
super();
mDirectoryOptions = dirs;
}
// DownloadDirectoryProvider implementation.
@Override
public void getAllDirectoriesOptions(Callback<ArrayList<DirectoryOption>> callback) {
mHandler.post(() -> callback.onResult(mDirectoryOptions));
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment