Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / fetch / ResourceTest.cpp
blob6ad9f3c6921c7ab1b13bffe14e63ff3a902283fa
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"
14 #include <gtest/gtest.h>
16 namespace blink {
18 namespace {
20 class MockPlatform final : public Platform {
21 public:
22 MockPlatform() : m_oldPlatform(Platform::current()) { }
23 ~MockPlatform() override { }
25 // From blink::Platform:
26 void cacheMetadata(const WebURL& url, int64, const char*, size_t) override
28 m_cachedURLs.append(url);
31 const Vector<WebURL>& cachedURLs() const
33 return m_cachedURLs;
36 WebThread* currentThread() override
38 return m_oldPlatform->currentThread();
41 // These blink::Platform methods must be overriden to make a usable object.
42 void cryptographicallyRandomValues(unsigned char* buffer, size_t length) override
44 ASSERT_NOT_REACHED();
47 const unsigned char* getTraceCategoryEnabledFlag(const char* categoryName) override
49 static const unsigned char tracingIsDisabled = 0;
50 return &tracingIsDisabled;
53 private:
54 Platform* m_oldPlatform; // Not owned.
55 Vector<WebURL> m_cachedURLs;
58 class AutoInstallMockPlatform {
59 public:
60 AutoInstallMockPlatform()
62 m_oldPlatform = Platform::current();
63 Platform::initialize(&m_mockPlatform);
65 ~AutoInstallMockPlatform()
67 Platform::initialize(m_oldPlatform);
69 MockPlatform* platform() { return &m_mockPlatform; }
70 private:
71 MockPlatform m_mockPlatform;
72 Platform* m_oldPlatform;
75 PassOwnPtr<ResourceResponse> createTestResourceResponse()
77 OwnPtr<ResourceResponse> response = adoptPtr(new ResourceResponse);
78 response->setURL(URLTestHelpers::toKURL("https://example.com/"));
79 response->setHTTPStatusCode(200);
80 return response.release();
83 void createTestResourceAndSetCachedMetadata(const ResourceResponse* response)
85 const char testData[] = "test data";
86 ResourcePtr<Resource> resource = new Resource(ResourceRequest(response->url()), Resource::Raw);
87 resource->setResponse(*response);
88 resource->cacheHandler()->setCachedMetadata(100, testData, sizeof(testData), CachedMetadataHandler::SendToPlatform);
89 return;
92 } // anonymous namespace
94 TEST(ResourceTest, SetCachedMetadata_SendsMetadataToPlatform)
96 AutoInstallMockPlatform mock;
97 OwnPtr<ResourceResponse> response(createTestResourceResponse());
98 createTestResourceAndSetCachedMetadata(response.get());
99 EXPECT_EQ(1u, mock.platform()->cachedURLs().size());
102 TEST(ResourceTest, SetCachedMetadata_DoesNotSendMetadataToPlatformWhenFetchedViaServiceWorker)
104 AutoInstallMockPlatform mock;
105 OwnPtr<ResourceResponse> response(createTestResourceResponse());
106 response->setWasFetchedViaServiceWorker(true);
107 createTestResourceAndSetCachedMetadata(response.get());
108 EXPECT_EQ(0u, mock.platform()->cachedURLs().size());
111 } // namespace blink