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 "base/strings/string_number_conversions.h" | 5 #include "base/strings/string_number_conversions.h" |
6 #include "base/strings/stringprintf.h" | 6 #include "base/strings/stringprintf.h" |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/apps/app_browsertest_util.h" | |
8 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/extensions/api/notifications/notifications_api.h" | 10 #include "chrome/browser/extensions/api/notifications/notifications_api.h" |
10 #include "chrome/browser/extensions/extension_apitest.h" | 11 #include "chrome/browser/extensions/extension_apitest.h" |
11 #include "chrome/browser/extensions/extension_function_test_utils.h" | 12 #include "chrome/browser/extensions/extension_function_test_utils.h" |
12 #include "chrome/browser/notifications/notification.h" | 13 #include "chrome/browser/notifications/notification.h" |
13 #include "chrome/browser/notifications/notification_ui_manager.h" | 14 #include "chrome/browser/notifications/notification_ui_manager.h" |
14 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
16 #include "chrome/browser/ui/extensions/app_launch_params.h" | |
17 #include "chrome/browser/ui/extensions/application_launch.h" | |
15 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
16 #include "content/public/test/test_utils.h" | 19 #include "content/public/test/test_utils.h" |
17 #include "extensions/browser/api/test/test_api.h" | 20 #include "extensions/browser/api/test/test_api.h" |
18 #include "extensions/browser/notification_types.h" | 21 #include "extensions/browser/notification_types.h" |
19 #include "extensions/common/features/feature.h" | 22 #include "extensions/common/features/feature.h" |
20 #include "extensions/common/test_util.h" | 23 #include "extensions/common/test_util.h" |
24 #include "extensions/test/extension_test_message_listener.h" | |
21 #include "extensions/test/result_catcher.h" | 25 #include "extensions/test/result_catcher.h" |
22 #include "ui/message_center/message_center.h" | 26 #include "ui/message_center/message_center.h" |
23 #include "ui/message_center/notification_list.h" | 27 #include "ui/message_center/notification_list.h" |
24 #include "ui/message_center/notifier_settings.h" | 28 #include "ui/message_center/notifier_settings.h" |
25 | 29 |
26 using extensions::Extension; | 30 using extensions::Extension; |
27 using extensions::ResultCatcher; | 31 using extensions::ResultCatcher; |
28 | 32 |
29 namespace utils = extension_function_test_utils; | 33 namespace utils = extension_function_test_utils; |
30 | 34 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 content::NotificationRegistrar registrar_; | 78 content::NotificationRegistrar registrar_; |
75 | 79 |
76 // A sequential list of user gesture notifications from the test extension(s). | 80 // A sequential list of user gesture notifications from the test extension(s). |
77 std::deque<bool> results_; | 81 std::deque<bool> results_; |
78 | 82 |
79 // True if we're in a nested message loop waiting for results from | 83 // True if we're in a nested message loop waiting for results from |
80 // the extension. | 84 // the extension. |
81 bool waiting_; | 85 bool waiting_; |
82 }; | 86 }; |
83 | 87 |
88 enum class WindowState { | |
89 FULLSCREEN, | |
90 NORMAL | |
91 }; | |
92 static const char* const kWindowStateStrings[] = { | |
93 "fullscreen", | |
94 "normal" | |
95 }; | |
96 | |
84 class NotificationsApiTest : public ExtensionApiTest { | 97 class NotificationsApiTest : public ExtensionApiTest { |
85 public: | 98 public: |
86 const extensions::Extension* LoadExtensionAndWait( | 99 const Extension* LoadExtensionAndWait( |
87 const std::string& test_name) { | 100 const std::string& test_name) { |
88 base::FilePath extdir = test_data_dir_.AppendASCII(test_name); | 101 base::FilePath extdir = test_data_dir_.AppendASCII(test_name); |
89 content::WindowedNotificationObserver page_created( | 102 content::WindowedNotificationObserver page_created( |
90 extensions::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY, | 103 extensions::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY, |
91 content::NotificationService::AllSources()); | 104 content::NotificationService::AllSources()); |
92 const extensions::Extension* extension = LoadExtension(extdir); | 105 const extensions::Extension* extension = LoadExtension(extdir); |
93 if (extension) { | 106 if (extension) { |
94 page_created.Wait(); | 107 page_created.Wait(); |
95 } | 108 } |
96 return extension; | 109 return extension; |
97 } | 110 } |
98 | 111 |
112 const Extension* LoadAppWithWindowState( | |
113 const std::string& test_name, WindowState window_state) { | |
114 const std::string& create_window_options = base::StringPrintf( | |
115 "{\"state\":\"%s\"}", | |
116 kWindowStateStrings[static_cast<int>(window_state)]); | |
dewittj
2016/09/14 23:09:47
Sorry to be picky here, but since there's only 2 l
bmalcolm
2016/09/15 16:25:52
Done.
| |
117 base::FilePath extdir = test_data_dir_.AppendASCII(test_name); | |
118 const extensions::Extension* extension = LoadExtension(extdir); | |
119 EXPECT_TRUE(extension); | |
120 | |
121 ExtensionTestMessageListener launched_listener("launched", true); | |
122 LaunchPlatformApp(extension); | |
123 EXPECT_TRUE(launched_listener.WaitUntilSatisfied()); | |
124 launched_listener.Reply(create_window_options); | |
125 | |
126 return extension; | |
127 } | |
128 | |
99 protected: | 129 protected: |
100 std::string GetNotificationIdFromDelegateId(const std::string& delegate_id) { | 130 std::string GetNotificationIdFromDelegateId(const std::string& delegate_id) { |
101 return g_browser_process->notification_ui_manager() | 131 return g_browser_process->notification_ui_manager() |
102 ->FindById( | 132 ->FindById( |
103 delegate_id, | 133 delegate_id, |
104 NotificationUIManager::GetProfileID( | 134 NotificationUIManager::GetProfileID( |
105 g_browser_process->profile_manager()->GetLastUsedProfile())) | 135 g_browser_process->profile_manager()->GetLastUsedProfile())) |
106 ->id(); | 136 ->id(); |
107 } | 137 } |
138 | |
139 void LaunchPlatformApp(const Extension* extension) { | |
140 OpenApplication(AppLaunchParams( | |
141 browser()->profile(), extension, extensions::LAUNCH_CONTAINER_NONE, | |
142 WindowOpenDisposition::NEW_WINDOW, extensions::SOURCE_TEST)); | |
143 } | |
108 }; | 144 }; |
109 | 145 |
110 } // namespace | 146 } // namespace |
111 | 147 |
112 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestBasicUsage) { | 148 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestBasicUsage) { |
113 ASSERT_TRUE(RunExtensionTest("notifications/api/basic_usage")) << message_; | 149 ASSERT_TRUE(RunExtensionTest("notifications/api/basic_usage")) << message_; |
114 } | 150 } |
115 | 151 |
116 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestEvents) { | 152 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestEvents) { |
117 ASSERT_TRUE(RunExtensionTest("notifications/api/events")) << message_; | 153 ASSERT_TRUE(RunExtensionTest("notifications/api/events")) << message_; |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
295 ASSERT_TRUE(extension) << message_; | 331 ASSERT_TRUE(extension) << message_; |
296 | 332 |
297 const message_center::NotificationList::Notifications& notifications = | 333 const message_center::NotificationList::Notifications& notifications = |
298 g_browser_process->message_center()->GetVisibleNotifications(); | 334 g_browser_process->message_center()->GetVisibleNotifications(); |
299 ASSERT_EQ(1u, notifications.size()); | 335 ASSERT_EQ(1u, notifications.size()); |
300 message_center::Notification* notification = *(notifications.begin()); | 336 message_center::Notification* notification = *(notifications.begin()); |
301 ASSERT_EQ(extension->url(), notification->origin_url()); | 337 ASSERT_EQ(extension->url(), notification->origin_url()); |
302 | 338 |
303 EXPECT_TRUE(notification->never_timeout()); | 339 EXPECT_TRUE(notification->never_timeout()); |
304 } | 340 } |
341 | |
342 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestShouldDisplayNormal) { | |
343 ExtensionTestMessageListener notification_created_listener("created", false); | |
344 const Extension* extension = LoadAppWithWindowState( | |
345 "notifications/api/basic_app", WindowState::NORMAL); | |
346 ASSERT_TRUE(extension) << message_; | |
347 ASSERT_TRUE(notification_created_listener.WaitUntilSatisfied()); | |
348 | |
349 const message_center::NotificationList::Notifications& notifications = | |
350 g_browser_process->message_center()->GetVisibleNotifications(); | |
351 ASSERT_EQ(1u, notifications.size()); | |
352 message_center::Notification* notification = *(notifications.begin()); | |
353 // If the app hasn't created a fullscreen window, then its notifications | |
354 // shouldn't be displayed when a window is fullscreen. | |
355 ASSERT_FALSE(notification->delegate()->ShouldDisplayOverFullscreen()); | |
356 } | |
357 | |
358 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestShouldDisplayFullscreen) { | |
359 ExtensionTestMessageListener notification_created_listener("created", false); | |
360 const Extension* extension = LoadAppWithWindowState( | |
361 "notifications/api/basic_app", WindowState::FULLSCREEN); | |
362 ASSERT_TRUE(extension) << message_; | |
363 ASSERT_TRUE(notification_created_listener.WaitUntilSatisfied()); | |
364 | |
365 const message_center::NotificationList::Notifications& notifications = | |
366 g_browser_process->message_center()->GetVisibleNotifications(); | |
367 ASSERT_EQ(1u, notifications.size()); | |
368 message_center::Notification* notification = *(notifications.begin()); | |
369 // If the app has created a fullscreen window, then its notifications should | |
370 // be displayed when a window is fullscreen. | |
371 ASSERT_TRUE(notification->delegate()->ShouldDisplayOverFullscreen()); | |
372 } | |
373 | |
374 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestShouldDisplayMultiFullscreen) { | |
375 // Start a fullscreen app, and then start another fullscreen app on top of the | |
376 // first. Notifications from the first should not be displayed because it is | |
377 // not the app actually displaying on the screen. | |
378 ExtensionTestMessageListener notification_created_listener("created", false); | |
379 const Extension* extension1 = LoadAppWithWindowState( | |
380 "notifications/api/basic_app", WindowState::FULLSCREEN); | |
381 ASSERT_TRUE(extension1) << message_; | |
382 | |
383 ExtensionTestMessageListener window_visible_listener("visible", false); | |
384 const Extension* extension2 = LoadAppWithWindowState( | |
385 "notifications/api/other_app", WindowState::FULLSCREEN); | |
386 ASSERT_TRUE(extension2) << message_; | |
387 | |
388 ASSERT_TRUE(notification_created_listener.WaitUntilSatisfied()); | |
389 ASSERT_TRUE(window_visible_listener.WaitUntilSatisfied()); | |
390 | |
391 const message_center::NotificationList::Notifications& notifications = | |
392 g_browser_process->message_center()->GetVisibleNotifications(); | |
393 ASSERT_EQ(1u, notifications.size()); | |
394 message_center::Notification* notification = *(notifications.begin()); | |
395 // The first app window is superseded by the second window, so its | |
396 // notification shouldn't be displayed. | |
397 ASSERT_FALSE(notification->delegate()->ShouldDisplayOverFullscreen()); | |
398 } | |
OLD | NEW |