| OLD | NEW |
| 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 <string> | 6 #include <string> |
| 6 | 7 |
| 7 #include "base/bind.h" | 8 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 10 #include "chrome/browser/infobars/infobar_service.h" | 11 #include "chrome/browser/infobars/infobar_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" | 13 #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/gcm_profile_service_factory.h" |
| 14 #include "chrome/browser/services/gcm/push_messaging_application_id.h" | 15 #include "chrome/browser/services/gcm/push_messaging_application_id.h" |
| 15 #include "chrome/browser/services/gcm/push_messaging_constants.h" | 16 #include "chrome/browser/services/gcm/push_messaging_constants.h" |
| 16 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 18 #include "chrome/test/base/in_process_browser_test.h" | 19 #include "chrome/test/base/in_process_browser_test.h" |
| 19 #include "chrome/test/base/ui_test_utils.h" | 20 #include "chrome/test/base/ui_test_utils.h" |
| 21 #include "components/gcm_driver/gcm_client.h" |
| 20 #include "components/infobars/core/confirm_infobar_delegate.h" | 22 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 21 #include "components/infobars/core/infobar.h" | 23 #include "components/infobars/core/infobar.h" |
| 22 #include "components/infobars/core/infobar_manager.h" | 24 #include "components/infobars/core/infobar_manager.h" |
| 23 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 24 #include "content/public/common/content_switches.h" | 26 #include "content/public/common/content_switches.h" |
| 25 #include "content/public/test/browser_test_utils.h" | 27 #include "content/public/test/browser_test_utils.h" |
| 26 | 28 |
| 27 namespace gcm { | 29 namespace gcm { |
| 28 | 30 |
| 29 namespace { | 31 namespace { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 InProcessBrowserTest::SetUp(); | 99 InProcessBrowserTest::SetUp(); |
| 98 } | 100 } |
| 99 | 101 |
| 100 // InProcessBrowserTest: | 102 // InProcessBrowserTest: |
| 101 virtual void SetUpOnMainThread() override { | 103 virtual void SetUpOnMainThread() override { |
| 102 gcm_service_ = static_cast<FakeGCMProfileService*>( | 104 gcm_service_ = static_cast<FakeGCMProfileService*>( |
| 103 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 105 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 104 browser()->profile(), &FakeGCMProfileService::Build)); | 106 browser()->profile(), &FakeGCMProfileService::Build)); |
| 105 gcm_service_->set_collect(true); | 107 gcm_service_->set_collect(true); |
| 106 | 108 |
| 109 loadTestPage(); |
| 110 |
| 111 InProcessBrowserTest::SetUpOnMainThread(); |
| 112 } |
| 113 |
| 114 void loadTestPage() { |
| 107 ui_test_utils::NavigateToURL( | 115 ui_test_utils::NavigateToURL( |
| 108 browser(), https_server_->GetURL("files/push_messaging/test.html")); | 116 browser(), https_server_->GetURL("files/push_messaging/test.html")); |
| 109 | |
| 110 InProcessBrowserTest::SetUpOnMainThread(); | |
| 111 } | 117 } |
| 112 | 118 |
| 113 bool RunScript(const std::string& script, std::string* result) { | 119 bool RunScript(const std::string& script, std::string* result) { |
| 114 return content::ExecuteScriptAndExtractString( | 120 return content::ExecuteScriptAndExtractString( |
| 115 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), | 121 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), |
| 116 script, | 122 script, |
| 117 result); | 123 result); |
| 118 } | 124 } |
| 119 | 125 |
| 120 bool RegisterServiceWorker(std::string* result) { | |
| 121 return RunScript("registerServiceWorker()", result); | |
| 122 } | |
| 123 | |
| 124 bool RegisterPush(std::string* result) { | |
| 125 return RunScript("registerPush()", result); | |
| 126 } | |
| 127 | |
| 128 net::SpawnedTestServer* https_server() const { return https_server_.get(); } | 126 net::SpawnedTestServer* https_server() const { return https_server_.get(); } |
| 129 | 127 |
| 130 FakeGCMProfileService* gcm_service() const { return gcm_service_; } | 128 FakeGCMProfileService* gcm_service() const { return gcm_service_; } |
| 131 | 129 |
| 130 PushMessagingServiceImpl* push_service() { |
| 131 return static_cast<PushMessagingServiceImpl*>( |
| 132 gcm_service_->push_messaging_service()); |
| 133 } |
| 134 |
| 132 private: | 135 private: |
| 133 scoped_ptr<net::SpawnedTestServer> https_server_; | 136 scoped_ptr<net::SpawnedTestServer> https_server_; |
| 134 FakeGCMProfileService* gcm_service_; | 137 FakeGCMProfileService* gcm_service_; |
| 135 | 138 |
| 136 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); | 139 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); |
| 137 }; | 140 }; |
| 138 | 141 |
| 139 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterSuccess) { | 142 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterSuccess) { |
| 140 std::string register_worker_result; | 143 std::string script_result; |
| 141 ASSERT_TRUE(RegisterServiceWorker(®ister_worker_result)); | 144 |
| 142 ASSERT_EQ("ok", register_worker_result); | 145 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 146 ASSERT_EQ("ok - service worker registered", script_result); |
| 143 | 147 |
| 144 InfoBarResponder accepting_responder(browser(), true); | 148 InfoBarResponder accepting_responder(browser(), true); |
| 145 | 149 |
| 146 std::string register_push_result; | 150 ASSERT_TRUE(RunScript("registerPush()", &script_result)); |
| 147 ASSERT_TRUE(RegisterPush(®ister_push_result)); | 151 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1", script_result); |
| 148 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1", register_push_result); | |
| 149 | 152 |
| 150 PushMessagingApplicationId expected_id(https_server()->GetURL(""), 0L); | 153 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L); |
| 151 EXPECT_EQ(expected_id.ToString(), gcm_service()->last_registered_app_id()); | 154 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id()); |
| 152 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); | 155 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); |
| 153 } | 156 } |
| 154 | 157 |
| 155 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterFailureNoPermission) { | 158 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterFailureNoPermission) { |
| 156 std::string register_worker_result; | 159 std::string script_result; |
| 157 ASSERT_TRUE(RegisterServiceWorker(®ister_worker_result)); | 160 |
| 158 ASSERT_EQ("ok", register_worker_result); | 161 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 162 ASSERT_EQ("ok - service worker registered", script_result); |
| 159 | 163 |
| 160 InfoBarResponder cancelling_responder(browser(), false); | 164 InfoBarResponder cancelling_responder(browser(), false); |
| 161 | 165 |
| 162 std::string register_push_result; | 166 ASSERT_TRUE(RunScript("registerPush()", &script_result)); |
| 163 ASSERT_TRUE(RegisterPush(®ister_push_result)); | |
| 164 EXPECT_EQ("AbortError - Registration failed - permission denied", | 167 EXPECT_EQ("AbortError - Registration failed - permission denied", |
| 165 register_push_result); | 168 script_result); |
| 169 } |
| 170 |
| 171 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventSuccess) { |
| 172 std::string script_result; |
| 173 |
| 174 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 175 ASSERT_EQ("ok - service worker registered", script_result); |
| 176 |
| 177 InfoBarResponder accepting_responder(browser(), true); |
| 178 |
| 179 ASSERT_TRUE(RunScript("registerPush()", &script_result)); |
| 180 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1", script_result); |
| 181 |
| 182 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L); |
| 183 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id()); |
| 184 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); |
| 185 |
| 186 ASSERT_TRUE(RunScript("isControlled()", &script_result)); |
| 187 ASSERT_EQ("false - is not controlled", script_result); |
| 188 |
| 189 loadTestPage(); // Reload to become controlled. |
| 190 |
| 191 ASSERT_TRUE(RunScript("isControlled()", &script_result)); |
| 192 ASSERT_EQ("true - is controlled", script_result); |
| 193 |
| 194 GCMClient::IncomingMessage message; |
| 195 GCMClient::MessageData messageData; |
| 196 messageData.insert(std::pair<std::string, std::string>("data", "testdata")); |
| 197 message.data = messageData; |
| 198 push_service()->OnMessage(app_id.ToString(), message); |
| 199 ASSERT_TRUE(RunScript("pushData.get()", &script_result)); |
| 200 EXPECT_EQ("testdata", script_result); |
| 166 } | 201 } |
| 167 | 202 |
| 168 } // namespace gcm | 203 } // namespace gcm |
| OLD | NEW |