OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "chrome/browser/infobars/infobar_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" |
| 13 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 14 #include "chrome/browser/services/gcm/push_messaging_application_id.h" |
| 15 #include "chrome/browser/services/gcm/push_messaging_constants.h" |
| 16 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 18 #include "chrome/test/base/in_process_browser_test.h" |
| 19 #include "chrome/test/base/ui_test_utils.h" |
| 20 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 21 #include "components/infobars/core/infobar.h" |
| 22 #include "components/infobars/core/infobar_manager.h" |
| 23 #include "content/public/browser/web_contents.h" |
| 24 #include "content/public/common/content_switches.h" |
| 25 #include "content/public/test/browser_test_utils.h" |
| 26 |
| 27 namespace gcm { |
| 28 |
| 29 namespace { |
| 30 // Responds to a confirm infobar by accepting or cancelling it. Responds to at |
| 31 // most one infobar. |
| 32 class InfoBarResponder : public infobars::InfoBarManager::Observer { |
| 33 public: |
| 34 InfoBarResponder(Browser* browser, bool accept) |
| 35 : infobar_service_(InfoBarService::FromWebContents( |
| 36 browser->tab_strip_model()->GetActiveWebContents())), |
| 37 accept_(accept), |
| 38 has_observed_(false) { |
| 39 infobar_service_->AddObserver(this); |
| 40 } |
| 41 |
| 42 virtual ~InfoBarResponder() { infobar_service_->RemoveObserver(this); } |
| 43 |
| 44 // infobars::InfoBarManager::Observer |
| 45 virtual void OnInfoBarAdded(infobars::InfoBar* infobar) override { |
| 46 if (has_observed_) |
| 47 return; |
| 48 has_observed_ = true; |
| 49 ConfirmInfoBarDelegate* delegate = |
| 50 infobar->delegate()->AsConfirmInfoBarDelegate(); |
| 51 DCHECK(delegate); |
| 52 |
| 53 // Respond to the infobar asynchronously, like a person. |
| 54 base::MessageLoop::current()->PostTask( |
| 55 FROM_HERE, |
| 56 base::Bind( |
| 57 &InfoBarResponder::Respond, base::Unretained(this), delegate)); |
| 58 } |
| 59 |
| 60 private: |
| 61 void Respond(ConfirmInfoBarDelegate* delegate) { |
| 62 if (accept_) { |
| 63 delegate->Accept(); |
| 64 } else { |
| 65 delegate->Cancel(); |
| 66 } |
| 67 } |
| 68 |
| 69 InfoBarService* infobar_service_; |
| 70 bool accept_; |
| 71 bool has_observed_; |
| 72 }; |
| 73 } // namespace |
| 74 |
| 75 class PushMessagingBrowserTest : public InProcessBrowserTest { |
| 76 public: |
| 77 PushMessagingBrowserTest() : gcm_service_(nullptr) {} |
| 78 virtual ~PushMessagingBrowserTest() {} |
| 79 |
| 80 // InProcessBrowserTest: |
| 81 virtual void SetUpCommandLine(base::CommandLine* command_line) override { |
| 82 command_line->AppendSwitch( |
| 83 switches::kEnableExperimentalWebPlatformFeatures); |
| 84 |
| 85 InProcessBrowserTest::SetUpCommandLine(command_line); |
| 86 } |
| 87 |
| 88 // InProcessBrowserTest: |
| 89 virtual void SetUp() override { |
| 90 https_server_.reset(new net::SpawnedTestServer( |
| 91 net::SpawnedTestServer::TYPE_HTTPS, |
| 92 net::BaseTestServer::SSLOptions( |
| 93 net::BaseTestServer::SSLOptions::CERT_OK), |
| 94 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/")))); |
| 95 ASSERT_TRUE(https_server_->Start()); |
| 96 |
| 97 InProcessBrowserTest::SetUp(); |
| 98 } |
| 99 |
| 100 // InProcessBrowserTest: |
| 101 virtual void SetUpOnMainThread() override { |
| 102 gcm_service_ = static_cast<FakeGCMProfileService*>( |
| 103 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 104 browser()->profile(), &FakeGCMProfileService::Build)); |
| 105 gcm_service_->set_collect(true); |
| 106 |
| 107 ui_test_utils::NavigateToURL( |
| 108 browser(), https_server_->GetURL("files/push_messaging/test.html")); |
| 109 |
| 110 InProcessBrowserTest::SetUpOnMainThread(); |
| 111 } |
| 112 |
| 113 bool RunScript(const std::string& script, std::string* result) { |
| 114 return content::ExecuteScriptAndExtractString( |
| 115 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), |
| 116 script, |
| 117 result); |
| 118 } |
| 119 |
| 120 bool RegisterServiceWorker(std::string* result) { |
| 121 return RunScript("registerServiceWorker()", result); |
| 122 } |
| 123 |
| 124 bool RegisterPush(std::string* result) { |
| 125 return RunScript("registerPush('1234567890')", result); |
| 126 } |
| 127 |
| 128 net::SpawnedTestServer* https_server() const { return https_server_.get(); } |
| 129 |
| 130 FakeGCMProfileService* gcm_service() const { return gcm_service_; } |
| 131 |
| 132 private: |
| 133 scoped_ptr<net::SpawnedTestServer> https_server_; |
| 134 FakeGCMProfileService* gcm_service_; |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); |
| 137 }; |
| 138 |
| 139 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterSuccess) { |
| 140 std::string register_worker_result; |
| 141 ASSERT_TRUE(RegisterServiceWorker(®ister_worker_result)); |
| 142 ASSERT_EQ("ok", register_worker_result); |
| 143 |
| 144 InfoBarResponder accepting_responder(browser(), true); |
| 145 |
| 146 std::string register_push_result; |
| 147 ASSERT_TRUE(RegisterPush(®ister_push_result)); |
| 148 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1", register_push_result); |
| 149 |
| 150 PushMessagingApplicationId expected_id(https_server()->GetURL(""), 0L); |
| 151 EXPECT_EQ(expected_id.ToString(), gcm_service()->last_registered_app_id()); |
| 152 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); |
| 153 } |
| 154 |
| 155 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterFailureNoPermission) { |
| 156 std::string register_worker_result; |
| 157 ASSERT_TRUE(RegisterServiceWorker(®ister_worker_result)); |
| 158 ASSERT_EQ("ok", register_worker_result); |
| 159 |
| 160 InfoBarResponder cancelling_responder(browser(), false); |
| 161 |
| 162 std::string register_push_result; |
| 163 ASSERT_TRUE(RegisterPush(®ister_push_result)); |
| 164 EXPECT_EQ("AbortError - Registration failed - permission denied", |
| 165 register_push_result); |
| 166 } |
| 167 |
| 168 } // namespace gcm |
OLD | NEW |