| 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 if (!StartsWithASCII("foobar", it->second, true)) | 64 if (!StartsWithASCII("foobar", it->second, true)) |
| 64 return scoped_ptr<net::test_server::HttpResponse>(); | 65 return scoped_ptr<net::test_server::HttpResponse>(); |
| 65 | 66 |
| 66 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | 67 scoped_ptr<net::test_server::BasicHttpResponse> http_response( |
| 67 new net::test_server::BasicHttpResponse); | 68 new net::test_server::BasicHttpResponse); |
| 68 http_response->set_code(net::HTTP_MOVED_PERMANENTLY); | 69 http_response->set_code(net::HTTP_MOVED_PERMANENTLY); |
| 69 http_response->AddCustomHeader("Location", redirect_target.spec()); | 70 http_response->AddCustomHeader("Location", redirect_target.spec()); |
| 70 return http_response.PassAs<net::test_server::HttpResponse>(); | 71 return http_response.PassAs<net::test_server::HttpResponse>(); |
| 71 } | 72 } |
| 72 | 73 |
| 74 class WebContentsHiddenObserver : public content::WebContentsObserver { |
| 75 public: |
| 76 WebContentsHiddenObserver(content::WebContents* web_contents, |
| 77 const base::Closure& hidden_callback) |
| 78 : WebContentsObserver(web_contents), |
| 79 hidden_callback_(hidden_callback), |
| 80 hidden_observed_(false) { |
| 81 } |
| 82 |
| 83 // WebContentsObserver. |
| 84 virtual void WasHidden() OVERRIDE { |
| 85 hidden_observed_ = true; |
| 86 hidden_callback_.Run(); |
| 87 } |
| 88 |
| 89 bool hidden_observed() { return hidden_observed_; } |
| 90 |
| 91 private: |
| 92 base::Closure hidden_callback_; |
| 93 bool hidden_observed_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(WebContentsHiddenObserver); |
| 96 }; |
| 97 |
| 73 // Handles |request| by serving a redirect response. | 98 // Handles |request| by serving a redirect response. |
| 74 scoped_ptr<net::test_server::HttpResponse> RedirectResponseHandler( | 99 scoped_ptr<net::test_server::HttpResponse> RedirectResponseHandler( |
| 75 const std::string& path, | 100 const std::string& path, |
| 76 const GURL& redirect_target, | 101 const GURL& redirect_target, |
| 77 const net::test_server::HttpRequest& request) { | 102 const net::test_server::HttpRequest& request) { |
| 78 if (!StartsWithASCII(path, request.relative_url, true)) | 103 if (!StartsWithASCII(path, request.relative_url, true)) |
| 79 return scoped_ptr<net::test_server::HttpResponse>(); | 104 return scoped_ptr<net::test_server::HttpResponse>(); |
| 80 | 105 |
| 81 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | 106 scoped_ptr<net::test_server::BasicHttpResponse> http_response( |
| 82 new net::test_server::BasicHttpResponse); | 107 new net::test_server::BasicHttpResponse); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 113 net::FilePathToFileURL(test_data_dir).spec()); | 138 net::FilePathToFileURL(test_data_dir).spec()); |
| 114 | 139 |
| 115 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); | 140 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); |
| 116 | 141 |
| 117 ASSERT_TRUE(extension_system_->LoadApp(test_data_dir)); | 142 ASSERT_TRUE(extension_system_->LoadApp(test_data_dir)); |
| 118 extension_system_->LaunchApp(); | 143 extension_system_->LaunchApp(); |
| 119 | 144 |
| 120 ExtensionTestMessageListener launch_listener("LAUNCHED", false); | 145 ExtensionTestMessageListener launch_listener("LAUNCHED", false); |
| 121 ASSERT_TRUE(launch_listener.WaitUntilSatisfied()); | 146 ASSERT_TRUE(launch_listener.WaitUntilSatisfied()); |
| 122 | 147 |
| 148 embedder_web_contents_ = GetFirstAppWindowWebContents(); |
| 149 } |
| 150 |
| 151 content::WebContents* WebViewAPITest::GetFirstAppWindowWebContents() { |
| 123 const AppWindowRegistry::AppWindowList& app_window_list = | 152 const AppWindowRegistry::AppWindowList& app_window_list = |
| 124 AppWindowRegistry::Get(browser_context_)->app_windows(); | 153 AppWindowRegistry::Get(browser_context_)->app_windows(); |
| 125 DCHECK(app_window_list.size() == 1); | 154 DCHECK(app_window_list.size() == 1); |
| 126 embedder_web_contents_ = (*app_window_list.begin())->web_contents(); | 155 return (*app_window_list.begin())->web_contents(); |
| 127 } | 156 } |
| 128 | 157 |
| 129 void WebViewAPITest::RunTest(const std::string& test_name, | 158 void WebViewAPITest::RunTest(const std::string& test_name, |
| 130 const std::string& app_location) { | 159 const std::string& app_location) { |
| 131 LaunchApp(app_location); | 160 LaunchApp(app_location); |
| 132 | 161 |
| 133 ExtensionTestMessageListener done_listener("TEST_PASSED", false); | 162 ExtensionTestMessageListener done_listener("TEST_PASSED", false); |
| 134 done_listener.set_failure_message("TEST_FAILED"); | 163 done_listener.set_failure_message("TEST_FAILED"); |
| 135 if (!content::ExecuteScript( | 164 if (!content::ExecuteScript( |
| 136 embedder_web_contents_, | 165 embedder_web_contents_, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 LOG(ERROR) << "Failed to shutdown test server."; | 212 LOG(ERROR) << "Failed to shutdown test server."; |
| 184 } | 213 } |
| 185 } | 214 } |
| 186 | 215 |
| 187 void WebViewAPITest::TearDownOnMainThread() { | 216 void WebViewAPITest::TearDownOnMainThread() { |
| 188 TestGetConfigFunction::set_test_config_state(nullptr); | 217 TestGetConfigFunction::set_test_config_state(nullptr); |
| 189 | 218 |
| 190 AppShellTest::TearDownOnMainThread(); | 219 AppShellTest::TearDownOnMainThread(); |
| 191 } | 220 } |
| 192 | 221 |
| 222 void WebViewAPITest::SendMessageToEmbedder(const std::string& message) { |
| 223 EXPECT_TRUE( |
| 224 content::ExecuteScript( |
| 225 GetEmbedderWebContents(), |
| 226 base::StringPrintf("onAppCommand('%s');", message.c_str()))); |
| 227 } |
| 228 |
| 229 content::WebContents* WebViewAPITest::GetEmbedderWebContents() { |
| 230 if (!embedder_web_contents_) |
| 231 embedder_web_contents_ = GetFirstAppWindowWebContents(); |
| 232 return embedder_web_contents_; |
| 233 } |
| 234 |
| 193 TestGuestViewManager* WebViewAPITest::GetGuestViewManager() { | 235 TestGuestViewManager* WebViewAPITest::GetGuestViewManager() { |
| 194 return static_cast<TestGuestViewManager*>( | 236 return static_cast<TestGuestViewManager*>( |
| 195 TestGuestViewManager::FromBrowserContext( | 237 TestGuestViewManager::FromBrowserContext( |
| 196 ShellContentBrowserClient::Get()->GetBrowserContext())); | 238 ShellContentBrowserClient::Get()->GetBrowserContext())); |
| 197 } | 239 } |
| 198 | 240 |
| 241 void WebViewAPITest::SendMessageToGuestAndWait( |
| 242 const std::string& message, |
| 243 const std::string& wait_message) { |
| 244 scoped_ptr<ExtensionTestMessageListener> listener; |
| 245 if (!wait_message.empty()) |
| 246 listener.reset(new ExtensionTestMessageListener(wait_message, false)); |
| 247 |
| 248 EXPECT_TRUE( |
| 249 content::ExecuteScript( |
| 250 GetGuestWebContents(), |
| 251 base::StringPrintf("onAppCommand('%s');", message.c_str()))); |
| 252 |
| 253 if (listener) |
| 254 ASSERT_TRUE(listener->WaitUntilSatisfied()); |
| 255 } |
| 256 |
| 199 void WebViewDPIAPITest::SetUp() { | 257 void WebViewDPIAPITest::SetUp() { |
| 200 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 258 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 201 command_line->AppendSwitchASCII(switches::kForceDeviceScaleFactor, | 259 command_line->AppendSwitchASCII(switches::kForceDeviceScaleFactor, |
| 202 base::StringPrintf("%f", scale())); | 260 base::StringPrintf("%f", scale())); |
| 203 WebViewAPITest::SetUp(); | 261 WebViewAPITest::SetUp(); |
| 204 } | 262 } |
| 205 | 263 |
| 264 content::WebContents* WebViewAPITest::GetGuestWebContents() { |
| 265 return GetGuestViewManager()->WaitForSingleGuestCreated(); |
| 266 } |
| 267 |
| 268 IN_PROC_BROWSER_TEST_F(WebViewAPITest, AcceptTouchEvents) { |
| 269 LaunchApp("web_view/accept_touch_events"); |
| 270 |
| 271 content::RenderViewHost* embedder_rvh = |
| 272 GetEmbedderWebContents()->GetRenderViewHost(); |
| 273 |
| 274 bool embedder_has_touch_handler = |
| 275 content::RenderViewHostTester::HasTouchEventHandler(embedder_rvh); |
| 276 EXPECT_FALSE(embedder_has_touch_handler); |
| 277 |
| 278 SendMessageToGuestAndWait("install-touch-handler", "installed-touch-handler"); |
| 279 |
| 280 // Note that we need to wait for the installed/registered touch handler to |
| 281 // appear in browser process before querying |embedder_rvh|. |
| 282 // In practice, since we do a roundrtip from browser process to guest and |
| 283 // back, this is sufficient. |
| 284 embedder_has_touch_handler = |
| 285 content::RenderViewHostTester::HasTouchEventHandler(embedder_rvh); |
| 286 EXPECT_TRUE(embedder_has_touch_handler); |
| 287 |
| 288 SendMessageToGuestAndWait("uninstall-touch-handler", |
| 289 "uninstalled-touch-handler"); |
| 290 // Same as the note above about waiting. |
| 291 embedder_has_touch_handler = |
| 292 content::RenderViewHostTester::HasTouchEventHandler(embedder_rvh); |
| 293 EXPECT_FALSE(embedder_has_touch_handler); |
| 294 } |
| 295 |
| 296 // This test verifies that hiding the embedder also hides the guest. |
| 297 IN_PROC_BROWSER_TEST_F(WebViewAPITest, EmbedderVisibilityChanged) { |
| 298 LaunchApp("web_view/visibility_changed"); |
| 299 |
| 300 scoped_refptr<content::MessageLoopRunner> loop_runner( |
| 301 new content::MessageLoopRunner); |
| 302 WebContentsHiddenObserver observer(GetGuestWebContents(), |
| 303 loop_runner->QuitClosure()); |
| 304 |
| 305 // Handled in web_view/visibility_changed/main.js |
| 306 SendMessageToEmbedder("hide-embedder"); |
| 307 if (!observer.hidden_observed()) |
| 308 loop_runner->Run(); |
| 309 } |
| 310 |
| 311 // This test verifies that hiding the guest triggers WebContents::WasHidden(). |
| 312 IN_PROC_BROWSER_TEST_F(WebViewAPITest, GuestVisibilityChanged) { |
| 313 LaunchApp("web_view/visibility_changed"); |
| 314 |
| 315 scoped_refptr<content::MessageLoopRunner> loop_runner( |
| 316 new content::MessageLoopRunner); |
| 317 WebContentsHiddenObserver observer(GetGuestWebContents(), |
| 318 loop_runner->QuitClosure()); |
| 319 |
| 320 // Handled in web_view/visibility_changed/main.js |
| 321 SendMessageToEmbedder("hide-guest"); |
| 322 if (!observer.hidden_observed()) |
| 323 loop_runner->Run(); |
| 324 } |
| 325 |
| 326 // This test verifies that reloading the embedder reloads the guest (and doest |
| 327 // not crash). |
| 328 IN_PROC_BROWSER_TEST_F(WebViewAPITest, ReloadEmbedder) { |
| 329 // Just load a guest from other test, we do not want to add a separate |
| 330 // app for this test. |
| 331 LaunchApp("web_view/visibility_changed"); |
| 332 |
| 333 ExtensionTestMessageListener launched_again_listener("LAUNCHED", false); |
| 334 embedder_web_contents_->GetController().Reload(false); |
| 335 ASSERT_TRUE(launched_again_listener.WaitUntilSatisfied()); |
| 336 } |
| 337 |
| 206 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAllowTransparencyAttribute) { | 338 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAllowTransparencyAttribute) { |
| 207 RunTest("testAllowTransparencyAttribute", "web_view/apitest"); | 339 RunTest("testAllowTransparencyAttribute", "web_view/apitest"); |
| 208 } | 340 } |
| 209 | 341 |
| 210 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAPIMethodExistence) { | 342 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAPIMethodExistence) { |
| 211 RunTest("testAPIMethodExistence", "web_view/apitest"); | 343 RunTest("testAPIMethodExistence", "web_view/apitest"); |
| 212 } | 344 } |
| 213 | 345 |
| 214 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAssignSrcAfterCrash) { | 346 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAssignSrcAfterCrash) { |
| 215 RunTest("testAssignSrcAfterCrash", "web_view/apitest"); | 347 RunTest("testAssignSrcAfterCrash", "web_view/apitest"); |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 GURL::Replacements replace_host; | 574 GURL::Replacements replace_host; |
| 443 std::string host_str("localhost"); // Must stay in scope with replace_host. | 575 std::string host_str("localhost"); // Must stay in scope with replace_host. |
| 444 replace_host.SetHostStr(host_str); | 576 replace_host.SetHostStr(host_str); |
| 445 | 577 |
| 446 // Run the test and wait until the guest WebContents is available and has | 578 // Run the test and wait until the guest WebContents is available and has |
| 447 // finished loading. | 579 // finished loading. |
| 448 ExtensionTestMessageListener guest_loaded_listener("guest-loaded", false); | 580 ExtensionTestMessageListener guest_loaded_listener("guest-loaded", false); |
| 449 EXPECT_TRUE(content::ExecuteScript(embedder_web_contents_, | 581 EXPECT_TRUE(content::ExecuteScript(embedder_web_contents_, |
| 450 "runTest('testRemoveWebviewOnExit')")); | 582 "runTest('testRemoveWebviewOnExit')")); |
| 451 | 583 |
| 452 content::WebContents* guest_web_contents = | 584 content::WebContents* guest_web_contents = GetGuestWebContents(); |
| 453 GetGuestViewManager()->WaitForSingleGuestCreated(); | |
| 454 EXPECT_TRUE(guest_web_contents->GetRenderProcessHost()->IsIsolatedGuest()); | 585 EXPECT_TRUE(guest_web_contents->GetRenderProcessHost()->IsIsolatedGuest()); |
| 455 ASSERT_TRUE(guest_loaded_listener.WaitUntilSatisfied()); | 586 ASSERT_TRUE(guest_loaded_listener.WaitUntilSatisfied()); |
| 456 | 587 |
| 457 content::WebContentsDestroyedWatcher destroyed_watcher(guest_web_contents); | 588 content::WebContentsDestroyedWatcher destroyed_watcher(guest_web_contents); |
| 458 | 589 |
| 459 // Tell the embedder to kill the guest. | 590 // Tell the embedder to kill the guest. |
| 460 EXPECT_TRUE(content::ExecuteScript(embedder_web_contents_, | 591 EXPECT_TRUE(content::ExecuteScript(embedder_web_contents_, |
| 461 "removeWebviewOnExitDoCrash()")); | 592 "removeWebviewOnExitDoCrash()")); |
| 462 | 593 |
| 463 // Wait until the guest WebContents is destroyed. | 594 // Wait until the guest WebContents is destroyed. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 // object, on the webview element, and hanging directly off webview. | 639 // object, on the webview element, and hanging directly off webview. |
| 509 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIExistence) { | 640 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIExistence) { |
| 510 RunTest("testWebRequestAPIExistence", "web_view/apitest"); | 641 RunTest("testWebRequestAPIExistence", "web_view/apitest"); |
| 511 } | 642 } |
| 512 | 643 |
| 513 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIGoogleProperty) { | 644 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIGoogleProperty) { |
| 514 RunTest("testWebRequestAPIGoogleProperty", "web_view/apitest"); | 645 RunTest("testWebRequestAPIGoogleProperty", "web_view/apitest"); |
| 515 } | 646 } |
| 516 | 647 |
| 517 } // namespace extensions | 648 } // namespace extensions |
| OLD | NEW |