Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: content/child/url_response_body_consumer_unittest.cc

Issue 1970693002: Use mojo for Chrome Loading, Part 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/child/url_response_body_consumer.cc ('k') | content/child/web_url_loader_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 explicit TestRequestPeer(Context* context) : context_(context) {}
27
28 void OnUploadProgress(uint64_t position, uint64_t size) override {
29 ADD_FAILURE() << "OnUploadProgress should not be called.";
30 }
31
32 bool OnReceivedRedirect(const net::RedirectInfo& redirect_info,
33 const ResourceResponseInfo& info) override {
34 ADD_FAILURE() << "OnReceivedRedirect should not be called.";
35 return false;
36 }
37
38 void OnReceivedResponse(const ResourceResponseInfo& info) override {
39 ADD_FAILURE() << "OnReceivedResponse should not be called.";
40 }
41
42 void OnDownloadedData(int len, int encoded_data_length) override {
43 ADD_FAILURE() << "OnDownloadedData should not be called.";
44 }
45
46 void OnReceivedData(std::unique_ptr<ReceivedData> data) override {
47 EXPECT_FALSE(context_->complete);
48 context_->data.append(data->payload(), data->length());
49 context_->run_loop_quit_closure.Run();
50 }
51
52 void OnCompletedRequest(int error_code,
53 bool was_ignored_by_handler,
54 bool stale_copy_in_cache,
55 const std::string& security_info,
56 const base::TimeTicks& completion_time,
57 int64_t total_transfer_size) override {
58 EXPECT_FALSE(context_->complete);
59 context_->complete = true;
60 context_->error_code = error_code;
61 context_->run_loop_quit_closure.Run();
62 }
63
64 struct Context {
65 // Data received. If downloading to file, remains empty.
66 std::string data;
67 bool complete = false;
68 base::Closure run_loop_quit_closure;
69 int error_code = net::OK;
70 };
71
72 private:
73 Context* context_;
74
75 DISALLOW_COPY_AND_ASSIGN(TestRequestPeer);
76 };
77
78 class URLResponseBodyConsumerTest : public ::testing::Test,
79 public ::IPC::Sender {
80 protected:
81 URLResponseBodyConsumerTest()
82 : dispatcher_(new ResourceDispatcher(this, message_loop_.task_runner())) {
83 }
84
85 ~URLResponseBodyConsumerTest() override {
86 dispatcher_.reset();
87 base::RunLoop().RunUntilIdle();
88 }
89
90 bool Send(IPC::Message* message) override {
91 delete message;
92 return true;
93 }
94
95 std::unique_ptr<RequestInfo> CreateRequestInfo() {
96 std::unique_ptr<RequestInfo> request_info(new RequestInfo);
97 request_info->method = "GET";
98 request_info->url = GURL("http://www.example.com/");
99 return request_info;
100 }
101
102 MojoCreateDataPipeOptions CreateDataPipeOptions() {
103 MojoCreateDataPipeOptions options;
104 options.struct_size = sizeof(MojoCreateDataPipeOptions);
105 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
106 options.element_num_bytes = 1;
107 options.capacity_num_bytes = 1024;
108 return options;
109 }
110
111 // Returns the request id.
112 int SetUpRequestPeer(const RequestInfo& request_info,
113 TestRequestPeer::Context* context) {
114 return dispatcher_->StartAsync(
115 request_info, nullptr, base::WrapUnique(new TestRequestPeer(context)),
116 blink::WebURLRequest::LoadingIPCType::ChromeIPC, nullptr);
117 }
118
119 void Run(TestRequestPeer::Context* context) {
120 base::RunLoop run_loop;
121 context->run_loop_quit_closure = run_loop.QuitClosure();
122 run_loop.Run();
123 }
124
125 base::MessageLoop message_loop_;
126 std::unique_ptr<ResourceDispatcher> dispatcher_;
127 static const MojoWriteDataFlags kNone = MOJO_WRITE_DATA_FLAG_NONE;
128 };
129
130 TEST_F(URLResponseBodyConsumerTest, ReceiveData) {
131 TestRequestPeer::Context context;
132 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo());
133 int request_id = SetUpRequestPeer(*request_info, &context);
134 mojo::DataPipe data_pipe(CreateDataPipeOptions());
135
136 scoped_refptr<URLResponseBodyConsumer> consumer(new URLResponseBodyConsumer(
137 request_id, dispatcher_.get(), std::move(data_pipe.consumer_handle),
138 message_loop_.task_runner().get()));
139
140 mojo::ScopedDataPipeProducerHandle writer =
141 std::move(data_pipe.producer_handle);
142 std::string buffer = "hello";
143 uint32_t size = buffer.size();
144 MojoResult result =
145 mojo::WriteDataRaw(writer.get(), buffer.c_str(), &size, kNone);
146 ASSERT_EQ(MOJO_RESULT_OK, result);
147 ASSERT_EQ(buffer.size(), size);
148
149 Run(&context);
150
151 EXPECT_FALSE(context.complete);
152 EXPECT_EQ("hello", context.data);
153 }
154
155 TEST_F(URLResponseBodyConsumerTest, OnCompleteThenClose) {
156 TestRequestPeer::Context context;
157 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo());
158 int request_id = SetUpRequestPeer(*request_info, &context);
159 mojo::DataPipe data_pipe(CreateDataPipeOptions());
160
161 scoped_refptr<URLResponseBodyConsumer> consumer(new URLResponseBodyConsumer(
162 request_id, dispatcher_.get(), std::move(data_pipe.consumer_handle),
163 message_loop_.task_runner().get()));
164
165 consumer->OnComplete(ResourceRequestCompletionStatus());
166 mojo::ScopedDataPipeProducerHandle writer =
167 std::move(data_pipe.producer_handle);
168 std::string buffer = "hello";
169 uint32_t size = buffer.size();
170 MojoResult result =
171 mojo::WriteDataRaw(writer.get(), buffer.c_str(), &size, kNone);
172 ASSERT_EQ(MOJO_RESULT_OK, result);
173 ASSERT_EQ(buffer.size(), size);
174
175 Run(&context);
176
177 writer.reset();
178 EXPECT_FALSE(context.complete);
179 EXPECT_EQ("hello", context.data);
180
181 Run(&context);
182
183 EXPECT_TRUE(context.complete);
184 EXPECT_EQ("hello", context.data);
185 }
186
187 TEST_F(URLResponseBodyConsumerTest, CloseThenOnComplete) {
188 TestRequestPeer::Context context;
189 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo());
190 int request_id = SetUpRequestPeer(*request_info, &context);
191 mojo::DataPipe data_pipe(CreateDataPipeOptions());
192
193 scoped_refptr<URLResponseBodyConsumer> consumer(new URLResponseBodyConsumer(
194 request_id, dispatcher_.get(), std::move(data_pipe.consumer_handle),
195 message_loop_.task_runner().get()));
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
OLDNEW
« no previous file with comments | « content/child/url_response_body_consumer.cc ('k') | content/child/web_url_loader_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698