| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "bindings/core/v8/V8ScriptRunner.h" | 6 #include "bindings/core/v8/V8ScriptRunner.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/V8Binding.h" | 8 #include "bindings/core/v8/V8Binding.h" |
| 9 #include "core/fetch/CachedMetadataHandler.h" |
| 9 #include "core/fetch/ScriptResource.h" | 10 #include "core/fetch/ScriptResource.h" |
| 10 #include "platform/heap/Handle.h" | 11 #include "platform/heap/Handle.h" |
| 11 #include <gtest/gtest.h> | 12 #include <gtest/gtest.h> |
| 12 #include <v8.h> | 13 #include <v8.h> |
| 13 | 14 |
| 14 namespace blink { | 15 namespace blink { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 class V8ScriptRunnerTest : public ::testing::Test { | 19 class V8ScriptRunnerTest : public ::testing::Test { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 45 return WTF::String::format("a = function() { 1 + 1; } // %01000d\n", cou
nter); | 46 return WTF::String::format("a = function() { 1 + 1; } // %01000d\n", cou
nter); |
| 46 } | 47 } |
| 47 WTF::String filename() const | 48 WTF::String filename() const |
| 48 { | 49 { |
| 49 return WTF::String::format("whatever%d.js", counter); | 50 return WTF::String::format("whatever%d.js", counter); |
| 50 } | 51 } |
| 51 WTF::String url() const | 52 WTF::String url() const |
| 52 { | 53 { |
| 53 return WTF::String::format("http://bla.com/bla%d", counter); | 54 return WTF::String::format("http://bla.com/bla%d", counter); |
| 54 } | 55 } |
| 55 unsigned tagForParserCache(Resource* resource) const | 56 unsigned tagForParserCache(CachedMetadataHandler* cacheHandler) const |
| 56 { | 57 { |
| 57 return V8ScriptRunner::tagForParserCache(resource); | 58 return V8ScriptRunner::tagForParserCache(cacheHandler); |
| 58 } | 59 } |
| 59 unsigned tagForCodeCache(Resource* resource) const | 60 unsigned tagForCodeCache(CachedMetadataHandler* cacheHandler) const |
| 60 { | 61 { |
| 61 return V8ScriptRunner::tagForCodeCache(resource); | 62 return V8ScriptRunner::tagForCodeCache(cacheHandler); |
| 62 } | 63 } |
| 63 | 64 |
| 64 bool compileScript(V8CacheOptions cacheOptions) | 65 bool compileScript(V8CacheOptions cacheOptions) |
| 65 { | 66 { |
| 66 return !V8ScriptRunner::compileScript( | 67 return !V8ScriptRunner::compileScript( |
| 67 v8String(isolate(), code()), filename(), WTF::TextPosition(), | 68 v8String(isolate(), code()), filename(), WTF::TextPosition(), |
| 68 m_resource.get(), 0, isolate(), NotSharableCrossOrigin, cacheOptions
) | 69 isolate(), m_resource.get(), nullptr, m_resource.get() ? m_resource-
>cacheHandler(): nullptr, NotSharableCrossOrigin, cacheOptions) |
| 69 .IsEmpty(); | 70 .IsEmpty(); |
| 70 } | 71 } |
| 71 | 72 |
| 72 void setEmptyResource() | 73 void setEmptyResource() |
| 73 { | 74 { |
| 74 m_resourceRequest = WTF::adoptPtr(new ResourceRequest); | 75 m_resourceRequest = WTF::adoptPtr(new ResourceRequest); |
| 75 m_resource = ScriptResource::create(*m_resourceRequest.get(), "UTF-8"); | 76 m_resource = ScriptResource::create(*m_resourceRequest.get(), "UTF-8"); |
| 76 } | 77 } |
| 77 | 78 |
| 78 void setResource() | 79 void setResource() |
| 79 { | 80 { |
| 80 m_resourceRequest = WTF::adoptPtr(new ResourceRequest(url())); | 81 m_resourceRequest = WTF::adoptPtr(new ResourceRequest(url())); |
| 81 m_resource = ScriptResource::create(*m_resourceRequest.get(), "UTF-8"); | 82 m_resource = ScriptResource::create(*m_resourceRequest.get(), "UTF-8"); |
| 82 } | 83 } |
| 83 | 84 |
| 85 CachedMetadataHandler* cacheHandler() |
| 86 { |
| 87 return m_resource->cacheHandler(); |
| 88 } |
| 89 |
| 84 protected: | 90 protected: |
| 85 WTF::OwnPtr<ResourceRequest> m_resourceRequest; | 91 WTF::OwnPtr<ResourceRequest> m_resourceRequest; |
| 86 OwnPtrWillBePersistent<ScriptResource> m_resource; | 92 OwnPtrWillBePersistent<ScriptResource> m_resource; |
| 87 V8TestingScope m_scope; | 93 V8TestingScope m_scope; |
| 88 | 94 |
| 89 static int counter; | 95 static int counter; |
| 90 }; | 96 }; |
| 91 | 97 |
| 92 int V8ScriptRunnerTest::counter = 0; | 98 int V8ScriptRunnerTest::counter = 0; |
| 93 | 99 |
| 94 TEST_F(V8ScriptRunnerTest, resourcelessShouldPass) | 100 TEST_F(V8ScriptRunnerTest, resourcelessShouldPass) |
| 95 { | 101 { |
| 96 EXPECT_TRUE(compileScript(V8CacheOptionsNone)); | 102 EXPECT_TRUE(compileScript(V8CacheOptionsNone)); |
| 97 EXPECT_TRUE(compileScript(V8CacheOptionsParseMemory)); | 103 EXPECT_TRUE(compileScript(V8CacheOptionsParseMemory)); |
| 98 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); | 104 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); |
| 99 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); | 105 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); |
| 100 } | 106 } |
| 101 | 107 |
| 102 TEST_F(V8ScriptRunnerTest, emptyResourceDoesNothing) | 108 TEST_F(V8ScriptRunnerTest, emptyResourceDoesNotHaveCacheHandler) |
| 103 { | 109 { |
| 104 setEmptyResource(); | 110 setEmptyResource(); |
| 105 EXPECT_TRUE(compileScript(V8CacheOptionsDefault)); | 111 EXPECT_FALSE(cacheHandler()); |
| 106 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache(m_resource.get()))
); | |
| 107 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache(m_resource.get()))); | |
| 108 | |
| 109 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); | |
| 110 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache(m_resource.get()))
); | |
| 111 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache(m_resource.get()))); | |
| 112 | |
| 113 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); | |
| 114 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache(m_resource.get()))
); | |
| 115 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache(m_resource.get()))); | |
| 116 } | 112 } |
| 117 | 113 |
| 118 TEST_F(V8ScriptRunnerTest, parseMemoryOption) | 114 TEST_F(V8ScriptRunnerTest, parseMemoryOption) |
| 119 { | 115 { |
| 120 setResource(); | 116 setResource(); |
| 121 EXPECT_TRUE(compileScript(V8CacheOptionsParseMemory)); | 117 EXPECT_TRUE(compileScript(V8CacheOptionsParseMemory)); |
| 122 EXPECT_TRUE(m_resource->cachedMetadata(tagForParserCache(m_resource.get())))
; | 118 EXPECT_TRUE(cacheHandler()->cachedMetadata(tagForParserCache(cacheHandler())
)); |
| 123 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache(m_resource.get()))); | 119 EXPECT_FALSE(cacheHandler()->cachedMetadata(tagForCodeCache(cacheHandler()))
); |
| 124 // The cached data is associated with the encoding. | 120 // The cached data is associated with the encoding. |
| 125 ResourceRequest request(url()); | 121 ResourceRequest request(url()); |
| 126 OwnPtrWillBeRawPtr<ScriptResource> anotherResource = ScriptResource::create(
request, "UTF-16"); | 122 OwnPtrWillBeRawPtr<ScriptResource> anotherResource = ScriptResource::create(
request, "UTF-16"); |
| 127 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache(anotherResource.ge
t()))); | 123 EXPECT_FALSE(cacheHandler()->cachedMetadata(tagForParserCache(anotherResourc
e->cacheHandler()))); |
| 128 } | 124 } |
| 129 | 125 |
| 130 TEST_F(V8ScriptRunnerTest, parseOption) | 126 TEST_F(V8ScriptRunnerTest, parseOption) |
| 131 { | 127 { |
| 132 setResource(); | 128 setResource(); |
| 133 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); | 129 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); |
| 134 EXPECT_TRUE(m_resource->cachedMetadata(tagForParserCache(m_resource.get())))
; | 130 EXPECT_TRUE(cacheHandler()->cachedMetadata(tagForParserCache(cacheHandler())
)); |
| 135 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache(m_resource.get()))); | 131 EXPECT_FALSE(cacheHandler()->cachedMetadata(tagForCodeCache(cacheHandler()))
); |
| 136 // The cached data is associated with the encoding. | 132 // The cached data is associated with the encoding. |
| 137 ResourceRequest request(url()); | 133 ResourceRequest request(url()); |
| 138 OwnPtrWillBeRawPtr<ScriptResource> anotherResource = ScriptResource::create(
request, "UTF-16"); | 134 OwnPtrWillBeRawPtr<ScriptResource> anotherResource = ScriptResource::create(
request, "UTF-16"); |
| 139 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache(anotherResource.ge
t()))); | 135 EXPECT_FALSE(cacheHandler()->cachedMetadata(tagForParserCache(anotherResourc
e->cacheHandler()))); |
| 140 } | 136 } |
| 141 | 137 |
| 142 TEST_F(V8ScriptRunnerTest, codeOption) | 138 TEST_F(V8ScriptRunnerTest, codeOption) |
| 143 { | 139 { |
| 144 setResource(); | 140 setResource(); |
| 145 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); | 141 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); |
| 146 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache(m_resource.get()))
); | 142 EXPECT_FALSE(cacheHandler()->cachedMetadata(tagForParserCache(cacheHandler()
))); |
| 147 EXPECT_TRUE(m_resource->cachedMetadata(tagForCodeCache(m_resource.get()))); | 143 EXPECT_TRUE(cacheHandler()->cachedMetadata(tagForCodeCache(cacheHandler())))
; |
| 148 // The cached data is associated with the encoding. | 144 // The cached data is associated with the encoding. |
| 149 ResourceRequest request(url()); | 145 ResourceRequest request(url()); |
| 150 OwnPtrWillBeRawPtr<ScriptResource> anotherResource = ScriptResource::create(
request, "UTF-16"); | 146 OwnPtrWillBeRawPtr<ScriptResource> anotherResource = ScriptResource::create(
request, "UTF-16"); |
| 151 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache(anotherResource.get(
)))); | 147 EXPECT_FALSE(cacheHandler()->cachedMetadata(tagForCodeCache(anotherResource-
>cacheHandler()))); |
| 152 } | 148 } |
| 153 | 149 |
| 154 TEST_F(V8ScriptRunnerTest, codeCompressedOptions) | 150 TEST_F(V8ScriptRunnerTest, codeCompressedOptions) |
| 155 { | 151 { |
| 156 setResource(); | 152 setResource(); |
| 157 EXPECT_TRUE(compileScript(V8CacheOptionsCodeCompressed)); | 153 EXPECT_TRUE(compileScript(V8CacheOptionsCodeCompressed)); |
| 158 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache(m_resource.get()))
); | 154 EXPECT_FALSE(cacheHandler()->cachedMetadata(tagForParserCache(cacheHandler()
))); |
| 159 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache(m_resource.get()))); | 155 EXPECT_FALSE(cacheHandler()->cachedMetadata(tagForCodeCache(cacheHandler()))
); |
| 160 } | 156 } |
| 161 | 157 |
| 162 } // namespace | 158 } // namespace |
| 163 | 159 |
| 164 } // namespace blink | 160 } // namespace blink |
| OLD | NEW |