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

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

Issue 784383002: Support persistent notifications in the PlatformNotificationServiceImpl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@n-chrome-base
Patch Set: add copyright header Created 6 years 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
(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 imitate the behavior of a user.
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 // infobars::InfoBarManager::Observer overrides.
35 void OnInfoBarAdded(infobars::InfoBar* infobar) override;
36
37 private:
38 void Respond(ConfirmInfoBarDelegate* delegate);
39
40 InfoBarService* infobar_service_;
41 bool accept_;
42 bool has_observed_;
43 };
44
45 InfoBarResponder::InfoBarResponder(Browser* browser, bool accept)
46 : infobar_service_(InfoBarService::FromWebContents(
47 browser->tab_strip_model()->GetActiveWebContents())),
48 accept_(accept),
49 has_observed_(false) {
50 infobar_service_->AddObserver(this);
51 }
52
53 InfoBarResponder::~InfoBarResponder() {
54 infobar_service_->RemoveObserver(this);
55 }
56
57 void InfoBarResponder::OnInfoBarAdded(infobars::InfoBar* infobar) {
58 if (has_observed_)
59 return;
60
61 has_observed_ = true;
62 ConfirmInfoBarDelegate* delegate =
63 infobar->delegate()->AsConfirmInfoBarDelegate();
64 DCHECK(delegate);
65
66 // Respond to the infobar asynchronously, like a person.
67 base::MessageLoop::current()->PostTask(
68 FROM_HERE,
69 base::Bind(
70 &InfoBarResponder::Respond, base::Unretained(this), delegate));
71 }
72
73 void InfoBarResponder::Respond(ConfirmInfoBarDelegate* delegate) {
74 if (accept_)
75 delegate->Accept();
76 else
77 delegate->Cancel();
78 }
79
80 // -----------------------------------------------------------------------------
81
82 class PlatformNotificationServiceBrowserTest : public InProcessBrowserTest {
83 public:
84 ~PlatformNotificationServiceBrowserTest() override {}
85
86 // InProcessBrowserTest overrides.
87 void SetUpCommandLine(base::CommandLine* command_line) override;
88 void SetUp() override;
89 void SetUpOnMainThread() override;
90 void TearDown() override;
91
92 protected:
93 // Returns the Platform Notification Service these unit tests are for.
94 PlatformNotificationServiceImpl* service() const {
95 return PlatformNotificationServiceImpl::GetInstance();
96 }
97
98 // Returns the UI Manager on which notifications will be displayed.
99 StubNotificationUIManager* ui_manager() const { return ui_manager_.get(); }
100
101 // Navigates the browser to the test page indicated by |path|.
102 void NavigateToTestPage(const std::string& path) const;
103
104 // Executes |script| and stores the result as a string in |result|. A boolean
105 // will be returned, indicating whether the script was executed successfully.
106 bool RunScript(const std::string& script, std::string* result) const;
107
108 private:
109 scoped_ptr<StubNotificationUIManager> ui_manager_;
110 scoped_ptr<net::SpawnedTestServer> https_server_;
111 };
112
113 // -----------------------------------------------------------------------------
114
115 namespace {
116
117 const char kTestPageUrl[] =
118 "files/notifications/platform_notification_service.html";
119
120 } // namespace
121
122 void PlatformNotificationServiceBrowserTest::SetUpCommandLine(
123 base::CommandLine* command_line) {
124 command_line->AppendSwitch(
125 switches::kEnableExperimentalWebPlatformFeatures);
126
127 InProcessBrowserTest::SetUpCommandLine(command_line);
128 }
129
130 void PlatformNotificationServiceBrowserTest::SetUp() {
131 ui_manager_.reset(new StubNotificationUIManager);
132 https_server_.reset(new net::SpawnedTestServer(
133 net::SpawnedTestServer::TYPE_HTTPS,
134 net::BaseTestServer::SSLOptions(
135 net::BaseTestServer::SSLOptions::CERT_OK),
136 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/"))));
137 ASSERT_TRUE(https_server_->Start());
138
139 service()->SetNotificationUIManagerForTesting(ui_manager_.get());
140
141 InProcessBrowserTest::SetUp();
142 }
143
144 void PlatformNotificationServiceBrowserTest::SetUpOnMainThread() {
145 NavigateToTestPage(kTestPageUrl);
146
147 InProcessBrowserTest::SetUpOnMainThread();
148 }
149
150 void PlatformNotificationServiceBrowserTest::TearDown() {
151 service()->SetNotificationUIManagerForTesting(nullptr);
152 }
153
154 void PlatformNotificationServiceBrowserTest::NavigateToTestPage(
155 const std::string& path) const {
156 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(path));
157 }
158
159 bool PlatformNotificationServiceBrowserTest::RunScript(
160 const std::string& script, std::string* result) const {
161 return content::ExecuteScriptAndExtractString(
162 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
163 script,
164 result);
165 }
166
167 // -----------------------------------------------------------------------------
168
169 // TODO(peter): Move PlatformNotificationService-related tests over from
170 // notification_browsertest.cc to this file.
171
172 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
173 DisplayPersistentNotificationWithoutPermission) {
174 std::string script_result;
175
176 InfoBarResponder accepting_responder(browser(), false);
177 ASSERT_TRUE(RunScript("RequestPermission()", &script_result));
178 EXPECT_EQ("denied", script_result);
179
180 ASSERT_TRUE(RunScript("DisplayPersistentNotification()", &script_result));
181 EXPECT_EQ(
182 "TypeError: No notification permission has been granted for this origin.",
183 script_result);
184
185 ASSERT_EQ(0u, ui_manager()->GetNotificationCount());
186 }
187
188 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
189 DisplayPersistentNotificationWithPermission) {
190 std::string script_result;
191
192 InfoBarResponder accepting_responder(browser(), true);
193 ASSERT_TRUE(RunScript("RequestPermission()", &script_result));
194 EXPECT_EQ("granted", script_result);
195
196 ASSERT_TRUE(RunScript("DisplayPersistentNotification('action_none')",
197 &script_result));
198 EXPECT_EQ("ok", script_result);
199
200 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
201
202 const Notification& notification = ui_manager()->GetNotificationAt(0);
203 notification.delegate()->Click();
204
205 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result));
206 EXPECT_EQ("action_none", script_result);
207
208 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
209 }
210
211 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
212 CloseDisplayedPersistentNotification) {
213 std::string script_result;
214
215 InfoBarResponder accepting_responder(browser(), true);
216 ASSERT_TRUE(RunScript("RequestPermission()", &script_result));
217 EXPECT_EQ("granted", script_result);
218
219 ASSERT_TRUE(RunScript("DisplayPersistentNotification('action_close')",
220 &script_result));
221 EXPECT_EQ("ok", script_result);
222
223 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
224
225 const Notification& notification = ui_manager()->GetNotificationAt(0);
226 notification.delegate()->Click();
227
228 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result));
229 EXPECT_EQ("action_close", script_result);
230
231 ASSERT_EQ(0u, ui_manager()->GetNotificationCount());
232 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698