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

Side by Side Diff: content/browser/manifest/manifest_browsertest.cc

Issue 748373003: Report errors when parsing Manifest and expose them in developer console. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments 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
« no previous file with comments | « no previous file | content/renderer/manifest/manifest_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/command_line.h" 5 #include "base/command_line.h"
6 #include "content/public/browser/web_contents.h" 6 #include "content/public/browser/web_contents.h"
7 #include "content/public/common/content_switches.h" 7 #include "content/public/common/content_switches.h"
8 #include "content/public/common/manifest.h" 8 #include "content/public/common/manifest.h"
9 #include "content/public/test/browser_test_utils.h" 9 #include "content/public/test/browser_test_utils.h"
10 #include "content/public/test/content_browser_test.h" 10 #include "content/public/test/content_browser_test.h"
11 #include "content/public/test/content_browser_test_utils.h" 11 #include "content/public/test/content_browser_test_utils.h"
12 #include "content/public/test/test_navigation_observer.h" 12 #include "content/public/test/test_navigation_observer.h"
13 #include "content/shell/browser/shell.h" 13 #include "content/shell/browser/shell.h"
14 #include "net/test/embedded_test_server/embedded_test_server.h" 14 #include "net/test/embedded_test_server/embedded_test_server.h"
15 15
16 namespace content { 16 namespace content {
17 17
18 class ManifestBrowserTest;
19
20 // Mock of a WebContentsDelegate that catches messages sent to the console.
21 class MockWebContentsDelegate : public WebContentsDelegate {
22 public:
23 MockWebContentsDelegate(WebContents* web_contents, ManifestBrowserTest* test)
24 : web_contents_(web_contents),
25 test_(test) {
26 }
27
28 bool AddMessageToConsole(WebContents* source,
29 int32 level,
30 const base::string16& message,
31 int32 line_no,
32 const base::string16& source_id) override;
33
34 private:
35 WebContents* web_contents_;
36 ManifestBrowserTest* test_;
37 };
38
18 class ManifestBrowserTest : public ContentBrowserTest { 39 class ManifestBrowserTest : public ContentBrowserTest {
19 protected: 40 protected:
20 ManifestBrowserTest() {} 41 friend MockWebContentsDelegate;
42
43 ManifestBrowserTest() : console_error_count_(0) {}
21 ~ManifestBrowserTest() override {} 44 ~ManifestBrowserTest() override {}
22 45
46 void SetUpOnMainThread() override {
47 ContentBrowserTest::SetUpOnMainThread();
48 DCHECK(shell()->web_contents());
49
50 mock_web_contents_delegate_.reset(
51 new MockWebContentsDelegate(shell()->web_contents(), this));
52 shell()->web_contents()->SetDelegate(mock_web_contents_delegate_.get());
53 }
54
23 void GetManifestAndWait() { 55 void GetManifestAndWait() {
24 shell()->web_contents()->GetManifest( 56 shell()->web_contents()->GetManifest(
25 base::Bind(&ManifestBrowserTest::OnGetManifest, 57 base::Bind(&ManifestBrowserTest::OnGetManifest,
26 base::Unretained(this))); 58 base::Unretained(this)));
27 59
28 message_loop_runner_ = new MessageLoopRunner(); 60 message_loop_runner_ = new MessageLoopRunner();
29 message_loop_runner_->Run(); 61 message_loop_runner_->Run();
30 } 62 }
31 63
32 void OnGetManifest(const Manifest& manifest) { 64 void OnGetManifest(const Manifest& manifest) {
33 manifest_ = manifest; 65 manifest_ = manifest;
34 message_loop_runner_->Quit(); 66 message_loop_runner_->Quit();
35 } 67 }
36 68
37 const Manifest& manifest() const { 69 const Manifest& manifest() const {
38 return manifest_; 70 return manifest_;
39 } 71 }
40 72
73 unsigned int console_error_count() const {
74 return console_error_count_;
75 }
76
77 void OnReceivedConsoleError() {
78 console_error_count_++;
79 }
80
41 private: 81 private:
42 scoped_refptr<MessageLoopRunner> message_loop_runner_; 82 scoped_refptr<MessageLoopRunner> message_loop_runner_;
83 scoped_ptr<MockWebContentsDelegate> mock_web_contents_delegate_;
43 Manifest manifest_; 84 Manifest manifest_;
85 int console_error_count_;
44 86
45 DISALLOW_COPY_AND_ASSIGN(ManifestBrowserTest); 87 DISALLOW_COPY_AND_ASSIGN(ManifestBrowserTest);
46 }; 88 };
47 89
90 // The implementation of AddMessageToConsole isn't inlined because it needs
91 // to know about |test_|.
92 bool MockWebContentsDelegate::AddMessageToConsole(
93 WebContents* source,
94 int32 level,
95 const base::string16& message,
96 int32 line_no,
97 const base::string16& source_id) {
98 DCHECK(source == web_contents_);
99
100 if (level == logging::LOG_ERROR)
101 test_->OnReceivedConsoleError();
102 return false;
103 }
104
48 // If a page has no manifest, requesting a manifest should return the empty 105 // If a page has no manifest, requesting a manifest should return the empty
49 // manifest. 106 // manifest.
50 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, NoManifest) { 107 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, NoManifest) {
51 GURL test_url = GetTestUrl("manifest", "no-manifest.html"); 108 GURL test_url = GetTestUrl("manifest", "no-manifest.html");
52 109
53 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); 110 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
54 shell()->LoadURL(test_url); 111 shell()->LoadURL(test_url);
55 navigation_observer.Wait(); 112 navigation_observer.Wait();
56 113
57 GetManifestAndWait(); 114 GetManifestAndWait();
58 EXPECT_TRUE(manifest().IsEmpty()); 115 EXPECT_TRUE(manifest().IsEmpty());
116 EXPECT_EQ(0u, console_error_count());
59 } 117 }
60 118
61 // If a page manifest points to a 404 URL, requesting the manifest should return 119 // If a page manifest points to a 404 URL, requesting the manifest should return
62 // the empty manifest. 120 // the empty manifest.
63 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, 404Manifest) { 121 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, 404Manifest) {
64 GURL test_url = GetTestUrl("manifest", "404-manifest.html"); 122 GURL test_url = GetTestUrl("manifest", "404-manifest.html");
65 123
66 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); 124 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
67 shell()->LoadURL(test_url); 125 shell()->LoadURL(test_url);
68 navigation_observer.Wait(); 126 navigation_observer.Wait();
69 127
70 GetManifestAndWait(); 128 GetManifestAndWait();
71 EXPECT_TRUE(manifest().IsEmpty()); 129 EXPECT_TRUE(manifest().IsEmpty());
130 EXPECT_EQ(0u, console_error_count());
72 } 131 }
73 132
74 // If a page has an empty manifest, requesting the manifest should return the 133 // If a page has an empty manifest, requesting the manifest should return the
75 // empty manifest. 134 // empty manifest.
76 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, EmptyManifest) { 135 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, EmptyManifest) {
77 GURL test_url = GetTestUrl("manifest", "empty-manifest.html"); 136 GURL test_url = GetTestUrl("manifest", "empty-manifest.html");
78 137
79 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); 138 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
80 shell()->LoadURL(test_url); 139 shell()->LoadURL(test_url);
81 navigation_observer.Wait(); 140 navigation_observer.Wait();
82 141
83 GetManifestAndWait(); 142 GetManifestAndWait();
84 EXPECT_TRUE(manifest().IsEmpty()); 143 EXPECT_TRUE(manifest().IsEmpty());
144 EXPECT_EQ(0u, console_error_count());
85 } 145 }
86 146
87 // If a page's manifest can't be parsed correctly, requesting the manifest 147 // If a page's manifest can't be parsed correctly, requesting the manifest
88 // should return an empty manifest. 148 // should return an empty manifest.
89 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, ParseErrorManifest) { 149 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, ParseErrorManifest) {
90 GURL test_url = GetTestUrl("manifest", "parse-error-manifest.html"); 150 GURL test_url = GetTestUrl("manifest", "parse-error-manifest.html");
91 151
92 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); 152 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
93 shell()->LoadURL(test_url); 153 shell()->LoadURL(test_url);
94 navigation_observer.Wait(); 154 navigation_observer.Wait();
95 155
96 GetManifestAndWait(); 156 GetManifestAndWait();
97 EXPECT_TRUE(manifest().IsEmpty()); 157 EXPECT_TRUE(manifest().IsEmpty());
158 EXPECT_EQ(1u, console_error_count());
98 } 159 }
99 160
100 // If a page has a manifest that can be fetched and parsed, requesting the 161 // If a page has a manifest that can be fetched and parsed, requesting the
101 // manifest should return a properly filled manifest. 162 // manifest should return a properly filled manifest.
102 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, DummyManifest) { 163 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, DummyManifest) {
103 GURL test_url = GetTestUrl("manifest", "dummy-manifest.html"); 164 GURL test_url = GetTestUrl("manifest", "dummy-manifest.html");
104 165
105 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); 166 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
106 shell()->LoadURL(test_url); 167 shell()->LoadURL(test_url);
107 navigation_observer.Wait(); 168 navigation_observer.Wait();
108 169
109 GetManifestAndWait(); 170 GetManifestAndWait();
110 EXPECT_FALSE(manifest().IsEmpty()); 171 EXPECT_FALSE(manifest().IsEmpty());
172 EXPECT_EQ(0u, console_error_count());
111 } 173 }
112 174
113 // If a page changes manifest during its life-time, requesting the manifest 175 // If a page changes manifest during its life-time, requesting the manifest
114 // should return the current manifest. 176 // should return the current manifest.
115 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, DynamicManifest) { 177 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, DynamicManifest) {
116 GURL test_url = GetTestUrl("manifest", "dynamic-manifest.html"); 178 GURL test_url = GetTestUrl("manifest", "dynamic-manifest.html");
117 179
118 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); 180 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
119 shell()->LoadURL(test_url); 181 shell()->LoadURL(test_url);
120 navigation_observer.Wait(); 182 navigation_observer.Wait();
(...skipping 15 matching lines...) Expand all
136 198
137 { 199 {
138 std::string manifest_url = 200 std::string manifest_url =
139 GetTestUrl("manifest", "empty-manifest.json").spec(); 201 GetTestUrl("manifest", "empty-manifest.json").spec();
140 ASSERT_TRUE(content::ExecuteScript( 202 ASSERT_TRUE(content::ExecuteScript(
141 shell()->web_contents(), "setManifestTo('" + manifest_url + "')")); 203 shell()->web_contents(), "setManifestTo('" + manifest_url + "')"));
142 204
143 GetManifestAndWait(); 205 GetManifestAndWait();
144 EXPECT_TRUE(manifest().IsEmpty()); 206 EXPECT_TRUE(manifest().IsEmpty());
145 } 207 }
208
209 EXPECT_EQ(0u, console_error_count());
146 } 210 }
147 211
148 // If a page's manifest lives in a different origin, it should follow the CORS 212 // If a page's manifest lives in a different origin, it should follow the CORS
149 // rules and requesting the manifest should return an empty manifest (unless the 213 // rules and requesting the manifest should return an empty manifest (unless the
150 // response contains CORS headers). 214 // response contains CORS headers).
151 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, CORSManifest) { 215 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, CORSManifest) {
152 scoped_ptr<net::test_server::EmbeddedTestServer> cors_embedded_test_server( 216 scoped_ptr<net::test_server::EmbeddedTestServer> cors_embedded_test_server(
153 new net::test_server::EmbeddedTestServer); 217 new net::test_server::EmbeddedTestServer);
154 218
155 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 219 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
156 ASSERT_TRUE(cors_embedded_test_server->InitializeAndWaitUntilReady()); 220 ASSERT_TRUE(cors_embedded_test_server->InitializeAndWaitUntilReady());
157 ASSERT_NE(embedded_test_server()->port(), cors_embedded_test_server->port()); 221 ASSERT_NE(embedded_test_server()->port(), cors_embedded_test_server->port());
158 222
159 GURL test_url = 223 GURL test_url =
160 embedded_test_server()->GetURL("/manifest/dynamic-manifest.html"); 224 embedded_test_server()->GetURL("/manifest/dynamic-manifest.html");
161 225
162 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); 226 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
163 shell()->LoadURL(test_url); 227 shell()->LoadURL(test_url);
164 navigation_observer.Wait(); 228 navigation_observer.Wait();
165 229
166 std::string manifest_url = 230 std::string manifest_url =
167 cors_embedded_test_server->GetURL("/manifest/dummy-manifest.json").spec(); 231 cors_embedded_test_server->GetURL("/manifest/dummy-manifest.json").spec();
168 ASSERT_TRUE(content::ExecuteScript(shell()->web_contents(), 232 ASSERT_TRUE(content::ExecuteScript(shell()->web_contents(),
169 "setManifestTo('" + manifest_url + "')")); 233 "setManifestTo('" + manifest_url + "')"));
170 234
171 GetManifestAndWait(); 235 GetManifestAndWait();
172 EXPECT_TRUE(manifest().IsEmpty()); 236 EXPECT_TRUE(manifest().IsEmpty());
237
238 EXPECT_EQ(0u, console_error_count());
173 } 239 }
174 240
175 // If a page's manifest is in an unsecure origin while the page is in a secure 241 // If a page's manifest is in an unsecure origin while the page is in a secure
176 // origin, requesting the manifest should return the empty manifest. 242 // origin, requesting the manifest should return the empty manifest.
177 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, MixedContentManifest) { 243 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, MixedContentManifest) {
178 scoped_ptr<net::SpawnedTestServer> https_server(new net::SpawnedTestServer( 244 scoped_ptr<net::SpawnedTestServer> https_server(new net::SpawnedTestServer(
179 net::SpawnedTestServer::TYPE_HTTPS, 245 net::SpawnedTestServer::TYPE_HTTPS,
180 net::BaseTestServer::SSLOptions(net::BaseTestServer::SSLOptions::CERT_OK), 246 net::BaseTestServer::SSLOptions(net::BaseTestServer::SSLOptions::CERT_OK),
181 base::FilePath(FILE_PATH_LITERAL("content/test/data")))); 247 base::FilePath(FILE_PATH_LITERAL("content/test/data"))));
182 248
183 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 249 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
184 ASSERT_TRUE(https_server->Start()); 250 ASSERT_TRUE(https_server->Start());
185 251
186 GURL test_url = 252 GURL test_url =
187 embedded_test_server()->GetURL("/manifest/dynamic-manifest.html"); 253 embedded_test_server()->GetURL("/manifest/dynamic-manifest.html");
188 254
189 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); 255 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
190 shell()->LoadURL(test_url); 256 shell()->LoadURL(test_url);
191 navigation_observer.Wait(); 257 navigation_observer.Wait();
192 258
193 std::string manifest_url = 259 std::string manifest_url =
194 https_server->GetURL("/manifest/dummy-manifest.json").spec(); 260 https_server->GetURL("/manifest/dummy-manifest.json").spec();
195 ASSERT_TRUE(content::ExecuteScript(shell()->web_contents(), 261 ASSERT_TRUE(content::ExecuteScript(shell()->web_contents(),
196 "setManifestTo('" + manifest_url + "')")); 262 "setManifestTo('" + manifest_url + "')"));
197 263
198 GetManifestAndWait(); 264 GetManifestAndWait();
199 EXPECT_TRUE(manifest().IsEmpty()); 265 EXPECT_TRUE(manifest().IsEmpty());
266
267 EXPECT_EQ(0u, console_error_count());
268 }
269
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");
274
275 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
276 shell()->LoadURL(test_url);
277 navigation_observer.Wait();
278
279 GetManifestAndWait();
280 EXPECT_TRUE(manifest().IsEmpty());
281 EXPECT_EQ(6u, console_error_count());
200 } 282 }
201 283
202 } // namespace content 284 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/renderer/manifest/manifest_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698