OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/notifications/notification_interactive_uitest_support.h " | |
6 | |
7 #include "base/run_loop.h" | |
8 #include "chrome/browser/chrome_notification_types.h" | |
9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | |
10 #include "chrome/browser/notifications/desktop_notification_profile_util.h" | |
11 #include "chrome/browser/notifications/web_notification_delegate.h" | |
12 #include "chrome/browser/permissions/permission_request_manager.h" | |
13 #include "chrome/browser/ui/browser_window.h" | |
14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
15 #include "chrome/common/chrome_features.h" | |
16 #include "chrome/test/base/ui_test_utils.h" | |
17 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
18 #include "content/public/test/browser_test_utils.h" | |
19 #include "ui/message_center/message_center.h" | |
20 #include "ui/message_center/message_center_observer.h" | |
21 | |
22 namespace { | |
23 // | |
Nico
2017/03/08 16:23:25
remove //
Sidney San Martín
2017/03/08 19:34:47
Done.
| |
24 // Used to observe the creation of permission prompt without responding. | |
25 class PermissionRequestObserver : public PermissionRequestManager::Observer { | |
26 public: | |
27 explicit PermissionRequestObserver(content::WebContents* web_contents) | |
28 : request_manager_( | |
29 PermissionRequestManager::FromWebContents(web_contents)), | |
30 request_shown_(false), | |
31 message_loop_runner_(new content::MessageLoopRunner) { | |
32 request_manager_->AddObserver(this); | |
33 } | |
34 ~PermissionRequestObserver() override { | |
35 // Safe to remove twice if it happens. | |
36 request_manager_->RemoveObserver(this); | |
37 } | |
38 | |
39 void Wait() { message_loop_runner_->Run(); } | |
40 | |
41 bool request_shown() { return request_shown_; } | |
42 | |
43 private: | |
44 // PermissionRequestManager::Observer | |
45 void OnBubbleAdded() override { | |
46 request_shown_ = true; | |
47 request_manager_->RemoveObserver(this); | |
48 message_loop_runner_->Quit(); | |
49 } | |
50 | |
51 PermissionRequestManager* request_manager_; | |
52 bool request_shown_; | |
53 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
54 | |
55 DISALLOW_COPY_AND_ASSIGN(PermissionRequestObserver); | |
56 }; | |
57 | |
58 } // namespace | |
59 | |
60 class MessageCenterChangeObserver::Impl | |
61 : public message_center::MessageCenterObserver { | |
62 public: | |
63 Impl() : notification_received_(false) { | |
64 message_center::MessageCenter::Get()->AddObserver(this); | |
65 } | |
66 | |
67 ~Impl() override { | |
68 message_center::MessageCenter::Get()->RemoveObserver(this); | |
69 } | |
70 | |
71 bool Wait() { | |
72 if (notification_received_) | |
73 return true; | |
74 | |
75 message_loop_runner_ = new content::MessageLoopRunner; | |
76 message_loop_runner_->Run(); | |
77 return notification_received_; | |
78 } | |
79 | |
80 // message_center::MessageCenterObserver: | |
81 void OnNotificationAdded(const std::string& notification_id) override { | |
82 OnMessageCenterChanged(); | |
83 } | |
84 | |
85 void OnNotificationRemoved(const std::string& notification_id, | |
86 bool by_user) override { | |
87 OnMessageCenterChanged(); | |
88 } | |
89 | |
90 void OnNotificationUpdated(const std::string& notification_id) override { | |
91 OnMessageCenterChanged(); | |
92 } | |
93 | |
94 void OnNotificationClicked(const std::string& notification_id) override { | |
95 OnMessageCenterChanged(); | |
96 } | |
97 | |
98 void OnMessageCenterChanged() { | |
99 notification_received_ = true; | |
100 if (message_loop_runner_.get()) | |
101 message_loop_runner_->Quit(); | |
102 } | |
103 | |
104 bool notification_received_; | |
105 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
106 | |
107 DISALLOW_COPY_AND_ASSIGN(Impl); | |
108 }; | |
109 | |
110 MessageCenterChangeObserver::MessageCenterChangeObserver() : impl_(new Impl) {} | |
111 MessageCenterChangeObserver::~MessageCenterChangeObserver() = default; | |
112 | |
113 bool MessageCenterChangeObserver::Wait() { | |
114 return impl_->Wait(); | |
115 } | |
116 | |
117 // ----------------------------------------------------------------------------- | |
118 | |
119 int NotificationsTest::GetNotificationCount() { | |
120 return message_center::MessageCenter::Get()->NotificationCount(); | |
121 } | |
122 | |
123 int NotificationsTest::GetNotificationPopupCount() { | |
124 return message_center::MessageCenter::Get()->GetPopupNotifications().size(); | |
125 } | |
126 | |
127 void NotificationsTest::CloseBrowserWindow(Browser* browser) { | |
128 content::WindowedNotificationObserver observer( | |
129 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(browser)); | |
130 browser->window()->Close(); | |
131 observer.Wait(); | |
132 } | |
133 | |
134 void NotificationsTest::CrashTab(Browser* browser, int index) { | |
135 content::CrashTab(browser->tab_strip_model()->GetWebContentsAt(index)); | |
136 } | |
137 | |
138 void NotificationsTest::DenyOrigin(const GURL& origin) { | |
139 DropOriginPreference(origin); | |
140 DesktopNotificationProfileUtil::DenyPermission(browser()->profile(), origin); | |
141 } | |
142 | |
143 void NotificationsTest::AllowOrigin(const GURL& origin) { | |
144 DropOriginPreference(origin); | |
145 DesktopNotificationProfileUtil::GrantPermission(browser()->profile(), origin); | |
146 } | |
147 | |
148 void NotificationsTest::AllowAllOrigins() { | |
149 // Reset all origins | |
150 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) | |
151 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | |
152 SetDefaultContentSetting(CONTENT_SETTING_ALLOW); | |
153 } | |
154 | |
155 void NotificationsTest::SetDefaultContentSetting(ContentSetting setting) { | |
156 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) | |
157 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting); | |
158 } | |
159 | |
160 std::string NotificationsTest::CreateNotification(Browser* browser, | |
161 bool wait_for_new_balloon, | |
162 const char* icon, | |
163 const char* title, | |
164 const char* body, | |
165 const char* replace_id, | |
166 const char* onclick) { | |
167 std::string script = base::StringPrintf( | |
168 "createNotification('%s', '%s', '%s', '%s', (e) => { %s });", icon, title, | |
169 body, replace_id, onclick); | |
170 | |
171 MessageCenterChangeObserver observer; | |
172 std::string result; | |
173 bool success = content::ExecuteScriptAndExtractString( | |
174 GetActiveWebContents(browser), script, &result); | |
175 if (success && result != "-1" && wait_for_new_balloon) | |
176 success = observer.Wait(); | |
177 EXPECT_TRUE(success); | |
178 | |
179 return result; | |
180 } | |
181 | |
182 std::string NotificationsTest::CreateSimpleNotification( | |
183 Browser* browser, | |
184 bool wait_for_new_balloon) { | |
185 return CreateNotification(browser, wait_for_new_balloon, "no_such_file.png", | |
186 "My Title", "My Body", ""); | |
187 } | |
188 | |
189 std::string NotificationsTest::RequestAndRespondToPermission( | |
190 Browser* browser, | |
191 PermissionRequestManager::AutoResponseType bubble_response) { | |
192 std::string result; | |
193 content::WebContents* web_contents = GetActiveWebContents(browser); | |
194 PermissionRequestManager::FromWebContents(web_contents) | |
195 ->set_auto_response_for_test(bubble_response); | |
196 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
197 web_contents, "requestPermission();", &result)); | |
198 return result; | |
199 } | |
200 | |
201 bool NotificationsTest::RequestAndAcceptPermission(Browser* browser) { | |
202 std::string result = RequestAndRespondToPermission( | |
203 browser, PermissionRequestManager::ACCEPT_ALL); | |
204 return "request-callback-granted" == result; | |
205 } | |
206 | |
207 bool NotificationsTest::RequestAndDenyPermission(Browser* browser) { | |
208 std::string result = RequestAndRespondToPermission( | |
209 browser, PermissionRequestManager::DENY_ALL); | |
210 return "request-callback-denied" == result; | |
211 } | |
212 | |
213 bool NotificationsTest::RequestAndDismissPermission(Browser* browser) { | |
214 std::string result = | |
215 RequestAndRespondToPermission(browser, PermissionRequestManager::DISMISS); | |
216 return "request-callback-default" == result; | |
217 } | |
218 | |
219 bool NotificationsTest::RequestPermissionAndWait(Browser* browser) { | |
220 content::WebContents* web_contents = GetActiveWebContents(browser); | |
221 ui_test_utils::NavigateToURL(browser, GetTestPageURL()); | |
222 PermissionRequestObserver observer(web_contents); | |
223 std::string result; | |
224 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
225 web_contents, "requestPermissionAndRespond();", &result)); | |
226 EXPECT_EQ("requested", result); | |
227 observer.Wait(); | |
228 return observer.request_shown(); | |
229 } | |
230 | |
231 bool NotificationsTest::CancelNotification(const char* notification_id, | |
232 Browser* browser) { | |
233 std::string script = | |
234 base::StringPrintf("cancelNotification('%s');", notification_id); | |
235 | |
236 MessageCenterChangeObserver observer; | |
237 std::string result; | |
238 bool success = content::ExecuteScriptAndExtractString( | |
239 GetActiveWebContents(browser), script, &result); | |
240 if (!success || result != "1") | |
241 return false; | |
242 return observer.Wait(); | |
243 } | |
244 | |
245 void NotificationsTest::GetPrefsByContentSetting( | |
246 ContentSetting setting, | |
247 ContentSettingsForOneType* settings) { | |
248 DesktopNotificationProfileUtil::GetNotificationsSettings(browser()->profile(), | |
249 settings); | |
250 for (ContentSettingsForOneType::iterator it = settings->begin(); | |
251 it != settings->end();) { | |
252 if (it->setting != setting || it->source.compare("preference") != 0) | |
253 it = settings->erase(it); | |
254 else | |
255 ++it; | |
256 } | |
257 } | |
258 | |
259 bool NotificationsTest::CheckOriginInSetting( | |
260 const ContentSettingsForOneType& settings, | |
261 const GURL& origin) { | |
262 ContentSettingsPattern pattern = | |
263 ContentSettingsPattern::FromURLNoWildcard(origin); | |
264 for (ContentSettingsForOneType::const_iterator it = settings.begin(); | |
Nico
2017/03/08 16:23:25
can be a for-each loop
Sidney San Martín
2017/03/08 19:34:47
Done.
| |
265 it != settings.end(); ++it) { | |
266 if (it->primary_pattern == pattern) | |
267 return true; | |
268 } | |
269 return false; | |
270 } | |
271 | |
272 GURL NotificationsTest::GetTestPageURLForFile(const std::string& file) const { | |
273 return embedded_test_server()->GetURL(std::string("/notifications/") + file); | |
274 } | |
275 | |
276 GURL NotificationsTest::GetTestPageURL() const { | |
277 return GetTestPageURLForFile("notification_tester.html"); | |
278 } | |
279 | |
280 content::WebContents* NotificationsTest::GetActiveWebContents( | |
281 Browser* browser) { | |
282 return browser->tab_strip_model()->GetActiveWebContents(); | |
283 } | |
284 | |
285 void NotificationsTest::EnableFullscreenNotifications() { | |
286 feature_list_.InitWithFeatures( | |
287 {features::kPreferHtmlOverPlugins, | |
288 features::kAllowFullscreenWebNotificationsFeature}, | |
289 {}); | |
290 } | |
291 | |
292 void NotificationsTest::DisableFullscreenNotifications() { | |
293 feature_list_.InitWithFeatures( | |
294 {features::kPreferHtmlOverPlugins}, | |
295 {features::kAllowFullscreenWebNotificationsFeature}); | |
296 } | |
297 | |
298 void NotificationsTest::DropOriginPreference(const GURL& origin) { | |
299 DesktopNotificationProfileUtil::ClearSetting(browser()->profile(), origin); | |
300 } | |
OLD | NEW |