Chromium Code Reviews| 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 { |
| 19 public: | 20 public: |
| 20 V8ScriptRunnerTest() : m_scope(v8::Isolate::GetCurrent()) { } | 21 V8ScriptRunnerTest() : m_cacheHandler(nullptr), m_scope(v8::Isolate::GetCurr ent()) { } |
| 21 virtual ~V8ScriptRunnerTest() { } | 22 virtual ~V8ScriptRunnerTest() { } |
| 22 | 23 |
| 23 virtual void SetUp() override | 24 virtual void SetUp() override |
| 24 { | 25 { |
| 25 // To trick various layers of caching, increment a counter for each | 26 // To trick various layers of caching, increment a counter for each |
| 26 // test and use it in code(), fielname() and url(). | 27 // test and use it in code(), fielname() and url(). |
| 27 counter++; | 28 counter++; |
| 28 } | 29 } |
| 29 | 30 |
| 30 virtual void TearDown() override | 31 virtual void TearDown() override |
| 31 { | 32 { |
| 32 m_resourceRequest.clear(); | 33 m_resourceRequest.clear(); |
| 34 m_cacheHandler = nullptr; | |
| 33 m_resource.clear(); | 35 m_resource.clear(); |
| 34 } | 36 } |
| 35 | 37 |
| 36 v8::Isolate* isolate() const | 38 v8::Isolate* isolate() const |
| 37 { | 39 { |
| 38 return m_scope.isolate(); | 40 return m_scope.isolate(); |
| 39 } | 41 } |
| 40 WTF::String code() const | 42 WTF::String code() const |
| 41 { | 43 { |
| 42 // Simple function for testing. Note: | 44 // Simple function for testing. Note: |
| 43 // - Add counter to trick V8 code cache. | 45 // - Add counter to trick V8 code cache. |
| 44 // - Pad counter to 1000 digits, to trick minimal cacheability threshold . | 46 // - Pad counter to 1000 digits, to trick minimal cacheability threshold . |
| 45 return WTF::String::format("a = function() { 1 + 1; } // %01000d\n", cou nter); | 47 return WTF::String::format("a = function() { 1 + 1; } // %01000d\n", cou nter); |
| 46 } | 48 } |
| 47 WTF::String filename() const | 49 WTF::String filename() const |
| 48 { | 50 { |
| 49 return WTF::String::format("whatever%d.js", counter); | 51 return WTF::String::format("whatever%d.js", counter); |
| 50 } | 52 } |
| 51 WTF::String url() const | 53 WTF::String url() const |
| 52 { | 54 { |
| 53 return WTF::String::format("http://bla.com/bla%d", counter); | 55 return WTF::String::format("http://bla.com/bla%d", counter); |
| 54 } | 56 } |
| 55 unsigned tagForParserCache(Resource* resource) const | 57 unsigned tagForParserCache(CachedMetadataHandler* cacheHandler) const |
| 56 { | 58 { |
| 57 return V8ScriptRunner::tagForParserCache(resource); | 59 return V8ScriptRunner::tagForParserCache(cacheHandler); |
| 58 } | 60 } |
| 59 unsigned tagForCodeCache(Resource* resource) const | 61 unsigned tagForCodeCache(CachedMetadataHandler* cacheHandler) const |
| 60 { | 62 { |
| 61 return V8ScriptRunner::tagForCodeCache(resource); | 63 return V8ScriptRunner::tagForCodeCache(cacheHandler); |
| 62 } | 64 } |
| 63 | 65 |
| 64 bool compileScript(V8CacheOptions cacheOptions) | 66 bool compileScript(V8CacheOptions cacheOptions) |
| 65 { | 67 { |
| 66 return !V8ScriptRunner::compileScript( | 68 return !V8ScriptRunner::compileScript( |
| 67 v8String(isolate(), code()), filename(), WTF::TextPosition(), | 69 v8String(isolate(), code()), filename(), WTF::TextPosition(), |
| 68 m_resource.get(), 0, isolate(), NotSharableCrossOrigin, cacheOptions ) | 70 isolate(), m_resource.get(), nullptr, nullptr, NotSharableCrossOrigi n, cacheOptions) |
| 69 .IsEmpty(); | 71 .IsEmpty(); |
| 70 } | 72 } |
| 71 | 73 |
| 72 void setEmptyResource() | 74 void setEmptyResource() |
| 73 { | 75 { |
| 74 m_resourceRequest = WTF::adoptPtr(new ResourceRequest); | 76 m_resourceRequest = WTF::adoptPtr(new ResourceRequest); |
| 75 m_resource = ScriptResource::create(*m_resourceRequest.get(), "UTF-8"); | 77 m_resource = ScriptResource::create(*m_resourceRequest.get(), "UTF-8"); |
| 78 m_cacheHandler = m_resource->cacheHandler(); | |
| 76 } | 79 } |
| 77 | 80 |
| 78 void setResource() | 81 void setResource() |
| 79 { | 82 { |
| 80 m_resourceRequest = WTF::adoptPtr(new ResourceRequest(url())); | 83 m_resourceRequest = WTF::adoptPtr(new ResourceRequest(url())); |
| 81 m_resource = ScriptResource::create(*m_resourceRequest.get(), "UTF-8"); | 84 m_resource = ScriptResource::create(*m_resourceRequest.get(), "UTF-8"); |
| 85 m_cacheHandler = m_resource->cacheHandler(); | |
| 82 } | 86 } |
| 83 | 87 |
| 84 protected: | 88 protected: |
| 85 WTF::OwnPtr<ResourceRequest> m_resourceRequest; | 89 WTF::OwnPtr<ResourceRequest> m_resourceRequest; |
| 86 OwnPtrWillBePersistent<ScriptResource> m_resource; | 90 OwnPtrWillBePersistent<ScriptResource> m_resource; |
| 91 CachedMetadataHandler* m_cacheHandler; | |
|
marja
2015/02/17 11:31:58
Instead of adding this member, could you add a hel
horo
2015/02/17 12:53:42
Done.
Added cacheHandler() function.
| |
| 87 V8TestingScope m_scope; | 92 V8TestingScope m_scope; |
| 88 | 93 |
| 89 static int counter; | 94 static int counter; |
| 90 }; | 95 }; |
| 91 | 96 |
| 92 int V8ScriptRunnerTest::counter = 0; | 97 int V8ScriptRunnerTest::counter = 0; |
| 93 | 98 |
| 94 TEST_F(V8ScriptRunnerTest, resourcelessShouldPass) | 99 TEST_F(V8ScriptRunnerTest, resourcelessShouldPass) |
| 95 { | 100 { |
| 96 EXPECT_TRUE(compileScript(V8CacheOptionsNone)); | 101 EXPECT_TRUE(compileScript(V8CacheOptionsNone)); |
| 97 EXPECT_TRUE(compileScript(V8CacheOptionsParseMemory)); | 102 EXPECT_TRUE(compileScript(V8CacheOptionsParseMemory)); |
| 98 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); | 103 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); |
| 99 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); | 104 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); |
| 100 } | 105 } |
| 101 | 106 |
| 102 TEST_F(V8ScriptRunnerTest, emptyResourceDoesNothing) | 107 TEST_F(V8ScriptRunnerTest, emptyResourceDoesNotHaveCacheHandler) |
| 103 { | 108 { |
| 104 setEmptyResource(); | 109 setEmptyResource(); |
| 105 EXPECT_TRUE(compileScript(V8CacheOptionsDefault)); | 110 EXPECT_FALSE(m_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 } | 111 } |
| 117 | 112 |
| 118 TEST_F(V8ScriptRunnerTest, parseMemoryOption) | 113 TEST_F(V8ScriptRunnerTest, parseMemoryOption) |
| 119 { | 114 { |
| 120 setResource(); | 115 setResource(); |
| 121 EXPECT_TRUE(compileScript(V8CacheOptionsParseMemory)); | 116 EXPECT_TRUE(compileScript(V8CacheOptionsParseMemory)); |
| 122 EXPECT_TRUE(m_resource->cachedMetadata(tagForParserCache(m_resource.get()))) ; | 117 EXPECT_TRUE(m_cacheHandler->cachedMetadata(tagForParserCache(m_cacheHandler) )); |
| 123 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache(m_resource.get()))); | 118 EXPECT_FALSE(m_cacheHandler->cachedMetadata(tagForCodeCache(m_cacheHandler)) ); |
| 124 // The cached data is associated with the encoding. | 119 // The cached data is associated with the encoding. |
| 125 ResourceRequest request(url()); | 120 ResourceRequest request(url()); |
| 126 OwnPtrWillBeRawPtr<ScriptResource> anotherResource = ScriptResource::create( request, "UTF-16"); | 121 OwnPtrWillBeRawPtr<ScriptResource> anotherResource = ScriptResource::create( request, "UTF-16"); |
| 127 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache(anotherResource.ge t()))); | 122 EXPECT_FALSE(m_cacheHandler->cachedMetadata(tagForParserCache(anotherResourc e->cacheHandler()))); |
| 128 } | 123 } |
| 129 | 124 |
| 130 TEST_F(V8ScriptRunnerTest, parseOption) | 125 TEST_F(V8ScriptRunnerTest, parseOption) |
| 131 { | 126 { |
| 132 setResource(); | 127 setResource(); |
| 133 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); | 128 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); |
| 134 EXPECT_TRUE(m_resource->cachedMetadata(tagForParserCache(m_resource.get()))) ; | 129 EXPECT_TRUE(m_cacheHandler->cachedMetadata(tagForParserCache(m_cacheHandler) )); |
| 135 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache(m_resource.get()))); | 130 EXPECT_FALSE(m_cacheHandler->cachedMetadata(tagForCodeCache(m_cacheHandler)) ); |
| 136 // The cached data is associated with the encoding. | 131 // The cached data is associated with the encoding. |
| 137 ResourceRequest request(url()); | 132 ResourceRequest request(url()); |
| 138 OwnPtrWillBeRawPtr<ScriptResource> anotherResource = ScriptResource::create( request, "UTF-16"); | 133 OwnPtrWillBeRawPtr<ScriptResource> anotherResource = ScriptResource::create( request, "UTF-16"); |
| 139 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache(anotherResource.ge t()))); | 134 EXPECT_FALSE(m_cacheHandler->cachedMetadata(tagForParserCache(anotherResourc e->cacheHandler()))); |
| 140 } | 135 } |
| 141 | 136 |
| 142 TEST_F(V8ScriptRunnerTest, codeOption) | 137 TEST_F(V8ScriptRunnerTest, codeOption) |
| 143 { | 138 { |
| 144 setResource(); | 139 setResource(); |
| 145 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); | 140 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); |
| 146 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache(m_resource.get())) ); | 141 EXPECT_FALSE(m_cacheHandler->cachedMetadata(tagForParserCache(m_cacheHandler ))); |
| 147 EXPECT_TRUE(m_resource->cachedMetadata(tagForCodeCache(m_resource.get()))); | 142 EXPECT_TRUE(m_cacheHandler->cachedMetadata(tagForCodeCache(m_cacheHandler))) ; |
| 148 // The cached data is associated with the encoding. | 143 // The cached data is associated with the encoding. |
| 149 ResourceRequest request(url()); | 144 ResourceRequest request(url()); |
| 150 OwnPtrWillBeRawPtr<ScriptResource> anotherResource = ScriptResource::create( request, "UTF-16"); | 145 OwnPtrWillBeRawPtr<ScriptResource> anotherResource = ScriptResource::create( request, "UTF-16"); |
| 151 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache(anotherResource.get( )))); | 146 EXPECT_FALSE(m_cacheHandler->cachedMetadata(tagForCodeCache(anotherResource- >cacheHandler()))); |
| 152 } | 147 } |
| 153 | 148 |
| 154 TEST_F(V8ScriptRunnerTest, codeCompressedOptions) | 149 TEST_F(V8ScriptRunnerTest, codeCompressedOptions) |
| 155 { | 150 { |
| 156 setResource(); | 151 setResource(); |
| 157 EXPECT_TRUE(compileScript(V8CacheOptionsCodeCompressed)); | 152 EXPECT_TRUE(compileScript(V8CacheOptionsCodeCompressed)); |
| 158 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache(m_resource.get())) ); | 153 EXPECT_FALSE(m_cacheHandler->cachedMetadata(tagForParserCache(m_cacheHandler ))); |
| 159 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache(m_resource.get()))); | 154 EXPECT_FALSE(m_cacheHandler->cachedMetadata(tagForCodeCache(m_cacheHandler)) ); |
| 160 } | 155 } |
| 161 | 156 |
| 162 } // namespace | 157 } // namespace |
| 163 | 158 |
| 164 } // namespace blink | 159 } // namespace blink |
| OLD | NEW |