Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.notifications; | 5 package org.chromium.chrome.browser.notifications; |
| 6 | 6 |
| 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; | 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
| 8 | 8 |
| 9 import android.annotation.TargetApi; | 9 import android.annotation.TargetApi; |
| 10 import android.app.Notification; | 10 import android.app.Notification; |
| 11 import android.app.PendingIntent; | 11 import android.app.PendingIntent; |
| 12 import android.app.RemoteInput; | 12 import android.app.RemoteInput; |
| 13 import android.content.Context; | 13 import android.content.Context; |
| 14 import android.content.Intent; | 14 import android.content.Intent; |
| 15 import android.graphics.Bitmap; | 15 import android.graphics.Bitmap; |
| 16 import android.graphics.BitmapFactory; | 16 import android.graphics.BitmapFactory; |
| 17 import android.graphics.Color; | 17 import android.graphics.Color; |
| 18 import android.os.Build; | 18 import android.os.Build; |
| 19 import android.os.Bundle; | 19 import android.os.Bundle; |
| 20 import android.support.test.InstrumentationRegistry; | |
| 20 import android.support.test.filters.LargeTest; | 21 import android.support.test.filters.LargeTest; |
| 21 import android.support.test.filters.MediumTest; | 22 import android.support.test.filters.MediumTest; |
| 22 | 23 |
| 24 import org.junit.Assert; | |
| 25 import org.junit.Before; | |
| 26 import org.junit.Rule; | |
| 27 import org.junit.Test; | |
| 28 import org.junit.runner.RunWith; | |
| 29 | |
| 23 import org.chromium.base.ThreadUtils; | 30 import org.chromium.base.ThreadUtils; |
| 24 import org.chromium.base.annotations.SuppressFBWarnings; | 31 import org.chromium.base.annotations.SuppressFBWarnings; |
| 25 import org.chromium.base.test.util.CommandLineFlags; | 32 import org.chromium.base.test.util.CommandLineFlags; |
| 26 import org.chromium.base.test.util.DisabledTest; | 33 import org.chromium.base.test.util.DisabledTest; |
| 27 import org.chromium.base.test.util.Feature; | 34 import org.chromium.base.test.util.Feature; |
| 28 import org.chromium.base.test.util.MinAndroidSdkLevel; | 35 import org.chromium.base.test.util.MinAndroidSdkLevel; |
| 29 import org.chromium.base.test.util.RetryOnFailure; | 36 import org.chromium.base.test.util.RetryOnFailure; |
| 30 import org.chromium.chrome.R; | 37 import org.chromium.chrome.R; |
| 38 import org.chromium.chrome.browser.ChromeSwitches; | |
| 31 import org.chromium.chrome.browser.engagement.SiteEngagementService; | 39 import org.chromium.chrome.browser.engagement.SiteEngagementService; |
| 32 import org.chromium.chrome.browser.infobar.InfoBar; | 40 import org.chromium.chrome.browser.infobar.InfoBar; |
| 33 import org.chromium.chrome.browser.preferences.PrefServiceBridge; | 41 import org.chromium.chrome.browser.preferences.PrefServiceBridge; |
| 34 import org.chromium.chrome.browser.preferences.website.ContentSetting; | 42 import org.chromium.chrome.browser.preferences.website.ContentSetting; |
| 35 import org.chromium.chrome.browser.profiles.Profile; | 43 import org.chromium.chrome.browser.profiles.Profile; |
| 36 import org.chromium.chrome.browser.tab.Tab; | 44 import org.chromium.chrome.browser.tab.Tab; |
| 37 import org.chromium.chrome.browser.widget.RoundedIconGenerator; | 45 import org.chromium.chrome.browser.widget.RoundedIconGenerator; |
| 46 import org.chromium.chrome.test.ChromeActivityTestRule; | |
| 47 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; | |
| 38 import org.chromium.chrome.test.util.InfoBarUtil; | 48 import org.chromium.chrome.test.util.InfoBarUtil; |
| 39 import org.chromium.chrome.test.util.browser.TabTitleObserver; | 49 import org.chromium.chrome.test.util.browser.TabTitleObserver; |
| 40 import org.chromium.chrome.test.util.browser.notifications.MockNotificationManag erProxy.NotificationEntry; | 50 import org.chromium.chrome.test.util.browser.notifications.MockNotificationManag erProxy.NotificationEntry; |
| 41 import org.chromium.components.url_formatter.UrlFormatter; | 51 import org.chromium.components.url_formatter.UrlFormatter; |
| 42 import org.chromium.content.browser.test.util.Criteria; | 52 import org.chromium.content.browser.test.util.Criteria; |
| 43 import org.chromium.content.browser.test.util.CriteriaHelper; | 53 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 44 | 54 |
| 45 import java.net.URL; | 55 import java.net.URL; |
| 46 import java.util.List; | 56 import java.util.List; |
| 47 import java.util.concurrent.Callable; | 57 import java.util.concurrent.Callable; |
| 48 import java.util.concurrent.ExecutionException; | 58 import java.util.concurrent.ExecutionException; |
| 49 import java.util.concurrent.TimeoutException; | 59 import java.util.concurrent.TimeoutException; |
| 50 | 60 |
| 51 /** | 61 /** |
| 52 * Instrumentation tests for the Notification Bridge. | 62 * Instrumentation tests for the Notification Bridge. |
| 53 * | 63 * |
| 54 * Web Notifications are only supported on Android JellyBean and beyond. | 64 * Web Notifications are only supported on Android JellyBean and beyond. |
| 55 */ | 65 */ |
| 56 public class NotificationPlatformBridgeTest extends NotificationTestBase { | 66 @RunWith(ChromeJUnit4ClassRunner.class) |
| 67 @CommandLineFlags.Add({ | |
| 68 ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE, | |
| 69 ChromeActivityTestRule.DISABLE_NETWORK_PREDICTION_FLAG, | |
|
awdf
2017/05/15 13:23:11
I can see it makes sense to disable the first run
the real yoland
2017/05/15 18:19:32
Aha, this test (and all chrome public test apk tes
| |
| 70 }) | |
| 71 public class NotificationPlatformBridgeTest { | |
| 72 @Rule | |
| 73 public NotificationTestRule mNotificationTestRule = new NotificationTestRule (); | |
| 74 | |
| 57 private static final String NOTIFICATION_TEST_PAGE = | 75 private static final String NOTIFICATION_TEST_PAGE = |
| 58 "/chrome/test/data/notifications/android_test.html"; | 76 "/chrome/test/data/notifications/android_test.html"; |
| 59 private static final int TITLE_UPDATE_TIMEOUT_SECONDS = (int) scaleTimeout(5 ); | 77 private static final int TITLE_UPDATE_TIMEOUT_SECONDS = (int) scaleTimeout(5 ); |
| 60 | 78 |
| 61 @Override | 79 @Before |
| 62 protected void setUp() throws Exception { | 80 public void setUp() throws Exception { |
| 63 super.setUp(); | |
| 64 SiteEngagementService.setParamValuesForTesting(); | 81 SiteEngagementService.setParamValuesForTesting(); |
| 65 loadUrl(getTestServer().getURL(NOTIFICATION_TEST_PAGE)); | 82 mNotificationTestRule.loadUrl( |
| 83 mNotificationTestRule.getTestServer().getURL(NOTIFICATION_TEST_P AGE)); | |
| 66 } | 84 } |
| 67 | 85 |
| 68 private void waitForTitle(String expectedTitle) throws InterruptedException { | 86 private void waitForTitle(String expectedTitle) throws InterruptedException { |
| 69 Tab tab = getActivity().getActivityTab(); | 87 Tab tab = mNotificationTestRule.getActivity().getActivityTab(); |
| 70 TabTitleObserver titleObserver = new TabTitleObserver(tab, expectedTitle ); | 88 TabTitleObserver titleObserver = new TabTitleObserver(tab, expectedTitle ); |
| 71 try { | 89 try { |
| 72 titleObserver.waitForTitleUpdate(TITLE_UPDATE_TIMEOUT_SECONDS); | 90 titleObserver.waitForTitleUpdate(TITLE_UPDATE_TIMEOUT_SECONDS); |
| 73 } catch (TimeoutException e) { | 91 } catch (TimeoutException e) { |
| 74 // The title is not as expected, this assertion neatly logs what the difference is. | 92 // The title is not as expected, this assertion neatly logs what the difference is. |
| 75 assertEquals(expectedTitle, tab.getTitle()); | 93 Assert.assertEquals(expectedTitle, tab.getTitle()); |
|
awdf
2017/05/15 13:23:10
Aside - In some of my recent junit 4 tests I have
the real yoland
2017/05/15 18:19:32
In my experience, as long as they are not deprecat
| |
| 76 } | 94 } |
| 77 } | 95 } |
| 78 | 96 |
| 79 private InfoBar getInfobarBlocking() { | 97 private InfoBar getInfobarBlocking() { |
| 80 CriteriaHelper.pollUiThread(new Criteria() { | 98 CriteriaHelper.pollUiThread(new Criteria() { |
| 81 @Override | 99 @Override |
| 82 public boolean isSatisfied() { | 100 public boolean isSatisfied() { |
| 83 return !getInfoBars().isEmpty(); | 101 return !mNotificationTestRule.getInfoBars().isEmpty(); |
| 84 } | 102 } |
| 85 }); | 103 }); |
| 86 List<InfoBar> infoBars = getInfoBars(); | 104 List<InfoBar> infoBars = mNotificationTestRule.getInfoBars(); |
| 87 assertEquals(1, infoBars.size()); | 105 Assert.assertEquals(1, infoBars.size()); |
| 88 return infoBars.get(0); | 106 return infoBars.get(0); |
| 89 } | 107 } |
| 90 | 108 |
| 91 private void waitForInfobarToClose() { | 109 private void waitForInfobarToClose() { |
| 92 CriteriaHelper.pollUiThread(new Criteria() { | 110 CriteriaHelper.pollUiThread(new Criteria() { |
| 93 @Override | 111 @Override |
| 94 public boolean isSatisfied() { | 112 public boolean isSatisfied() { |
| 95 return getInfoBars().isEmpty(); | 113 return mNotificationTestRule.getInfoBars().isEmpty(); |
| 96 } | 114 } |
| 97 }); | 115 }); |
| 98 assertEquals(0, getInfoBars().size()); | 116 Assert.assertEquals(0, mNotificationTestRule.getInfoBars().size()); |
| 99 } | 117 } |
| 100 | 118 |
| 101 private void checkThatShowNotificationIsDenied() throws Exception { | 119 private void checkThatShowNotificationIsDenied() throws Exception { |
| 102 runJavaScriptCodeInCurrentTab("showNotification('MyNotification', {})"); | 120 mNotificationTestRule.runJavaScriptCodeInCurrentTab( |
| 121 "showNotification('MyNotification', {})"); | |
| 103 waitForTitle("TypeError: No notification permission has been granted for this origin."); | 122 waitForTitle("TypeError: No notification permission has been granted for this origin."); |
| 104 // Ideally we'd wait a little here, but it's hard to wait for things tha t shouldn't happen. | 123 // Ideally we'd wait a little here, but it's hard to wait for things tha t shouldn't happen. |
| 105 assertTrue(getNotificationEntries().isEmpty()); | 124 Assert.assertTrue(mNotificationTestRule.getNotificationEntries().isEmpty ()); |
| 106 } | 125 } |
| 107 | 126 |
| 108 private double getEngagementScoreBlocking() { | 127 private double getEngagementScoreBlocking() { |
| 109 try { | 128 try { |
| 110 return ThreadUtils.runOnUiThreadBlocking(new Callable<Double>() { | 129 return ThreadUtils.runOnUiThreadBlocking(new Callable<Double>() { |
| 111 @Override | 130 @Override |
| 112 public Double call() throws Exception { | 131 public Double call() throws Exception { |
| 113 return SiteEngagementService.getForProfile(Profile.getLastUs edProfile()) | 132 return SiteEngagementService.getForProfile(Profile.getLastUs edProfile()) |
| 114 .getScore(getOrigin()); | 133 .getScore(mNotificationTestRule.getOrigin()); |
| 115 } | 134 } |
| 116 }); | 135 }); |
| 117 } catch (ExecutionException ex) { | 136 } catch (ExecutionException ex) { |
| 118 assert false : "Unexpected ExecutionException"; | 137 assert false : "Unexpected ExecutionException"; |
| 119 } | 138 } |
| 120 return 0.0; | 139 return 0.0; |
| 121 } | 140 } |
| 122 | 141 |
| 123 /** | 142 /** |
| 124 * Verifies that notifcations cannot be shown without permission, and that d ismissing or denying | 143 * Verifies that notifcations cannot be shown without permission, and that d ismissing or denying |
| 125 * the infobar works correctly. | 144 * the infobar works correctly. |
| 126 */ | 145 */ |
| 127 //@LargeTest | 146 //@LargeTest |
| 128 //@Feature({"Browser", "Notifications"}) | 147 //@Feature({"Browser", "Notifications"}) |
| 129 // crbug.com/707528 | 148 // crbug.com/707528 |
| 149 @Test | |
| 130 @DisabledTest | 150 @DisabledTest |
| 131 public void testPermissionDenied() throws Exception { | 151 public void testPermissionDenied() throws Exception { |
| 132 // Notifications permission should initially be prompt, and showing shou ld fail. | 152 // Notifications permission should initially be prompt, and showing shou ld fail. |
| 133 assertEquals("\"default\"", runJavaScriptCodeInCurrentTab("Notification. permission")); | 153 Assert.assertEquals("\"default\"", |
| 154 mNotificationTestRule.runJavaScriptCodeInCurrentTab("Notificatio n.permission")); | |
| 134 checkThatShowNotificationIsDenied(); | 155 checkThatShowNotificationIsDenied(); |
| 135 | 156 |
| 136 // Notification.requestPermission() should show the notifications infoba r. | 157 // Notification.requestPermission() should show the notifications infoba r. |
| 137 assertEquals(0, getInfoBars().size()); | 158 Assert.assertEquals(0, mNotificationTestRule.getInfoBars().size()); |
| 138 runJavaScriptCodeInCurrentTab("Notification.requestPermission(sendToTest )"); | 159 mNotificationTestRule.runJavaScriptCodeInCurrentTab( |
| 160 "Notification.requestPermission(sendToTest)"); | |
| 139 InfoBar infoBar = getInfobarBlocking(); | 161 InfoBar infoBar = getInfobarBlocking(); |
| 140 | 162 |
| 141 // Dismissing the infobar should pass prompt to the requestPermission ca llback. | 163 // Dismissing the infobar should pass prompt to the requestPermission ca llback. |
| 142 assertTrue(InfoBarUtil.clickCloseButton(infoBar)); | 164 Assert.assertTrue(InfoBarUtil.clickCloseButton(infoBar)); |
| 143 waitForInfobarToClose(); | 165 waitForInfobarToClose(); |
| 144 waitForTitle("default"); // See https://crbug.com/434547. | 166 waitForTitle("default"); // See https://crbug.com/434547. |
| 145 | 167 |
| 146 // Notifications permission should still be prompt. | 168 // Notifications permission should still be prompt. |
| 147 assertEquals("\"default\"", runJavaScriptCodeInCurrentTab("Notification. permission")); | 169 Assert.assertEquals("\"default\"", |
| 170 mNotificationTestRule.runJavaScriptCodeInCurrentTab("Notificatio n.permission")); | |
| 148 checkThatShowNotificationIsDenied(); | 171 checkThatShowNotificationIsDenied(); |
| 149 | 172 |
| 150 // Notification.requestPermission() should show the notifications infoba r again. | 173 // Notification.requestPermission() should show the notifications infoba r again. |
| 151 runJavaScriptCodeInCurrentTab("Notification.requestPermission(sendToTest )"); | 174 mNotificationTestRule.runJavaScriptCodeInCurrentTab( |
| 175 "Notification.requestPermission(sendToTest)"); | |
| 152 infoBar = getInfobarBlocking(); | 176 infoBar = getInfobarBlocking(); |
| 153 | 177 |
| 154 // Denying the infobar should pass denied to the requestPermission callb ack. | 178 // Denying the infobar should pass denied to the requestPermission callb ack. |
| 155 assertTrue(InfoBarUtil.clickSecondaryButton(infoBar)); | 179 Assert.assertTrue(InfoBarUtil.clickSecondaryButton(infoBar)); |
| 156 waitForInfobarToClose(); | 180 waitForInfobarToClose(); |
| 157 waitForTitle("denied"); | 181 waitForTitle("denied"); |
| 158 | 182 |
| 159 // This should have caused notifications permission to become denied. | 183 // This should have caused notifications permission to become denied. |
| 160 assertEquals("\"denied\"", runJavaScriptCodeInCurrentTab("Notification.p ermission")); | 184 Assert.assertEquals("\"denied\"", |
| 185 mNotificationTestRule.runJavaScriptCodeInCurrentTab("Notificatio n.permission")); | |
| 161 checkThatShowNotificationIsDenied(); | 186 checkThatShowNotificationIsDenied(); |
| 162 | 187 |
| 163 // Reload page to ensure the block is persisted. | 188 // Reload page to ensure the block is persisted. |
| 164 loadUrl(getTestServer().getURL(NOTIFICATION_TEST_PAGE)); | 189 mNotificationTestRule.loadUrl( |
| 190 mNotificationTestRule.getTestServer().getURL(NOTIFICATION_TEST_P AGE)); | |
| 165 | 191 |
| 166 // Notification.requestPermission() should immediately pass denied to th e callback without | 192 // Notification.requestPermission() should immediately pass denied to th e callback without |
| 167 // showing an infobar. | 193 // showing an infobar. |
| 168 runJavaScriptCodeInCurrentTab("Notification.requestPermission(sendToTest )"); | 194 mNotificationTestRule.runJavaScriptCodeInCurrentTab( |
| 195 "Notification.requestPermission(sendToTest)"); | |
| 169 waitForTitle("denied"); | 196 waitForTitle("denied"); |
| 170 assertEquals(0, getInfoBars().size()); | 197 Assert.assertEquals(0, mNotificationTestRule.getInfoBars().size()); |
| 171 | 198 |
| 172 // Notifications permission should still be denied. | 199 // Notifications permission should still be denied. |
| 173 assertEquals("\"denied\"", runJavaScriptCodeInCurrentTab("Notification.p ermission")); | 200 Assert.assertEquals("\"denied\"", |
| 201 mNotificationTestRule.runJavaScriptCodeInCurrentTab("Notificatio n.permission")); | |
| 174 checkThatShowNotificationIsDenied(); | 202 checkThatShowNotificationIsDenied(); |
| 175 } | 203 } |
| 176 | 204 |
| 177 /** | 205 /** |
| 178 * Verifies granting permission via the infobar. | 206 * Verifies granting permission via the infobar. |
| 179 */ | 207 */ |
| 180 //@MediumTest | 208 //@MediumTest |
| 181 //@Feature({"Browser", "Notifications"}) | 209 //@Feature({"Browser", "Notifications"}) |
| 182 // crbug.com/707528 | 210 // crbug.com/707528 |
| 211 @Test | |
| 183 @DisabledTest | 212 @DisabledTest |
| 184 public void testPermissionGranted() throws Exception { | 213 public void testPermissionGranted() throws Exception { |
| 185 // Notifications permission should initially be prompt, and showing shou ld fail. | 214 // Notifications permission should initially be prompt, and showing shou ld fail. |
| 186 assertEquals("\"default\"", runJavaScriptCodeInCurrentTab("Notification. permission")); | 215 Assert.assertEquals("\"default\"", |
| 216 mNotificationTestRule.runJavaScriptCodeInCurrentTab("Notificatio n.permission")); | |
| 187 checkThatShowNotificationIsDenied(); | 217 checkThatShowNotificationIsDenied(); |
| 188 | 218 |
| 189 // Notification.requestPermission() should show the notifications infoba r. | 219 // Notification.requestPermission() should show the notifications infoba r. |
| 190 assertEquals(0, getInfoBars().size()); | 220 Assert.assertEquals(0, mNotificationTestRule.getInfoBars().size()); |
| 191 runJavaScriptCodeInCurrentTab("Notification.requestPermission(sendToTest )"); | 221 mNotificationTestRule.runJavaScriptCodeInCurrentTab( |
| 222 "Notification.requestPermission(sendToTest)"); | |
| 192 InfoBar infoBar = getInfobarBlocking(); | 223 InfoBar infoBar = getInfobarBlocking(); |
| 193 | 224 |
| 194 // Accepting the infobar should pass granted to the requestPermission ca llback. | 225 // Accepting the infobar should pass granted to the requestPermission ca llback. |
| 195 assertTrue(InfoBarUtil.clickPrimaryButton(infoBar)); | 226 Assert.assertTrue(InfoBarUtil.clickPrimaryButton(infoBar)); |
| 196 waitForInfobarToClose(); | 227 waitForInfobarToClose(); |
| 197 waitForTitle("granted"); | 228 waitForTitle("granted"); |
| 198 | 229 |
| 199 // Reload page to ensure the grant is persisted. | 230 // Reload page to ensure the grant is persisted. |
| 200 loadUrl(getTestServer().getURL(NOTIFICATION_TEST_PAGE)); | 231 mNotificationTestRule.loadUrl( |
| 232 mNotificationTestRule.getTestServer().getURL(NOTIFICATION_TEST_P AGE)); | |
| 201 | 233 |
| 202 // Notifications permission should now be granted, and showing should su cceed. | 234 // Notifications permission should now be granted, and showing should su cceed. |
| 203 assertEquals("\"granted\"", runJavaScriptCodeInCurrentTab("Notification. permission")); | 235 Assert.assertEquals("\"granted\"", |
| 204 showAndGetNotification("MyNotification", "{}"); | 236 mNotificationTestRule.runJavaScriptCodeInCurrentTab("Notificatio n.permission")); |
| 237 mNotificationTestRule.showAndGetNotification("MyNotification", "{}"); | |
| 205 } | 238 } |
| 206 | 239 |
| 207 /** | 240 /** |
| 208 * Verifies that the intended default properties of a notification will inde ed be set on the | 241 * Verifies that the intended default properties of a notification will inde ed be set on the |
| 209 * Notification object that will be send to Android. | 242 * Notification object that will be send to Android. |
| 210 */ | 243 */ |
| 244 @Test | |
| 211 @MediumTest | 245 @MediumTest |
| 212 @Feature({"Browser", "Notifications"}) | 246 @Feature({"Browser", "Notifications"}) |
| 213 @RetryOnFailure | 247 @RetryOnFailure |
| 214 public void testDefaultNotificationProperties() throws Exception { | 248 public void testDefaultNotificationProperties() throws Exception { |
| 215 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 249 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 216 Context context = getInstrumentation().getTargetContext(); | 250 Context context = InstrumentationRegistry.getInstrumentation().getTarget Context(); |
| 217 | 251 |
| 218 Notification notification = showAndGetNotification("MyNotification", "{ body: 'Hello' }"); | 252 Notification notification = |
| 219 String expectedOrigin = | 253 mNotificationTestRule.showAndGetNotification("MyNotification", " { body: 'Hello' }"); |
| 220 UrlFormatter.formatUrlForSecurityDisplay(getOrigin(), false /* s howScheme */); | 254 String expectedOrigin = UrlFormatter.formatUrlForSecurityDisplay( |
| 255 mNotificationTestRule.getOrigin(), false /* showScheme */); | |
| 221 | 256 |
| 222 // Validate the contents of the notification. | 257 // Validate the contents of the notification. |
| 223 assertEquals("MyNotification", NotificationTestUtil.getExtraTitle(notifi cation)); | 258 Assert.assertEquals("MyNotification", NotificationTestUtil.getExtraTitle (notification)); |
| 224 assertEquals("Hello", NotificationTestUtil.getExtraText(notification)); | 259 Assert.assertEquals("Hello", NotificationTestUtil.getExtraText(notificat ion)); |
| 225 assertEquals(expectedOrigin, NotificationTestUtil.getExtraSubText(notifi cation)); | 260 Assert.assertEquals(expectedOrigin, NotificationTestUtil.getExtraSubText (notification)); |
| 226 | 261 |
| 227 // Verify that the ticker text contains the notification's title and bod y. | 262 // Verify that the ticker text contains the notification's title and bod y. |
| 228 String tickerText = notification.tickerText.toString(); | 263 String tickerText = notification.tickerText.toString(); |
| 229 | 264 |
| 230 assertTrue(tickerText.contains("MyNotification")); | 265 Assert.assertTrue(tickerText.contains("MyNotification")); |
| 231 assertTrue(tickerText.contains("Hello")); | 266 Assert.assertTrue(tickerText.contains("Hello")); |
| 232 | 267 |
| 233 // On L+, verify the public version of the notification contains the not ification's origin, | 268 // On L+, verify the public version of the notification contains the not ification's origin, |
| 234 // and that the body text has been replaced. | 269 // and that the body text has been replaced. |
| 235 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | 270 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 236 assertNotNull(notification.publicVersion); | 271 Assert.assertNotNull(notification.publicVersion); |
| 237 assertEquals(context.getString(R.string.notification_hidden_text), | 272 Assert.assertEquals(context.getString(R.string.notification_hidden_t ext), |
| 238 NotificationTestUtil.getExtraText(notification.publicVersion )); | 273 NotificationTestUtil.getExtraText(notification.publicVersion )); |
| 239 } | 274 } |
| 240 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { | 275 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { |
| 241 // On N+, origin should be set as the subtext of the public notifica tion. | 276 // On N+, origin should be set as the subtext of the public notifica tion. |
| 242 assertEquals(expectedOrigin, | 277 Assert.assertEquals(expectedOrigin, |
| 243 NotificationTestUtil.getExtraSubText(notification.publicVers ion)); | 278 NotificationTestUtil.getExtraSubText(notification.publicVers ion)); |
| 244 } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | 279 } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 245 // On L/M, origin should be the title of the public notification. | 280 // On L/M, origin should be the title of the public notification. |
| 246 assertEquals( | 281 Assert.assertEquals( |
| 247 expectedOrigin, NotificationTestUtil.getExtraTitle(notificat ion.publicVersion)); | 282 expectedOrigin, NotificationTestUtil.getExtraTitle(notificat ion.publicVersion)); |
| 248 } | 283 } |
| 249 | 284 |
| 250 // Verify that the notification's timestamp is set in the past 60 second s. This number has | 285 // Verify that the notification's timestamp is set in the past 60 second s. This number has |
| 251 // no significance, but needs to be high enough to not cause flakiness a s it's set by the | 286 // no significance, but needs to be high enough to not cause flakiness a s it's set by the |
| 252 // renderer process on notification creation. | 287 // renderer process on notification creation. |
| 253 assertTrue(Math.abs(System.currentTimeMillis() - notification.when) < 60 * 1000); | 288 Assert.assertTrue(Math.abs(System.currentTimeMillis() - notification.whe n) < 60 * 1000); |
| 254 | 289 |
| 255 assertNotNull(NotificationTestUtil.getLargeIconFromNotification(context, notification)); | 290 Assert.assertNotNull( |
| 291 NotificationTestUtil.getLargeIconFromNotification(context, notif ication)); | |
| 256 | 292 |
| 257 // Validate the notification's behavior. | 293 // Validate the notification's behavior. |
| 258 assertEquals(Notification.DEFAULT_ALL, notification.defaults); | 294 Assert.assertEquals(Notification.DEFAULT_ALL, notification.defaults); |
| 259 assertEquals(Notification.PRIORITY_DEFAULT, notification.priority); | 295 Assert.assertEquals(Notification.PRIORITY_DEFAULT, notification.priority ); |
| 260 } | 296 } |
| 261 | 297 |
| 262 /** | 298 /** |
| 263 * Verifies that specifying a notification action with type: 'text' results in a notification | 299 * Verifies that specifying a notification action with type: 'text' results in a notification |
| 264 * with a remote input on the action. | 300 * with a remote input on the action. |
| 265 */ | 301 */ |
| 302 @Test | |
| 266 @CommandLineFlags.Add("enable-experimental-web-platform-features") | 303 @CommandLineFlags.Add("enable-experimental-web-platform-features") |
| 267 @MinAndroidSdkLevel(Build.VERSION_CODES.KITKAT_WATCH) | 304 @MinAndroidSdkLevel(Build.VERSION_CODES.KITKAT_WATCH) |
| 268 @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) // RemoteInputs were only added in KITKAT_WATCH. | 305 @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) // RemoteInputs were only added in KITKAT_WATCH. |
| 269 @MediumTest | 306 @MediumTest |
| 270 @Feature({"Browser", "Notifications"}) | 307 @Feature({"Browser", "Notifications"}) |
| 271 public void testShowNotificationWithTextAction() throws Exception { | 308 public void testShowNotificationWithTextAction() throws Exception { |
| 272 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 309 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 273 | 310 |
| 274 Notification notification = showAndGetNotification("MyNotification", "{ " | 311 Notification notification = mNotificationTestRule.showAndGetNotification ("MyNotification", |
| 312 "{ " | |
| 275 + " actions: [{action: 'myAction', title: 'reply', type: 'text'," | 313 + " actions: [{action: 'myAction', title: 'reply', type: 'text'," |
| 276 + " placeholder: 'hi' }]}"); | 314 + " placeholder: 'hi' }]}"); |
| 277 | 315 |
| 278 // The specified action should be present, as well as a default settings action. | 316 // The specified action should be present, as well as a default settings action. |
| 279 assertEquals(2, notification.actions.length); | 317 Assert.assertEquals(2, notification.actions.length); |
| 280 | 318 |
| 281 Notification.Action action = notification.actions[0]; | 319 Notification.Action action = notification.actions[0]; |
| 282 assertEquals("reply", action.title); | 320 Assert.assertEquals("reply", action.title); |
| 283 assertNotNull(notification.actions[0].getRemoteInputs()); | 321 Assert.assertNotNull(notification.actions[0].getRemoteInputs()); |
| 284 assertEquals(1, action.getRemoteInputs().length); | 322 Assert.assertEquals(1, action.getRemoteInputs().length); |
| 285 assertEquals("hi", action.getRemoteInputs()[0].getLabel()); | 323 Assert.assertEquals("hi", action.getRemoteInputs()[0].getLabel()); |
| 286 } | 324 } |
| 287 | 325 |
| 288 /** | 326 /** |
| 289 * Verifies that setting a reply on the remote input of a notification actio n with type 'text' | 327 * Verifies that setting a reply on the remote input of a notification actio n with type 'text' |
| 290 * and triggering the action's intent causes the same reply to be received i n the subsequent | 328 * and triggering the action's intent causes the same reply to be received i n the subsequent |
| 291 * notificationclick event on the service worker. Verifies that site engagem ent is incremented | 329 * notificationclick event on the service worker. Verifies that site engagem ent is incremented |
| 292 * appropriately. | 330 * appropriately. |
| 293 */ | 331 */ |
| 332 @Test | |
| 294 @CommandLineFlags.Add("enable-experimental-web-platform-features") | 333 @CommandLineFlags.Add("enable-experimental-web-platform-features") |
| 295 @MinAndroidSdkLevel(Build.VERSION_CODES.KITKAT_WATCH) | 334 @MinAndroidSdkLevel(Build.VERSION_CODES.KITKAT_WATCH) |
| 296 @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) // RemoteInputs were only added in KITKAT_WATCH. | 335 @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) // RemoteInputs were only added in KITKAT_WATCH. |
| 297 @MediumTest | 336 @MediumTest |
| 298 @Feature({"Browser", "Notifications"}) | 337 @Feature({"Browser", "Notifications"}) |
| 299 public void testReplyToNotification() throws Exception { | 338 public void testReplyToNotification() throws Exception { |
| 300 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 339 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 301 Context context = getInstrumentation().getTargetContext(); | 340 Context context = InstrumentationRegistry.getInstrumentation().getTarget Context(); |
| 302 | 341 |
| 303 // +5 engagement from notification permission and +0.5 from navigating t o the test page. | 342 // +5 engagement from notification permission and +0.5 from navigating t o the test page. |
| 304 assertEquals(5.5, getEngagementScoreBlocking()); | 343 Assert.assertEquals(5.5, getEngagementScoreBlocking(), 0); |
|
awdf
2017/05/15 13:23:11
Aside: huh, nice that Assert.assertEquals surfaced
the real yoland
2017/05/15 18:19:32
ya, without the delta value, this would actually f
| |
| 305 Notification notification = showAndGetNotification("MyNotification", "{ " | 344 Notification notification = mNotificationTestRule.showAndGetNotification ("MyNotification", |
| 345 "{ " | |
| 306 + " actions: [{action: 'myAction', title: 'reply', type: 'text'}]," | 346 + " actions: [{action: 'myAction', title: 'reply', type: 'text'}]," |
| 307 + " data: 'ACTION_REPLY'}"); | 347 + " data: 'ACTION_REPLY'}"); |
| 308 | 348 |
| 309 // Check the action is present with a remote input attached. | 349 // Check the action is present with a remote input attached. |
| 310 Notification.Action action = notification.actions[0]; | 350 Notification.Action action = notification.actions[0]; |
| 311 assertEquals("reply", action.title); | 351 Assert.assertEquals("reply", action.title); |
| 312 RemoteInput[] remoteInputs = action.getRemoteInputs(); | 352 RemoteInput[] remoteInputs = action.getRemoteInputs(); |
| 313 assertNotNull(remoteInputs); | 353 Assert.assertNotNull(remoteInputs); |
| 314 | 354 |
| 315 // Set a reply using the action's remote input key and send it on the in tent. | 355 // Set a reply using the action's remote input key and send it on the in tent. |
| 316 sendIntentWithRemoteInput(context, action.actionIntent, remoteInputs, | 356 Helper.sendIntentWithRemoteInput(context, action.actionIntent, remoteInp uts, |
| 317 remoteInputs[0].getResultKey(), "My Reply" /* reply */); | 357 remoteInputs[0].getResultKey(), "My Reply" /* reply */); |
| 318 | 358 |
| 319 // Check reply was received by the service worker (see android_test_work er.js). | 359 // Check reply was received by the service worker (see android_test_work er.js). |
| 320 // Expect +1 engagement from interacting with the notification. | 360 // Expect +1 engagement from interacting with the notification. |
| 321 waitForTitle("reply: My Reply"); | 361 waitForTitle("reply: My Reply"); |
| 322 assertEquals(6.5, getEngagementScoreBlocking()); | 362 Assert.assertEquals(6.5, getEngagementScoreBlocking(), 0); |
| 323 } | 363 } |
| 324 | 364 |
| 325 /** | 365 /** |
| 326 * Verifies that setting an empty reply on the remote input of a notificatio n action with type | 366 * Verifies that setting an empty reply on the remote input of a notificatio n action with type |
| 327 * 'text' and triggering the action's intent causes an empty reply string to be received in the | 367 * 'text' and triggering the action's intent causes an empty reply string to be received in the |
| 328 * subsequent notificationclick event on the service worker. Verifies that s ite engagement is | 368 * subsequent notificationclick event on the service worker. Verifies that s ite engagement is |
| 329 * incremented appropriately. | 369 * incremented appropriately. |
| 330 */ | 370 */ |
| 371 @Test | |
| 331 @CommandLineFlags.Add("enable-experimental-web-platform-features") | 372 @CommandLineFlags.Add("enable-experimental-web-platform-features") |
| 332 @MinAndroidSdkLevel(Build.VERSION_CODES.KITKAT_WATCH) | 373 @MinAndroidSdkLevel(Build.VERSION_CODES.KITKAT_WATCH) |
| 333 @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) // RemoteInputs added in KITKAT _WATCH. | 374 @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) // RemoteInputs added in KITKAT _WATCH. |
| 334 @MediumTest | 375 @MediumTest |
| 335 @Feature({"Browser", "Notifications"}) | 376 @Feature({"Browser", "Notifications"}) |
| 336 public void testReplyToNotificationWithEmptyReply() throws Exception { | 377 public void testReplyToNotificationWithEmptyReply() throws Exception { |
| 337 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 378 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 338 Context context = getInstrumentation().getTargetContext(); | 379 Context context = InstrumentationRegistry.getInstrumentation().getTarget Context(); |
| 339 | 380 |
| 340 // +5 engagement from notification permission and +0.5 from navigating t o the test page. | 381 // +5 engagement from notification permission and +0.5 from navigating t o the test page. |
| 341 assertEquals(5.5, getEngagementScoreBlocking()); | 382 Assert.assertEquals(5.5, getEngagementScoreBlocking(), 0); |
| 342 Notification notification = showAndGetNotification("MyNotification", "{ " | 383 Notification notification = mNotificationTestRule.showAndGetNotification ("MyNotification", |
| 384 "{ " | |
| 343 + " actions: [{action: 'myAction', title: 'reply', type: 'text'}]," | 385 + " actions: [{action: 'myAction', title: 'reply', type: 'text'}]," |
| 344 + " data: 'ACTION_REPLY'}"); | 386 + " data: 'ACTION_REPLY'}"); |
| 345 | 387 |
| 346 // Check the action is present with a remote input attached. | 388 // Check the action is present with a remote input attached. |
| 347 Notification.Action action = notification.actions[0]; | 389 Notification.Action action = notification.actions[0]; |
| 348 assertEquals("reply", action.title); | 390 Assert.assertEquals("reply", action.title); |
| 349 RemoteInput[] remoteInputs = action.getRemoteInputs(); | 391 RemoteInput[] remoteInputs = action.getRemoteInputs(); |
| 350 assertNotNull(remoteInputs); | 392 Assert.assertNotNull(remoteInputs); |
| 351 | 393 |
| 352 // Set a reply using the action's remote input key and send it on the in tent. | 394 // Set a reply using the action's remote input key and send it on the in tent. |
| 353 sendIntentWithRemoteInput(context, action.actionIntent, remoteInputs, | 395 Helper.sendIntentWithRemoteInput(context, action.actionIntent, remoteInp uts, |
| 354 remoteInputs[0].getResultKey(), "" /* reply */); | 396 remoteInputs[0].getResultKey(), "" /* reply */); |
| 355 | 397 |
| 356 // Check empty reply was received by the service worker (see android_tes t_worker.js). | 398 // Check empty reply was received by the service worker (see android_tes t_worker.js). |
| 357 // Expect +1 engagement from interacting with the notification. | 399 // Expect +1 engagement from interacting with the notification. |
| 358 waitForTitle("reply:"); | 400 waitForTitle("reply:"); |
| 359 assertEquals(6.5, getEngagementScoreBlocking()); | 401 Assert.assertEquals(6.5, getEngagementScoreBlocking(), 0); |
| 360 } | 402 } |
| 361 | 403 |
| 362 @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) // RemoteInputs added in KITKAT _WATCH. | 404 //TODO(yolandyan): remove this after supporing SdkSuppress annotation |
|
jbudorick
2017/05/15 02:59:35
What would this require?
also, nits: space after
the real yoland
2017/05/15 18:19:32
This would require test_runner.py side changes, th
| |
| 363 private static void sendIntentWithRemoteInput(Context context, PendingIntent pendingIntent, | 405 //Currently JUnit4 reflection would look for all test methods in a Test clas s, and |
| 364 RemoteInput[] remoteInputs, String resultKey, String reply) | 406 //this test uses RemoteInput as input, this would cause NoClassDefFoundError |
| 365 throws PendingIntent.CanceledException { | 407 //in lower than L devices |
| 366 Bundle results = new Bundle(); | 408 private static class Helper { |
| 367 results.putString(resultKey, reply); | 409 @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) // RemoteInputs added in KI TKAT_WATCH. |
| 368 Intent fillInIntent = new Intent().addFlags(Intent.FLAG_RECEIVER_FOREGRO UND); | 410 private static void sendIntentWithRemoteInput(Context context, PendingIn tent pendingIntent, |
| 369 RemoteInput.addResultsToIntent(remoteInputs, fillInIntent, results); | 411 RemoteInput[] remoteInputs, String resultKey, String reply) |
| 412 throws PendingIntent.CanceledException { | |
| 413 Bundle results = new Bundle(); | |
| 414 results.putString(resultKey, reply); | |
| 415 Intent fillInIntent = new Intent().addFlags(Intent.FLAG_RECEIVER_FOR EGROUND); | |
| 416 RemoteInput.addResultsToIntent(remoteInputs, fillInIntent, results); | |
| 370 | 417 |
| 371 // Send the pending intent filled in with the additional information fro m the new intent. | 418 // Send the pending intent filled in with the additional information from the new |
| 372 pendingIntent.send(context, 0 /* code */, fillInIntent); | 419 // intent. |
| 420 pendingIntent.send(context, 0 /* code */, fillInIntent); | |
| 421 } | |
| 373 } | 422 } |
| 374 | 423 |
| 375 /** | 424 /** |
| 376 * Verifies that *not* setting a reply on the remote input of a notification action with type | 425 * Verifies that *not* setting a reply on the remote input of a notification action with type |
| 377 * 'text' and triggering the action's intent causes a null reply to be recei ved in the | 426 * 'text' and triggering the action's intent causes a null reply to be recei ved in the |
| 378 * subsequent notificationclick event on the service worker. Verifies that site engagement is | 427 * subsequent notificationclick event on the service worker. Verifies that site engagement is |
| 379 * incremented appropriately. | 428 * incremented appropriately. |
| 380 */ | 429 */ |
| 430 @Test | |
| 381 @TargetApi(Build.VERSION_CODES.KITKAT) // Notification.Action.actionIntent a dded in Android K. | 431 @TargetApi(Build.VERSION_CODES.KITKAT) // Notification.Action.actionIntent a dded in Android K. |
| 382 @CommandLineFlags.Add("enable-experimental-web-platform-features") | 432 @CommandLineFlags.Add("enable-experimental-web-platform-features") |
| 383 @MediumTest | 433 @MediumTest |
| 384 @Feature({"Browser", "Notifications"}) | 434 @Feature({"Browser", "Notifications"}) |
| 385 public void testReplyToNotificationWithNoRemoteInput() throws Exception { | 435 public void testReplyToNotificationWithNoRemoteInput() throws Exception { |
| 386 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 436 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 387 | 437 |
| 388 // +5 engagement from notification permission and +0.5 from navigating t o the test page. | 438 // +5 engagement from notification permission and +0.5 from navigating t o the test page. |
| 389 assertEquals(5.5, getEngagementScoreBlocking()); | 439 Assert.assertEquals(5.5, getEngagementScoreBlocking(), 0); |
| 390 Notification notification = showAndGetNotification("MyNotification", "{ " | 440 Notification notification = mNotificationTestRule.showAndGetNotification ("MyNotification", |
| 441 "{ " | |
| 391 + " actions: [{action: 'myAction', title: 'reply', type: 'text'}]," | 442 + " actions: [{action: 'myAction', title: 'reply', type: 'text'}]," |
| 392 + " data: 'ACTION_REPLY'}"); | 443 + " data: 'ACTION_REPLY'}"); |
| 393 | 444 |
| 394 assertEquals("reply", notification.actions[0].title); | 445 Assert.assertEquals("reply", notification.actions[0].title); |
| 395 notification.actions[0].actionIntent.send(); | 446 notification.actions[0].actionIntent.send(); |
| 396 | 447 |
| 397 // Check reply was received by the service worker (see android_test_work er.js). | 448 // Check reply was received by the service worker (see android_test_work er.js). |
| 398 // Expect +1 engagement from interacting with the notification. | 449 // Expect +1 engagement from interacting with the notification. |
| 399 waitForTitle("reply: null"); | 450 waitForTitle("reply: null"); |
| 400 assertEquals(6.5, getEngagementScoreBlocking()); | 451 Assert.assertEquals(6.5, getEngagementScoreBlocking(), 0); |
| 401 } | 452 } |
| 402 | 453 |
| 403 /** | 454 /** |
| 404 * Verifies that the ONLY_ALERT_ONCE flag is not set when renotify is true. | 455 * Verifies that the ONLY_ALERT_ONCE flag is not set when renotify is true. |
| 405 */ | 456 */ |
| 457 @Test | |
| 406 @MediumTest | 458 @MediumTest |
| 407 @Feature({"Browser", "Notifications"}) | 459 @Feature({"Browser", "Notifications"}) |
| 408 public void testNotificationRenotifyProperty() throws Exception { | 460 public void testNotificationRenotifyProperty() throws Exception { |
| 409 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 461 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 410 | 462 |
| 411 Notification notification = | 463 Notification notification = mNotificationTestRule.showAndGetNotification ( |
| 412 showAndGetNotification("MyNotification", "{ tag: 'myTag', renoti fy: true }"); | 464 "MyNotification", "{ tag: 'myTag', renotify: true }"); |
| 413 | 465 |
| 414 assertEquals(0, notification.flags & Notification.FLAG_ONLY_ALERT_ONCE); | 466 Assert.assertEquals(0, notification.flags & Notification.FLAG_ONLY_ALERT _ONCE); |
| 415 } | 467 } |
| 416 | 468 |
| 417 /** | 469 /** |
| 418 * Verifies that notifications created with the "silent" flag do not inherit system defaults | 470 * Verifies that notifications created with the "silent" flag do not inherit system defaults |
| 419 * in regards to their sound, vibration and light indicators. | 471 * in regards to their sound, vibration and light indicators. |
| 420 */ | 472 */ |
| 473 @Test | |
| 421 @MediumTest | 474 @MediumTest |
| 422 @Feature({"Browser", "Notifications"}) | 475 @Feature({"Browser", "Notifications"}) |
| 423 public void testNotificationSilentProperty() throws Exception { | 476 public void testNotificationSilentProperty() throws Exception { |
| 424 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 477 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 425 | 478 |
| 426 Notification notification = showAndGetNotification("MyNotification", "{ silent: true }"); | 479 Notification notification = |
| 480 mNotificationTestRule.showAndGetNotification("MyNotification", " { silent: true }"); | |
| 427 | 481 |
| 428 // Zero indicates that no defaults should be inherited from the system. | 482 // Zero indicates that no defaults should be inherited from the system. |
| 429 assertEquals(0, notification.defaults); | 483 Assert.assertEquals(0, notification.defaults); |
| 430 } | 484 } |
| 431 | 485 |
| 432 private void verifyVibrationNotRequestedWhenDisabledInPrefs(String notificat ionOptions) | 486 private void verifyVibrationNotRequestedWhenDisabledInPrefs(String notificat ionOptions) |
| 433 throws Exception { | 487 throws Exception { |
| 434 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 488 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 435 | 489 |
| 436 // Disable notification vibration in preferences. | 490 // Disable notification vibration in preferences. |
| 437 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 491 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 438 @Override | 492 @Override |
| 439 public void run() { | 493 public void run() { |
| 440 PrefServiceBridge.getInstance().setNotificationsVibrateEnabled(f alse); | 494 PrefServiceBridge.getInstance().setNotificationsVibrateEnabled(f alse); |
| 441 } | 495 } |
| 442 }); | 496 }); |
| 443 | 497 |
| 444 Notification notification = showAndGetNotification("MyNotification", not ificationOptions); | 498 Notification notification = |
| 499 mNotificationTestRule.showAndGetNotification("MyNotification", n otificationOptions); | |
| 445 | 500 |
| 446 // Vibration should not be in the defaults. | 501 // Vibration should not be in the defaults. |
| 447 assertEquals( | 502 Assert.assertEquals( |
| 448 Notification.DEFAULT_ALL & ~Notification.DEFAULT_VIBRATE, notifi cation.defaults); | 503 Notification.DEFAULT_ALL & ~Notification.DEFAULT_VIBRATE, notifi cation.defaults); |
| 449 | 504 |
| 450 // There should be a custom no-op vibration pattern. | 505 // There should be a custom no-op vibration pattern. |
| 451 assertEquals(1, notification.vibrate.length); | 506 Assert.assertEquals(1, notification.vibrate.length); |
| 452 assertEquals(0L, notification.vibrate[0]); | 507 Assert.assertEquals(0L, notification.vibrate[0]); |
| 453 } | 508 } |
| 454 | 509 |
| 455 /** | 510 /** |
| 456 * Verifies that when notification vibration is disabled in preferences and no custom pattern is | 511 * Verifies that when notification vibration is disabled in preferences and no custom pattern is |
| 457 * specified, no vibration is requested from the framework. | 512 * specified, no vibration is requested from the framework. |
| 458 */ | 513 */ |
| 514 @Test | |
| 459 @MediumTest | 515 @MediumTest |
| 460 @Feature({"Browser", "Notifications"}) | 516 @Feature({"Browser", "Notifications"}) |
| 461 @RetryOnFailure | 517 @RetryOnFailure |
| 462 public void testNotificationVibratePreferenceDisabledDefault() throws Except ion { | 518 public void testNotificationVibratePreferenceDisabledDefault() throws Except ion { |
| 463 verifyVibrationNotRequestedWhenDisabledInPrefs("{}"); | 519 verifyVibrationNotRequestedWhenDisabledInPrefs("{}"); |
| 464 } | 520 } |
| 465 | 521 |
| 466 /** | 522 /** |
| 467 * Verifies that when notification vibration is disabled in preferences and a custom pattern is | 523 * Verifies that when notification vibration is disabled in preferences and a custom pattern is |
| 468 * specified, no vibration is requested from the framework. | 524 * specified, no vibration is requested from the framework. |
| 469 */ | 525 */ |
| 526 @Test | |
| 470 @MediumTest | 527 @MediumTest |
| 471 @Feature({"Browser", "Notifications"}) | 528 @Feature({"Browser", "Notifications"}) |
| 472 @RetryOnFailure | 529 @RetryOnFailure |
| 473 public void testNotificationVibratePreferenceDisabledCustomPattern() throws Exception { | 530 public void testNotificationVibratePreferenceDisabledCustomPattern() throws Exception { |
| 474 verifyVibrationNotRequestedWhenDisabledInPrefs("{ vibrate: 42 }"); | 531 verifyVibrationNotRequestedWhenDisabledInPrefs("{ vibrate: 42 }"); |
| 475 } | 532 } |
| 476 | 533 |
| 477 /** | 534 /** |
| 478 * Verifies that by default the notification vibration preference is enabled , and a custom | 535 * Verifies that by default the notification vibration preference is enabled , and a custom |
| 479 * pattern is passed along. | 536 * pattern is passed along. |
| 480 */ | 537 */ |
| 538 @Test | |
| 481 @MediumTest | 539 @MediumTest |
| 482 @Feature({"Browser", "Notifications"}) | 540 @Feature({"Browser", "Notifications"}) |
| 483 public void testNotificationVibrateCustomPattern() throws Exception { | 541 public void testNotificationVibrateCustomPattern() throws Exception { |
| 484 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 542 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 485 | 543 |
| 486 // By default, vibration is enabled in notifications. | 544 // By default, vibration is enabled in notifications. |
| 487 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 545 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 488 @Override | 546 @Override |
| 489 public void run() { | 547 public void run() { |
| 490 assertTrue(PrefServiceBridge.getInstance().isNotificationsVibrat eEnabled()); | 548 Assert.assertTrue(PrefServiceBridge.getInstance().isNotification sVibrateEnabled()); |
| 491 } | 549 } |
| 492 }); | 550 }); |
| 493 | 551 |
| 494 Notification notification = showAndGetNotification("MyNotification", "{ vibrate: 42 }"); | 552 Notification notification = |
| 553 mNotificationTestRule.showAndGetNotification("MyNotification", " { vibrate: 42 }"); | |
| 495 | 554 |
| 496 // Vibration should not be in the defaults, a custom pattern was provide d. | 555 // Vibration should not be in the defaults, a custom pattern was provide d. |
| 497 assertEquals( | 556 Assert.assertEquals( |
| 498 Notification.DEFAULT_ALL & ~Notification.DEFAULT_VIBRATE, notifi cation.defaults); | 557 Notification.DEFAULT_ALL & ~Notification.DEFAULT_VIBRATE, notifi cation.defaults); |
| 499 | 558 |
| 500 // The custom pattern should have been passed along. | 559 // The custom pattern should have been passed along. |
| 501 assertEquals(2, notification.vibrate.length); | 560 Assert.assertEquals(2, notification.vibrate.length); |
| 502 assertEquals(0L, notification.vibrate[0]); | 561 Assert.assertEquals(0L, notification.vibrate[0]); |
| 503 assertEquals(42L, notification.vibrate[1]); | 562 Assert.assertEquals(42L, notification.vibrate[1]); |
| 504 } | 563 } |
| 505 | 564 |
| 506 /** | 565 /** |
| 507 * Verifies that on Android M+, notifications which specify a badge will hav e that icon | 566 * Verifies that on Android M+, notifications which specify a badge will hav e that icon |
| 508 * fetched and included as the small icon in the notification and public ver sion. | 567 * fetched and included as the small icon in the notification and public ver sion. |
| 509 * If the test target is L or below, verifies the small icon (and public sma ll icon on L) is | 568 * If the test target is L or below, verifies the small icon (and public sma ll icon on L) is |
| 510 * the expected chrome logo. | 569 * the expected chrome logo. |
| 511 */ | 570 */ |
| 571 @Test | |
| 512 @MediumTest | 572 @MediumTest |
| 513 @Feature({"Browser", "Notifications"}) | 573 @Feature({"Browser", "Notifications"}) |
| 514 public void testShowNotificationWithBadge() throws Exception { | 574 public void testShowNotificationWithBadge() throws Exception { |
| 515 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 575 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 516 | 576 |
| 517 Notification notification = | 577 Notification notification = mNotificationTestRule.showAndGetNotification ( |
| 518 showAndGetNotification("MyNotification", "{badge: 'badge.png'}") ; | 578 "MyNotification", "{badge: 'badge.png'}"); |
| 519 | 579 |
| 520 assertEquals("MyNotification", NotificationTestUtil.getExtraTitle(notifi cation)); | 580 Assert.assertEquals("MyNotification", NotificationTestUtil.getExtraTitle (notification)); |
| 521 | 581 |
| 522 Context context = getInstrumentation().getTargetContext(); | 582 Context context = InstrumentationRegistry.getInstrumentation().getTarget Context(); |
| 523 Bitmap smallIcon = NotificationTestUtil.getSmallIconFromNotification(con text, notification); | 583 Bitmap smallIcon = NotificationTestUtil.getSmallIconFromNotification(con text, notification); |
| 524 assertNotNull(smallIcon); | 584 Assert.assertNotNull(smallIcon); |
| 525 | 585 |
| 526 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | 586 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 527 // Custom badges are only supported on M+. | 587 // Custom badges are only supported on M+. |
| 528 // 1. Check the notification badge. | 588 // 1. Check the notification badge. |
| 529 URL badgeUrl = | 589 URL badgeUrl = new URL(mNotificationTestRule.getTestServer().getURL( |
| 530 new URL(getTestServer().getURL("/chrome/test/data/notificati ons/badge.png")); | 590 "/chrome/test/data/notifications/badge.png")); |
| 531 Bitmap bitmap = BitmapFactory.decodeStream(badgeUrl.openStream()); | 591 Bitmap bitmap = BitmapFactory.decodeStream(badgeUrl.openStream()); |
| 532 Bitmap expected = bitmap.copy(bitmap.getConfig(), true); | 592 Bitmap expected = bitmap.copy(bitmap.getConfig(), true); |
| 533 NotificationBuilderBase.applyWhiteOverlayToBitmap(expected); | 593 NotificationBuilderBase.applyWhiteOverlayToBitmap(expected); |
| 534 assertTrue(expected.sameAs(smallIcon)); | 594 Assert.assertTrue(expected.sameAs(smallIcon)); |
| 535 | 595 |
| 536 // 2. Check the public notification badge. | 596 // 2. Check the public notification badge. |
| 537 assertNotNull(notification.publicVersion); | 597 Assert.assertNotNull(notification.publicVersion); |
| 538 Bitmap publicSmallIcon = NotificationTestUtil.getSmallIconFromNotifi cation( | 598 Bitmap publicSmallIcon = NotificationTestUtil.getSmallIconFromNotifi cation( |
| 539 context, notification.publicVersion); | 599 context, notification.publicVersion); |
| 540 assertNotNull(publicSmallIcon); | 600 Assert.assertNotNull(publicSmallIcon); |
| 541 assertEquals(expected.getWidth(), publicSmallIcon.getWidth()); | 601 Assert.assertEquals(expected.getWidth(), publicSmallIcon.getWidth()) ; |
| 542 assertEquals(expected.getHeight(), publicSmallIcon.getHeight()); | 602 Assert.assertEquals(expected.getHeight(), publicSmallIcon.getHeight( )); |
| 543 assertTrue(expected.sameAs(publicSmallIcon)); | 603 Assert.assertTrue(expected.sameAs(publicSmallIcon)); |
| 544 } else { | 604 } else { |
| 545 Bitmap expected = | 605 Bitmap expected = |
| 546 BitmapFactory.decodeResource(context.getResources(), R.drawa ble.ic_chrome); | 606 BitmapFactory.decodeResource(context.getResources(), R.drawa ble.ic_chrome); |
| 547 assertTrue(expected.sameAs(smallIcon)); | 607 Assert.assertTrue(expected.sameAs(smallIcon)); |
| 548 | 608 |
| 549 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | 609 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 550 // Public versions of notifications are only supported on L+. | 610 // Public versions of notifications are only supported on L+. |
| 551 assertNotNull(notification.publicVersion); | 611 Assert.assertNotNull(notification.publicVersion); |
| 552 Bitmap publicSmallIcon = NotificationTestUtil.getSmallIconFromNo tification( | 612 Bitmap publicSmallIcon = NotificationTestUtil.getSmallIconFromNo tification( |
| 553 context, notification.publicVersion); | 613 context, notification.publicVersion); |
| 554 assertTrue(expected.sameAs(publicSmallIcon)); | 614 Assert.assertTrue(expected.sameAs(publicSmallIcon)); |
| 555 } | 615 } |
| 556 } | 616 } |
| 557 } | 617 } |
| 558 | 618 |
| 559 /** | 619 /** |
| 560 * Verifies that notifications which specify an icon will have that icon fet ched, converted into | 620 * Verifies that notifications which specify an icon will have that icon fet ched, converted into |
| 561 * a Bitmap and included as the large icon in the notification. | 621 * a Bitmap and included as the large icon in the notification. |
| 562 */ | 622 */ |
| 623 @Test | |
| 563 @MediumTest | 624 @MediumTest |
| 564 @Feature({"Browser", "Notifications"}) | 625 @Feature({"Browser", "Notifications"}) |
| 565 @RetryOnFailure | 626 @RetryOnFailure |
| 566 public void testShowNotificationWithIcon() throws Exception { | 627 public void testShowNotificationWithIcon() throws Exception { |
| 567 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 628 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 568 | 629 |
| 569 Notification notification = showAndGetNotification("MyNotification", "{i con: 'red.png'}"); | 630 Notification notification = |
| 631 mNotificationTestRule.showAndGetNotification("MyNotification", " {icon: 'red.png'}"); | |
| 570 | 632 |
| 571 assertEquals("MyNotification", NotificationTestUtil.getExtraTitle(notifi cation)); | 633 Assert.assertEquals("MyNotification", NotificationTestUtil.getExtraTitle (notification)); |
| 572 | 634 |
| 573 Context context = getInstrumentation().getTargetContext(); | 635 Context context = InstrumentationRegistry.getInstrumentation().getTarget Context(); |
| 574 Bitmap largeIcon = NotificationTestUtil.getLargeIconFromNotification(con text, notification); | 636 Bitmap largeIcon = NotificationTestUtil.getLargeIconFromNotification(con text, notification); |
| 575 assertNotNull(largeIcon); | 637 Assert.assertNotNull(largeIcon); |
| 576 assertEquals(Color.RED, largeIcon.getPixel(0, 0)); | 638 Assert.assertEquals(Color.RED, largeIcon.getPixel(0, 0)); |
| 577 } | 639 } |
| 578 | 640 |
| 579 /** | 641 /** |
| 580 * Verifies that notifications which don't specify an icon will get an autom atically generated | 642 * Verifies that notifications which don't specify an icon will get an autom atically generated |
| 581 * icon based on their origin. The size of these icons are dependent on the resolution of the | 643 * icon based on their origin. The size of these icons are dependent on the resolution of the |
| 582 * device the test is being ran on, so we create it again in order to compar e. | 644 * device the test is being ran on, so we create it again in order to compar e. |
| 583 */ | 645 */ |
| 646 @Test | |
| 584 @MediumTest | 647 @MediumTest |
| 585 @Feature({"Browser", "Notifications"}) | 648 @Feature({"Browser", "Notifications"}) |
| 586 @RetryOnFailure | 649 @RetryOnFailure |
| 587 public void testShowNotificationWithoutIcon() throws Exception { | 650 public void testShowNotificationWithoutIcon() throws Exception { |
| 588 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 651 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 589 | 652 |
| 590 Notification notification = showAndGetNotification("NoIconNotification", "{}"); | 653 Notification notification = |
| 654 mNotificationTestRule.showAndGetNotification("NoIconNotification ", "{}"); | |
| 591 | 655 |
| 592 assertEquals("NoIconNotification", NotificationTestUtil.getExtraTitle(no tification)); | 656 Assert.assertEquals("NoIconNotification", NotificationTestUtil.getExtraT itle(notification)); |
| 593 | 657 |
| 594 Context context = getInstrumentation().getTargetContext(); | 658 Context context = InstrumentationRegistry.getInstrumentation().getTarget Context(); |
| 595 assertNotNull(NotificationTestUtil.getLargeIconFromNotification(context, notification)); | 659 Assert.assertNotNull( |
| 660 NotificationTestUtil.getLargeIconFromNotification(context, notif ication)); | |
| 596 | 661 |
| 597 NotificationPlatformBridge notificationBridge = | 662 NotificationPlatformBridge notificationBridge = |
| 598 NotificationPlatformBridge.getInstanceForTests(); | 663 NotificationPlatformBridge.getInstanceForTests(); |
| 599 assertNotNull(notificationBridge); | 664 Assert.assertNotNull(notificationBridge); |
| 600 | 665 |
| 601 // Create a second rounded icon for the test's origin, and compare its d imensions against | 666 // Create a second rounded icon for the test's origin, and compare its d imensions against |
| 602 // those of the icon associated to the notification itself. | 667 // those of the icon associated to the notification itself. |
| 603 RoundedIconGenerator generator = | 668 RoundedIconGenerator generator = |
| 604 NotificationBuilderBase.createIconGenerator(context.getResources ()); | 669 NotificationBuilderBase.createIconGenerator(context.getResources ()); |
| 605 | 670 |
| 606 Bitmap generatedIcon = generator.generateIconForUrl(getOrigin()); | 671 Bitmap generatedIcon = generator.generateIconForUrl(mNotificationTestRul e.getOrigin()); |
| 607 assertNotNull(generatedIcon); | 672 Assert.assertNotNull(generatedIcon); |
| 608 assertTrue(generatedIcon.sameAs( | 673 Assert.assertTrue(generatedIcon.sameAs( |
| 609 NotificationTestUtil.getLargeIconFromNotification(context, notif ication))); | 674 NotificationTestUtil.getLargeIconFromNotification(context, notif ication))); |
| 610 } | 675 } |
| 611 | 676 |
| 612 /* | 677 /* |
| 613 * Verifies that starting the PendingIntent stored as the notification's con tent intent will | 678 * Verifies that starting the PendingIntent stored as the notification's con tent intent will |
| 614 * start up the associated Service Worker, where the JavaScript code will cl ose the notification | 679 * start up the associated Service Worker, where the JavaScript code will cl ose the notification |
| 615 * by calling event.notification.close(). | 680 * by calling event.notification.close(). |
| 616 */ | 681 */ |
| 682 @Test | |
| 617 @LargeTest | 683 @LargeTest |
| 618 @Feature({"Browser", "Notifications"}) | 684 @Feature({"Browser", "Notifications"}) |
| 619 @RetryOnFailure | 685 @RetryOnFailure |
| 620 public void testNotificationContentIntentClosesNotification() throws Excepti on { | 686 public void testNotificationContentIntentClosesNotification() throws Excepti on { |
| 621 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 687 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 622 // +5 engagement from notification permission and +0.5 from navigating t o the test page. | 688 // +5 engagement from notification permission and +0.5 from navigating t o the test page. |
| 623 assertEquals(5.5, getEngagementScoreBlocking()); | 689 Assert.assertEquals(5.5, getEngagementScoreBlocking(), 0); |
| 624 | 690 |
| 625 Notification notification = showAndGetNotification("MyNotification", "{} "); | 691 Notification notification = |
| 692 mNotificationTestRule.showAndGetNotification("MyNotification", " {}"); | |
| 626 | 693 |
| 627 // Sending the PendingIntent resembles activating the notification. | 694 // Sending the PendingIntent resembles activating the notification. |
| 628 assertNotNull(notification.contentIntent); | 695 Assert.assertNotNull(notification.contentIntent); |
| 629 notification.contentIntent.send(); | 696 notification.contentIntent.send(); |
| 630 | 697 |
| 631 // The Service Worker will close the notification upon receiving the not ificationclick | 698 // The Service Worker will close the notification upon receiving the not ificationclick |
| 632 // event. This will eventually bubble up to a call to cancel() in the No tificationManager. | 699 // event. This will eventually bubble up to a call to cancel() in the No tificationManager. |
| 633 // Expect +1 engagement from interacting with the notification. | 700 // Expect +1 engagement from interacting with the notification. |
| 634 waitForNotificationManagerMutation(); | 701 mNotificationTestRule.waitForNotificationManagerMutation(); |
| 635 assertTrue(getNotificationEntries().isEmpty()); | 702 Assert.assertTrue(mNotificationTestRule.getNotificationEntries().isEmpty ()); |
| 636 assertEquals(6.5, getEngagementScoreBlocking()); | 703 Assert.assertEquals(6.5, getEngagementScoreBlocking(), 0); |
| 637 } | 704 } |
| 638 | 705 |
| 639 /** | 706 /** |
| 640 * Verifies that starting the PendingIntent stored as the notification's con tent intent will | 707 * Verifies that starting the PendingIntent stored as the notification's con tent intent will |
| 641 * start up the associated Service Worker, where the JavaScript code will cr eate a new tab for | 708 * start up the associated Service Worker, where the JavaScript code will cr eate a new tab for |
| 642 * displaying the notification's event to the user. | 709 * displaying the notification's event to the user. |
| 643 */ | 710 */ |
| 711 @Test | |
| 644 @LargeTest | 712 @LargeTest |
| 645 @Feature({"Browser", "Notifications"}) | 713 @Feature({"Browser", "Notifications"}) |
| 646 @RetryOnFailure | 714 @RetryOnFailure |
| 647 public void testNotificationContentIntentCreatesTab() throws Exception { | 715 public void testNotificationContentIntentCreatesTab() throws Exception { |
| 648 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 716 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 649 | 717 |
| 650 assertEquals("Expected the notification test page to be the sole tab in the current model", | 718 Assert.assertEquals( |
| 651 1, getActivity().getCurrentTabModel().getCount()); | 719 "Expected the notification test page to be the sole tab in the c urrent model", 1, |
| 720 mNotificationTestRule.getActivity().getCurrentTabModel().getCoun t()); | |
| 652 | 721 |
| 653 Notification notification = | 722 Notification notification = mNotificationTestRule.showAndGetNotification ( |
| 654 showAndGetNotification("MyNotification", "{ data: 'ACTION_CREATE _TAB' }"); | 723 "MyNotification", "{ data: 'ACTION_CREATE_TAB' }"); |
| 655 | 724 |
| 656 // Sending the PendingIntent resembles activating the notification. | 725 // Sending the PendingIntent resembles activating the notification. |
| 657 assertNotNull(notification.contentIntent); | 726 Assert.assertNotNull(notification.contentIntent); |
| 658 notification.contentIntent.send(); | 727 notification.contentIntent.send(); |
| 659 | 728 |
| 660 // The Service Worker, upon receiving the notificationclick event, will create a new tab | 729 // The Service Worker, upon receiving the notificationclick event, will create a new tab |
| 661 // after which it closes the notification. | 730 // after which it closes the notification. |
| 662 waitForNotificationManagerMutation(); | 731 mNotificationTestRule.waitForNotificationManagerMutation(); |
| 663 assertTrue(getNotificationEntries().isEmpty()); | 732 Assert.assertTrue(mNotificationTestRule.getNotificationEntries().isEmpty ()); |
| 664 | 733 |
| 665 CriteriaHelper.pollInstrumentationThread(new Criteria("Expected a new ta b to be created") { | 734 CriteriaHelper.pollInstrumentationThread(new Criteria("Expected a new ta b to be created") { |
| 666 @Override | 735 @Override |
| 667 public boolean isSatisfied() { | 736 public boolean isSatisfied() { |
| 668 return 2 == getActivity().getCurrentTabModel().getCount(); | 737 return 2 == mNotificationTestRule.getActivity().getCurrentTabMod el().getCount(); |
| 669 } | 738 } |
| 670 }); | 739 }); |
| 671 } | 740 } |
| 672 | 741 |
| 673 /** | 742 /** |
| 674 * Verifies that creating a notification with an associated "tag" will cause any previous | 743 * Verifies that creating a notification with an associated "tag" will cause any previous |
| 675 * notification with the same tag to be dismissed prior to being shown. | 744 * notification with the same tag to be dismissed prior to being shown. |
| 676 */ | 745 */ |
| 746 @Test | |
| 677 @SuppressFBWarnings("DLS_DEAD_LOCAL_STORE") | 747 @SuppressFBWarnings("DLS_DEAD_LOCAL_STORE") |
| 678 @MediumTest | 748 @MediumTest |
| 679 @Feature({"Browser", "Notifications"}) | 749 @Feature({"Browser", "Notifications"}) |
| 680 public void testNotificationTagReplacement() throws Exception { | 750 public void testNotificationTagReplacement() throws Exception { |
| 681 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 751 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 682 // +5 engagement from notification permission and +0.5 from navigating t o the test page. | 752 // +5 engagement from notification permission and +0.5 from navigating t o the test page. |
| 683 assertEquals(5.5, getEngagementScoreBlocking()); | 753 Assert.assertEquals(5.5, getEngagementScoreBlocking(), 0); |
| 684 | 754 |
| 685 runJavaScriptCodeInCurrentTab("showNotification('MyNotification', {tag: 'myTag'});"); | 755 mNotificationTestRule.runJavaScriptCodeInCurrentTab( |
| 686 waitForNotificationManagerMutation(); | 756 "showNotification('MyNotification', {tag: 'myTag'});"); |
| 687 List<NotificationEntry> notifications = getNotificationEntries(); | 757 mNotificationTestRule.waitForNotificationManagerMutation(); |
| 758 List<NotificationEntry> notifications = mNotificationTestRule.getNotific ationEntries(); | |
| 688 String tag = notifications.get(0).tag; | 759 String tag = notifications.get(0).tag; |
| 689 int id = notifications.get(0).id; | 760 int id = notifications.get(0).id; |
| 690 | 761 |
| 691 runJavaScriptCodeInCurrentTab("showNotification('SecondNotification', {t ag: 'myTag'});"); | 762 mNotificationTestRule.runJavaScriptCodeInCurrentTab( |
| 692 waitForNotificationManagerMutation(); | 763 "showNotification('SecondNotification', {tag: 'myTag'});"); |
| 764 mNotificationTestRule.waitForNotificationManagerMutation(); | |
| 693 | 765 |
| 694 // Verify that the notification was successfully replaced. | 766 // Verify that the notification was successfully replaced. |
| 695 notifications = getNotificationEntries(); | 767 notifications = mNotificationTestRule.getNotificationEntries(); |
| 696 assertEquals(1, notifications.size()); | 768 Assert.assertEquals(1, notifications.size()); |
| 697 assertEquals("SecondNotification", | 769 Assert.assertEquals("SecondNotification", |
| 698 NotificationTestUtil.getExtraTitle(notifications.get(0).notifica tion)); | 770 NotificationTestUtil.getExtraTitle(notifications.get(0).notifica tion)); |
| 699 | 771 |
| 700 // Verify that for replaced notifications their tag was the same. | 772 // Verify that for replaced notifications their tag was the same. |
| 701 assertEquals(tag, notifications.get(0).tag); | 773 Assert.assertEquals(tag, notifications.get(0).tag); |
| 702 | 774 |
| 703 // Verify that as always, the same integer is used, also for replaced no tifications. | 775 // Verify that as always, the same integer is used, also for replaced no tifications. |
| 704 assertEquals(id, notifications.get(0).id); | 776 Assert.assertEquals(id, notifications.get(0).id); |
| 705 assertEquals(NotificationPlatformBridge.PLATFORM_ID, notifications.get(0 ).id); | 777 Assert.assertEquals(NotificationPlatformBridge.PLATFORM_ID, notification s.get(0).id); |
| 706 | 778 |
| 707 // Engagement should not have changed since we didn't interact. | 779 // Engagement should not have changed since we didn't interact. |
| 708 assertEquals(5.5, getEngagementScoreBlocking()); | 780 Assert.assertEquals(5.5, getEngagementScoreBlocking(), 0); |
| 709 } | 781 } |
| 710 | 782 |
| 711 /** | 783 /** |
| 712 * Verifies that multiple notifications without a tag can be opened and clos ed without | 784 * Verifies that multiple notifications without a tag can be opened and clos ed without |
| 713 * affecting eachother. | 785 * affecting eachother. |
| 714 */ | 786 */ |
| 787 @Test | |
| 715 @LargeTest | 788 @LargeTest |
| 716 @Feature({"Browser", "Notifications"}) | 789 @Feature({"Browser", "Notifications"}) |
| 717 public void testShowAndCloseMultipleNotifications() throws Exception { | 790 public void testShowAndCloseMultipleNotifications() throws Exception { |
| 718 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 791 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont entSetting.ALLOW); |
| 719 // +5 engagement from notification permission and +0.5 from navigating t o the test page. | 792 // +5 engagement from notification permission and +0.5 from navigating t o the test page. |
| 720 assertEquals(5.5, getEngagementScoreBlocking()); | 793 Assert.assertEquals(5.5, getEngagementScoreBlocking(), 0); |
| 721 | 794 |
| 722 // Open the first notification and verify it is displayed. | 795 // Open the first notification and verify it is displayed. |
| 723 runJavaScriptCodeInCurrentTab("showNotification('One');"); | 796 mNotificationTestRule.runJavaScriptCodeInCurrentTab("showNotification('O ne');"); |
| 724 waitForNotificationManagerMutation(); | 797 mNotificationTestRule.waitForNotificationManagerMutation(); |
| 725 List<NotificationEntry> notifications = getNotificationEntries(); | 798 List<NotificationEntry> notifications = mNotificationTestRule.getNotific ationEntries(); |
| 726 assertEquals(1, notifications.size()); | 799 Assert.assertEquals(1, notifications.size()); |
| 727 Notification notificationOne = notifications.get(0).notification; | 800 Notification notificationOne = notifications.get(0).notification; |
| 728 assertEquals("One", NotificationTestUtil.getExtraTitle(notificationOne)) ; | 801 Assert.assertEquals("One", NotificationTestUtil.getExtraTitle(notificati onOne)); |
| 729 | 802 |
| 730 // Open the second notification and verify it is displayed. | 803 // Open the second notification and verify it is displayed. |
| 731 runJavaScriptCodeInCurrentTab("showNotification('Two');"); | 804 mNotificationTestRule.runJavaScriptCodeInCurrentTab("showNotification('T wo');"); |
| 732 waitForNotificationManagerMutation(); | 805 mNotificationTestRule.waitForNotificationManagerMutation(); |
| 733 notifications = getNotificationEntries(); | 806 notifications = mNotificationTestRule.getNotificationEntries(); |
| 734 assertEquals(2, notifications.size()); | 807 Assert.assertEquals(2, notifications.size()); |
| 735 Notification notificationTwo = notifications.get(1).notification; | 808 Notification notificationTwo = notifications.get(1).notification; |
| 736 assertEquals("Two", NotificationTestUtil.getExtraTitle(notificationTwo)) ; | 809 Assert.assertEquals("Two", NotificationTestUtil.getExtraTitle(notificati onTwo)); |
| 737 | 810 |
| 738 // The same integer id is always used as it is not needed for uniqueness , we rely on the tag | 811 // The same integer id is always used as it is not needed for uniqueness , we rely on the tag |
| 739 // for uniqueness when the replacement behavior is not needed. | 812 // for uniqueness when the replacement behavior is not needed. |
| 740 assertEquals(NotificationPlatformBridge.PLATFORM_ID, notifications.get(0 ).id); | 813 Assert.assertEquals(NotificationPlatformBridge.PLATFORM_ID, notification s.get(0).id); |
| 741 assertEquals(NotificationPlatformBridge.PLATFORM_ID, notifications.get(1 ).id); | 814 Assert.assertEquals(NotificationPlatformBridge.PLATFORM_ID, notification s.get(1).id); |
| 742 | 815 |
| 743 // As these notifications were not meant to replace eachother, they must not have the same | 816 // As these notifications were not meant to replace eachother, they must not have the same |
| 744 // tag internally. | 817 // tag internally. |
| 745 assertFalse(notifications.get(0).tag.equals(notifications.get(1).tag)); | 818 Assert.assertFalse(notifications.get(0).tag.equals(notifications.get(1). tag)); |
| 746 | 819 |
| 747 // Verify that the PendingIntent for content and delete is different for each notification. | 820 // Verify that the PendingIntent for content and delete is different for each notification. |
| 748 assertFalse(notificationOne.contentIntent.equals(notificationTwo.content Intent)); | 821 Assert.assertFalse(notificationOne.contentIntent.equals(notificationTwo. contentIntent)); |
| 749 assertFalse(notificationOne.deleteIntent.equals(notificationTwo.deleteIn tent)); | 822 Assert.assertFalse(notificationOne.deleteIntent.equals(notificationTwo.d eleteIntent)); |
| 750 | 823 |
| 751 // Close the first notification and verify that only the second remains. | 824 // Close the first notification and verify that only the second remains. |
| 752 // Sending the content intent resembles touching the notification. In re sponse tho this the | 825 // Sending the content intent resembles touching the notification. In re sponse tho this the |
| 753 // notificationclick event is fired. The test service worker will close the notification | 826 // notificationclick event is fired. The test service worker will close the notification |
| 754 // upon receiving the event. | 827 // upon receiving the event. |
| 755 notificationOne.contentIntent.send(); | 828 notificationOne.contentIntent.send(); |
| 756 waitForNotificationManagerMutation(); | 829 mNotificationTestRule.waitForNotificationManagerMutation(); |
| 757 notifications = getNotificationEntries(); | 830 notifications = mNotificationTestRule.getNotificationEntries(); |
| 758 assertEquals(1, notifications.size()); | 831 Assert.assertEquals(1, notifications.size()); |
| 759 assertEquals("Two", NotificationTestUtil.getExtraTitle(notifications.get (0).notification)); | 832 Assert.assertEquals( |
| 833 "Two", NotificationTestUtil.getExtraTitle(notifications.get(0).n otification)); | |
| 760 | 834 |
| 761 // Expect +1 engagement from interacting with the notification. | 835 // Expect +1 engagement from interacting with the notification. |
| 762 assertEquals(6.5, getEngagementScoreBlocking()); | 836 Assert.assertEquals(6.5, getEngagementScoreBlocking(), 0); |
| 763 | 837 |
| 764 // Close the last notification and verify that none remain. | 838 // Close the last notification and verify that none remain. |
| 765 notifications.get(0).notification.contentIntent.send(); | 839 notifications.get(0).notification.contentIntent.send(); |
| 766 waitForNotificationManagerMutation(); | 840 mNotificationTestRule.waitForNotificationManagerMutation(); |
| 767 assertTrue(getNotificationEntries().isEmpty()); | 841 Assert.assertTrue(mNotificationTestRule.getNotificationEntries().isEmpty ()); |
| 768 | 842 |
| 769 // Expect +1 engagement from interacting with the notification. | 843 // Expect +1 engagement from interacting with the notification. |
| 770 assertEquals(7.5, getEngagementScoreBlocking()); | 844 Assert.assertEquals(7.5, getEngagementScoreBlocking(), 0); |
| 771 } | 845 } |
| 772 } | 846 } |
| OLD | NEW |