OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include "content/child/url_response_body_consumer.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_forward.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "content/child/request_info.h" |
| 13 #include "content/child/resource_dispatcher.h" |
| 14 #include "content/common/resource_messages.h" |
| 15 #include "content/common/resource_request_completion_status.h" |
| 16 #include "content/public/child/request_peer.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace content { |
| 20 |
| 21 namespace { |
| 22 |
| 23 class TestRequestPeer : public RequestPeer { |
| 24 public: |
| 25 struct Context; |
| 26 TestRequestPeer(ResourceDispatcher* dispatcher, Context* context) |
| 27 : dispatcher_(dispatcher), context_(context) {} |
| 28 |
| 29 void OnUploadProgress(uint64_t position, uint64_t size) override { |
| 30 EXPECT_TRUE(false); |
| 31 } |
| 32 |
| 33 bool OnReceivedRedirect(const net::RedirectInfo& redirect_info, |
| 34 const ResourceResponseInfo& info) override { |
| 35 EXPECT_TRUE(false); |
| 36 return false; |
| 37 } |
| 38 |
| 39 void OnReceivedResponse(const ResourceResponseInfo& info) override { |
| 40 EXPECT_TRUE(false); |
| 41 } |
| 42 |
| 43 void OnDownloadedData(int len, int encoded_data_length) override { |
| 44 EXPECT_TRUE(false); |
| 45 } |
| 46 |
| 47 void OnReceivedData(std::unique_ptr<ReceivedData> data) override { |
| 48 EXPECT_FALSE(context_->complete); |
| 49 context_->data.append(data->payload(), data->length()); |
| 50 context_->run_loop_quit_closure.Run(); |
| 51 } |
| 52 |
| 53 void OnCompletedRequest(int error_code, |
| 54 bool was_ignored_by_handler, |
| 55 bool stale_copy_in_cache, |
| 56 const std::string& security_info, |
| 57 const base::TimeTicks& completion_time, |
| 58 int64_t total_transfer_size) override { |
| 59 EXPECT_FALSE(context_->complete); |
| 60 context_->complete = true; |
| 61 context_->error_code = error_code; |
| 62 context_->run_loop_quit_closure.Run(); |
| 63 } |
| 64 |
| 65 struct Context { |
| 66 // Data received. If downloading to file, remains empty. |
| 67 std::string data; |
| 68 bool complete = false; |
| 69 base::Closure run_loop_quit_closure; |
| 70 int error_code = net::OK; |
| 71 }; |
| 72 |
| 73 private: |
| 74 ResourceDispatcher* dispatcher_; |
| 75 Context* context_; |
| 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(TestRequestPeer); |
| 78 }; |
| 79 |
| 80 class URLResponseBodyConsumerTest : public ::testing::Test, |
| 81 public ::IPC::Sender { |
| 82 protected: |
| 83 URLResponseBodyConsumerTest() |
| 84 : dispatcher_(new ResourceDispatcher(this, message_loop_.task_runner())) { |
| 85 } |
| 86 |
| 87 ~URLResponseBodyConsumerTest() override { |
| 88 dispatcher_.reset(); |
| 89 base::RunLoop().RunUntilIdle(); |
| 90 } |
| 91 |
| 92 bool Send(IPC::Message* message) override { |
| 93 delete message; |
| 94 return true; |
| 95 } |
| 96 |
| 97 std::unique_ptr<RequestInfo> CreateRequestInfo() { |
| 98 std::unique_ptr<RequestInfo> request_info(new RequestInfo); |
| 99 request_info->method = "GET"; |
| 100 request_info->url = GURL("http://www.example.com/"); |
| 101 return request_info; |
| 102 } |
| 103 |
| 104 MojoCreateDataPipeOptions CreateDataPipeOptions() { |
| 105 MojoCreateDataPipeOptions options; |
| 106 options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| 107 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 108 options.element_num_bytes = 1; |
| 109 options.capacity_num_bytes = 1024; |
| 110 return options; |
| 111 } |
| 112 |
| 113 void Run(TestRequestPeer::Context* context) { |
| 114 base::RunLoop run_loop; |
| 115 context->run_loop_quit_closure = run_loop.QuitClosure(); |
| 116 run_loop.Run(); |
| 117 } |
| 118 |
| 119 base::MessageLoop message_loop_; |
| 120 std::unique_ptr<ResourceDispatcher> dispatcher_; |
| 121 static const MojoWriteDataFlags kNone = MOJO_WRITE_DATA_FLAG_NONE; |
| 122 }; |
| 123 |
| 124 TEST_F(URLResponseBodyConsumerTest, ReceiveData) { |
| 125 TestRequestPeer::Context context; |
| 126 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo()); |
| 127 int request_id = dispatcher_->StartAsync( |
| 128 *request_info, nullptr, |
| 129 base::WrapUnique(new TestRequestPeer(dispatcher_.get(), &context)), |
| 130 nullptr); |
| 131 mojo::DataPipe data_pipe(CreateDataPipeOptions()); |
| 132 |
| 133 scoped_refptr<URLResponseBodyConsumer> consumer(new URLResponseBodyConsumer( |
| 134 request_id, dispatcher_.get(), std::move(data_pipe.consumer_handle))); |
| 135 |
| 136 mojo::ScopedDataPipeProducerHandle writer = |
| 137 std::move(data_pipe.producer_handle); |
| 138 std::string buffer = "hello"; |
| 139 uint32_t size = buffer.size(); |
| 140 MojoResult result = |
| 141 mojo::WriteDataRaw(writer.get(), buffer.c_str(), &size, kNone); |
| 142 ASSERT_EQ(MOJO_RESULT_OK, result); |
| 143 ASSERT_EQ(buffer.size(), size); |
| 144 |
| 145 Run(&context); |
| 146 |
| 147 EXPECT_FALSE(context.complete); |
| 148 EXPECT_EQ("hello", context.data); |
| 149 } |
| 150 |
| 151 TEST_F(URLResponseBodyConsumerTest, OnCompleteThenClose) { |
| 152 TestRequestPeer::Context context; |
| 153 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo()); |
| 154 int request_id = dispatcher_->StartAsync( |
| 155 *request_info, nullptr, |
| 156 base::WrapUnique(new TestRequestPeer(dispatcher_.get(), &context)), |
| 157 nullptr); |
| 158 mojo::DataPipe data_pipe(CreateDataPipeOptions()); |
| 159 |
| 160 scoped_refptr<URLResponseBodyConsumer> consumer(new URLResponseBodyConsumer( |
| 161 request_id, dispatcher_.get(), std::move(data_pipe.consumer_handle))); |
| 162 |
| 163 consumer->OnComplete(ResourceRequestCompletionStatus()); |
| 164 mojo::ScopedDataPipeProducerHandle writer = |
| 165 std::move(data_pipe.producer_handle); |
| 166 std::string buffer = "hello"; |
| 167 uint32_t size = buffer.size(); |
| 168 MojoResult result = |
| 169 mojo::WriteDataRaw(writer.get(), buffer.c_str(), &size, kNone); |
| 170 ASSERT_EQ(MOJO_RESULT_OK, result); |
| 171 ASSERT_EQ(buffer.size(), size); |
| 172 writer.reset(); |
| 173 |
| 174 Run(&context); |
| 175 |
| 176 EXPECT_FALSE(context.complete); |
| 177 EXPECT_EQ("hello", context.data); |
| 178 |
| 179 Run(&context); |
| 180 |
| 181 EXPECT_TRUE(context.complete); |
| 182 EXPECT_EQ("hello", context.data); |
| 183 } |
| 184 |
| 185 TEST_F(URLResponseBodyConsumerTest, CloseThenOnComplete) { |
| 186 TestRequestPeer::Context context; |
| 187 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo()); |
| 188 int request_id = dispatcher_->StartAsync( |
| 189 *request_info, nullptr, |
| 190 base::WrapUnique(new TestRequestPeer(dispatcher_.get(), &context)), |
| 191 nullptr); |
| 192 mojo::DataPipe data_pipe(CreateDataPipeOptions()); |
| 193 |
| 194 scoped_refptr<URLResponseBodyConsumer> consumer(new URLResponseBodyConsumer( |
| 195 request_id, dispatcher_.get(), std::move(data_pipe.consumer_handle))); |
| 196 |
| 197 ResourceRequestCompletionStatus status; |
| 198 status.error_code = net::ERR_FAILED; |
| 199 data_pipe.producer_handle.reset(); |
| 200 consumer->OnComplete(status); |
| 201 |
| 202 Run(&context); |
| 203 |
| 204 EXPECT_TRUE(context.complete); |
| 205 EXPECT_EQ(net::ERR_FAILED, context.error_code); |
| 206 EXPECT_EQ("", context.data); |
| 207 } |
| 208 |
| 209 } // namespace |
| 210 |
| 211 } // namespace content |
OLD | NEW |