Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: chrome/browser/notifications/notification_test_util.cc

Issue 2729613006: When navigation focuses a web contents, also activate its window. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/notifications/notification_test_util.h" 5 #include "chrome/browser/notifications/notification_test_util.h"
6 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"
7 #include "chrome/browser/ui/browser.h" 13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_window.h"
8 #include "chrome/browser/ui/exclusive_access/exclusive_access_context.h" 15 #include "chrome/browser/ui/exclusive_access/exclusive_access_context.h"
9 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h" 16 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/chrome_features.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "components/content_settings/core/browser/host_content_settings_map.h"
21 #include "content/public/test/browser_test_utils.h"
10 #include "content/public/test/test_utils.h" 22 #include "content/public/test/test_utils.h"
23 #include "ui/message_center/message_center.h"
24 #include "ui/message_center/message_center_observer.h"
25
26 namespace {
27 //
28 // Used to observe the creation of permission prompt without responding.
29 class PermissionRequestObserver : public PermissionRequestManager::Observer {
30 public:
31 explicit PermissionRequestObserver(content::WebContents* web_contents)
32 : request_manager_(
33 PermissionRequestManager::FromWebContents(web_contents)),
34 request_shown_(false),
35 message_loop_runner_(new content::MessageLoopRunner) {
36 request_manager_->AddObserver(this);
37 }
38 ~PermissionRequestObserver() override {
39 // Safe to remove twice if it happens.
40 request_manager_->RemoveObserver(this);
41 }
42
43 void Wait() { message_loop_runner_->Run(); }
44
45 bool request_shown() { return request_shown_; }
46
47 private:
48 // PermissionRequestManager::Observer
49 void OnBubbleAdded() override {
50 request_shown_ = true;
51 request_manager_->RemoveObserver(this);
52 message_loop_runner_->Quit();
53 }
54
55 PermissionRequestManager* request_manager_;
56 bool request_shown_;
57 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
58
59 DISALLOW_COPY_AND_ASSIGN(PermissionRequestObserver);
60 };
61
62 } // namespace
11 63
12 MockNotificationDelegate::MockNotificationDelegate(const std::string& id) 64 MockNotificationDelegate::MockNotificationDelegate(const std::string& id)
13 : id_(id) {} 65 : id_(id) {}
14 66
15 MockNotificationDelegate::~MockNotificationDelegate() {} 67 MockNotificationDelegate::~MockNotificationDelegate() {}
16 68
17 std::string MockNotificationDelegate::id() const { return id_; } 69 std::string MockNotificationDelegate::id() const { return id_; }
18 70
19 // ----------------------------------------------------------------------------- 71 // -----------------------------------------------------------------------------
20 72
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 Browser* browser, bool desired_state) 210 Browser* browser, bool desired_state)
159 : browser_(browser), 211 : browser_(browser),
160 desired_state_(desired_state) {} 212 desired_state_(desired_state) {}
161 213
162 void FullscreenStateWaiter::Wait() { 214 void FullscreenStateWaiter::Wait() {
163 while (desired_state_ != 215 while (desired_state_ !=
164 browser_->exclusive_access_manager()->context()->IsFullscreen()) { 216 browser_->exclusive_access_manager()->context()->IsFullscreen()) {
165 content::RunAllPendingInMessageLoop(); 217 content::RunAllPendingInMessageLoop();
166 } 218 }
167 } 219 }
220
221 // -----------------------------------------------------------------------------
222
223 class MessageCenterChangeObserver::Impl
224 : public message_center::MessageCenterObserver {
225 public:
226 Impl() : notification_received_(false) {
227 message_center::MessageCenter::Get()->AddObserver(this);
228 }
229
230 ~Impl() override {
231 message_center::MessageCenter::Get()->RemoveObserver(this);
232 }
233
234 bool Wait() {
235 if (notification_received_)
236 return true;
237
238 message_loop_runner_ = new content::MessageLoopRunner;
239 message_loop_runner_->Run();
240 return notification_received_;
241 }
242
243 // message_center::MessageCenterObserver:
244 void OnNotificationAdded(const std::string& notification_id) override {
245 OnMessageCenterChanged();
246 }
247
248 void OnNotificationRemoved(const std::string& notification_id,
249 bool by_user) override {
250 OnMessageCenterChanged();
251 }
252
253 void OnNotificationUpdated(const std::string& notification_id) override {
254 OnMessageCenterChanged();
255 }
256
257 void OnNotificationClicked(const std::string& notification_id) override {
258 OnMessageCenterChanged();
259 }
260
261 void OnMessageCenterChanged() {
262 notification_received_ = true;
263 if (message_loop_runner_.get())
264 message_loop_runner_->Quit();
265 }
266
267 bool notification_received_;
268 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
269
270 DISALLOW_COPY_AND_ASSIGN(Impl);
271 };
272
273 MessageCenterChangeObserver::MessageCenterChangeObserver() : impl_(new Impl) {}
274 MessageCenterChangeObserver::~MessageCenterChangeObserver() = default;
275
276 bool MessageCenterChangeObserver::Wait() {
277 return impl_->Wait();
278 }
279
280 // -----------------------------------------------------------------------------
281
282 int NotificationsTest::GetNotificationCount() {
283 return message_center::MessageCenter::Get()->NotificationCount();
284 }
285
286 int NotificationsTest::GetNotificationPopupCount() {
287 return message_center::MessageCenter::Get()->GetPopupNotifications().size();
288 }
289
290 void NotificationsTest::CloseBrowserWindow(Browser* browser) {
291 content::WindowedNotificationObserver observer(
292 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(browser));
293 browser->window()->Close();
294 observer.Wait();
295 }
296
297 void NotificationsTest::CrashTab(Browser* browser, int index) {
298 content::CrashTab(browser->tab_strip_model()->GetWebContentsAt(index));
299 }
300
301 void NotificationsTest::DenyOrigin(const GURL& origin) {
302 DropOriginPreference(origin);
303 DesktopNotificationProfileUtil::DenyPermission(browser()->profile(), origin);
304 }
305
306 void NotificationsTest::AllowOrigin(const GURL& origin) {
307 DropOriginPreference(origin);
308 DesktopNotificationProfileUtil::GrantPermission(browser()->profile(), origin);
309 }
310
311 void NotificationsTest::AllowAllOrigins() {
312 // Reset all origins
313 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
314 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
315 SetDefaultContentSetting(CONTENT_SETTING_ALLOW);
316 }
317
318 void NotificationsTest::SetDefaultContentSetting(ContentSetting setting) {
319 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
320 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting);
321 }
322
323 std::string NotificationsTest::CreateNotification(Browser* browser,
324 bool wait_for_new_balloon,
325 const char* icon,
326 const char* title,
327 const char* body,
328 const char* replace_id,
329 const char* onclick) {
330 std::string script = base::StringPrintf(
331 "createNotification('%s', '%s', '%s', '%s', (e) => { %s });", icon, title,
332 body, replace_id, onclick);
333
334 MessageCenterChangeObserver observer;
335 std::string result;
336 bool success = content::ExecuteScriptAndExtractString(
337 GetActiveWebContents(browser), script, &result);
338 if (success && result != "-1" && wait_for_new_balloon)
339 success = observer.Wait();
340 EXPECT_TRUE(success);
341
342 return result;
343 }
344
345 std::string NotificationsTest::CreateSimpleNotification(
346 Browser* browser,
347 bool wait_for_new_balloon) {
348 return CreateNotification(browser, wait_for_new_balloon, "no_such_file.png",
349 "My Title", "My Body", "");
350 }
351
352 std::string NotificationsTest::RequestAndRespondToPermission(
353 Browser* browser,
354 PermissionRequestManager::AutoResponseType bubble_response) {
355 std::string result;
356 content::WebContents* web_contents = GetActiveWebContents(browser);
357 PermissionRequestManager::FromWebContents(web_contents)
358 ->set_auto_response_for_test(bubble_response);
359 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
360 web_contents, "requestPermission();", &result));
361 return result;
362 }
363
364 bool NotificationsTest::RequestAndAcceptPermission(Browser* browser) {
365 std::string result = RequestAndRespondToPermission(
366 browser, PermissionRequestManager::ACCEPT_ALL);
367 return "request-callback-granted" == result;
368 }
369
370 bool NotificationsTest::RequestAndDenyPermission(Browser* browser) {
371 std::string result = RequestAndRespondToPermission(
372 browser, PermissionRequestManager::DENY_ALL);
373 return "request-callback-denied" == result;
374 }
375
376 bool NotificationsTest::RequestAndDismissPermission(Browser* browser) {
377 std::string result =
378 RequestAndRespondToPermission(browser, PermissionRequestManager::DISMISS);
379 return "request-callback-default" == result;
380 }
381
382 bool NotificationsTest::RequestPermissionAndWait(Browser* browser) {
383 content::WebContents* web_contents = GetActiveWebContents(browser);
384 ui_test_utils::NavigateToURL(browser, GetTestPageURL());
385 PermissionRequestObserver observer(web_contents);
386 std::string result;
387 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
388 web_contents, "requestPermissionAndRespond();", &result));
389 EXPECT_EQ("requested", result);
390 observer.Wait();
391 return observer.request_shown();
392 }
393
394 bool NotificationsTest::CancelNotification(const char* notification_id,
395 Browser* browser) {
396 std::string script =
397 base::StringPrintf("cancelNotification('%s');", notification_id);
398
399 MessageCenterChangeObserver observer;
400 std::string result;
401 bool success = content::ExecuteScriptAndExtractString(
402 GetActiveWebContents(browser), script, &result);
403 if (!success || result != "1")
404 return false;
405 return observer.Wait();
406 }
407
408 void NotificationsTest::GetPrefsByContentSetting(
409 ContentSetting setting,
410 ContentSettingsForOneType* settings) {
411 DesktopNotificationProfileUtil::GetNotificationsSettings(browser()->profile(),
412 settings);
413 for (ContentSettingsForOneType::iterator it = settings->begin();
414 it != settings->end();) {
415 if (it->setting != setting || it->source.compare("preference") != 0)
416 it = settings->erase(it);
417 else
418 ++it;
419 }
420 }
421
422 bool NotificationsTest::CheckOriginInSetting(
423 const ContentSettingsForOneType& settings,
424 const GURL& origin) {
425 ContentSettingsPattern pattern =
426 ContentSettingsPattern::FromURLNoWildcard(origin);
427 for (ContentSettingsForOneType::const_iterator it = settings.begin();
428 it != settings.end(); ++it) {
429 if (it->primary_pattern == pattern)
430 return true;
431 }
432 return false;
433 }
434
435 GURL NotificationsTest::GetTestPageURLForFile(const std::string& file) const {
436 return embedded_test_server()->GetURL(std::string("/notifications/") + file);
437 }
438
439 GURL NotificationsTest::GetTestPageURL() const {
440 return GetTestPageURLForFile("notification_tester.html");
441 }
442
443 content::WebContents* NotificationsTest::GetActiveWebContents(
444 Browser* browser) {
445 return browser->tab_strip_model()->GetActiveWebContents();
446 }
447
448 void NotificationsTest::EnableFullscreenNotifications() {
449 feature_list_.InitWithFeatures(
450 {features::kPreferHtmlOverPlugins,
451 features::kAllowFullscreenWebNotificationsFeature},
452 {});
453 }
454
455 void NotificationsTest::DisableFullscreenNotifications() {
456 feature_list_.InitWithFeatures(
457 {features::kPreferHtmlOverPlugins},
458 {features::kAllowFullscreenWebNotificationsFeature});
459 }
460
461 void NotificationsTest::DropOriginPreference(const GURL& origin) {
462 DesktopNotificationProfileUtil::ClearSetting(browser()->profile(), origin);
463 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698