| OLD | NEW |
| 1 // Copyright (c) 2011 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 #include <atlbase.h> | 5 #include <atlbase.h> |
| 6 #include <atlcom.h> | 6 #include <atlcom.h> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/win/scoped_com_initializer.h" | 10 #include "base/win/scoped_com_initializer.h" |
| 11 #include "chrome/common/automation_messages.h" | 11 #include "chrome/common/automation_messages.h" |
| 12 #include "chrome_frame/test/chrome_frame_test_utils.h" | 12 #include "chrome_frame/test/chrome_frame_test_utils.h" |
| 13 #include "chrome_frame/test/test_server.h" | 13 #include "chrome_frame/test/test_server.h" |
| 14 #include "chrome_frame/test/test_with_web_server.h" | 14 #include "chrome_frame/test/test_with_web_server.h" |
| 15 #include "chrome_frame/urlmon_url_request.h" | 15 #include "chrome_frame/urlmon_url_request.h" |
| 16 #include "chrome_frame/urlmon_url_request_private.h" | 16 #include "chrome_frame/urlmon_url_request_private.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gmock_mutant.h" | 18 #include "testing/gmock_mutant.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 using testing::CreateFunctor; | 21 using testing::CreateFunctor; |
| 22 | 22 |
| 23 const int kChromeFrameLongNavigationTimeoutInSeconds = 10; | 23 using chrome_frame_test::kChromeFrameLongNavigationTimeout; |
| 24 | 24 |
| 25 static void AppendToStream(IStream* s, void* buffer, ULONG cb) { | 25 static void AppendToStream(IStream* s, void* buffer, ULONG cb) { |
| 26 ULONG bytes_written; | 26 ULONG bytes_written; |
| 27 LARGE_INTEGER current_pos; | 27 LARGE_INTEGER current_pos; |
| 28 LARGE_INTEGER zero = {0}; | 28 LARGE_INTEGER zero = {0}; |
| 29 // Remember current position. | 29 // Remember current position. |
| 30 ASSERT_HRESULT_SUCCEEDED(s->Seek(zero, STREAM_SEEK_CUR, | 30 ASSERT_HRESULT_SUCCEEDED(s->Seek(zero, STREAM_SEEK_CUR, |
| 31 reinterpret_cast<ULARGE_INTEGER*>(¤t_pos))); | 31 reinterpret_cast<ULARGE_INTEGER*>(¤t_pos))); |
| 32 // Seek to the end. | 32 // Seek to the end. |
| 33 ASSERT_HRESULT_SUCCEEDED(s->Seek(zero, STREAM_SEEK_END, NULL)); | 33 ASSERT_HRESULT_SUCCEEDED(s->Seek(zero, STREAM_SEEK_END, NULL)); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 | 99 |
| 100 | 100 |
| 101 EXPECT_CALL(mock, OnReadComplete(1, testing::Property(&std::string::size, | 101 EXPECT_CALL(mock, OnReadComplete(1, testing::Property(&std::string::size, |
| 102 testing::Gt(0u)))) | 102 testing::Gt(0u)))) |
| 103 .Times(testing::AtLeast(1)) | 103 .Times(testing::AtLeast(1)) |
| 104 .WillRepeatedly(testing::InvokeWithoutArgs(CreateFunctor(&mock, | 104 .WillRepeatedly(testing::InvokeWithoutArgs(CreateFunctor(&mock, |
| 105 &MockUrlDelegate::PostponeReadRequest, &loop, &request, 64))); | 105 &MockUrlDelegate::PostponeReadRequest, &loop, &request, 64))); |
| 106 | 106 |
| 107 EXPECT_CALL(mock, OnResponseEnd(1, testing::_)) | 107 EXPECT_CALL(mock, OnResponseEnd(1, testing::_)) |
| 108 .Times(1) | 108 .Times(1) |
| 109 .WillOnce(QUIT_LOOP_SOON(loop, 2)); | 109 .WillOnce(QUIT_LOOP_SOON(loop, base::TimeDelta::FromSeconds(2))); |
| 110 | 110 |
| 111 request.Start(); | 111 request.Start(); |
| 112 loop.RunFor(kChromeFrameLongNavigationTimeoutInSeconds); | 112 loop.RunFor(kChromeFrameLongNavigationTimeout); |
| 113 request.Release(); | 113 request.Release(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 // Same as Simple1 except we use the HEAD verb to fetch only the headers | 116 // Same as Simple1 except we use the HEAD verb to fetch only the headers |
| 117 // from the server. | 117 // from the server. |
| 118 TEST(UrlmonUrlRequestTest, Head) { | 118 TEST(UrlmonUrlRequestTest, Head) { |
| 119 MockUrlDelegate mock; | 119 MockUrlDelegate mock; |
| 120 chrome_frame_test::TimedMsgLoop loop; | 120 chrome_frame_test::TimedMsgLoop loop; |
| 121 // Use SimpleWebServer instead of the python server to support HEAD | 121 // Use SimpleWebServer instead of the python server to support HEAD |
| 122 // requests. | 122 // requests. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 144 testing::_)) | 144 testing::_)) |
| 145 .Times(1) | 145 .Times(1) |
| 146 .WillOnce(testing::IgnoreResult(testing::InvokeWithoutArgs(CreateFunctor( | 146 .WillOnce(testing::IgnoreResult(testing::InvokeWithoutArgs(CreateFunctor( |
| 147 &request, &UrlmonUrlRequest::Read, 512)))); | 147 &request, &UrlmonUrlRequest::Read, 512)))); |
| 148 | 148 |
| 149 // For HEAD requests we don't expect content reads. | 149 // For HEAD requests we don't expect content reads. |
| 150 EXPECT_CALL(mock, OnReadComplete(1, testing::_)).Times(0); | 150 EXPECT_CALL(mock, OnReadComplete(1, testing::_)).Times(0); |
| 151 | 151 |
| 152 EXPECT_CALL(mock, OnResponseEnd(1, testing::_)) | 152 EXPECT_CALL(mock, OnResponseEnd(1, testing::_)) |
| 153 .Times(1) | 153 .Times(1) |
| 154 .WillOnce(QUIT_LOOP_SOON(loop, 2)); | 154 .WillOnce(QUIT_LOOP_SOON(loop, base::TimeDelta::FromSeconds(2))); |
| 155 | 155 |
| 156 request.Start(); | 156 request.Start(); |
| 157 loop.RunFor(kChromeFrameLongNavigationTimeoutInSeconds); | 157 loop.RunFor(kChromeFrameLongNavigationTimeout); |
| 158 request.Release(); | 158 request.Release(); |
| 159 } | 159 } |
| 160 | 160 |
| 161 TEST(UrlmonUrlRequestTest, UnreachableUrl) { | 161 TEST(UrlmonUrlRequestTest, UnreachableUrl) { |
| 162 MockUrlDelegate mock; | 162 MockUrlDelegate mock; |
| 163 chrome_frame_test::TimedMsgLoop loop; | 163 chrome_frame_test::TimedMsgLoop loop; |
| 164 base::win::ScopedCOMInitializer init_com; | 164 base::win::ScopedCOMInitializer init_com; |
| 165 CComObjectStackEx<UrlmonUrlRequest> request; | 165 CComObjectStackEx<UrlmonUrlRequest> request; |
| 166 | 166 |
| 167 testing::StrictMock<MockWebServer> mock_server(1337, L"127.0.0.1", | 167 testing::StrictMock<MockWebServer> mock_server(1337, L"127.0.0.1", |
| (...skipping 12 matching lines...) Expand all Loading... |
| 180 ResourceType::MAIN_FRAME, // resource type | 180 ResourceType::MAIN_FRAME, // resource type |
| 181 true, | 181 true, |
| 182 0); // frame busting | 182 0); // frame busting |
| 183 | 183 |
| 184 // Expect headers | 184 // Expect headers |
| 185 EXPECT_CALL(mock, OnResponseStarted(1, testing::_, | 185 EXPECT_CALL(mock, OnResponseStarted(1, testing::_, |
| 186 testing::StartsWith("HTTP/1.1 404"), | 186 testing::StartsWith("HTTP/1.1 404"), |
| 187 testing::_, testing::_, testing::_, | 187 testing::_, testing::_, testing::_, |
| 188 testing::_, testing::_)) | 188 testing::_, testing::_)) |
| 189 .Times(1) | 189 .Times(1) |
| 190 .WillOnce(QUIT_LOOP_SOON(loop, 2)); | 190 .WillOnce(QUIT_LOOP_SOON(loop, base::TimeDelta::FromSeconds(2))); |
| 191 | 191 |
| 192 EXPECT_CALL(mock, OnResponseEnd(1, testing::Property( | 192 EXPECT_CALL(mock, OnResponseEnd(1, testing::Property( |
| 193 &net::URLRequestStatus::error, | 193 &net::URLRequestStatus::error, |
| 194 net::ERR_TUNNEL_CONNECTION_FAILED))) | 194 net::ERR_TUNNEL_CONNECTION_FAILED))) |
| 195 .Times(testing::AtMost(1)); | 195 .Times(testing::AtMost(1)); |
| 196 | 196 |
| 197 request.Start(); | 197 request.Start(); |
| 198 loop.RunFor(kChromeFrameLongNavigationTimeoutInSeconds); | 198 loop.RunFor(kChromeFrameLongNavigationTimeout); |
| 199 request.Release(); | 199 request.Release(); |
| 200 } | 200 } |
| 201 | 201 |
| 202 TEST(UrlmonUrlRequestTest, ZeroLengthResponse) { | 202 TEST(UrlmonUrlRequestTest, ZeroLengthResponse) { |
| 203 MockUrlDelegate mock; | 203 MockUrlDelegate mock; |
| 204 chrome_frame_test::TimedMsgLoop loop; | 204 chrome_frame_test::TimedMsgLoop loop; |
| 205 | 205 |
| 206 testing::StrictMock<MockWebServer> mock_server(1337, L"127.0.0.1", | 206 testing::StrictMock<MockWebServer> mock_server(1337, L"127.0.0.1", |
| 207 chrome_frame_test::GetTestDataFolder()); | 207 chrome_frame_test::GetTestDataFolder()); |
| 208 mock_server.ExpectAndServeAnyRequests(CFInvocation(CFInvocation::NONE)); | 208 mock_server.ExpectAndServeAnyRequests(CFInvocation(CFInvocation::NONE)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 221 0); // frame busting | 221 0); // frame busting |
| 222 | 222 |
| 223 // Expect headers | 223 // Expect headers |
| 224 EXPECT_CALL(mock, OnResponseStarted(1, testing::_, testing::_, testing::_, | 224 EXPECT_CALL(mock, OnResponseStarted(1, testing::_, testing::_, testing::_, |
| 225 testing::_, testing::_, testing::_, | 225 testing::_, testing::_, testing::_, |
| 226 testing::_)) | 226 testing::_)) |
| 227 .Times(1) | 227 .Times(1) |
| 228 .WillOnce(QUIT_LOOP(loop)); | 228 .WillOnce(QUIT_LOOP(loop)); |
| 229 | 229 |
| 230 request.Start(); | 230 request.Start(); |
| 231 loop.RunFor(kChromeFrameLongNavigationTimeoutInSeconds); | 231 loop.RunFor(kChromeFrameLongNavigationTimeout); |
| 232 EXPECT_FALSE(loop.WasTimedOut()); | 232 EXPECT_FALSE(loop.WasTimedOut()); |
| 233 | 233 |
| 234 // Should stay quiet, since we do not ask for anything for awhile. | 234 // Should stay quiet, since we do not ask for anything for awhile. |
| 235 EXPECT_CALL(mock, OnResponseEnd(1, testing::_)).Times(0); | 235 EXPECT_CALL(mock, OnResponseEnd(1, testing::_)).Times(0); |
| 236 loop.RunFor(3); | 236 loop.RunFor(base::TimeDelta::FromSeconds(3)); |
| 237 | 237 |
| 238 // Invoke read. Only now the response end ("server closed the connection") | 238 // Invoke read. Only now the response end ("server closed the connection") |
| 239 // is supposed to be delivered. | 239 // is supposed to be delivered. |
| 240 EXPECT_CALL(mock, OnResponseEnd(1, testing::Property( | 240 EXPECT_CALL(mock, OnResponseEnd(1, testing::Property( |
| 241 &net::URLRequestStatus::is_success, true))).Times(1); | 241 &net::URLRequestStatus::is_success, true))).Times(1); |
| 242 request.Read(512); | 242 request.Read(512); |
| 243 request.Release(); | 243 request.Release(); |
| 244 } | 244 } |
| 245 | 245 |
| 246 ACTION_P4(ManagerRead, loop, mgr, request_id, bytes_to_read) { | 246 ACTION_P4(ManagerRead, loop, mgr, request_id, bytes_to_read) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 276 .Times(1) | 276 .Times(1) |
| 277 .WillOnce(ManagerRead(&loop, mgr.get(), 1, 512)); | 277 .WillOnce(ManagerRead(&loop, mgr.get(), 1, 512)); |
| 278 | 278 |
| 279 EXPECT_CALL(mock, OnReadComplete(1, testing::Property(&std::string::size, | 279 EXPECT_CALL(mock, OnReadComplete(1, testing::Property(&std::string::size, |
| 280 testing::Gt(0u)))) | 280 testing::Gt(0u)))) |
| 281 .Times(testing::AtLeast(1)) | 281 .Times(testing::AtLeast(1)) |
| 282 .WillRepeatedly(ManagerRead(&loop, mgr.get(), 1, 2)); | 282 .WillRepeatedly(ManagerRead(&loop, mgr.get(), 1, 2)); |
| 283 | 283 |
| 284 EXPECT_CALL(mock, OnResponseEnd(1, testing::_)) | 284 EXPECT_CALL(mock, OnResponseEnd(1, testing::_)) |
| 285 .Times(1) | 285 .Times(1) |
| 286 .WillOnce(QUIT_LOOP_SOON(loop, 2)); | 286 .WillOnce(QUIT_LOOP_SOON(loop, base::TimeDelta::FromSeconds(2))); |
| 287 | 287 |
| 288 mgr->StartUrlRequest(1, r1); | 288 mgr->StartUrlRequest(1, r1); |
| 289 loop.RunFor(kChromeFrameLongNavigationTimeoutInSeconds); | 289 loop.RunFor(kChromeFrameLongNavigationTimeout); |
| 290 mgr.reset(); | 290 mgr.reset(); |
| 291 } | 291 } |
| 292 | 292 |
| 293 TEST(UrlmonUrlRequestManagerTest, Abort1) { | 293 TEST(UrlmonUrlRequestManagerTest, Abort1) { |
| 294 MockUrlDelegate mock; | 294 MockUrlDelegate mock; |
| 295 chrome_frame_test::TimedMsgLoop loop; | 295 chrome_frame_test::TimedMsgLoop loop; |
| 296 | 296 |
| 297 testing::StrictMock<MockWebServer> mock_server(1337, L"127.0.0.1", | 297 testing::StrictMock<MockWebServer> mock_server(1337, L"127.0.0.1", |
| 298 chrome_frame_test::GetTestDataFolder()); | 298 chrome_frame_test::GetTestDataFolder()); |
| 299 mock_server.ExpectAndServeAnyRequests(CFInvocation(CFInvocation::NONE)); | 299 mock_server.ExpectAndServeAnyRequests(CFInvocation(CFInvocation::NONE)); |
| 300 | 300 |
| 301 scoped_ptr<UrlmonUrlRequestManager> mgr(new UrlmonUrlRequestManager()); | 301 scoped_ptr<UrlmonUrlRequestManager> mgr(new UrlmonUrlRequestManager()); |
| 302 mgr->set_delegate(&mock); | 302 mgr->set_delegate(&mock); |
| 303 AutomationURLRequest r1; | 303 AutomationURLRequest r1; |
| 304 r1.url = WideToUTF8(mock_server.Resolve(L"chrome_frame_window_open.html")); | 304 r1.url = WideToUTF8(mock_server.Resolve(L"chrome_frame_window_open.html")); |
| 305 r1.method = "get"; | 305 r1.method = "get"; |
| 306 r1.resource_type = 0; | 306 r1.resource_type = 0; |
| 307 r1.load_flags = 0; | 307 r1.load_flags = 0; |
| 308 | 308 |
| 309 EXPECT_CALL(mock, OnResponseStarted(1, testing::_, testing::_, testing::_, | 309 EXPECT_CALL(mock, OnResponseStarted(1, testing::_, testing::_, testing::_, |
| 310 testing::_, testing::_, testing::_, testing::_)) | 310 testing::_, testing::_, testing::_, testing::_)) |
| 311 .Times(1) | 311 .Times(1) |
| 312 .WillOnce(testing::DoAll( | 312 .WillOnce(testing::DoAll( |
| 313 ManagerEndRequest(&loop, mgr.get(), 1), | 313 ManagerEndRequest(&loop, mgr.get(), 1), |
| 314 QUIT_LOOP_SOON(loop, 3))); | 314 QUIT_LOOP_SOON(loop, base::TimeDelta::FromSeconds(3)))); |
| 315 | 315 |
| 316 EXPECT_CALL(mock, OnReadComplete(1, testing::_)) | 316 EXPECT_CALL(mock, OnReadComplete(1, testing::_)) |
| 317 .Times(0); | 317 .Times(0); |
| 318 | 318 |
| 319 EXPECT_CALL(mock, OnResponseEnd(1, testing::_)) | 319 EXPECT_CALL(mock, OnResponseEnd(1, testing::_)) |
| 320 .Times(0); | 320 .Times(0); |
| 321 | 321 |
| 322 mgr->StartUrlRequest(1, r1); | 322 mgr->StartUrlRequest(1, r1); |
| 323 loop.RunFor(kChromeFrameLongNavigationTimeoutInSeconds); | 323 loop.RunFor(kChromeFrameLongNavigationTimeout); |
| 324 mgr.reset(); | 324 mgr.reset(); |
| 325 } | 325 } |
| OLD | NEW |