Implement timers by posting delayed tasks
[chromium-blink-merge.git] / third_party / WebKit / Source / core / fetch / ResourceTest.cpp
blob5dd02c66dd8a81713f34f16d74ffa465dc483415
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.
5 #include "config.h"
6 #include "core/fetch/Resource.h"
8 #include "core/fetch/ResourcePtr.h"
9 #include "platform/network/ResourceRequest.h"
10 #include "platform/network/ResourceResponse.h"
11 #include "platform/testing/URLTestHelpers.h"
12 #include "public/platform/Platform.h"
13 #include "wtf/Vector.h"
15 #include <gtest/gtest.h>
17 using namespace blink;
19 namespace blink {
21 namespace {
23 class MockPlatform final : public Platform {
24 public:
25 MockPlatform() : m_oldPlatform(Platform::current()) { }
26 ~MockPlatform() override { }
28 // From blink::Platform:
29 void cacheMetadata(const WebURL& url, int64, const char*, size_t) override
31 m_cachedURLs.append(url);
33 void cryptographicallyRandomValues(unsigned char* buffer, size_t length) override
35 ASSERT_NOT_REACHED();
38 const Vector<WebURL>& cachedURLs() const
40 return m_cachedURLs;
43 WebThread* currentThread() override
45 return m_oldPlatform->currentThread();
48 private:
49 Platform* m_oldPlatform; // Not owned.
50 Vector<WebURL> m_cachedURLs;
53 class AutoInstallMockPlatform {
54 public:
55 AutoInstallMockPlatform()
57 m_oldPlatform = Platform::current();
58 Platform::initialize(&m_mockPlatform);
60 ~AutoInstallMockPlatform()
62 Platform::initialize(m_oldPlatform);
64 MockPlatform* platform() { return &m_mockPlatform; }
65 private:
66 MockPlatform m_mockPlatform;
67 Platform* m_oldPlatform;
70 PassOwnPtr<ResourceResponse> createTestResourceResponse()
72 OwnPtr<ResourceResponse> response = adoptPtr(new ResourceResponse);
73 response->setURL(URLTestHelpers::toKURL("https://example.com/"));
74 response->setHTTPStatusCode(200);
75 return response.release();
78 void createTestResourceAndSetCachedMetadata(const ResourceResponse* response)
80 const char testData[] = "test data";
81 ResourcePtr<Resource> resource = new Resource(ResourceRequest(response->url()), Resource::Raw);
82 resource->setResponse(*response);
83 resource->cacheHandler()->setCachedMetadata(100, testData, sizeof(testData), CachedMetadataHandler::SendToPlatform);
84 return;
87 } // namespace
89 TEST(ResourceTest, SetCachedMetadata_SendsMetadataToPlatform)
91 AutoInstallMockPlatform mock;
92 OwnPtr<ResourceResponse> response(createTestResourceResponse());
93 createTestResourceAndSetCachedMetadata(response.get());
94 EXPECT_EQ(1u, mock.platform()->cachedURLs().size());
97 TEST(ResourceTest, SetCachedMetadata_DoesNotSendMetadataToPlatformWhenFetchedViaServiceWorker)
99 AutoInstallMockPlatform mock;
100 OwnPtr<ResourceResponse> response(createTestResourceResponse());
101 response->setWasFetchedViaServiceWorker(true);
102 createTestResourceAndSetCachedMetadata(response.get());
103 EXPECT_EQ(0u, mock.platform()->cachedURLs().size());
106 } // namespace blink