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

Side by Side Diff: third_party/WebKit/Source/modules/fetch/FetchBlobDataConsumerHandleTest.cpp

Issue 1264453002: Split the constructor of ThreadableLoader into two methods (ctor and start()) (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Uplaod MockThreadableLoader Created 4 years, 10 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "modules/fetch/FetchBlobDataConsumerHandle.h" 5 #include "modules/fetch/FetchBlobDataConsumerHandle.h"
6 6
7 #include "core/dom/ExecutionContext.h" 7 #include "core/dom/ExecutionContext.h"
8 #include "core/fetch/ResourceLoaderOptions.h" 8 #include "core/fetch/ResourceLoaderOptions.h"
9 #include "core/loader/ThreadableLoader.h" 9 #include "core/loader/MockThreadableLoader.h"
10 #include "core/loader/ThreadableLoaderClient.h" 10 #include "core/loader/ThreadableLoaderClient.h"
11 #include "core/testing/DummyPageHolder.h" 11 #include "core/testing/DummyPageHolder.h"
12 #include "modules/fetch/DataConsumerHandleTestUtil.h" 12 #include "modules/fetch/DataConsumerHandleTestUtil.h"
13 #include "platform/blob/BlobData.h" 13 #include "platform/blob/BlobData.h"
14 #include "platform/blob/BlobURL.h" 14 #include "platform/blob/BlobURL.h"
15 #include "platform/network/ResourceError.h" 15 #include "platform/network/ResourceError.h"
16 #include "platform/network/ResourceRequest.h" 16 #include "platform/network/ResourceRequest.h"
17 #include "platform/network/ResourceResponse.h" 17 #include "platform/network/ResourceResponse.h"
18 #include "platform/testing/UnitTestHelpers.h" 18 #include "platform/testing/UnitTestHelpers.h"
19 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
(...skipping 24 matching lines...) Expand all
44 using ::testing::DoAll; 44 using ::testing::DoAll;
45 using ::testing::InSequence; 45 using ::testing::InSequence;
46 using ::testing::Ref; 46 using ::testing::Ref;
47 using ::testing::Return; 47 using ::testing::Return;
48 using ::testing::SaveArg; 48 using ::testing::SaveArg;
49 using ::testing::StrictMock; 49 using ::testing::StrictMock;
50 using Checkpoint = StrictMock<::testing::MockFunction<void(int)>>; 50 using Checkpoint = StrictMock<::testing::MockFunction<void(int)>>;
51 51
52 class MockLoaderFactory : public FetchBlobDataConsumerHandle::LoaderFactory { 52 class MockLoaderFactory : public FetchBlobDataConsumerHandle::LoaderFactory {
53 public: 53 public:
54 MOCK_METHOD5(create, PassRefPtr<ThreadableLoader>(ExecutionContext&, Threada bleLoaderClient*, const ResourceRequest&, const ThreadableLoaderOptions&, const ResourceLoaderOptions&)); 54 MOCK_METHOD4(create, PassRefPtr<ThreadableLoader>(ExecutionContext&, Threada bleLoaderClient*, const ThreadableLoaderOptions&, const ResourceLoaderOptions&)) ;
55 };
56
57 class MockThreadableLoader : public ThreadableLoader {
58 public:
59 static PassRefPtr<MockThreadableLoader> create() { return adoptRef(new Stric tMock<MockThreadableLoader>); }
60
61 MOCK_METHOD1(overrideTimeout, void(unsigned long));
62 MOCK_METHOD0(cancel, void());
63
64 protected:
65 MockThreadableLoader() = default;
66 }; 55 };
67 56
68 PassRefPtr<BlobDataHandle> createBlobDataHandle(const char* s) 57 PassRefPtr<BlobDataHandle> createBlobDataHandle(const char* s)
69 { 58 {
70 OwnPtr<BlobData> data = BlobData::create(); 59 OwnPtr<BlobData> data = BlobData::create();
71 data->appendText(s, false); 60 data->appendText(s, false);
72 auto size = data->length(); 61 auto size = data->length();
73 return BlobDataHandle::create(data.release(), size); 62 return BlobDataHandle::create(data.release(), size);
74 } 63 }
75 64
(...skipping 25 matching lines...) Expand all
101 Checkpoint checkpoint; 90 Checkpoint checkpoint;
102 91
103 ResourceRequest request; 92 ResourceRequest request;
104 ThreadableLoaderOptions options; 93 ThreadableLoaderOptions options;
105 ResourceLoaderOptions resourceLoaderOptions; 94 ResourceLoaderOptions resourceLoaderOptions;
106 95
107 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); 96 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create();
108 97
109 InSequence s; 98 InSequence s;
110 EXPECT_CALL(checkpoint, Call(1)); 99 EXPECT_CALL(checkpoint, Call(1));
111 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll( 100 EXPECT_CALL(*factory, create(Ref(document()), _, _, _)).WillOnce(DoAll(
112 SaveArg<2>(&request), 101 SaveArg<2>(&options),
113 SaveArg<3>(&options), 102 SaveArg<3>(&resourceLoaderOptions),
114 SaveArg<4>(&resourceLoaderOptions),
115 Return(loader.get()))); 103 Return(loader.get())));
104 EXPECT_CALL(*loader, start(_)).WillOnce(SaveArg<0>(&request));
116 EXPECT_CALL(checkpoint, Call(2)); 105 EXPECT_CALL(checkpoint, Call(2));
117 EXPECT_CALL(*loader, cancel()); 106 EXPECT_CALL(*loader, cancel());
118 107
119 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me"); 108 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me");
120 OwnPtr<WebDataConsumerHandle> handle 109 OwnPtr<WebDataConsumerHandle> handle
121 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry); 110 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry);
122 testing::runPendingTasks(); 111 testing::runPendingTasks();
123 112
124 size_t size = 0; 113 size_t size = 0;
125 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); 114 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size);
(...skipping 19 matching lines...) Expand all
145 134
146 TEST_F(FetchBlobDataConsumerHandleTest, CancelLoaderWhenStopped) 135 TEST_F(FetchBlobDataConsumerHandleTest, CancelLoaderWhenStopped)
147 { 136 {
148 auto factory = new StrictMock<MockLoaderFactory>; 137 auto factory = new StrictMock<MockLoaderFactory>;
149 Checkpoint checkpoint; 138 Checkpoint checkpoint;
150 139
151 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); 140 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create();
152 141
153 InSequence s; 142 InSequence s;
154 EXPECT_CALL(checkpoint, Call(1)); 143 EXPECT_CALL(checkpoint, Call(1));
155 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(Return(l oader.get())); 144 EXPECT_CALL(*factory, create(Ref(document()), _, _, _)).WillOnce(Return(load er.get()));
145 EXPECT_CALL(*loader, start(_));
156 EXPECT_CALL(checkpoint, Call(2)); 146 EXPECT_CALL(checkpoint, Call(2));
157 EXPECT_CALL(*loader, cancel()); 147 EXPECT_CALL(*loader, cancel());
158 EXPECT_CALL(checkpoint, Call(3)); 148 EXPECT_CALL(checkpoint, Call(3));
159 149
160 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me"); 150 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me");
161 OwnPtr<WebDataConsumerHandle> handle 151 OwnPtr<WebDataConsumerHandle> handle
162 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry); 152 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry);
163 testing::runPendingTasks(); 153 testing::runPendingTasks();
164 154
165 size_t size = 0; 155 size_t size = 0;
166 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); 156 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size);
167 checkpoint.Call(1); 157 checkpoint.Call(1);
168 testing::runPendingTasks(); 158 testing::runPendingTasks();
169 checkpoint.Call(2); 159 checkpoint.Call(2);
170 document().stopActiveDOMObjects(); 160 document().stopActiveDOMObjects();
171 checkpoint.Call(3); 161 checkpoint.Call(3);
172 } 162 }
173 163
174 TEST_F(FetchBlobDataConsumerHandleTest, CancelLoaderWhenDestinationDetached) 164 TEST_F(FetchBlobDataConsumerHandleTest, CancelLoaderWhenDestinationDetached)
175 { 165 {
176 auto factory = new StrictMock<MockLoaderFactory>; 166 auto factory = new StrictMock<MockLoaderFactory>;
177 Checkpoint checkpoint; 167 Checkpoint checkpoint;
178 168
179 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); 169 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create();
180 170
181 InSequence s; 171 InSequence s;
182 EXPECT_CALL(checkpoint, Call(1)); 172 EXPECT_CALL(checkpoint, Call(1));
183 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(Return(l oader.get())); 173 EXPECT_CALL(*factory, create(Ref(document()), _, _, _)).WillOnce(Return(load er.get()));
174 EXPECT_CALL(*loader, start(_));
184 EXPECT_CALL(checkpoint, Call(2)); 175 EXPECT_CALL(checkpoint, Call(2));
185 EXPECT_CALL(checkpoint, Call(3)); 176 EXPECT_CALL(checkpoint, Call(3));
186 EXPECT_CALL(*loader, cancel()); 177 EXPECT_CALL(*loader, cancel());
187 EXPECT_CALL(checkpoint, Call(4)); 178 EXPECT_CALL(checkpoint, Call(4));
188 179
189 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me"); 180 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me");
190 OwnPtr<WebDataConsumerHandle> handle 181 OwnPtr<WebDataConsumerHandle> handle
191 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry); 182 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry);
192 OwnPtr<WebDataConsumerHandle::Reader> reader = handle->obtainReader(nullptr) ; 183 OwnPtr<WebDataConsumerHandle::Reader> reader = handle->obtainReader(nullptr) ;
193 testing::runPendingTasks(); 184 testing::runPendingTasks();
(...skipping 13 matching lines...) Expand all
207 TEST_F(FetchBlobDataConsumerHandleTest, ReadTest) 198 TEST_F(FetchBlobDataConsumerHandleTest, ReadTest)
208 { 199 {
209 auto factory = new StrictMock<MockLoaderFactory>; 200 auto factory = new StrictMock<MockLoaderFactory>;
210 Checkpoint checkpoint; 201 Checkpoint checkpoint;
211 202
212 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); 203 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create();
213 ThreadableLoaderClient* client = nullptr; 204 ThreadableLoaderClient* client = nullptr;
214 205
215 InSequence s; 206 InSequence s;
216 EXPECT_CALL(checkpoint, Call(1)); 207 EXPECT_CALL(checkpoint, Call(1));
217 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll(Sa veArg<1>(&client), Return(loader.get()))); 208 EXPECT_CALL(*factory, create(Ref(document()), _, _, _)).WillOnce(DoAll(SaveA rg<1>(&client), Return(loader.get())));
209 EXPECT_CALL(*loader, start(_));
218 EXPECT_CALL(checkpoint, Call(2)); 210 EXPECT_CALL(checkpoint, Call(2));
219 EXPECT_CALL(*loader, cancel()); 211 EXPECT_CALL(*loader, cancel());
220 212
221 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me"); 213 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me");
222 OwnPtr<WebDataConsumerHandle> handle 214 OwnPtr<WebDataConsumerHandle> handle
223 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry); 215 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry);
224 216
225 OwnPtr<ReplayingHandle> src = ReplayingHandle::create(); 217 OwnPtr<ReplayingHandle> src = ReplayingHandle::create();
226 src->add(Command(Command::Wait)); 218 src->add(Command(Command::Wait));
227 src->add(Command(Command::Data, "hello, ")); 219 src->add(Command(Command::Data, "hello, "));
(...skipping 16 matching lines...) Expand all
244 TEST_F(FetchBlobDataConsumerHandleTest, TwoPhaseReadTest) 236 TEST_F(FetchBlobDataConsumerHandleTest, TwoPhaseReadTest)
245 { 237 {
246 auto factory = new StrictMock<MockLoaderFactory>; 238 auto factory = new StrictMock<MockLoaderFactory>;
247 Checkpoint checkpoint; 239 Checkpoint checkpoint;
248 240
249 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); 241 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create();
250 ThreadableLoaderClient* client = nullptr; 242 ThreadableLoaderClient* client = nullptr;
251 243
252 InSequence s; 244 InSequence s;
253 EXPECT_CALL(checkpoint, Call(1)); 245 EXPECT_CALL(checkpoint, Call(1));
254 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll(Sa veArg<1>(&client), Return(loader.get()))); 246 EXPECT_CALL(*factory, create(Ref(document()), _, _, _)).WillOnce(DoAll(SaveA rg<1>(&client), Return(loader.get())));
247 EXPECT_CALL(*loader, start(_));
255 EXPECT_CALL(checkpoint, Call(2)); 248 EXPECT_CALL(checkpoint, Call(2));
256 EXPECT_CALL(*loader, cancel()); 249 EXPECT_CALL(*loader, cancel());
257 250
258 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me"); 251 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me");
259 OwnPtr<WebDataConsumerHandle> handle 252 OwnPtr<WebDataConsumerHandle> handle
260 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry); 253 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry);
261 254
262 OwnPtr<ReplayingHandle> src = ReplayingHandle::create(); 255 OwnPtr<ReplayingHandle> src = ReplayingHandle::create();
263 src->add(Command(Command::Wait)); 256 src->add(Command(Command::Wait));
264 src->add(Command(Command::Data, "hello, ")); 257 src->add(Command(Command::Data, "hello, "));
(...skipping 16 matching lines...) Expand all
281 TEST_F(FetchBlobDataConsumerHandleTest, LoadErrorTest) 274 TEST_F(FetchBlobDataConsumerHandleTest, LoadErrorTest)
282 { 275 {
283 auto factory = new StrictMock<MockLoaderFactory>; 276 auto factory = new StrictMock<MockLoaderFactory>;
284 Checkpoint checkpoint; 277 Checkpoint checkpoint;
285 278
286 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); 279 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create();
287 ThreadableLoaderClient* client = nullptr; 280 ThreadableLoaderClient* client = nullptr;
288 281
289 InSequence s; 282 InSequence s;
290 EXPECT_CALL(checkpoint, Call(1)); 283 EXPECT_CALL(checkpoint, Call(1));
291 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll(Sa veArg<1>(&client), Return(loader.get()))); 284 EXPECT_CALL(*factory, create(Ref(document()), _, _, _)).WillOnce(DoAll(SaveA rg<1>(&client), Return(loader.get())));
285 EXPECT_CALL(*loader, start(_));
292 EXPECT_CALL(checkpoint, Call(2)); 286 EXPECT_CALL(checkpoint, Call(2));
293 287
294 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me"); 288 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me");
295 OwnPtr<WebDataConsumerHandle> handle 289 OwnPtr<WebDataConsumerHandle> handle
296 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry); 290 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry);
297 291
298 size_t size = 0; 292 size_t size = 0;
299 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); 293 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size);
300 checkpoint.Call(1); 294 checkpoint.Call(1);
301 testing::runPendingTasks(); 295 testing::runPendingTasks();
302 checkpoint.Call(2); 296 checkpoint.Call(2);
303 client->didFail(ResourceError()); 297 client->didFail(ResourceError());
304 HandleReaderRunner<HandleReader> runner(handle.release()); 298 HandleReaderRunner<HandleReader> runner(handle.release());
305 OwnPtr<HandleReadResult> r = runner.wait(); 299 OwnPtr<HandleReadResult> r = runner.wait();
306 EXPECT_EQ(kUnexpectedError, r->result()); 300 EXPECT_EQ(kUnexpectedError, r->result());
307 } 301 }
308 302
309 TEST_F(FetchBlobDataConsumerHandleTest, BodyLoadErrorTest) 303 TEST_F(FetchBlobDataConsumerHandleTest, BodyLoadErrorTest)
310 { 304 {
311 auto factory = new StrictMock<MockLoaderFactory>; 305 auto factory = new StrictMock<MockLoaderFactory>;
312 Checkpoint checkpoint; 306 Checkpoint checkpoint;
313 307
314 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); 308 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create();
315 ThreadableLoaderClient* client = nullptr; 309 ThreadableLoaderClient* client = nullptr;
316 310
317 InSequence s; 311 InSequence s;
318 EXPECT_CALL(checkpoint, Call(1)); 312 EXPECT_CALL(checkpoint, Call(1));
319 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll(Sa veArg<1>(&client), Return(loader.get()))); 313 EXPECT_CALL(*factory, create(Ref(document()), _, _, _)).WillOnce(DoAll(SaveA rg<1>(&client), Return(loader.get())));
314 EXPECT_CALL(*loader, start(_));
320 EXPECT_CALL(checkpoint, Call(2)); 315 EXPECT_CALL(checkpoint, Call(2));
321 EXPECT_CALL(*loader, cancel()); 316 EXPECT_CALL(*loader, cancel());
322 317
323 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me"); 318 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me");
324 OwnPtr<WebDataConsumerHandle> handle 319 OwnPtr<WebDataConsumerHandle> handle
325 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry); 320 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry);
326 321
327 OwnPtr<ReplayingHandle> src = ReplayingHandle::create(); 322 OwnPtr<ReplayingHandle> src = ReplayingHandle::create();
328 src->add(Command(Command::Wait)); 323 src->add(Command(Command::Wait));
329 src->add(Command(Command::Data, "hello, ")); 324 src->add(Command(Command::Data, "hello, "));
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 OwnPtr<FetchDataConsumerHandle::Reader> reader = handle->obtainReader(nullpt r); 410 OwnPtr<FetchDataConsumerHandle::Reader> reader = handle->obtainReader(nullpt r);
416 411
417 const void* buffer; 412 const void* buffer;
418 size_t available; 413 size_t available;
419 EXPECT_EQ(kShouldWait, reader->beginRead(&buffer, kNone, &available)); 414 EXPECT_EQ(kShouldWait, reader->beginRead(&buffer, kNone, &available));
420 EXPECT_FALSE(reader->drainAsBlobDataHandle()); 415 EXPECT_FALSE(reader->drainAsBlobDataHandle());
421 } 416 }
422 417
423 } // namespace 418 } // namespace
424 } // namespace blink 419 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698