OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 #include <atlbase.h> |
| 5 #include <atlcom.h> |
| 6 #include "app/win_util.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "testing/gmock/include/gmock/gmock.h" |
| 9 #include "testing/gmock_mutant.h" |
| 10 |
| 11 #include "chrome_frame/urlmon_url_request.h" |
| 12 #include "chrome_frame/urlmon_url_request_private.h" |
| 13 #include "chrome_frame/test/chrome_frame_test_utils.h" |
| 14 #include "chrome_frame/test/http_server.h" |
| 15 using testing::CreateFunctor; |
| 16 |
| 17 const int kChromeFrameLongNavigationTimeoutInSeconds = 10; |
| 18 |
| 19 class MockUrlDelegate : public PluginUrlRequestDelegate { |
| 20 public: |
| 21 MOCK_METHOD8(OnResponseStarted, void(int request_id, const char* mime_type, |
| 22 const char* headers, int size, |
| 23 base::Time last_modified, const std::string& peristent_cookies, |
| 24 const std::string& redirect_url, int redirect_status)); |
| 25 MOCK_METHOD3(OnReadComplete, void(int request_id, const void* buffer, |
| 26 int len)); |
| 27 MOCK_METHOD2(OnResponseEnd, void(int request_id, |
| 28 const URLRequestStatus& status)); |
| 29 |
| 30 static bool ImplementsThreadSafeReferenceCounting() { |
| 31 return false; |
| 32 } |
| 33 void AddRef() {} |
| 34 void Release() {} |
| 35 |
| 36 void PostponeReadRequest(chrome_frame_test::TimedMsgLoop* loop, |
| 37 UrlmonUrlRequest* request, int bytes_to_read) { |
| 38 loop->PostDelayedTask(FROM_HERE, NewRunnableMethod(this, |
| 39 &MockUrlDelegate::Read, request, bytes_to_read), 0); |
| 40 } |
| 41 |
| 42 private: |
| 43 void Read(UrlmonUrlRequest* request, int bytes_to_read) { |
| 44 request->Read(bytes_to_read); |
| 45 } |
| 46 }; |
| 47 |
| 48 // Simplest UrlmonUrlRequest. Retrieve a file from local web server. |
| 49 TEST(UrlmonUrlRequestTest, Simple1) { |
| 50 MockUrlDelegate mock; |
| 51 ChromeFrameHTTPServer server; |
| 52 chrome_frame_test::TimedMsgLoop loop; |
| 53 win_util::ScopedCOMInitializer init_com; |
| 54 CComObjectStackEx<UrlmonUrlRequest> request; |
| 55 |
| 56 server.SetUp(); |
| 57 request.AddRef(); |
| 58 request.Initialize(&mock, 1, // request_id |
| 59 server.Resolve(L"chrome_frame_window_open.html").spec(), |
| 60 "get", |
| 61 "", // referrer |
| 62 "", // extra request |
| 63 NULL, // upload data |
| 64 true); // frame busting |
| 65 |
| 66 EXPECT_CALL(mock, OnResponseStarted(1, testing::_, testing::_, testing::_, |
| 67 testing::_, testing::_, testing::_, |
| 68 testing::_)) |
| 69 .Times(1) |
| 70 .WillOnce(testing::IgnoreResult(testing::InvokeWithoutArgs(CreateFunctor( |
| 71 &request, &UrlmonUrlRequest::Read, 512)))); |
| 72 |
| 73 |
| 74 EXPECT_CALL(mock, OnReadComplete(1, testing::_, testing::Gt(0))) |
| 75 .Times(testing::AtLeast(1)) |
| 76 .WillRepeatedly(testing::InvokeWithoutArgs(CreateFunctor(&mock, |
| 77 &MockUrlDelegate::PostponeReadRequest, &loop, &request, 64))); |
| 78 |
| 79 EXPECT_CALL(mock, OnResponseEnd(1, testing::_)) |
| 80 .Times(1) |
| 81 .WillOnce(QUIT_LOOP_SOON(loop, 2)); |
| 82 |
| 83 request.Start(); |
| 84 loop.RunFor(kChromeFrameLongNavigationTimeoutInSeconds); |
| 85 request.Release(); |
| 86 server.TearDown(); |
| 87 } |
| 88 |
| 89 // Simplest test - retrieve file from local web server. |
| 90 TEST(UrlmonUrlRequestManagerTest, Simple1) { |
| 91 MockUrlDelegate mock; |
| 92 ChromeFrameHTTPServer server; |
| 93 chrome_frame_test::TimedMsgLoop loop; |
| 94 server.SetUp(); |
| 95 scoped_ptr<UrlmonUrlRequestManager> mgr(new UrlmonUrlRequestManager()); |
| 96 mgr->set_delegate(&mock); |
| 97 IPC::AutomationURLRequest r1 = { |
| 98 server.Resolve(L"chrome_frame_window_open.html").spec(), "get" }; |
| 99 |
| 100 EXPECT_CALL(mock, OnResponseStarted(1, testing::_, testing::_, testing::_, |
| 101 testing::_, testing::_, testing::_, testing::_)) |
| 102 .Times(1) |
| 103 .WillOnce(testing::InvokeWithoutArgs(CreateFunctor(mgr.get(), |
| 104 &PluginUrlRequestManager::ReadUrlRequest, 0, 1, 512))); |
| 105 |
| 106 EXPECT_CALL(mock, OnReadComplete(1, testing::_, testing::Gt(0))) |
| 107 .Times(testing::AtLeast(1)) |
| 108 .WillRepeatedly(testing::InvokeWithoutArgs(CreateFunctor(mgr.get(), |
| 109 &PluginUrlRequestManager::ReadUrlRequest, 0, 1, 2))); |
| 110 |
| 111 EXPECT_CALL(mock, OnResponseEnd(1, testing::_)) |
| 112 .Times(1) |
| 113 .WillOnce(QUIT_LOOP_SOON(loop, 2)); |
| 114 |
| 115 mgr->StartUrlRequest(0, 1, r1); |
| 116 loop.RunFor(kChromeFrameLongNavigationTimeoutInSeconds); |
| 117 mgr.reset(); |
| 118 server.TearDown(); |
| 119 |
| 120 } |
| 121 |
| 122 TEST(UrlmonUrlRequestManagerTest, Abort1) { |
| 123 MockUrlDelegate mock; |
| 124 ChromeFrameHTTPServer server; |
| 125 chrome_frame_test::TimedMsgLoop loop; |
| 126 server.SetUp(); |
| 127 scoped_ptr<UrlmonUrlRequestManager> mgr(new UrlmonUrlRequestManager()); |
| 128 mgr->set_delegate(&mock); |
| 129 IPC::AutomationURLRequest r1 = { |
| 130 server.Resolve(L"chrome_frame_window_open.html").spec(), "get" }; |
| 131 |
| 132 EXPECT_CALL(mock, OnResponseStarted(1, testing::_, testing::_, testing::_, |
| 133 testing::_, testing::_, testing::_, testing::_)) |
| 134 .Times(1) |
| 135 .WillOnce(testing::DoAll( |
| 136 testing::InvokeWithoutArgs(CreateFunctor(mgr.get(), |
| 137 &PluginUrlRequestManager::EndUrlRequest, 0, 1, URLRequestStatus())), |
| 138 testing::InvokeWithoutArgs(CreateFunctor(mgr.get(), |
| 139 &PluginUrlRequestManager::ReadUrlRequest, 0, 1, 2)))); |
| 140 |
| 141 EXPECT_CALL(mock, OnReadComplete(1, testing::_, testing::Gt(0))) |
| 142 .Times(0); |
| 143 |
| 144 EXPECT_CALL(mock, OnResponseEnd(1, testing::_)) |
| 145 .Times(1) |
| 146 .WillOnce(QUIT_LOOP_SOON(loop, 2)); |
| 147 |
| 148 mgr->StartUrlRequest(0, 1, r1); |
| 149 loop.RunFor(kChromeFrameLongNavigationTimeoutInSeconds); |
| 150 mgr.reset(); |
| 151 server.TearDown(); |
| 152 } |
OLD | NEW |