Commit 86fa36be authored by Anita Woodruff's avatar Anita Woodruff Committed by Commit Bot

[Android Tests] Fix up disabled tests in NotificationPlatformBridgeTest

- Turns out these tests were failing because notification permissions
were switched from info bars to modals a while ago and these tests were
never updated.

Bug: 853405
Change-Id: Ia663240507a7fa674f96170c4fdcbfdc9c028ec9
Reviewed-on: https://chromium-review.googlesource.com/1145309
Commit-Queue: Anita Woodruff <awdf@chromium.org>
Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577898}
parent e459c6e9
...@@ -33,7 +33,6 @@ import org.junit.runner.RunWith; ...@@ -33,7 +33,6 @@ import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
import org.chromium.base.metrics.RecordHistogram; import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.test.util.CommandLineFlags; import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.MinAndroidSdkLevel; import org.chromium.base.test.util.MinAndroidSdkLevel;
import org.chromium.base.test.util.RetryOnFailure; import org.chromium.base.test.util.RetryOnFailure;
...@@ -41,20 +40,19 @@ import org.chromium.base.test.util.UserActionTester; ...@@ -41,20 +40,19 @@ import org.chromium.base.test.util.UserActionTester;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeSwitches; import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.engagement.SiteEngagementService; import org.chromium.chrome.browser.engagement.SiteEngagementService;
import org.chromium.chrome.browser.infobar.InfoBar; import org.chromium.chrome.browser.permissions.PermissionDialogController;
import org.chromium.chrome.browser.permissions.PermissionTestRule;
import org.chromium.chrome.browser.preferences.PrefServiceBridge; import org.chromium.chrome.browser.preferences.PrefServiceBridge;
import org.chromium.chrome.browser.preferences.website.ContentSetting; import org.chromium.chrome.browser.preferences.website.ContentSetting;
import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.Tab; import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.widget.RoundedIconGenerator; import org.chromium.chrome.browser.widget.RoundedIconGenerator;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner; import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.InfoBarUtil;
import org.chromium.chrome.test.util.browser.TabTitleObserver; import org.chromium.chrome.test.util.browser.TabTitleObserver;
import org.chromium.chrome.test.util.browser.notifications.MockNotificationManagerProxy.NotificationEntry; import org.chromium.chrome.test.util.browser.notifications.MockNotificationManagerProxy.NotificationEntry;
import org.chromium.components.url_formatter.UrlFormatter; import org.chromium.components.url_formatter.UrlFormatter;
import org.chromium.content.browser.test.util.Criteria; import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper; import org.chromium.content.browser.test.util.CriteriaHelper;
import org.chromium.net.test.EmbeddedTestServerRule;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -73,7 +71,7 @@ import java.util.concurrent.TimeoutException; ...@@ -73,7 +71,7 @@ import java.util.concurrent.TimeoutException;
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE}) @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
public class NotificationPlatformBridgeTest { public class NotificationPlatformBridgeTest {
@Rule @Rule
public EmbeddedTestServerRule mEmbeddedTestServerRule = new EmbeddedTestServerRule(); public PermissionTestRule mPermissionTestRule = new PermissionTestRule();
@Rule @Rule
public NotificationTestRule mNotificationTestRule = new NotificationTestRule(); public NotificationTestRule mNotificationTestRule = new NotificationTestRule();
...@@ -85,8 +83,8 @@ public class NotificationPlatformBridgeTest { ...@@ -85,8 +83,8 @@ public class NotificationPlatformBridgeTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
SiteEngagementService.setParamValuesForTesting(); SiteEngagementService.setParamValuesForTesting();
mNotificationTestRule.loadUrl( mNotificationTestRule.loadUrl(mPermissionTestRule.getURL(NOTIFICATION_TEST_PAGE));
mEmbeddedTestServerRule.getServer().getURL(NOTIFICATION_TEST_PAGE)); mPermissionTestRule.setActivity(mNotificationTestRule.getActivity());
} }
@SuppressWarnings("MissingFail") @SuppressWarnings("MissingFail")
...@@ -102,31 +100,11 @@ public class NotificationPlatformBridgeTest { ...@@ -102,31 +100,11 @@ public class NotificationPlatformBridgeTest {
} }
} }
private InfoBar getInfobarBlocking() {
CriteriaHelper.pollUiThread(new Criteria() {
@Override
public boolean isSatisfied() {
return !mNotificationTestRule.getInfoBars().isEmpty();
}
});
List<InfoBar> infoBars = mNotificationTestRule.getInfoBars();
Assert.assertEquals(1, infoBars.size());
return infoBars.get(0);
}
private void waitForInfobarToClose() {
CriteriaHelper.pollUiThread(new Criteria() {
@Override
public boolean isSatisfied() {
return mNotificationTestRule.getInfoBars().isEmpty();
}
});
Assert.assertEquals(0, mNotificationTestRule.getInfoBars().size());
}
private void checkThatShowNotificationIsDenied() throws Exception { private void checkThatShowNotificationIsDenied() throws Exception {
showNotification("MyNotification", "{}"); showNotification("MyNotification", "{}");
waitForTitle("TypeError: No notification permission has been granted for this origin."); waitForTitle(
"TypeError: Failed to execute 'showNotification' on 'ServiceWorkerRegistration': "
+ "No notification permission has been granted for this origin.");
// Ideally we'd wait a little here, but it's hard to wait for things that shouldn't happen. // Ideally we'd wait a little here, but it's hard to wait for things that shouldn't happen.
Assert.assertTrue(mNotificationTestRule.getNotificationEntries().isEmpty()); Assert.assertTrue(mNotificationTestRule.getNotificationEntries().isEmpty());
...@@ -138,7 +116,7 @@ public class NotificationPlatformBridgeTest { ...@@ -138,7 +116,7 @@ public class NotificationPlatformBridgeTest {
@Override @Override
public Double call() throws Exception { public Double call() throws Exception {
return SiteEngagementService.getForProfile(Profile.getLastUsedProfile()) return SiteEngagementService.getForProfile(Profile.getLastUsedProfile())
.getScore(mEmbeddedTestServerRule.getOrigin()); .getScore(mPermissionTestRule.getOrigin());
} }
}); });
} catch (ExecutionException ex) { } catch (ExecutionException ex) {
...@@ -151,52 +129,36 @@ public class NotificationPlatformBridgeTest { ...@@ -151,52 +129,36 @@ public class NotificationPlatformBridgeTest {
* Verifies that notifcations cannot be shown without permission, and that dismissing or denying * Verifies that notifcations cannot be shown without permission, and that dismissing or denying
* the infobar works correctly. * the infobar works correctly.
*/ */
//@LargeTest @LargeTest
//@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
// crbug.com/707528
@Test @Test
@DisabledTest
public void testPermissionDenied() throws Exception { public void testPermissionDenied() throws Exception {
// Notifications permission should initially be prompt, and showing should fail. // Notifications permission should initially be prompt, and showing should fail.
Assert.assertEquals("\"default\"", runJavaScript("Notification.permission")); Assert.assertEquals("\"default\"", runJavaScript("Notification.permission"));
checkThatShowNotificationIsDenied(); checkThatShowNotificationIsDenied();
// Notification.requestPermission() should show the notifications infobar. PermissionTestRule.PermissionUpdateWaiter updateWaiter =
Assert.assertEquals(0, mNotificationTestRule.getInfoBars().size()); new PermissionTestRule.PermissionUpdateWaiter(
runJavaScript("Notification.requestPermission(sendToTest)"); "denied: ", mNotificationTestRule.getActivity());
InfoBar infoBar = getInfobarBlocking();
// Dismissing the infobar should pass prompt to the requestPermission callback. mNotificationTestRule.getActivity().getActivityTab().addObserver(updateWaiter);
Assert.assertTrue(InfoBarUtil.clickCloseButton(infoBar));
waitForInfobarToClose();
waitForTitle("default"); // See https://crbug.com/434547.
// Notifications permission should still be prompt. mPermissionTestRule.runDenyTest(updateWaiter, NOTIFICATION_TEST_PAGE,
Assert.assertEquals("\"default\"", runJavaScript("Notification.permission")); "Notification.requestPermission(addCountAndSendToTest)", 1, false, true);
checkThatShowNotificationIsDenied();
// Notification.requestPermission() should show the notifications infobar again.
runJavaScript("Notification.requestPermission(sendToTest)");
infoBar = getInfobarBlocking();
// Denying the infobar should pass denied to the requestPermission callback.
Assert.assertTrue(InfoBarUtil.clickSecondaryButton(infoBar));
waitForInfobarToClose();
waitForTitle("denied");
// This should have caused notifications permission to become denied. // This should have caused notifications permission to become denied.
Assert.assertEquals("\"denied\"", runJavaScript("Notification.permission")); Assert.assertEquals("\"denied\"", runJavaScript("Notification.permission"));
checkThatShowNotificationIsDenied(); checkThatShowNotificationIsDenied();
// Reload page to ensure the block is persisted. // Reload page to ensure the block is persisted.
mNotificationTestRule.loadUrl( mNotificationTestRule.loadUrl(mPermissionTestRule.getURL(NOTIFICATION_TEST_PAGE));
mEmbeddedTestServerRule.getServer().getURL(NOTIFICATION_TEST_PAGE));
// Notification.requestPermission() should immediately pass denied to the callback without // Notification.requestPermission() should immediately pass denied to the callback without
// showing an infobar. // showing a dialog.
runJavaScript("Notification.requestPermission(sendToTest)"); runJavaScript("Notification.requestPermission(sendToTest)");
waitForTitle("denied"); waitForTitle("denied");
Assert.assertEquals(0, mNotificationTestRule.getInfoBars().size()); Assert.assertEquals(
null, PermissionDialogController.getInstance().getCurrentDialogForTesting());
// Notifications permission should still be denied. // Notifications permission should still be denied.
Assert.assertEquals("\"denied\"", runJavaScript("Notification.permission")); Assert.assertEquals("\"denied\"", runJavaScript("Notification.permission"));
...@@ -206,29 +168,25 @@ public class NotificationPlatformBridgeTest { ...@@ -206,29 +168,25 @@ public class NotificationPlatformBridgeTest {
/** /**
* Verifies granting permission via the infobar. * Verifies granting permission via the infobar.
*/ */
//@MediumTest @MediumTest
//@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
// crbug.com/707528
@Test @Test
@DisabledTest
public void testPermissionGranted() throws Exception { public void testPermissionGranted() throws Exception {
// Notifications permission should initially be prompt, and showing should fail. // Notifications permission should initially be prompt, and showing should fail.
Assert.assertEquals("\"default\"", runJavaScript("Notification.permission")); Assert.assertEquals("\"default\"", runJavaScript("Notification.permission"));
checkThatShowNotificationIsDenied(); checkThatShowNotificationIsDenied();
// Notification.requestPermission() should show the notifications infobar. PermissionTestRule.PermissionUpdateWaiter updateWaiter =
Assert.assertEquals(0, mNotificationTestRule.getInfoBars().size()); new PermissionTestRule.PermissionUpdateWaiter(
runJavaScript("Notification.requestPermission(sendToTest)"); "granted: ", mNotificationTestRule.getActivity());
InfoBar infoBar = getInfobarBlocking();
mNotificationTestRule.getActivity().getActivityTab().addObserver(updateWaiter);
// Accepting the infobar should pass granted to the requestPermission callback. mPermissionTestRule.runAllowTest(updateWaiter, NOTIFICATION_TEST_PAGE,
Assert.assertTrue(InfoBarUtil.clickPrimaryButton(infoBar)); "Notification.requestPermission(addCountAndSendToTest)", 1, false, true);
waitForInfobarToClose();
waitForTitle("granted");
// Reload page to ensure the grant is persisted. // Reload page to ensure the grant is persisted.
mNotificationTestRule.loadUrl( mNotificationTestRule.loadUrl(mPermissionTestRule.getURL(NOTIFICATION_TEST_PAGE));
mEmbeddedTestServerRule.getServer().getURL(NOTIFICATION_TEST_PAGE));
// Notifications permission should now be granted, and showing should succeed. // Notifications permission should now be granted, and showing should succeed.
Assert.assertEquals("\"granted\"", runJavaScript("Notification.permission")); Assert.assertEquals("\"granted\"", runJavaScript("Notification.permission"));
...@@ -245,13 +203,13 @@ public class NotificationPlatformBridgeTest { ...@@ -245,13 +203,13 @@ public class NotificationPlatformBridgeTest {
@RetryOnFailure @RetryOnFailure
public void testDefaultNotificationProperties() throws Exception { public void testDefaultNotificationProperties() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
Context context = InstrumentationRegistry.getTargetContext(); Context context = InstrumentationRegistry.getTargetContext();
Notification notification = showAndGetNotification("MyNotification", "{body: 'Hello'}"); Notification notification = showAndGetNotification("MyNotification", "{body: 'Hello'}");
String expectedOrigin = UrlFormatter.formatUrlForSecurityDisplayOmitScheme( String expectedOrigin =
mEmbeddedTestServerRule.getOrigin()); UrlFormatter.formatUrlForSecurityDisplayOmitScheme(mPermissionTestRule.getOrigin());
// Validate the contents of the notification. // Validate the contents of the notification.
Assert.assertEquals("MyNotification", NotificationTestUtil.getExtraTitle(notification)); Assert.assertEquals("MyNotification", NotificationTestUtil.getExtraTitle(notification));
...@@ -309,7 +267,7 @@ public class NotificationPlatformBridgeTest { ...@@ -309,7 +267,7 @@ public class NotificationPlatformBridgeTest {
@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
public void testShowNotificationWithTextAction() throws Exception { public void testShowNotificationWithTextAction() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
Notification notification = showAndGetNotification("MyNotification", Notification notification = showAndGetNotification("MyNotification",
"{ " "{ "
...@@ -340,7 +298,7 @@ public class NotificationPlatformBridgeTest { ...@@ -340,7 +298,7 @@ public class NotificationPlatformBridgeTest {
@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
public void testReplyToNotification() throws Exception { public void testReplyToNotification() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
Context context = InstrumentationRegistry.getTargetContext(); Context context = InstrumentationRegistry.getTargetContext();
UserActionTester actionTester = new UserActionTester(); UserActionTester actionTester = new UserActionTester();
...@@ -388,7 +346,7 @@ public class NotificationPlatformBridgeTest { ...@@ -388,7 +346,7 @@ public class NotificationPlatformBridgeTest {
@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
public void testReplyToNotificationWithEmptyReply() throws Exception { public void testReplyToNotificationWithEmptyReply() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
Context context = InstrumentationRegistry.getTargetContext(); Context context = InstrumentationRegistry.getTargetContext();
// +0.5 engagement from navigating to the test page. // +0.5 engagement from navigating to the test page.
...@@ -448,7 +406,7 @@ public class NotificationPlatformBridgeTest { ...@@ -448,7 +406,7 @@ public class NotificationPlatformBridgeTest {
@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
public void testReplyToNotificationWithNoRemoteInput() throws Exception { public void testReplyToNotificationWithNoRemoteInput() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
// +0.5 engagement from navigating to the test page. // +0.5 engagement from navigating to the test page.
Assert.assertEquals(0.5, getEngagementScoreBlocking(), 0); Assert.assertEquals(0.5, getEngagementScoreBlocking(), 0);
...@@ -475,7 +433,7 @@ public class NotificationPlatformBridgeTest { ...@@ -475,7 +433,7 @@ public class NotificationPlatformBridgeTest {
@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
public void testNotificationRenotifyProperty() throws Exception { public void testNotificationRenotifyProperty() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
Notification notification = Notification notification =
showAndGetNotification("MyNotification", "{ tag: 'myTag', renotify: true }"); showAndGetNotification("MyNotification", "{ tag: 'myTag', renotify: true }");
...@@ -492,7 +450,7 @@ public class NotificationPlatformBridgeTest { ...@@ -492,7 +450,7 @@ public class NotificationPlatformBridgeTest {
@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
public void testNotificationSilentProperty() throws Exception { public void testNotificationSilentProperty() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
Notification notification = showAndGetNotification("MyNotification", "{ silent: true }"); Notification notification = showAndGetNotification("MyNotification", "{ silent: true }");
...@@ -503,7 +461,7 @@ public class NotificationPlatformBridgeTest { ...@@ -503,7 +461,7 @@ public class NotificationPlatformBridgeTest {
private void verifyVibrationNotRequestedWhenDisabledInPrefs(String notificationOptions) private void verifyVibrationNotRequestedWhenDisabledInPrefs(String notificationOptions)
throws Exception { throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
// Disable notification vibration in preferences. // Disable notification vibration in preferences.
ThreadUtils.runOnUiThreadBlocking(new Runnable() { ThreadUtils.runOnUiThreadBlocking(new Runnable() {
...@@ -557,7 +515,7 @@ public class NotificationPlatformBridgeTest { ...@@ -557,7 +515,7 @@ public class NotificationPlatformBridgeTest {
@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
public void testNotificationVibrateCustomPattern() throws Exception { public void testNotificationVibrateCustomPattern() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
// By default, vibration is enabled in notifications. // By default, vibration is enabled in notifications.
ThreadUtils.runOnUiThreadBlocking(new Runnable() { ThreadUtils.runOnUiThreadBlocking(new Runnable() {
...@@ -590,7 +548,7 @@ public class NotificationPlatformBridgeTest { ...@@ -590,7 +548,7 @@ public class NotificationPlatformBridgeTest {
@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
public void testShowNotificationWithBadge() throws Exception { public void testShowNotificationWithBadge() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
Notification notification = Notification notification =
showAndGetNotification("MyNotification", "{badge: 'badge.png'}"); showAndGetNotification("MyNotification", "{badge: 'badge.png'}");
...@@ -604,8 +562,8 @@ public class NotificationPlatformBridgeTest { ...@@ -604,8 +562,8 @@ public class NotificationPlatformBridgeTest {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Custom badges are only supported on M+. // Custom badges are only supported on M+.
// 1. Check the notification badge. // 1. Check the notification badge.
URL badgeUrl = new URL(mEmbeddedTestServerRule.getServer().getURL( URL badgeUrl = new URL(
"/chrome/test/data/notifications/badge.png")); mPermissionTestRule.getURL("/chrome/test/data/notifications/badge.png"));
Bitmap bitmap = BitmapFactory.decodeStream(badgeUrl.openStream()); Bitmap bitmap = BitmapFactory.decodeStream(badgeUrl.openStream());
Bitmap expected = bitmap.copy(bitmap.getConfig(), true); Bitmap expected = bitmap.copy(bitmap.getConfig(), true);
NotificationBuilderBase.applyWhiteOverlayToBitmap(expected); NotificationBuilderBase.applyWhiteOverlayToBitmap(expected);
...@@ -644,7 +602,7 @@ public class NotificationPlatformBridgeTest { ...@@ -644,7 +602,7 @@ public class NotificationPlatformBridgeTest {
@RetryOnFailure @RetryOnFailure
public void testShowNotificationWithIcon() throws Exception { public void testShowNotificationWithIcon() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
Notification notification = showAndGetNotification("MyNotification", "{icon: 'red.png'}"); Notification notification = showAndGetNotification("MyNotification", "{icon: 'red.png'}");
...@@ -667,7 +625,7 @@ public class NotificationPlatformBridgeTest { ...@@ -667,7 +625,7 @@ public class NotificationPlatformBridgeTest {
@RetryOnFailure @RetryOnFailure
public void testShowNotificationWithoutIcon() throws Exception { public void testShowNotificationWithoutIcon() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
Notification notification = showAndGetNotification("NoIconNotification", "{}"); Notification notification = showAndGetNotification("NoIconNotification", "{}");
...@@ -686,7 +644,7 @@ public class NotificationPlatformBridgeTest { ...@@ -686,7 +644,7 @@ public class NotificationPlatformBridgeTest {
RoundedIconGenerator generator = RoundedIconGenerator generator =
NotificationBuilderBase.createIconGenerator(context.getResources()); NotificationBuilderBase.createIconGenerator(context.getResources());
Bitmap generatedIcon = generator.generateIconForUrl(mEmbeddedTestServerRule.getOrigin()); Bitmap generatedIcon = generator.generateIconForUrl(mPermissionTestRule.getOrigin());
Assert.assertNotNull(generatedIcon); Assert.assertNotNull(generatedIcon);
Assert.assertTrue(generatedIcon.sameAs( Assert.assertTrue(generatedIcon.sameAs(
NotificationTestUtil.getLargeIconFromNotification(context, notification))); NotificationTestUtil.getLargeIconFromNotification(context, notification)));
...@@ -703,7 +661,7 @@ public class NotificationPlatformBridgeTest { ...@@ -703,7 +661,7 @@ public class NotificationPlatformBridgeTest {
@RetryOnFailure @RetryOnFailure
public void testNotificationContentIntentClosesNotification() throws Exception { public void testNotificationContentIntentClosesNotification() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
// +0.5 engagement from navigating to the test page. // +0.5 engagement from navigating to the test page.
Assert.assertEquals(0.5, getEngagementScoreBlocking(), 0); Assert.assertEquals(0.5, getEngagementScoreBlocking(), 0);
...@@ -749,7 +707,7 @@ public class NotificationPlatformBridgeTest { ...@@ -749,7 +707,7 @@ public class NotificationPlatformBridgeTest {
@RetryOnFailure @RetryOnFailure
public void testNotificationContentIntentCreatesTab() throws Exception { public void testNotificationContentIntentCreatesTab() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
Assert.assertEquals( Assert.assertEquals(
"Expected the notification test page to be the sole tab in the current model", 1, "Expected the notification test page to be the sole tab in the current model", 1,
...@@ -790,7 +748,7 @@ public class NotificationPlatformBridgeTest { ...@@ -790,7 +748,7 @@ public class NotificationPlatformBridgeTest {
@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
public void testNotificationTagReplacement() throws Exception { public void testNotificationTagReplacement() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
// +0.5 engagement from navigating to the test page. // +0.5 engagement from navigating to the test page.
Assert.assertEquals(0.5, getEngagementScoreBlocking(), 0); Assert.assertEquals(0.5, getEngagementScoreBlocking(), 0);
...@@ -829,7 +787,7 @@ public class NotificationPlatformBridgeTest { ...@@ -829,7 +787,7 @@ public class NotificationPlatformBridgeTest {
@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
public void testShowAndCloseMultipleNotifications() throws Exception { public void testShowAndCloseMultipleNotifications() throws Exception {
mNotificationTestRule.setNotificationContentSettingForOrigin( mNotificationTestRule.setNotificationContentSettingForOrigin(
ContentSetting.ALLOW, mEmbeddedTestServerRule.getOrigin()); ContentSetting.ALLOW, mPermissionTestRule.getOrigin());
// +0.5 engagement from navigating to the test page. // +0.5 engagement from navigating to the test page.
Assert.assertEquals(0.5, getEngagementScoreBlocking(), 0); Assert.assertEquals(0.5, getEngagementScoreBlocking(), 0);
......
...@@ -44,6 +44,7 @@ public class GeolocationTest { ...@@ -44,6 +44,7 @@ public class GeolocationTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
mPermissionRule.setUpActivity();
LocationSettingsTestUtil.setSystemLocationSettingEnabled(true); LocationSettingsTestUtil.setSystemLocationSettingEnabled(true);
LocationProviderOverrider.setLocationProviderImpl(new MockLocationProvider()); LocationProviderOverrider.setLocationProviderImpl(new MockLocationProvider());
} }
......
...@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.permissions; ...@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.permissions;
import android.support.test.filters.MediumTest; import android.support.test.filters.MediumTest;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -34,6 +35,11 @@ public class MediaTest { ...@@ -34,6 +35,11 @@ public class MediaTest {
public MediaTest() {} public MediaTest() {}
@Before
public void setUp() throws Exception {
mPermissionRule.setUpActivity();
}
private void testMediaPermissionsPlumbing(String prefix, String script, int numUpdates, private void testMediaPermissionsPlumbing(String prefix, String script, int numUpdates,
boolean withGesture, boolean isDialog) throws Exception { boolean withGesture, boolean isDialog) throws Exception {
Tab tab = mPermissionRule.getActivity().getActivityTab(); Tab tab = mPermissionRule.getActivity().getActivityTab();
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
package org.chromium.chrome.browser.permissions; package org.chromium.chrome.browser.permissions;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -33,6 +34,11 @@ public class PermissionNavigationTest { ...@@ -33,6 +34,11 @@ public class PermissionNavigationTest {
public PermissionNavigationTest() {} public PermissionNavigationTest() {}
@Before
public void setUp() throws Exception {
mPermissionRule.setUpActivity();
}
/** /**
* Check that modal permission prompts and queued permission requests are removed upon * Check that modal permission prompts and queued permission requests are removed upon
* navigation. * navigation.
......
...@@ -60,7 +60,7 @@ public class PermissionTestRule extends ChromeActivityTestRule<ChromeActivity> { ...@@ -60,7 +60,7 @@ public class PermissionTestRule extends ChromeActivityTestRule<ChromeActivity> {
* Waits till a JavaScript callback which updates the page title is called the specified number * Waits till a JavaScript callback which updates the page title is called the specified number
* of times. The page title is expected to be of the form <prefix>: <count>. * of times. The page title is expected to be of the form <prefix>: <count>.
*/ */
protected static class PermissionUpdateWaiter extends EmptyTabObserver { public static class PermissionUpdateWaiter extends EmptyTabObserver {
private CallbackHelper mCallbackHelper; private CallbackHelper mCallbackHelper;
private String mPrefix; private String mPrefix;
private int mExpectedCount; private int mExpectedCount;
...@@ -136,13 +136,19 @@ public class PermissionTestRule extends ChromeActivityTestRule<ChromeActivity> { ...@@ -136,13 +136,19 @@ public class PermissionTestRule extends ChromeActivityTestRule<ChromeActivity> {
} }
private void ruleSetUp() throws Throwable { private void ruleSetUp() throws Throwable {
// TODO(https://crbug.com/867446): Refactor to use EmbeddedTestServerRule.
mTestServer = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
}
/**
* Starts an activity and listens for info-bars appearing/disappearing.
*/
void setUpActivity() throws InterruptedException {
startMainActivityOnBlankPage(); startMainActivityOnBlankPage();
InfoBarContainer container = InfoBarContainer container =
getActivity().getTabModelSelector().getCurrentTab().getInfoBarContainer(); getActivity().getTabModelSelector().getCurrentTab().getInfoBarContainer();
mListener = new InfoBarTestAnimationListener(); mListener = new InfoBarTestAnimationListener();
container.addAnimationListener(mListener); container.addAnimationListener(mListener);
// TODO(yolandyan): refactor to use EmbeddedTestServerRule
mTestServer = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
} }
private void ruleTearDown() throws Exception { private void ruleTearDown() throws Exception {
...@@ -150,9 +156,16 @@ public class PermissionTestRule extends ChromeActivityTestRule<ChromeActivity> { ...@@ -150,9 +156,16 @@ public class PermissionTestRule extends ChromeActivityTestRule<ChromeActivity> {
} }
protected void setUpUrl(final String url) throws InterruptedException { protected void setUpUrl(final String url) throws InterruptedException {
loadUrl(mTestServer.getURL(url)); loadUrl(getURL(url));
}
public String getURL(String url) {
return mTestServer.getURL(url);
} }
public String getOrigin() {
return mTestServer.getURL("/");
}
/** /**
* Simulates clicking a button on an PermissionDialogView. * Simulates clicking a button on an PermissionDialogView.
*/ */
...@@ -181,23 +194,55 @@ public class PermissionTestRule extends ChromeActivityTestRule<ChromeActivity> { ...@@ -181,23 +194,55 @@ public class PermissionTestRule extends ChromeActivityTestRule<ChromeActivity> {
String javascript, int nUpdates, boolean withGesture, boolean isDialog) String javascript, int nUpdates, boolean withGesture, boolean isDialog)
throws Exception { throws Exception {
setUpUrl(url); setUpUrl(url);
if (withGesture) {
runJavaScriptCodeInCurrentTabWithGesture(javascript);
} else {
runJavaScriptCodeInCurrentTab(javascript);
}
replyToPromptAndWaitForUpdates(updateWaiter, true, nUpdates, isDialog);
}
/**
* Runs a permission prompt test that grants the permission and expects the page title to be
* updated in response.
* @param updateWaiter The update waiter to wait for callbacks. Should be added as an observer
* to the current tab prior to calling this method.
* @param javascript The JS function to run in the current tab to execute the test and update
* the page title.
* @param nUpdates How many updates of the page title to wait for.
* @param withGesture True if we require a user gesture to trigger the prompt.
* @param isDialog True if we are expecting a permission dialog, false for an infobar.
* @throws Exception
*/
public void runDenyTest(PermissionUpdateWaiter updateWaiter, final String url,
String javascript, int nUpdates, boolean withGesture, boolean isDialog)
throws Exception {
setUpUrl(url);
if (withGesture) { if (withGesture) {
runJavaScriptCodeInCurrentTab("functionToRun = '" + javascript + "'"); runJavaScriptCodeInCurrentTabWithGesture(javascript);
TouchCommon.singleClickView(getActivity().getActivityTab().getView());
} else { } else {
runJavaScriptCodeInCurrentTab(javascript); runJavaScriptCodeInCurrentTab(javascript);
} }
replyToPromptAndWaitForUpdates(updateWaiter, false, nUpdates, isDialog);
}
private void replyToPromptAndWaitForUpdates(PermissionUpdateWaiter updateWaiter, boolean allow,
int nUpdates, boolean isDialog) throws Exception {
if (isDialog) { if (isDialog) {
DialogShownCriteria criteria = new DialogShownCriteria("Dialog not shown", true); DialogShownCriteria criteria = new DialogShownCriteria("Dialog not shown", true);
CriteriaHelper.pollUiThread(criteria); CriteriaHelper.pollUiThread(criteria);
replyToDialogAndWaitForUpdates(updateWaiter, criteria.getDialog(), nUpdates, true); replyToDialogAndWaitForUpdates(updateWaiter, criteria.getDialog(), nUpdates, allow);
} else { } else {
replyToInfoBarAndWaitForUpdates(updateWaiter, nUpdates, true); replyToInfoBarAndWaitForUpdates(updateWaiter, nUpdates, allow);
} }
} }
private void runJavaScriptCodeInCurrentTabWithGesture(String javascript)
throws InterruptedException, java.util.concurrent.TimeoutException {
runJavaScriptCodeInCurrentTab("functionToRun = '" + javascript + "'");
TouchCommon.singleClickView(getActivity().getActivityTab().getView());
}
/** /**
* Replies to an infobar permission prompt and waits for a provided number * Replies to an infobar permission prompt and waits for a provided number
* of updates to the page title in response. * of updates to the page title in response.
......
...@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.permissions; ...@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.permissions;
import android.support.test.filters.MediumTest; import android.support.test.filters.MediumTest;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -30,6 +31,11 @@ public class QuotaTest { ...@@ -30,6 +31,11 @@ public class QuotaTest {
private static final String TEST_FILE = "/content/test/data/android/quota_permissions.html"; private static final String TEST_FILE = "/content/test/data/android/quota_permissions.html";
@Before
public void setUp() throws Exception {
mPermissionRule.setUpActivity();
}
public QuotaTest() {} public QuotaTest() {}
private void testQuotaPermissionsPlumbing( private void testQuotaPermissionsPlumbing(
......
...@@ -39,6 +39,17 @@ ...@@ -39,6 +39,17 @@
} }
document.title = message; document.title = message;
} }
// Some tests require a count of how frequently they were invoked.
var message_counts = {}
function addCountAndSendToTest(message) {
if (message in message_counts) {
message_counts[message]++;
} else {
message_counts[message] = 1;
}
document.title = message + ': ' + message_counts[message];
}
</script> </script>
</body> </body>
</html> </html>
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