Commit bb716a7d authored by Min Qin's avatar Min Qin Committed by Commit Bot

Fix a disabled download test

This is a reland of the previous CL. The previous CL was reverted as
there are still some other info bars showing up and messing with the
duplicate info bar. So this reland just searches for the duplicate
infobar, rather than waiting for 1 info bar to appear.

There are several issues with this download test:
1. The new download progress info bar will mess up with the duplicate
infobar when trying to find the latter.
2. Download update may never go through DownloadManagerService in
download's new code paths
3. Only 1 duplicate infobar will show at a time, so the
AllowMultipleInfoBars test is no longer valid

This CL fixes the above 3 issues.

BUG=1128665,597230

Change-Id: I3e3dcc818e9ae6ed2a4b1218bc1f7a38bddd94b1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2412989
Commit-Queue: Min Qin <qinmin@chromium.org>
Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808565}
parent 3c55ad62
...@@ -35,6 +35,7 @@ import org.chromium.chrome.browser.compositor.layouts.StaticLayout; ...@@ -35,6 +35,7 @@ import org.chromium.chrome.browser.compositor.layouts.StaticLayout;
import org.chromium.chrome.browser.download.DownloadTestRule.CustomMainActivityStart; import org.chromium.chrome.browser.download.DownloadTestRule.CustomMainActivityStart;
import org.chromium.chrome.browser.flags.ChromeFeatureList; import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches; import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.infobar.DuplicateDownloadInfoBar;
import org.chromium.chrome.browser.infobar.InfoBarContainer; import org.chromium.chrome.browser.infobar.InfoBarContainer;
import org.chromium.chrome.browser.tab.Tab; import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tabmodel.TabModel; import org.chromium.chrome.browser.tabmodel.TabModel;
...@@ -43,6 +44,8 @@ import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate; ...@@ -43,6 +44,8 @@ import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
import org.chromium.chrome.test.util.ChromeTabUtils; import org.chromium.chrome.test.util.ChromeTabUtils;
import org.chromium.chrome.test.util.InfoBarUtil; import org.chromium.chrome.test.util.InfoBarUtil;
import org.chromium.chrome.test.util.browser.Features; import org.chromium.chrome.test.util.browser.Features;
import org.chromium.components.download.DownloadState;
import org.chromium.components.infobars.InfoBar;
import org.chromium.content_public.browser.test.util.Criteria; import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper; import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.DOMUtils; import org.chromium.content_public.browser.test.util.DOMUtils;
...@@ -54,7 +57,9 @@ import org.chromium.net.test.util.TestWebServer; ...@@ -54,7 +57,9 @@ import org.chromium.net.test.util.TestWebServer;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* Tests Chrome download feature by attempting to download some files. * Tests Chrome download feature by attempting to download some files.
...@@ -105,6 +110,18 @@ import java.util.List; ...@@ -105,6 +110,18 @@ import java.util.List;
} }
} }
static class TestDownloadInfoBarController extends DownloadInfoBarController {
public TestDownloadInfoBarController() {
super(false);
}
@Override
protected void showInfoBar(
@DownloadInfoBarState int state, DownloadProgressInfoBarData info) {
// Do nothing, so we don't impact other info bars.
}
}
public DownloadTest(boolean useDownloadOfflineContentProvider) { public DownloadTest(boolean useDownloadOfflineContentProvider) {
mUseDownloadOfflineContentProvider = useDownloadOfflineContentProvider; mUseDownloadOfflineContentProvider = useDownloadOfflineContentProvider;
} }
...@@ -131,6 +148,15 @@ import java.util.List; ...@@ -131,6 +148,15 @@ import java.util.List;
mDownloadTestRule.startMainActivityOnBlankPage(); mDownloadTestRule.startMainActivityOnBlankPage();
} }
void waitForLastDownloadToFinish() {
CriteriaHelper.pollUiThread(() -> {
List<DownloadItem> downloads = mDownloadTestRule.getAllDownloads();
Criteria.checkThat(downloads.size(), Matchers.greaterThanOrEqualTo(1));
Criteria.checkThat(downloads.get(downloads.size() - 1).getDownloadInfo().state(),
Matchers.is(DownloadState.COMPLETE));
});
}
@Test @Test
@MediumTest @MediumTest
@Feature({"Downloads"}) @Feature({"Downloads"})
...@@ -235,28 +261,45 @@ import java.util.List; ...@@ -235,28 +261,45 @@ import java.util.List;
@Test @Test
@MediumTest @MediumTest
@Feature({"Downloads"}) @Feature({"Downloads"})
@DisabledTest(message = "crbug.com/597230")
public void testDuplicateHttpPostDownload_Cancel() { public void testDuplicateHttpPostDownload_Cancel() {
// Remove download progress info bar.
TestThreadUtils.runOnUiThreadBlocking(
()
-> DownloadManagerService.getDownloadManagerService()
.setInfoBarControllerForTesting(
new TestDownloadInfoBarController()));
// Download a file. // Download a file.
mDownloadTestRule.loadUrl(mTestServer.getURL(TEST_DOWNLOAD_DIRECTORY + "post.html")); mDownloadTestRule.loadUrl(mTestServer.getURL(TEST_DOWNLOAD_DIRECTORY + "post.html"));
waitForFocus(); waitForFocus();
View currentView = mDownloadTestRule.getActivity().getActivityTab().getView(); View currentView = mDownloadTestRule.getActivity().getActivityTab().getView();
int callCount = mDownloadTestRule.getChromeDownloadCallCount();
TouchCommon.singleClickView(currentView); TouchCommon.singleClickView(currentView);
Assert.assertTrue("Failed to finish downloading file for the first time.", waitForLastDownloadToFinish();
mDownloadTestRule.waitForChromeDownloadToFinish(callCount)); int downloadCount = mDownloadTestRule.getAllDownloads().size();
// Download a file with the same name. // Download a file with the same name.
mDownloadTestRule.loadUrl(mTestServer.getURL(TEST_DOWNLOAD_DIRECTORY + "post.html")); mDownloadTestRule.loadUrl(mTestServer.getURL(TEST_DOWNLOAD_DIRECTORY + "post.html"));
waitForFocus(); waitForFocus();
currentView = mDownloadTestRule.getActivity().getActivityTab().getView(); currentView = mDownloadTestRule.getActivity().getActivityTab().getView();
callCount = mDownloadTestRule.getChromeDownloadCallCount();
TouchCommon.singleClickView(currentView); TouchCommon.singleClickView(currentView);
assertPollForInfoBarSize(1); CriteriaHelper.pollUiThread(() -> {
Assert.assertTrue("CREATE NEW button wasn't found", InfoBar infobar = findDuplicateDownloadInfoBar();
InfoBarUtil.clickSecondaryButton(mDownloadTestRule.getInfoBars().get(0))); Criteria.checkThat(infobar, Matchers.notNullValue());
Assert.assertFalse("Download should not happen when clicking cancel button", Criteria.checkThat(
mDownloadTestRule.waitForChromeDownloadToFinish(callCount)); mDownloadTestRule.getInfoBarContainer().isAnimating(), Matchers.is(false));
});
Assert.assertTrue("CANCEL button wasn't found",
InfoBarUtil.clickSecondaryButton(findDuplicateDownloadInfoBar()));
// The download should be canceled.
List<DownloadItem> downloads = mDownloadTestRule.getAllDownloads();
Assert.assertEquals(downloads.size(), downloadCount + 1);
Set<Integer> states = new HashSet<>(
Arrays.asList(downloads.get(downloads.size() - 1).getDownloadInfo().state(),
downloads.get(downloads.size() - 2).getDownloadInfo().state()));
Assert.assertEquals(states,
new HashSet<>(Arrays.asList(DownloadState.COMPLETE, DownloadState.CANCELLED)));
} }
@Test @Test
...@@ -291,56 +334,6 @@ import java.util.List; ...@@ -291,56 +334,6 @@ import java.util.List;
mDownloadTestRule.hasDownload(FILENAME_TEXT_1, SUPERBO_CONTENTS)); mDownloadTestRule.hasDownload(FILENAME_TEXT_1, SUPERBO_CONTENTS));
} }
@Test
@MediumTest
@Feature({"Downloads"})
@DisabledTest(message = "crbug.com/597230")
public void testDuplicateHttpPostDownload_AllowMultipleInfoBars() throws Exception {
Assert.assertFalse(mDownloadTestRule.hasDownload(FILENAME_TEXT, SUPERBO_CONTENTS));
// Download a file.
mDownloadTestRule.loadUrl(mTestServer.getURL(TEST_DOWNLOAD_DIRECTORY + "post.html"));
waitForFocus();
View currentView = mDownloadTestRule.getActivity().getActivityTab().getView();
int callCount = mDownloadTestRule.getChromeDownloadCallCount();
TouchCommon.singleClickView(currentView);
Assert.assertTrue("Failed to finish downloading file for the first time.",
mDownloadTestRule.waitForChromeDownloadToFinish(callCount));
// Download the file for the second time.
mDownloadTestRule.loadUrl(mTestServer.getURL(TEST_DOWNLOAD_DIRECTORY + "post.html"));
waitForFocus();
currentView = mDownloadTestRule.getActivity().getActivityTab().getView();
TouchCommon.singleClickView(currentView);
assertPollForInfoBarSize(1);
// Download the file for the third time.
mDownloadTestRule.loadUrl(mTestServer.getURL(TEST_DOWNLOAD_DIRECTORY + "post.html"));
waitForFocus();
currentView = mDownloadTestRule.getActivity().getActivityTab().getView();
TouchCommon.singleClickView(currentView);
assertPollForInfoBarSize(2);
// Now create two new files by clicking on the infobars.
callCount = mDownloadTestRule.getChromeDownloadCallCount();
Assert.assertTrue("CREATE NEW button wasn't found",
InfoBarUtil.clickSecondaryButton(mDownloadTestRule.getInfoBars().get(0)));
Assert.assertTrue("Failed to finish downloading the second file.",
mDownloadTestRule.waitForChromeDownloadToFinish(callCount));
assertPollForInfoBarSize(1);
callCount = mDownloadTestRule.getChromeDownloadCallCount();
Assert.assertTrue("CREATE NEW button wasn't found",
InfoBarUtil.clickSecondaryButton(mDownloadTestRule.getInfoBars().get(0)));
Assert.assertTrue("Failed to finish downloading the third file.",
mDownloadTestRule.waitForChromeDownloadToFinish(callCount));
Assert.assertTrue("Missing first download",
mDownloadTestRule.hasDownload(FILENAME_TEXT, SUPERBO_CONTENTS));
Assert.assertTrue("Missing second download",
mDownloadTestRule.hasDownload(FILENAME_TEXT_1, SUPERBO_CONTENTS));
Assert.assertTrue("Missing third download",
mDownloadTestRule.hasDownload(FILENAME_TEXT_2, SUPERBO_CONTENTS));
}
private void goToLastTab() { private void goToLastTab() {
final TabModel model = mDownloadTestRule.getActivity().getCurrentTabModel(); final TabModel model = mDownloadTestRule.getActivity().getCurrentTabModel();
final int count = model.getCount(); final int count = model.getCount();
...@@ -480,6 +473,20 @@ import java.util.List; ...@@ -480,6 +473,20 @@ import java.util.List;
}); });
} }
/**
* Get the duplicate download info bar if it exists, or null otherwise.
* @return Duplicate download info bar if it is being displayed.
*/
private InfoBar findDuplicateDownloadInfoBar() {
List<InfoBar> infoBars = mDownloadTestRule.getInfoBars();
for (InfoBar infoBar : infoBars) {
if (infoBar instanceof DuplicateDownloadInfoBar) {
return infoBar;
}
}
return null;
}
/** /**
* Makes sure there are no files with names identical to the ones this test uses in the * Makes sure there are no files with names identical to the ones this test uses in the
* downloads directory * downloads directory
......
...@@ -51,6 +51,7 @@ public class DownloadTestRule extends ChromeActivityTestRule<ChromeActivity> { ...@@ -51,6 +51,7 @@ public class DownloadTestRule extends ChromeActivityTestRule<ChromeActivity> {
public static final long UPDATE_DELAY_MILLIS = 1000; public static final long UPDATE_DELAY_MILLIS = 1000;
private final CustomMainActivityStart mActivityStart; private final CustomMainActivityStart mActivityStart;
private List<DownloadItem> mAllDownloads;
public DownloadTestRule(CustomMainActivityStart action) { public DownloadTestRule(CustomMainActivityStart action) {
super(ChromeActivity.class); super(ChromeActivity.class);
...@@ -186,10 +187,18 @@ public class DownloadTestRule extends ChromeActivityTestRule<ChromeActivity> { ...@@ -186,10 +187,18 @@ public class DownloadTestRule extends ChromeActivityTestRule<ChromeActivity> {
return eventReceived; return eventReceived;
} }
public List<DownloadItem> getAllDownloads() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
DownloadManagerService.getDownloadManagerService().getAllDownloads(false);
});
return mAllDownloads;
}
private class TestDownloadManagerServiceObserver private class TestDownloadManagerServiceObserver
implements DownloadManagerService.DownloadObserver { implements DownloadManagerService.DownloadObserver {
@Override @Override
public void onAllDownloadsRetrieved(final List<DownloadItem> list, boolean isOffTheRecord) { public void onAllDownloadsRetrieved(final List<DownloadItem> list, boolean isOffTheRecord) {
mAllDownloads = list;
} }
@Override @Override
......
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