| 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 #ifndef MockFetchContext_h |
| 6 #define MockFetchContext_h |
| 7 |
| 8 #include "core/fetch/FetchContext.h" |
| 9 #include "core/fetch/FetchRequest.h" |
| 10 #include "core/fetch/ResourceLoaderOptions.h" |
| 11 #include "platform/network/ResourceTimingInfo.h" |
| 12 #include "platform/scheduler/test/fake_web_task_runner.h" |
| 13 #include "wtf/PtrUtil.h" |
| 14 |
| 15 #include <memory> |
| 16 |
| 17 namespace blink { |
| 18 |
| 19 class KURL; |
| 20 class ResourceRequest; |
| 21 class WebTaskRunner; |
| 22 |
| 23 // Mocked FetchContext for testing. |
| 24 // TODO(toyoshim): Use this class by other unit tests that currently have own |
| 25 // mocked FetchContext respectively. |
| 26 class MockFetchContext : public FetchContext { |
| 27 public: |
| 28 enum LoadPolicy { |
| 29 kShouldLoadNewResource, |
| 30 kShouldNotLoadNewResource, |
| 31 }; |
| 32 static MockFetchContext* create(LoadPolicy loadPolicy) { |
| 33 return new MockFetchContext(loadPolicy); |
| 34 } |
| 35 |
| 36 ~MockFetchContext() override {} |
| 37 |
| 38 bool allowImage(bool imagesEnabled, const KURL&) const override { |
| 39 return true; |
| 40 } |
| 41 bool canRequest(Resource::Type, |
| 42 const ResourceRequest&, |
| 43 const KURL&, |
| 44 const ResourceLoaderOptions&, |
| 45 bool forPreload, |
| 46 FetchRequest::OriginRestriction) const override { |
| 47 return true; |
| 48 } |
| 49 bool shouldLoadNewResource(Resource::Type) const override { |
| 50 return m_loadPolicy == kShouldLoadNewResource; |
| 51 } |
| 52 WebTaskRunner* loadingTaskRunner() const override { return m_runner.get(); } |
| 53 |
| 54 void setCachePolicy(CachePolicy policy) { m_policy = policy; } |
| 55 CachePolicy getCachePolicy() const override { return m_policy; } |
| 56 void setLoadComplete(bool complete) { m_complete = complete; } |
| 57 bool isLoadComplete() const override { return m_complete; } |
| 58 |
| 59 void addResourceTiming( |
| 60 const ResourceTimingInfo& resourceTimingInfo) override { |
| 61 m_transferSize = resourceTimingInfo.transferSize(); |
| 62 } |
| 63 long long getTransferSize() const { return m_transferSize; } |
| 64 |
| 65 private: |
| 66 MockFetchContext(LoadPolicy loadPolicy) |
| 67 : m_loadPolicy(loadPolicy), |
| 68 m_policy(CachePolicyVerify), |
| 69 m_runner(wrapUnique(new scheduler::FakeWebTaskRunner)), |
| 70 m_complete(false), |
| 71 m_transferSize(-1) {} |
| 72 |
| 73 enum LoadPolicy m_loadPolicy; |
| 74 CachePolicy m_policy; |
| 75 std::unique_ptr<scheduler::FakeWebTaskRunner> m_runner; |
| 76 bool m_complete; |
| 77 long long m_transferSize; |
| 78 }; |
| 79 |
| 80 } // namespace blink |
| 81 |
| 82 #endif |
| OLD | NEW |