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

Side by Side Diff: chrome/browser/push_messaging/push_messaging_browsertest.cc

Issue 955673004: Move gcm-independent parts of push messaging out of gcm namespace and directory (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: buildfix push_messaging_application_id_unittest Created 5 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 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"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/infobars/infobar_service.h" 12 #include "chrome/browser/infobars/infobar_service.h"
13 #include "chrome/browser/notifications/notification_test_util.h" 13 #include "chrome/browser/notifications/notification_test_util.h"
14 #include "chrome/browser/notifications/platform_notification_service_impl.h" 14 #include "chrome/browser/notifications/platform_notification_service_impl.h"
15 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/push_messaging/push_messaging_application_id.h"
17 #include "chrome/browser/push_messaging/push_messaging_constants.h"
18 #include "chrome/browser/push_messaging/push_messaging_service_factory.h"
16 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" 19 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
17 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" 20 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
18 #include "chrome/browser/services/gcm/push_messaging_application_id.h"
19 #include "chrome/browser/services/gcm/push_messaging_constants.h"
20 #include "chrome/browser/ui/browser.h" 21 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h" 22 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/test/base/in_process_browser_test.h" 23 #include "chrome/test/base/in_process_browser_test.h"
23 #include "chrome/test/base/ui_test_utils.h" 24 #include "chrome/test/base/ui_test_utils.h"
24 #include "components/content_settings/core/browser/host_content_settings_map.h" 25 #include "components/content_settings/core/browser/host_content_settings_map.h"
25 #include "components/content_settings/core/common/content_settings.h" 26 #include "components/content_settings/core/common/content_settings.h"
26 #include "components/content_settings/core/common/content_settings_types.h" 27 #include "components/content_settings/core/common/content_settings_types.h"
27 #include "components/gcm_driver/gcm_client.h" 28 #include "components/gcm_driver/gcm_client.h"
28 #include "components/infobars/core/confirm_infobar_delegate.h" 29 #include "components/infobars/core/confirm_infobar_delegate.h"
29 #include "components/infobars/core/infobar.h" 30 #include "components/infobars/core/infobar.h"
30 #include "components/infobars/core/infobar_manager.h" 31 #include "components/infobars/core/infobar_manager.h"
31 #include "content/public/browser/web_contents.h" 32 #include "content/public/browser/web_contents.h"
32 #include "content/public/common/content_switches.h" 33 #include "content/public/common/content_switches.h"
33 #include "content/public/test/browser_test_utils.h" 34 #include "content/public/test/browser_test_utils.h"
34 #include "ui/base/window_open_disposition.h" 35 #include "ui/base/window_open_disposition.h"
35 36
36 #if defined(OS_ANDROID) 37 #if defined(OS_ANDROID)
37 #include "base/android/build_info.h" 38 #include "base/android/build_info.h"
38 #endif 39 #endif
39 40
40 namespace gcm {
41
42 namespace { 41 namespace {
43 // Responds to a confirm infobar by accepting or cancelling it. Responds to at 42 // Responds to a confirm infobar by accepting or cancelling it. Responds to at
44 // most one infobar. 43 // most one infobar.
45 class InfoBarResponder : public infobars::InfoBarManager::Observer { 44 class InfoBarResponder : public infobars::InfoBarManager::Observer {
46 public: 45 public:
47 InfoBarResponder(Browser* browser, bool accept) 46 InfoBarResponder(Browser* browser, bool accept)
48 : infobar_service_(InfoBarService::FromWebContents( 47 : infobar_service_(InfoBarService::FromWebContents(
49 browser->tab_strip_model()->GetActiveWebContents())), 48 browser->tab_strip_model()->GetActiveWebContents())),
50 accept_(accept), 49 accept_(accept),
51 has_observed_(false) { 50 has_observed_(false) {
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 net::BaseTestServer::SSLOptions::CERT_OK), 179 net::BaseTestServer::SSLOptions::CERT_OK),
181 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/")))); 180 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/"))));
182 ASSERT_TRUE(https_server_->Start()); 181 ASSERT_TRUE(https_server_->Start());
183 182
184 #if defined(ENABLE_NOTIFICATIONS) 183 #if defined(ENABLE_NOTIFICATIONS)
185 notification_manager_.reset(new StubNotificationUIManager); 184 notification_manager_.reset(new StubNotificationUIManager);
186 notification_service()->SetNotificationUIManagerForTesting( 185 notification_service()->SetNotificationUIManagerForTesting(
187 notification_manager()); 186 notification_manager());
188 #endif 187 #endif
189 188
189 gcm::GCMProfileServiceFactory::SetGlobalTestingFactory(
190 &gcm::FakeGCMProfileService::Build);
191
190 InProcessBrowserTest::SetUp(); 192 InProcessBrowserTest::SetUp();
191 } 193 }
192 194
193 // InProcessBrowserTest: 195 // InProcessBrowserTest:
194 void SetUpOnMainThread() override { 196 void SetUpOnMainThread() override {
195 gcm_service_ = static_cast<FakeGCMProfileService*>( 197 gcm_service_ = gcm::GCMProfileServiceFactory::GetForProfile(
196 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( 198 browser()->profile())->AsFakeGCMProfileService();
197 browser()->profile(), &FakeGCMProfileService::Build));
198 gcm_service_->set_collect(true); 199 gcm_service_->set_collect(true);
200 push_service_ =
201 PushMessagingServiceFactory::GetForProfile(browser()->profile());
199 202
200 LoadTestPage(); 203 LoadTestPage();
201 204
202 InProcessBrowserTest::SetUpOnMainThread(); 205 InProcessBrowserTest::SetUpOnMainThread();
203 } 206 }
204 207
205 // InProcessBrowserTest: 208 // InProcessBrowserTest:
206 void TearDown() override { 209 void TearDown() override {
207 #if defined(ENABLE_NOTIFICATIONS) 210 #if defined(ENABLE_NOTIFICATIONS)
208 notification_service()->SetNotificationUIManagerForTesting(nullptr); 211 notification_service()->SetNotificationUIManagerForTesting(nullptr);
(...skipping 24 matching lines...) Expand all
233 } 236 }
234 237
235 void TryToRegisterSuccessfully( 238 void TryToRegisterSuccessfully(
236 const std::string& expected_push_registration_id); 239 const std::string& expected_push_registration_id);
237 240
238 PushMessagingApplicationId GetServiceWorkerAppId( 241 PushMessagingApplicationId GetServiceWorkerAppId(
239 int64 service_worker_registration_id); 242 int64 service_worker_registration_id);
240 243
241 net::SpawnedTestServer* https_server() const { return https_server_.get(); } 244 net::SpawnedTestServer* https_server() const { return https_server_.get(); }
242 245
243 FakeGCMProfileService* gcm_service() const { return gcm_service_; } 246 gcm::FakeGCMProfileService* gcm_service() const { return gcm_service_; }
244 247
245 #if defined(ENABLE_NOTIFICATIONS) 248 #if defined(ENABLE_NOTIFICATIONS)
246 StubNotificationUIManager* notification_manager() const { 249 StubNotificationUIManager* notification_manager() const {
247 return notification_manager_.get(); 250 return notification_manager_.get();
248 } 251 }
249 252
250 PlatformNotificationServiceImpl* notification_service() const { 253 PlatformNotificationServiceImpl* notification_service() const {
251 return PlatformNotificationServiceImpl::GetInstance(); 254 return PlatformNotificationServiceImpl::GetInstance();
252 } 255 }
253 #endif 256 #endif
254 257
255 PushMessagingServiceImpl* push_service() { 258 PushMessagingServiceImpl* push_service() { return push_service_; }
256 return static_cast<PushMessagingServiceImpl*>(
257 gcm_service_->push_messaging_service());
258 }
259 259
260 protected: 260 protected:
261 virtual std::string GetTestURL() { 261 virtual std::string GetTestURL() {
262 return "files/push_messaging/test.html"; 262 return "files/push_messaging/test.html";
263 } 263 }
264 264
265 private: 265 private:
266 scoped_ptr<net::SpawnedTestServer> https_server_; 266 scoped_ptr<net::SpawnedTestServer> https_server_;
267 FakeGCMProfileService* gcm_service_; 267 gcm::FakeGCMProfileService* gcm_service_;
268 PushMessagingServiceImpl* push_service_;
268 scoped_ptr<StubNotificationUIManager> notification_manager_; 269 scoped_ptr<StubNotificationUIManager> notification_manager_;
269 270
270 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); 271 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest);
271 }; 272 };
272 273
273 class PushMessagingBadManifestBrowserTest : public PushMessagingBrowserTest { 274 class PushMessagingBadManifestBrowserTest : public PushMessagingBrowserTest {
274 std::string GetTestURL() override { 275 std::string GetTestURL() override {
275 return "files/push_messaging/test_bad_manifest.html"; 276 return "files/push_messaging/test_bad_manifest.html";
276 } 277 }
277 }; 278 };
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 459 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
459 460
460 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 461 ASSERT_TRUE(RunScript("isControlled()", &script_result));
461 ASSERT_EQ("false - is not controlled", script_result); 462 ASSERT_EQ("false - is not controlled", script_result);
462 463
463 LoadTestPage(); // Reload to become controlled. 464 LoadTestPage(); // Reload to become controlled.
464 465
465 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 466 ASSERT_TRUE(RunScript("isControlled()", &script_result));
466 ASSERT_EQ("true - is controlled", script_result); 467 ASSERT_EQ("true - is controlled", script_result);
467 468
468 GCMClient::IncomingMessage message; 469 gcm::GCMClient::IncomingMessage message;
469 message.sender_id = "1234567890"; 470 message.sender_id = "1234567890";
470 message.data["data"] = "testdata"; 471 message.data["data"] = "testdata";
471 push_service()->OnMessage(app_id.app_id_guid(), message); 472 push_service()->OnMessage(app_id.app_id_guid(), message);
472 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); 473 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result));
473 EXPECT_EQ("testdata", script_result); 474 EXPECT_EQ("testdata", script_result);
474 } 475 }
475 476
476 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoServiceWorker) { 477 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoServiceWorker) {
477 if (!IsPushSupported()) 478 if (!IsPushSupported())
478 return; 479 return;
(...skipping 17 matching lines...) Expand all
496 // Unregister service worker. Sending a message should now fail. 497 // Unregister service worker. Sending a message should now fail.
497 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result)); 498 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result));
498 ASSERT_EQ("service worker unregistration status: true", script_result); 499 ASSERT_EQ("service worker unregistration status: true", script_result);
499 500
500 // When the push service will receive it next message, given that there is no 501 // When the push service will receive it next message, given that there is no
501 // SW available, it should unregister |app_id|. 502 // SW available, it should unregister |app_id|.
502 UnregistrationCallback callback; 503 UnregistrationCallback callback;
503 gcm_service()->SetUnregisterCallback(base::Bind(&UnregistrationCallback::Run, 504 gcm_service()->SetUnregisterCallback(base::Bind(&UnregistrationCallback::Run,
504 base::Unretained(&callback))); 505 base::Unretained(&callback)));
505 506
506 GCMClient::IncomingMessage message; 507 gcm::GCMClient::IncomingMessage message;
507 message.sender_id = "1234567890"; 508 message.sender_id = "1234567890";
508 message.data["data"] = "testdata"; 509 message.data["data"] = "testdata";
509 push_service()->OnMessage(app_id.app_id_guid(), message); 510 push_service()->OnMessage(app_id.app_id_guid(), message);
510 511
511 callback.WaitUntilSatisfied(); 512 callback.WaitUntilSatisfied();
512 EXPECT_EQ(app_id.app_id_guid(), callback.app_id()); 513 EXPECT_EQ(app_id.app_id_guid(), callback.app_id());
513 514
514 // No push data should have been received. 515 // No push data should have been received.
515 ASSERT_TRUE(RunScript("resultQueue.popImmediately()", &script_result)); 516 ASSERT_TRUE(RunScript("resultQueue.popImmediately()", &script_result));
516 EXPECT_EQ("null", script_result); 517 EXPECT_EQ("null", script_result);
(...skipping 24 matching lines...) Expand all
541 notification_manager()->CancelAll(); 542 notification_manager()->CancelAll();
542 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); 543 ASSERT_EQ(0u, notification_manager()->GetNotificationCount());
543 544
544 // We'll need to specify the web_contents in which to eval script, since we're 545 // We'll need to specify the web_contents in which to eval script, since we're
545 // going to run script in a background tab. 546 // going to run script in a background tab.
546 content::WebContents* web_contents = 547 content::WebContents* web_contents =
547 browser()->tab_strip_model()->GetActiveWebContents(); 548 browser()->tab_strip_model()->GetActiveWebContents();
548 549
549 // If the site is visible in an active tab, we should not force a notification 550 // If the site is visible in an active tab, we should not force a notification
550 // to be shown. Try it twice, since we allow one mistake per 10 push events. 551 // to be shown. Try it twice, since we allow one mistake per 10 push events.
551 GCMClient::IncomingMessage message; 552 gcm::GCMClient::IncomingMessage message;
552 message.sender_id = "1234567890"; 553 message.sender_id = "1234567890";
553 for (int n = 0; n < 2; n++) { 554 for (int n = 0; n < 2; n++) {
554 message.data["data"] = "testdata"; 555 message.data["data"] = "testdata";
555 push_service()->OnMessage(app_id.app_id_guid(), message); 556 push_service()->OnMessage(app_id.app_id_guid(), message);
556 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); 557 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result));
557 EXPECT_EQ("testdata", script_result); 558 EXPECT_EQ("testdata", script_result);
558 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); 559 EXPECT_EQ(0u, notification_manager()->GetNotificationCount());
559 } 560 }
560 561
561 // Open a blank foreground tab so site is no longer visible. 562 // Open a blank foreground tab so site is no longer visible.
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 633
633 LoadTestPage(); // Reload to become controlled. 634 LoadTestPage(); // Reload to become controlled.
634 635
635 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 636 ASSERT_TRUE(RunScript("isControlled()", &script_result));
636 ASSERT_EQ("true - is controlled", script_result); 637 ASSERT_EQ("true - is controlled", script_result);
637 638
638 NotificationAddedCallback callback; 639 NotificationAddedCallback callback;
639 notification_manager()->SetNotificationAddedCallback( 640 notification_manager()->SetNotificationAddedCallback(
640 base::Bind(&NotificationAddedCallback::Run, base::Unretained(&callback))); 641 base::Bind(&NotificationAddedCallback::Run, base::Unretained(&callback)));
641 642
642 GCMClient::IncomingMessage message; 643 gcm::GCMClient::IncomingMessage message;
643 message.sender_id = "1234567890"; 644 message.sender_id = "1234567890";
644 message.data["data"] = "shownotification-without-waituntil"; 645 message.data["data"] = "shownotification-without-waituntil";
645 push_service()->OnMessage(app_id.app_id_guid(), message); 646 push_service()->OnMessage(app_id.app_id_guid(), message);
646 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); 647 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents));
647 EXPECT_EQ("immediate:shownotification-without-waituntil", script_result); 648 EXPECT_EQ("immediate:shownotification-without-waituntil", script_result);
648 649
649 callback.WaitUntilSatisfied(); 650 callback.WaitUntilSatisfied();
650 651
651 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); 652 ASSERT_EQ(1u, notification_manager()->GetNotificationCount());
652 EXPECT_EQ(base::ASCIIToUTF16("push_test_tag"), 653 EXPECT_EQ(base::ASCIIToUTF16("push_test_tag"),
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 } 714 }
714 715
715 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnregisterSuccess) { 716 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnregisterSuccess) {
716 if (!IsPushSupported()) 717 if (!IsPushSupported())
717 return; 718 return;
718 719
719 std::string script_result; 720 std::string script_result;
720 721
721 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); 722 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
722 723
723 gcm_service()->AddExpectedUnregisterResponse(GCMClient::SUCCESS); 724 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
724 725
725 ASSERT_TRUE(RunScript("unregister()", &script_result)); 726 ASSERT_TRUE(RunScript("unregister()", &script_result));
726 EXPECT_EQ("unregister result: true", script_result); 727 EXPECT_EQ("unregister result: true", script_result);
727 } 728 }
728 729
729 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnregisterNetworkError) { 730 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnregisterNetworkError) {
730 if (!IsPushSupported()) 731 if (!IsPushSupported())
731 return; 732 return;
732 733
733 std::string script_result; 734 std::string script_result;
734 735
735 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); 736 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
736 737
737 gcm_service()->AddExpectedUnregisterResponse(GCMClient::NETWORK_ERROR); 738 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::NETWORK_ERROR);
738 739
739 ASSERT_TRUE(RunScript("unregister()", &script_result)); 740 ASSERT_TRUE(RunScript("unregister()", &script_result));
740 EXPECT_EQ("unregister error: NetworkError: " 741 EXPECT_EQ("unregister error: NetworkError: "
741 "Unregistration failed - could not connect to push server", 742 "Unregistration failed - could not connect to push server",
742 script_result); 743 script_result);
743 } 744 }
744 745
745 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnregisterAbortError) { 746 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnregisterAbortError) {
746 if (!IsPushSupported()) 747 if (!IsPushSupported())
747 return; 748 return;
748 749
749 std::string script_result; 750 std::string script_result;
750 751
751 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); 752 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
752 753
753 gcm_service()->AddExpectedUnregisterResponse(GCMClient::UNKNOWN_ERROR); 754 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::UNKNOWN_ERROR);
754 755
755 ASSERT_TRUE(RunScript("unregister()", &script_result)); 756 ASSERT_TRUE(RunScript("unregister()", &script_result));
756 EXPECT_EQ("unregister error: " 757 EXPECT_EQ("unregister error: "
757 "AbortError: Unregistration failed - push service error", 758 "AbortError: Unregistration failed - push service error",
758 script_result); 759 script_result);
759 } 760 }
760 761
761 #if defined(OS_ANDROID) 762 #if defined(OS_ANDROID)
762 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushUnavailableOnAndroidICS) { 763 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushUnavailableOnAndroidICS) {
763 // This test should only run on Android ICS to confirm that the Push API 764 // This test should only run on Android ICS to confirm that the Push API
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
1002 // if the rules it used to have have been reset. 1003 // if the rules it used to have have been reset.
1003 // The Push service should not unsubcribe |origin| because at no point it was 1004 // The Push service should not unsubcribe |origin| because at no point it was
1004 // left without permission to use Push. 1005 // left without permission to use Push.
1005 1006
1006 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); 1007 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
1007 EXPECT_EQ("permission status - granted", script_result); 1008 EXPECT_EQ("permission status - granted", script_result);
1008 1009
1009 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); 1010 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
1010 EXPECT_EQ("true - registered", script_result); 1011 EXPECT_EQ("true - registered", script_result);
1011 } 1012 }
1012
1013 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698