Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "config.h" | |
| 6 #include "modules/fetch/FetchBlobDataConsumerHandle.h" | |
| 7 | |
| 8 #include "core/dom/ExecutionContext.h" | |
| 9 #include "core/fetch/ResourceLoaderOptions.h" | |
| 10 #include "core/loader/ThreadableLoader.h" | |
| 11 #include "core/loader/ThreadableLoaderClient.h" | |
| 12 #include "core/testing/DummyPageHolder.h" | |
| 13 #include "modules/fetch/DataConsumerHandleTestUtil.h" | |
| 14 #include "platform/blob/BlobData.h" | |
| 15 #include "platform/blob/BlobURL.h" | |
| 16 #include "platform/network/ResourceError.h" | |
| 17 #include "platform/network/ResourceRequest.h" | |
| 18 #include "platform/network/ResourceResponse.h" | |
| 19 #include "platform/testing/UnitTestHelpers.h" | |
| 20 #include "wtf/PassRefPtr.h" | |
| 21 #include "wtf/RefPtr.h" | |
| 22 | |
| 23 #include <gmock/gmock.h> | |
| 24 #include <gtest/gtest.h> | |
| 25 #include <string.h> | |
| 26 | |
| 27 namespace blink { | |
| 28 namespace { | |
| 29 | |
| 30 using Result = WebDataConsumerHandle::Result; | |
| 31 const Result kUnexpectedError = WebDataConsumerHandle::UnexpectedError; | |
| 32 const Result kDone = WebDataConsumerHandle::Done; | |
| 33 using Flags = WebDataConsumerHandle::Flags; | |
| 34 const Flags kNone = WebDataConsumerHandle::FlagNone; | |
| 35 using Thread = DataConsumerHandleTestUtil::Thread; | |
| 36 using HandleReader = DataConsumerHandleTestUtil::HandleReader; | |
| 37 using HandleTwoPhaseReader = DataConsumerHandleTestUtil::HandleTwoPhaseReader; | |
| 38 using HandleReadResult = DataConsumerHandleTestUtil::HandleReadResult; | |
| 39 using ReplayingHandle = DataConsumerHandleTestUtil::ReplayingHandle; | |
| 40 using Command = DataConsumerHandleTestUtil::Command; | |
| 41 template <typename T> | |
| 42 using HandleReaderRunner = DataConsumerHandleTestUtil::HandleReaderRunner<T>; | |
| 43 | |
| 44 using ::testing::_; | |
| 45 using ::testing::DoAll; | |
| 46 using ::testing::InSequence; | |
| 47 using ::testing::Ref; | |
| 48 using ::testing::Return; | |
| 49 using ::testing::SaveArg; | |
| 50 using ::testing::StrictMock; | |
| 51 using Checkpoint = StrictMock<::testing::MockFunction<void(int)>>; | |
| 52 | |
| 53 class MockLoaderFactory : public FetchBlobDataConsumerHandle::LoaderFactory { | |
| 54 public: | |
| 55 MOCK_METHOD5(create, PassRefPtr<ThreadableLoader>(ExecutionContext&, Threada bleLoaderClient*, const ResourceRequest&, const ThreadableLoaderOptions&, const ResourceLoaderOptions&)); | |
| 56 }; | |
| 57 | |
| 58 class MockThreadableLoader : public ThreadableLoader { | |
| 59 public: | |
| 60 static PassRefPtr<MockThreadableLoader> create() { return adoptRef(new Stric tMock<MockThreadableLoader>); } | |
| 61 | |
| 62 MOCK_METHOD1(overrideTimeout, void(unsigned long)); | |
| 63 MOCK_METHOD0(cancel, void()); | |
| 64 | |
| 65 protected: | |
| 66 MockThreadableLoader() = default; | |
| 67 }; | |
| 68 | |
| 69 PassRefPtr<BlobDataHandle> createBlobDataHandle(const char* s) | |
| 70 { | |
| 71 OwnPtr<BlobData> data = BlobData::create(); | |
| 72 data->appendText(s, false); | |
| 73 auto size = data->length(); | |
| 74 return BlobDataHandle::create(data.release(), size); | |
| 75 } | |
| 76 | |
| 77 String toString(const Vector<char>& data) | |
| 78 { | |
| 79 return String(data.data(), data.size()); | |
| 80 } | |
| 81 | |
| 82 class FetchBlobDataConsumerHandleTest : public ::testing::Test { | |
| 83 public: | |
| 84 FetchBlobDataConsumerHandleTest() | |
| 85 : m_dummyPageHolder(DummyPageHolder::create(IntSize(1, 1))) | |
| 86 { | |
| 87 } | |
| 88 | |
| 89 ~FetchBlobDataConsumerHandleTest() override | |
| 90 { | |
| 91 m_dummyPageHolder = nullptr; | |
| 92 // We need this to collect garbage-collected mocks. | |
| 93 Heap::collectAllGarbage(); | |
| 94 } | |
| 95 | |
| 96 protected: | |
| 97 Document& document() { return m_dummyPageHolder->document(); } | |
| 98 | |
| 99 OwnPtr<DummyPageHolder> m_dummyPageHolder; | |
|
hiroshige
2015/07/15 11:27:55
|m_dummyPageHolder| can be private.
yhirano
2015/07/16 05:29:10
Done.
| |
| 100 }; | |
| 101 | |
| 102 TEST_F(FetchBlobDataConsumerHandleTest, CreateLoader) | |
| 103 { | |
| 104 auto factory = new StrictMock<MockLoaderFactory>; | |
| 105 Checkpoint checkpoint; | |
| 106 | |
| 107 ResourceRequest request; | |
| 108 ThreadableLoaderOptions options; | |
| 109 ResourceLoaderOptions resourceLoaderOptions; | |
| 110 | |
| 111 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); | |
| 112 | |
| 113 InSequence s; | |
| 114 EXPECT_CALL(checkpoint, Call(1)); | |
| 115 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll( | |
| 116 SaveArg<2>(&request), | |
| 117 SaveArg<3>(&options), | |
| 118 SaveArg<4>(&resourceLoaderOptions), | |
| 119 Return(loader.get()))); | |
| 120 EXPECT_CALL(checkpoint, Call(2)); | |
| 121 EXPECT_CALL(*loader, cancel()); | |
| 122 | |
| 123 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me"); | |
| 124 OwnPtr<WebDataConsumerHandle> handle | |
| 125 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry); | |
| 126 testing::runPendingTasks(); | |
| 127 | |
| 128 size_t size = 0; | |
| 129 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); | |
| 130 checkpoint.Call(1); | |
| 131 testing::runPendingTasks(); | |
| 132 checkpoint.Call(2); | |
| 133 | |
| 134 EXPECT_TRUE(request.url().string().startsWith("blob:")); | |
| 135 EXPECT_TRUE(request.useStreamOnResponse()); | |
| 136 | |
| 137 EXPECT_EQ(ConsiderPreflight, options.preflightPolicy); | |
| 138 EXPECT_EQ(DenyCrossOriginRequests, options.crossOriginRequestPolicy); | |
| 139 EXPECT_EQ(DoNotEnforceContentSecurityPolicy, options.contentSecurityPolicyEn forcement); | |
| 140 | |
| 141 EXPECT_EQ(DoNotBufferData, resourceLoaderOptions.dataBufferingPolicy); | |
| 142 EXPECT_EQ(DoNotAllowStoredCredentials, resourceLoaderOptions.allowCredential s); | |
| 143 EXPECT_EQ(ClientDidNotRequestCredentials, resourceLoaderOptions.credentialsR equested); | |
| 144 EXPECT_EQ(CheckContentSecurityPolicy, resourceLoaderOptions.contentSecurityP olicyOption); | |
| 145 EXPECT_EQ(DocumentContext, resourceLoaderOptions.requestInitiatorContext); | |
| 146 EXPECT_EQ(RequestAsynchronously, resourceLoaderOptions.synchronousPolicy); | |
| 147 EXPECT_EQ(NotCORSEnabled, resourceLoaderOptions.corsEnabled); | |
| 148 } | |
| 149 | |
| 150 TEST_F(FetchBlobDataConsumerHandleTest, CancelLoaderWhenStopped) | |
| 151 { | |
| 152 auto factory = new StrictMock<MockLoaderFactory>; | |
| 153 Checkpoint checkpoint; | |
| 154 | |
| 155 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); | |
| 156 | |
| 157 InSequence s; | |
| 158 EXPECT_CALL(checkpoint, Call(1)); | |
| 159 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(Return(l oader.get())); | |
| 160 EXPECT_CALL(checkpoint, Call(2)); | |
| 161 EXPECT_CALL(*loader, cancel()); | |
| 162 EXPECT_CALL(checkpoint, Call(3)); | |
| 163 | |
| 164 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me"); | |
| 165 OwnPtr<WebDataConsumerHandle> handle | |
| 166 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry); | |
| 167 testing::runPendingTasks(); | |
| 168 | |
| 169 size_t size = 0; | |
| 170 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); | |
| 171 checkpoint.Call(1); | |
| 172 testing::runPendingTasks(); | |
| 173 checkpoint.Call(2); | |
| 174 document().stopActiveDOMObjects(); | |
| 175 checkpoint.Call(3); | |
| 176 } | |
| 177 | |
| 178 TEST_F(FetchBlobDataConsumerHandleTest, CancelLoaderWhenDestinationDetached) | |
| 179 { | |
| 180 auto factory = new StrictMock<MockLoaderFactory>; | |
| 181 Checkpoint checkpoint; | |
| 182 | |
| 183 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); | |
| 184 | |
| 185 InSequence s; | |
| 186 EXPECT_CALL(checkpoint, Call(1)); | |
| 187 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(Return(l oader.get())); | |
| 188 EXPECT_CALL(checkpoint, Call(2)); | |
| 189 EXPECT_CALL(checkpoint, Call(3)); | |
| 190 EXPECT_CALL(*loader, cancel()); | |
| 191 EXPECT_CALL(checkpoint, Call(4)); | |
| 192 | |
| 193 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me"); | |
| 194 OwnPtr<WebDataConsumerHandle> handle | |
| 195 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry); | |
| 196 OwnPtr<WebDataConsumerHandle::Reader> reader = handle->obtainReader(nullptr) ; | |
| 197 testing::runPendingTasks(); | |
| 198 | |
| 199 size_t size = 0; | |
| 200 reader->read(nullptr, 0, kNone, &size); | |
| 201 checkpoint.Call(1); | |
| 202 testing::runPendingTasks(); | |
| 203 checkpoint.Call(2); | |
| 204 handle = nullptr; | |
| 205 reader = nullptr; | |
| 206 checkpoint.Call(3); | |
| 207 Heap::collectAllGarbage(); | |
| 208 checkpoint.Call(4); | |
| 209 } | |
| 210 | |
| 211 TEST_F(FetchBlobDataConsumerHandleTest, ReadTest) | |
| 212 { | |
| 213 auto factory = new StrictMock<MockLoaderFactory>; | |
| 214 Checkpoint checkpoint; | |
| 215 | |
| 216 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); | |
| 217 ThreadableLoaderClient* client = nullptr; | |
| 218 | |
| 219 InSequence s; | |
| 220 EXPECT_CALL(checkpoint, Call(1)); | |
| 221 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll(Sa veArg<1>(&client), Return(loader.get()))); | |
| 222 EXPECT_CALL(checkpoint, Call(2)); | |
| 223 EXPECT_CALL(*loader, cancel()); | |
| 224 | |
| 225 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me"); | |
| 226 OwnPtr<WebDataConsumerHandle> handle | |
| 227 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry); | |
| 228 | |
| 229 OwnPtr<ReplayingHandle> src = ReplayingHandle::create(); | |
| 230 src->add(Command(Command::Wait)); | |
| 231 src->add(Command(Command::Data, "hello, ")); | |
| 232 src->add(Command(Command::Data, "world")); | |
| 233 src->add(Command(Command::Wait)); | |
| 234 src->add(Command(Command::Done)); | |
| 235 | |
| 236 size_t size = 0; | |
| 237 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); | |
|
hiroshige
2015/07/15 11:27:54
optional: can we read() here and the following run
yhirano
2015/07/15 13:07:08
Sorry, I'm not sure what you propose: can you rest
hiroshige
2015/07/16 05:01:36
Er, I originally thought as the following but chan
yhirano
2015/07/16 05:29:10
Acknowledged.
| |
| 238 checkpoint.Call(1); | |
| 239 testing::runPendingTasks(); | |
| 240 checkpoint.Call(2); | |
| 241 client->didReceiveResponse(0, ResourceResponse(), src.release()); | |
| 242 HandleReaderRunner<HandleReader> runner(handle.release()); | |
| 243 OwnPtr<HandleReadResult> r = runner.wait(); | |
| 244 EXPECT_EQ(kDone, r->result()); | |
| 245 EXPECT_EQ("hello, world", toString(r->data())); | |
| 246 } | |
| 247 | |
| 248 TEST_F(FetchBlobDataConsumerHandleTest, TwoPhaseReadTest) | |
| 249 { | |
| 250 auto factory = new StrictMock<MockLoaderFactory>; | |
| 251 Checkpoint checkpoint; | |
| 252 | |
| 253 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); | |
| 254 ThreadableLoaderClient* client = nullptr; | |
| 255 | |
| 256 InSequence s; | |
| 257 EXPECT_CALL(checkpoint, Call(1)); | |
| 258 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll(Sa veArg<1>(&client), Return(loader.get()))); | |
| 259 EXPECT_CALL(checkpoint, Call(2)); | |
| 260 EXPECT_CALL(*loader, cancel()); | |
| 261 | |
| 262 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me"); | |
| 263 OwnPtr<WebDataConsumerHandle> handle | |
| 264 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry); | |
| 265 | |
| 266 OwnPtr<ReplayingHandle> src = ReplayingHandle::create(); | |
| 267 src->add(Command(Command::Wait)); | |
| 268 src->add(Command(Command::Data, "hello, ")); | |
| 269 src->add(Command(Command::Data, "world")); | |
| 270 src->add(Command(Command::Wait)); | |
| 271 src->add(Command(Command::Done)); | |
| 272 | |
| 273 size_t size = 0; | |
| 274 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); | |
|
hiroshige
2015/07/15 11:27:55
ditto.
yhirano
2015/07/16 05:29:10
Acknowledged.
| |
| 275 checkpoint.Call(1); | |
| 276 testing::runPendingTasks(); | |
| 277 checkpoint.Call(2); | |
| 278 client->didReceiveResponse(0, ResourceResponse(), src.release()); | |
| 279 HandleReaderRunner<HandleTwoPhaseReader> runner(handle.release()); | |
| 280 OwnPtr<HandleReadResult> r = runner.wait(); | |
| 281 EXPECT_EQ(kDone, r->result()); | |
| 282 EXPECT_EQ("hello, world", toString(r->data())); | |
| 283 } | |
| 284 | |
| 285 TEST_F(FetchBlobDataConsumerHandleTest, LoadErrorTest) | |
| 286 { | |
| 287 auto factory = new StrictMock<MockLoaderFactory>; | |
| 288 Checkpoint checkpoint; | |
| 289 | |
| 290 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); | |
| 291 ThreadableLoaderClient* client = nullptr; | |
| 292 | |
| 293 InSequence s; | |
| 294 EXPECT_CALL(checkpoint, Call(1)); | |
| 295 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll(Sa veArg<1>(&client), Return(loader.get()))); | |
| 296 EXPECT_CALL(checkpoint, Call(2)); | |
| 297 | |
| 298 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me"); | |
| 299 OwnPtr<WebDataConsumerHandle> handle | |
| 300 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry); | |
| 301 | |
| 302 size_t size = 0; | |
| 303 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); | |
| 304 checkpoint.Call(1); | |
| 305 testing::runPendingTasks(); | |
| 306 checkpoint.Call(2); | |
| 307 client->didFail(ResourceError()); | |
| 308 HandleReaderRunner<HandleReader> runner(handle.release()); | |
| 309 OwnPtr<HandleReadResult> r = runner.wait(); | |
| 310 EXPECT_EQ(kUnexpectedError, r->result()); | |
| 311 } | |
| 312 | |
| 313 TEST_F(FetchBlobDataConsumerHandleTest, BodyLoadErrorTest) | |
| 314 { | |
| 315 auto factory = new StrictMock<MockLoaderFactory>; | |
| 316 Checkpoint checkpoint; | |
| 317 | |
| 318 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); | |
| 319 ThreadableLoaderClient* client = nullptr; | |
| 320 | |
| 321 InSequence s; | |
| 322 EXPECT_CALL(checkpoint, Call(1)); | |
| 323 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll(Sa veArg<1>(&client), Return(loader.get()))); | |
| 324 EXPECT_CALL(checkpoint, Call(2)); | |
| 325 EXPECT_CALL(*loader, cancel()); | |
| 326 | |
| 327 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti me"); | |
| 328 OwnPtr<WebDataConsumerHandle> handle | |
| 329 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto ry); | |
| 330 | |
| 331 OwnPtr<ReplayingHandle> src = ReplayingHandle::create(); | |
| 332 src->add(Command(Command::Wait)); | |
| 333 src->add(Command(Command::Data, "hello, ")); | |
| 334 src->add(Command(Command::Error)); | |
| 335 | |
| 336 size_t size = 0; | |
| 337 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); | |
| 338 checkpoint.Call(1); | |
| 339 testing::runPendingTasks(); | |
| 340 checkpoint.Call(2); | |
| 341 client->didReceiveResponse(0, ResourceResponse(), src.release()); | |
| 342 HandleReaderRunner<HandleReader> runner(handle.release()); | |
| 343 OwnPtr<HandleReadResult> r = runner.wait(); | |
| 344 EXPECT_EQ(kUnexpectedError, r->result()); | |
| 345 } | |
| 346 | |
| 347 } // namespace | |
| 348 } // namespace blink | |
| OLD | NEW |