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