Commit f98f2cf9 authored by Hung Vu's avatar Hung Vu Committed by Commit Bot

Add DownloadDialogUtils unit tests.

This CL handles unit test for shouldSuggestDownloadLocation in DownloadDialogUtils.

Change-Id: I39442ffe032550631c8f47f6ecc49529b489903c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2446518
Commit-Queue: Hung Vu <vuhung@google.com>
Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813418}
parent 5ab0c988
......@@ -144,6 +144,7 @@ android_library("junit_tests") {
sources = [
"java/src/org/chromium/chrome/browser/download/DownloadDialogBridgeUnitTest.java",
"java/src/org/chromium/chrome/browser/download/DownloadLaterMetricsUnitTest.java",
"java/src/org/chromium/chrome/browser/download/dialogs/DownloadDialogUtilsUnitTest.java",
"java/src/org/chromium/chrome/browser/download/dialogs/DownloadLaterDialogHelperUnitTest.java",
]
......
......@@ -111,7 +111,8 @@ public class DownloadDialogBridge
@DownloadLocationDialogType
int suggestedDialogType = dialogType;
if (ChromeFeatureList.isEnabled(ChromeFeatureList.SMART_SUGGESTION_FOR_LARGE_DOWNLOADS)
&& DownloadDialogUtils.shouldSuggestDownloadLocation(dirs, totalBytes)) {
&& DownloadDialogUtils.shouldSuggestDownloadLocation(
dirs, getDownloadDefaultDirectory(), totalBytes)) {
suggestedDialogType = DownloadLocationDialogType.LOCATION_SUGGESTION;
}
......
......@@ -5,7 +5,6 @@
package org.chromium.chrome.browser.download.dialogs;
import org.chromium.chrome.browser.download.DirectoryOption;
import org.chromium.chrome.browser.download.DownloadDialogBridge;
import org.chromium.ui.modelutil.PropertyModel;
import org.chromium.ui.modelutil.PropertyModel.ReadableObjectPropertyKey;
......@@ -36,11 +35,10 @@ public class DownloadDialogUtils {
* @param totalBytes The download size.
*/
public static boolean shouldSuggestDownloadLocation(
ArrayList<DirectoryOption> dirs, long totalBytes) {
ArrayList<DirectoryOption> dirs, String defaultLocation, long totalBytes) {
// Return false if totalBytes is unknown.
if (totalBytes <= 0) return false;
String defaultLocation = DownloadDialogBridge.getDownloadDefaultDirectory();
boolean shouldSuggestDownloadLocation = false;
for (DirectoryOption dir : dirs) {
double spaceLeft = (double) (dir.availableSpace - totalBytes) / dir.totalSpace;
......
// Copyright 2020 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.dialogs;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.chromium.chrome.browser.download.DirectoryOption;
import org.chromium.testing.local.LocalRobolectricTestRunner;
import java.util.ArrayList;
/**
* Unit test for {@link DownloadDialogUtils}.
*/
@RunWith(LocalRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class DownloadDialogUtilsUnitTest {
private final DirectoryOption mInternalSmallOption = new DirectoryOption(
"/internal", 301, 1000, DirectoryOption.DownloadLocationDirectoryType.DEFAULT);
private final DirectoryOption mInternalLargeOption = new DirectoryOption(
"/internal", 600, 1000, DirectoryOption.DownloadLocationDirectoryType.DEFAULT);
private final DirectoryOption mExternalSmallOption = new DirectoryOption(
"/sd_card", 100, 2000, DirectoryOption.DownloadLocationDirectoryType.ADDITIONAL);
private final DirectoryOption mExternalLargeOption = new DirectoryOption(
"/sd_card", 1000, 2000, DirectoryOption.DownloadLocationDirectoryType.ADDITIONAL);
private final String mDefaultLocation = "/internal";
@Test
public void testShouldSuggestDownloadLocation_DefaultEnoughSpace() {
ArrayList<DirectoryOption> dirs = new ArrayList<>();
dirs.add(mInternalLargeOption);
dirs.add(mExternalLargeOption);
assertFalse(DownloadDialogUtils.shouldSuggestDownloadLocation(dirs, mDefaultLocation, 300));
}
@Test
public void testShouldSuggestDownloadLocation_UnknownBytes() {
ArrayList<DirectoryOption> dirs = new ArrayList<>();
dirs.add(mInternalLargeOption);
dirs.add(mExternalLargeOption);
assertFalse(DownloadDialogUtils.shouldSuggestDownloadLocation(dirs, mDefaultLocation, 0));
}
@Test
public void testShouldSuggestDownloadLocation_SuggestExternal() {
ArrayList<DirectoryOption> dirs = new ArrayList<>();
dirs.add(mInternalSmallOption);
dirs.add(mExternalLargeOption);
assertTrue(DownloadDialogUtils.shouldSuggestDownloadLocation(dirs, mDefaultLocation, 300));
}
@Test
public void testShouldSuggestDownloadLocation_BothNotEnoughSPace() {
ArrayList<DirectoryOption> dirs = new ArrayList<>();
dirs.add(mInternalSmallOption);
dirs.add(mExternalSmallOption);
assertFalse(DownloadDialogUtils.shouldSuggestDownloadLocation(dirs, mDefaultLocation, 300));
}
@Test
public void testShouldSuggestDownloadLocation_NoAvailableStorage() {
ArrayList<DirectoryOption> dirs = new ArrayList<>();
assertFalse(DownloadDialogUtils.shouldSuggestDownloadLocation(dirs, mDefaultLocation, 300));
}
@Test
public void testShouldSuggestDownloadLocation_NoExternalStorage() {
ArrayList<DirectoryOption> dirs = new ArrayList<>();
dirs.add(mInternalLargeOption);
assertFalse(DownloadDialogUtils.shouldSuggestDownloadLocation(dirs, mDefaultLocation, 300));
}
}
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