Commit c9a5a0c4 authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Download later: Add more UI tests for download later dialog.

This CL adds a few more test cases for download later dialog. Also
fixed a minor bug that the button text is certain scenario should be
"Next" instead of "Download".

Bug: 1115241
Change-Id: I957b7e1efc726b133c746e75f4c148511be43dc7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2350192Reviewed-by: default avatarHesen Zhang <hesen@chromium.org>
Commit-Queue: Xing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#797489}
parent 094453bd
......@@ -7,9 +7,11 @@ package org.chromium.chrome.browser.download.dialogs;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.not;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
......@@ -21,6 +23,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.view.View;
import android.widget.CheckBox;
import androidx.test.espresso.NoMatchingViewException;
import androidx.test.filters.MediumTest;
......@@ -88,20 +91,33 @@ public class DownloadLaterDialogTest {
mActivityTestRule.startMainActivityOnBlankPage();
mDialogCoordinator = new DownloadLaterDialogCoordinator(mDateTimePicker);
mModel = new PropertyModel.Builder(DownloadLaterDialogProperties.ALL_KEYS)
.with(DownloadLaterDialogProperties.CONTROLLER, mDialogCoordinator)
.with(DownloadLaterDialogProperties.INITIAL_CHOICE,
DownloadLaterDialogChoice.ON_WIFI)
.with(DownloadLaterDialogProperties.DONT_SHOW_AGAIN_SELECTION,
DownloadLaterPromptStatus.SHOW_INITIAL)
.build();
mModel = createModel(
DownloadLaterDialogChoice.ON_WIFI, DownloadLaterPromptStatus.SHOW_INITIAL);
Assert.assertNotNull(mController);
mDialogCoordinator.initialize(mController);
}
private PropertyModel createModel(Integer choice, Integer promptStatus) {
PropertyModel.Builder builder =
new PropertyModel.Builder(DownloadLaterDialogProperties.ALL_KEYS)
.with(DownloadLaterDialogProperties.CONTROLLER, mDialogCoordinator);
if (choice != null) {
builder.with(DownloadLaterDialogProperties.INITIAL_CHOICE, choice);
}
if (promptStatus != null) {
builder.with(DownloadLaterDialogProperties.DONT_SHOW_AGAIN_SELECTION, promptStatus);
}
return builder.build();
}
private void showDialog() {
mDialogCoordinator.showDialog(
mActivityTestRule.getActivity(), getModalDialogManager(), mPrefService, mModel);
TestThreadUtils.runOnUiThreadBlocking(() -> {
mDialogCoordinator.showDialog(
mActivityTestRule.getActivity(), getModalDialogManager(), mPrefService, mModel);
});
}
private void clickPositiveButton() {
......@@ -112,23 +128,87 @@ public class DownloadLaterDialogTest {
onView(withId(org.chromium.chrome.R.id.negative_button)).perform(click());
}
private void assertPositiveButtonText(String expectedText) {
onView(withId(org.chromium.chrome.R.id.positive_button))
.check(matches(withText(expectedText)));
}
private void assertShowAgainCheckBox(boolean enabled, int visibility, boolean checked) {
onView(withId(R.id.show_again_checkbox)).check((View view, NoMatchingViewException e) -> {
Assert.assertEquals(enabled, view.isEnabled());
Assert.assertEquals(visibility, view.getVisibility());
if (visibility == View.VISIBLE) {
Assert.assertEquals(checked, ((CheckBox) (view)).isChecked());
}
});
}
private void assertEditText(boolean hasEditText) {
if (hasEditText) {
onView(withId(R.id.edit_location)).check(matches(isDisplayed()));
} else {
onView(withId(R.id.edit_location)).check(matches(not(isDisplayed())));
}
}
@Test
@MediumTest
public void testShowDialogThenDismiss() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
showDialog();
});
public void testInitialSelectionDownloadNowWithOutCheckbox() {
mModel = createModel(DownloadLaterDialogChoice.DOWNLOAD_NOW, null);
showDialog();
assertPositiveButtonText("Download");
assertShowAgainCheckBox(true, View.GONE, true);
assertEditText(false);
}
@Test
@MediumTest
public void testInitialSelectionOnWifiWithCheckbox() {
mModel = createModel(
DownloadLaterDialogChoice.ON_WIFI, DownloadLaterPromptStatus.SHOW_INITIAL);
showDialog();
assertPositiveButtonText("Download");
assertShowAgainCheckBox(true, View.VISIBLE, true);
assertEditText(false);
}
@Test
@MediumTest
public void testInitialSelectionOnWifiWithEditLocation() {
mModel = createModel(
DownloadLaterDialogChoice.ON_WIFI, DownloadLaterPromptStatus.SHOW_PREFERENCE);
mModel.set(DownloadLaterDialogProperties.LOCATION_TEXT, "location");
showDialog();
assertPositiveButtonText("Download");
assertShowAgainCheckBox(true, View.VISIBLE, false);
assertEditText(true);
}
@Test
@MediumTest
public void testInitialSelectionDownloadLater() {
mModel = createModel(
DownloadLaterDialogChoice.DOWNLOAD_LATER, DownloadLaterPromptStatus.SHOW_INITIAL);
showDialog();
assertPositiveButtonText("Next");
assertShowAgainCheckBox(false, View.VISIBLE, true);
assertEditText(false);
}
@Test
@MediumTest
public void testClickNegativeButtonShouldCancel() {
showDialog();
clickNegativeButton();
verify(mController).onDownloadLaterDialogCanceled();
}
@Test
@MediumTest
public void testSelectRadioButton() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
showDialog();
public void testSelectFromOnWifiToDownloadNow() {
showDialog();
TestThreadUtils.runOnUiThreadBlocking(() -> {
// Verify the initial selection of the dialog. The controller should not get an event
// for the initial setup.
RadioButtonWithDescription onWifiButton =
......@@ -153,10 +233,10 @@ public class DownloadLaterDialogTest {
@Test
@MediumTest
public void testSelectDownloadLater() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
showDialog();
public void testSelectFromOnWifiToDownloadLater() {
showDialog();
TestThreadUtils.runOnUiThreadBlocking(() -> {
RadioButtonWithDescription downloadLaterButton =
getDownloadLaterDialogView().findViewById(R.id.choose_date_time);
Assert.assertNotNull(downloadLaterButton);
......@@ -164,10 +244,8 @@ public class DownloadLaterDialogTest {
getDownloadLaterDialogView().onCheckedChanged(null, -1);
});
onView(withId(org.chromium.chrome.R.id.positive_button)).check(matches(withText("Next")));
onView(withId(R.id.show_again_checkbox)).check((View view, NoMatchingViewException e) -> {
Assert.assertFalse(view.isEnabled());
});
assertPositiveButtonText("Next");
assertShowAgainCheckBox(false, View.VISIBLE, true);
clickPositiveButton();
verify(mController, times(0)).onDownloadLaterDialogComplete(anyInt(), anyLong());
......
......@@ -70,6 +70,7 @@ public class DownloadLaterDialogCoordinator implements ModalDialogProperties.Con
* @param prefService {@link PrefService} to write download later prompt status preference.
* @param model The data model that defines the UI details.
*/
// TODO(xingliu): The public showDialog API should use a param instead of exposing the model.
public void showDialog(Context context, ModalDialogManager modalDialogManager,
PrefService prefService, PropertyModel model) {
if (context == null || modalDialogManager == null) {
......@@ -88,10 +89,13 @@ public class DownloadLaterDialogCoordinator implements ModalDialogProperties.Con
mPropertyModelChangeProcessor =
PropertyModelChangeProcessor.create(mDownloadLaterDialogModel, mCustomView,
DownloadLaterDialogView.Binder::bind, true /*performInitialBind*/);
mDownloadLaterChoice = model.get(DownloadLaterDialogProperties.INITIAL_CHOICE);
// Set up the modal dialog.
mDialogModel = getModalDialogModel(context, this);
// Adjust models based on initial choice.
onChoiceChanged(model.get(DownloadLaterDialogProperties.INITIAL_CHOICE));
mModalDialogManager.showDialog(mDialogModel, ModalDialogManager.ModalDialogType.APP);
}
......@@ -138,8 +142,6 @@ public class DownloadLaterDialogCoordinator implements ModalDialogProperties.Con
}
private void onPositiveButtonClicked(@DownloadLaterDialogChoice int choice) {
mDownloadLaterChoice = choice;
// Immediately show the date time picker when selecting the "Download later".
if (choice == DownloadLaterDialogChoice.DOWNLOAD_LATER) {
dismissDialog(DialogDismissalCause.ACTION_ON_CONTENT);
......@@ -244,6 +246,10 @@ public class DownloadLaterDialogCoordinator implements ModalDialogProperties.Con
@Override
public void onCheckedChanged(@DownloadLaterDialogChoice int choice) {
onChoiceChanged(choice);
}
private void onChoiceChanged(@DownloadLaterDialogChoice int choice) {
@DownloadLaterDialogChoice
int previousChoice = mDownloadLaterChoice;
mDownloadLaterChoice = choice;
......
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