| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/path_service.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/infobars/infobar_service.h" | 12 #include "chrome/browser/infobars/infobar_service.h" |
| 13 #include "chrome/browser/notifications/desktop_notification_service.h" |
| 14 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
| 11 #include "chrome/browser/notifications/notification_test_util.h" | 15 #include "chrome/browser/notifications/notification_test_util.h" |
| 12 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 16 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 13 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "chrome/test/base/in_process_browser_test.h" | 19 #include "chrome/test/base/in_process_browser_test.h" |
| 16 #include "chrome/test/base/ui_test_utils.h" | 20 #include "chrome/test/base/ui_test_utils.h" |
| 17 #include "components/infobars/core/confirm_infobar_delegate.h" | 21 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 18 #include "components/infobars/core/infobar.h" | 22 #include "components/infobars/core/infobar.h" |
| 19 #include "components/infobars/core/infobar_manager.h" | 23 #include "components/infobars/core/infobar_manager.h" |
| 20 #include "content/public/common/content_switches.h" | 24 #include "content/public/common/content_switches.h" |
| 21 #include "content/public/test/browser_test_utils.h" | 25 #include "content/public/test/browser_test_utils.h" |
| 26 #include "net/base/filename_util.h" |
| 22 #include "net/test/spawned_test_server/spawned_test_server.h" | 27 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 23 | 28 |
| 24 // ----------------------------------------------------------------------------- | 29 // ----------------------------------------------------------------------------- |
| 25 | 30 |
| 26 // Accept or rejects the first shown confirm infobar. The infobar will be | 31 // Accept or rejects the first shown confirm infobar. The infobar will be |
| 27 // responsed to asynchronously, to imitate the behavior of a user. | 32 // responsed to asynchronously, to imitate the behavior of a user. |
| 28 // TODO(peter): Generalize this class, as it's commonly useful. | 33 // TODO(peter): Generalize this class, as it's commonly useful. |
| 29 class InfoBarResponder : public infobars::InfoBarManager::Observer { | 34 class InfoBarResponder : public infobars::InfoBarManager::Observer { |
| 30 public: | 35 public: |
| 31 InfoBarResponder(Browser* browser, bool accept); | 36 InfoBarResponder(Browser* browser, bool accept); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 if (accept_) | 79 if (accept_) |
| 75 delegate->Accept(); | 80 delegate->Accept(); |
| 76 else | 81 else |
| 77 delegate->Cancel(); | 82 delegate->Cancel(); |
| 78 } | 83 } |
| 79 | 84 |
| 80 // ----------------------------------------------------------------------------- | 85 // ----------------------------------------------------------------------------- |
| 81 | 86 |
| 82 class PlatformNotificationServiceBrowserTest : public InProcessBrowserTest { | 87 class PlatformNotificationServiceBrowserTest : public InProcessBrowserTest { |
| 83 public: | 88 public: |
| 89 PlatformNotificationServiceBrowserTest(); |
| 84 ~PlatformNotificationServiceBrowserTest() override {} | 90 ~PlatformNotificationServiceBrowserTest() override {} |
| 85 | 91 |
| 86 // InProcessBrowserTest overrides. | 92 // InProcessBrowserTest overrides. |
| 87 void SetUpCommandLine(base::CommandLine* command_line) override; | 93 void SetUpCommandLine(base::CommandLine* command_line) override; |
| 88 void SetUp() override; | 94 void SetUp() override; |
| 89 void SetUpOnMainThread() override; | 95 void SetUpOnMainThread() override; |
| 90 void TearDown() override; | 96 void TearDown() override; |
| 91 | 97 |
| 92 protected: | 98 protected: |
| 93 // Returns the Platform Notification Service these unit tests are for. | 99 // Returns the Platform Notification Service these unit tests are for. |
| 94 PlatformNotificationServiceImpl* service() const { | 100 PlatformNotificationServiceImpl* service() const { |
| 95 return PlatformNotificationServiceImpl::GetInstance(); | 101 return PlatformNotificationServiceImpl::GetInstance(); |
| 96 } | 102 } |
| 97 | 103 |
| 98 // Returns the UI Manager on which notifications will be displayed. | 104 // Returns the UI Manager on which notifications will be displayed. |
| 99 StubNotificationUIManager* ui_manager() const { return ui_manager_.get(); } | 105 StubNotificationUIManager* ui_manager() const { return ui_manager_.get(); } |
| 100 | 106 |
| 107 const base::FilePath& server_root() const { return server_root_; } |
| 108 |
| 101 // Navigates the browser to the test page indicated by |path|. | 109 // Navigates the browser to the test page indicated by |path|. |
| 102 void NavigateToTestPage(const std::string& path) const; | 110 void NavigateToTestPage(const std::string& path) const; |
| 103 | 111 |
| 104 // Executes |script| and stores the result as a string in |result|. A boolean | 112 // Executes |script| and stores the result as a string in |result|. A boolean |
| 105 // will be returned, indicating whether the script was executed successfully. | 113 // will be returned, indicating whether the script was executed successfully. |
| 106 bool RunScript(const std::string& script, std::string* result) const; | 114 bool RunScript(const std::string& script, std::string* result) const; |
| 107 | 115 |
| 116 net::HostPortPair ServerHostPort() const; |
| 117 GURL TestPageUrl() const; |
| 118 |
| 108 private: | 119 private: |
| 120 const base::FilePath server_root_; |
| 121 const std::string test_page_url_; |
| 109 scoped_ptr<StubNotificationUIManager> ui_manager_; | 122 scoped_ptr<StubNotificationUIManager> ui_manager_; |
| 110 scoped_ptr<net::SpawnedTestServer> https_server_; | 123 scoped_ptr<net::SpawnedTestServer> https_server_; |
| 111 }; | 124 }; |
| 112 | 125 |
| 113 // ----------------------------------------------------------------------------- | 126 // ----------------------------------------------------------------------------- |
| 114 | 127 |
| 115 namespace { | 128 namespace { |
| 129 const char kTestFileName[] = "notifications/platform_notification_service.html"; |
| 130 } |
| 116 | 131 |
| 117 const char kTestPageUrl[] = | 132 PlatformNotificationServiceBrowserTest::PlatformNotificationServiceBrowserTest() |
| 118 "files/notifications/platform_notification_service.html"; | 133 : server_root_(FILE_PATH_LITERAL("chrome/test/data")), |
| 119 | 134 // The test server has a base directory that doesn't exist in the |
| 120 } // namespace | 135 // filesystem. |
| 136 test_page_url_(std::string("files/") + kTestFileName) { |
| 137 } |
| 121 | 138 |
| 122 void PlatformNotificationServiceBrowserTest::SetUpCommandLine( | 139 void PlatformNotificationServiceBrowserTest::SetUpCommandLine( |
| 123 base::CommandLine* command_line) { | 140 base::CommandLine* command_line) { |
| 124 command_line->AppendSwitch( | 141 command_line->AppendSwitch( |
| 125 switches::kEnableExperimentalWebPlatformFeatures); | 142 switches::kEnableExperimentalWebPlatformFeatures); |
| 126 | 143 |
| 127 InProcessBrowserTest::SetUpCommandLine(command_line); | 144 InProcessBrowserTest::SetUpCommandLine(command_line); |
| 128 } | 145 } |
| 129 | 146 |
| 130 void PlatformNotificationServiceBrowserTest::SetUp() { | 147 void PlatformNotificationServiceBrowserTest::SetUp() { |
| 131 ui_manager_.reset(new StubNotificationUIManager); | 148 ui_manager_.reset(new StubNotificationUIManager); |
| 132 https_server_.reset(new net::SpawnedTestServer( | 149 https_server_.reset(new net::SpawnedTestServer( |
| 133 net::SpawnedTestServer::TYPE_HTTPS, | 150 net::SpawnedTestServer::TYPE_HTTPS, |
| 134 net::BaseTestServer::SSLOptions( | 151 net::BaseTestServer::SSLOptions(net::BaseTestServer::SSLOptions::CERT_OK), |
| 135 net::BaseTestServer::SSLOptions::CERT_OK), | 152 server_root_)); |
| 136 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/")))); | |
| 137 ASSERT_TRUE(https_server_->Start()); | 153 ASSERT_TRUE(https_server_->Start()); |
| 138 | 154 |
| 139 service()->SetNotificationUIManagerForTesting(ui_manager_.get()); | 155 service()->SetNotificationUIManagerForTesting(ui_manager_.get()); |
| 140 | 156 |
| 141 InProcessBrowserTest::SetUp(); | 157 InProcessBrowserTest::SetUp(); |
| 142 } | 158 } |
| 143 | 159 |
| 144 void PlatformNotificationServiceBrowserTest::SetUpOnMainThread() { | 160 void PlatformNotificationServiceBrowserTest::SetUpOnMainThread() { |
| 145 NavigateToTestPage(kTestPageUrl); | 161 NavigateToTestPage(test_page_url_); |
| 146 | 162 |
| 147 InProcessBrowserTest::SetUpOnMainThread(); | 163 InProcessBrowserTest::SetUpOnMainThread(); |
| 148 } | 164 } |
| 149 | 165 |
| 150 void PlatformNotificationServiceBrowserTest::TearDown() { | 166 void PlatformNotificationServiceBrowserTest::TearDown() { |
| 151 service()->SetNotificationUIManagerForTesting(nullptr); | 167 service()->SetNotificationUIManagerForTesting(nullptr); |
| 152 } | 168 } |
| 153 | 169 |
| 154 void PlatformNotificationServiceBrowserTest::NavigateToTestPage( | 170 void PlatformNotificationServiceBrowserTest::NavigateToTestPage( |
| 155 const std::string& path) const { | 171 const std::string& path) const { |
| 156 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(path)); | 172 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(path)); |
| 157 } | 173 } |
| 158 | 174 |
| 159 bool PlatformNotificationServiceBrowserTest::RunScript( | 175 bool PlatformNotificationServiceBrowserTest::RunScript( |
| 160 const std::string& script, std::string* result) const { | 176 const std::string& script, std::string* result) const { |
| 161 return content::ExecuteScriptAndExtractString( | 177 return content::ExecuteScriptAndExtractString( |
| 162 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), | 178 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), |
| 163 script, | 179 script, |
| 164 result); | 180 result); |
| 165 } | 181 } |
| 166 | 182 |
| 183 net::HostPortPair PlatformNotificationServiceBrowserTest::ServerHostPort() |
| 184 const { |
| 185 return https_server_->host_port_pair(); |
| 186 } |
| 187 |
| 188 GURL PlatformNotificationServiceBrowserTest::TestPageUrl() const { |
| 189 return https_server_->GetURL(test_page_url_); |
| 190 } |
| 191 |
| 167 // ----------------------------------------------------------------------------- | 192 // ----------------------------------------------------------------------------- |
| 168 | 193 |
| 169 // TODO(peter): Move PlatformNotificationService-related tests over from | 194 // TODO(peter): Move PlatformNotificationService-related tests over from |
| 170 // notification_browsertest.cc to this file. | 195 // notification_browsertest.cc to this file. |
| 171 | 196 |
| 172 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, | 197 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, |
| 173 DisplayPersistentNotificationWithoutPermission) { | 198 DisplayPersistentNotificationWithoutPermission) { |
| 174 std::string script_result; | 199 std::string script_result; |
| 175 | 200 |
| 176 InfoBarResponder accepting_responder(browser(), false); | 201 InfoBarResponder accepting_responder(browser(), false); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); | 248 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); |
| 224 | 249 |
| 225 const Notification& notification = ui_manager()->GetNotificationAt(0); | 250 const Notification& notification = ui_manager()->GetNotificationAt(0); |
| 226 notification.delegate()->Click(); | 251 notification.delegate()->Click(); |
| 227 | 252 |
| 228 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result)); | 253 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result)); |
| 229 EXPECT_EQ("action_close", script_result); | 254 EXPECT_EQ("action_close", script_result); |
| 230 | 255 |
| 231 ASSERT_EQ(0u, ui_manager()->GetNotificationCount()); | 256 ASSERT_EQ(0u, ui_manager()->GetNotificationCount()); |
| 232 } | 257 } |
| 258 |
| 259 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, |
| 260 TestDisplayOriginContextMessage) { |
| 261 std::string script_result; |
| 262 |
| 263 // Creates a simple notification. |
| 264 InfoBarResponder accepting_responder(browser(), true); |
| 265 ASSERT_TRUE(RunScript("RequestPermission()", &script_result)); |
| 266 ASSERT_EQ("granted", script_result); |
| 267 ASSERT_TRUE(RunScript("DisplayPersistentNotification()", &script_result)); |
| 268 |
| 269 net::HostPortPair host_port = ServerHostPort(); |
| 270 |
| 271 const Notification& notification = ui_manager()->GetNotificationAt(0); |
| 272 |
| 273 EXPECT_EQ(base::UTF8ToUTF16(host_port.ToString()), |
| 274 notification.context_message()); |
| 275 } |
| 276 |
| 277 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, |
| 278 CheckFilePermissionNotGranted) { |
| 279 // TODO(dewittj): This test verifies that a bug exists in Chrome; the test |
| 280 // will fail if the bug is fixed. The |
| 281 // |PlatformNotificationServiceImpl::WebOriginDisplayName| function needs |
| 282 // to be updated to properly display file:// URL origins. |
| 283 // See crbug.com/402191. |
| 284 std::string script_result; |
| 285 |
| 286 InfoBarResponder accepting_responder_web(browser(), true); |
| 287 |
| 288 DesktopNotificationService* notification_service = |
| 289 DesktopNotificationServiceFactory::GetForProfile(browser()->profile()); |
| 290 ASSERT_TRUE(notification_service); |
| 291 message_center::NotifierId web_notifier(TestPageUrl()); |
| 292 EXPECT_FALSE(notification_service->IsNotifierEnabled(web_notifier)); |
| 293 ASSERT_TRUE(RunScript("RequestPermission()", &script_result)); |
| 294 EXPECT_EQ("granted", script_result); |
| 295 |
| 296 EXPECT_TRUE(notification_service->IsNotifierEnabled(web_notifier)); |
| 297 |
| 298 base::FilePath dir_source_root; |
| 299 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &dir_source_root)); |
| 300 base::FilePath full_file_path = |
| 301 dir_source_root.Append(server_root()).AppendASCII(kTestFileName); |
| 302 GURL file_url(net::FilePathToFileURL(full_file_path)); |
| 303 ui_test_utils::NavigateToURL(browser(), file_url); |
| 304 |
| 305 message_center::NotifierId file_notifier(file_url); |
| 306 EXPECT_FALSE(notification_service->IsNotifierEnabled(file_notifier)); |
| 307 |
| 308 InfoBarResponder accepting_responder_file(browser(), true); |
| 309 ASSERT_TRUE(RunScript("RequestPermission()", &script_result)); |
| 310 EXPECT_EQ("granted", script_result); |
| 311 |
| 312 EXPECT_FALSE(notification_service->IsNotifierEnabled(file_notifier)) |
| 313 << "If this test fails, you may have fixed a bug preventing file origins " |
| 314 << "from sending their origin from Blink; if so you need to update the " |
| 315 << "display function for notification origins to show the file path."; |
| 316 } |
| OLD | NEW |