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

Side by Side Diff: chrome/browser/services/gcm/push_messaging_browsertest.cc

Issue 866443003: Push API: Don't require notification if tab is visible. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ycm_outdir
Patch Set: Created 5 years, 11 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <map> 5 #include <map>
6 #include <string> 6 #include <string>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 12 matching lines...) Expand all
23 #include "chrome/test/base/ui_test_utils.h" 23 #include "chrome/test/base/ui_test_utils.h"
24 #include "components/content_settings/core/browser/host_content_settings_map.h" 24 #include "components/content_settings/core/browser/host_content_settings_map.h"
25 #include "components/content_settings/core/common/content_settings_types.h" 25 #include "components/content_settings/core/common/content_settings_types.h"
26 #include "components/gcm_driver/gcm_client.h" 26 #include "components/gcm_driver/gcm_client.h"
27 #include "components/infobars/core/confirm_infobar_delegate.h" 27 #include "components/infobars/core/confirm_infobar_delegate.h"
28 #include "components/infobars/core/infobar.h" 28 #include "components/infobars/core/infobar.h"
29 #include "components/infobars/core/infobar_manager.h" 29 #include "components/infobars/core/infobar_manager.h"
30 #include "content/public/browser/web_contents.h" 30 #include "content/public/browser/web_contents.h"
31 #include "content/public/common/content_switches.h" 31 #include "content/public/common/content_switches.h"
32 #include "content/public/test/browser_test_utils.h" 32 #include "content/public/test/browser_test_utils.h"
33 #include "ui/base/window_open_disposition.h"
33 34
34 namespace gcm { 35 namespace gcm {
35 36
36 namespace { 37 namespace {
37 // Responds to a confirm infobar by accepting or cancelling it. Responds to at 38 // Responds to a confirm infobar by accepting or cancelling it. Responds to at
38 // most one infobar. 39 // most one infobar.
39 class InfoBarResponder : public infobars::InfoBarManager::Observer { 40 class InfoBarResponder : public infobars::InfoBarManager::Observer {
40 public: 41 public:
41 InfoBarResponder(Browser* browser, bool accept) 42 InfoBarResponder(Browser* browser, bool accept)
42 : infobar_service_(InfoBarService::FromWebContents( 43 : infobar_service_(InfoBarService::FromWebContents(
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 162
162 void LoadTestPage(const std::string& path) { 163 void LoadTestPage(const std::string& path) {
163 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(path)); 164 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(path));
164 } 165 }
165 166
166 void LoadTestPage() { 167 void LoadTestPage() {
167 LoadTestPage(GetTestURL()); 168 LoadTestPage(GetTestURL());
168 } 169 }
169 170
170 bool RunScript(const std::string& script, std::string* result) { 171 bool RunScript(const std::string& script, std::string* result) {
171 return content::ExecuteScriptAndExtractString( 172 return RunScript(script, result, nullptr);
172 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), 173 }
173 script, 174
174 result); 175 bool RunScript(const std::string& script, std::string* result,
176 content::WebContents* web_contents) {
177 if (!web_contents)
178 web_contents = browser()->tab_strip_model()->GetActiveWebContents();
179 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(),
180 script,
181 result);
175 } 182 }
176 183
177 void TryToRegisterSuccessfully( 184 void TryToRegisterSuccessfully(
178 const std::string& expected_push_registration_id); 185 const std::string& expected_push_registration_id);
179 186
180 net::SpawnedTestServer* https_server() const { return https_server_.get(); } 187 net::SpawnedTestServer* https_server() const { return https_server_.get(); }
181 188
182 FakeGCMProfileService* gcm_service() const { return gcm_service_; } 189 FakeGCMProfileService* gcm_service() const { return gcm_service_; }
183 190
184 #if defined(ENABLE_NOTIFICATIONS) 191 #if defined(ENABLE_NOTIFICATIONS)
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 ASSERT_EQ("false - is not controlled", script_result); 484 ASSERT_EQ("false - is not controlled", script_result);
478 485
479 LoadTestPage(); // Reload to become controlled. 486 LoadTestPage(); // Reload to become controlled.
480 487
481 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 488 ASSERT_TRUE(RunScript("isControlled()", &script_result));
482 ASSERT_EQ("true - is controlled", script_result); 489 ASSERT_EQ("true - is controlled", script_result);
483 490
484 notification_manager()->CancelAll(); 491 notification_manager()->CancelAll();
485 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); 492 ASSERT_EQ(0u, notification_manager()->GetNotificationCount());
486 493
494 // We'll need to specify the web_contents in which to eval script, since we're
495 // going to run script in a background tab.
496 content::WebContents* web_contents =
497 browser()->tab_strip_model()->GetActiveWebContents();
498
499 // If the site is visible in an active tab, we should not force a notification
500 // to be shown.
501 GCMClient::IncomingMessage message;
502 message.data["data"] = "testdata";
503 push_service()->OnMessage(app_id.ToString(), message);
504 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result));
505 EXPECT_EQ("testdata", script_result);
506 ASSERT_EQ(0u, notification_manager()->GetNotificationCount());
507
508 // Open a blank foreground tab so site is no longer visible.
509 ui_test_utils::NavigateToURLWithDisposition(
510 browser(), GURL("about:blank"), NEW_FOREGROUND_TAB,
511 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
512
487 // If the Service Worker push event handler shows a notification, we should 513 // If the Service Worker push event handler shows a notification, we should
488 // not show a forced one. 514 // not show a forced one.
489 GCMClient::IncomingMessage message;
490 message.data["data"] = "shownotification"; 515 message.data["data"] = "shownotification";
491 push_service()->OnMessage(app_id.ToString(), message); 516 push_service()->OnMessage(app_id.ToString(), message);
492 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); 517 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
493 EXPECT_EQ("shownotification", script_result); 518 EXPECT_EQ("shownotification", script_result);
494 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); 519 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
495 EXPECT_EQ(base::ASCIIToUTF16("push_test_tag"), 520 EXPECT_EQ(base::ASCIIToUTF16("push_test_tag"),
496 notification_manager()->GetNotificationAt(0).replace_id()); 521 notification_manager()->GetNotificationAt(0).replace_id());
497 522
498 notification_manager()->CancelAll(); 523 notification_manager()->CancelAll();
499 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); 524 ASSERT_EQ(0u, notification_manager()->GetNotificationCount());
500 525
501 // However if the Service Worker push event handler does not show a 526 // However if the Service Worker push event handler does not show a
502 // notification, we should show a forced one. 527 // notification, we should show a forced one.
503 message.data["data"] = "testdata"; 528 message.data["data"] = "testdata";
504 push_service()->OnMessage(app_id.ToString(), message); 529 push_service()->OnMessage(app_id.ToString(), message);
505 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); 530 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
506 EXPECT_EQ("testdata", script_result); 531 EXPECT_EQ("testdata", script_result);
507 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); 532 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
508 EXPECT_EQ(base::ASCIIToUTF16(kPushMessagingForcedNotificationTag), 533 EXPECT_EQ(base::ASCIIToUTF16(kPushMessagingForcedNotificationTag),
509 notification_manager()->GetNotificationAt(0).replace_id()); 534 notification_manager()->GetNotificationAt(0).replace_id());
510 535
511 // Currently, this notification will stick around until the user or webapp 536 // Currently, this notification will stick around until the user or webapp
512 // explicitly dismisses it (though we may change this later). 537 // explicitly dismisses it (though we may change this later).
513 message.data["data"] = "shownotification"; 538 message.data["data"] = "shownotification";
514 push_service()->OnMessage(app_id.ToString(), message); 539 push_service()->OnMessage(app_id.ToString(), message);
515 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); 540 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
516 EXPECT_EQ("shownotification", script_result); 541 EXPECT_EQ("shownotification", script_result);
517 ASSERT_EQ(2u, notification_manager()->GetNotificationCount()); 542 ASSERT_EQ(2u, notification_manager()->GetNotificationCount());
518 } 543 }
519 #endif 544 #endif
520 545
521 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, HasPermissionSaysDefault) { 546 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, HasPermissionSaysDefault) {
522 std::string script_result; 547 std::string script_result;
523 548
524 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 549 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
525 ASSERT_EQ("ok - service worker registered", script_result); 550 ASSERT_EQ("ok - service worker registered", script_result);
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 619
595 gcm_service()->AddExpectedUnregisterResponse(GCMClient::UNKNOWN_ERROR); 620 gcm_service()->AddExpectedUnregisterResponse(GCMClient::UNKNOWN_ERROR);
596 621
597 ASSERT_TRUE(RunScript("unregister()", &script_result)); 622 ASSERT_TRUE(RunScript("unregister()", &script_result));
598 EXPECT_EQ("unregister error: " 623 EXPECT_EQ("unregister error: "
599 "UnknownError: Unexpected error while trying to unregister from the" 624 "UnknownError: Unexpected error while trying to unregister from the"
600 " push server.", script_result); 625 " push server.", script_result);
601 } 626 }
602 627
603 } // namespace gcm 628 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698