Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 content::WebContentsDelegate { | |
|
Peter Beverloo
2014/11/27 14:37:37
nit: no need for content::
mlamouri (slow - plz ping)
2014/11/27 14:44:10
Done.
| |
| 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 OverrideWebContentsDelegate() { | |
|
Peter Beverloo
2014/11/27 14:37:37
I wonder if we could do this in SetUp(), avoiding
mlamouri (slow - plz ping)
2014/11/27 14:44:10
I've been experimenting with that but the DCHECK w
| |
| 47 DCHECK(shell()->web_contents()); | |
| 48 | |
| 49 mock_web_contents_delegate_.reset( | |
| 50 new MockWebContentsDelegate(shell()->web_contents(), this)); | |
| 51 shell()->web_contents()->SetDelegate(mock_web_contents_delegate_.get()); | |
| 52 } | |
| 53 | |
| 23 void GetManifestAndWait() { | 54 void GetManifestAndWait() { |
| 24 shell()->web_contents()->GetManifest( | 55 shell()->web_contents()->GetManifest( |
| 25 base::Bind(&ManifestBrowserTest::OnGetManifest, | 56 base::Bind(&ManifestBrowserTest::OnGetManifest, |
| 26 base::Unretained(this))); | 57 base::Unretained(this))); |
| 27 | 58 |
| 28 message_loop_runner_ = new MessageLoopRunner(); | 59 message_loop_runner_ = new MessageLoopRunner(); |
| 29 message_loop_runner_->Run(); | 60 message_loop_runner_->Run(); |
| 30 } | 61 } |
| 31 | 62 |
| 32 void OnGetManifest(const Manifest& manifest) { | 63 void OnGetManifest(const Manifest& manifest) { |
| 33 manifest_ = manifest; | 64 manifest_ = manifest; |
| 34 message_loop_runner_->Quit(); | 65 message_loop_runner_->Quit(); |
| 35 } | 66 } |
| 36 | 67 |
| 37 const Manifest& manifest() const { | 68 const Manifest& manifest() const { |
| 38 return manifest_; | 69 return manifest_; |
| 39 } | 70 } |
| 40 | 71 |
| 72 unsigned int console_error_count() const { | |
| 73 return console_error_count_; | |
| 74 } | |
| 75 | |
| 76 void OnReceivedConsoleError() { | |
| 77 console_error_count_++; | |
| 78 } | |
| 79 | |
| 41 private: | 80 private: |
| 42 scoped_refptr<MessageLoopRunner> message_loop_runner_; | 81 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 82 scoped_ptr<MockWebContentsDelegate> mock_web_contents_delegate_; | |
| 43 Manifest manifest_; | 83 Manifest manifest_; |
| 84 int console_error_count_; | |
| 44 | 85 |
| 45 DISALLOW_COPY_AND_ASSIGN(ManifestBrowserTest); | 86 DISALLOW_COPY_AND_ASSIGN(ManifestBrowserTest); |
| 46 }; | 87 }; |
| 47 | 88 |
| 89 // The implementation of AddMessageToConsole isn't inlined because it needs | |
| 90 // to know about |test_|. | |
| 91 bool MockWebContentsDelegate::AddMessageToConsole( | |
| 92 WebContents* source, | |
| 93 int32 level, | |
| 94 const base::string16& message, | |
| 95 int32 line_no, | |
| 96 const base::string16& source_id) { | |
| 97 if (source != web_contents_) | |
|
Peter Beverloo
2014/11/27 14:37:37
When does this happen? Can it be a DCHECK?
mlamouri (slow - plz ping)
2014/11/27 14:44:10
Done.
| |
| 98 return false; | |
| 99 | |
| 100 if (level != logging::LOG_ERROR) | |
| 101 return false; | |
| 102 | |
| 103 test_->OnReceivedConsoleError(); | |
| 104 return false; | |
| 105 } | |
| 106 | |
| 48 // If a page has no manifest, requesting a manifest should return the empty | 107 // If a page has no manifest, requesting a manifest should return the empty |
| 49 // manifest. | 108 // manifest. |
| 50 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, NoManifest) { | 109 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, NoManifest) { |
| 51 GURL test_url = GetTestUrl("manifest", "no-manifest.html"); | 110 GURL test_url = GetTestUrl("manifest", "no-manifest.html"); |
| 52 | 111 |
| 112 OverrideWebContentsDelegate(); | |
| 113 | |
| 53 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); | 114 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); |
| 54 shell()->LoadURL(test_url); | 115 shell()->LoadURL(test_url); |
| 55 navigation_observer.Wait(); | 116 navigation_observer.Wait(); |
| 56 | 117 |
| 57 GetManifestAndWait(); | 118 GetManifestAndWait(); |
| 58 EXPECT_TRUE(manifest().IsEmpty()); | 119 EXPECT_TRUE(manifest().IsEmpty()); |
| 120 EXPECT_EQ(0u, console_error_count()); | |
| 59 } | 121 } |
| 60 | 122 |
| 61 // If a page manifest points to a 404 URL, requesting the manifest should return | 123 // If a page manifest points to a 404 URL, requesting the manifest should return |
| 62 // the empty manifest. | 124 // the empty manifest. |
| 63 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, 404Manifest) { | 125 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, 404Manifest) { |
| 64 GURL test_url = GetTestUrl("manifest", "404-manifest.html"); | 126 GURL test_url = GetTestUrl("manifest", "404-manifest.html"); |
| 65 | 127 |
| 128 OverrideWebContentsDelegate(); | |
| 129 | |
| 66 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); | 130 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); |
| 67 shell()->LoadURL(test_url); | 131 shell()->LoadURL(test_url); |
| 68 navigation_observer.Wait(); | 132 navigation_observer.Wait(); |
| 69 | 133 |
| 70 GetManifestAndWait(); | 134 GetManifestAndWait(); |
| 71 EXPECT_TRUE(manifest().IsEmpty()); | 135 EXPECT_TRUE(manifest().IsEmpty()); |
| 136 EXPECT_EQ(0u, console_error_count()); | |
| 72 } | 137 } |
| 73 | 138 |
| 74 // If a page has an empty manifest, requesting the manifest should return the | 139 // If a page has an empty manifest, requesting the manifest should return the |
| 75 // empty manifest. | 140 // empty manifest. |
| 76 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, EmptyManifest) { | 141 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, EmptyManifest) { |
| 77 GURL test_url = GetTestUrl("manifest", "empty-manifest.html"); | 142 GURL test_url = GetTestUrl("manifest", "empty-manifest.html"); |
| 78 | 143 |
| 144 OverrideWebContentsDelegate(); | |
| 145 | |
| 79 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); | 146 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); |
| 80 shell()->LoadURL(test_url); | 147 shell()->LoadURL(test_url); |
| 81 navigation_observer.Wait(); | 148 navigation_observer.Wait(); |
| 82 | 149 |
| 83 GetManifestAndWait(); | 150 GetManifestAndWait(); |
| 84 EXPECT_TRUE(manifest().IsEmpty()); | 151 EXPECT_TRUE(manifest().IsEmpty()); |
| 152 EXPECT_EQ(0u, console_error_count()); | |
| 85 } | 153 } |
| 86 | 154 |
| 87 // If a page's manifest can't be parsed correctly, requesting the manifest | 155 // If a page's manifest can't be parsed correctly, requesting the manifest |
| 88 // should return an empty manifest. | 156 // should return an empty manifest. |
| 89 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, ParseErrorManifest) { | 157 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, ParseErrorManifest) { |
| 90 GURL test_url = GetTestUrl("manifest", "parse-error-manifest.html"); | 158 GURL test_url = GetTestUrl("manifest", "parse-error-manifest.html"); |
| 91 | 159 |
| 160 OverrideWebContentsDelegate(); | |
| 161 | |
| 92 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); | 162 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); |
| 93 shell()->LoadURL(test_url); | 163 shell()->LoadURL(test_url); |
| 94 navigation_observer.Wait(); | 164 navigation_observer.Wait(); |
| 95 | 165 |
| 96 GetManifestAndWait(); | 166 GetManifestAndWait(); |
| 97 EXPECT_TRUE(manifest().IsEmpty()); | 167 EXPECT_TRUE(manifest().IsEmpty()); |
| 168 EXPECT_EQ(1u, console_error_count()); | |
| 98 } | 169 } |
| 99 | 170 |
| 100 // If a page has a manifest that can be fetched and parsed, requesting the | 171 // If a page has a manifest that can be fetched and parsed, requesting the |
| 101 // manifest should return a properly filled manifest. | 172 // manifest should return a properly filled manifest. |
| 102 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, DummyManifest) { | 173 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, DummyManifest) { |
| 103 GURL test_url = GetTestUrl("manifest", "dummy-manifest.html"); | 174 GURL test_url = GetTestUrl("manifest", "dummy-manifest.html"); |
| 104 | 175 |
| 176 OverrideWebContentsDelegate(); | |
| 177 | |
| 105 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); | 178 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); |
| 106 shell()->LoadURL(test_url); | 179 shell()->LoadURL(test_url); |
| 107 navigation_observer.Wait(); | 180 navigation_observer.Wait(); |
| 108 | 181 |
| 109 GetManifestAndWait(); | 182 GetManifestAndWait(); |
| 110 EXPECT_FALSE(manifest().IsEmpty()); | 183 EXPECT_FALSE(manifest().IsEmpty()); |
| 184 EXPECT_EQ(0u, console_error_count()); | |
| 111 } | 185 } |
| 112 | 186 |
| 113 // If a page changes manifest during its life-time, requesting the manifest | 187 // If a page changes manifest during its life-time, requesting the manifest |
| 114 // should return the current manifest. | 188 // should return the current manifest. |
| 115 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, DynamicManifest) { | 189 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, DynamicManifest) { |
| 116 GURL test_url = GetTestUrl("manifest", "dynamic-manifest.html"); | 190 GURL test_url = GetTestUrl("manifest", "dynamic-manifest.html"); |
| 117 | 191 |
| 192 OverrideWebContentsDelegate(); | |
| 193 | |
| 118 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); | 194 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); |
| 119 shell()->LoadURL(test_url); | 195 shell()->LoadURL(test_url); |
| 120 navigation_observer.Wait(); | 196 navigation_observer.Wait(); |
| 121 | 197 |
| 122 { | 198 { |
| 123 GetManifestAndWait(); | 199 GetManifestAndWait(); |
| 124 EXPECT_TRUE(manifest().IsEmpty()); | 200 EXPECT_TRUE(manifest().IsEmpty()); |
| 125 } | 201 } |
| 126 | 202 |
| 127 { | 203 { |
| 128 std::string manifest_url = | 204 std::string manifest_url = |
| 129 GetTestUrl("manifest", "dummy-manifest.json").spec(); | 205 GetTestUrl("manifest", "dummy-manifest.json").spec(); |
| 130 ASSERT_TRUE(content::ExecuteScript( | 206 ASSERT_TRUE(content::ExecuteScript( |
| 131 shell()->web_contents(), "setManifestTo('" + manifest_url + "')")); | 207 shell()->web_contents(), "setManifestTo('" + manifest_url + "')")); |
| 132 | 208 |
| 133 GetManifestAndWait(); | 209 GetManifestAndWait(); |
| 134 EXPECT_FALSE(manifest().IsEmpty()); | 210 EXPECT_FALSE(manifest().IsEmpty()); |
| 135 } | 211 } |
| 136 | 212 |
| 137 { | 213 { |
| 138 std::string manifest_url = | 214 std::string manifest_url = |
| 139 GetTestUrl("manifest", "empty-manifest.json").spec(); | 215 GetTestUrl("manifest", "empty-manifest.json").spec(); |
| 140 ASSERT_TRUE(content::ExecuteScript( | 216 ASSERT_TRUE(content::ExecuteScript( |
| 141 shell()->web_contents(), "setManifestTo('" + manifest_url + "')")); | 217 shell()->web_contents(), "setManifestTo('" + manifest_url + "')")); |
| 142 | 218 |
| 143 GetManifestAndWait(); | 219 GetManifestAndWait(); |
| 144 EXPECT_TRUE(manifest().IsEmpty()); | 220 EXPECT_TRUE(manifest().IsEmpty()); |
| 145 } | 221 } |
| 222 | |
| 223 EXPECT_EQ(0u, console_error_count()); | |
| 146 } | 224 } |
| 147 | 225 |
| 148 // If a page's manifest lives in a different origin, it should follow the CORS | 226 // 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 | 227 // rules and requesting the manifest should return an empty manifest (unless the |
| 150 // response contains CORS headers). | 228 // response contains CORS headers). |
| 151 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, CORSManifest) { | 229 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, CORSManifest) { |
| 152 scoped_ptr<net::test_server::EmbeddedTestServer> cors_embedded_test_server( | 230 scoped_ptr<net::test_server::EmbeddedTestServer> cors_embedded_test_server( |
| 153 new net::test_server::EmbeddedTestServer); | 231 new net::test_server::EmbeddedTestServer); |
| 154 | 232 |
| 155 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 233 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 156 ASSERT_TRUE(cors_embedded_test_server->InitializeAndWaitUntilReady()); | 234 ASSERT_TRUE(cors_embedded_test_server->InitializeAndWaitUntilReady()); |
| 157 ASSERT_NE(embedded_test_server()->port(), cors_embedded_test_server->port()); | 235 ASSERT_NE(embedded_test_server()->port(), cors_embedded_test_server->port()); |
| 158 | 236 |
| 159 GURL test_url = | 237 GURL test_url = |
| 160 embedded_test_server()->GetURL("/manifest/dynamic-manifest.html"); | 238 embedded_test_server()->GetURL("/manifest/dynamic-manifest.html"); |
| 161 | 239 |
| 240 OverrideWebContentsDelegate(); | |
| 241 | |
| 162 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); | 242 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); |
| 163 shell()->LoadURL(test_url); | 243 shell()->LoadURL(test_url); |
| 164 navigation_observer.Wait(); | 244 navigation_observer.Wait(); |
| 165 | 245 |
| 166 std::string manifest_url = | 246 std::string manifest_url = |
| 167 cors_embedded_test_server->GetURL("/manifest/dummy-manifest.json").spec(); | 247 cors_embedded_test_server->GetURL("/manifest/dummy-manifest.json").spec(); |
| 168 ASSERT_TRUE(content::ExecuteScript(shell()->web_contents(), | 248 ASSERT_TRUE(content::ExecuteScript(shell()->web_contents(), |
| 169 "setManifestTo('" + manifest_url + "')")); | 249 "setManifestTo('" + manifest_url + "')")); |
| 170 | 250 |
| 171 GetManifestAndWait(); | 251 GetManifestAndWait(); |
| 172 EXPECT_TRUE(manifest().IsEmpty()); | 252 EXPECT_TRUE(manifest().IsEmpty()); |
| 253 | |
| 254 EXPECT_EQ(0u, console_error_count()); | |
| 173 } | 255 } |
| 174 | 256 |
| 175 // If a page's manifest is in an unsecure origin while the page is in a secure | 257 // 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. | 258 // origin, requesting the manifest should return the empty manifest. |
| 177 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, MixedContentManifest) { | 259 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, MixedContentManifest) { |
| 178 scoped_ptr<net::SpawnedTestServer> https_server(new net::SpawnedTestServer( | 260 scoped_ptr<net::SpawnedTestServer> https_server(new net::SpawnedTestServer( |
| 179 net::SpawnedTestServer::TYPE_HTTPS, | 261 net::SpawnedTestServer::TYPE_HTTPS, |
| 180 net::BaseTestServer::SSLOptions(net::BaseTestServer::SSLOptions::CERT_OK), | 262 net::BaseTestServer::SSLOptions(net::BaseTestServer::SSLOptions::CERT_OK), |
| 181 base::FilePath(FILE_PATH_LITERAL("content/test/data")))); | 263 base::FilePath(FILE_PATH_LITERAL("content/test/data")))); |
| 182 | 264 |
| 183 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 265 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 184 ASSERT_TRUE(https_server->Start()); | 266 ASSERT_TRUE(https_server->Start()); |
| 185 | 267 |
| 186 GURL test_url = | 268 GURL test_url = |
| 187 embedded_test_server()->GetURL("/manifest/dynamic-manifest.html"); | 269 embedded_test_server()->GetURL("/manifest/dynamic-manifest.html"); |
| 188 | 270 |
| 271 OverrideWebContentsDelegate(); | |
| 272 | |
| 189 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); | 273 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); |
| 190 shell()->LoadURL(test_url); | 274 shell()->LoadURL(test_url); |
| 191 navigation_observer.Wait(); | 275 navigation_observer.Wait(); |
| 192 | 276 |
| 193 std::string manifest_url = | 277 std::string manifest_url = |
| 194 https_server->GetURL("/manifest/dummy-manifest.json").spec(); | 278 https_server->GetURL("/manifest/dummy-manifest.json").spec(); |
| 195 ASSERT_TRUE(content::ExecuteScript(shell()->web_contents(), | 279 ASSERT_TRUE(content::ExecuteScript(shell()->web_contents(), |
| 196 "setManifestTo('" + manifest_url + "')")); | 280 "setManifestTo('" + manifest_url + "')")); |
| 197 | 281 |
| 198 GetManifestAndWait(); | 282 GetManifestAndWait(); |
| 199 EXPECT_TRUE(manifest().IsEmpty()); | 283 EXPECT_TRUE(manifest().IsEmpty()); |
| 284 | |
| 285 EXPECT_EQ(0u, console_error_count()); | |
| 286 } | |
| 287 | |
| 288 // If a page's manifest has some parsing errors, they should show up in the | |
| 289 // developer console. | |
| 290 IN_PROC_BROWSER_TEST_F(ManifestBrowserTest, ParsingErrorsManifest) { | |
| 291 GURL test_url = GetTestUrl("manifest", "parsing-errors.html"); | |
| 292 | |
| 293 OverrideWebContentsDelegate(); | |
| 294 | |
| 295 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); | |
| 296 shell()->LoadURL(test_url); | |
| 297 navigation_observer.Wait(); | |
| 298 | |
| 299 GetManifestAndWait(); | |
| 300 EXPECT_TRUE(manifest().IsEmpty()); | |
| 301 EXPECT_EQ(6u, console_error_count()); | |
| 200 } | 302 } |
| 201 | 303 |
| 202 } // namespace content | 304 } // namespace content |
| OLD | NEW |