| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CHROME_FRAME_TEST_TEST_WITH_WEB_SERVER_H_ | 5 #ifndef CHROME_FRAME_TEST_TEST_WITH_WEB_SERVER_H_ |
| 6 #define CHROME_FRAME_TEST_TEST_WITH_WEB_SERVER_H_ | 6 #define CHROME_FRAME_TEST_TEST_WITH_WEB_SERVER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 bool invokes_cf() const { | 41 bool invokes_cf() const { |
| 42 return method_ != NONE; | 42 return method_ != NONE; |
| 43 } | 43 } |
| 44 | 44 |
| 45 Type type() const { return method_; } | 45 Type type() const { return method_; } |
| 46 | 46 |
| 47 private: | 47 private: |
| 48 Type method_; | 48 Type method_; |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 // An interface for listeners of interesting events on a MockWebServer. |
| 52 class WebServerListener { |
| 53 public: |
| 54 virtual ~WebServerListener() {} |
| 55 |
| 56 // Invoked when a MockWebServer receives an expected response; see |
| 57 // MockWebServer::ExpectAndHandlePostedResult. |
| 58 virtual void OnExpectedResponse() = 0; |
| 59 }; |
| 60 |
| 51 // Simple Gmock friendly web server. Sample usage: | 61 // Simple Gmock friendly web server. Sample usage: |
| 52 // MockWebServer mock(9999, "0.0.0.0"); | 62 // MockWebServer mock(9999, "0.0.0.0"); |
| 53 // EXPECT_CALL(mock, Get(_, StrEq("/favicon.ico"), _)).WillRepeatedly(SendFast( | 63 // EXPECT_CALL(mock, Get(_, StrEq("/favicon.ico"), _)).WillRepeatedly(SendFast( |
| 54 // "HTTP/1.1 404 Not Found" | 64 // "HTTP/1.1 404 Not Found" |
| 55 // "text/html; charset=UTF-8", EmptyString())); | 65 // "text/html; charset=UTF-8", EmptyString())); |
| 56 // | 66 // |
| 57 // EXPECT_CALL(mock, Get(_, StrEq("/book"), _)).WillRepeatedly(Send( | 67 // EXPECT_CALL(mock, Get(_, StrEq("/book"), _)).WillRepeatedly(Send( |
| 58 // "HTTP/1.1 302 Found\r\n" | 68 // "HTTP/1.1 302 Found\r\n" |
| 59 // "Connection: close\r\n" | 69 // "Connection: close\r\n" |
| 60 // "Content-Type: text/html\r\n" | 70 // "Content-Type: text/html\r\n" |
| 61 // "Location: library\r\n", | 71 // "Location: library\r\n", |
| 62 // "<html>Lalalala</html>", 3, 1000)); | 72 // "<html>Lalalala</html>", 3, 1000)); |
| 63 // | 73 // |
| 64 // EXPECT_CALL(mock, Get(_, StrEq("/library"), _)).WillRepeatedly(Send( | 74 // EXPECT_CALL(mock, Get(_, StrEq("/library"), _)).WillRepeatedly(Send( |
| 65 // "HTTP/1.1 200 OK\r\n" | 75 // "HTTP/1.1 200 OK\r\n" |
| 66 // "Connection: close\r\n" | 76 // "Connection: close\r\n" |
| 67 // "Content-Type: text/html\r\n", | 77 // "Content-Type: text/html\r\n", |
| 68 // "<html><meta http-equiv=\"X-UA-Compatible\" content=\"chrome=1\" />" | 78 // "<html><meta http-equiv=\"X-UA-Compatible\" content=\"chrome=1\" />" |
| 69 // "<body>Rendered in CF.</body></html>", 4, 1000)); | 79 // "<body>Rendered in CF.</body></html>", 4, 1000)); |
| 70 class MockWebServer : public test_server::HTTPTestServer { | 80 class MockWebServer : public test_server::HTTPTestServer { |
| 71 public: | 81 public: |
| 72 MockWebServer(int port, const std::wstring& address, FilePath root_dir) | 82 MockWebServer(int port, const std::wstring& address, FilePath root_dir) |
| 73 : test_server::HTTPTestServer(port, address, root_dir) {} | 83 : test_server::HTTPTestServer(port, address, root_dir), listener_(NULL) {} |
| 74 | 84 |
| 75 // Overriden from test_server::HTTPTestServer. | 85 // Overriden from test_server::HTTPTestServer. |
| 76 MOCK_METHOD3(Get, void(test_server::ConfigurableConnection* connection, | 86 MOCK_METHOD3(Get, void(test_server::ConfigurableConnection* connection, |
| 77 const std::wstring& path, | 87 const std::wstring& path, |
| 78 const test_server::Request&r)); | 88 const test_server::Request& r)); |
| 79 MOCK_METHOD3(Post, void(test_server::ConfigurableConnection* connection, | 89 MOCK_METHOD3(Post, void(test_server::ConfigurableConnection* connection, |
| 80 const std::wstring& path, | 90 const std::wstring& path, |
| 81 const test_server::Request&r)); | 91 const test_server::Request& r)); |
| 82 | 92 |
| 83 // Expect a GET request for |url|. Respond with the file appropriate for | 93 // Expect a GET request for |url|. Respond with the file appropriate for |
| 84 // the given |url|. Modify the file to follow the given CFInvocation method. | 94 // the given |url|. Modify the file to follow the given CFInvocation method. |
| 85 // The response includes a no-cache header. |allow_meta_tag_double_req| | 95 // The response includes a no-cache header. |allow_meta_tag_double_req| |
| 86 // specifies whether to allow the request to happen twice if the invocation | 96 // specifies whether to allow the request to happen twice if the invocation |
| 87 // is using the CF meta tag. | 97 // is using the CF meta tag. |
| 88 void ExpectAndServeRequest(CFInvocation invocation, const std::wstring& url); | 98 void ExpectAndServeRequest(CFInvocation invocation, const std::wstring& url); |
| 89 | 99 |
| 90 // Expect a number of GET requests for |url|. Rest is similar to the function | 100 // Expect a number of GET requests for |url|. Rest is similar to the function |
| 91 // ExpectAndServeRequest. | 101 // ExpectAndServeRequest. |
| 92 void ExpectAndServeRequestWithCardinality(CFInvocation invocation, | 102 void ExpectAndServeRequestWithCardinality(CFInvocation invocation, |
| 93 const std::wstring& url, | 103 const std::wstring& url, |
| 94 testing::Cardinality cardinality); | 104 testing::Cardinality cardinality); |
| 95 | 105 |
| 96 // Same as above except do not include the no-cache header. | 106 // Same as above except do not include the no-cache header. |
| 97 void ExpectAndServeRequestAllowCache(CFInvocation invocation, | 107 void ExpectAndServeRequestAllowCache(CFInvocation invocation, |
| 98 const std::wstring& url); | 108 const std::wstring& url); |
| 99 | 109 |
| 100 // Expect any number of GETs for the given resource path (e.g, /favicon.ico) | 110 // Expect any number of GETs for the given resource path (e.g, /favicon.ico) |
| 101 // and respond with the file, if it exists, or a 404 if it does not. | 111 // and respond with the file, if it exists, or a 404 if it does not. |
| 102 void ExpectAndServeRequestAnyNumberTimes(CFInvocation invocation, | 112 void ExpectAndServeRequestAnyNumberTimes(CFInvocation invocation, |
| 103 const std::wstring& path_prefix); | 113 const std::wstring& path_prefix); |
| 104 | 114 |
| 115 void set_listener(WebServerListener* listener) { listener_ = listener; } |
| 116 |
| 117 // Expect a POST to an URL containing |post_suffix|, saving the response |
| 118 // contents for retrieval by posted_result(). Invokes the listener's |
| 119 // OnExpectedResponse method if the posted response matches the expected |
| 120 // result. |
| 105 void ExpectAndHandlePostedResult(CFInvocation invocation, | 121 void ExpectAndHandlePostedResult(CFInvocation invocation, |
| 106 const std::wstring& post_suffix); | 122 const std::wstring& post_suffix); |
| 107 | 123 |
| 108 // Expect and serve all incoming GET requests. | 124 // Expect and serve all incoming GET requests. |
| 109 void ExpectAndServeAnyRequests(CFInvocation invocation) { | 125 void ExpectAndServeAnyRequests(CFInvocation invocation) { |
| 110 ExpectAndServeRequestAnyNumberTimes(invocation, L""); | 126 ExpectAndServeRequestAnyNumberTimes(invocation, L""); |
| 111 } | 127 } |
| 112 | 128 |
| 113 | 129 |
| 114 // Send a response on the given connection appropriate for |resource_uri|. | 130 // Send a response on the given connection appropriate for |resource_uri|. |
| 115 // If the file referred to by |path| exists, send the file data, otherwise | 131 // If the file referred to by |path| exists, send the file data, otherwise |
| 116 // send 404. Modify the file data according to the given invocation method. | 132 // send 404. Modify the file data according to the given invocation method. |
| 117 void SendResponseHelper(test_server::ConfigurableConnection* connection, | 133 void SendResponseHelper(test_server::ConfigurableConnection* connection, |
| 118 const std::wstring& resource_uri, | 134 const std::wstring& resource_uri, |
| 119 const test_server::Request& request, | 135 const test_server::Request& request, |
| 120 CFInvocation invocation, | 136 CFInvocation invocation, |
| 121 bool add_no_cache_header); | 137 bool add_no_cache_header); |
| 122 // Handles the posted /writefile response | 138 // Handles the posted /writefile response |
| 123 void HandlePostedResponse(test_server::ConfigurableConnection* connection, | 139 void HandlePostedResponse(test_server::ConfigurableConnection* connection, |
| 124 const test_server::Request& request); | 140 const test_server::Request& request); |
| 125 | 141 |
| 142 void ClearResults() { |
| 143 posted_result_.clear(); |
| 144 expected_result_.clear(); |
| 145 } |
| 146 |
| 126 void set_expected_result(const std::string& expected_result) { | 147 void set_expected_result(const std::string& expected_result) { |
| 127 expected_result_ = expected_result; | 148 expected_result_ = expected_result; |
| 128 } | 149 } |
| 129 | 150 |
| 130 const std::string& posted_result() const { | 151 const std::string& posted_result() const { |
| 131 return posted_result_; | 152 return posted_result_; |
| 132 } | 153 } |
| 133 | 154 |
| 134 private: | 155 private: |
| 156 WebServerListener* listener_; |
| 135 // Holds the results of tests which post success/failure. | 157 // Holds the results of tests which post success/failure. |
| 136 std::string posted_result_; | 158 std::string posted_result_; |
| 137 std::string expected_result_; | 159 std::string expected_result_; |
| 138 }; | 160 }; |
| 139 | 161 |
| 162 class MockWebServerListener : public WebServerListener { |
| 163 public: |
| 164 MOCK_METHOD0(OnExpectedResponse, void()); |
| 165 }; |
| 166 |
| 140 // Class that: | 167 // Class that: |
| 141 // 1) Starts the local webserver, | 168 // 1) Starts the local webserver, |
| 142 // 2) Supports launching browsers - Internet Explorer with local url | 169 // 2) Supports launching browsers - Internet Explorer with local url |
| 143 // 3) Wait the webserver to finish. It is supposed the test webpage to shutdown | 170 // 3) Wait the webserver to finish. It is supposed the test webpage to shutdown |
| 144 // the server by navigating to "kill" page | 171 // the server by navigating to "kill" page |
| 145 // 4) Supports read the posted results from the test webpage to the "dump" | 172 // 4) Supports read the posted results from the test webpage to the "dump" |
| 146 // webserver directory | 173 // webserver directory |
| 147 class ChromeFrameTestWithWebServer : public testing::Test { | 174 class ChromeFrameTestWithWebServer : public testing::Test { |
| 148 public: | 175 public: |
| 149 ChromeFrameTestWithWebServer(); | 176 ChromeFrameTestWithWebServer(); |
| 150 | 177 |
| 151 protected: | 178 protected: |
| 152 enum BrowserKind { INVALID, IE, CHROME }; | 179 enum BrowserKind { INVALID, IE, CHROME }; |
| 153 | 180 |
| 154 bool LaunchBrowser(BrowserKind browser, const wchar_t* url); | 181 bool LaunchBrowser(BrowserKind browser, const wchar_t* url); |
| 182 |
| 183 // Returns true if the test completed in time, or false if it timed out. |
| 155 bool WaitForTestToComplete(base::TimeDelta duration); | 184 bool WaitForTestToComplete(base::TimeDelta duration); |
| 156 | 185 |
| 157 // Waits for the page to notify us of the window.onload event firing. | 186 // Waits for the page to notify us of the window.onload event firing. |
| 158 // Note that the milliseconds value is only approximate. | 187 // Note that the milliseconds value is only approximate. |
| 159 bool WaitForOnLoad(int milliseconds); | 188 bool WaitForOnLoad(int milliseconds); |
| 160 | 189 |
| 161 // Launches the specified browser and waits for the test to complete | 190 // Launches the specified browser and waits for the test to complete (see |
| 162 // (see WaitForTestToComplete). Then checks that the outcome is equal | 191 // WaitForTestToComplete). Then checks that the outcome is equal to the |
| 163 // to the expected result. | 192 // expected result. The test is repeated once if it fails due to a timeout. |
| 164 // This function uses EXPECT_TRUE and ASSERT_TRUE for all steps performed | 193 // This function uses EXPECT_TRUE and ASSERT_TRUE for all steps performed |
| 165 // hence no return value. | 194 // hence no return value. |
| 166 void SimpleBrowserTestExpectedResult(BrowserKind browser, | 195 void SimpleBrowserTestExpectedResult(BrowserKind browser, |
| 167 const wchar_t* page, const char* result); | 196 const wchar_t* page, const char* result); |
| 168 void SimpleBrowserTest(BrowserKind browser, const wchar_t* page); | 197 void SimpleBrowserTest(BrowserKind browser, const wchar_t* page); |
| 169 | 198 |
| 199 // Sets up expectations for a page to post back a result. |
| 200 void ExpectAndHandlePostedResult(); |
| 201 |
| 170 // Test if chrome frame correctly reports its version. | 202 // Test if chrome frame correctly reports its version. |
| 171 void VersionTest(BrowserKind browser, const wchar_t* page); | 203 void VersionTest(BrowserKind browser, const wchar_t* page); |
| 172 | 204 |
| 173 void CloseBrowser(); | 205 void CloseBrowser(); |
| 174 | 206 |
| 175 // Ensures (well, at least tries to ensure) that the browser window has focus. | 207 // Ensures (well, at least tries to ensure) that the browser window has focus. |
| 176 bool BringBrowserToTop(); | 208 bool BringBrowserToTop(); |
| 177 | 209 |
| 178 const FilePath& GetCFTestFilePath() { | 210 const FilePath& GetCFTestFilePath() { |
| 179 return test_file_path_; | 211 return test_file_path_; |
| 180 } | 212 } |
| 181 | 213 |
| 214 static chrome_frame_test::TimedMsgLoop& loop() { |
| 215 return *loop_; |
| 216 } |
| 217 |
| 218 static testing::StrictMock<MockWebServerListener>& listener_mock() { |
| 219 return *listener_mock_; |
| 220 } |
| 221 |
| 222 static testing::StrictMock<MockWebServer>& server_mock() { |
| 223 return *server_mock_; |
| 224 } |
| 225 |
| 182 static void SetUpTestCase(); | 226 static void SetUpTestCase(); |
| 183 static void TearDownTestCase(); | 227 static void TearDownTestCase(); |
| 184 | 228 |
| 185 static const FilePath& GetChromeUserDataDirectory(); | 229 static const FilePath& GetChromeUserDataDirectory(); |
| 186 | 230 |
| 187 virtual void SetUp() OVERRIDE; | 231 virtual void SetUp() OVERRIDE; |
| 188 virtual void TearDown() OVERRIDE; | 232 virtual void TearDown() OVERRIDE; |
| 189 | 233 |
| 190 // The on-disk path to our html test files. | 234 // The on-disk path to our html test files. |
| 191 static FilePath test_file_path_; | 235 static FilePath test_file_path_; |
| 192 static FilePath results_dir_; | 236 static FilePath results_dir_; |
| 193 static FilePath CFInstall_path_; | 237 static FilePath CFInstall_path_; |
| 194 static FilePath CFInstance_path_; | 238 static FilePath CFInstance_path_; |
| 195 static FilePath chrome_user_data_dir_; | 239 static FilePath chrome_user_data_dir_; |
| 196 | 240 |
| 197 // The user data directory used for Chrome instances. | 241 // The user data directory used for Chrome instances. |
| 198 static ScopedTempDir temp_dir_; | 242 static ScopedTempDir temp_dir_; |
| 199 | 243 |
| 244 // The web server from which we serve the web! |
| 245 static chrome_frame_test::TimedMsgLoop* loop_; |
| 246 static testing::StrictMock<MockWebServerListener>* listener_mock_; |
| 247 static testing::StrictMock<MockWebServer>* server_mock_; |
| 248 |
| 200 BrowserKind browser_; | 249 BrowserKind browser_; |
| 201 base::win::ScopedHandle browser_handle_; | 250 base::win::ScopedHandle browser_handle_; |
| 202 chrome_frame_test::TimedMsgLoop loop_; | |
| 203 testing::StrictMock<MockWebServer> server_mock_; | |
| 204 }; | 251 }; |
| 205 | 252 |
| 206 // A helper class for doing some bookkeeping when using the | 253 // A helper class for doing some bookkeeping when using the |
| 207 // SimpleWebServer class. | 254 // SimpleWebServer class. |
| 208 class SimpleWebServerTest { | 255 class SimpleWebServerTest { |
| 209 public: | 256 public: |
| 210 explicit SimpleWebServerTest(int port) : server_(port), port_(port) { | 257 explicit SimpleWebServerTest(int port) : server_(port), port_(port) { |
| 211 } | 258 } |
| 212 | 259 |
| 213 ~SimpleWebServerTest() { | 260 ~SimpleWebServerTest() { |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 // Same as above except that the response does not include the no-cache header. | 361 // Same as above except that the response does not include the no-cache header. |
| 315 ACTION_P2(SendAllowCacheResponse, server, invocation) { | 362 ACTION_P2(SendAllowCacheResponse, server, invocation) { |
| 316 server->SendResponseHelper(arg0, arg1, invocation, false); | 363 server->SendResponseHelper(arg0, arg1, invocation, false); |
| 317 } | 364 } |
| 318 | 365 |
| 319 ACTION_P2(HandlePostedResponseHelper, server, invocation) { | 366 ACTION_P2(HandlePostedResponseHelper, server, invocation) { |
| 320 server->HandlePostedResponse(arg0, arg2); | 367 server->HandlePostedResponse(arg0, arg2); |
| 321 } | 368 } |
| 322 | 369 |
| 323 #endif // CHROME_FRAME_TEST_TEST_WITH_WEB_SERVER_H_ | 370 #endif // CHROME_FRAME_TEST_TEST_WITH_WEB_SERVER_H_ |
| OLD | NEW |