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 "modules/fetch/BlobBytesConsumer.h" |
| 6 |
| 7 #include "core/dom/Document.h" |
| 8 #include "core/loader/ThreadableLoader.h" |
| 9 #include "core/testing/DummyPageHolder.h" |
| 10 #include "modules/fetch/BytesConsumerTestUtil.h" |
| 11 #include "modules/fetch/DataConsumerHandleTestUtil.h" |
| 12 #include "platform/blob/BlobData.h" |
| 13 #include "platform/network/EncodedFormData.h" |
| 14 #include "platform/network/ResourceError.h" |
| 15 #include "platform/network/ResourceResponse.h" |
| 16 #include "platform/testing/UnitTestHelpers.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "wtf/Vector.h" |
| 19 #include "wtf/text/WTFString.h" |
| 20 |
| 21 namespace blink { |
| 22 |
| 23 namespace { |
| 24 |
| 25 using Command = DataConsumerHandleTestUtil::Command; |
| 26 using PublicState = BytesConsumer::PublicState; |
| 27 using ReplayingHandle = DataConsumerHandleTestUtil::ReplayingHandle; |
| 28 using Result = BytesConsumer::Result; |
| 29 |
| 30 String toString(const Vector<char>& v) |
| 31 { |
| 32 return String(v.data(), v.size()); |
| 33 } |
| 34 |
| 35 class TestThreadableLoader : public ThreadableLoader { |
| 36 public: |
| 37 ~TestThreadableLoader() override |
| 38 { |
| 39 EXPECT_FALSE(m_shouldBeCancelled && !m_isCancelled) << "The loader shoul
d be cancelled but is not cancelled."; |
| 40 } |
| 41 |
| 42 void start(const ResourceRequest& request) override |
| 43 { |
| 44 m_isStarted = true; |
| 45 } |
| 46 |
| 47 void overrideTimeout(unsigned long timeoutMilliseconds) override |
| 48 { |
| 49 ADD_FAILURE() << "overrideTimeout should not be called."; |
| 50 } |
| 51 |
| 52 void cancel() override |
| 53 { |
| 54 m_isCancelled = true; |
| 55 } |
| 56 |
| 57 bool isStarted() const { return m_isStarted; } |
| 58 bool isCancelled() const { return m_isCancelled; } |
| 59 void setShouldBeCancelled() { m_shouldBeCancelled = true; } |
| 60 |
| 61 private: |
| 62 bool m_isStarted = false; |
| 63 bool m_isCancelled = false; |
| 64 bool m_shouldBeCancelled = false; |
| 65 }; |
| 66 |
| 67 class SyncLoadingTestThreadableLoader : public ThreadableLoader { |
| 68 public: |
| 69 ~SyncLoadingTestThreadableLoader() override |
| 70 { |
| 71 DCHECK(!m_handle); |
| 72 } |
| 73 |
| 74 void start(const ResourceRequest& request) override |
| 75 { |
| 76 m_isStarted = true; |
| 77 m_client->didReceiveResponse(0, ResourceResponse(), std::move(m_handle))
; |
| 78 m_client->didFinishLoading(0, 0); |
| 79 } |
| 80 |
| 81 void overrideTimeout(unsigned long timeoutMilliseconds) override |
| 82 { |
| 83 ADD_FAILURE() << "overrideTimeout should not be called."; |
| 84 } |
| 85 |
| 86 void cancel() override |
| 87 { |
| 88 m_isCancelled = true; |
| 89 } |
| 90 |
| 91 bool isStarted() const { return m_isStarted; } |
| 92 bool isCancelled() const { return m_isCancelled; } |
| 93 |
| 94 void setClient(ThreadableLoaderClient* client) |
| 95 { |
| 96 m_client = client; |
| 97 } |
| 98 |
| 99 void setHandle(std::unique_ptr<WebDataConsumerHandle> handle) |
| 100 { |
| 101 m_handle = std::move(handle); |
| 102 } |
| 103 |
| 104 private: |
| 105 bool m_isStarted = false; |
| 106 bool m_isCancelled = false; |
| 107 ThreadableLoaderClient* m_client = nullptr; |
| 108 std::unique_ptr<WebDataConsumerHandle> m_handle; |
| 109 }; |
| 110 |
| 111 class SyncErrorTestThreadableLoader : public ThreadableLoader { |
| 112 public: |
| 113 ~SyncErrorTestThreadableLoader() override {} |
| 114 |
| 115 void start(const ResourceRequest& request) override |
| 116 { |
| 117 m_isStarted = true; |
| 118 m_client->didFail(ResourceError()); |
| 119 } |
| 120 |
| 121 void overrideTimeout(unsigned long timeoutMilliseconds) override |
| 122 { |
| 123 ADD_FAILURE() << "overrideTimeout should not be called."; |
| 124 } |
| 125 |
| 126 void cancel() override |
| 127 { |
| 128 m_isCancelled = true; |
| 129 } |
| 130 |
| 131 bool isStarted() const { return m_isStarted; } |
| 132 bool isCancelled() const { return m_isCancelled; } |
| 133 |
| 134 void setClient(ThreadableLoaderClient* client) |
| 135 { |
| 136 m_client = client; |
| 137 } |
| 138 |
| 139 private: |
| 140 bool m_isStarted = false; |
| 141 bool m_isCancelled = false; |
| 142 ThreadableLoaderClient* m_client = nullptr; |
| 143 }; |
| 144 |
| 145 class TestClient final : public GarbageCollectedFinalized<TestClient>, public By
tesConsumer::Client { |
| 146 USING_GARBAGE_COLLECTED_MIXIN(TestClient); |
| 147 public: |
| 148 void onStateChange() override { ++m_numOnStateChangeCalled; } |
| 149 int numOnStateChangeCalled() const { return m_numOnStateChangeCalled; } |
| 150 |
| 151 private: |
| 152 int m_numOnStateChangeCalled = 0; |
| 153 }; |
| 154 |
| 155 class BlobBytesConsumerTest : public ::testing::Test { |
| 156 public: |
| 157 BlobBytesConsumerTest() |
| 158 : m_dummyPageHolder(DummyPageHolder::create(IntSize(1, 1))) {} |
| 159 |
| 160 Document& document() { return m_dummyPageHolder->document(); } |
| 161 |
| 162 private: |
| 163 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; |
| 164 }; |
| 165 |
| 166 TEST_F(BlobBytesConsumerTest, TwoPhaseRead) |
| 167 { |
| 168 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), 12345); |
| 169 TestThreadableLoader* loader = new TestThreadableLoader(); |
| 170 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document(
), blobDataHandle, loader); |
| 171 std::unique_ptr<ReplayingHandle> src = ReplayingHandle::create(); |
| 172 src->add(Command(Command::Data, "hello, ")); |
| 173 src->add(Command(Command::Wait)); |
| 174 src->add(Command(Command::Data, "world")); |
| 175 src->add(Command(Command::Done)); |
| 176 |
| 177 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 178 EXPECT_FALSE(loader->isStarted()); |
| 179 |
| 180 const char* buffer = nullptr; |
| 181 size_t available = 0; |
| 182 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); |
| 183 EXPECT_TRUE(loader->isStarted()); |
| 184 EXPECT_FALSE(consumer->drainAsBlobDataHandle(BytesConsumer::BlobSizePolicy::
AllowBlobWithInvalidSize)); |
| 185 EXPECT_FALSE(consumer->drainAsFormData()); |
| 186 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 187 |
| 188 consumer->didReceiveResponse(0, ResourceResponse(), std::move(src)); |
| 189 consumer->didFinishLoading(0, 0); |
| 190 |
| 191 auto result = (new BytesConsumerTestUtil::TwoPhaseReader(consumer))->run(); |
| 192 EXPECT_EQ(Result::Done, result.first); |
| 193 EXPECT_EQ("hello, world", toString(result.second)); |
| 194 } |
| 195 |
| 196 TEST_F(BlobBytesConsumerTest, FailLoading) |
| 197 { |
| 198 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), 12345); |
| 199 TestThreadableLoader* loader = new TestThreadableLoader(); |
| 200 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document(
), blobDataHandle, loader); |
| 201 TestClient* client = new TestClient(); |
| 202 consumer->setClient(client); |
| 203 |
| 204 const char* buffer = nullptr; |
| 205 size_t available = 0; |
| 206 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); |
| 207 EXPECT_TRUE(loader->isStarted()); |
| 208 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 209 |
| 210 int numOnStateChangeCalled = client->numOnStateChangeCalled(); |
| 211 consumer->didFail(ResourceError()); |
| 212 |
| 213 EXPECT_EQ(numOnStateChangeCalled + 1, client->numOnStateChangeCalled()); |
| 214 EXPECT_EQ(PublicState::Errored, consumer->getPublicState()); |
| 215 EXPECT_EQ(Result::Error, consumer->beginRead(&buffer, &available)); |
| 216 } |
| 217 |
| 218 TEST_F(BlobBytesConsumerTest, FailLoadingAfterResponseReceived) |
| 219 { |
| 220 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), 12345); |
| 221 TestThreadableLoader* loader = new TestThreadableLoader(); |
| 222 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document(
), blobDataHandle, loader); |
| 223 TestClient* client = new TestClient(); |
| 224 consumer->setClient(client); |
| 225 |
| 226 const char* buffer = nullptr; |
| 227 size_t available; |
| 228 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); |
| 229 EXPECT_TRUE(loader->isStarted()); |
| 230 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 231 |
| 232 int numOnStateChangeCalled = client->numOnStateChangeCalled(); |
| 233 consumer->didReceiveResponse(0, ResourceResponse(), createWaitingDataConsume
rHandle()); |
| 234 EXPECT_EQ(numOnStateChangeCalled + 1, client->numOnStateChangeCalled()); |
| 235 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); |
| 236 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 237 |
| 238 consumer->didFail(ResourceError()); |
| 239 EXPECT_EQ(numOnStateChangeCalled + 2, client->numOnStateChangeCalled()); |
| 240 EXPECT_EQ(PublicState::Errored, consumer->getPublicState()); |
| 241 EXPECT_EQ(Result::Error, consumer->beginRead(&buffer, &available)); |
| 242 } |
| 243 |
| 244 TEST_F(BlobBytesConsumerTest, FailAccessControlCheck) |
| 245 { |
| 246 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), 12345); |
| 247 TestThreadableLoader* loader = new TestThreadableLoader(); |
| 248 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document(
), blobDataHandle, loader); |
| 249 TestClient* client = new TestClient(); |
| 250 consumer->setClient(client); |
| 251 |
| 252 const char* buffer = nullptr; |
| 253 size_t available; |
| 254 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); |
| 255 EXPECT_TRUE(loader->isStarted()); |
| 256 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 257 |
| 258 int numOnStateChangeCalled = client->numOnStateChangeCalled(); |
| 259 consumer->didFailAccessControlCheck(ResourceError()); |
| 260 EXPECT_EQ(numOnStateChangeCalled + 1, client->numOnStateChangeCalled()); |
| 261 |
| 262 EXPECT_EQ(PublicState::Errored, consumer->getPublicState()); |
| 263 EXPECT_EQ(Result::Error, consumer->beginRead(&buffer, &available)); |
| 264 } |
| 265 |
| 266 TEST_F(BlobBytesConsumerTest, CancelBeforeStarting) |
| 267 { |
| 268 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), 12345); |
| 269 TestThreadableLoader* loader = new TestThreadableLoader(); |
| 270 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document(
), blobDataHandle, loader); |
| 271 TestClient* client = new TestClient(); |
| 272 consumer->setClient(client); |
| 273 |
| 274 consumer->cancel(); |
| 275 // This should be FALSE in production, but TRUE here because we set the |
| 276 // loader before starting loading in tests. |
| 277 EXPECT_TRUE(loader->isCancelled()); |
| 278 |
| 279 const char* buffer = nullptr; |
| 280 size_t available; |
| 281 EXPECT_EQ(Result::Done, consumer->beginRead(&buffer, &available)); |
| 282 EXPECT_EQ(PublicState::Closed, consumer->getPublicState()); |
| 283 EXPECT_FALSE(loader->isStarted()); |
| 284 EXPECT_EQ(0, client->numOnStateChangeCalled()); |
| 285 } |
| 286 |
| 287 TEST_F(BlobBytesConsumerTest, CancelAfterStarting) |
| 288 { |
| 289 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), 12345); |
| 290 TestThreadableLoader* loader = new TestThreadableLoader(); |
| 291 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document(
), blobDataHandle, loader); |
| 292 TestClient* client = new TestClient(); |
| 293 consumer->setClient(client); |
| 294 |
| 295 const char* buffer = nullptr; |
| 296 size_t available; |
| 297 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); |
| 298 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 299 EXPECT_TRUE(loader->isStarted()); |
| 300 EXPECT_EQ(0, client->numOnStateChangeCalled()); |
| 301 |
| 302 consumer->cancel(); |
| 303 EXPECT_TRUE(loader->isCancelled()); |
| 304 EXPECT_EQ(PublicState::Closed, consumer->getPublicState()); |
| 305 EXPECT_EQ(Result::Done, consumer->beginRead(&buffer, &available)); |
| 306 EXPECT_EQ(0, client->numOnStateChangeCalled()); |
| 307 } |
| 308 |
| 309 TEST_F(BlobBytesConsumerTest, ReadLastChunkBeforeDidFinishLoadingArrives) |
| 310 { |
| 311 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), 12345); |
| 312 TestThreadableLoader* loader = new TestThreadableLoader(); |
| 313 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document(
), blobDataHandle, loader); |
| 314 TestClient* client = new TestClient(); |
| 315 consumer->setClient(client); |
| 316 std::unique_ptr<ReplayingHandle> src = ReplayingHandle::create(); |
| 317 src->add(Command(Command::Data, "hello")); |
| 318 src->add(Command(Command::Done)); |
| 319 |
| 320 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 321 EXPECT_FALSE(loader->isStarted()); |
| 322 |
| 323 const char* buffer = nullptr; |
| 324 size_t available; |
| 325 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); |
| 326 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 327 EXPECT_TRUE(loader->isStarted()); |
| 328 EXPECT_EQ(0, client->numOnStateChangeCalled()); |
| 329 |
| 330 consumer->didReceiveResponse(0, ResourceResponse(), std::move(src)); |
| 331 EXPECT_EQ(1, client->numOnStateChangeCalled()); |
| 332 testing::runPendingTasks(); |
| 333 EXPECT_EQ(2, client->numOnStateChangeCalled()); |
| 334 |
| 335 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 336 ASSERT_EQ(Result::Ok, consumer->beginRead(&buffer, &available)); |
| 337 ASSERT_EQ(5u, available); |
| 338 EXPECT_EQ("hello", String(buffer, available)); |
| 339 ASSERT_EQ(Result::Ok, consumer->endRead(available)); |
| 340 |
| 341 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 342 ASSERT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); |
| 343 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 344 |
| 345 consumer->didFinishLoading(0, 0); |
| 346 EXPECT_EQ(3, client->numOnStateChangeCalled()); |
| 347 EXPECT_EQ(PublicState::Closed, consumer->getPublicState()); |
| 348 ASSERT_EQ(Result::Done, consumer->beginRead(&buffer, &available)); |
| 349 } |
| 350 |
| 351 TEST_F(BlobBytesConsumerTest, ReadLastChunkAfterDidFinishLoadingArrives) |
| 352 { |
| 353 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), 12345); |
| 354 TestThreadableLoader* loader = new TestThreadableLoader(); |
| 355 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document(
), blobDataHandle, loader); |
| 356 TestClient* client = new TestClient(); |
| 357 consumer->setClient(client); |
| 358 std::unique_ptr<ReplayingHandle> src = ReplayingHandle::create(); |
| 359 src->add(Command(Command::Data, "hello")); |
| 360 src->add(Command(Command::Done)); |
| 361 |
| 362 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 363 EXPECT_FALSE(loader->isStarted()); |
| 364 |
| 365 const char* buffer = nullptr; |
| 366 size_t available; |
| 367 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); |
| 368 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 369 EXPECT_TRUE(loader->isStarted()); |
| 370 EXPECT_EQ(0, client->numOnStateChangeCalled()); |
| 371 |
| 372 consumer->didReceiveResponse(0, ResourceResponse(), std::move(src)); |
| 373 EXPECT_EQ(1, client->numOnStateChangeCalled()); |
| 374 testing::runPendingTasks(); |
| 375 EXPECT_EQ(2, client->numOnStateChangeCalled()); |
| 376 |
| 377 consumer->didFinishLoading(0, 0); |
| 378 testing::runPendingTasks(); |
| 379 EXPECT_EQ(2, client->numOnStateChangeCalled()); |
| 380 |
| 381 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 382 ASSERT_EQ(Result::Ok, consumer->beginRead(&buffer, &available)); |
| 383 ASSERT_EQ(5u, available); |
| 384 EXPECT_EQ("hello", String(buffer, available)); |
| 385 ASSERT_EQ(Result::Ok, consumer->endRead(available)); |
| 386 |
| 387 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 388 EXPECT_EQ(Result::Done, consumer->beginRead(&buffer, &available)); |
| 389 EXPECT_EQ(2, client->numOnStateChangeCalled()); |
| 390 } |
| 391 |
| 392 TEST_F(BlobBytesConsumerTest, DrainAsBlobDataHandle) |
| 393 { |
| 394 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), 12345); |
| 395 TestThreadableLoader* loader = new TestThreadableLoader(); |
| 396 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document(
), blobDataHandle, loader); |
| 397 |
| 398 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 399 EXPECT_FALSE(loader->isStarted()); |
| 400 |
| 401 RefPtr<BlobDataHandle> result = consumer->drainAsBlobDataHandle(BytesConsume
r::BlobSizePolicy::DisallowBlobWithInvalidSize); |
| 402 ASSERT_TRUE(result); |
| 403 EXPECT_FALSE(consumer->drainAsBlobDataHandle(BytesConsumer::BlobSizePolicy::
DisallowBlobWithInvalidSize)); |
| 404 EXPECT_EQ(12345u, result->size()); |
| 405 EXPECT_EQ(PublicState::Closed, consumer->getPublicState()); |
| 406 EXPECT_FALSE(loader->isStarted()); |
| 407 } |
| 408 |
| 409 |
| 410 TEST_F(BlobBytesConsumerTest, DrainAsBlobDataHandle_2) |
| 411 { |
| 412 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), -1); |
| 413 TestThreadableLoader* loader = new TestThreadableLoader(); |
| 414 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document(
), blobDataHandle, loader); |
| 415 |
| 416 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 417 EXPECT_FALSE(loader->isStarted()); |
| 418 |
| 419 RefPtr<BlobDataHandle> result = consumer->drainAsBlobDataHandle(BytesConsume
r::BlobSizePolicy::AllowBlobWithInvalidSize); |
| 420 ASSERT_TRUE(result); |
| 421 EXPECT_FALSE(consumer->drainAsBlobDataHandle(BytesConsumer::BlobSizePolicy::
AllowBlobWithInvalidSize)); |
| 422 EXPECT_EQ(UINT64_MAX, result->size()); |
| 423 EXPECT_EQ(PublicState::Closed, consumer->getPublicState()); |
| 424 EXPECT_FALSE(loader->isStarted()); |
| 425 } |
| 426 |
| 427 TEST_F(BlobBytesConsumerTest, DrainAsBlobDataHandle_3) |
| 428 { |
| 429 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), -1); |
| 430 TestThreadableLoader* loader = new TestThreadableLoader(); |
| 431 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document(
), blobDataHandle, loader); |
| 432 |
| 433 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 434 EXPECT_FALSE(loader->isStarted()); |
| 435 |
| 436 EXPECT_FALSE(consumer->drainAsBlobDataHandle(BytesConsumer::BlobSizePolicy::
DisallowBlobWithInvalidSize)); |
| 437 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 438 EXPECT_FALSE(loader->isStarted()); |
| 439 } |
| 440 |
| 441 TEST_F(BlobBytesConsumerTest, DrainAsFormData) |
| 442 { |
| 443 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), 12345); |
| 444 TestThreadableLoader* loader = new TestThreadableLoader(); |
| 445 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document(
), blobDataHandle, loader); |
| 446 |
| 447 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); |
| 448 EXPECT_FALSE(loader->isStarted()); |
| 449 |
| 450 RefPtr<EncodedFormData> result = consumer->drainAsFormData(); |
| 451 ASSERT_TRUE(result); |
| 452 ASSERT_EQ(1u, result->elements().size()); |
| 453 ASSERT_EQ(FormDataElement::encodedBlob, result->elements()[0].m_type); |
| 454 ASSERT_TRUE(result->elements()[0].m_optionalBlobDataHandle); |
| 455 EXPECT_EQ(12345u, result->elements()[0].m_optionalBlobDataHandle->size()); |
| 456 EXPECT_EQ(blobDataHandle->uuid(), result->elements()[0].m_blobUUID); |
| 457 EXPECT_EQ(PublicState::Closed, consumer->getPublicState()); |
| 458 EXPECT_FALSE(loader->isStarted()); |
| 459 } |
| 460 |
| 461 TEST_F(BlobBytesConsumerTest, LoaderShouldBeCancelled) |
| 462 { |
| 463 { |
| 464 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData:
:create(), 12345); |
| 465 TestThreadableLoader* loader = new TestThreadableLoader(); |
| 466 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&docum
ent(), blobDataHandle, loader); |
| 467 |
| 468 const char* buffer = nullptr; |
| 469 size_t available; |
| 470 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); |
| 471 EXPECT_TRUE(loader->isStarted()); |
| 472 loader->setShouldBeCancelled(); |
| 473 } |
| 474 ThreadState::current()->collectAllGarbage(); |
| 475 } |
| 476 |
| 477 TEST_F(BlobBytesConsumerTest, SyncErrorDispatch) |
| 478 { |
| 479 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), 12345); |
| 480 SyncErrorTestThreadableLoader* loader = new SyncErrorTestThreadableLoader(); |
| 481 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document(
), blobDataHandle, loader); |
| 482 loader->setClient(consumer); |
| 483 TestClient* client = new TestClient(); |
| 484 consumer->setClient(client); |
| 485 |
| 486 const char* buffer = nullptr; |
| 487 size_t available; |
| 488 EXPECT_EQ(Result::Error, consumer->beginRead(&buffer, &available)); |
| 489 EXPECT_TRUE(loader->isStarted()); |
| 490 |
| 491 EXPECT_EQ(0, client->numOnStateChangeCalled()); |
| 492 EXPECT_EQ(BytesConsumer::PublicState::Errored, consumer->getPublicState()); |
| 493 } |
| 494 |
| 495 TEST_F(BlobBytesConsumerTest, SyncLoading) |
| 496 { |
| 497 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), 12345); |
| 498 SyncLoadingTestThreadableLoader* loader = new SyncLoadingTestThreadableLoade
r(); |
| 499 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document(
), blobDataHandle, loader); |
| 500 std::unique_ptr<ReplayingHandle> src = ReplayingHandle::create(); |
| 501 src->add(Command(Command::Data, "hello, ")); |
| 502 src->add(Command(Command::Wait)); |
| 503 src->add(Command(Command::Data, "world")); |
| 504 src->add(Command(Command::Done)); |
| 505 loader->setClient(consumer); |
| 506 loader->setHandle(std::move(src)); |
| 507 TestClient* client = new TestClient(); |
| 508 consumer->setClient(client); |
| 509 |
| 510 const char* buffer = nullptr; |
| 511 size_t available; |
| 512 ASSERT_EQ(Result::Ok, consumer->beginRead(&buffer, &available)); |
| 513 EXPECT_TRUE(loader->isStarted()); |
| 514 ASSERT_EQ(7u, available); |
| 515 EXPECT_EQ("hello, ", String(buffer, available)); |
| 516 |
| 517 EXPECT_EQ(0, client->numOnStateChangeCalled()); |
| 518 EXPECT_EQ(BytesConsumer::PublicState::ReadableOrWaiting, consumer->getPublic
State()); |
| 519 } |
| 520 |
| 521 TEST_F(BlobBytesConsumerTest, ConstructedFromNullHandle) |
| 522 { |
| 523 BlobBytesConsumer* consumer = new BlobBytesConsumer(&document(), nullptr); |
| 524 const char* buffer = nullptr; |
| 525 size_t available; |
| 526 EXPECT_EQ(BytesConsumer::PublicState::Closed, consumer->getPublicState()); |
| 527 EXPECT_EQ(Result::Done, consumer->beginRead(&buffer, &available)); |
| 528 } |
| 529 |
| 530 } // namespace |
| 531 |
| 532 } // namespace blink |
OLD | NEW |