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; ...@@ -7,9 +7,11 @@ package org.chromium.chrome.browser.download.dialogs;
import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches; 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.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText; 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.any;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong;
...@@ -21,6 +23,7 @@ import static org.mockito.Mockito.verify; ...@@ -21,6 +23,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.view.View; import android.view.View;
import android.widget.CheckBox;
import androidx.test.espresso.NoMatchingViewException; import androidx.test.espresso.NoMatchingViewException;
import androidx.test.filters.MediumTest; import androidx.test.filters.MediumTest;
...@@ -88,20 +91,33 @@ public class DownloadLaterDialogTest { ...@@ -88,20 +91,33 @@ public class DownloadLaterDialogTest {
mActivityTestRule.startMainActivityOnBlankPage(); mActivityTestRule.startMainActivityOnBlankPage();
mDialogCoordinator = new DownloadLaterDialogCoordinator(mDateTimePicker); mDialogCoordinator = new DownloadLaterDialogCoordinator(mDateTimePicker);
mModel = new PropertyModel.Builder(DownloadLaterDialogProperties.ALL_KEYS) mModel = createModel(
.with(DownloadLaterDialogProperties.CONTROLLER, mDialogCoordinator) DownloadLaterDialogChoice.ON_WIFI, DownloadLaterPromptStatus.SHOW_INITIAL);
.with(DownloadLaterDialogProperties.INITIAL_CHOICE,
DownloadLaterDialogChoice.ON_WIFI)
.with(DownloadLaterDialogProperties.DONT_SHOW_AGAIN_SELECTION,
DownloadLaterPromptStatus.SHOW_INITIAL)
.build();
Assert.assertNotNull(mController); Assert.assertNotNull(mController);
mDialogCoordinator.initialize(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() { private void showDialog() {
mDialogCoordinator.showDialog( TestThreadUtils.runOnUiThreadBlocking(() -> {
mActivityTestRule.getActivity(), getModalDialogManager(), mPrefService, mModel); mDialogCoordinator.showDialog(
mActivityTestRule.getActivity(), getModalDialogManager(), mPrefService, mModel);
});
} }
private void clickPositiveButton() { private void clickPositiveButton() {
...@@ -112,23 +128,87 @@ public class DownloadLaterDialogTest { ...@@ -112,23 +128,87 @@ public class DownloadLaterDialogTest {
onView(withId(org.chromium.chrome.R.id.negative_button)).perform(click()); 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 @Test
@MediumTest @MediumTest
public void testShowDialogThenDismiss() { public void testInitialSelectionDownloadNowWithOutCheckbox() {
TestThreadUtils.runOnUiThreadBlocking(() -> { mModel = createModel(DownloadLaterDialogChoice.DOWNLOAD_NOW, null);
showDialog(); 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(); clickNegativeButton();
verify(mController).onDownloadLaterDialogCanceled(); verify(mController).onDownloadLaterDialogCanceled();
} }
@Test @Test
@MediumTest @MediumTest
public void testSelectRadioButton() { public void testSelectFromOnWifiToDownloadNow() {
TestThreadUtils.runOnUiThreadBlocking(() -> { showDialog();
showDialog();
TestThreadUtils.runOnUiThreadBlocking(() -> {
// Verify the initial selection of the dialog. The controller should not get an event // Verify the initial selection of the dialog. The controller should not get an event
// for the initial setup. // for the initial setup.
RadioButtonWithDescription onWifiButton = RadioButtonWithDescription onWifiButton =
...@@ -153,10 +233,10 @@ public class DownloadLaterDialogTest { ...@@ -153,10 +233,10 @@ public class DownloadLaterDialogTest {
@Test @Test
@MediumTest @MediumTest
public void testSelectDownloadLater() { public void testSelectFromOnWifiToDownloadLater() {
TestThreadUtils.runOnUiThreadBlocking(() -> { showDialog();
showDialog();
TestThreadUtils.runOnUiThreadBlocking(() -> {
RadioButtonWithDescription downloadLaterButton = RadioButtonWithDescription downloadLaterButton =
getDownloadLaterDialogView().findViewById(R.id.choose_date_time); getDownloadLaterDialogView().findViewById(R.id.choose_date_time);
Assert.assertNotNull(downloadLaterButton); Assert.assertNotNull(downloadLaterButton);
...@@ -164,10 +244,8 @@ public class DownloadLaterDialogTest { ...@@ -164,10 +244,8 @@ public class DownloadLaterDialogTest {
getDownloadLaterDialogView().onCheckedChanged(null, -1); getDownloadLaterDialogView().onCheckedChanged(null, -1);
}); });
onView(withId(org.chromium.chrome.R.id.positive_button)).check(matches(withText("Next"))); assertPositiveButtonText("Next");
onView(withId(R.id.show_again_checkbox)).check((View view, NoMatchingViewException e) -> { assertShowAgainCheckBox(false, View.VISIBLE, true);
Assert.assertFalse(view.isEnabled());
});
clickPositiveButton(); clickPositiveButton();
verify(mController, times(0)).onDownloadLaterDialogComplete(anyInt(), anyLong()); verify(mController, times(0)).onDownloadLaterDialogComplete(anyInt(), anyLong());
......
...@@ -70,6 +70,7 @@ public class DownloadLaterDialogCoordinator implements ModalDialogProperties.Con ...@@ -70,6 +70,7 @@ public class DownloadLaterDialogCoordinator implements ModalDialogProperties.Con
* @param prefService {@link PrefService} to write download later prompt status preference. * @param prefService {@link PrefService} to write download later prompt status preference.
* @param model The data model that defines the UI details. * @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, public void showDialog(Context context, ModalDialogManager modalDialogManager,
PrefService prefService, PropertyModel model) { PrefService prefService, PropertyModel model) {
if (context == null || modalDialogManager == null) { if (context == null || modalDialogManager == null) {
...@@ -88,10 +89,13 @@ public class DownloadLaterDialogCoordinator implements ModalDialogProperties.Con ...@@ -88,10 +89,13 @@ public class DownloadLaterDialogCoordinator implements ModalDialogProperties.Con
mPropertyModelChangeProcessor = mPropertyModelChangeProcessor =
PropertyModelChangeProcessor.create(mDownloadLaterDialogModel, mCustomView, PropertyModelChangeProcessor.create(mDownloadLaterDialogModel, mCustomView,
DownloadLaterDialogView.Binder::bind, true /*performInitialBind*/); DownloadLaterDialogView.Binder::bind, true /*performInitialBind*/);
mDownloadLaterChoice = model.get(DownloadLaterDialogProperties.INITIAL_CHOICE);
// Set up the modal dialog. // Set up the modal dialog.
mDialogModel = getModalDialogModel(context, this); mDialogModel = getModalDialogModel(context, this);
// Adjust models based on initial choice.
onChoiceChanged(model.get(DownloadLaterDialogProperties.INITIAL_CHOICE));
mModalDialogManager.showDialog(mDialogModel, ModalDialogManager.ModalDialogType.APP); mModalDialogManager.showDialog(mDialogModel, ModalDialogManager.ModalDialogType.APP);
} }
...@@ -138,8 +142,6 @@ public class DownloadLaterDialogCoordinator implements ModalDialogProperties.Con ...@@ -138,8 +142,6 @@ public class DownloadLaterDialogCoordinator implements ModalDialogProperties.Con
} }
private void onPositiveButtonClicked(@DownloadLaterDialogChoice int choice) { private void onPositiveButtonClicked(@DownloadLaterDialogChoice int choice) {
mDownloadLaterChoice = choice;
// Immediately show the date time picker when selecting the "Download later". // Immediately show the date time picker when selecting the "Download later".
if (choice == DownloadLaterDialogChoice.DOWNLOAD_LATER) { if (choice == DownloadLaterDialogChoice.DOWNLOAD_LATER) {
dismissDialog(DialogDismissalCause.ACTION_ON_CONTENT); dismissDialog(DialogDismissalCause.ACTION_ON_CONTENT);
...@@ -244,6 +246,10 @@ public class DownloadLaterDialogCoordinator implements ModalDialogProperties.Con ...@@ -244,6 +246,10 @@ public class DownloadLaterDialogCoordinator implements ModalDialogProperties.Con
@Override @Override
public void onCheckedChanged(@DownloadLaterDialogChoice int choice) { public void onCheckedChanged(@DownloadLaterDialogChoice int choice) {
onChoiceChanged(choice);
}
private void onChoiceChanged(@DownloadLaterDialogChoice int choice) {
@DownloadLaterDialogChoice @DownloadLaterDialogChoice
int previousChoice = mDownloadLaterChoice; int previousChoice = mDownloadLaterChoice;
mDownloadLaterChoice = choice; 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