| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 <string.h> | 5 #include <string.h> |
| 6 #include <algorithm> | 6 #include <algorithm> |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 int byte_count)); | 63 int byte_count)); |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 // A URLRequestJob that returns static content for given URLs. We do | 66 // A URLRequestJob that returns static content for given URLs. We do |
| 67 // not use URLRequestTestJob here because URLRequestTestJob fakes | 67 // not use URLRequestTestJob here because URLRequestTestJob fakes |
| 68 // async operations by calling ReadRawData synchronously in an async | 68 // async operations by calling ReadRawData synchronously in an async |
| 69 // callback. This test requires a URLRequestJob that returns false for | 69 // callback. This test requires a URLRequestJob that returns false for |
| 70 // async reads, in order to exercise the real async read codepath. | 70 // async reads, in order to exercise the real async read codepath. |
| 71 class URLRequestJobTrackerTestJob : public URLRequestJob { | 71 class URLRequestJobTrackerTestJob : public URLRequestJob { |
| 72 public: | 72 public: |
| 73 URLRequestJobTrackerTestJob(URLRequest* request, bool async_reads) | 73 URLRequestJobTrackerTestJob(net::URLRequest* request, bool async_reads) |
| 74 : URLRequestJob(request), async_reads_(async_reads) {} | 74 : URLRequestJob(request), async_reads_(async_reads) {} |
| 75 | 75 |
| 76 void Start() { | 76 void Start() { |
| 77 ASSERT_TRUE(GetResponseBody(request_->url(), &response_data_)); | 77 ASSERT_TRUE(GetResponseBody(request_->url(), &response_data_)); |
| 78 | 78 |
| 79 // Start reading asynchronously so that all error reporting and data | 79 // Start reading asynchronously so that all error reporting and data |
| 80 // callbacks happen as they would for network requests. | 80 // callbacks happen as they would for network requests. |
| 81 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 81 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 82 this, &URLRequestJobTrackerTestJob::NotifyHeadersComplete)); | 82 this, &URLRequestJobTrackerTestJob::NotifyHeadersComplete)); |
| 83 } | 83 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 } | 144 } |
| 145 | 145 |
| 146 // Google Mock Matcher to check that two blocks of memory are equal. | 146 // Google Mock Matcher to check that two blocks of memory are equal. |
| 147 MATCHER_P2(MemEq, other, len, "") { | 147 MATCHER_P2(MemEq, other, len, "") { |
| 148 return memcmp(arg, other, len) == 0; | 148 return memcmp(arg, other, len) == 0; |
| 149 } | 149 } |
| 150 | 150 |
| 151 class URLRequestJobTrackerTest : public PlatformTest { | 151 class URLRequestJobTrackerTest : public PlatformTest { |
| 152 protected: | 152 protected: |
| 153 static void SetUpTestCase() { | 153 static void SetUpTestCase() { |
| 154 URLRequest::RegisterProtocolFactory("test", &Factory); | 154 net::URLRequest::RegisterProtocolFactory("test", &Factory); |
| 155 } | 155 } |
| 156 | 156 |
| 157 virtual void SetUp() { | 157 virtual void SetUp() { |
| 158 g_async_reads = true; | 158 g_async_reads = true; |
| 159 } | 159 } |
| 160 | 160 |
| 161 void AssertJobTrackerCallbacks(const char* url) { | 161 void AssertJobTrackerCallbacks(const char* url) { |
| 162 InSequence seq; | 162 InSequence seq; |
| 163 testing::StrictMock<MockJobObserver> observer; | 163 testing::StrictMock<MockJobObserver> observer; |
| 164 | 164 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 177 | 177 |
| 178 // Attach our observer and perform the resource fetch. | 178 // Attach our observer and perform the resource fetch. |
| 179 g_url_request_job_tracker.AddObserver(&observer); | 179 g_url_request_job_tracker.AddObserver(&observer); |
| 180 Fetch(gurl); | 180 Fetch(gurl); |
| 181 g_url_request_job_tracker.RemoveObserver(&observer); | 181 g_url_request_job_tracker.RemoveObserver(&observer); |
| 182 } | 182 } |
| 183 | 183 |
| 184 void Fetch(const GURL& url) { | 184 void Fetch(const GURL& url) { |
| 185 TestDelegate d; | 185 TestDelegate d; |
| 186 { | 186 { |
| 187 URLRequest request(url, &d); | 187 net::URLRequest request(url, &d); |
| 188 request.Start(); | 188 request.Start(); |
| 189 MessageLoop::current()->RunAllPending(); | 189 MessageLoop::current()->RunAllPending(); |
| 190 } | 190 } |
| 191 | 191 |
| 192 // A few sanity checks to make sure that the delegate also | 192 // A few sanity checks to make sure that the delegate also |
| 193 // receives the expected callbacks. | 193 // receives the expected callbacks. |
| 194 EXPECT_EQ(1, d.response_started_count()); | 194 EXPECT_EQ(1, d.response_started_count()); |
| 195 EXPECT_FALSE(d.received_data_before_response()); | 195 EXPECT_FALSE(d.received_data_before_response()); |
| 196 EXPECT_STREQ(kBasic, d.data_received().c_str()); | 196 EXPECT_STREQ(kBasic, d.data_received().c_str()); |
| 197 } | 197 } |
| 198 | 198 |
| 199 static URLRequest::ProtocolFactory Factory; | 199 static net::URLRequest::ProtocolFactory Factory; |
| 200 static bool g_async_reads; | 200 static bool g_async_reads; |
| 201 }; | 201 }; |
| 202 | 202 |
| 203 // static | 203 // static |
| 204 URLRequestJob* URLRequestJobTrackerTest::Factory(URLRequest* request, | 204 URLRequestJob* URLRequestJobTrackerTest::Factory(net::URLRequest* request, |
| 205 const std::string& scheme) { | 205 const std::string& scheme) { |
| 206 return new URLRequestJobTrackerTestJob(request, g_async_reads); | 206 return new URLRequestJobTrackerTestJob(request, g_async_reads); |
| 207 } | 207 } |
| 208 | 208 |
| 209 // static | 209 // static |
| 210 bool URLRequestJobTrackerTest::g_async_reads = true; | 210 bool URLRequestJobTrackerTest::g_async_reads = true; |
| 211 | 211 |
| 212 TEST_F(URLRequestJobTrackerTest, BasicAsync) { | 212 TEST_F(URLRequestJobTrackerTest, BasicAsync) { |
| 213 g_async_reads = true; | 213 g_async_reads = true; |
| 214 AssertJobTrackerCallbacks("test:basic"); | 214 AssertJobTrackerCallbacks("test:basic"); |
| 215 } | 215 } |
| 216 | 216 |
| 217 TEST_F(URLRequestJobTrackerTest, BasicSync) { | 217 TEST_F(URLRequestJobTrackerTest, BasicSync) { |
| 218 g_async_reads = false; | 218 g_async_reads = false; |
| 219 AssertJobTrackerCallbacks("test:basic"); | 219 AssertJobTrackerCallbacks("test:basic"); |
| 220 } | 220 } |
| 221 | 221 |
| 222 TEST_F(URLRequestJobTrackerTest, CompressedAsync) { | 222 TEST_F(URLRequestJobTrackerTest, CompressedAsync) { |
| 223 g_async_reads = true; | 223 g_async_reads = true; |
| 224 AssertJobTrackerCallbacks("test:compressed"); | 224 AssertJobTrackerCallbacks("test:compressed"); |
| 225 } | 225 } |
| 226 | 226 |
| 227 TEST_F(URLRequestJobTrackerTest, CompressedSync) { | 227 TEST_F(URLRequestJobTrackerTest, CompressedSync) { |
| 228 g_async_reads = false; | 228 g_async_reads = false; |
| 229 AssertJobTrackerCallbacks("test:compressed"); | 229 AssertJobTrackerCallbacks("test:compressed"); |
| 230 } | 230 } |
| 231 | 231 |
| 232 } // namespace | 232 } // namespace |
| OLD | NEW |