| 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 "modules/fetch/ReadableStreamDataConsumerHandle.h" | |
| 6 | |
| 7 #include "bindings/core/v8/ScriptState.h" | |
| 8 #include "bindings/core/v8/V8BindingMacros.h" | |
| 9 #include "bindings/core/v8/V8GCController.h" | |
| 10 #include "core/dom/Document.h" | |
| 11 #include "core/streams/ReadableStreamOperations.h" | |
| 12 #include "core/testing/DummyPageHolder.h" | |
| 13 #include "modules/fetch/DataConsumerHandleTestUtil.h" | |
| 14 #include "platform/heap/Handle.h" | |
| 15 #include "platform/testing/UnitTestHelpers.h" | |
| 16 #include "public/platform/WebDataConsumerHandle.h" | |
| 17 #include "testing/gmock/include/gmock/gmock.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 #include <memory> | |
| 20 #include <v8.h> | |
| 21 | |
| 22 // TODO(yhirano): Add cross-thread tests once the handle gets thread-safe. | |
| 23 namespace blink { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 using ::testing::InSequence; | |
| 28 using ::testing::StrictMock; | |
| 29 using Checkpoint = StrictMock<::testing::MockFunction<void(int)>>; | |
| 30 using Result = WebDataConsumerHandle::Result; | |
| 31 const Result kOk = WebDataConsumerHandle::Ok; | |
| 32 const Result kShouldWait = WebDataConsumerHandle::ShouldWait; | |
| 33 const Result kUnexpectedError = WebDataConsumerHandle::UnexpectedError; | |
| 34 const Result kDone = WebDataConsumerHandle::Done; | |
| 35 using Flags = WebDataConsumerHandle::Flags; | |
| 36 const Flags kNone = WebDataConsumerHandle::FlagNone; | |
| 37 | |
| 38 class MockClient : public GarbageCollectedFinalized<MockClient>, public WebDataC
onsumerHandle::Client { | |
| 39 public: | |
| 40 static StrictMock<MockClient>* create() { return new StrictMock<MockClient>(
); } | |
| 41 MOCK_METHOD0(didGetReadable, void()); | |
| 42 | |
| 43 DEFINE_INLINE_TRACE() {} | |
| 44 | |
| 45 protected: | |
| 46 MockClient() = default; | |
| 47 }; | |
| 48 | |
| 49 class ReadableStreamDataConsumerHandleTest : public ::testing::Test { | |
| 50 public: | |
| 51 ReadableStreamDataConsumerHandleTest() | |
| 52 : m_page(DummyPageHolder::create()) | |
| 53 { | |
| 54 } | |
| 55 | |
| 56 ScriptState* getScriptState() { return ScriptState::forMainWorld(m_page->doc
ument().frame()); } | |
| 57 v8::Isolate* isolate() { return getScriptState()->isolate(); } | |
| 58 | |
| 59 v8::MaybeLocal<v8::Value> eval(const char* s) | |
| 60 { | |
| 61 v8::Local<v8::String> source; | |
| 62 v8::Local<v8::Script> script; | |
| 63 v8::MicrotasksScope microtasks(isolate(), v8::MicrotasksScope::kDoNotRun
Microtasks); | |
| 64 if (!v8Call(v8::String::NewFromUtf8(isolate(), s, v8::NewStringType::kNo
rmal), source)) { | |
| 65 ADD_FAILURE(); | |
| 66 return v8::MaybeLocal<v8::Value>(); | |
| 67 } | |
| 68 if (!v8Call(v8::Script::Compile(getScriptState()->context(), source), sc
ript)) { | |
| 69 ADD_FAILURE() << "Compilation fails"; | |
| 70 return v8::MaybeLocal<v8::Value>(); | |
| 71 } | |
| 72 return script->Run(getScriptState()->context()); | |
| 73 } | |
| 74 v8::MaybeLocal<v8::Value> evalWithPrintingError(const char* s) | |
| 75 { | |
| 76 v8::TryCatch block(isolate()); | |
| 77 v8::MaybeLocal<v8::Value> r = eval(s); | |
| 78 if (block.HasCaught()) { | |
| 79 ADD_FAILURE() << toCoreString(block.Exception()->ToString(isolate())
).utf8().data(); | |
| 80 block.ReThrow(); | |
| 81 } | |
| 82 return r; | |
| 83 } | |
| 84 | |
| 85 std::unique_ptr<ReadableStreamDataConsumerHandle> createHandle(ScriptValue s
tream) | |
| 86 { | |
| 87 NonThrowableExceptionState es; | |
| 88 ScriptValue reader = ReadableStreamOperations::getReader(getScriptState(
), stream, es); | |
| 89 ASSERT(!reader.isEmpty()); | |
| 90 ASSERT(reader.v8Value()->IsObject()); | |
| 91 return ReadableStreamDataConsumerHandle::create(getScriptState(), reader
); | |
| 92 } | |
| 93 | |
| 94 void gc() { V8GCController::collectAllGarbageForTesting(isolate()); } | |
| 95 | |
| 96 private: | |
| 97 std::unique_ptr<DummyPageHolder> m_page; | |
| 98 }; | |
| 99 | |
| 100 TEST_F(ReadableStreamDataConsumerHandleTest, Create) | |
| 101 { | |
| 102 ScriptState::Scope scope(getScriptState()); | |
| 103 ScriptValue stream(getScriptState(), evalWithPrintingError("new ReadableStre
am")); | |
| 104 ASSERT_FALSE(stream.isEmpty()); | |
| 105 std::unique_ptr<ReadableStreamDataConsumerHandle> handle = createHandle(stre
am); | |
| 106 ASSERT_TRUE(handle); | |
| 107 Persistent<MockClient> client = MockClient::create(); | |
| 108 Checkpoint checkpoint; | |
| 109 | |
| 110 InSequence s; | |
| 111 EXPECT_CALL(checkpoint, Call(1)); | |
| 112 EXPECT_CALL(*client, didGetReadable()); | |
| 113 EXPECT_CALL(checkpoint, Call(2)); | |
| 114 | |
| 115 std::unique_ptr<FetchDataConsumerHandle::Reader> reader = handle->obtainFetc
hDataReader(client); | |
| 116 ASSERT_TRUE(reader); | |
| 117 checkpoint.Call(1); | |
| 118 testing::runPendingTasks(); | |
| 119 checkpoint.Call(2); | |
| 120 } | |
| 121 | |
| 122 TEST_F(ReadableStreamDataConsumerHandleTest, EmptyStream) | |
| 123 { | |
| 124 ScriptState::Scope scope(getScriptState()); | |
| 125 ScriptValue stream(getScriptState(), evalWithPrintingError( | |
| 126 "new ReadableStream({start: c => c.close()})")); | |
| 127 ASSERT_FALSE(stream.isEmpty()); | |
| 128 std::unique_ptr<ReadableStreamDataConsumerHandle> handle = createHandle(stre
am); | |
| 129 ASSERT_TRUE(handle); | |
| 130 Persistent<MockClient> client = MockClient::create(); | |
| 131 Checkpoint checkpoint; | |
| 132 | |
| 133 InSequence s; | |
| 134 EXPECT_CALL(checkpoint, Call(1)); | |
| 135 EXPECT_CALL(*client, didGetReadable()); | |
| 136 EXPECT_CALL(checkpoint, Call(2)); | |
| 137 EXPECT_CALL(*client, didGetReadable()); | |
| 138 EXPECT_CALL(checkpoint, Call(3)); | |
| 139 | |
| 140 char c; | |
| 141 size_t readBytes; | |
| 142 std::unique_ptr<FetchDataConsumerHandle::Reader> reader = handle->obtainFetc
hDataReader(client); | |
| 143 ASSERT_TRUE(reader); | |
| 144 checkpoint.Call(1); | |
| 145 testing::runPendingTasks(); | |
| 146 checkpoint.Call(2); | |
| 147 EXPECT_EQ(kShouldWait, reader->read(&c, 1, kNone, &readBytes)); | |
| 148 testing::runPendingTasks(); | |
| 149 checkpoint.Call(3); | |
| 150 EXPECT_EQ(kDone, reader->read(&c, 1, kNone, &readBytes)); | |
| 151 } | |
| 152 | |
| 153 TEST_F(ReadableStreamDataConsumerHandleTest, ErroredStream) | |
| 154 { | |
| 155 ScriptState::Scope scope(getScriptState()); | |
| 156 ScriptValue stream(getScriptState(), evalWithPrintingError( | |
| 157 "new ReadableStream({start: c => c.error()})")); | |
| 158 ASSERT_FALSE(stream.isEmpty()); | |
| 159 std::unique_ptr<ReadableStreamDataConsumerHandle> handle = createHandle(stre
am); | |
| 160 ASSERT_TRUE(handle); | |
| 161 Persistent<MockClient> client = MockClient::create(); | |
| 162 Checkpoint checkpoint; | |
| 163 | |
| 164 InSequence s; | |
| 165 EXPECT_CALL(checkpoint, Call(1)); | |
| 166 EXPECT_CALL(*client, didGetReadable()); | |
| 167 EXPECT_CALL(checkpoint, Call(2)); | |
| 168 EXPECT_CALL(*client, didGetReadable()); | |
| 169 EXPECT_CALL(checkpoint, Call(3)); | |
| 170 | |
| 171 char c; | |
| 172 size_t readBytes; | |
| 173 std::unique_ptr<FetchDataConsumerHandle::Reader> reader = handle->obtainFetc
hDataReader(client); | |
| 174 ASSERT_TRUE(reader); | |
| 175 checkpoint.Call(1); | |
| 176 testing::runPendingTasks(); | |
| 177 checkpoint.Call(2); | |
| 178 EXPECT_EQ(kShouldWait, reader->read(&c, 1, kNone, &readBytes)); | |
| 179 testing::runPendingTasks(); | |
| 180 checkpoint.Call(3); | |
| 181 EXPECT_EQ(kUnexpectedError, reader->read(&c, 1, kNone, &readBytes)); | |
| 182 } | |
| 183 | |
| 184 TEST_F(ReadableStreamDataConsumerHandleTest, Read) | |
| 185 { | |
| 186 ScriptState::Scope scope(getScriptState()); | |
| 187 ScriptValue stream(getScriptState(), evalWithPrintingError( | |
| 188 "var controller;" | |
| 189 "var stream = new ReadableStream({start: c => controller = c});" | |
| 190 "controller.enqueue(new Uint8Array());" | |
| 191 "controller.enqueue(new Uint8Array([0x43, 0x44, 0x45, 0x46]));" | |
| 192 "controller.enqueue(new Uint8Array([0x47, 0x48, 0x49, 0x4a]));" | |
| 193 "controller.close();" | |
| 194 "stream")); | |
| 195 ASSERT_FALSE(stream.isEmpty()); | |
| 196 std::unique_ptr<ReadableStreamDataConsumerHandle> handle = createHandle(stre
am); | |
| 197 ASSERT_TRUE(handle); | |
| 198 Persistent<MockClient> client = MockClient::create(); | |
| 199 Checkpoint checkpoint; | |
| 200 | |
| 201 InSequence s; | |
| 202 EXPECT_CALL(checkpoint, Call(1)); | |
| 203 EXPECT_CALL(*client, didGetReadable()); | |
| 204 EXPECT_CALL(checkpoint, Call(2)); | |
| 205 EXPECT_CALL(*client, didGetReadable()); | |
| 206 EXPECT_CALL(checkpoint, Call(3)); | |
| 207 EXPECT_CALL(*client, didGetReadable()); | |
| 208 EXPECT_CALL(checkpoint, Call(4)); | |
| 209 EXPECT_CALL(*client, didGetReadable()); | |
| 210 EXPECT_CALL(checkpoint, Call(5)); | |
| 211 EXPECT_CALL(*client, didGetReadable()); | |
| 212 EXPECT_CALL(checkpoint, Call(6)); | |
| 213 | |
| 214 char buffer[3]; | |
| 215 size_t readBytes; | |
| 216 std::unique_ptr<FetchDataConsumerHandle::Reader> reader = handle->obtainFetc
hDataReader(client); | |
| 217 ASSERT_TRUE(reader); | |
| 218 checkpoint.Call(1); | |
| 219 testing::runPendingTasks(); | |
| 220 checkpoint.Call(2); | |
| 221 EXPECT_EQ(kShouldWait, reader->read(buffer, 3, kNone, &readBytes)); | |
| 222 testing::runPendingTasks(); | |
| 223 checkpoint.Call(3); | |
| 224 EXPECT_EQ(kShouldWait, reader->read(buffer, 3, kNone, &readBytes)); | |
| 225 testing::runPendingTasks(); | |
| 226 checkpoint.Call(4); | |
| 227 EXPECT_EQ(kOk, reader->read(buffer, 3, kNone, &readBytes)); | |
| 228 EXPECT_EQ(3u, readBytes); | |
| 229 EXPECT_EQ(0x43, buffer[0]); | |
| 230 EXPECT_EQ(0x44, buffer[1]); | |
| 231 EXPECT_EQ(0x45, buffer[2]); | |
| 232 EXPECT_EQ(kOk, reader->read(buffer, 3, kNone, &readBytes)); | |
| 233 EXPECT_EQ(1u, readBytes); | |
| 234 EXPECT_EQ(0x46, buffer[0]); | |
| 235 EXPECT_EQ(kShouldWait, reader->read(buffer, 3, kNone, &readBytes)); | |
| 236 testing::runPendingTasks(); | |
| 237 checkpoint.Call(5); | |
| 238 EXPECT_EQ(kOk, reader->read(buffer, 3, kNone, &readBytes)); | |
| 239 EXPECT_EQ(3u, readBytes); | |
| 240 EXPECT_EQ(0x47, buffer[0]); | |
| 241 EXPECT_EQ(0x48, buffer[1]); | |
| 242 EXPECT_EQ(0x49, buffer[2]); | |
| 243 EXPECT_EQ(kOk, reader->read(buffer, 3, kNone, &readBytes)); | |
| 244 EXPECT_EQ(1u, readBytes); | |
| 245 EXPECT_EQ(0x4a, buffer[0]); | |
| 246 EXPECT_EQ(kShouldWait, reader->read(buffer, 3, kNone, &readBytes)); | |
| 247 testing::runPendingTasks(); | |
| 248 checkpoint.Call(6); | |
| 249 EXPECT_EQ(kDone, reader->read(buffer, 3, kNone, &readBytes)); | |
| 250 } | |
| 251 | |
| 252 TEST_F(ReadableStreamDataConsumerHandleTest, TwoPhaseRead) | |
| 253 { | |
| 254 ScriptState::Scope scope(getScriptState()); | |
| 255 ScriptValue stream(getScriptState(), evalWithPrintingError( | |
| 256 "var controller;" | |
| 257 "var stream = new ReadableStream({start: c => controller = c});" | |
| 258 "controller.enqueue(new Uint8Array());" | |
| 259 "controller.enqueue(new Uint8Array([0x43, 0x44, 0x45, 0x46]));" | |
| 260 "controller.enqueue(new Uint8Array([0x47, 0x48, 0x49, 0x4a]));" | |
| 261 "controller.close();" | |
| 262 "stream")); | |
| 263 ASSERT_FALSE(stream.isEmpty()); | |
| 264 std::unique_ptr<ReadableStreamDataConsumerHandle> handle = createHandle(stre
am); | |
| 265 ASSERT_TRUE(handle); | |
| 266 Persistent<MockClient> client = MockClient::create(); | |
| 267 Checkpoint checkpoint; | |
| 268 | |
| 269 InSequence s; | |
| 270 EXPECT_CALL(checkpoint, Call(1)); | |
| 271 EXPECT_CALL(*client, didGetReadable()); | |
| 272 EXPECT_CALL(checkpoint, Call(2)); | |
| 273 EXPECT_CALL(*client, didGetReadable()); | |
| 274 EXPECT_CALL(checkpoint, Call(3)); | |
| 275 EXPECT_CALL(*client, didGetReadable()); | |
| 276 EXPECT_CALL(checkpoint, Call(4)); | |
| 277 EXPECT_CALL(*client, didGetReadable()); | |
| 278 EXPECT_CALL(checkpoint, Call(5)); | |
| 279 EXPECT_CALL(*client, didGetReadable()); | |
| 280 EXPECT_CALL(checkpoint, Call(6)); | |
| 281 | |
| 282 const void* buffer; | |
| 283 size_t available; | |
| 284 std::unique_ptr<FetchDataConsumerHandle::Reader> reader = handle->obtainFetc
hDataReader(client); | |
| 285 ASSERT_TRUE(reader); | |
| 286 checkpoint.Call(1); | |
| 287 testing::runPendingTasks(); | |
| 288 checkpoint.Call(2); | |
| 289 EXPECT_EQ(kShouldWait, reader->beginRead(&buffer, kNone, &available)); | |
| 290 testing::runPendingTasks(); | |
| 291 checkpoint.Call(3); | |
| 292 EXPECT_EQ(kShouldWait, reader->beginRead(&buffer, kNone, &available)); | |
| 293 testing::runPendingTasks(); | |
| 294 checkpoint.Call(4); | |
| 295 EXPECT_EQ(kOk, reader->beginRead(&buffer, kNone, &available)); | |
| 296 EXPECT_EQ(4u, available); | |
| 297 EXPECT_EQ(0x43, static_cast<const char*>(buffer)[0]); | |
| 298 EXPECT_EQ(0x44, static_cast<const char*>(buffer)[1]); | |
| 299 EXPECT_EQ(0x45, static_cast<const char*>(buffer)[2]); | |
| 300 EXPECT_EQ(0x46, static_cast<const char*>(buffer)[3]); | |
| 301 EXPECT_EQ(kOk, reader->endRead(0)); | |
| 302 EXPECT_EQ(kOk, reader->beginRead(&buffer, kNone, &available)); | |
| 303 EXPECT_EQ(4u, available); | |
| 304 EXPECT_EQ(0x43, static_cast<const char*>(buffer)[0]); | |
| 305 EXPECT_EQ(0x44, static_cast<const char*>(buffer)[1]); | |
| 306 EXPECT_EQ(0x45, static_cast<const char*>(buffer)[2]); | |
| 307 EXPECT_EQ(0x46, static_cast<const char*>(buffer)[3]); | |
| 308 EXPECT_EQ(kOk, reader->endRead(1)); | |
| 309 EXPECT_EQ(kOk, reader->beginRead(&buffer, kNone, &available)); | |
| 310 EXPECT_EQ(3u, available); | |
| 311 EXPECT_EQ(0x44, static_cast<const char*>(buffer)[0]); | |
| 312 EXPECT_EQ(0x45, static_cast<const char*>(buffer)[1]); | |
| 313 EXPECT_EQ(0x46, static_cast<const char*>(buffer)[2]); | |
| 314 EXPECT_EQ(kOk, reader->endRead(3)); | |
| 315 EXPECT_EQ(kShouldWait, reader->beginRead(&buffer, kNone, &available)); | |
| 316 testing::runPendingTasks(); | |
| 317 checkpoint.Call(5); | |
| 318 EXPECT_EQ(kOk, reader->beginRead(&buffer, kNone, &available)); | |
| 319 EXPECT_EQ(4u, available); | |
| 320 EXPECT_EQ(0x47, static_cast<const char*>(buffer)[0]); | |
| 321 EXPECT_EQ(0x48, static_cast<const char*>(buffer)[1]); | |
| 322 EXPECT_EQ(0x49, static_cast<const char*>(buffer)[2]); | |
| 323 EXPECT_EQ(0x4a, static_cast<const char*>(buffer)[3]); | |
| 324 EXPECT_EQ(kOk, reader->endRead(4)); | |
| 325 EXPECT_EQ(kShouldWait, reader->beginRead(&buffer, kNone, &available)); | |
| 326 testing::runPendingTasks(); | |
| 327 checkpoint.Call(6); | |
| 328 EXPECT_EQ(kDone, reader->beginRead(&buffer, kNone, &available)); | |
| 329 } | |
| 330 | |
| 331 TEST_F(ReadableStreamDataConsumerHandleTest, EnqueueUndefined) | |
| 332 { | |
| 333 ScriptState::Scope scope(getScriptState()); | |
| 334 ScriptValue stream(getScriptState(), evalWithPrintingError( | |
| 335 "var controller;" | |
| 336 "var stream = new ReadableStream({start: c => controller = c});" | |
| 337 "controller.enqueue(undefined);" | |
| 338 "controller.close();" | |
| 339 "stream")); | |
| 340 ASSERT_FALSE(stream.isEmpty()); | |
| 341 std::unique_ptr<ReadableStreamDataConsumerHandle> handle = createHandle(stre
am); | |
| 342 ASSERT_TRUE(handle); | |
| 343 Persistent<MockClient> client = MockClient::create(); | |
| 344 Checkpoint checkpoint; | |
| 345 | |
| 346 InSequence s; | |
| 347 EXPECT_CALL(checkpoint, Call(1)); | |
| 348 EXPECT_CALL(*client, didGetReadable()); | |
| 349 EXPECT_CALL(checkpoint, Call(2)); | |
| 350 EXPECT_CALL(*client, didGetReadable()); | |
| 351 EXPECT_CALL(checkpoint, Call(3)); | |
| 352 | |
| 353 const void* buffer; | |
| 354 size_t available; | |
| 355 std::unique_ptr<FetchDataConsumerHandle::Reader> reader = handle->obtainFetc
hDataReader(client); | |
| 356 ASSERT_TRUE(reader); | |
| 357 checkpoint.Call(1); | |
| 358 testing::runPendingTasks(); | |
| 359 checkpoint.Call(2); | |
| 360 EXPECT_EQ(kShouldWait, reader->beginRead(&buffer, kNone, &available)); | |
| 361 testing::runPendingTasks(); | |
| 362 checkpoint.Call(3); | |
| 363 EXPECT_EQ(kUnexpectedError, reader->beginRead(&buffer, kNone, &available)); | |
| 364 } | |
| 365 | |
| 366 TEST_F(ReadableStreamDataConsumerHandleTest, EnqueueNull) | |
| 367 { | |
| 368 ScriptState::Scope scope(getScriptState()); | |
| 369 ScriptValue stream(getScriptState(), evalWithPrintingError( | |
| 370 "var controller;" | |
| 371 "var stream = new ReadableStream({start: c => controller = c});" | |
| 372 "controller.enqueue(null);" | |
| 373 "controller.close();" | |
| 374 "stream")); | |
| 375 ASSERT_FALSE(stream.isEmpty()); | |
| 376 std::unique_ptr<ReadableStreamDataConsumerHandle> handle = createHandle(stre
am); | |
| 377 ASSERT_TRUE(handle); | |
| 378 Persistent<MockClient> client = MockClient::create(); | |
| 379 Checkpoint checkpoint; | |
| 380 | |
| 381 InSequence s; | |
| 382 EXPECT_CALL(checkpoint, Call(1)); | |
| 383 EXPECT_CALL(*client, didGetReadable()); | |
| 384 EXPECT_CALL(checkpoint, Call(2)); | |
| 385 EXPECT_CALL(*client, didGetReadable()); | |
| 386 EXPECT_CALL(checkpoint, Call(3)); | |
| 387 | |
| 388 const void* buffer; | |
| 389 size_t available; | |
| 390 std::unique_ptr<FetchDataConsumerHandle::Reader> reader = handle->obtainFetc
hDataReader(client); | |
| 391 ASSERT_TRUE(reader); | |
| 392 checkpoint.Call(1); | |
| 393 testing::runPendingTasks(); | |
| 394 checkpoint.Call(2); | |
| 395 EXPECT_EQ(kShouldWait, reader->beginRead(&buffer, kNone, &available)); | |
| 396 testing::runPendingTasks(); | |
| 397 checkpoint.Call(3); | |
| 398 EXPECT_EQ(kUnexpectedError, reader->beginRead(&buffer, kNone, &available)); | |
| 399 } | |
| 400 | |
| 401 TEST_F(ReadableStreamDataConsumerHandleTest, EnqueueString) | |
| 402 { | |
| 403 ScriptState::Scope scope(getScriptState()); | |
| 404 ScriptValue stream(getScriptState(), evalWithPrintingError( | |
| 405 "var controller;" | |
| 406 "var stream = new ReadableStream({start: c => controller = c});" | |
| 407 "controller.enqueue('hello');" | |
| 408 "controller.close();" | |
| 409 "stream")); | |
| 410 ASSERT_FALSE(stream.isEmpty()); | |
| 411 std::unique_ptr<ReadableStreamDataConsumerHandle> handle = createHandle(stre
am); | |
| 412 ASSERT_TRUE(handle); | |
| 413 Persistent<MockClient> client = MockClient::create(); | |
| 414 Checkpoint checkpoint; | |
| 415 | |
| 416 InSequence s; | |
| 417 EXPECT_CALL(checkpoint, Call(1)); | |
| 418 EXPECT_CALL(*client, didGetReadable()); | |
| 419 EXPECT_CALL(checkpoint, Call(2)); | |
| 420 EXPECT_CALL(*client, didGetReadable()); | |
| 421 EXPECT_CALL(checkpoint, Call(3)); | |
| 422 | |
| 423 const void* buffer; | |
| 424 size_t available; | |
| 425 std::unique_ptr<FetchDataConsumerHandle::Reader> reader = handle->obtainFetc
hDataReader(client); | |
| 426 ASSERT_TRUE(reader); | |
| 427 checkpoint.Call(1); | |
| 428 testing::runPendingTasks(); | |
| 429 checkpoint.Call(2); | |
| 430 EXPECT_EQ(kShouldWait, reader->beginRead(&buffer, kNone, &available)); | |
| 431 testing::runPendingTasks(); | |
| 432 checkpoint.Call(3); | |
| 433 EXPECT_EQ(kUnexpectedError, reader->beginRead(&buffer, kNone, &available)); | |
| 434 } | |
| 435 | |
| 436 TEST_F(ReadableStreamDataConsumerHandleTest, StreamReaderShouldBeWeak) | |
| 437 { | |
| 438 std::unique_ptr<FetchDataConsumerHandle::Reader> reader; | |
| 439 Checkpoint checkpoint; | |
| 440 Persistent<MockClient> client = MockClient::create(); | |
| 441 ScriptValue stream; | |
| 442 | |
| 443 InSequence s; | |
| 444 EXPECT_CALL(checkpoint, Call(1)); | |
| 445 EXPECT_CALL(*client, didGetReadable()); | |
| 446 EXPECT_CALL(checkpoint, Call(2)); | |
| 447 EXPECT_CALL(checkpoint, Call(3)); | |
| 448 EXPECT_CALL(*client, didGetReadable()); | |
| 449 EXPECT_CALL(checkpoint, Call(4)); | |
| 450 | |
| 451 { | |
| 452 // We need this scope to collect local handles. | |
| 453 ScriptState::Scope scope(getScriptState()); | |
| 454 stream = ScriptValue(getScriptState(), evalWithPrintingError("new Readab
leStream()")); | |
| 455 ASSERT_FALSE(stream.isEmpty()); | |
| 456 std::unique_ptr<ReadableStreamDataConsumerHandle> handle = createHandle(
stream); | |
| 457 ASSERT_TRUE(handle); | |
| 458 | |
| 459 reader = handle->obtainFetchDataReader(client); | |
| 460 ASSERT_TRUE(reader); | |
| 461 } | |
| 462 | |
| 463 checkpoint.Call(1); | |
| 464 testing::runPendingTasks(); | |
| 465 checkpoint.Call(2); | |
| 466 stream.clear(); | |
| 467 gc(); | |
| 468 checkpoint.Call(3); | |
| 469 testing::runPendingTasks(); | |
| 470 | |
| 471 checkpoint.Call(4); | |
| 472 const void* buffer; | |
| 473 size_t available; | |
| 474 EXPECT_EQ(kUnexpectedError, reader->beginRead(&buffer, kNone, &available)); | |
| 475 } | |
| 476 | |
| 477 TEST_F(ReadableStreamDataConsumerHandleTest, StreamReaderShouldBeWeakWhenReading
) | |
| 478 { | |
| 479 std::unique_ptr<FetchDataConsumerHandle::Reader> reader; | |
| 480 Checkpoint checkpoint; | |
| 481 Persistent<MockClient> client = MockClient::create(); | |
| 482 ScriptValue stream; | |
| 483 | |
| 484 InSequence s; | |
| 485 EXPECT_CALL(checkpoint, Call(1)); | |
| 486 EXPECT_CALL(*client, didGetReadable()); | |
| 487 EXPECT_CALL(checkpoint, Call(2)); | |
| 488 EXPECT_CALL(checkpoint, Call(3)); | |
| 489 EXPECT_CALL(checkpoint, Call(4)); | |
| 490 EXPECT_CALL(*client, didGetReadable()); | |
| 491 EXPECT_CALL(checkpoint, Call(5)); | |
| 492 | |
| 493 { | |
| 494 // We need this scope to collect local handles. | |
| 495 ScriptState::Scope scope(getScriptState()); | |
| 496 stream = ScriptValue(getScriptState(), evalWithPrintingError("new Readab
leStream()")); | |
| 497 ASSERT_FALSE(stream.isEmpty()); | |
| 498 std::unique_ptr<ReadableStreamDataConsumerHandle> handle = createHandle(
stream); | |
| 499 ASSERT_TRUE(handle); | |
| 500 | |
| 501 reader = handle->obtainFetchDataReader(client); | |
| 502 ASSERT_TRUE(reader); | |
| 503 } | |
| 504 | |
| 505 const void* buffer; | |
| 506 size_t available; | |
| 507 checkpoint.Call(1); | |
| 508 testing::runPendingTasks(); | |
| 509 checkpoint.Call(2); | |
| 510 EXPECT_EQ(kShouldWait, reader->beginRead(&buffer, kNone, &available)); | |
| 511 | |
| 512 testing::runPendingTasks(); | |
| 513 checkpoint.Call(3); | |
| 514 stream.clear(); | |
| 515 gc(); | |
| 516 checkpoint.Call(4); | |
| 517 testing::runPendingTasks(); | |
| 518 | |
| 519 checkpoint.Call(5); | |
| 520 EXPECT_EQ(kUnexpectedError, reader->beginRead(&buffer, kNone, &available)); | |
| 521 } | |
| 522 | |
| 523 } // namespace | |
| 524 | |
| 525 } // namespace blink | |
| OLD | NEW |