| OLD | NEW |
| 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 #ifndef DataConsumerHandleTestUtil_h | 5 #ifndef DataConsumerHandleTestUtil_h |
| 6 #define DataConsumerHandleTestUtil_h | 6 #define DataConsumerHandleTestUtil_h |
| 7 | 7 |
| 8 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
| 9 #include "core/testing/NullExecutionContext.h" | 9 #include "core/testing/NullExecutionContext.h" |
| 10 #include "gin/public/isolate_holder.h" | 10 #include "gin/public/isolate_holder.h" |
| 11 #include "modules/fetch/DataConsumerHandleUtil.h" | 11 #include "modules/fetch/DataConsumerHandleUtil.h" |
| 12 #include "modules/fetch/FetchDataConsumerHandle.h" | 12 #include "modules/fetch/FetchDataConsumerHandle.h" |
| 13 #include "modules/fetch/FetchDataLoader.h" |
| 13 #include "platform/Task.h" | 14 #include "platform/Task.h" |
| 14 #include "platform/ThreadSafeFunctional.h" | 15 #include "platform/ThreadSafeFunctional.h" |
| 15 #include "platform/WebThreadSupportingGC.h" | 16 #include "platform/WebThreadSupportingGC.h" |
| 16 #include "platform/heap/Handle.h" | 17 #include "platform/heap/Handle.h" |
| 17 #include "public/platform/Platform.h" | 18 #include "public/platform/Platform.h" |
| 18 #include "public/platform/WebDataConsumerHandle.h" | 19 #include "public/platform/WebDataConsumerHandle.h" |
| 19 #include "public/platform/WebTraceLocation.h" | 20 #include "public/platform/WebTraceLocation.h" |
| 20 #include "public/platform/WebWaitableEvent.h" | 21 #include "public/platform/WebWaitableEvent.h" |
| 22 #include "wtf/Deque.h" |
| 21 #include "wtf/Locker.h" | 23 #include "wtf/Locker.h" |
| 24 #include "wtf/ThreadSafeRefCounted.h" |
| 25 #include "wtf/ThreadingPrimitives.h" |
| 26 #include "wtf/Vector.h" |
| 22 | 27 |
| 23 #include <gmock/gmock.h> | 28 #include <gmock/gmock.h> |
| 24 #include <gtest/gtest.h> | 29 #include <gtest/gtest.h> |
| 25 #include <v8.h> | 30 #include <v8.h> |
| 26 | 31 |
| 27 namespace blink { | 32 namespace blink { |
| 28 | 33 |
| 29 class DataConsumerHandleTestUtil { | 34 class DataConsumerHandleTestUtil { |
| 30 public: | 35 public: |
| 31 class NoopClient final : public WebDataConsumerHandle::Client { | 36 class NoopClient final : public WebDataConsumerHandle::Client { |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 m_reader = nullptr; | 209 m_reader = nullptr; |
| 205 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::
signalDone, this))); | 210 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::
signalDone, this))); |
| 206 } | 211 } |
| 207 void didGetReadable() override | 212 void didGetReadable() override |
| 208 { | 213 { |
| 209 ASSERT_NOT_REACHED(); | 214 ASSERT_NOT_REACHED(); |
| 210 } | 215 } |
| 211 | 216 |
| 212 OwnPtr<WebDataConsumerHandle> m_handle; | 217 OwnPtr<WebDataConsumerHandle> m_handle; |
| 213 }; | 218 }; |
| 219 |
| 220 class MockFetchDataLoaderClient : public GarbageCollectedFinalized<MockFetch
DataLoaderClient>, public FetchDataLoader::Client { |
| 221 USING_GARBAGE_COLLECTED_MIXIN(MockFetchDataLoaderClient); |
| 222 public: |
| 223 static ::testing::StrictMock<MockFetchDataLoaderClient>* create() { retu
rn new ::testing::StrictMock<MockFetchDataLoaderClient>; } |
| 224 |
| 225 DEFINE_INLINE_VIRTUAL_TRACE() |
| 226 { |
| 227 FetchDataLoader::Client::trace(visitor); |
| 228 } |
| 229 |
| 230 MOCK_METHOD1(didFetchDataLoadedBlobHandleMock, void(RefPtr<BlobDataHandl
e>)); |
| 231 MOCK_METHOD1(didFetchDataLoadedArrayBufferMock, void(RefPtr<DOMArrayBuff
er>)); |
| 232 MOCK_METHOD1(didFetchDataLoadedString, void(const String&)); |
| 233 MOCK_METHOD0(didFetchDataLoadFailed, void()); |
| 234 |
| 235 // In mock methods we use RefPtr<> rather than PassRefPtr<>. |
| 236 void didFetchDataLoadedArrayBuffer(PassRefPtr<DOMArrayBuffer> arrayBuffe
r) override |
| 237 { |
| 238 didFetchDataLoadedArrayBufferMock(arrayBuffer); |
| 239 } |
| 240 void didFetchDataLoadedBlobHandle(PassRefPtr<BlobDataHandle> blobDataHan
dle) override |
| 241 { |
| 242 didFetchDataLoadedBlobHandleMock(blobDataHandle); |
| 243 } |
| 244 }; |
| 245 |
| 246 class Command final { |
| 247 public: |
| 248 enum Name { |
| 249 Data, |
| 250 Done, |
| 251 Error, |
| 252 Wait, |
| 253 }; |
| 254 |
| 255 Command(Name name) : m_name(name) { } |
| 256 Command(Name name, const Vector<char>& body) : m_name(name), m_body(body
) { } |
| 257 Command(Name name, const char* body, size_t size) : m_name(name) |
| 258 { |
| 259 m_body.append(body, size); |
| 260 } |
| 261 Command(Name name, const char* body) : Command(name, body, strlen(body))
{ } |
| 262 Name name() const { return m_name; } |
| 263 const Vector<char>& body() const { return m_body; } |
| 264 |
| 265 private: |
| 266 const Name m_name; |
| 267 Vector<char> m_body; |
| 268 }; |
| 269 |
| 270 // ReplayingHandle stores commands via |add| and replays the stored commends
when read. |
| 271 class ReplayingHandle final : public WebDataConsumerHandle { |
| 272 public: |
| 273 static PassOwnPtr<ReplayingHandle> create() { return adoptPtr(new Replay
ingHandle()); } |
| 274 ~ReplayingHandle(); |
| 275 |
| 276 // Add a command to this handle. This function must be called on the |
| 277 // creator thread. This function must be called BEFORE any reader is |
| 278 // obtained. |
| 279 void add(const Command&); |
| 280 |
| 281 class Context final : public ThreadSafeRefCounted<Context> { |
| 282 public: |
| 283 static PassRefPtr<Context> create() { return adoptRef(new Context);
} |
| 284 |
| 285 // This function cannot be called after creating a tee. |
| 286 void add(const Command&); |
| 287 void attachReader(WebDataConsumerHandle::Client*); |
| 288 void detachReader(); |
| 289 void detachHandle(); |
| 290 Result beginRead(const void** buffer, Flags, size_t* available); |
| 291 Result endRead(size_t readSize); |
| 292 WebWaitableEvent* detached() { return m_detached.get(); } |
| 293 |
| 294 private: |
| 295 Context(); |
| 296 bool isEmpty() const { return m_commands.isEmpty(); } |
| 297 const Command& top(); |
| 298 void consume(size_t); |
| 299 size_t offset() const { return m_offset; } |
| 300 void notify(); |
| 301 void notifyInternal(); |
| 302 |
| 303 Deque<Command> m_commands; |
| 304 size_t m_offset; |
| 305 WebThread* m_readerThread; |
| 306 Client* m_client; |
| 307 Result m_result; |
| 308 bool m_isHandleAttached; |
| 309 Mutex m_mutex; |
| 310 OwnPtr<WebWaitableEvent> m_detached; |
| 311 }; |
| 312 |
| 313 Context* context() { return m_context.get(); } |
| 314 |
| 315 private: |
| 316 class ReaderImpl; |
| 317 |
| 318 ReplayingHandle(); |
| 319 Reader* obtainReaderInternal(Client*) override; |
| 320 |
| 321 RefPtr<Context> m_context; |
| 322 }; |
| 323 |
| 214 }; | 324 }; |
| 215 | 325 |
| 216 } // namespace blink | 326 } // namespace blink |
| 217 | 327 |
| 218 #endif // DataConsumerHandleTestUtil_h | 328 #endif // DataConsumerHandleTestUtil_h |
| OLD | NEW |