| 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 #include "config.h" | 5 #include "config.h" |
| 6 #include "core/fetch/Resource.h" | 6 #include "core/fetch/Resource.h" |
| 7 | 7 |
| 8 #include "core/fetch/ResourcePtr.h" | 8 #include "core/fetch/ResourcePtr.h" |
| 9 #include "platform/network/ResourceRequest.h" | 9 #include "platform/network/ResourceRequest.h" |
| 10 #include "platform/network/ResourceResponse.h" | 10 #include "platform/network/ResourceResponse.h" |
| 11 #include "platform/testing/TestingPlatformSupport.h" |
| 11 #include "platform/testing/URLTestHelpers.h" | 12 #include "platform/testing/URLTestHelpers.h" |
| 12 #include "public/platform/Platform.h" | 13 #include "public/platform/Platform.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "wtf/Vector.h" | 15 #include "wtf/Vector.h" |
| 15 | 16 |
| 16 namespace blink { | 17 namespace blink { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 class MockPlatform final : public Platform { | 21 class MockPlatform final : public TestingPlatformSupport { |
| 21 public: | 22 public: |
| 22 MockPlatform() : m_oldPlatform(Platform::current()) { } | 23 MockPlatform() { } |
| 23 ~MockPlatform() override { } | 24 ~MockPlatform() override { } |
| 24 | 25 |
| 25 // From blink::Platform: | 26 // From blink::Platform: |
| 26 void cacheMetadata(const WebURL& url, int64, const char*, size_t) override | 27 void cacheMetadata(const WebURL& url, int64, const char*, size_t) override |
| 27 { | 28 { |
| 28 m_cachedURLs.append(url); | 29 m_cachedURLs.append(url); |
| 29 } | 30 } |
| 30 | 31 |
| 31 const Vector<WebURL>& cachedURLs() const | 32 const Vector<WebURL>& cachedURLs() const |
| 32 { | 33 { |
| 33 return m_cachedURLs; | 34 return m_cachedURLs; |
| 34 } | 35 } |
| 35 | 36 |
| 36 WebThread* currentThread() override | |
| 37 { | |
| 38 return m_oldPlatform->currentThread(); | |
| 39 } | |
| 40 | |
| 41 // These blink::Platform methods must be overriden to make a usable object. | |
| 42 void cryptographicallyRandomValues(unsigned char* buffer, size_t length) ove
rride | |
| 43 { | |
| 44 ASSERT_NOT_REACHED(); | |
| 45 } | |
| 46 | |
| 47 const unsigned char* getTraceCategoryEnabledFlag(const char* categoryName) o
verride | |
| 48 { | |
| 49 static const unsigned char tracingIsDisabled = 0; | |
| 50 return &tracingIsDisabled; | |
| 51 } | |
| 52 | |
| 53 private: | 37 private: |
| 54 Platform* m_oldPlatform; // Not owned. | |
| 55 Vector<WebURL> m_cachedURLs; | 38 Vector<WebURL> m_cachedURLs; |
| 56 }; | 39 }; |
| 57 | 40 |
| 58 class AutoInstallMockPlatform { | |
| 59 public: | |
| 60 AutoInstallMockPlatform() | |
| 61 { | |
| 62 m_oldPlatform = Platform::current(); | |
| 63 Platform::initialize(&m_mockPlatform); | |
| 64 } | |
| 65 ~AutoInstallMockPlatform() | |
| 66 { | |
| 67 Platform::initialize(m_oldPlatform); | |
| 68 } | |
| 69 MockPlatform* platform() { return &m_mockPlatform; } | |
| 70 private: | |
| 71 MockPlatform m_mockPlatform; | |
| 72 Platform* m_oldPlatform; | |
| 73 }; | |
| 74 | |
| 75 PassOwnPtr<ResourceResponse> createTestResourceResponse() | 41 PassOwnPtr<ResourceResponse> createTestResourceResponse() |
| 76 { | 42 { |
| 77 OwnPtr<ResourceResponse> response = adoptPtr(new ResourceResponse); | 43 OwnPtr<ResourceResponse> response = adoptPtr(new ResourceResponse); |
| 78 response->setURL(URLTestHelpers::toKURL("https://example.com/")); | 44 response->setURL(URLTestHelpers::toKURL("https://example.com/")); |
| 79 response->setHTTPStatusCode(200); | 45 response->setHTTPStatusCode(200); |
| 80 return response.release(); | 46 return response.release(); |
| 81 } | 47 } |
| 82 | 48 |
| 83 void createTestResourceAndSetCachedMetadata(const ResourceResponse* response) | 49 void createTestResourceAndSetCachedMetadata(const ResourceResponse* response) |
| 84 { | 50 { |
| 85 const char testData[] = "test data"; | 51 const char testData[] = "test data"; |
| 86 ResourcePtr<Resource> resource = new Resource(ResourceRequest(response->url(
)), Resource::Raw); | 52 ResourcePtr<Resource> resource = new Resource(ResourceRequest(response->url(
)), Resource::Raw); |
| 87 resource->setResponse(*response); | 53 resource->setResponse(*response); |
| 88 resource->cacheHandler()->setCachedMetadata(100, testData, sizeof(testData),
CachedMetadataHandler::SendToPlatform); | 54 resource->cacheHandler()->setCachedMetadata(100, testData, sizeof(testData),
CachedMetadataHandler::SendToPlatform); |
| 89 return; | 55 return; |
| 90 } | 56 } |
| 91 | 57 |
| 92 } // anonymous namespace | 58 } // anonymous namespace |
| 93 | 59 |
| 94 TEST(ResourceTest, SetCachedMetadata_SendsMetadataToPlatform) | 60 TEST(ResourceTest, SetCachedMetadata_SendsMetadataToPlatform) |
| 95 { | 61 { |
| 96 AutoInstallMockPlatform mock; | 62 MockPlatform mock; |
| 97 OwnPtr<ResourceResponse> response(createTestResourceResponse()); | 63 OwnPtr<ResourceResponse> response(createTestResourceResponse()); |
| 98 createTestResourceAndSetCachedMetadata(response.get()); | 64 createTestResourceAndSetCachedMetadata(response.get()); |
| 99 EXPECT_EQ(1u, mock.platform()->cachedURLs().size()); | 65 EXPECT_EQ(1u, mock.cachedURLs().size()); |
| 100 } | 66 } |
| 101 | 67 |
| 102 TEST(ResourceTest, SetCachedMetadata_DoesNotSendMetadataToPlatformWhenFetchedVia
ServiceWorker) | 68 TEST(ResourceTest, SetCachedMetadata_DoesNotSendMetadataToPlatformWhenFetchedVia
ServiceWorker) |
| 103 { | 69 { |
| 104 AutoInstallMockPlatform mock; | 70 MockPlatform mock; |
| 105 OwnPtr<ResourceResponse> response(createTestResourceResponse()); | 71 OwnPtr<ResourceResponse> response(createTestResourceResponse()); |
| 106 response->setWasFetchedViaServiceWorker(true); | 72 response->setWasFetchedViaServiceWorker(true); |
| 107 createTestResourceAndSetCachedMetadata(response.get()); | 73 createTestResourceAndSetCachedMetadata(response.get()); |
| 108 EXPECT_EQ(0u, mock.platform()->cachedURLs().size()); | 74 EXPECT_EQ(0u, mock.cachedURLs().size()); |
| 109 } | 75 } |
| 110 | 76 |
| 111 } // namespace blink | 77 } // namespace blink |
| OLD | NEW |