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.
5 #include "base/command_line.h"
6 #include "content/public/browser/web_contents.h"
7 #include "content/public/common/content_switches.h"
8 #include "content/public/common/manifest.h"
9 #include "content/public/test/browser_test_utils.h"
10 #include "content/public/test/content_browser_test.h"
11 #include "content/public/test/content_browser_test_utils.h"
12 #include "content/public/test/test_navigation_observer.h"
13 #include "content/shell/browser/shell.h"
14 #include "net/test/embedded_test_server/embedded_test_server.h"
18 class ManifestBrowserTest
;
20 // Mock of a WebContentsDelegate that catches messages sent to the console.
21 class MockWebContentsDelegate
: public WebContentsDelegate
{
23 MockWebContentsDelegate(WebContents
* web_contents
, ManifestBrowserTest
* test
)
24 : web_contents_(web_contents
),
28 bool AddMessageToConsole(WebContents
* source
,
30 const base::string16
& message
,
32 const base::string16
& source_id
) override
;
35 WebContents
* web_contents_
;
36 ManifestBrowserTest
* test_
;
39 class ManifestBrowserTest
: public ContentBrowserTest
{
41 friend MockWebContentsDelegate
;
43 ManifestBrowserTest() : console_error_count_(0) {}
44 ~ManifestBrowserTest() override
{}
46 void SetUpOnMainThread() override
{
47 ContentBrowserTest::SetUpOnMainThread();
48 DCHECK(shell()->web_contents());
50 mock_web_contents_delegate_
.reset(
51 new MockWebContentsDelegate(shell()->web_contents(), this));
52 shell()->web_contents()->SetDelegate(mock_web_contents_delegate_
.get());
55 void GetManifestAndWait() {
56 shell()->web_contents()->GetManifest(
57 base::Bind(&ManifestBrowserTest::OnGetManifest
,
58 base::Unretained(this)));
60 message_loop_runner_
= new MessageLoopRunner();
61 message_loop_runner_
->Run();
64 void OnGetManifest(const Manifest
& manifest
) {
66 message_loop_runner_
->Quit();
69 const Manifest
& manifest() const {
73 unsigned int console_error_count() const {
74 return console_error_count_
;
77 void OnReceivedConsoleError() {
78 console_error_count_
++;
82 scoped_refptr
<MessageLoopRunner
> message_loop_runner_
;
83 scoped_ptr
<MockWebContentsDelegate
> mock_web_contents_delegate_
;
85 int console_error_count_
;
87 DISALLOW_COPY_AND_ASSIGN(ManifestBrowserTest
);
90 // The implementation of AddMessageToConsole isn't inlined because it needs
91 // to know about |test_|.
92 bool MockWebContentsDelegate::AddMessageToConsole(
95 const base::string16
& message
,
97 const base::string16
& source_id
) {
98 DCHECK(source
== web_contents_
);
100 if (level
== logging::LOG_ERROR
)
101 test_
->OnReceivedConsoleError();
105 // If a page has no manifest, requesting a manifest should return the empty
107 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest
, NoManifest
) {
108 GURL test_url
= GetTestUrl("manifest", "no-manifest.html");
110 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
111 shell()->LoadURL(test_url
);
112 navigation_observer
.Wait();
114 GetManifestAndWait();
115 EXPECT_TRUE(manifest().IsEmpty());
116 EXPECT_EQ(0u, console_error_count());
119 // If a page manifest points to a 404 URL, requesting the manifest should return
120 // the empty manifest.
121 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest
, 404Manifest
) {
122 GURL test_url
= GetTestUrl("manifest", "404-manifest.html");
124 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
125 shell()->LoadURL(test_url
);
126 navigation_observer
.Wait();
128 GetManifestAndWait();
129 EXPECT_TRUE(manifest().IsEmpty());
130 EXPECT_EQ(0u, console_error_count());
133 // If a page has an empty manifest, requesting the manifest should return the
135 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest
, EmptyManifest
) {
136 GURL test_url
= GetTestUrl("manifest", "empty-manifest.html");
138 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
139 shell()->LoadURL(test_url
);
140 navigation_observer
.Wait();
142 GetManifestAndWait();
143 EXPECT_TRUE(manifest().IsEmpty());
144 EXPECT_EQ(0u, console_error_count());
147 // If a page's manifest can't be parsed correctly, requesting the manifest
148 // should return an empty manifest.
149 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest
, ParseErrorManifest
) {
150 GURL test_url
= GetTestUrl("manifest", "parse-error-manifest.html");
152 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
153 shell()->LoadURL(test_url
);
154 navigation_observer
.Wait();
156 GetManifestAndWait();
157 EXPECT_TRUE(manifest().IsEmpty());
158 EXPECT_EQ(1u, console_error_count());
161 // If a page has a manifest that can be fetched and parsed, requesting the
162 // manifest should return a properly filled manifest.
163 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest
, DummyManifest
) {
164 GURL test_url
= GetTestUrl("manifest", "dummy-manifest.html");
166 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
167 shell()->LoadURL(test_url
);
168 navigation_observer
.Wait();
170 GetManifestAndWait();
171 EXPECT_FALSE(manifest().IsEmpty());
172 EXPECT_EQ(0u, console_error_count());
175 // If a page changes manifest during its life-time, requesting the manifest
176 // should return the current manifest.
177 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest
, DynamicManifest
) {
178 GURL test_url
= GetTestUrl("manifest", "dynamic-manifest.html");
180 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
181 shell()->LoadURL(test_url
);
182 navigation_observer
.Wait();
185 GetManifestAndWait();
186 EXPECT_TRUE(manifest().IsEmpty());
190 std::string manifest_url
=
191 GetTestUrl("manifest", "dummy-manifest.json").spec();
192 ASSERT_TRUE(content::ExecuteScript(
193 shell()->web_contents(), "setManifestTo('" + manifest_url
+ "')"));
195 GetManifestAndWait();
196 EXPECT_FALSE(manifest().IsEmpty());
200 std::string manifest_url
=
201 GetTestUrl("manifest", "empty-manifest.json").spec();
202 ASSERT_TRUE(content::ExecuteScript(
203 shell()->web_contents(), "setManifestTo('" + manifest_url
+ "')"));
205 GetManifestAndWait();
206 EXPECT_TRUE(manifest().IsEmpty());
209 EXPECT_EQ(0u, console_error_count());
212 // If a page's manifest lives in a different origin, it should follow the CORS
213 // rules and requesting the manifest should return an empty manifest (unless the
214 // response contains CORS headers).
215 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest
, CORSManifest
) {
216 scoped_ptr
<net::test_server::EmbeddedTestServer
> cors_embedded_test_server(
217 new net::test_server::EmbeddedTestServer
);
219 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
220 ASSERT_TRUE(cors_embedded_test_server
->InitializeAndWaitUntilReady());
221 ASSERT_NE(embedded_test_server()->port(), cors_embedded_test_server
->port());
224 embedded_test_server()->GetURL("/manifest/dynamic-manifest.html");
226 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
227 shell()->LoadURL(test_url
);
228 navigation_observer
.Wait();
230 std::string manifest_url
=
231 cors_embedded_test_server
->GetURL("/manifest/dummy-manifest.json").spec();
232 ASSERT_TRUE(content::ExecuteScript(shell()->web_contents(),
233 "setManifestTo('" + manifest_url
+ "')"));
235 GetManifestAndWait();
236 EXPECT_TRUE(manifest().IsEmpty());
238 EXPECT_EQ(0u, console_error_count());
241 // If a page's manifest is in an unsecure origin while the page is in a secure
242 // origin, requesting the manifest should return the empty manifest.
243 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest
, MixedContentManifest
) {
244 scoped_ptr
<net::SpawnedTestServer
> https_server(new net::SpawnedTestServer(
245 net::SpawnedTestServer::TYPE_HTTPS
,
246 net::BaseTestServer::SSLOptions(net::BaseTestServer::SSLOptions::CERT_OK
),
247 base::FilePath(FILE_PATH_LITERAL("content/test/data"))));
249 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
250 ASSERT_TRUE(https_server
->Start());
253 embedded_test_server()->GetURL("/manifest/dynamic-manifest.html");
255 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
256 shell()->LoadURL(test_url
);
257 navigation_observer
.Wait();
259 std::string manifest_url
=
260 https_server
->GetURL("/manifest/dummy-manifest.json").spec();
261 ASSERT_TRUE(content::ExecuteScript(shell()->web_contents(),
262 "setManifestTo('" + manifest_url
+ "')"));
264 GetManifestAndWait();
265 EXPECT_TRUE(manifest().IsEmpty());
267 EXPECT_EQ(0u, console_error_count());
270 // If a page's manifest has some parsing errors, they should show up in the
271 // developer console.
272 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest
, ParsingErrorsManifest
) {
273 GURL test_url
= GetTestUrl("manifest", "parsing-errors.html");
275 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
276 shell()->LoadURL(test_url
);
277 navigation_observer
.Wait();
279 GetManifestAndWait();
280 EXPECT_TRUE(manifest().IsEmpty());
281 EXPECT_EQ(6u, console_error_count());
284 // If a page has a manifest and the page is navigated to a page without a
285 // manifest, the page's manifest should be updated.
286 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest
, Navigation
) {
287 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
290 embedded_test_server()->GetURL("/manifest/dummy-manifest.html");
292 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
293 shell()->LoadURL(test_url
);
294 navigation_observer
.Wait();
296 GetManifestAndWait();
297 EXPECT_FALSE(manifest().IsEmpty());
298 EXPECT_EQ(0u, console_error_count());
303 embedded_test_server()->GetURL("/manifest/no-manifest.html");
305 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
306 shell()->LoadURL(test_url
);
307 navigation_observer
.Wait();
309 GetManifestAndWait();
310 EXPECT_TRUE(manifest().IsEmpty());
311 EXPECT_EQ(0u, console_error_count());
315 // If a page has a manifest and the page is navigated using pushState (ie. same
316 // page), it should keep its manifest state.
317 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest
, PushStateNavigation
) {
318 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
320 embedded_test_server()->GetURL("/manifest/dummy-manifest.html");
323 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
324 shell()->LoadURL(test_url
);
325 navigation_observer
.Wait();
329 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
330 ASSERT_TRUE(content::ExecuteScript(
331 shell()->web_contents(),
332 "history.pushState({foo: \"bar\"}, 'page', 'page.html');"));
333 navigation_observer
.Wait();
336 GetManifestAndWait();
337 EXPECT_FALSE(manifest().IsEmpty());
338 EXPECT_EQ(0u, console_error_count());
341 // If a page has a manifest and is navigated using an anchor (ie. same page), it
342 // should keep its manifest state.
343 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest
, AnchorNavigation
) {
344 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
346 embedded_test_server()->GetURL("/manifest/dummy-manifest.html");
349 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
350 shell()->LoadURL(test_url
);
351 navigation_observer
.Wait();
355 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
356 ASSERT_TRUE(content::ExecuteScript(
357 shell()->web_contents(),
358 "var a = document.createElement('a'); a.href='#foo';"
359 "document.body.appendChild(a); a.click();"));
360 navigation_observer
.Wait();
363 GetManifestAndWait();
364 EXPECT_FALSE(manifest().IsEmpty());
365 EXPECT_EQ(0u, console_error_count());
368 } // namespace content