| Index: chrome/android/javatests/src/org/chromium/chrome/browser/GeolocationTest.java
|
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/GeolocationTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/GeolocationTest.java
|
| index 0c4dfc763b7707e4de6595bb02ab078052168886..03c85d95ab2fe7f365ebaa9cd31411af857cf8d5 100644
|
| --- a/chrome/android/javatests/src/org/chromium/chrome/browser/GeolocationTest.java
|
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/GeolocationTest.java
|
| @@ -4,10 +4,13 @@
|
|
|
| package org.chromium.chrome.browser;
|
|
|
| +import android.content.DialogInterface;
|
| +import android.support.v7.app.AlertDialog;
|
| import android.support.v7.widget.SwitchCompat;
|
| import android.test.suitebuilder.annotation.MediumTest;
|
| import android.test.suitebuilder.annotation.Smoke;
|
|
|
| +import org.chromium.base.ThreadUtils;
|
| import org.chromium.base.test.util.CallbackHelper;
|
| import org.chromium.base.test.util.CommandLineFlags;
|
| import org.chromium.base.test.util.Feature;
|
| @@ -15,6 +18,7 @@ import org.chromium.base.test.util.RetryOnFailure;
|
| import org.chromium.chrome.R;
|
| import org.chromium.chrome.browser.infobar.InfoBar;
|
| import org.chromium.chrome.browser.infobar.InfoBarContainer;
|
| +import org.chromium.chrome.browser.permissions.PermissionDialogController;
|
| import org.chromium.chrome.browser.tab.EmptyTabObserver;
|
| import org.chromium.chrome.browser.tab.Tab;
|
| import org.chromium.chrome.test.ChromeActivityTestCaseBase;
|
| @@ -29,6 +33,7 @@ import org.chromium.device.geolocation.MockLocationProvider;
|
| import org.chromium.net.test.EmbeddedTestServer;
|
|
|
| import java.util.concurrent.Callable;
|
| +import java.util.concurrent.ExecutionException;
|
|
|
| /**
|
| * Test suite for Geo-Location functionality.
|
| @@ -45,6 +50,10 @@ public class GeolocationTest extends ChromeActivityTestCaseBase<ChromeActivity>
|
| private static final float ACCURACY = 10;
|
| private static final String TEST_FILE = "/content/test/data/android/geolocation.html";
|
|
|
| + private static final String MODAL_FLAG = "ModalPermissionPrompts";
|
| + private static final String TOGGLE_FLAG = "DisplayPersistenceToggleInPermissionPrompts";
|
| + private static final String MODAL_TOGGLE_FLAG = MODAL_FLAG + "," + TOGGLE_FLAG;
|
| +
|
| private InfoBarTestAnimationListener mListener;
|
| private EmbeddedTestServer mTestServer;
|
|
|
| @@ -85,7 +94,7 @@ public class GeolocationTest extends ChromeActivityTestCaseBase<ChromeActivity>
|
|
|
| InfoBarContainer container =
|
| getActivity().getTabModelSelector().getCurrentTab().getInfoBarContainer();
|
| - mListener = new InfoBarTestAnimationListener();
|
| + mListener = new InfoBarTestAnimationListener();
|
| container.setAnimationListener(mListener);
|
|
|
| LocationProviderFactory.setLocationProviderImpl(new MockLocationProvider());
|
| @@ -100,25 +109,140 @@ public class GeolocationTest extends ChromeActivityTestCaseBase<ChromeActivity>
|
| }
|
|
|
| /**
|
| + * Returns the current permissions dialog showing or null if no such dialog is currently
|
| + * showing.
|
| + */
|
| + private AlertDialog getCurrentDialog() throws ExecutionException {
|
| + return ThreadUtils.runOnUiThreadBlocking(new Callable<AlertDialog>() {
|
| + @Override
|
| + public AlertDialog call() {
|
| + return PermissionDialogController.getInstance().getCurrentDialogForTesting();
|
| + }
|
| + });
|
| + }
|
| +
|
| + /**
|
| + * Simulates clicking a button on an AlertDialog.
|
| + */
|
| + private void clickButton(final AlertDialog dialog, final int button) {
|
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + dialog.getButton(button).performClick();
|
| + }
|
| + });
|
| + }
|
| +
|
| + /**
|
| + * Runs a geolocation test that permits location access.
|
| + * @param updateWaiter The update waiter to wait for callbacks.
|
| + * @param javascript The JS function to run in the current tab to execute the test.
|
| + * @param nUpdates How many updates to wait for.
|
| + * @param isDialog True if we are expecting a permission dialog, false for an infobar.
|
| + * @param hasSwitch True if we are expecting a persistence switch, false otherwise.
|
| + * @param toggleSwitch True if we should toggle the switch off, false otherwise.
|
| + * @throws Exception
|
| + */
|
| + private void runAllowTest(GeolocationUpdateWaiter updateWaiter, String javascript, int nUpdates,
|
| + boolean isDialog, boolean hasSwitch, boolean toggleSwitch) throws Exception {
|
| + final String url = mTestServer.getURL(TEST_FILE);
|
| +
|
| + loadUrl(url);
|
| + runJavaScriptCodeInCurrentTab(javascript);
|
| + if (isDialog) {
|
| + replyToDialogAndWaitForUpdates(updateWaiter, nUpdates, true, hasSwitch, toggleSwitch);
|
| + } else {
|
| + replyToInfoBarAndWaitForUpdates(updateWaiter, nUpdates, true, hasSwitch, toggleSwitch);
|
| + }
|
| + }
|
| +
|
| + private void replyToInfoBarAndWaitForUpdates(GeolocationUpdateWaiter updateWaiter, int nUpdates,
|
| + boolean allow, boolean hasSwitch, boolean toggleSwitch) throws Exception {
|
| + assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished());
|
| + InfoBar infobar = getInfoBars().get(0);
|
| + assertNotNull(infobar);
|
| +
|
| + if (hasSwitch) {
|
| + SwitchCompat persistSwitch = (SwitchCompat) infobar.getView().findViewById(
|
| + R.id.permission_infobar_persist_toggle);
|
| + assertNotNull(persistSwitch);
|
| + assertTrue(persistSwitch.isChecked());
|
| + if (toggleSwitch) {
|
| + TouchCommon.singleClickView(persistSwitch);
|
| + waitForCheckedState(persistSwitch, false);
|
| + }
|
| + }
|
| +
|
| + if (allow) {
|
| + assertTrue("Allow button wasn't found", InfoBarUtil.clickPrimaryButton(infobar));
|
| + } else {
|
| + assertTrue("Block button wasn't found", InfoBarUtil.clickSecondaryButton(infobar));
|
| + }
|
| + updateWaiter.waitForNumUpdates(nUpdates);
|
| + }
|
| +
|
| + private void replyToDialogAndWaitForUpdates(GeolocationUpdateWaiter updateWaiter, int nUpdates,
|
| + boolean allow, boolean hasSwitch, boolean toggleSwitch) throws Exception {
|
| + AlertDialog dialog = getCurrentDialog();
|
| + assertNotNull(dialog);
|
| +
|
| + if (hasSwitch) {
|
| + SwitchCompat persistSwitch =
|
| + (SwitchCompat) dialog.findViewById(R.id.permission_dialog_persist_toggle);
|
| + assertNotNull(persistSwitch);
|
| + assertTrue(persistSwitch.isChecked());
|
| + if (toggleSwitch) {
|
| + TouchCommon.singleClickView(persistSwitch);
|
| + waitForCheckedState(persistSwitch, false);
|
| + }
|
| + }
|
| +
|
| + if (allow) {
|
| + clickButton(dialog, DialogInterface.BUTTON_POSITIVE);
|
| + } else {
|
| + clickButton(dialog, DialogInterface.BUTTON_NEGATIVE);
|
| + }
|
| + updateWaiter.waitForNumUpdates(nUpdates);
|
| + }
|
| +
|
| + private void waitForCheckedState(final SwitchCompat persistSwitch, boolean isChecked)
|
| + throws InterruptedException {
|
| + CriteriaHelper.pollUiThread(Criteria.equals(isChecked, new Callable<Boolean>() {
|
| + @Override
|
| + public Boolean call() {
|
| + return persistSwitch.isChecked();
|
| + }
|
| + }));
|
| + }
|
| +
|
| + /**
|
| * Verify Geolocation creates an InfoBar and receives a mock location.
|
| * @throws Exception
|
| */
|
| @Smoke
|
| @MediumTest
|
| @Feature({"Location", "Main"})
|
| - public void testGeolocationPlumbing() throws Exception {
|
| - final String url = mTestServer.getURL(TEST_FILE);
|
| -
|
| + public void testGeolocationPlumbingAllowedInfoBar() throws Exception {
|
| Tab tab = getActivity().getActivityTab();
|
| GeolocationUpdateWaiter updateWaiter = new GeolocationUpdateWaiter();
|
| tab.addObserver(updateWaiter);
|
| + runAllowTest(updateWaiter, "initiate_getCurrentPosition()", 1, false, false, false);
|
| + tab.removeObserver(updateWaiter);
|
| + }
|
|
|
| - loadUrl(url);
|
| - runJavaScriptCodeInCurrentTab("initiate_getCurrentPosition()");
|
| - assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished());
|
| - assertTrue("OK button wasn't found", InfoBarUtil.clickPrimaryButton(getInfoBars().get(0)));
|
| - updateWaiter.waitForNumUpdates(1);
|
| -
|
| + /**
|
| + * Verify Geolocation creates a dialog and receives a mock location.
|
| + * @throws Exception
|
| + */
|
| + @Smoke
|
| + @MediumTest
|
| + @CommandLineFlags.Add("enable-features=" + MODAL_FLAG)
|
| + @Feature({"Location", "Main"})
|
| + public void testGeolocationPlumbingAllowedDialog() throws Exception {
|
| + Tab tab = getActivity().getActivityTab();
|
| + GeolocationUpdateWaiter updateWaiter = new GeolocationUpdateWaiter();
|
| + tab.addObserver(updateWaiter);
|
| + runAllowTest(updateWaiter, "initiate_getCurrentPosition()", 1, true, false, false);
|
| tab.removeObserver(updateWaiter);
|
| }
|
|
|
| @@ -128,85 +252,103 @@ public class GeolocationTest extends ChromeActivityTestCaseBase<ChromeActivity>
|
| */
|
| @MediumTest
|
| @Feature({"Location"})
|
| - public void testGeolocationWatch() throws Exception {
|
| - final String url = mTestServer.getURL(TEST_FILE);
|
| -
|
| + public void testGeolocationWatchInfoBar() throws Exception {
|
| Tab tab = getActivity().getActivityTab();
|
| GeolocationUpdateWaiter updateWaiter = new GeolocationUpdateWaiter();
|
| tab.addObserver(updateWaiter);
|
| + runAllowTest(updateWaiter, "initiate_watchPosition()", 2, false, false, false);
|
| + tab.removeObserver(updateWaiter);
|
| + }
|
|
|
| - loadUrl(url);
|
| - runJavaScriptCodeInCurrentTab("initiate_watchPosition()");
|
| - assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished());
|
| - assertTrue("OK button wasn't found", InfoBarUtil.clickPrimaryButton(getInfoBars().get(0)));
|
| - updateWaiter.waitForNumUpdates(2);
|
| + /**
|
| + * Verify Geolocation creates a dialog and receives multiple locations.
|
| + * @throws Exception
|
| + */
|
| + @MediumTest
|
| + @CommandLineFlags.Add("enable-features=" + MODAL_FLAG)
|
| + @Feature({"Location"})
|
| + public void testGeolocationWatchDialog() throws Exception {
|
| + Tab tab = getActivity().getActivityTab();
|
| + GeolocationUpdateWaiter updateWaiter = new GeolocationUpdateWaiter();
|
| + tab.addObserver(updateWaiter);
|
| + runAllowTest(updateWaiter, "initiate_watchPosition()", 2, true, false, false);
|
| + tab.removeObserver(updateWaiter);
|
| + }
|
|
|
| + /**
|
| + * Verify Geolocation creates an infobar with a persistence toggle if that feature is enabled.
|
| + * Check the switch appears and that permission is granted with it toggled on.
|
| + * @throws Exception
|
| + */
|
| + @MediumTest
|
| + @CommandLineFlags.Add("enable-features=" + TOGGLE_FLAG)
|
| + @Feature({"Location"})
|
| + public void testGeolocationPersistenceAllowedInfoBar() throws Exception {
|
| + Tab tab = getActivity().getActivityTab();
|
| + GeolocationUpdateWaiter updateWaiter = new GeolocationUpdateWaiter();
|
| + tab.addObserver(updateWaiter);
|
| + runAllowTest(updateWaiter, "initiate_getCurrentPosition()", 1, false, true, false);
|
| tab.removeObserver(updateWaiter);
|
| }
|
|
|
| /**
|
| - * Verify Geolocation prompts with a persistence toggle if that feature is enabled. Check the
|
| - * switch appears and that permission is granted with it toggled on.
|
| + * Verify Geolocation creates a dialog with a persistence toggle if both features are enabled.
|
| + * Check the switch appears and that permission is granted with it toggled on.
|
| * @throws Exception
|
| */
|
| @MediumTest
|
| - @CommandLineFlags.Add("enable-features=DisplayPersistenceToggleInPermissionPrompts")
|
| + @CommandLineFlags.Add("enable-features=" + MODAL_TOGGLE_FLAG)
|
| @Feature({"Location"})
|
| - public void testGeolocationPersistence() throws Exception {
|
| - final String url = mTestServer.getURL(TEST_FILE);
|
| + public void testGeolocationPersistenceAllowedDialog() throws Exception {
|
| + Tab tab = getActivity().getActivityTab();
|
| + GeolocationUpdateWaiter updateWaiter = new GeolocationUpdateWaiter();
|
| + tab.addObserver(updateWaiter);
|
| + runAllowTest(updateWaiter, "initiate_getCurrentPosition()", 1, true, true, false);
|
| + tab.removeObserver(updateWaiter);
|
| + }
|
|
|
| + /**
|
| + * Verify Geolocation creates an infobar with a persistence toggle if that feature is enabled.
|
| + * Check the switch toggled off.
|
| + * @throws Exception
|
| + */
|
| + @MediumTest
|
| + @CommandLineFlags.Add("enable-features=" + TOGGLE_FLAG)
|
| + @Feature({"Location"})
|
| + public void testGeolocationPersistenceOffAllowedInfoBar() throws Exception {
|
| Tab tab = getActivity().getActivityTab();
|
| GeolocationUpdateWaiter updateWaiter = new GeolocationUpdateWaiter();
|
| tab.addObserver(updateWaiter);
|
| + runAllowTest(updateWaiter, "initiate_getCurrentPosition()", 1, false, true, true);
|
|
|
| - loadUrl(url);
|
| + // Ask for permission again and make sure it doesn't prompt again (grant is cached in the
|
| + // Blink layer).
|
| runJavaScriptCodeInCurrentTab("initiate_getCurrentPosition()");
|
| - assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished());
|
| - InfoBar infobar = getInfoBars().get(0);
|
| - SwitchCompat persistSwitch = (SwitchCompat) infobar.getView().findViewById(
|
| - R.id.permission_infobar_persist_toggle);
|
| - assertNotNull(persistSwitch);
|
| - assertTrue(persistSwitch.isChecked());
|
| + updateWaiter.waitForNumUpdates(2);
|
|
|
| - assertTrue("OK button wasn't found", InfoBarUtil.clickPrimaryButton(infobar));
|
| - updateWaiter.waitForNumUpdates(1);
|
| + // Ask for permission a third time and make sure it doesn't prompt again.
|
| + runJavaScriptCodeInCurrentTab("initiate_getCurrentPosition()");
|
| + updateWaiter.waitForNumUpdates(3);
|
|
|
| tab.removeObserver(updateWaiter);
|
| }
|
|
|
| /**
|
| - * Verify Geolocation prompts with a persistence toggle if that feature is enabled. Check the
|
| - * switch toggled off.
|
| + * Verify Geolocation creates a dialog with a persistence toggle if that feature is enabled.
|
| + * Check the switch toggled off.
|
| * @throws Exception
|
| */
|
| @MediumTest
|
| - @CommandLineFlags.Add("enable-features=DisplayPersistenceToggleInPermissionPrompts")
|
| + @CommandLineFlags.Add("enable-features=" + MODAL_TOGGLE_FLAG)
|
| @Feature({"Location"})
|
| - public void testGeolocationPersistenceOff() throws Exception {
|
| - final String url = mTestServer.getURL(TEST_FILE);
|
| -
|
| + public void testGeolocationPersistenceOffAllowedDialog() throws Exception {
|
| Tab tab = getActivity().getActivityTab();
|
| GeolocationUpdateWaiter updateWaiter = new GeolocationUpdateWaiter();
|
| tab.addObserver(updateWaiter);
|
| -
|
| - loadUrl(url);
|
| - runJavaScriptCodeInCurrentTab("initiate_getCurrentPosition()");
|
| - assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished());
|
| - InfoBar infobar = getInfoBars().get(0);
|
| - SwitchCompat persistSwitch = (SwitchCompat) infobar.getView().findViewById(
|
| - R.id.permission_infobar_persist_toggle);
|
| - assertNotNull(persistSwitch);
|
| - assertTrue(persistSwitch.isChecked());
|
| -
|
| - // Uncheck the switch
|
| - TouchCommon.singleClickView(persistSwitch);
|
| - waitForCheckedState(persistSwitch, false);
|
| -
|
| - assertTrue("OK button wasn't found", InfoBarUtil.clickPrimaryButton(infobar));
|
| - updateWaiter.waitForNumUpdates(1);
|
| + runAllowTest(updateWaiter, "initiate_getCurrentPosition()", 1, true, true, true);
|
|
|
| // Ask for permission again and make sure it doesn't prompt again (grant is cached in the
|
| - // blink layer).
|
| + // Blink layer).
|
| runJavaScriptCodeInCurrentTab("initiate_getCurrentPosition()");
|
| updateWaiter.waitForNumUpdates(2);
|
|
|
| @@ -219,47 +361,34 @@ public class GeolocationTest extends ChromeActivityTestCaseBase<ChromeActivity>
|
|
|
| /**
|
| * Verify Geolocation prompts once and sends multiple locations with a persistence toggle if
|
| - * that feature is enabled.
|
| + * that feature is enabled. Use an infobar.
|
| * @throws Exception
|
| */
|
| @MediumTest
|
| - @CommandLineFlags.Add("enable-features=DisplayPersistenceToggleInPermissionPrompts")
|
| + @CommandLineFlags.Add("enable-features=" + TOGGLE_FLAG)
|
| @Feature({"Location"})
|
| - public void testGeolocationWatchPersistenceOff() throws Exception {
|
| - final String url = mTestServer.getURL(TEST_FILE);
|
| -
|
| + public void testGeolocationWatchPersistenceOffAllowedInfoBar() throws Exception {
|
| Tab tab = getActivity().getActivityTab();
|
| GeolocationUpdateWaiter updateWaiter = new GeolocationUpdateWaiter();
|
| tab.addObserver(updateWaiter);
|
| -
|
| - loadUrl(url);
|
| - runJavaScriptCodeInCurrentTab("initiate_watchPosition()");
|
| - assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished());
|
| - InfoBar infobar = getInfoBars().get(0);
|
| - SwitchCompat persistSwitch = (SwitchCompat) infobar.getView().findViewById(
|
| - R.id.permission_infobar_persist_toggle);
|
| - assertNotNull(persistSwitch);
|
| - assertTrue(persistSwitch.isChecked());
|
| -
|
| - // Uncheck the switch
|
| - TouchCommon.singleClickView(persistSwitch);
|
| - waitForCheckedState(persistSwitch, false);
|
| -
|
| - // Make sure we get multiple updates without another prompt.
|
| - assertTrue("OK button wasn't found", InfoBarUtil.clickPrimaryButton(infobar));
|
| - updateWaiter.waitForNumUpdates(4);
|
| -
|
| + runAllowTest(updateWaiter, "initiate_watchPosition()", 2, false, true, true);
|
| tab.removeObserver(updateWaiter);
|
| }
|
|
|
| - private void waitForCheckedState(final SwitchCompat persistSwitch, boolean isChecked)
|
| - throws InterruptedException {
|
| - CriteriaHelper.pollUiThread(Criteria.equals(isChecked, new Callable<Boolean>() {
|
| - @Override
|
| - public Boolean call() {
|
| - return persistSwitch.isChecked();
|
| - }
|
| - }));
|
| + /**
|
| + * Verify Geolocation prompts once and sends multiple locations with a persistence toggle if
|
| + * that feature is enabled. Use a dialog.
|
| + * @throws Exception
|
| + */
|
| + @MediumTest
|
| + @CommandLineFlags.Add("enable-features=" + MODAL_TOGGLE_FLAG)
|
| + @Feature({"Location"})
|
| + public void testGeolocationWatchPersistenceOffAllowedDialog() throws Exception {
|
| + Tab tab = getActivity().getActivityTab();
|
| + GeolocationUpdateWaiter updateWaiter = new GeolocationUpdateWaiter();
|
| + tab.addObserver(updateWaiter);
|
| + runAllowTest(updateWaiter, "initiate_watchPosition()", 2, true, true, true);
|
| + tab.removeObserver(updateWaiter);
|
| }
|
|
|
| @Override
|
|
|