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