| 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" |
| 6 |
| 5 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 6 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 7 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 8 #include "content/public/browser/render_process_host.h" | 10 #include "content/public/browser/render_process_host.h" |
| 9 #include "content/public/test/browser_test_utils.h" | 11 #include "content/public/test/browser_test_utils.h" |
| 10 #include "content/public/test/test_utils.h" | 12 #include "content/public/test/test_utils.h" |
| 11 #include "extensions/browser/api/test/test_api.h" | 13 #include "extensions/browser/api/test/test_api.h" |
| 12 #include "extensions/browser/app_window/app_window.h" | 14 #include "extensions/browser/app_window/app_window.h" |
| 13 #include "extensions/browser/app_window/app_window_registry.h" | 15 #include "extensions/browser/app_window/app_window_registry.h" |
| 14 #include "extensions/browser/extension_host.h" | 16 #include "extensions/browser/extension_host.h" |
| 15 #include "extensions/browser/guest_view/guest_view_manager.h" | 17 #include "extensions/browser/guest_view/guest_view_manager.h" |
| 16 #include "extensions/browser/guest_view/guest_view_manager_factory.h" | 18 #include "extensions/browser/guest_view/guest_view_manager_factory.h" |
| 17 #include "extensions/browser/guest_view/web_view/test_guest_view_manager.h" | 19 #include "extensions/browser/guest_view/test_guest_view_manager.h" |
| 18 #include "extensions/browser/process_manager.h" | 20 #include "extensions/browser/process_manager.h" |
| 19 #include "extensions/common/extension.h" | 21 #include "extensions/common/extension.h" |
| 20 #include "extensions/common/extension_paths.h" | 22 #include "extensions/common/extension_paths.h" |
| 21 #include "extensions/shell/browser/shell_content_browser_client.h" | 23 #include "extensions/shell/browser/shell_content_browser_client.h" |
| 22 #include "extensions/shell/browser/shell_extension_system.h" | 24 #include "extensions/shell/browser/shell_extension_system.h" |
| 23 #include "extensions/shell/test/shell_test.h" | 25 #include "extensions/shell/test/shell_test.h" |
| 24 #include "extensions/test/extension_test_message_listener.h" | 26 #include "extensions/test/extension_test_message_listener.h" |
| 25 #include "net/base/filename_util.h" | 27 #include "net/base/filename_util.h" |
| 26 #include "net/test/embedded_test_server/embedded_test_server.h" | 28 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 27 #include "net/test/embedded_test_server/http_request.h" | 29 #include "net/test/embedded_test_server/http_request.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 return scoped_ptr<net::test_server::HttpResponse>(new EmptyHttpResponse); | 68 return scoped_ptr<net::test_server::HttpResponse>(new EmptyHttpResponse); |
| 67 } | 69 } |
| 68 | 70 |
| 69 return scoped_ptr<net::test_server::HttpResponse>(); | 71 return scoped_ptr<net::test_server::HttpResponse>(); |
| 70 } | 72 } |
| 71 | 73 |
| 72 } // namespace | 74 } // namespace |
| 73 | 75 |
| 74 namespace extensions { | 76 namespace extensions { |
| 75 | 77 |
| 76 // This class intercepts download request from the guest. | 78 WebViewAPITest::WebViewAPITest() { |
| 77 class WebViewAPITest : public AppShellTest { | 79 GuestViewManager::set_factory_for_testing(&factory_); |
| 78 protected: | 80 } |
| 79 void LaunchApp(const std::string& app_location) { | |
| 80 base::FilePath test_data_dir; | |
| 81 PathService::Get(DIR_TEST_DATA, &test_data_dir); | |
| 82 test_data_dir = test_data_dir.AppendASCII(app_location.c_str()); | |
| 83 | 81 |
| 84 test_config_.SetString(kTestDataDirectory, | 82 void WebViewAPITest::LaunchApp(const std::string& app_location) { |
| 85 net::FilePathToFileURL(test_data_dir).spec()); | 83 base::FilePath test_data_dir; |
| 84 PathService::Get(DIR_TEST_DATA, &test_data_dir); |
| 85 test_data_dir = test_data_dir.AppendASCII(app_location.c_str()); |
| 86 | 86 |
| 87 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); | 87 test_config_.SetString(kTestDataDirectory, |
| 88 net::FilePathToFileURL(test_data_dir).spec()); |
| 88 | 89 |
| 89 ASSERT_TRUE(extension_system_->LoadApp(test_data_dir)); | 90 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); |
| 90 extension_system_->LaunchApp(); | |
| 91 | 91 |
| 92 ExtensionTestMessageListener launch_listener("LAUNCHED", false); | 92 ASSERT_TRUE(extension_system_->LoadApp(test_data_dir)); |
| 93 ASSERT_TRUE(launch_listener.WaitUntilSatisfied()); | 93 extension_system_->LaunchApp(); |
| 94 | 94 |
| 95 const AppWindowRegistry::AppWindowList& app_window_list = | 95 ExtensionTestMessageListener launch_listener("LAUNCHED", false); |
| 96 AppWindowRegistry::Get(browser_context_)->app_windows(); | 96 ASSERT_TRUE(launch_listener.WaitUntilSatisfied()); |
| 97 DCHECK(app_window_list.size() == 1); | 97 |
| 98 embedder_web_contents_ = (*app_window_list.begin())->web_contents(); | 98 const AppWindowRegistry::AppWindowList& app_window_list = |
| 99 AppWindowRegistry::Get(browser_context_)->app_windows(); |
| 100 DCHECK(app_window_list.size() == 1); |
| 101 embedder_web_contents_ = (*app_window_list.begin())->web_contents(); |
| 102 } |
| 103 |
| 104 void WebViewAPITest::RunTest(const std::string& test_name, |
| 105 const std::string& app_location) { |
| 106 LaunchApp(app_location); |
| 107 |
| 108 ExtensionTestMessageListener done_listener("TEST_PASSED", false); |
| 109 done_listener.set_failure_message("TEST_FAILED"); |
| 110 if (!content::ExecuteScript( |
| 111 embedder_web_contents_, |
| 112 base::StringPrintf("runTest('%s')", test_name.c_str()))) { |
| 113 LOG(ERROR) << "Unable to start test."; |
| 114 return; |
| 115 } |
| 116 ASSERT_TRUE(done_listener.WaitUntilSatisfied()); |
| 117 } |
| 118 |
| 119 void WebViewAPITest::SetUpOnMainThread() { |
| 120 AppShellTest::SetUpOnMainThread(); |
| 121 |
| 122 TestGetConfigFunction::set_test_config_state(&test_config_); |
| 123 base::FilePath test_data_dir; |
| 124 test_config_.SetInteger(kTestWebSocketPort, 0); |
| 125 } |
| 126 |
| 127 void WebViewAPITest::StartTestServer() { |
| 128 // For serving guest pages. |
| 129 if (!embedded_test_server()->InitializeAndWaitUntilReady()) { |
| 130 LOG(ERROR) << "Failed to start test server."; |
| 131 return; |
| 99 } | 132 } |
| 100 | 133 |
| 101 void RunTest(const std::string& test_name, const std::string& app_location) { | 134 test_config_.SetInteger(kTestServerPort, embedded_test_server()->port()); |
| 102 LaunchApp(app_location); | |
| 103 | 135 |
| 104 ExtensionTestMessageListener done_listener("TEST_PASSED", false); | 136 embedded_test_server()->RegisterRequestHandler( |
| 105 done_listener.set_failure_message("TEST_FAILED"); | 137 base::Bind(&RedirectResponseHandler, |
| 106 if (!content::ExecuteScript( | 138 kRedirectResponsePath, |
| 107 embedder_web_contents_, | 139 embedded_test_server()->GetURL(kRedirectResponseFullPath))); |
| 108 base::StringPrintf("runTest('%s')", test_name.c_str()))) { | 140 |
| 109 LOG(ERROR) << "Unable to start test."; | 141 embedded_test_server()->RegisterRequestHandler( |
| 110 return; | 142 base::Bind(&EmptyResponseHandler, kEmptyResponsePath)); |
| 111 } | 143 } |
| 112 ASSERT_TRUE(done_listener.WaitUntilSatisfied()); | 144 |
| 145 void WebViewAPITest::StopTestServer() { |
| 146 if (!embedded_test_server()->ShutdownAndWaitUntilComplete()) { |
| 147 LOG(ERROR) << "Failed to shutdown test server."; |
| 113 } | 148 } |
| 149 } |
| 114 | 150 |
| 115 virtual void SetUpOnMainThread() override { | 151 void WebViewAPITest::TearDownOnMainThread() { |
| 116 AppShellTest::SetUpOnMainThread(); | 152 TestGetConfigFunction::set_test_config_state(nullptr); |
| 117 | 153 |
| 118 TestGetConfigFunction::set_test_config_state(&test_config_); | 154 AppShellTest::TearDownOnMainThread(); |
| 119 base::FilePath test_data_dir; | 155 } |
| 120 test_config_.SetInteger(kTestWebSocketPort, 0); | |
| 121 } | |
| 122 | 156 |
| 123 void StartTestServer() { | 157 TestGuestViewManager* WebViewAPITest::GetGuestViewManager() { |
| 124 // For serving guest pages. | 158 return static_cast<TestGuestViewManager*>( |
| 125 if (!embedded_test_server()->InitializeAndWaitUntilReady()) { | 159 TestGuestViewManager::FromBrowserContext( |
| 126 LOG(ERROR) << "Failed to start test server."; | 160 ShellContentBrowserClient::Get()->GetBrowserContext())); |
| 127 return; | 161 } |
| 128 } | |
| 129 | |
| 130 test_config_.SetInteger(kTestServerPort, embedded_test_server()->port()); | |
| 131 | |
| 132 embedded_test_server()->RegisterRequestHandler( | |
| 133 base::Bind(&RedirectResponseHandler, | |
| 134 kRedirectResponsePath, | |
| 135 embedded_test_server()->GetURL(kRedirectResponseFullPath))); | |
| 136 | |
| 137 embedded_test_server()->RegisterRequestHandler( | |
| 138 base::Bind(&EmptyResponseHandler, kEmptyResponsePath)); | |
| 139 } | |
| 140 | |
| 141 void StopTestServer() { | |
| 142 if (!embedded_test_server()->ShutdownAndWaitUntilComplete()) { | |
| 143 LOG(ERROR) << "Failed to shutdown test server."; | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 virtual void TearDownOnMainThread() override { | |
| 148 TestGetConfigFunction::set_test_config_state(nullptr); | |
| 149 | |
| 150 AppShellTest::TearDownOnMainThread(); | |
| 151 } | |
| 152 | |
| 153 TestGuestViewManager* GetGuestViewManager() { | |
| 154 return static_cast<TestGuestViewManager*>( | |
| 155 TestGuestViewManager::FromBrowserContext( | |
| 156 ShellContentBrowserClient::Get()->GetBrowserContext())); | |
| 157 } | |
| 158 | |
| 159 WebViewAPITest() { GuestViewManager::set_factory_for_testing(&factory_); } | |
| 160 | |
| 161 protected: | |
| 162 content::WebContents* embedder_web_contents_; | |
| 163 TestGuestViewManagerFactory factory_; | |
| 164 base::DictionaryValue test_config_; | |
| 165 }; | |
| 166 | 162 |
| 167 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAllowTransparencyAttribute) { | 163 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAllowTransparencyAttribute) { |
| 168 RunTest("testAllowTransparencyAttribute", "web_view/apitest"); | 164 RunTest("testAllowTransparencyAttribute", "web_view/apitest"); |
| 169 } | 165 } |
| 170 | 166 |
| 171 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAPIMethodExistence) { | 167 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAPIMethodExistence) { |
| 172 RunTest("testAPIMethodExistence", "web_view/apitest"); | 168 RunTest("testAPIMethodExistence", "web_view/apitest"); |
| 173 } | 169 } |
| 174 | 170 |
| 175 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAssignSrcAfterCrash) { | 171 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestAssignSrcAfterCrash) { |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 StopTestServer(); | 451 StopTestServer(); |
| 456 } | 452 } |
| 457 | 453 |
| 458 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIGoogleProperty) { | 454 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIGoogleProperty) { |
| 459 StartTestServer(); | 455 StartTestServer(); |
| 460 RunTest("testWebRequestAPIGoogleProperty", "web_view/apitest"); | 456 RunTest("testWebRequestAPIGoogleProperty", "web_view/apitest"); |
| 461 StopTestServer(); | 457 StopTestServer(); |
| 462 } | 458 } |
| 463 | 459 |
| 464 } // namespace extensions | 460 } // namespace extensions |
| OLD | NEW |