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.
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>
20 class MockPlatform final
: public Platform
{
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
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
47 const unsigned char* getTraceCategoryEnabledFlag(const char* categoryName
) override
49 static const unsigned char tracingIsDisabled
= 0;
50 return &tracingIsDisabled
;
54 Platform
* m_oldPlatform
; // Not owned.
55 Vector
<WebURL
> m_cachedURLs
;
58 class AutoInstallMockPlatform
{
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
; }
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
);
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());