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

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

Issue 1100953002: Move InfoBarResponder into its own file. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comment. Created 5 years, 8 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/path_service.h" 10 #include "base/path_service.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/infobars/infobar_responder.h"
12 #include "chrome/browser/infobars/infobar_service.h" 13 #include "chrome/browser/infobars/infobar_service.h"
13 #include "chrome/browser/notifications/desktop_notification_profile_util.h" 14 #include "chrome/browser/notifications/desktop_notification_profile_util.h"
14 #include "chrome/browser/notifications/desktop_notification_service.h" 15 #include "chrome/browser/notifications/desktop_notification_service.h"
15 #include "chrome/browser/notifications/desktop_notification_service_factory.h" 16 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
16 #include "chrome/browser/notifications/notification_test_util.h" 17 #include "chrome/browser/notifications/notification_test_util.h"
17 #include "chrome/browser/notifications/platform_notification_service_impl.h" 18 #include "chrome/browser/notifications/platform_notification_service_impl.h"
18 #include "chrome/browser/ui/browser.h" 19 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h" 20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/test/base/in_process_browser_test.h" 21 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/ui_test_utils.h" 22 #include "chrome/test/base/ui_test_utils.h"
22 #include "components/infobars/core/confirm_infobar_delegate.h"
23 #include "components/infobars/core/infobar.h"
24 #include "components/infobars/core/infobar_manager.h"
25 #include "content/public/test/browser_test_utils.h" 23 #include "content/public/test/browser_test_utils.h"
26 #include "net/base/filename_util.h" 24 #include "net/base/filename_util.h"
27 #include "net/test/spawned_test_server/spawned_test_server.h" 25 #include "net/test/spawned_test_server/spawned_test_server.h"
28 26
29 // ----------------------------------------------------------------------------- 27 // -----------------------------------------------------------------------------
30 28
31 // Accept or rejects the first shown confirm infobar. The infobar will be
32 // responsed to asynchronously, to imitate the behavior of a user.
33 // TODO(peter): Generalize this class, as it's commonly useful.
34 class InfoBarResponder : public infobars::InfoBarManager::Observer {
35 public:
36 InfoBarResponder(Browser* browser, bool accept);
37 ~InfoBarResponder() override;
38
39 // infobars::InfoBarManager::Observer overrides.
40 void OnInfoBarAdded(infobars::InfoBar* infobar) override;
41
42 private:
43 void Respond(ConfirmInfoBarDelegate* delegate);
44
45 InfoBarService* infobar_service_;
46 bool accept_;
47 bool has_observed_;
48 };
49
50 InfoBarResponder::InfoBarResponder(Browser* browser, bool accept)
51 : infobar_service_(InfoBarService::FromWebContents(
52 browser->tab_strip_model()->GetActiveWebContents())),
53 accept_(accept),
54 has_observed_(false) {
55 infobar_service_->AddObserver(this);
56 }
57
58 InfoBarResponder::~InfoBarResponder() {
59 infobar_service_->RemoveObserver(this);
60 }
61
62 void InfoBarResponder::OnInfoBarAdded(infobars::InfoBar* infobar) {
63 if (has_observed_)
64 return;
65
66 has_observed_ = true;
67 ConfirmInfoBarDelegate* delegate =
68 infobar->delegate()->AsConfirmInfoBarDelegate();
69 DCHECK(delegate);
70
71 // Respond to the infobar asynchronously, like a person.
72 base::MessageLoop::current()->PostTask(
73 FROM_HERE,
74 base::Bind(
75 &InfoBarResponder::Respond, base::Unretained(this), delegate));
76 }
77
78 void InfoBarResponder::Respond(ConfirmInfoBarDelegate* delegate) {
79 if (accept_)
80 delegate->Accept();
81 else
82 delegate->Cancel();
83 }
84
85 // -----------------------------------------------------------------------------
86
87 // Dimensions of the icon.png resource in the notification test data directory. 29 // Dimensions of the icon.png resource in the notification test data directory.
88 const int kIconWidth = 100; 30 const int kIconWidth = 100;
89 const int kIconHeight = 100; 31 const int kIconHeight = 100;
90 32
91 class PlatformNotificationServiceBrowserTest : public InProcessBrowserTest { 33 class PlatformNotificationServiceBrowserTest : public InProcessBrowserTest {
92 public: 34 public:
93 PlatformNotificationServiceBrowserTest(); 35 PlatformNotificationServiceBrowserTest();
94 ~PlatformNotificationServiceBrowserTest() override {} 36 ~PlatformNotificationServiceBrowserTest() override {}
95 37
96 // InProcessBrowserTest overrides. 38 // InProcessBrowserTest overrides.
(...skipping 18 matching lines...) Expand all
115 57
116 // Navigates the browser to the test page indicated by |path|. 58 // Navigates the browser to the test page indicated by |path|.
117 void NavigateToTestPage(const std::string& path) const; 59 void NavigateToTestPage(const std::string& path) const;
118 60
119 // Executes |script| and stores the result as a string in |result|. A boolean 61 // Executes |script| and stores the result as a string in |result|. A boolean
120 // will be returned, indicating whether the script was executed successfully. 62 // will be returned, indicating whether the script was executed successfully.
121 bool RunScript(const std::string& script, std::string* result) const; 63 bool RunScript(const std::string& script, std::string* result) const;
122 64
123 net::HostPortPair ServerHostPort() const; 65 net::HostPortPair ServerHostPort() const;
124 GURL TestPageUrl() const; 66 GURL TestPageUrl() const;
67 InfoBarService* GetInfoBarService();
125 68
126 private: 69 private:
127 const base::FilePath server_root_; 70 const base::FilePath server_root_;
128 const std::string test_page_url_; 71 const std::string test_page_url_;
129 scoped_ptr<StubNotificationUIManager> ui_manager_; 72 scoped_ptr<StubNotificationUIManager> ui_manager_;
130 scoped_ptr<net::SpawnedTestServer> https_server_; 73 scoped_ptr<net::SpawnedTestServer> https_server_;
131 }; 74 };
132 75
133 // ----------------------------------------------------------------------------- 76 // -----------------------------------------------------------------------------
134 77
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 134
192 net::HostPortPair PlatformNotificationServiceBrowserTest::ServerHostPort() 135 net::HostPortPair PlatformNotificationServiceBrowserTest::ServerHostPort()
193 const { 136 const {
194 return https_server_->host_port_pair(); 137 return https_server_->host_port_pair();
195 } 138 }
196 139
197 GURL PlatformNotificationServiceBrowserTest::TestPageUrl() const { 140 GURL PlatformNotificationServiceBrowserTest::TestPageUrl() const {
198 return https_server_->GetURL(test_page_url_); 141 return https_server_->GetURL(test_page_url_);
199 } 142 }
200 143
144 InfoBarService* PlatformNotificationServiceBrowserTest::GetInfoBarService() {
145 return InfoBarService::FromWebContents(
146 browser()->tab_strip_model()->GetActiveWebContents());
147 }
148
201 // ----------------------------------------------------------------------------- 149 // -----------------------------------------------------------------------------
202 150
203 // TODO(peter): Move PlatformNotificationService-related tests over from 151 // TODO(peter): Move PlatformNotificationService-related tests over from
204 // notification_browsertest.cc to this file. 152 // notification_browsertest.cc to this file.
205 153
206 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, 154 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
207 DisplayPersistentNotificationWithoutPermission) { 155 DisplayPersistentNotificationWithoutPermission) {
208 std::string script_result; 156 std::string script_result;
209 157
210 InfoBarResponder accepting_responder(browser(), false); 158 InfoBarResponder cancelling_responder(GetInfoBarService(), false);
211 ASSERT_TRUE(RunScript("RequestPermission()", &script_result)); 159 ASSERT_TRUE(RunScript("RequestPermission()", &script_result));
212 EXPECT_EQ("denied", script_result); 160 EXPECT_EQ("denied", script_result);
213 161
214 ASSERT_TRUE(RunScript("DisplayPersistentNotification()", &script_result)); 162 ASSERT_TRUE(RunScript("DisplayPersistentNotification()", &script_result));
215 EXPECT_EQ( 163 EXPECT_EQ(
216 "TypeError: No notification permission has been granted for this origin.", 164 "TypeError: No notification permission has been granted for this origin.",
217 script_result); 165 script_result);
218 166
219 ASSERT_EQ(0u, ui_manager()->GetNotificationCount()); 167 ASSERT_EQ(0u, ui_manager()->GetNotificationCount());
220 } 168 }
221 169
222 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, 170 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
223 DisplayPersistentNotificationWithPermission) { 171 DisplayPersistentNotificationWithPermission) {
224 std::string script_result; 172 std::string script_result;
225 173
226 InfoBarResponder accepting_responder(browser(), true); 174 InfoBarResponder accepting_responder(GetInfoBarService(), true);
227 ASSERT_TRUE(RunScript("RequestPermission()", &script_result)); 175 ASSERT_TRUE(RunScript("RequestPermission()", &script_result));
228 EXPECT_EQ("granted", script_result); 176 EXPECT_EQ("granted", script_result);
229 177
230 ASSERT_TRUE(RunScript("DisplayPersistentNotification('action_none')", 178 ASSERT_TRUE(RunScript("DisplayPersistentNotification('action_none')",
231 &script_result)); 179 &script_result));
232 EXPECT_EQ("ok", script_result); 180 EXPECT_EQ("ok", script_result);
233 181
234 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); 182 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
235 183
236 const Notification& notification = ui_manager()->GetNotificationAt(0); 184 const Notification& notification = ui_manager()->GetNotificationAt(0);
237 notification.delegate()->Click(); 185 notification.delegate()->Click();
238 186
239 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result)); 187 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result));
240 EXPECT_EQ("action_none", script_result); 188 EXPECT_EQ("action_none", script_result);
241 189
242 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); 190 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
243 } 191 }
244 192
245 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, 193 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
246 WebNotificationOptionsReflection) { 194 WebNotificationOptionsReflection) {
247 std::string script_result; 195 std::string script_result;
248 196
249 // TODO(peter): It doesn't add much value if we use the InfoBarResponder for 197 // TODO(peter): It doesn't add much value if we use the InfoBarResponder for
250 // each test. Rather, we should just toggle the content setting. 198 // each test. Rather, we should just toggle the content setting.
251 InfoBarResponder accepting_responder(browser(), true); 199 InfoBarResponder accepting_responder(GetInfoBarService(), true);
252 ASSERT_TRUE(RunScript("RequestPermission()", &script_result)); 200 ASSERT_TRUE(RunScript("RequestPermission()", &script_result));
253 EXPECT_EQ("granted", script_result); 201 EXPECT_EQ("granted", script_result);
254 202
255 ASSERT_TRUE(RunScript("DisplayPersistentAllOptionsNotification()", 203 ASSERT_TRUE(RunScript("DisplayPersistentAllOptionsNotification()",
256 &script_result)); 204 &script_result));
257 EXPECT_EQ("ok", script_result); 205 EXPECT_EQ("ok", script_result);
258 206
259 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); 207 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
260 208
261 // We don't use or check the notification's direction and language. 209 // We don't use or check the notification's direction and language.
262 const Notification& notification = ui_manager()->GetNotificationAt(0); 210 const Notification& notification = ui_manager()->GetNotificationAt(0);
263 EXPECT_EQ("Title", base::UTF16ToUTF8(notification.title())); 211 EXPECT_EQ("Title", base::UTF16ToUTF8(notification.title()));
264 EXPECT_EQ("Contents", base::UTF16ToUTF8(notification.message())); 212 EXPECT_EQ("Contents", base::UTF16ToUTF8(notification.message()));
265 EXPECT_EQ("replace-id", notification.tag()); 213 EXPECT_EQ("replace-id", notification.tag());
266 EXPECT_FALSE(notification.icon().IsEmpty()); 214 EXPECT_FALSE(notification.icon().IsEmpty());
267 EXPECT_TRUE(notification.silent()); 215 EXPECT_TRUE(notification.silent());
268 216
269 EXPECT_EQ(kIconWidth, notification.icon().Width()); 217 EXPECT_EQ(kIconWidth, notification.icon().Width());
270 EXPECT_EQ(kIconHeight, notification.icon().Height()); 218 EXPECT_EQ(kIconHeight, notification.icon().Height());
271 } 219 }
272 220
273 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, 221 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
274 CloseDisplayedPersistentNotification) { 222 CloseDisplayedPersistentNotification) {
275 std::string script_result; 223 std::string script_result;
276 224
277 InfoBarResponder accepting_responder(browser(), true); 225 InfoBarResponder accepting_responder(GetInfoBarService(), true);
278 ASSERT_TRUE(RunScript("RequestPermission()", &script_result)); 226 ASSERT_TRUE(RunScript("RequestPermission()", &script_result));
279 EXPECT_EQ("granted", script_result); 227 EXPECT_EQ("granted", script_result);
280 228
281 ASSERT_TRUE(RunScript("DisplayPersistentNotification('action_close')", 229 ASSERT_TRUE(RunScript("DisplayPersistentNotification('action_close')",
282 &script_result)); 230 &script_result));
283 EXPECT_EQ("ok", script_result); 231 EXPECT_EQ("ok", script_result);
284 232
285 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); 233 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
286 234
287 const Notification& notification = ui_manager()->GetNotificationAt(0); 235 const Notification& notification = ui_manager()->GetNotificationAt(0);
288 notification.delegate()->Click(); 236 notification.delegate()->Click();
289 237
290 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result)); 238 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result));
291 EXPECT_EQ("action_close", script_result); 239 EXPECT_EQ("action_close", script_result);
292 240
293 ASSERT_EQ(0u, ui_manager()->GetNotificationCount()); 241 ASSERT_EQ(0u, ui_manager()->GetNotificationCount());
294 } 242 }
295 243
296 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, 244 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
297 TestDisplayOriginContextMessage) { 245 TestDisplayOriginContextMessage) {
298 std::string script_result; 246 std::string script_result;
299 247
300 // Creates a simple notification. 248 // Creates a simple notification.
301 InfoBarResponder accepting_responder(browser(), true); 249 InfoBarResponder accepting_responder(GetInfoBarService(), true);
302 ASSERT_TRUE(RunScript("RequestPermission()", &script_result)); 250 ASSERT_TRUE(RunScript("RequestPermission()", &script_result));
303 ASSERT_EQ("granted", script_result); 251 ASSERT_EQ("granted", script_result);
304 ASSERT_TRUE(RunScript("DisplayPersistentNotification()", &script_result)); 252 ASSERT_TRUE(RunScript("DisplayPersistentNotification()", &script_result));
305 253
306 net::HostPortPair host_port = ServerHostPort(); 254 net::HostPortPair host_port = ServerHostPort();
307 255
308 const Notification& notification = ui_manager()->GetNotificationAt(0); 256 const Notification& notification = ui_manager()->GetNotificationAt(0);
309 257
310 EXPECT_EQ(base::UTF8ToUTF16(host_port.ToString()), 258 EXPECT_EQ(base::UTF8ToUTF16(host_port.ToString()),
311 notification.context_message()); 259 notification.context_message());
312 } 260 }
313 261
314 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, 262 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
315 CheckFilePermissionNotGranted) { 263 CheckFilePermissionNotGranted) {
316 // TODO(dewittj): This test verifies that a bug exists in Chrome; the test 264 // TODO(dewittj): This test verifies that a bug exists in Chrome; the test
317 // will fail if the bug is fixed. The 265 // will fail if the bug is fixed. The
318 // |PlatformNotificationServiceImpl::WebOriginDisplayName| function needs 266 // |PlatformNotificationServiceImpl::WebOriginDisplayName| function needs
319 // to be updated to properly display file:// URL origins. 267 // to be updated to properly display file:// URL origins.
320 // See crbug.com/402191. 268 // See crbug.com/402191.
321 std::string script_result; 269 std::string script_result;
322 270
323 InfoBarResponder accepting_responder_web(browser(), true); 271 InfoBarResponder accepting_responder_web(GetInfoBarService(), true);
324 272
325 DesktopNotificationService* notification_service = 273 DesktopNotificationService* notification_service =
326 DesktopNotificationServiceFactory::GetForProfile(browser()->profile()); 274 DesktopNotificationServiceFactory::GetForProfile(browser()->profile());
327 ASSERT_TRUE(notification_service); 275 ASSERT_TRUE(notification_service);
328 message_center::NotifierId web_notifier(TestPageUrl()); 276 message_center::NotifierId web_notifier(TestPageUrl());
329 EXPECT_FALSE(notification_service->IsNotifierEnabled(web_notifier)); 277 EXPECT_FALSE(notification_service->IsNotifierEnabled(web_notifier));
330 ASSERT_TRUE(RunScript("RequestPermission()", &script_result)); 278 ASSERT_TRUE(RunScript("RequestPermission()", &script_result));
331 EXPECT_EQ("granted", script_result); 279 EXPECT_EQ("granted", script_result);
332 280
333 EXPECT_TRUE(notification_service->IsNotifierEnabled(web_notifier)); 281 EXPECT_TRUE(notification_service->IsNotifierEnabled(web_notifier));
334 282
335 base::FilePath dir_source_root; 283 base::FilePath dir_source_root;
336 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &dir_source_root)); 284 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &dir_source_root));
337 base::FilePath full_file_path = 285 base::FilePath full_file_path =
338 dir_source_root.Append(server_root()).AppendASCII(kTestFileName); 286 dir_source_root.Append(server_root()).AppendASCII(kTestFileName);
339 GURL file_url(net::FilePathToFileURL(full_file_path)); 287 GURL file_url(net::FilePathToFileURL(full_file_path));
340 ui_test_utils::NavigateToURL(browser(), file_url); 288 ui_test_utils::NavigateToURL(browser(), file_url);
341 289
342 message_center::NotifierId file_notifier(file_url); 290 message_center::NotifierId file_notifier(file_url);
343 EXPECT_FALSE(notification_service->IsNotifierEnabled(file_notifier)); 291 EXPECT_FALSE(notification_service->IsNotifierEnabled(file_notifier));
344 292
345 InfoBarResponder accepting_responder_file(browser(), true); 293 InfoBarResponder accepting_responder_file(GetInfoBarService(), true);
346 ASSERT_TRUE(RunScript("RequestPermission()", &script_result)); 294 ASSERT_TRUE(RunScript("RequestPermission()", &script_result));
347 EXPECT_EQ("granted", script_result); 295 EXPECT_EQ("granted", script_result);
348 296
349 EXPECT_FALSE(notification_service->IsNotifierEnabled(file_notifier)) 297 EXPECT_FALSE(notification_service->IsNotifierEnabled(file_notifier))
350 << "If this test fails, you may have fixed a bug preventing file origins " 298 << "If this test fails, you may have fixed a bug preventing file origins "
351 << "from sending their origin from Blink; if so you need to update the " 299 << "from sending their origin from Blink; if so you need to update the "
352 << "display function for notification origins to show the file path."; 300 << "display function for notification origins to show the file path.";
353 } 301 }
354 302
355 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, 303 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
(...skipping 26 matching lines...) Expand all
382 330
383 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); 331 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
384 332
385 const Notification& notification = ui_manager()->GetNotificationAt(0); 333 const Notification& notification = ui_manager()->GetNotificationAt(0);
386 EXPECT_FALSE(notification.icon().IsEmpty()); 334 EXPECT_FALSE(notification.icon().IsEmpty());
387 335
388 EXPECT_EQ("Blob Title", base::UTF16ToUTF8(notification.title())); 336 EXPECT_EQ("Blob Title", base::UTF16ToUTF8(notification.title()));
389 EXPECT_EQ(kIconWidth, notification.icon().Width()); 337 EXPECT_EQ(kIconWidth, notification.icon().Width());
390 EXPECT_EQ(kIconHeight, notification.icon().Height()); 338 EXPECT_EQ(kIconHeight, notification.icon().Height());
391 } 339 }
OLDNEW
« no previous file with comments | « chrome/browser/infobars/infobar_responder.cc ('k') | chrome/browser/push_messaging/push_messaging_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698