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 "extensions/browser/guest_view/web_view/web_view_apitest.h" | 5 #include "extensions/browser/guest_view/web_view/web_view_apitest.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "content/public/browser/render_process_host.h" | 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "content/public/test/browser_test_utils.h" | 12 #include "content/public/test/browser_test_utils.h" |
| 13 #include "content/public/test/test_renderer_host.h" | |
| 13 #include "content/public/test/test_utils.h" | 14 #include "content/public/test/test_utils.h" |
| 14 #include "extensions/browser/api/test/test_api.h" | 15 #include "extensions/browser/api/test/test_api.h" |
| 15 #include "extensions/browser/app_window/app_window.h" | 16 #include "extensions/browser/app_window/app_window.h" |
| 16 #include "extensions/browser/app_window/app_window_registry.h" | 17 #include "extensions/browser/app_window/app_window_registry.h" |
| 17 #include "extensions/browser/extension_host.h" | 18 #include "extensions/browser/extension_host.h" |
| 18 #include "extensions/browser/guest_view/guest_view_manager.h" | 19 #include "extensions/browser/guest_view/guest_view_manager.h" |
| 19 #include "extensions/browser/guest_view/guest_view_manager_factory.h" | 20 #include "extensions/browser/guest_view/guest_view_manager_factory.h" |
| 20 #include "extensions/browser/guest_view/test_guest_view_manager.h" | 21 #include "extensions/browser/guest_view/test_guest_view_manager.h" |
| 21 #include "extensions/browser/process_manager.h" | 22 #include "extensions/browser/process_manager.h" |
| 22 #include "extensions/common/extension.h" | 23 #include "extensions/common/extension.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 40 const char kTestServerPort[] = "testServer.port"; | 41 const char kTestServerPort[] = "testServer.port"; |
| 41 const char kTestWebSocketPort[] = "testWebSocketPort"; | 42 const char kTestWebSocketPort[] = "testWebSocketPort"; |
| 42 | 43 |
| 43 class EmptyHttpResponse : public net::test_server::HttpResponse { | 44 class EmptyHttpResponse : public net::test_server::HttpResponse { |
| 44 public: | 45 public: |
| 45 virtual std::string ToResponseString() const override { | 46 virtual std::string ToResponseString() const override { |
| 46 return std::string(); | 47 return std::string(); |
| 47 } | 48 } |
| 48 }; | 49 }; |
| 49 | 50 |
| 51 class WebContentsHiddenObserver : public content::WebContentsObserver { | |
| 52 public: | |
| 53 WebContentsHiddenObserver(content::WebContents* web_contents, | |
| 54 const base::Closure& hidden_callback) | |
| 55 : WebContentsObserver(web_contents), | |
| 56 hidden_callback_(hidden_callback), | |
| 57 hidden_observed_(false) { | |
| 58 } | |
| 59 | |
| 60 // WebContentsObserver. | |
| 61 virtual void WasHidden() OVERRIDE { | |
| 62 hidden_observed_ = true; | |
| 63 hidden_callback_.Run(); | |
| 64 } | |
| 65 | |
| 66 bool hidden_observed() { return hidden_observed_; } | |
| 67 | |
| 68 private: | |
| 69 base::Closure hidden_callback_; | |
| 70 bool hidden_observed_; | |
| 71 | |
| 72 DISALLOW_COPY_AND_ASSIGN(WebContentsHiddenObserver); | |
| 73 }; | |
| 74 | |
| 50 // Handles |request| by serving a redirect response. | 75 // Handles |request| by serving a redirect response. |
| 51 scoped_ptr<net::test_server::HttpResponse> RedirectResponseHandler( | 76 scoped_ptr<net::test_server::HttpResponse> RedirectResponseHandler( |
| 52 const std::string& path, | 77 const std::string& path, |
| 53 const GURL& redirect_target, | 78 const GURL& redirect_target, |
| 54 const net::test_server::HttpRequest& request) { | 79 const net::test_server::HttpRequest& request) { |
| 55 if (!StartsWithASCII(path, request.relative_url, true)) | 80 if (!StartsWithASCII(path, request.relative_url, true)) |
| 56 return scoped_ptr<net::test_server::HttpResponse>(); | 81 return scoped_ptr<net::test_server::HttpResponse>(); |
| 57 | 82 |
| 58 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | 83 scoped_ptr<net::test_server::BasicHttpResponse> http_response( |
| 59 new net::test_server::BasicHttpResponse); | 84 new net::test_server::BasicHttpResponse); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 90 net::FilePathToFileURL(test_data_dir).spec()); | 115 net::FilePathToFileURL(test_data_dir).spec()); |
| 91 | 116 |
| 92 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); | 117 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); |
| 93 | 118 |
| 94 ASSERT_TRUE(extension_system_->LoadApp(test_data_dir)); | 119 ASSERT_TRUE(extension_system_->LoadApp(test_data_dir)); |
| 95 extension_system_->LaunchApp(); | 120 extension_system_->LaunchApp(); |
| 96 | 121 |
| 97 ExtensionTestMessageListener launch_listener("LAUNCHED", false); | 122 ExtensionTestMessageListener launch_listener("LAUNCHED", false); |
| 98 ASSERT_TRUE(launch_listener.WaitUntilSatisfied()); | 123 ASSERT_TRUE(launch_listener.WaitUntilSatisfied()); |
| 99 | 124 |
| 125 embedder_web_contents_ = GetFirstAppWindowWebContents(); | |
| 126 } | |
| 127 | |
| 128 content::WebContents* WebViewAPITest::GetFirstAppWindowWebContents() { | |
| 100 const AppWindowRegistry::AppWindowList& app_window_list = | 129 const AppWindowRegistry::AppWindowList& app_window_list = |
| 101 AppWindowRegistry::Get(browser_context_)->app_windows(); | 130 AppWindowRegistry::Get(browser_context_)->app_windows(); |
| 102 DCHECK(app_window_list.size() == 1); | 131 DCHECK(app_window_list.size() == 1); |
| 103 embedder_web_contents_ = (*app_window_list.begin())->web_contents(); | 132 return (*app_window_list.begin())->web_contents(); |
| 104 } | 133 } |
| 105 | 134 |
| 106 void WebViewAPITest::RunTest(const std::string& test_name, | 135 void WebViewAPITest::RunTest(const std::string& test_name, |
| 107 const std::string& app_location) { | 136 const std::string& app_location) { |
| 108 LaunchApp(app_location); | 137 LaunchApp(app_location); |
| 109 | 138 |
| 110 ExtensionTestMessageListener done_listener("TEST_PASSED", false); | 139 ExtensionTestMessageListener done_listener("TEST_PASSED", false); |
| 111 done_listener.set_failure_message("TEST_FAILED"); | 140 done_listener.set_failure_message("TEST_FAILED"); |
| 112 if (!content::ExecuteScript( | 141 if (!content::ExecuteScript( |
| 113 embedder_web_contents_, | 142 embedder_web_contents_, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 LOG(ERROR) << "Failed to shutdown test server."; | 183 LOG(ERROR) << "Failed to shutdown test server."; |
| 155 } | 184 } |
| 156 } | 185 } |
| 157 | 186 |
| 158 void WebViewAPITest::TearDownOnMainThread() { | 187 void WebViewAPITest::TearDownOnMainThread() { |
| 159 TestGetConfigFunction::set_test_config_state(nullptr); | 188 TestGetConfigFunction::set_test_config_state(nullptr); |
| 160 | 189 |
| 161 AppShellTest::TearDownOnMainThread(); | 190 AppShellTest::TearDownOnMainThread(); |
| 162 } | 191 } |
| 163 | 192 |
| 193 void WebViewAPITest::SendMessageToEmbedder(const std::string& message) { | |
| 194 EXPECT_TRUE( | |
| 195 content::ExecuteScript( | |
| 196 GetEmbedderWebContents(), | |
| 197 base::StringPrintf("onAppCommand('%s');", message.c_str()))); | |
| 198 } | |
| 199 | |
| 200 content::WebContents* WebViewAPITest::GetEmbedderWebContents() { | |
| 201 if (!embedder_web_contents_) { | |
|
lazyboy
2014/10/10 02:24:56
nit: here and in other places below: single statem
| |
| 202 embedder_web_contents_ = GetFirstAppWindowWebContents(); | |
| 203 } | |
| 204 return embedder_web_contents_; | |
| 205 } | |
| 206 | |
| 164 TestGuestViewManager* WebViewAPITest::GetGuestViewManager() { | 207 TestGuestViewManager* WebViewAPITest::GetGuestViewManager() { |
| 165 return static_cast<TestGuestViewManager*>( | 208 return static_cast<TestGuestViewManager*>( |
| 166 TestGuestViewManager::FromBrowserContext( | 209 TestGuestViewManager::FromBrowserContext( |
| 167 ShellContentBrowserClient::Get()->GetBrowserContext())); | 210 ShellContentBrowserClient::Get()->GetBrowserContext())); |
| 168 } | 211 } |
| 169 | 212 |
| 213 void WebViewAPITest::SendMessageToGuestAndWait( | |
| 214 const std::string& message, | |
| 215 const std::string& wait_message) { | |
| 216 scoped_ptr<ExtensionTestMessageListener> listener; | |
| 217 if (!wait_message.empty()) { | |
| 218 listener.reset(new ExtensionTestMessageListener(wait_message, false)); | |
| 219 } | |
| 220 | |
| 221 EXPECT_TRUE( | |
| 222 content::ExecuteScript( | |
| 223 GetGuestWebContents(), | |
| 224 base::StringPrintf("onAppCommand('%s');", message.c_str()))); | |
| 225 | |
| 226 if (listener) { | |
| 227 ASSERT_TRUE(listener->WaitUntilSatisfied()); | |
| 228 } | |
| 229 } | |
| 230 | |
| 170 void WebViewDPIAPITest::SetUp() { | 231 void WebViewDPIAPITest::SetUp() { |
| 171 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 232 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 172 command_line->AppendSwitchASCII(switches::kForceDeviceScaleFactor, | 233 command_line->AppendSwitchASCII(switches::kForceDeviceScaleFactor, |
| 173 base::StringPrintf("%f", scale())); | 234 base::StringPrintf("%f", scale())); |
| 174 WebViewAPITest::SetUp(); | 235 WebViewAPITest::SetUp(); |
| 175 } | 236 } |
| 176 | 237 |
| 238 content::WebContents* WebViewAPITest::GetGuestWebContents() { | |
| 239 return GetGuestViewManager()->WaitForSingleGuestCreated(); | |
| 240 } | |
| 241 | |
| 242 IN_PROC_BROWSER_TEST_F(WebViewAPITest, AcceptTouchEvents) { | |
| 243 LaunchApp("web_view/accept_touch_events"); | |
| 244 | |
| 245 content::RenderViewHost* embedder_rvh = | |
| 246 GetEmbedderWebContents()->GetRenderViewHost(); | |
| 247 | |
| 248 bool embedder_has_touch_handler = | |
| 249 content::RenderViewHostTester::HasTouchEventHandler(embedder_rvh); | |
| 250 EXPECT_FALSE(embedder_has_touch_handler); | |
| 251 | |
| 252 SendMessageToGuestAndWait("install-touch-handler", "installed-touch-handler"); | |
| 253 | |
| 254 // Note that we need to wait for the installed/registered touch handler to | |
| 255 // appear in browser process before querying |embedder_rvh|. | |
| 256 // In practice, since we do a roundrtip from browser process to guest and | |
| 257 // back, this is sufficient. | |
| 258 embedder_has_touch_handler = | |
| 259 content::RenderViewHostTester::HasTouchEventHandler(embedder_rvh); | |
| 260 EXPECT_TRUE(embedder_has_touch_handler); | |
| 261 | |
| 262 SendMessageToGuestAndWait("uninstall-touch-handler", | |
| 263 "uninstalled-touch-handler"); | |
| 264 // Same as the note above about waiting. | |
| 265 embedder_has_touch_handler = | |
| 266 content::RenderViewHostTester::HasTouchEventHandler(embedder_rvh); | |
| 267 EXPECT_FALSE(embedder_has_touch_handler); | |
| 268 } | |
| 269 | |
| 270 // This test verifies that hiding the embedder also hides the guest. | |
| 271 IN_PROC_BROWSER_TEST_F(WebViewAPITest, EmbedderVisibilityChanged) { | |
| 272 LaunchApp("web_view/visibility_changed"); | |
| 273 | |
| 274 scoped_refptr<content::MessageLoopRunner> loop_runner( | |
| 275 new content::MessageLoopRunner); | |
| 276 WebContentsHiddenObserver observer(GetGuestWebContents(), | |
| 277 loop_runner->QuitClosure()); | |
| 278 | |
| 279 // Handled in web_view/visibility_changed/main.js | |
| 280 SendMessageToEmbedder("hide-embedder"); | |
| 281 if (!observer.hidden_observed()) | |
| 282 loop_runner->Run(); | |
| 283 } | |
| 284 | |
| 285 // This test verifies that hiding the guest triggers WebContents::WasHidden(). | |
| 286 IN_PROC_BROWSER_TEST_F(WebViewAPITest, GuestVisibilityChanged) { | |
| 287 LaunchApp("web_view/visibility_changed"); | |
| 288 | |
| 289 scoped_refptr<content::MessageLoopRunner> loop_runner( | |
| 290 new content::MessageLoopRunner); | |
| 291 WebContentsHiddenObserver observer(GetGuestWebContents(), | |
| 292 loop_runner->QuitClosure()); | |
| 293 | |
| 294 // Handled in web_view/visibility_changed/main.js | |
| 295 SendMessageToEmbedder("hide-guest"); | |
| 296 if (!observer.hidden_observed()) | |
| 297 loop_runner->Run(); | |
| 298 } | |
| 299 | |
| 300 // This test verifies that reloading the embedder reloads the guest (and doest | |
| 301 // not crash). | |
| 302 IN_PROC_BROWSER_TEST_F(WebViewAPITest, ReloadEmbedder) { | |
| 303 // Just load a guest from other test, we do not want to add a separate | |
| 304 // app for this test. | |
| 305 LaunchApp("web_view/visibility_changed"); | |
| 306 | |
| 307 ExtensionTestMessageListener launched_again_listener("LAUNCHED", false); | |
| 308 embedder_web_contents_->GetController().Reload(false); | |
| 309 ASSERT_TRUE(launched_again_listener.WaitUntilSatisfied()); | |
| 310 } | |
| 311 | |
| 177 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAllowTransparencyAttribute) { | 312 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAllowTransparencyAttribute) { |
| 178 RunTest("testAllowTransparencyAttribute", "web_view/apitest"); | 313 RunTest("testAllowTransparencyAttribute", "web_view/apitest"); |
| 179 } | 314 } |
| 180 | 315 |
| 181 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAPIMethodExistence) { | 316 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAPIMethodExistence) { |
| 182 RunTest("testAPIMethodExistence", "web_view/apitest"); | 317 RunTest("testAPIMethodExistence", "web_view/apitest"); |
| 183 } | 318 } |
| 184 | 319 |
| 185 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAssignSrcAfterCrash) { | 320 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAssignSrcAfterCrash) { |
| 186 RunTest("testAssignSrcAfterCrash", "web_view/apitest"); | 321 RunTest("testAssignSrcAfterCrash", "web_view/apitest"); |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 413 GURL::Replacements replace_host; | 548 GURL::Replacements replace_host; |
| 414 std::string host_str("localhost"); // Must stay in scope with replace_host. | 549 std::string host_str("localhost"); // Must stay in scope with replace_host. |
| 415 replace_host.SetHostStr(host_str); | 550 replace_host.SetHostStr(host_str); |
| 416 | 551 |
| 417 // Run the test and wait until the guest WebContents is available and has | 552 // Run the test and wait until the guest WebContents is available and has |
| 418 // finished loading. | 553 // finished loading. |
| 419 ExtensionTestMessageListener guest_loaded_listener("guest-loaded", false); | 554 ExtensionTestMessageListener guest_loaded_listener("guest-loaded", false); |
| 420 EXPECT_TRUE(content::ExecuteScript(embedder_web_contents_, | 555 EXPECT_TRUE(content::ExecuteScript(embedder_web_contents_, |
| 421 "runTest('testRemoveWebviewOnExit')")); | 556 "runTest('testRemoveWebviewOnExit')")); |
| 422 | 557 |
| 423 content::WebContents* guest_web_contents = | 558 content::WebContents* guest_web_contents = GetGuestWebContents(); |
| 424 GetGuestViewManager()->WaitForSingleGuestCreated(); | |
| 425 EXPECT_TRUE(guest_web_contents->GetRenderProcessHost()->IsIsolatedGuest()); | 559 EXPECT_TRUE(guest_web_contents->GetRenderProcessHost()->IsIsolatedGuest()); |
| 426 ASSERT_TRUE(guest_loaded_listener.WaitUntilSatisfied()); | 560 ASSERT_TRUE(guest_loaded_listener.WaitUntilSatisfied()); |
| 427 | 561 |
| 428 content::WebContentsDestroyedWatcher destroyed_watcher(guest_web_contents); | 562 content::WebContentsDestroyedWatcher destroyed_watcher(guest_web_contents); |
| 429 | 563 |
| 430 // Tell the embedder to kill the guest. | 564 // Tell the embedder to kill the guest. |
| 431 EXPECT_TRUE(content::ExecuteScript(embedder_web_contents_, | 565 EXPECT_TRUE(content::ExecuteScript(embedder_web_contents_, |
| 432 "removeWebviewOnExitDoCrash()")); | 566 "removeWebviewOnExitDoCrash()")); |
| 433 | 567 |
| 434 // Wait until the guest WebContents is destroyed. | 568 // Wait until the guest WebContents is destroyed. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 473 // object, on the webview element, and hanging directly off webview. | 607 // object, on the webview element, and hanging directly off webview. |
| 474 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIExistence) { | 608 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIExistence) { |
| 475 RunTest("testWebRequestAPIExistence", "web_view/apitest"); | 609 RunTest("testWebRequestAPIExistence", "web_view/apitest"); |
| 476 } | 610 } |
| 477 | 611 |
| 478 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIGoogleProperty) { | 612 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIGoogleProperty) { |
| 479 RunTest("testWebRequestAPIGoogleProperty", "web_view/apitest"); | 613 RunTest("testWebRequestAPIGoogleProperty", "web_view/apitest"); |
| 480 } | 614 } |
| 481 | 615 |
| 482 } // namespace extensions | 616 } // namespace extensions |
| OLD | NEW |