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

Side by Side Diff: chrome/browser/notifications/platform_notification_service_browsertest.cc

Issue 661643002: Adds a context message of the security origin for web notifications. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adds a bug reference. 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 <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/strings/utf_string_conversions.h"
10 #include "chrome/browser/infobars/infobar_service.h" 11 #include "chrome/browser/infobars/infobar_service.h"
11 #include "chrome/browser/notifications/notification_test_util.h" 12 #include "chrome/browser/notifications/notification_test_util.h"
12 #include "chrome/browser/notifications/platform_notification_service_impl.h" 13 #include "chrome/browser/notifications/platform_notification_service_impl.h"
13 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h" 15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/test/base/in_process_browser_test.h" 16 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chrome/test/base/ui_test_utils.h" 17 #include "chrome/test/base/ui_test_utils.h"
17 #include "components/infobars/core/confirm_infobar_delegate.h" 18 #include "components/infobars/core/confirm_infobar_delegate.h"
18 #include "components/infobars/core/infobar.h" 19 #include "components/infobars/core/infobar.h"
19 #include "components/infobars/core/infobar_manager.h" 20 #include "components/infobars/core/infobar_manager.h"
20 #include "content/public/common/content_switches.h" 21 #include "content/public/common/content_switches.h"
21 #include "content/public/test/browser_test_utils.h" 22 #include "content/public/test/browser_test_utils.h"
23 #include "net/base/filename_util.h"
22 #include "net/test/spawned_test_server/spawned_test_server.h" 24 #include "net/test/spawned_test_server/spawned_test_server.h"
23 25
24 // ----------------------------------------------------------------------------- 26 // -----------------------------------------------------------------------------
25 27
26 // Accept or rejects the first shown confirm infobar. The infobar will be 28 // Accept or rejects the first shown confirm infobar. The infobar will be
27 // responsed to asynchronously, to imitate the behavior of a user. 29 // responsed to asynchronously, to imitate the behavior of a user.
28 // TODO(peter): Generalize this class, as it's commonly useful. 30 // TODO(peter): Generalize this class, as it's commonly useful.
29 class InfoBarResponder : public infobars::InfoBarManager::Observer { 31 class InfoBarResponder : public infobars::InfoBarManager::Observer {
30 public: 32 public:
31 InfoBarResponder(Browser* browser, bool accept); 33 InfoBarResponder(Browser* browser, bool accept);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 if (accept_) 76 if (accept_)
75 delegate->Accept(); 77 delegate->Accept();
76 else 78 else
77 delegate->Cancel(); 79 delegate->Cancel();
78 } 80 }
79 81
80 // ----------------------------------------------------------------------------- 82 // -----------------------------------------------------------------------------
81 83
82 class PlatformNotificationServiceBrowserTest : public InProcessBrowserTest { 84 class PlatformNotificationServiceBrowserTest : public InProcessBrowserTest {
83 public: 85 public:
86 PlatformNotificationServiceBrowserTest();
84 ~PlatformNotificationServiceBrowserTest() override {} 87 ~PlatformNotificationServiceBrowserTest() override {}
85 88
86 // InProcessBrowserTest overrides. 89 // InProcessBrowserTest overrides.
87 void SetUpCommandLine(base::CommandLine* command_line) override; 90 void SetUpCommandLine(base::CommandLine* command_line) override;
88 void SetUp() override; 91 void SetUp() override;
89 void SetUpOnMainThread() override; 92 void SetUpOnMainThread() override;
90 void TearDown() override; 93 void TearDown() override;
91 94
92 protected: 95 protected:
93 // Returns the Platform Notification Service these unit tests are for. 96 // Returns the Platform Notification Service these unit tests are for.
94 PlatformNotificationServiceImpl* service() const { 97 PlatformNotificationServiceImpl* service() const {
95 return PlatformNotificationServiceImpl::GetInstance(); 98 return PlatformNotificationServiceImpl::GetInstance();
96 } 99 }
97 100
98 // Returns the UI Manager on which notifications will be displayed. 101 // Returns the UI Manager on which notifications will be displayed.
99 StubNotificationUIManager* ui_manager() const { return ui_manager_.get(); } 102 StubNotificationUIManager* ui_manager() const { return ui_manager_.get(); }
100 103
104 const base::FilePath& server_root() const { return server_root_; }
105
101 // Navigates the browser to the test page indicated by |path|. 106 // Navigates the browser to the test page indicated by |path|.
102 void NavigateToTestPage(const std::string& path) const; 107 void NavigateToTestPage(const std::string& path) const;
103 108
104 // Executes |script| and stores the result as a string in |result|. A boolean 109 // Executes |script| and stores the result as a string in |result|. A boolean
105 // will be returned, indicating whether the script was executed successfully. 110 // will be returned, indicating whether the script was executed successfully.
106 bool RunScript(const std::string& script, std::string* result) const; 111 bool RunScript(const std::string& script, std::string* result) const;
107 112
113 net::HostPortPair ServerHostPort() const;
114
108 private: 115 private:
116 const base::FilePath server_root_;
109 scoped_ptr<StubNotificationUIManager> ui_manager_; 117 scoped_ptr<StubNotificationUIManager> ui_manager_;
110 scoped_ptr<net::SpawnedTestServer> https_server_; 118 scoped_ptr<net::SpawnedTestServer> https_server_;
111 }; 119 };
112 120
113 // ----------------------------------------------------------------------------- 121 // -----------------------------------------------------------------------------
114 122
115 namespace { 123 namespace {
116 124
117 const char kTestPageUrl[] = 125 const char kTestFileName[] = "notifications/platform_notification_service.html";
118 "files/notifications/platform_notification_service.html"; 126 } // namespace
Peter Beverloo 2015/01/27 13:37:04 micro nit: namespace { const char kTestFileName[]
dewittj 2015/01/27 23:10:12 Done.
119 127
120 } // namespace 128 PlatformNotificationServiceBrowserTest::PlatformNotificationServiceBrowserTest()
129 : server_root_(FILE_PATH_LITERAL("chrome/test/data")) {
130 }
121 131
122 void PlatformNotificationServiceBrowserTest::SetUpCommandLine( 132 void PlatformNotificationServiceBrowserTest::SetUpCommandLine(
123 base::CommandLine* command_line) { 133 base::CommandLine* command_line) {
124 command_line->AppendSwitch( 134 command_line->AppendSwitch(
125 switches::kEnableExperimentalWebPlatformFeatures); 135 switches::kEnableExperimentalWebPlatformFeatures);
126 136
127 InProcessBrowserTest::SetUpCommandLine(command_line); 137 InProcessBrowserTest::SetUpCommandLine(command_line);
128 } 138 }
129 139
130 void PlatformNotificationServiceBrowserTest::SetUp() { 140 void PlatformNotificationServiceBrowserTest::SetUp() {
131 ui_manager_.reset(new StubNotificationUIManager); 141 ui_manager_.reset(new StubNotificationUIManager);
132 https_server_.reset(new net::SpawnedTestServer( 142 https_server_.reset(new net::SpawnedTestServer(
133 net::SpawnedTestServer::TYPE_HTTPS, 143 net::SpawnedTestServer::TYPE_HTTPS,
134 net::BaseTestServer::SSLOptions( 144 net::BaseTestServer::SSLOptions(net::BaseTestServer::SSLOptions::CERT_OK),
135 net::BaseTestServer::SSLOptions::CERT_OK), 145 server_root_));
136 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/"))));
137 ASSERT_TRUE(https_server_->Start()); 146 ASSERT_TRUE(https_server_->Start());
138 147
139 service()->SetNotificationUIManagerForTesting(ui_manager_.get()); 148 service()->SetNotificationUIManagerForTesting(ui_manager_.get());
140 149
141 InProcessBrowserTest::SetUp(); 150 InProcessBrowserTest::SetUp();
142 } 151 }
143 152
144 void PlatformNotificationServiceBrowserTest::SetUpOnMainThread() { 153 void PlatformNotificationServiceBrowserTest::SetUpOnMainThread() {
145 NavigateToTestPage(kTestPageUrl); 154 // The test server has a base directory that doesn't exist in the
155 // filesystem.
156 std::string test_page_url = std::string("files/") + kTestFileName;
157
158 NavigateToTestPage(test_page_url);
146 159
147 InProcessBrowserTest::SetUpOnMainThread(); 160 InProcessBrowserTest::SetUpOnMainThread();
148 } 161 }
149 162
150 void PlatformNotificationServiceBrowserTest::TearDown() { 163 void PlatformNotificationServiceBrowserTest::TearDown() {
151 service()->SetNotificationUIManagerForTesting(nullptr); 164 service()->SetNotificationUIManagerForTesting(nullptr);
152 } 165 }
153 166
154 void PlatformNotificationServiceBrowserTest::NavigateToTestPage( 167 void PlatformNotificationServiceBrowserTest::NavigateToTestPage(
155 const std::string& path) const { 168 const std::string& path) const {
156 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(path)); 169 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(path));
157 } 170 }
158 171
159 bool PlatformNotificationServiceBrowserTest::RunScript( 172 bool PlatformNotificationServiceBrowserTest::RunScript(
160 const std::string& script, std::string* result) const { 173 const std::string& script, std::string* result) const {
161 return content::ExecuteScriptAndExtractString( 174 return content::ExecuteScriptAndExtractString(
162 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), 175 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
163 script, 176 script,
164 result); 177 result);
165 } 178 }
166 179
180 net::HostPortPair PlatformNotificationServiceBrowserTest::ServerHostPort()
181 const {
182 return https_server_->host_port_pair();
183 }
184
167 // ----------------------------------------------------------------------------- 185 // -----------------------------------------------------------------------------
168 186
169 // TODO(peter): Move PlatformNotificationService-related tests over from 187 // TODO(peter): Move PlatformNotificationService-related tests over from
170 // notification_browsertest.cc to this file. 188 // notification_browsertest.cc to this file.
171 189
172 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, 190 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
173 DisplayPersistentNotificationWithoutPermission) { 191 DisplayPersistentNotificationWithoutPermission) {
174 std::string script_result; 192 std::string script_result;
175 193
176 InfoBarResponder accepting_responder(browser(), false); 194 InfoBarResponder accepting_responder(browser(), false);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); 241 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
224 242
225 const Notification& notification = ui_manager()->GetNotificationAt(0); 243 const Notification& notification = ui_manager()->GetNotificationAt(0);
226 notification.delegate()->Click(); 244 notification.delegate()->Click();
227 245
228 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result)); 246 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result));
229 EXPECT_EQ("action_close", script_result); 247 EXPECT_EQ("action_close", script_result);
230 248
231 ASSERT_EQ(0u, ui_manager()->GetNotificationCount()); 249 ASSERT_EQ(0u, ui_manager()->GetNotificationCount());
232 } 250 }
251
252 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
253 TestDisplayOriginContextMessage) {
254 std::string script_result;
255
256 // Creates a simple notification.
257 InfoBarResponder accepting_responder(browser(), true);
258 ASSERT_TRUE(RunScript("RequestPermission()", &script_result));
259 ASSERT_EQ("granted", script_result);
260 ASSERT_TRUE(RunScript("DisplayPersistentNotification()", &script_result));
261
262 net::HostPortPair host_port = ServerHostPort();
263
264 const Notification& notification = ui_manager()->GetNotificationAt(0);
265
266 EXPECT_EQ(base::UTF8ToUTF16(host_port.ToString()),
267 notification.context_message());
268 }
269
270 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
271 CheckFilePermissionNotGranted) {
272 base::FilePath full_file_path =
273 server_root().Append(base::FilePath::FromUTF8Unsafe(kTestFileName));
274 GURL file_url(net::FilePathToFileURL(full_file_path));
Peter Beverloo 2015/01/27 13:37:04 QQ: If server_root_ is "chrome/test/data" (which i
dewittj 2015/01/27 23:10:12 Right, looks like the try bots failed on that. It
275 ui_test_utils::NavigateToURL(browser(), file_url);
276
277 std::string script_result;
278
279 InfoBarResponder accepting_responder(browser(), true);
280
281 // TODO(dewittj): Update the
282 // |PlatformNotificationServiceImpl::WebOriginDisplayName| function to
283 // properly display file:// URL origins. See crbug.com/402191.
284 ASSERT_TRUE(RunScript("RequestPermission()", &script_result));
285 EXPECT_EQ("default", script_result)
286 << "If this test fails, you may have fixed a bug preventing file origins "
287 << "from sending their origin from Blink; if so you need to update the "
288 << "display function for notification origins to show the file path.";
289 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698