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/command_line.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "chrome/browser/infobars/infobar_service.h" | |
11 #include "chrome/browser/notifications/notification_test_util.h" | |
12 #include "chrome/browser/notifications/platform_notification_service_impl.h" | |
13 #include "chrome/browser/ui/browser.h" | |
14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
15 #include "chrome/test/base/in_process_browser_test.h" | |
16 #include "chrome/test/base/ui_test_utils.h" | |
17 #include "components/infobars/core/confirm_infobar_delegate.h" | |
18 #include "components/infobars/core/infobar.h" | |
19 #include "components/infobars/core/infobar_manager.h" | |
20 #include "content/public/common/content_switches.h" | |
21 #include "content/public/test/browser_test_utils.h" | |
22 #include "net/test/spawned_test_server/spawned_test_server.h" | |
23 | |
24 // ----------------------------------------------------------------------------- | |
25 | |
26 // Accept or rejects the first shown confirm infobar. The infobar will be | |
27 // responsed to asynchronously, to immitate the behavior of a user. | |
Michael van Ouwerkerk
2014/12/11 16:41:55
nit: imitate
Peter Beverloo
2014/12/11 19:02:00
Done.
| |
28 // TODO(peter): Generalize this class, as it's commonly useful. | |
29 class InfoBarResponder : public infobars::InfoBarManager::Observer { | |
30 public: | |
31 InfoBarResponder(Browser* browser, bool accept); | |
32 ~InfoBarResponder() override; | |
33 | |
34 | |
Michael van Ouwerkerk
2014/12/11 16:41:55
nit: blank line --
Peter Beverloo
2014/12/11 19:02:00
Done.
| |
35 // infobars::InfoBarManager::Observer overrides. | |
36 void OnInfoBarAdded(infobars::InfoBar* infobar) override; | |
37 | |
38 private: | |
39 void Respond(ConfirmInfoBarDelegate* delegate); | |
40 | |
41 InfoBarService* infobar_service_; | |
42 bool accept_; | |
43 bool has_observed_; | |
44 }; | |
45 | |
46 InfoBarResponder::InfoBarResponder(Browser* browser, bool accept) | |
47 : infobar_service_(InfoBarService::FromWebContents( | |
48 browser->tab_strip_model()->GetActiveWebContents())), | |
49 accept_(accept), | |
50 has_observed_(false) { | |
51 infobar_service_->AddObserver(this); | |
52 } | |
53 | |
54 InfoBarResponder::~InfoBarResponder() { | |
55 infobar_service_->RemoveObserver(this); | |
56 } | |
57 | |
58 void InfoBarResponder::OnInfoBarAdded(infobars::InfoBar* infobar) { | |
59 if (has_observed_) | |
60 return; | |
61 | |
62 has_observed_ = true; | |
63 ConfirmInfoBarDelegate* delegate = | |
64 infobar->delegate()->AsConfirmInfoBarDelegate(); | |
65 DCHECK(delegate); | |
66 | |
67 // Respond to the infobar asynchronously, like a person. | |
68 base::MessageLoop::current()->PostTask( | |
69 FROM_HERE, | |
70 base::Bind( | |
71 &InfoBarResponder::Respond, base::Unretained(this), delegate)); | |
72 } | |
73 | |
74 void InfoBarResponder::Respond(ConfirmInfoBarDelegate* delegate) { | |
75 if (accept_) { | |
Michael van Ouwerkerk
2014/12/11 16:41:55
nit: wanna omit the curly braces?
Peter Beverloo
2014/12/11 19:02:00
Done.
| |
76 delegate->Accept(); | |
77 } else { | |
78 delegate->Cancel(); | |
79 } | |
80 } | |
81 | |
82 // ----------------------------------------------------------------------------- | |
83 | |
84 class PlatformNotificationServiceBrowserTest : public InProcessBrowserTest { | |
85 public: | |
86 ~PlatformNotificationServiceBrowserTest() override {} | |
87 | |
88 // InProcessBrowserTest overrides. | |
89 void SetUpCommandLine(base::CommandLine* command_line) override; | |
90 void SetUp() override; | |
91 void SetUpOnMainThread() override; | |
92 void TearDown() override; | |
93 | |
94 protected: | |
95 // Returns the Platform Notification Service these unit tests are for. | |
96 PlatformNotificationServiceImpl* service() const { | |
97 return PlatformNotificationServiceImpl::GetInstance(); | |
98 } | |
99 | |
100 // Returns the UI Manager on which notifications will be displayed. | |
101 StubNotificationUIManager* ui_manager() const { return ui_manager_.get(); } | |
102 | |
103 // Navigates the browser to the test page indicated by |path|. | |
104 void NavigateToTestPage(const std::string& path) const; | |
105 | |
106 // Executes |script| and stores the result as a string in |result|. A boolean | |
107 // will be returned, indicating whether the JavaScript could be executed. | |
Michael van Ouwerkerk
2014/12/11 16:41:55
nit: s/could be executed/was executed successfully
Peter Beverloo
2014/12/11 19:02:00
Done.
| |
108 bool RunScript(const std::string& script, std::string* result) const; | |
109 | |
110 private: | |
111 scoped_ptr<StubNotificationUIManager> ui_manager_; | |
112 scoped_ptr<net::SpawnedTestServer> https_server_; | |
113 }; | |
114 | |
115 // ----------------------------------------------------------------------------- | |
116 | |
117 namespace { | |
118 | |
119 const char kTestPageUrl[] = | |
120 "files/notifications/platform_notification_service.html"; | |
121 | |
122 } // namespace | |
123 | |
124 void PlatformNotificationServiceBrowserTest::SetUpCommandLine( | |
125 base::CommandLine* command_line) { | |
126 command_line->AppendSwitch( | |
127 switches::kEnableExperimentalWebPlatformFeatures); | |
128 | |
129 InProcessBrowserTest::SetUpCommandLine(command_line); | |
130 } | |
131 | |
132 void PlatformNotificationServiceBrowserTest::SetUp() { | |
133 ui_manager_.reset(new StubNotificationUIManager); | |
134 https_server_.reset(new net::SpawnedTestServer( | |
Michael van Ouwerkerk
2014/12/11 16:41:55
So I'm no longer sure we need an https server, as
Peter Beverloo
2014/12/11 19:02:00
Technically we don't, the code works perfectly fin
| |
135 net::SpawnedTestServer::TYPE_HTTPS, | |
136 net::BaseTestServer::SSLOptions( | |
137 net::BaseTestServer::SSLOptions::CERT_OK), | |
138 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/")))); | |
139 ASSERT_TRUE(https_server_->Start()); | |
140 | |
141 service()->SetNotificationUIManagerForTesting(ui_manager_.get()); | |
142 | |
143 InProcessBrowserTest::SetUp(); | |
144 } | |
145 | |
146 void PlatformNotificationServiceBrowserTest::SetUpOnMainThread() { | |
147 NavigateToTestPage(kTestPageUrl); | |
148 | |
149 InProcessBrowserTest::SetUpOnMainThread(); | |
150 } | |
151 | |
152 void PlatformNotificationServiceBrowserTest::TearDown() { | |
153 service()->SetNotificationUIManagerForTesting(nullptr); | |
154 | |
155 ui_manager_.reset(); | |
Michael van Ouwerkerk
2014/12/11 16:41:55
I'm not sure I understand this correctly. These ar
Peter Beverloo
2014/12/11 19:02:00
Precedence in a certain other browser test :-). I
| |
156 https_server_.reset(); | |
157 } | |
158 | |
159 void PlatformNotificationServiceBrowserTest::NavigateToTestPage( | |
160 const std::string& path) const { | |
161 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(path)); | |
162 } | |
163 | |
164 bool PlatformNotificationServiceBrowserTest::RunScript( | |
165 const std::string& script, std::string* result) const { | |
166 return content::ExecuteScriptAndExtractString( | |
167 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), | |
168 script, | |
169 result); | |
170 } | |
171 | |
172 // ----------------------------------------------------------------------------- | |
173 | |
174 // TODO(peter): Move PlatformNotificationService-related tests over from | |
175 // notification_browsertest.cc to this file. | |
176 | |
177 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, | |
178 DisplayPersistentNotificationWithoutPermission) { | |
179 std::string script_result; | |
180 | |
181 InfoBarResponder accepting_responder(browser(), false); | |
182 ASSERT_TRUE(RunScript("RequestPermission()", &script_result)); | |
183 EXPECT_EQ("denied", script_result); | |
184 | |
185 ASSERT_TRUE(RunScript("DisplayPersistentNotification()", &script_result)); | |
186 EXPECT_EQ( | |
187 "TypeError: No notification permission has been granted for this origin.", | |
188 script_result); | |
189 | |
190 ASSERT_EQ(0u, ui_manager()->GetNotificationCount()); | |
191 } | |
192 | |
193 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, | |
194 DisplayPersistentNotificationWithPermission) { | |
195 std::string script_result; | |
196 | |
197 InfoBarResponder accepting_responder(browser(), true); | |
198 ASSERT_TRUE(RunScript("RequestPermission()", &script_result)); | |
199 EXPECT_EQ("granted", script_result); | |
200 | |
201 ASSERT_TRUE(RunScript("DisplayPersistentNotification('action_none')", | |
202 &script_result)); | |
203 EXPECT_EQ("ok", script_result); | |
204 | |
205 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); | |
206 | |
207 const Notification& notification = ui_manager()->GetNotificationAt(0); | |
208 notification.delegate()->Click(); | |
209 | |
210 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result)); | |
211 EXPECT_EQ("action_none", script_result); | |
212 | |
213 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); | |
214 } | |
215 | |
216 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest, | |
217 CloseDisplayedPersistentNotification) { | |
218 std::string script_result; | |
219 | |
220 InfoBarResponder accepting_responder(browser(), true); | |
221 ASSERT_TRUE(RunScript("RequestPermission()", &script_result)); | |
222 EXPECT_EQ("granted", script_result); | |
223 | |
224 ASSERT_TRUE(RunScript("DisplayPersistentNotification('action_close')", | |
225 &script_result)); | |
226 EXPECT_EQ("ok", script_result); | |
227 | |
228 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); | |
229 | |
230 const Notification& notification = ui_manager()->GetNotificationAt(0); | |
231 notification.delegate()->Click(); | |
232 | |
233 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result)); | |
234 EXPECT_EQ("action_close", script_result); | |
235 | |
236 ASSERT_EQ(0u, ui_manager()->GetNotificationCount()); | |
237 } | |
OLD | NEW |