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

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: Fix Android compile Created 5 years, 10 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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 164
164 void LoadTestPage(const std::string& path) { 165 void LoadTestPage(const std::string& path) {
165 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(path)); 166 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(path));
166 } 167 }
167 168
168 void LoadTestPage() { 169 void LoadTestPage() {
169 LoadTestPage(GetTestURL()); 170 LoadTestPage(GetTestURL());
170 } 171 }
171 172
172 bool RunScript(const std::string& script, std::string* result) { 173 bool RunScript(const std::string& script, std::string* result) {
173 return content::ExecuteScriptAndExtractString( 174 return RunScript(script, result, nullptr);
174 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), 175 }
175 script, 176
176 result); 177 bool RunScript(const std::string& script, std::string* result,
178 content::WebContents* web_contents) {
179 if (!web_contents)
180 web_contents = browser()->tab_strip_model()->GetActiveWebContents();
181 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(),
182 script,
183 result);
177 } 184 }
178 185
179 void TryToRegisterSuccessfully( 186 void TryToRegisterSuccessfully(
180 const std::string& expected_push_registration_id); 187 const std::string& expected_push_registration_id);
181 188
182 net::SpawnedTestServer* https_server() const { return https_server_.get(); } 189 net::SpawnedTestServer* https_server() const { return https_server_.get(); }
183 190
184 FakeGCMProfileService* gcm_service() const { return gcm_service_; } 191 FakeGCMProfileService* gcm_service() const { return gcm_service_; }
185 192
186 #if defined(ENABLE_NOTIFICATIONS) 193 #if defined(ENABLE_NOTIFICATIONS)
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 ASSERT_EQ("false - is not controlled", script_result); 486 ASSERT_EQ("false - is not controlled", script_result);
480 487
481 LoadTestPage(); // Reload to become controlled. 488 LoadTestPage(); // Reload to become controlled.
482 489
483 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 490 ASSERT_TRUE(RunScript("isControlled()", &script_result));
484 ASSERT_EQ("true - is controlled", script_result); 491 ASSERT_EQ("true - is controlled", script_result);
485 492
486 notification_manager()->CancelAll(); 493 notification_manager()->CancelAll();
487 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); 494 ASSERT_EQ(0u, notification_manager()->GetNotificationCount());
488 495
496 // We'll need to specify the web_contents in which to eval script, since we're
497 // going to run script in a background tab.
498 content::WebContents* web_contents =
499 browser()->tab_strip_model()->GetActiveWebContents();
500
501 // If the site is visible in an active tab, we should not force a notification
502 // to be shown.
503 GCMClient::IncomingMessage message;
504 message.data["data"] = "testdata";
505 push_service()->OnMessage(app_id.ToString(), message);
506 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result));
507 EXPECT_EQ("testdata", script_result);
508 ASSERT_EQ(0u, notification_manager()->GetNotificationCount());
509
510 // Open a blank foreground tab so site is no longer visible.
511 ui_test_utils::NavigateToURLWithDisposition(
512 browser(), GURL("about:blank"), NEW_FOREGROUND_TAB,
513 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
514
489 // If the Service Worker push event handler shows a notification, we should 515 // If the Service Worker push event handler shows a notification, we should
490 // not show a forced one. 516 // not show a forced one.
491 GCMClient::IncomingMessage message;
492 message.data["data"] = "shownotification"; 517 message.data["data"] = "shownotification";
493 push_service()->OnMessage(app_id.ToString(), message); 518 push_service()->OnMessage(app_id.ToString(), message);
494 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); 519 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
495 EXPECT_EQ("shownotification", script_result); 520 EXPECT_EQ("shownotification", script_result);
496 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); 521 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
497 EXPECT_EQ(base::ASCIIToUTF16("push_test_tag"), 522 EXPECT_EQ(base::ASCIIToUTF16("push_test_tag"),
498 notification_manager()->GetNotificationAt(0).replace_id()); 523 notification_manager()->GetNotificationAt(0).replace_id());
499 524
500 notification_manager()->CancelAll(); 525 notification_manager()->CancelAll();
501 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); 526 ASSERT_EQ(0u, notification_manager()->GetNotificationCount());
502 527
503 // However if the Service Worker push event handler does not show a 528 // However if the Service Worker push event handler does not show a
504 // notification, we should show a forced one. 529 // notification, we should show a forced one.
505 message.data["data"] = "testdata"; 530 message.data["data"] = "testdata";
506 push_service()->OnMessage(app_id.ToString(), message); 531 push_service()->OnMessage(app_id.ToString(), message);
507 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); 532 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
508 EXPECT_EQ("testdata", script_result); 533 EXPECT_EQ("testdata", script_result);
509 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); 534 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
510 EXPECT_EQ(base::ASCIIToUTF16(kPushMessagingForcedNotificationTag), 535 EXPECT_EQ(base::ASCIIToUTF16(kPushMessagingForcedNotificationTag),
511 notification_manager()->GetNotificationAt(0).replace_id()); 536 notification_manager()->GetNotificationAt(0).replace_id());
512 537
513 // Currently, this notification will stick around until the user or webapp 538 // Currently, this notification will stick around until the user or webapp
514 // explicitly dismisses it (though we may change this later). 539 // explicitly dismisses it (though we may change this later).
515 message.data["data"] = "shownotification"; 540 message.data["data"] = "shownotification";
516 push_service()->OnMessage(app_id.ToString(), message); 541 push_service()->OnMessage(app_id.ToString(), message);
517 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); 542 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
518 EXPECT_EQ("shownotification", script_result); 543 EXPECT_EQ("shownotification", script_result);
519 ASSERT_EQ(2u, notification_manager()->GetNotificationCount()); 544 ASSERT_EQ(2u, notification_manager()->GetNotificationCount());
520 } 545 }
521 #endif 546 #endif
522 547
523 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, HasPermissionSaysDefault) { 548 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, HasPermissionSaysDefault) {
524 std::string script_result; 549 std::string script_result;
525 550
526 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 551 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
527 ASSERT_EQ("ok - service worker registered", script_result); 552 ASSERT_EQ("ok - service worker registered", script_result);
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
596 621
597 gcm_service()->AddExpectedUnregisterResponse(GCMClient::UNKNOWN_ERROR); 622 gcm_service()->AddExpectedUnregisterResponse(GCMClient::UNKNOWN_ERROR);
598 623
599 ASSERT_TRUE(RunScript("unregister()", &script_result)); 624 ASSERT_TRUE(RunScript("unregister()", &script_result));
600 EXPECT_EQ("unregister error: " 625 EXPECT_EQ("unregister error: "
601 "UnknownError: Unexpected error while trying to unregister from the" 626 "UnknownError: Unexpected error while trying to unregister from the"
602 " push server.", script_result); 627 " push server.", script_result);
603 } 628 }
604 629
605 } // namespace gcm 630 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698