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.push_messaging; | 5 package org.chromium.chrome.browser.push_messaging; |
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.SuppressLint; | 9 import android.annotation.SuppressLint; |
10 import android.app.Notification; | 10 import android.app.Notification; |
11 import android.content.Context; | 11 import android.content.Context; |
12 import android.os.Bundle; | 12 import android.os.Bundle; |
| 13 import android.support.test.InstrumentationRegistry; |
13 import android.support.test.filters.LargeTest; | 14 import android.support.test.filters.LargeTest; |
14 import android.support.test.filters.MediumTest; | 15 import android.support.test.filters.MediumTest; |
15 import android.test.MoreAsserts; | 16 import android.test.MoreAsserts; |
16 import android.util.Pair; | 17 import android.util.Pair; |
17 | 18 |
| 19 import org.junit.After; |
| 20 import org.junit.Assert; |
| 21 import org.junit.Before; |
| 22 import org.junit.Rule; |
| 23 import org.junit.Test; |
| 24 import org.junit.runner.RunWith; |
| 25 |
18 import org.chromium.base.ThreadUtils; | 26 import org.chromium.base.ThreadUtils; |
19 import org.chromium.base.library_loader.ProcessInitException; | 27 import org.chromium.base.library_loader.ProcessInitException; |
20 import org.chromium.base.test.util.CallbackHelper; | 28 import org.chromium.base.test.util.CallbackHelper; |
21 import org.chromium.base.test.util.CommandLineFlags; | 29 import org.chromium.base.test.util.CommandLineFlags; |
22 import org.chromium.base.test.util.DisabledTest; | 30 import org.chromium.base.test.util.DisabledTest; |
23 import org.chromium.base.test.util.Feature; | 31 import org.chromium.base.test.util.Feature; |
24 import org.chromium.base.test.util.RetryOnFailure; | 32 import org.chromium.base.test.util.RetryOnFailure; |
| 33 import org.chromium.chrome.browser.ChromeSwitches; |
25 import org.chromium.chrome.browser.infobar.InfoBar; | 34 import org.chromium.chrome.browser.infobar.InfoBar; |
26 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; | 35 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; |
27 import org.chromium.chrome.browser.notifications.NotificationTestBase; | 36 import org.chromium.chrome.browser.notifications.NotificationTestRule; |
28 import org.chromium.chrome.browser.preferences.website.ContentSetting; | 37 import org.chromium.chrome.browser.preferences.website.ContentSetting; |
29 import org.chromium.chrome.browser.tab.Tab; | 38 import org.chromium.chrome.browser.tab.Tab; |
| 39 import org.chromium.chrome.test.ChromeActivityTestRule; |
| 40 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
30 import org.chromium.chrome.test.util.InfoBarUtil; | 41 import org.chromium.chrome.test.util.InfoBarUtil; |
31 import org.chromium.chrome.test.util.browser.TabTitleObserver; | 42 import org.chromium.chrome.test.util.browser.TabTitleObserver; |
32 import org.chromium.chrome.test.util.browser.notifications.MockNotificationManag
erProxy.NotificationEntry; | 43 import org.chromium.chrome.test.util.browser.notifications.MockNotificationManag
erProxy.NotificationEntry; |
33 import org.chromium.components.gcm_driver.GCMDriver; | 44 import org.chromium.components.gcm_driver.GCMDriver; |
34 import org.chromium.components.gcm_driver.GCMMessage; | 45 import org.chromium.components.gcm_driver.GCMMessage; |
35 import org.chromium.components.gcm_driver.instance_id.FakeInstanceIDWithSubtype; | 46 import org.chromium.components.gcm_driver.instance_id.FakeInstanceIDWithSubtype; |
36 import org.chromium.content.browser.test.util.Criteria; | 47 import org.chromium.content.browser.test.util.Criteria; |
37 import org.chromium.content.browser.test.util.CriteriaHelper; | 48 import org.chromium.content.browser.test.util.CriteriaHelper; |
38 import org.chromium.content.browser.test.util.JavaScriptUtils; | 49 import org.chromium.content.browser.test.util.JavaScriptUtils; |
39 | 50 |
40 import java.util.List; | 51 import java.util.List; |
41 import java.util.concurrent.TimeoutException; | 52 import java.util.concurrent.TimeoutException; |
42 | 53 |
43 /** | 54 /** |
44 * Instrumentation tests for the Push API and the integration with the Notificat
ions API on Android. | 55 * Instrumentation tests for the Push API and the integration with the Notificat
ions API on Android. |
45 */ | 56 */ |
46 // TODO(mvanouwerkerk): remove @SuppressLint once crbug.com/501900 is fixed. | 57 // TODO(mvanouwerkerk): remove @SuppressLint once crbug.com/501900 is fixed. |
| 58 @RunWith(ChromeJUnit4ClassRunner.class) |
| 59 @CommandLineFlags.Add({ |
| 60 ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE, |
| 61 ChromeActivityTestRule.DISABLE_NETWORK_PREDICTION_FLAG, |
| 62 }) |
47 @SuppressLint("NewApi") | 63 @SuppressLint("NewApi") |
48 public class PushMessagingTest | 64 public class PushMessagingTest implements PushMessagingServiceObserver.Listener
{ |
49 extends NotificationTestBase implements PushMessagingServiceObserver.Lis
tener { | 65 @Rule |
| 66 public NotificationTestRule mNotificationTestRule = new NotificationTestRule
(); |
| 67 |
50 private static final String PUSH_TEST_PAGE = | 68 private static final String PUSH_TEST_PAGE = |
51 "/chrome/test/data/push_messaging/push_messaging_test_android.html"; | 69 "/chrome/test/data/push_messaging/push_messaging_test_android.html"; |
52 private static final String ABOUT_BLANK = "about:blank"; | 70 private static final String ABOUT_BLANK = "about:blank"; |
53 private static final int TITLE_UPDATE_TIMEOUT_SECONDS = (int) scaleTimeout(5
); | 71 private static final int TITLE_UPDATE_TIMEOUT_SECONDS = (int) scaleTimeout(5
); |
54 private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "chrome"; | 72 private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "chrome"; |
55 | 73 |
56 private final CallbackHelper mMessageHandledHelper; | 74 private final CallbackHelper mMessageHandledHelper; |
57 private String mPushTestPage; | 75 private String mPushTestPage; |
58 | 76 |
59 public PushMessagingTest() { | 77 public PushMessagingTest() { |
60 mMessageHandledHelper = new CallbackHelper(); | 78 mMessageHandledHelper = new CallbackHelper(); |
61 } | 79 } |
62 | 80 |
63 @Override | 81 @Before |
64 protected void setUp() throws Exception { | 82 public void setUp() throws Exception { |
65 super.setUp(); | |
66 final PushMessagingServiceObserver.Listener listener = this; | 83 final PushMessagingServiceObserver.Listener listener = this; |
67 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 84 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
68 @Override | 85 @Override |
69 public void run() { | 86 public void run() { |
70 FakeInstanceIDWithSubtype.clearDataAndSetEnabled(true); | 87 FakeInstanceIDWithSubtype.clearDataAndSetEnabled(true); |
71 PushMessagingServiceObserver.setListenerForTesting(listener); | 88 PushMessagingServiceObserver.setListenerForTesting(listener); |
72 } | 89 } |
73 }); | 90 }); |
74 mPushTestPage = getTestServer().getURL(PUSH_TEST_PAGE); | 91 mPushTestPage = mNotificationTestRule.getTestServer().getURL(PUSH_TEST_P
AGE); |
75 loadUrl(mPushTestPage); | 92 mNotificationTestRule.loadUrl(mPushTestPage); |
76 } | 93 } |
77 | 94 |
78 @Override | 95 @After |
79 protected void tearDown() throws Exception { | 96 public void tearDown() throws Exception { |
80 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 97 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
81 @Override | 98 @Override |
82 public void run() { | 99 public void run() { |
83 PushMessagingServiceObserver.setListenerForTesting(null); | 100 PushMessagingServiceObserver.setListenerForTesting(null); |
84 FakeInstanceIDWithSubtype.clearDataAndSetEnabled(false); | 101 FakeInstanceIDWithSubtype.clearDataAndSetEnabled(false); |
85 } | 102 } |
86 }); | 103 }); |
87 super.tearDown(); | |
88 } | 104 } |
89 | 105 |
90 @Override | 106 @Override |
91 public void onMessageHandled() { | 107 public void onMessageHandled() { |
92 mMessageHandledHelper.notifyCalled(); | 108 mMessageHandledHelper.notifyCalled(); |
93 } | 109 } |
94 | 110 |
95 /** | 111 /** |
96 * Verifies that PushManager.subscribe() fails if Notifications permission w
as already denied. | 112 * Verifies that PushManager.subscribe() fails if Notifications permission w
as already denied. |
97 */ | 113 */ |
| 114 @Test |
98 @MediumTest | 115 @MediumTest |
99 @Feature({"Browser", "PushMessaging"}) | 116 @Feature({"Browser", "PushMessaging"}) |
100 public void testNotificationsPermissionDenied() throws InterruptedException,
TimeoutException { | 117 public void testNotificationsPermissionDenied() throws InterruptedException,
TimeoutException { |
101 // Deny Notifications permission before trying to subscribe Push. | 118 // Deny Notifications permission before trying to subscribe Push. |
102 setNotificationContentSettingForCurrentOrigin(ContentSetting.BLOCK); | 119 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont
entSetting.BLOCK); |
103 assertEquals("\"denied\"", runScriptBlocking("Notification.permission"))
; | 120 Assert.assertEquals("\"denied\"", runScriptBlocking("Notification.permis
sion")); |
104 | 121 |
105 // Reload page to ensure the block is persisted. | 122 // Reload page to ensure the block is persisted. |
106 loadUrl(mPushTestPage); | 123 mNotificationTestRule.loadUrl(mPushTestPage); |
107 | 124 |
108 // PushManager.subscribePush() should fail immediately without showing a
n infobar. | 125 // PushManager.subscribePush() should fail immediately without showing a
n infobar. |
109 runScriptAndWaitForTitle("subscribePush()", | 126 runScriptAndWaitForTitle("subscribePush()", |
110 "subscribe fail: NotAllowedError: Registration failed - permissi
on denied"); | 127 "subscribe fail: NotAllowedError: Registration failed - permissi
on denied"); |
111 assertEquals(0, getInfoBars().size()); | 128 Assert.assertEquals(0, mNotificationTestRule.getInfoBars().size()); |
112 | 129 |
113 // Notifications permission should still be denied. | 130 // Notifications permission should still be denied. |
114 assertEquals("\"denied\"", runScriptBlocking("Notification.permission"))
; | 131 Assert.assertEquals("\"denied\"", runScriptBlocking("Notification.permis
sion")); |
115 } | 132 } |
116 | 133 |
117 /** | 134 /** |
118 * Verifies that PushManager.subscribe() fails if permission is dismissed or
blocked. | 135 * Verifies that PushManager.subscribe() fails if permission is dismissed or
blocked. |
119 */ | 136 */ |
120 //@MediumTest | 137 //@MediumTest |
121 //@Feature({"Browser", "PushMessaging"}) | 138 //@Feature({"Browser", "PushMessaging"}) |
122 //@CommandLineFlags.Add("disable-features=ModalPermissionPrompts") | 139 //@CommandLineFlags.Add("disable-features=ModalPermissionPrompts") |
| 140 @Test |
123 @DisabledTest | 141 @DisabledTest |
124 public void testPushPermissionDenied() throws InterruptedException, TimeoutE
xception { | 142 public void testPushPermissionDenied() throws InterruptedException, TimeoutE
xception { |
125 // Notifications permission should initially be prompt. | 143 // Notifications permission should initially be prompt. |
126 assertEquals("\"default\"", runScriptBlocking("Notification.permission")
); | 144 Assert.assertEquals("\"default\"", runScriptBlocking("Notification.permi
ssion")); |
127 | 145 |
128 // PushManager.subscribePush() should show the notifications infobar. | 146 // PushManager.subscribePush() should show the notifications infobar. |
129 assertEquals(0, getInfoBars().size()); | 147 Assert.assertEquals(0, mNotificationTestRule.getInfoBars().size()); |
130 runScript("subscribePush()"); | 148 runScript("subscribePush()"); |
131 InfoBar infoBar = getInfobarBlocking(); | 149 InfoBar infoBar = getInfobarBlocking(); |
132 | 150 |
133 // Dismissing the infobar should cause subscribe() to fail. | 151 // Dismissing the infobar should cause subscribe() to fail. |
134 assertTrue(InfoBarUtil.clickCloseButton(infoBar)); | 152 Assert.assertTrue(InfoBarUtil.clickCloseButton(infoBar)); |
135 waitForInfobarToClose(); | 153 waitForInfobarToClose(); |
136 waitForTitle(getActivity().getActivityTab(), | 154 waitForTitle(mNotificationTestRule.getActivity().getActivityTab(), |
137 "subscribe fail: NotAllowedError: Registration failed - permissi
on denied"); | 155 "subscribe fail: NotAllowedError: Registration failed - permissi
on denied"); |
138 | 156 |
139 // Notifications permission should still be prompt. | 157 // Notifications permission should still be prompt. |
140 assertEquals("\"default\"", runScriptBlocking("Notification.permission")
); | 158 Assert.assertEquals("\"default\"", runScriptBlocking("Notification.permi
ssion")); |
141 | 159 |
142 runScriptAndWaitForTitle("sendToTest('reset title')", "reset title"); | 160 runScriptAndWaitForTitle("sendToTest('reset title')", "reset title"); |
143 | 161 |
144 // PushManager.subscribePush() should show the notifications infobar aga
in. | 162 // PushManager.subscribePush() should show the notifications infobar aga
in. |
145 runScript("subscribePush()"); | 163 runScript("subscribePush()"); |
146 infoBar = getInfobarBlocking(); | 164 infoBar = getInfobarBlocking(); |
147 | 165 |
148 // Denying the infobar should cause subscribe() to fail. | 166 // Denying the infobar should cause subscribe() to fail. |
149 assertTrue(InfoBarUtil.clickSecondaryButton(infoBar)); | 167 Assert.assertTrue(InfoBarUtil.clickSecondaryButton(infoBar)); |
150 waitForInfobarToClose(); | 168 waitForInfobarToClose(); |
151 waitForTitle(getActivity().getActivityTab(), | 169 waitForTitle(mNotificationTestRule.getActivity().getActivityTab(), |
152 "subscribe fail: NotAllowedError: Registration failed - permissi
on denied"); | 170 "subscribe fail: NotAllowedError: Registration failed - permissi
on denied"); |
153 | 171 |
154 // This should have caused notifications permission to become denied. | 172 // This should have caused notifications permission to become denied. |
155 assertEquals("\"denied\"", runScriptBlocking("Notification.permission"))
; | 173 Assert.assertEquals("\"denied\"", runScriptBlocking("Notification.permis
sion")); |
156 | 174 |
157 // Reload page to ensure the block is persisted. | 175 // Reload page to ensure the block is persisted. |
158 loadUrl(mPushTestPage); | 176 mNotificationTestRule.loadUrl(mPushTestPage); |
159 | 177 |
160 // PushManager.subscribePush() should now fail immediately without showi
ng an infobar. | 178 // PushManager.subscribePush() should now fail immediately without showi
ng an infobar. |
161 runScriptAndWaitForTitle("subscribePush()", | 179 runScriptAndWaitForTitle("subscribePush()", |
162 "subscribe fail: NotAllowedError: Registration failed - permissi
on denied"); | 180 "subscribe fail: NotAllowedError: Registration failed - permissi
on denied"); |
163 assertEquals(0, getInfoBars().size()); | 181 Assert.assertEquals(0, mNotificationTestRule.getInfoBars().size()); |
164 | 182 |
165 // Notifications permission should still be denied. | 183 // Notifications permission should still be denied. |
166 assertEquals("\"denied\"", runScriptBlocking("Notification.permission"))
; | 184 Assert.assertEquals("\"denied\"", runScriptBlocking("Notification.permis
sion")); |
167 } | 185 } |
168 | 186 |
169 /** | 187 /** |
170 * Verifies that PushManager.subscribe() requests permission correctly. | 188 * Verifies that PushManager.subscribe() requests permission correctly. |
171 */ | 189 */ |
| 190 @Test |
172 @MediumTest | 191 @MediumTest |
173 @Feature({"Browser", "PushMessaging"}) | 192 @Feature({"Browser", "PushMessaging"}) |
174 @CommandLineFlags.Add("disable-features=ModalPermissionPrompts") | 193 @CommandLineFlags.Add("disable-features=ModalPermissionPrompts") |
175 public void testPushPermissionGranted() throws InterruptedException, Timeout
Exception { | 194 public void testPushPermissionGranted() throws InterruptedException, Timeout
Exception { |
176 // Notifications permission should initially be prompt. | 195 // Notifications permission should initially be prompt. |
177 assertEquals("\"default\"", runScriptBlocking("Notification.permission")
); | 196 Assert.assertEquals("\"default\"", runScriptBlocking("Notification.permi
ssion")); |
178 | 197 |
179 // PushManager.subscribePush() should show the notifications infobar. | 198 // PushManager.subscribePush() should show the notifications infobar. |
180 assertEquals(0, getInfoBars().size()); | 199 Assert.assertEquals(0, mNotificationTestRule.getInfoBars().size()); |
181 runScript("subscribePush()"); | 200 runScript("subscribePush()"); |
182 InfoBar infoBar = getInfobarBlocking(); | 201 InfoBar infoBar = getInfobarBlocking(); |
183 | 202 |
184 // Accepting the infobar should cause subscribe() to succeed. | 203 // Accepting the infobar should cause subscribe() to succeed. |
185 assertTrue(InfoBarUtil.clickPrimaryButton(infoBar)); | 204 Assert.assertTrue(InfoBarUtil.clickPrimaryButton(infoBar)); |
186 waitForInfobarToClose(); | 205 waitForInfobarToClose(); |
187 waitForTitle(getActivity().getActivityTab(), "subscribe ok"); | 206 waitForTitle(mNotificationTestRule.getActivity().getActivityTab(), "subs
cribe ok"); |
188 | 207 |
189 // This should have caused notifications permission to become granted. | 208 // This should have caused notifications permission to become granted. |
190 assertEquals("\"granted\"", runScriptBlocking("Notification.permission")
); | 209 Assert.assertEquals("\"granted\"", runScriptBlocking("Notification.permi
ssion")); |
191 } | 210 } |
192 | 211 |
193 /** | 212 /** |
194 * Verifies that a notification can be shown from a push event handler in th
e service worker. | 213 * Verifies that a notification can be shown from a push event handler in th
e service worker. |
195 */ | 214 */ |
| 215 @Test |
196 @MediumTest | 216 @MediumTest |
197 @Feature({"Browser", "PushMessaging"}) | 217 @Feature({"Browser", "PushMessaging"}) |
198 @RetryOnFailure | 218 @RetryOnFailure |
199 public void testPushAndShowNotification() throws InterruptedException, Timeo
utException { | 219 public void testPushAndShowNotification() throws InterruptedException, Timeo
utException { |
200 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 220 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont
entSetting.ALLOW); |
201 runScriptAndWaitForTitle("subscribePush()", "subscribe ok"); | 221 runScriptAndWaitForTitle("subscribePush()", "subscribe ok"); |
202 | 222 |
203 Pair<String, String> appIdAndSenderId = | 223 Pair<String, String> appIdAndSenderId = |
204 FakeInstanceIDWithSubtype.getSubtypeAndAuthorizedEntityOfOnlyTok
en(); | 224 FakeInstanceIDWithSubtype.getSubtypeAndAuthorizedEntityOfOnlyTok
en(); |
205 sendPushAndWaitForCallback(appIdAndSenderId); | 225 sendPushAndWaitForCallback(appIdAndSenderId); |
206 NotificationEntry notificationEntry = waitForNotification(); | 226 NotificationEntry notificationEntry = mNotificationTestRule.waitForNotif
ication(); |
207 assertEquals("push notification 1", | 227 Assert.assertEquals("push notification 1", |
208 notificationEntry.notification.extras.getString(Notification.EXT
RA_TITLE)); | 228 notificationEntry.notification.extras.getString(Notification.EXT
RA_TITLE)); |
209 } | 229 } |
210 | 230 |
211 /** | 231 /** |
212 * Verifies that the default notification is shown when no notification is s
hown from the push | 232 * Verifies that the default notification is shown when no notification is s
hown from the push |
213 * event handler while no tab is visible for the origin, and grace has been
exceeded. | 233 * event handler while no tab is visible for the origin, and grace has been
exceeded. |
214 */ | 234 */ |
| 235 @Test |
215 @LargeTest | 236 @LargeTest |
216 @Feature({"Browser", "PushMessaging"}) | 237 @Feature({"Browser", "PushMessaging"}) |
217 @RetryOnFailure | 238 @RetryOnFailure |
218 public void testDefaultNotification() throws InterruptedException, TimeoutEx
ception { | 239 public void testDefaultNotification() throws InterruptedException, TimeoutEx
ception { |
219 // Start off using the tab loaded in setUp(). | 240 // Start off using the tab loaded in setUp(). |
220 assertEquals(1, getActivity().getCurrentTabModel().getCount()); | 241 Assert.assertEquals(1, mNotificationTestRule.getActivity().getCurrentTab
Model().getCount()); |
221 Tab tab = getActivity().getActivityTab(); | 242 Tab tab = mNotificationTestRule.getActivity().getActivityTab(); |
222 assertEquals(mPushTestPage, tab.getUrl()); | 243 Assert.assertEquals(mPushTestPage, tab.getUrl()); |
223 assertFalse(tab.isHidden()); | 244 Assert.assertFalse(tab.isHidden()); |
224 | 245 |
225 // Set up the push subscription and capture its details. | 246 // Set up the push subscription and capture its details. |
226 setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); | 247 mNotificationTestRule.setNotificationContentSettingForCurrentOrigin(Cont
entSetting.ALLOW); |
227 runScriptAndWaitForTitle("subscribePush()", "subscribe ok"); | 248 runScriptAndWaitForTitle("subscribePush()", "subscribe ok"); |
228 Pair<String, String> appIdAndSenderId = | 249 Pair<String, String> appIdAndSenderId = |
229 FakeInstanceIDWithSubtype.getSubtypeAndAuthorizedEntityOfOnlyTok
en(); | 250 FakeInstanceIDWithSubtype.getSubtypeAndAuthorizedEntityOfOnlyTok
en(); |
230 | 251 |
231 // Make the tab invisible by opening another one with a different origin
. | 252 // Make the tab invisible by opening another one with a different origin
. |
232 loadUrlInNewTab(ABOUT_BLANK); | 253 mNotificationTestRule.loadUrlInNewTab(ABOUT_BLANK); |
233 assertEquals(2, getActivity().getCurrentTabModel().getCount()); | 254 Assert.assertEquals(2, mNotificationTestRule.getActivity().getCurrentTab
Model().getCount()); |
234 assertEquals(ABOUT_BLANK, getActivity().getActivityTab().getUrl()); | 255 Assert.assertEquals( |
235 assertTrue(tab.isHidden()); | 256 ABOUT_BLANK, mNotificationTestRule.getActivity().getActivityTab(
).getUrl()); |
| 257 Assert.assertTrue(tab.isHidden()); |
236 | 258 |
237 // The first time a push event is fired and no notification is shown fro
m the service | 259 // The first time a push event is fired and no notification is shown fro
m the service |
238 // worker, grace permits it so no default notification is shown. | 260 // worker, grace permits it so no default notification is shown. |
239 runScriptAndWaitForTitle("setNotifyOnPush(false)", "setNotifyOnPush fals
e ok", tab); | 261 runScriptAndWaitForTitle("setNotifyOnPush(false)", "setNotifyOnPush fals
e ok", tab); |
240 sendPushAndWaitForCallback(appIdAndSenderId); | 262 sendPushAndWaitForCallback(appIdAndSenderId); |
241 | 263 |
242 // After grace runs out a default notification will be shown. | 264 // After grace runs out a default notification will be shown. |
243 sendPushAndWaitForCallback(appIdAndSenderId); | 265 sendPushAndWaitForCallback(appIdAndSenderId); |
244 NotificationEntry notificationEntry = waitForNotification(); | 266 NotificationEntry notificationEntry = mNotificationTestRule.waitForNotif
ication(); |
245 MoreAsserts.assertContainsRegex("user_visible_auto_notification", notifi
cationEntry.tag); | 267 MoreAsserts.assertContainsRegex("user_visible_auto_notification", notifi
cationEntry.tag); |
246 | 268 |
247 // When another push does show a notification, the default notification
is automatically | 269 // When another push does show a notification, the default notification
is automatically |
248 // dismissed (an additional mutation) so there is only one left in the e
nd. | 270 // dismissed (an additional mutation) so there is only one left in the e
nd. |
249 runScriptAndWaitForTitle("setNotifyOnPush(true)", "setNotifyOnPush true
ok", tab); | 271 runScriptAndWaitForTitle("setNotifyOnPush(true)", "setNotifyOnPush true
ok", tab); |
250 sendPushAndWaitForCallback(appIdAndSenderId); | 272 sendPushAndWaitForCallback(appIdAndSenderId); |
251 waitForNotificationManagerMutation(); | 273 mNotificationTestRule.waitForNotificationManagerMutation(); |
252 notificationEntry = waitForNotification(); | 274 notificationEntry = mNotificationTestRule.waitForNotification(); |
253 assertEquals("push notification 1", | 275 Assert.assertEquals("push notification 1", |
254 notificationEntry.notification.extras.getString(Notification.EXT
RA_TITLE)); | 276 notificationEntry.notification.extras.getString(Notification.EXT
RA_TITLE)); |
255 } | 277 } |
256 | 278 |
257 /** | 279 /** |
258 * Runs {@code script} in the current tab but does not wait for the result. | 280 * Runs {@code script} in the current tab but does not wait for the result. |
259 */ | 281 */ |
260 private void runScript(String script) { | 282 private void runScript(String script) { |
261 JavaScriptUtils.executeJavaScript(getActivity().getActivityTab().getWebC
ontents(), script); | 283 JavaScriptUtils.executeJavaScript( |
| 284 mNotificationTestRule.getActivity().getActivityTab().getWebConte
nts(), script); |
262 } | 285 } |
263 | 286 |
264 /** | 287 /** |
265 * Runs {@code script} in the current tab and returns its synchronous result
in JSON format. | 288 * Runs {@code script} in the current tab and returns its synchronous result
in JSON format. |
266 */ | 289 */ |
267 private String runScriptBlocking(String script) throws InterruptedException,
TimeoutException { | 290 private String runScriptBlocking(String script) throws InterruptedException,
TimeoutException { |
268 return JavaScriptUtils.executeJavaScriptAndWaitForResult( | 291 return JavaScriptUtils.executeJavaScriptAndWaitForResult( |
269 getActivity().getActivityTab().getWebContents(), script); | 292 mNotificationTestRule.getActivity().getActivityTab().getWebConte
nts(), script); |
270 } | 293 } |
271 | 294 |
272 /** | 295 /** |
273 * Runs {@code script} in the current tab and waits for the tab title to cha
nge to | 296 * Runs {@code script} in the current tab and waits for the tab title to cha
nge to |
274 * {@code expectedTitle}. | 297 * {@code expectedTitle}. |
275 */ | 298 */ |
276 private void runScriptAndWaitForTitle(String script, String expectedTitle) | 299 private void runScriptAndWaitForTitle(String script, String expectedTitle) |
277 throws InterruptedException { | 300 throws InterruptedException { |
278 runScriptAndWaitForTitle(script, expectedTitle, getActivity().getActivit
yTab()); | 301 runScriptAndWaitForTitle( |
| 302 script, expectedTitle, mNotificationTestRule.getActivity().getAc
tivityTab()); |
279 } | 303 } |
280 | 304 |
281 /** | 305 /** |
282 * Runs {@code script} in {@code tab} and waits for the tab title to change
to | 306 * Runs {@code script} in {@code tab} and waits for the tab title to change
to |
283 * {@code expectedTitle}. | 307 * {@code expectedTitle}. |
284 */ | 308 */ |
285 private void runScriptAndWaitForTitle(String script, String expectedTitle, T
ab tab) | 309 private void runScriptAndWaitForTitle(String script, String expectedTitle, T
ab tab) |
286 throws InterruptedException { | 310 throws InterruptedException { |
287 JavaScriptUtils.executeJavaScript(tab.getWebContents(), script); | 311 JavaScriptUtils.executeJavaScript(tab.getWebContents(), script); |
288 waitForTitle(tab, expectedTitle); | 312 waitForTitle(tab, expectedTitle); |
289 } | 313 } |
290 | 314 |
291 private void sendPushAndWaitForCallback(Pair<String, String> appIdAndSenderI
d) | 315 private void sendPushAndWaitForCallback(Pair<String, String> appIdAndSenderI
d) |
292 throws InterruptedException, TimeoutException { | 316 throws InterruptedException, TimeoutException { |
293 final String appId = appIdAndSenderId.first; | 317 final String appId = appIdAndSenderId.first; |
294 final String senderId = appIdAndSenderId.second; | 318 final String senderId = appIdAndSenderId.second; |
295 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 319 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
296 @Override | 320 @Override |
297 public void run() { | 321 public void run() { |
298 Context context = getInstrumentation().getTargetContext().getApp
licationContext(); | 322 Context context = InstrumentationRegistry.getInstrumentation() |
| 323 .getTargetContext() |
| 324 .getApplicationContext(); |
299 | 325 |
300 Bundle extras = new Bundle(); | 326 Bundle extras = new Bundle(); |
301 extras.putString("subtype", appId); | 327 extras.putString("subtype", appId); |
302 | 328 |
303 GCMMessage message = new GCMMessage(senderId, extras); | 329 GCMMessage message = new GCMMessage(senderId, extras); |
304 try { | 330 try { |
305 ChromeBrowserInitializer.getInstance(context).handleSynchron
ousStartup(); | 331 ChromeBrowserInitializer.getInstance(context).handleSynchron
ousStartup(); |
306 GCMDriver.dispatchMessage(message); | 332 GCMDriver.dispatchMessage(message); |
307 } catch (ProcessInitException e) { | 333 } catch (ProcessInitException e) { |
308 fail("Chrome browser failed to initialize."); | 334 Assert.fail("Chrome browser failed to initialize."); |
309 } | 335 } |
310 } | 336 } |
311 }); | 337 }); |
312 mMessageHandledHelper.waitForCallback(mMessageHandledHelper.getCallCount
()); | 338 mMessageHandledHelper.waitForCallback(mMessageHandledHelper.getCallCount
()); |
313 } | 339 } |
314 | 340 |
315 private void waitForTitle(Tab tab, String expectedTitle) throws InterruptedE
xception { | 341 private void waitForTitle(Tab tab, String expectedTitle) throws InterruptedE
xception { |
316 TabTitleObserver titleObserver = new TabTitleObserver(tab, expectedTitle
); | 342 TabTitleObserver titleObserver = new TabTitleObserver(tab, expectedTitle
); |
317 try { | 343 try { |
318 titleObserver.waitForTitleUpdate(TITLE_UPDATE_TIMEOUT_SECONDS); | 344 titleObserver.waitForTitleUpdate(TITLE_UPDATE_TIMEOUT_SECONDS); |
319 } catch (TimeoutException e) { | 345 } catch (TimeoutException e) { |
320 // The title is not as expected, this assertion neatly logs what the
difference is. | 346 // The title is not as expected, this assertion neatly logs what the
difference is. |
321 assertEquals(expectedTitle, tab.getTitle()); | 347 Assert.assertEquals(expectedTitle, tab.getTitle()); |
322 } | 348 } |
323 } | 349 } |
324 | 350 |
325 private InfoBar getInfobarBlocking() { | 351 private InfoBar getInfobarBlocking() { |
326 CriteriaHelper.pollUiThread(new Criteria() { | 352 CriteriaHelper.pollUiThread(new Criteria() { |
327 @Override | 353 @Override |
328 public boolean isSatisfied() { | 354 public boolean isSatisfied() { |
329 return !getInfoBars().isEmpty(); | 355 return !mNotificationTestRule.getInfoBars().isEmpty(); |
330 } | 356 } |
331 }); | 357 }); |
332 List<InfoBar> infoBars = getInfoBars(); | 358 List<InfoBar> infoBars = mNotificationTestRule.getInfoBars(); |
333 assertEquals(1, infoBars.size()); | 359 Assert.assertEquals(1, infoBars.size()); |
334 return infoBars.get(0); | 360 return infoBars.get(0); |
335 } | 361 } |
336 | 362 |
337 private void waitForInfobarToClose() { | 363 private void waitForInfobarToClose() { |
338 CriteriaHelper.pollUiThread(new Criteria() { | 364 CriteriaHelper.pollUiThread(new Criteria() { |
339 @Override | 365 @Override |
340 public boolean isSatisfied() { | 366 public boolean isSatisfied() { |
341 return getInfoBars().isEmpty(); | 367 return mNotificationTestRule.getInfoBars().isEmpty(); |
342 } | 368 } |
343 }); | 369 }); |
344 assertEquals(0, getInfoBars().size()); | 370 Assert.assertEquals(0, mNotificationTestRule.getInfoBars().size()); |
345 } | 371 } |
346 } | 372 } |
OLD | NEW |