OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <deque> | |
6 #include <string> | |
7 | |
8 #include "base/compiler_specific.h" | |
9 #include "base/stringprintf.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" | |
12 #include "chrome/browser/api/infobars/infobar_delegate.h" | |
13 #include "chrome/browser/browser_process.h" | |
5 #include "chrome/browser/infobars/infobar_tab_helper.h" | 14 #include "chrome/browser/infobars/infobar_tab_helper.h" |
15 #include "chrome/browser/notifications/balloon.h" | |
16 #include "chrome/browser/notifications/balloon_collection.h" | |
17 #include "chrome/browser/notifications/balloon_host.h" | |
18 #include "chrome/browser/notifications/desktop_notification_service.h" | |
19 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | |
20 #include "chrome/browser/notifications/notification.h" | |
21 #include "chrome/browser/notifications/notification_ui_manager.h" | |
22 #include "chrome/browser/profiles/profile.h" | |
6 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
7 #include "chrome/browser/ui/browser_tabstrip.h" | 24 #include "chrome/browser/ui/browser_tabstrip.h" |
25 #include "chrome/browser/ui/browser_window.h" | |
26 #include "chrome/browser/ui/tab_contents/tab_contents.h" | |
27 #include "chrome/common/chrome_notification_types.h" | |
28 #include "chrome/common/content_settings.h" | |
29 #include "chrome/common/content_settings_pattern.h" | |
8 #include "chrome/test/base/in_process_browser_test.h" | 30 #include "chrome/test/base/in_process_browser_test.h" |
9 #include "chrome/test/base/ui_test_utils.h" | 31 #include "chrome/test/base/ui_test_utils.h" |
32 #include "content/public/browser/notification_service.h" | |
33 #include "content/public/browser/notification_source.h" | |
34 #include "content/public/browser/notification_types.h" | |
35 #include "content/public/browser/render_view_host.h" | |
10 #include "content/public/browser/web_contents.h" | 36 #include "content/public/browser/web_contents.h" |
11 #include "content/public/test/browser_test_utils.h" | 37 #include "content/public/test/browser_test_utils.h" |
38 #include "content/public/test/test_utils.h" | |
39 #include "googleurl/src/gurl.h" | |
12 #include "net/base/net_util.h" | 40 #include "net/base/net_util.h" |
13 #include "net/test/test_server.h" | 41 #include "net/test/test_server.h" |
14 | 42 #include "testing/gtest/include/gtest/gtest.h" |
15 class NotificationsPermissionTest : public InProcessBrowserTest { | 43 #include "webkit/glue/window_open_disposition.h" |
44 | |
45 // Disable the whole test fixture on Chrome OS. | |
46 #if !defined(OS_CHROMEOS) | |
kkania
2012/11/26 21:08:50
it looks like the two original tests worked on chr
chrisgao (Use stgao instead)
2012/11/28 19:19:04
Disable all new testcases.
| |
47 | |
48 namespace { | |
49 | |
50 const char kExpectedIconUrl[] = "files/notifications/no_such_file.png"; | |
51 | |
52 enum InfobarAction { | |
53 DISMISS = 0, | |
54 ALLOW, | |
55 DENY, | |
56 }; | |
57 | |
58 } // namespace | |
59 | |
60 class NotificationsTest : public InProcessBrowserTest { | |
16 public: | 61 public: |
17 NotificationsPermissionTest() {} | 62 NotificationsTest() {} |
63 | |
64 protected: | |
65 // Overriden from InProcessBrowserTest: | |
66 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; | |
67 | |
68 const std::deque<Balloon*>& GetActiveBalloons(); | |
69 int GetNotificationCount(); | |
70 | |
71 bool CloseNotificationAndWait(const Notification& notification); | |
72 void CloseBrowserWindow(Browser* browser); | |
73 void CrashTab(Browser* browser, int index); | |
74 void CrashNotification(Balloon* balloon); | |
75 | |
76 void SetDefaultPermissionSetting(ContentSetting setting); | |
77 void DenyOrigin(const GURL& origin); | |
78 void AllowOrigin(const GURL& origin); | |
79 void AllowAllOrigins(); | |
80 | |
81 void VerifyInfobar(const Browser* browser, | |
82 const std::string& origin, | |
83 int index); | |
84 std::string CreateNotification(Browser* browser, | |
85 bool wait_for_new_balloon, | |
86 const char* icon, | |
87 const char* title, | |
88 const char* body, | |
89 const char* replace_id); | |
90 std::string CreateSimpleNotification(Browser* browser, | |
91 bool wait_for_new_balloon); | |
92 bool RequestPermissionAndWait(Browser* browser); | |
93 bool CancelNotification(const char* notification_id, Browser* browser); | |
94 bool PerformActionOnInfobar(Browser* browser, | |
95 InfobarAction action, | |
96 int infobar_index, | |
97 int tab_index); | |
98 void GetPrefsByContentSetting(ContentSetting setting, | |
99 ContentSettingsForOneType* settings); | |
100 bool CheckOriginInSetting(const ContentSettingsForOneType & settings, | |
kkania
2012/11/26 21:08:50
& should be next to type
chrisgao (Use stgao instead)
2012/11/28 19:19:04
Done.
| |
101 const GURL & origin); | |
kkania
2012/11/26 21:08:50
same here
chrisgao (Use stgao instead)
2012/11/28 19:19:04
Done.
| |
102 | |
103 GURL empty_page_url_; | |
104 GURL test_page_url_; | |
105 | |
106 private: | |
107 void DropOriginPreference(const GURL& origin); | |
108 DesktopNotificationService* GetDesktopNotificationService(); | |
18 }; | 109 }; |
19 | 110 |
111 void NotificationsTest::SetUpInProcessBrowserTestFixture() { | |
112 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); | |
113 | |
114 ASSERT_TRUE(test_server()->Start()); | |
115 empty_page_url_ = test_server()->GetURL("files/empty.html"); | |
116 test_page_url_ = test_server()->GetURL( | |
117 "files/notifications/notification_tester.html"); | |
118 } | |
119 | |
120 const std::deque<Balloon*>& NotificationsTest::GetActiveBalloons() { | |
121 NotificationUIManager* manager = g_browser_process->notification_ui_manager(); | |
122 return manager->balloon_collection()->GetActiveBalloons(); | |
123 } | |
124 | |
125 int NotificationsTest::GetNotificationCount() { | |
126 NotificationUIManager* manager = g_browser_process->notification_ui_manager(); | |
127 return manager->balloon_collection()->GetActiveBalloons().size(); | |
128 } | |
129 | |
130 bool NotificationsTest::CloseNotificationAndWait( | |
131 const Notification& notification) { | |
132 content::WindowedNotificationObserver observer( | |
133 chrome::NOTIFICATION_NOTIFY_BALLOON_DISCONNECTED, | |
134 content::NotificationService::AllSources()); | |
135 NotificationUIManager* manager = g_browser_process->notification_ui_manager(); | |
136 bool flag = manager->CancelById(notification.notification_id()); | |
137 if (flag) { | |
138 observer.Wait(); | |
139 content::RunAllPendingInMessageLoop(); | |
140 } | |
141 return flag; | |
142 } | |
143 | |
144 void NotificationsTest::CloseBrowserWindow(Browser* browser) { | |
145 content::WindowedNotificationObserver observer( | |
146 chrome::NOTIFICATION_BROWSER_CLOSED, | |
147 content::Source<Browser>(browser)); | |
148 browser->window()->Close(); | |
149 observer.Wait(); | |
150 } | |
151 | |
152 void NotificationsTest::CrashTab(Browser* browser, int index) { | |
153 content::CrashTab(chrome::GetWebContentsAt(browser, index)); | |
154 } | |
155 | |
156 void NotificationsTest::CrashNotification(Balloon* balloon) { | |
157 content::CrashTab(balloon->balloon_view()->GetHost()->web_contents()); | |
158 } | |
159 | |
160 void NotificationsTest::SetDefaultPermissionSetting(ContentSetting setting) { | |
161 DesktopNotificationService* service = GetDesktopNotificationService(); | |
162 service->SetDefaultContentSetting(setting); | |
163 } | |
164 | |
165 void NotificationsTest::DenyOrigin(const GURL& origin) { | |
166 DropOriginPreference(origin); | |
167 GetDesktopNotificationService()->DenyPermission(origin); | |
168 } | |
169 | |
170 void NotificationsTest::AllowOrigin(const GURL& origin) { | |
171 DropOriginPreference(origin); | |
172 GetDesktopNotificationService()->GrantPermission(origin); | |
173 } | |
174 | |
175 void NotificationsTest::AllowAllOrigins() { | |
176 GetDesktopNotificationService()->ResetAllOrigins(); | |
177 GetDesktopNotificationService()->SetDefaultContentSetting( | |
178 CONTENT_SETTING_ALLOW); | |
179 } | |
180 | |
181 void NotificationsTest::VerifyInfobar( | |
182 const Browser* browser, | |
183 const std::string& origin, | |
184 int index) { | |
185 InfoBarTabHelper* infobar_helper = InfoBarTabHelper::FromWebContents( | |
186 chrome::GetWebContentsAt(browser, index)); | |
187 | |
188 ASSERT_EQ(1U, infobar_helper->GetInfoBarCount()); | |
189 InfoBarDelegate* infobar = infobar_helper->GetInfoBarDelegateAt(0); | |
190 ASSERT_TRUE(infobar->AsConfirmInfoBarDelegate()); | |
191 ConfirmInfoBarDelegate* confirm_infobar = infobar->AsConfirmInfoBarDelegate(); | |
192 int buttons = confirm_infobar->GetButtons(); | |
193 if (buttons & ConfirmInfoBarDelegate::BUTTON_OK) { | |
194 EXPECT_EQ( | |
195 ASCIIToUTF16("Allow"), | |
196 confirm_infobar->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK)); | |
197 } else { | |
198 EXPECT_TRUE(false); | |
199 } | |
200 if (buttons & ConfirmInfoBarDelegate::BUTTON_CANCEL) { | |
201 EXPECT_EQ( | |
202 ASCIIToUTF16("Deny"), | |
203 confirm_infobar->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL)); | |
204 } else { | |
205 EXPECT_TRUE(false); | |
206 } | |
207 std::string text = base::StringPrintf( | |
208 "Allow %s to show desktop notifications?", | |
209 origin.c_str()); | |
210 EXPECT_EQ(UTF8ToUTF16(text), confirm_infobar->GetMessageText()); | |
211 } | |
212 | |
213 std::string NotificationsTest::CreateNotification( | |
214 Browser* browser, | |
215 bool wait_for_new_balloon, | |
216 const char* icon, | |
217 const char* title, | |
218 const char* body, | |
219 const char* replace_id) { | |
220 std::string script = base::StringPrintf( | |
221 "createNotification('%s', '%s', '%s', '%s');", | |
222 icon, | |
223 title, | |
224 body, | |
225 replace_id); | |
226 | |
227 content::WindowedNotificationObserver observer( | |
228 chrome::NOTIFICATION_NOTIFY_BALLOON_CONNECTED, | |
229 content::NotificationService::AllSources()); | |
230 std::string result; | |
231 bool flag = content::ExecuteJavaScriptAndExtractString( | |
kkania
2012/11/26 21:08:50
'flag' is a bit unclear. perhaps success or script
chrisgao (Use stgao instead)
2012/11/28 19:19:04
Done.
| |
232 chrome::GetActiveWebContents(browser)->GetRenderViewHost(), | |
233 L"", | |
234 UTF8ToWide(script), | |
235 &result); | |
236 if (flag && result != "-1" && wait_for_new_balloon) { | |
237 observer.Wait(); | |
238 content::RunAllPendingInMessageLoop(); | |
kkania
2012/11/26 21:08:50
Perhaps add a comment here why this is needed
chrisgao (Use stgao instead)
2012/11/28 19:19:04
Use set_on_collection_changed_callback instead.
| |
239 } | |
240 EXPECT_TRUE(flag); | |
241 | |
242 return result; | |
243 } | |
244 | |
245 std::string NotificationsTest::CreateSimpleNotification( | |
246 Browser* browser, | |
247 bool wait_for_new_balloon) { | |
248 return CreateNotification( | |
249 browser, | |
250 wait_for_new_balloon, | |
251 "no_such_file.png", | |
252 "My Title", | |
253 "My Body", | |
254 ""); | |
255 } | |
256 | |
257 bool NotificationsTest::RequestPermissionAndWait(Browser* browser) { | |
258 InfoBarTabHelper* infobar_helper = InfoBarTabHelper::FromWebContents( | |
259 chrome::GetActiveWebContents(browser)); | |
260 content::WindowedNotificationObserver observer( | |
261 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | |
262 content::Source<InfoBarTabHelper>(infobar_helper)); | |
263 std::string result; | |
264 bool flag = content::ExecuteJavaScriptAndExtractString( | |
265 chrome::GetActiveWebContents(browser)->GetRenderViewHost(), | |
266 L"", | |
267 L"requestPermission();", | |
268 &result); | |
269 if (!flag || result != "1") | |
270 return false; | |
271 observer.Wait(); | |
272 return true; | |
273 } | |
274 | |
275 bool NotificationsTest::CancelNotification( | |
276 const char* notification_id, | |
277 Browser* browser) { | |
278 std::string script = base::StringPrintf( | |
279 "cancelNotification('%s');", | |
280 notification_id); | |
281 | |
282 content::WindowedNotificationObserver observer( | |
283 chrome::NOTIFICATION_NOTIFY_BALLOON_DISCONNECTED, | |
284 content::NotificationService::AllSources()); | |
285 std::string result; | |
286 bool flag = content::ExecuteJavaScriptAndExtractString( | |
287 chrome::GetActiveWebContents(browser)->GetRenderViewHost(), | |
288 L"", | |
289 UTF8ToWide(script), | |
290 &result); | |
291 if (!flag || result != "1") | |
292 return false; | |
293 observer.Wait(); | |
294 content::RunAllPendingInMessageLoop(); | |
kkania
2012/11/26 21:08:50
Perhaps add a comment here why this is needed
chrisgao (Use stgao instead)
2012/11/28 19:19:04
Use set_on_collection_changed_callback instead.
| |
295 return true; | |
296 } | |
297 | |
298 bool NotificationsTest::PerformActionOnInfobar( | |
299 Browser* browser, | |
300 InfobarAction action, | |
301 int infobar_index, | |
302 int tab_index) { | |
303 InfoBarTabHelper* infobar_helper = InfoBarTabHelper::FromWebContents( | |
304 chrome::GetWebContentsAt(browser, tab_index)); | |
305 | |
306 InfoBarDelegate* infobar = infobar_helper->GetInfoBarDelegateAt( | |
307 infobar_index); | |
308 switch (action) { | |
309 case DISMISS: { | |
310 infobar->InfoBarDismissed(); | |
311 infobar_helper->RemoveInfoBar(infobar); | |
312 return true; | |
313 } | |
314 case ALLOW: { | |
315 ConfirmInfoBarDelegate* confirm_bar = infobar->AsConfirmInfoBarDelegate(); | |
316 if (confirm_bar->Accept()) { | |
317 infobar_helper->RemoveInfoBar(infobar); | |
318 return true; | |
319 } | |
320 } | |
321 case DENY: { | |
322 ConfirmInfoBarDelegate* confirm_bar = infobar->AsConfirmInfoBarDelegate(); | |
323 if (confirm_bar->Cancel()) { | |
324 infobar_helper->RemoveInfoBar(infobar); | |
325 return true; | |
326 } | |
327 } | |
328 } | |
329 | |
330 return false; | |
331 } | |
332 | |
333 void NotificationsTest::GetPrefsByContentSetting( | |
334 ContentSetting setting, | |
335 ContentSettingsForOneType* settings) { | |
336 DesktopNotificationService* service = GetDesktopNotificationService(); | |
337 service->GetNotificationsSettings(settings); | |
338 for (ContentSettingsForOneType::iterator it = settings->begin(); | |
339 it != settings->end();) { | |
340 if (it->setting != setting || it->source.compare("preference") != 0) { | |
341 it = settings->erase(it); | |
342 } else { | |
343 ++it; | |
344 } | |
345 } | |
346 } | |
347 | |
348 bool NotificationsTest::CheckOriginInSetting( | |
349 const ContentSettingsForOneType & settings, | |
350 const GURL & origin) { | |
351 ContentSettingsPattern pattern = | |
352 ContentSettingsPattern::FromURLNoWildcard(origin); | |
353 for (ContentSettingsForOneType::const_iterator it = settings.begin(); | |
354 it != settings.end(); ++it) { | |
355 if (it->primary_pattern == pattern) { | |
356 return true; | |
357 } | |
358 } | |
359 return false; | |
360 } | |
361 | |
362 void NotificationsTest::DropOriginPreference(const GURL& origin) { | |
363 GetDesktopNotificationService()->ClearSetting( | |
364 ContentSettingsPattern::FromURLNoWildcard(origin)); | |
365 } | |
366 | |
367 DesktopNotificationService* NotificationsTest::GetDesktopNotificationService() { | |
368 Profile* profile = browser()->profile(); | |
369 return DesktopNotificationServiceFactory::GetForProfile(profile); | |
370 } | |
371 | |
20 // If this flakes, use http://crbug.com/62311 and http://crbug.com/74428. | 372 // If this flakes, use http://crbug.com/62311 and http://crbug.com/74428. |
21 IN_PROC_BROWSER_TEST_F(NotificationsPermissionTest, TestUserGestureInfobar) { | 373 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestUserGestureInfobar) { |
22 ASSERT_TRUE(test_server()->Start()); | |
23 | |
24 ui_test_utils::NavigateToURL( | 374 ui_test_utils::NavigateToURL( |
25 browser(), | 375 browser(), |
26 test_server()->GetURL( | 376 test_server()->GetURL( |
27 "files/notifications/notifications_request_function.html")); | 377 "files/notifications/notifications_request_function.html")); |
28 | 378 |
29 // Request permission by calling request() while eval'ing an inline script; | 379 // Request permission by calling request() while eval'ing an inline script; |
30 // That's considered a user gesture to webkit, and should produce an infobar. | 380 // That's considered a user gesture to webkit, and should produce an infobar. |
31 bool result; | 381 bool result; |
32 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( | 382 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
33 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 383 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
34 L"", | 384 L"", |
35 L"window.domAutomationController.send(request());", | 385 L"window.domAutomationController.send(request());", |
36 &result)); | 386 &result)); |
37 EXPECT_TRUE(result); | 387 EXPECT_TRUE(result); |
38 | 388 |
39 EXPECT_EQ(1U, InfoBarTabHelper::FromWebContents( | 389 EXPECT_EQ(1U, InfoBarTabHelper::FromWebContents( |
40 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); | 390 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); |
41 } | 391 } |
42 | 392 |
43 // If this flakes, use http://crbug.com/62311. | 393 // If this flakes, use http://crbug.com/62311. |
44 IN_PROC_BROWSER_TEST_F(NotificationsPermissionTest, TestNoUserGestureInfobar) { | 394 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNoUserGestureInfobar) { |
45 ASSERT_TRUE(test_server()->Start()); | |
46 | |
47 // Load a page which just does a request; no user gesture should result | 395 // Load a page which just does a request; no user gesture should result |
48 // in no infobar. | 396 // in no infobar. |
49 ui_test_utils::NavigateToURL( | 397 ui_test_utils::NavigateToURL( |
50 browser(), | 398 browser(), |
51 test_server()->GetURL( | 399 test_server()->GetURL( |
52 "files/notifications/notifications_request_inline.html")); | 400 "files/notifications/notifications_request_inline.html")); |
53 | 401 |
54 EXPECT_EQ(0U, InfoBarTabHelper::FromWebContents( | 402 EXPECT_EQ(0U, InfoBarTabHelper::FromWebContents( |
55 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); | 403 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); |
56 } | 404 } |
405 | |
406 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateSimpleNotification) { | |
407 // Creates a simple notification. | |
408 AllowAllOrigins(); | |
409 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
410 | |
411 std::string result = CreateSimpleNotification(browser(), true); | |
412 EXPECT_NE("-1", result); | |
413 | |
414 const std::deque<Balloon*>& balloons = GetActiveBalloons(); | |
415 ASSERT_EQ(1U, balloons.size()); | |
416 Balloon* balloon = balloons[0]; | |
417 const Notification& notification = balloon->notification(); | |
418 GURL EXPECTED_ICON_URL = test_server()->GetURL(kExpectedIconUrl); | |
419 EXPECT_EQ(EXPECTED_ICON_URL, notification.icon_url()); | |
420 EXPECT_EQ(ASCIIToUTF16("My Title"), notification.title()); | |
421 EXPECT_EQ(ASCIIToUTF16("My Body"), notification.body()); | |
422 } | |
423 | |
424 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseNotification) { | |
425 // Creates a notification and closes it. | |
426 AllowAllOrigins(); | |
427 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
428 | |
429 std::string result = CreateSimpleNotification(browser(), true); | |
430 EXPECT_NE("-1", result); | |
431 | |
432 const std::deque<Balloon*>& balloons = GetActiveBalloons(); | |
433 ASSERT_EQ(1U, balloons.size()); | |
434 EXPECT_TRUE(CloseNotificationAndWait(balloons[0]->notification())); | |
435 ASSERT_EQ(0, GetNotificationCount()); | |
436 } | |
437 | |
438 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCancelNotification) { | |
439 // Creates a notification and cancels it in the origin page. | |
440 AllowAllOrigins(); | |
441 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
442 | |
443 std::string note_id = CreateSimpleNotification(browser(), true); | |
444 EXPECT_NE(note_id, "-1"); | |
445 | |
446 ASSERT_EQ(1, GetNotificationCount()); | |
447 ASSERT_TRUE(CancelNotification(note_id.c_str(), browser())); | |
448 ASSERT_EQ(0, GetNotificationCount()); | |
449 } | |
450 | |
451 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestPermissionInfobarAppears) { | |
452 // Requests notification privileges and verifies the infobar appears. | |
453 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
454 ASSERT_TRUE(RequestPermissionAndWait(browser())); | |
455 | |
456 ASSERT_EQ(0, GetNotificationCount()); | |
457 VerifyInfobar(browser(), test_page_url_.host(), 0); | |
458 } | |
459 | |
460 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowOnPermissionInfobar) { | |
461 // Tries to create a notification and clicks allow on the infobar. | |
462 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
463 // This notification should not be shown because we do not have permission. | |
464 CreateSimpleNotification(browser(), false); | |
465 ASSERT_EQ(0, GetNotificationCount()); | |
466 | |
467 ASSERT_TRUE(RequestPermissionAndWait(browser())); | |
468 ASSERT_TRUE(PerformActionOnInfobar(browser(), ALLOW, 0, 0)); | |
469 | |
470 CreateSimpleNotification(browser(), true); | |
471 EXPECT_EQ(1, GetNotificationCount()); | |
472 } | |
473 | |
474 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyOnPermissionInfobar) { | |
475 // Test that no notification is created | |
476 // when Deny is chosen from permission infobar. | |
477 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
478 ASSERT_TRUE(RequestPermissionAndWait(browser())); | |
479 PerformActionOnInfobar(browser(), DENY, 0, 0); | |
480 CreateSimpleNotification(browser(), false); | |
481 ASSERT_EQ(0, GetNotificationCount()); | |
482 ContentSettingsForOneType settings; | |
483 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); | |
484 EXPECT_TRUE(CheckOriginInSetting(settings, test_page_url_)); | |
485 } | |
486 | |
487 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestClosePermissionInfobar) { | |
488 // Test that no notification is created when permission infobar is dismissed. | |
489 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
490 ASSERT_TRUE(RequestPermissionAndWait(browser())); | |
491 PerformActionOnInfobar(browser(), DISMISS, 0, 0); | |
492 CreateSimpleNotification(browser(), false); | |
493 ASSERT_EQ(0, GetNotificationCount()); | |
494 ContentSettingsForOneType settings; | |
495 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); | |
496 EXPECT_EQ(0U, settings.size()); | |
497 } | |
498 | |
499 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationWithPropertyMissing) { | |
500 // Test that a notification can be created if one property is missing. | |
501 AllowAllOrigins(); | |
502 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
503 | |
504 std::string result = CreateSimpleNotification(browser(), true); | |
505 EXPECT_NE("-1", result); | |
506 | |
507 const std::deque<Balloon*>& balloons = GetActiveBalloons(); | |
508 ASSERT_EQ(1U, balloons.size()); | |
509 Balloon* balloon = balloons[0]; | |
510 const Notification& notification = balloon->notification(); | |
511 GURL EXPECTED_ICON_URL = test_server()->GetURL(kExpectedIconUrl); | |
512 EXPECT_EQ(EXPECTED_ICON_URL, notification.icon_url()); | |
513 EXPECT_EQ(ASCIIToUTF16("My Title"), notification.title()); | |
514 } | |
515 | |
516 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowNotificationsFromAllSites) { | |
517 // Verify that all domains can be allowed to show notifications. | |
518 SetDefaultPermissionSetting(CONTENT_SETTING_ALLOW); | |
519 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
520 | |
521 std::string result = CreateSimpleNotification(browser(), true); | |
522 EXPECT_NE("-1", result); | |
523 | |
524 ASSERT_EQ(1, GetNotificationCount()); | |
525 EXPECT_EQ(0U, InfoBarTabHelper::FromWebContents( | |
526 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); | |
527 } | |
528 | |
529 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyNotificationsFromAllSites) { | |
530 // Verify that no domain can show notifications. | |
531 SetDefaultPermissionSetting(CONTENT_SETTING_BLOCK); | |
532 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
533 | |
534 std::string result = CreateSimpleNotification(browser(), false); | |
535 EXPECT_EQ("-1", result); | |
536 | |
537 ASSERT_EQ(0, GetNotificationCount()); | |
538 } | |
539 | |
540 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyDomainAndAllowAll) { | |
541 // Verify that denying a domain and allowing all shouldn't show | |
542 // notifications from the denied domain. | |
543 DenyOrigin(test_page_url_.GetOrigin()); | |
544 SetDefaultPermissionSetting(CONTENT_SETTING_ALLOW); | |
545 | |
546 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
547 | |
548 std::string result = CreateSimpleNotification(browser(), false); | |
549 EXPECT_EQ("-1", result); | |
550 | |
551 ASSERT_EQ(0, GetNotificationCount()); | |
552 } | |
553 | |
554 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestAllowDomainAndDenyAll) { | |
555 // Verify that allowing a domain and denying all others should show | |
556 // notifications from the allowed domain. | |
557 AllowOrigin(test_page_url_.GetOrigin()); | |
558 SetDefaultPermissionSetting(CONTENT_SETTING_BLOCK); | |
559 | |
560 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
561 | |
562 std::string result = CreateSimpleNotification(browser(), true); | |
563 EXPECT_NE("-1", result); | |
564 | |
565 ASSERT_EQ(1, GetNotificationCount()); | |
566 } | |
567 | |
568 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestDenyAndThenAllowDomain) { | |
569 // Verify that denying and again allowing should show notifications. | |
570 DenyOrigin(test_page_url_.GetOrigin()); | |
571 | |
572 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
573 | |
574 std::string result = CreateSimpleNotification(browser(), false); | |
575 EXPECT_EQ("-1", result); | |
576 | |
577 ASSERT_EQ(0, GetNotificationCount()); | |
578 | |
579 AllowOrigin(test_page_url_.GetOrigin()); | |
580 result = CreateSimpleNotification(browser(), true); | |
581 EXPECT_NE("-1", result); | |
582 | |
583 ASSERT_EQ(1, GetNotificationCount()); | |
584 EXPECT_EQ(0U, InfoBarTabHelper::FromWebContents( | |
585 chrome::GetWebContentsAt(browser(), 0))->GetInfoBarCount()); | |
586 } | |
587 | |
588 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCreateDenyCloseNotifications) { | |
589 // Verify able to create, deny, and close the notification. | |
590 AllowAllOrigins(); | |
591 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
592 CreateSimpleNotification(browser(), true); | |
593 ASSERT_EQ(1, GetNotificationCount()); | |
594 | |
595 DenyOrigin(test_page_url_.GetOrigin()); | |
596 ContentSettingsForOneType settings; | |
597 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); | |
598 ASSERT_TRUE(CheckOriginInSetting(settings, test_page_url_.GetOrigin())); | |
599 | |
600 const std::deque<Balloon*>& balloons1 = GetActiveBalloons(); | |
601 EXPECT_EQ(1U, balloons1.size()); | |
602 ASSERT_TRUE(CloseNotificationAndWait(balloons1[0]->notification())); | |
603 ASSERT_EQ(0, GetNotificationCount()); | |
604 } | |
605 | |
606 // Crashes on Linux/Win. See http://crbug.com/160657. | |
607 IN_PROC_BROWSER_TEST_F( | |
608 NotificationsTest, | |
609 DISABLED_TestOriginPrefsNotSavedInIncognito) { | |
610 // Verify that allow/deny origin preferences are not saved in incognito. | |
611 Browser* incognito = CreateIncognitoBrowser(); | |
612 ui_test_utils::NavigateToURL(incognito, test_page_url_); | |
613 ASSERT_TRUE(RequestPermissionAndWait(incognito)); | |
614 PerformActionOnInfobar(incognito, DENY, 0, 0); | |
615 CloseBrowserWindow(incognito); | |
616 | |
617 incognito = CreateIncognitoBrowser(); | |
618 ui_test_utils::NavigateToURL(incognito, test_page_url_); | |
619 ASSERT_TRUE(RequestPermissionAndWait(incognito)); | |
620 PerformActionOnInfobar(incognito, ALLOW, 0, 0); | |
621 CreateSimpleNotification(incognito, true); | |
622 ASSERT_EQ(1, GetNotificationCount()); | |
623 CloseBrowserWindow(incognito); | |
624 | |
625 incognito = CreateIncognitoBrowser(); | |
626 ui_test_utils::NavigateToURL(incognito, test_page_url_); | |
627 ASSERT_TRUE(RequestPermissionAndWait(incognito)); | |
628 | |
629 ContentSettingsForOneType settings; | |
630 GetPrefsByContentSetting(CONTENT_SETTING_BLOCK, &settings); | |
631 EXPECT_EQ(0U, settings.size()); | |
632 GetPrefsByContentSetting(CONTENT_SETTING_ALLOW, &settings); | |
633 EXPECT_EQ(0U, settings.size()); | |
634 } | |
635 | |
636 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestExitBrowserWithInfobar) { | |
637 // Exit the browser window, when the infobar appears. | |
638 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
639 ASSERT_TRUE(RequestPermissionAndWait(browser())); | |
640 } | |
641 | |
642 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCrashTabWithPermissionInfobar) { | |
643 // Test crashing the tab with permission infobar doesn't crash Chrome. | |
644 ui_test_utils::NavigateToURLWithDisposition( | |
645 browser(), | |
646 empty_page_url_, | |
647 NEW_BACKGROUND_TAB, | |
648 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | |
649 chrome::ActivateTabAt(browser(), 0, true); | |
650 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
651 ASSERT_TRUE(RequestPermissionAndWait(browser())); | |
652 CrashTab(browser(), 0); | |
653 } | |
654 | |
655 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestKillNotificationProcess) { | |
656 // Test killing a notification doesn't crash Chrome. | |
657 AllowAllOrigins(); | |
658 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
659 CreateSimpleNotification(browser(), true); | |
660 ASSERT_EQ(1, GetNotificationCount()); | |
661 | |
662 const std::deque<Balloon*>& balloons = GetActiveBalloons(); | |
663 ASSERT_EQ(1U, balloons.size()); | |
664 CrashNotification(balloons[0]); | |
665 ASSERT_EQ(0, GetNotificationCount()); | |
666 } | |
667 | |
668 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestIncognitoNotification) { | |
669 // Test notifications in incognito window. | |
670 Browser* browser = CreateIncognitoBrowser(); | |
671 ui_test_utils::NavigateToURL(browser, test_page_url_); | |
672 chrome::ActivateTabAt(browser, 0, true); | |
673 ASSERT_TRUE(RequestPermissionAndWait(browser)); | |
674 PerformActionOnInfobar(browser, ALLOW, 0, 0); | |
675 CreateSimpleNotification(browser, true); | |
676 ASSERT_EQ(1, GetNotificationCount()); | |
677 } | |
678 | |
679 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCloseTabWithPermissionInfobar) { | |
680 // Test that user can close tab when infobar present. | |
681 ui_test_utils::NavigateToURLWithDisposition( | |
682 browser(), | |
683 GURL("about:blank"), | |
684 NEW_BACKGROUND_TAB, | |
685 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | |
686 chrome::ActivateTabAt(browser(), 0, true); | |
687 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
688 ASSERT_TRUE(RequestPermissionAndWait(browser())); | |
689 content::WindowedNotificationObserver observer( | |
690 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
691 content::NotificationService::AllSources()); | |
692 chrome::CloseWebContents(browser(), chrome::GetWebContentsAt(browser(), 0)); | |
693 observer.Wait(); | |
694 } | |
695 | |
696 IN_PROC_BROWSER_TEST_F( | |
697 NotificationsTest, | |
698 TestNavigateAwayWithPermissionInfobar) { | |
699 // Test navigating away when an infobar is present, | |
700 // then trying to create a notification from the same page. | |
701 ui_test_utils::NavigateToURLWithDisposition( | |
702 browser(), | |
703 GURL("about:blank"), | |
704 NEW_BACKGROUND_TAB, | |
705 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | |
706 chrome::ActivateTabAt(browser(), 0, true); | |
707 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
708 ASSERT_TRUE(RequestPermissionAndWait(browser())); | |
709 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
710 ASSERT_TRUE(RequestPermissionAndWait(browser())); | |
711 PerformActionOnInfobar(browser(), ALLOW, 0, 0); | |
712 CreateSimpleNotification(browser(), true); | |
713 ASSERT_EQ(1, GetNotificationCount()); | |
714 } | |
715 | |
716 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestCrashRendererNotificationRemain) { | |
717 // Test crashing renderer does not close or crash notification. | |
718 AllowAllOrigins(); | |
719 ui_test_utils::NavigateToURLWithDisposition( | |
720 browser(), | |
721 GURL("about:blank"), | |
722 NEW_BACKGROUND_TAB, | |
723 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | |
724 chrome::ActivateTabAt(browser(), 0, true); | |
725 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
726 CreateSimpleNotification(browser(), true); | |
727 ASSERT_EQ(1, GetNotificationCount()); | |
728 CrashTab(browser(), 0); | |
729 ASSERT_EQ(1, GetNotificationCount()); | |
730 } | |
731 | |
732 // Disable on MAC due to small screen, the third balloon fails to show up. | |
733 #if !defined(OS_MACOSX) | |
734 IN_PROC_BROWSER_TEST_F( | |
kkania
2012/11/26 21:08:50
just delete this test for now; some bots may have
chrisgao (Use stgao instead)
2012/11/28 19:19:04
Done.
| |
735 NotificationsTest, | |
736 TestNotificationOrderAfterClosingOne) { | |
737 // Tests that closing a notification leaves the rest | |
738 // of the notifications in the correct order. | |
739 AllowAllOrigins(); | |
740 | |
741 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
742 | |
743 CreateNotification(browser(), true, "", "Title1", "", "1"); | |
744 CreateNotification(browser(), true, "", "Title2", "", "2"); | |
745 CreateNotification(browser(), true, "", "Title3", "", "3"); | |
746 | |
747 ASSERT_EQ(3, GetNotificationCount()); | |
748 const std::deque<Balloon*>& old_balloons = GetActiveBalloons(); | |
749 | |
750 ASSERT_TRUE(CloseNotificationAndWait(old_balloons[1]->notification())); | |
751 | |
752 const std::deque<Balloon*>& new_balloons = GetActiveBalloons(); | |
753 ASSERT_EQ(2U, new_balloons.size()); | |
754 EXPECT_EQ(old_balloons[0]->notification().notification_id(), | |
755 new_balloons[0]->notification().notification_id()); | |
756 EXPECT_EQ(old_balloons[2]->notification().notification_id(), | |
757 new_balloons[1]->notification().notification_id()); | |
758 } | |
759 #endif // !defined(OS_MACOSX) | |
760 | |
761 IN_PROC_BROWSER_TEST_F(NotificationsTest, TestNotificationReplacement) { | |
762 // Test that we can replace a notification using the replaceId. | |
763 AllowAllOrigins(); | |
764 | |
765 ui_test_utils::NavigateToURL(browser(), test_page_url_); | |
766 | |
767 std::string result = CreateNotification( | |
768 browser(), | |
769 true, | |
770 "abc.png", | |
771 "Title1", | |
772 "Body1", | |
773 "chat"); | |
774 EXPECT_NE("-1", result); | |
775 | |
776 ASSERT_EQ(1, GetNotificationCount()); | |
777 | |
778 result = CreateNotification( | |
779 browser(), | |
780 false, | |
781 "no_such_file.png", | |
782 "Title2", | |
783 "Body2", | |
784 "chat"); | |
785 EXPECT_NE("-1", result); | |
786 | |
787 const std::deque<Balloon*>& balloons2 = GetActiveBalloons(); | |
788 EXPECT_EQ(1U, balloons2.size()); | |
789 GURL EXPECTED_ICON_URL = test_server()->GetURL(kExpectedIconUrl); | |
790 const Notification& notification = balloons2[0]->notification(); | |
791 EXPECT_EQ(EXPECTED_ICON_URL, notification.icon_url()); | |
792 EXPECT_EQ(ASCIIToUTF16("Title2"), notification.title()); | |
793 EXPECT_EQ(ASCIIToUTF16("Body2"), notification.body()); | |
794 } | |
795 | |
796 #endif // !defined(OS_CHROMEOS) | |
OLD | NEW |