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 "core/fetch/ScriptResource.h" | 9 #include "core/fetch/ScriptResource.h" |
9 #include "platform/heap/Handle.h" | 10 #include "platform/heap/Handle.h" |
10 #include <gtest/gtest.h> | 11 #include <gtest/gtest.h> |
11 #include <v8.h> | 12 #include <v8.h> |
12 | 13 |
13 namespace blink { | 14 namespace blink { |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 class V8ScriptRunnerTest : public ::testing::Test { | 18 class V8ScriptRunnerTest : public ::testing::Test { |
18 public: | 19 public: |
19 V8ScriptRunnerTest() { } | 20 V8ScriptRunnerTest() : m_scope(v8::Isolate::GetCurrent()) { } |
20 virtual ~V8ScriptRunnerTest() { } | 21 virtual ~V8ScriptRunnerTest() { } |
21 | 22 |
22 static void SetUpTestCase() | |
23 { | |
24 cacheTagParser = StringHash::hash(v8::V8::GetVersion()) * 2; | |
25 cacheTagCode = cacheTagParser + 1; | |
26 } | |
27 | |
28 virtual void SetUp() OVERRIDE | 23 virtual void SetUp() OVERRIDE |
29 { | 24 { |
30 // To trick various layers of caching, increment a counter for each | 25 // To trick various layers of caching, increment a counter for each |
31 // test and use it in code(), fielname() and url(). | 26 // test and use it in code(), fielname() and url(). |
32 counter++; | 27 counter++; |
33 } | 28 } |
34 | 29 |
35 virtual void TearDown() OVERRIDE | 30 virtual void TearDown() OVERRIDE |
36 { | 31 { |
37 m_resourceRequest.clear(); | 32 m_resourceRequest.clear(); |
38 m_resource.clear(); | 33 m_resource.clear(); |
39 } | 34 } |
40 | 35 |
41 v8::Isolate* isolate() | 36 v8::Isolate* isolate() const |
42 { | 37 { |
43 return v8::Isolate::GetCurrent(); | 38 return m_scope.isolate(); |
44 } | 39 } |
45 v8::Local<v8::String> v8String(const char* value) | 40 WTF::String code() const |
46 { | |
47 return v8::String::NewFromOneByte( | |
48 isolate(), reinterpret_cast<const uint8_t*>(value)); | |
49 } | |
50 v8::Local<v8::String> v8String(const WTF::String& value) | |
51 { | |
52 return v8String(value.ascii().data()); | |
53 } | |
54 WTF::String code() | |
55 { | 41 { |
56 // Simple function for testing. Note: | 42 // Simple function for testing. Note: |
57 // - Add counter to trick V8 code cache. | 43 // - Add counter to trick V8 code cache. |
58 // - Pad counter to 1000 digits, to trick minimal cacheability threshold
. | 44 // - Pad counter to 1000 digits, to trick minimal cacheability threshold
. |
59 return WTF::String::format("a = function() { 1 + 1; } // %01000d\n", cou
nter); | 45 return WTF::String::format("a = function() { 1 + 1; } // %01000d\n", cou
nter); |
60 } | 46 } |
61 WTF::String filename() | 47 WTF::String filename() const |
62 { | 48 { |
63 return WTF::String::format("whatever%d.js", counter); | 49 return WTF::String::format("whatever%d.js", counter); |
64 } | 50 } |
65 WTF::String url() | 51 WTF::String url() const |
66 { | 52 { |
67 return WTF::String::format("http://bla.com/bla%d", counter); | 53 return WTF::String::format("http://bla.com/bla%d", counter); |
68 } | 54 } |
| 55 unsigned tagForParserCache() const |
| 56 { |
| 57 return StringHash::hash(v8::V8::GetVersion()) * 2; |
| 58 } |
| 59 unsigned tagForCodeCache() const |
| 60 { |
| 61 return tagForParserCache() + 1; |
| 62 } |
69 | 63 |
70 bool compileScript(V8CacheOptions cacheOptions) | 64 bool compileScript(V8CacheOptions cacheOptions) |
71 { | 65 { |
72 v8::HandleScope handleScope(isolate()); | |
73 v8::Local<v8::Context> context = v8::Context::New(isolate()); | |
74 v8::Context::Scope contextScope(context); | |
75 return !V8ScriptRunner::compileScript( | 66 return !V8ScriptRunner::compileScript( |
76 v8String(code()), filename(), WTF::TextPosition(), m_resource.get(), | 67 v8String(isolate(), code()), filename(), WTF::TextPosition(), |
77 isolate(), NotSharableCrossOrigin, cacheOptions) | 68 m_resource.get(), isolate(), NotSharableCrossOrigin, cacheOptions) |
78 .IsEmpty(); | 69 .IsEmpty(); |
79 } | 70 } |
80 | 71 |
81 void setEmptyResource() | 72 void setEmptyResource() |
82 { | 73 { |
83 m_resourceRequest = WTF::adoptPtr(new ResourceRequest); | 74 m_resourceRequest = WTF::adoptPtr(new ResourceRequest); |
84 m_resource = adoptPtrWillBeNoop(new ScriptResource(*m_resourceRequest.ge
t(), "text/utf-8")); | 75 m_resource = adoptPtrWillBeNoop(new ScriptResource(*m_resourceRequest.ge
t(), "text/utf-8")); |
85 } | 76 } |
86 | 77 |
87 void setResource() | 78 void setResource() |
88 { | 79 { |
89 m_resourceRequest = WTF::adoptPtr(new ResourceRequest(url())); | 80 m_resourceRequest = WTF::adoptPtr(new ResourceRequest(url())); |
90 m_resource = adoptPtrWillBeNoop(new ScriptResource(*m_resourceRequest.ge
t(), "text/utf-8")); | 81 m_resource = adoptPtrWillBeNoop(new ScriptResource(*m_resourceRequest.ge
t(), "text/utf-8")); |
91 } | 82 } |
92 | 83 |
93 protected: | 84 protected: |
94 WTF::OwnPtr<ResourceRequest> m_resourceRequest; | 85 WTF::OwnPtr<ResourceRequest> m_resourceRequest; |
95 OwnPtrWillBePersistent<ScriptResource> m_resource; | 86 OwnPtrWillBePersistent<ScriptResource> m_resource; |
| 87 V8TestingScope m_scope; |
96 | 88 |
97 static unsigned cacheTagParser; | |
98 static unsigned cacheTagCode; | |
99 static int counter; | 89 static int counter; |
100 }; | 90 }; |
101 | 91 |
102 unsigned V8ScriptRunnerTest::cacheTagParser = 0; | |
103 unsigned V8ScriptRunnerTest::cacheTagCode = 0; | |
104 int V8ScriptRunnerTest::counter = 0; | 92 int V8ScriptRunnerTest::counter = 0; |
105 | 93 |
106 TEST_F(V8ScriptRunnerTest, resourcelessShouldPass) | 94 TEST_F(V8ScriptRunnerTest, resourcelessShouldPass) |
107 { | 95 { |
108 EXPECT_TRUE(compileScript(V8CacheOptionsOff)); | 96 EXPECT_TRUE(compileScript(V8CacheOptionsOff)); |
109 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); | 97 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); |
110 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); | 98 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); |
111 } | 99 } |
112 | 100 |
113 TEST_F(V8ScriptRunnerTest, emptyResourceDoesNothing) | 101 TEST_F(V8ScriptRunnerTest, emptyResourceDoesNothing) |
114 { | 102 { |
115 setEmptyResource(); | 103 setEmptyResource(); |
116 EXPECT_TRUE(compileScript(V8CacheOptionsOff)); | 104 EXPECT_TRUE(compileScript(V8CacheOptionsOff)); |
117 EXPECT_FALSE(m_resource->cachedMetadata(cacheTagParser)); | 105 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache())); |
118 EXPECT_FALSE(m_resource->cachedMetadata(cacheTagCode)); | 106 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache())); |
119 | 107 |
120 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); | 108 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); |
121 EXPECT_FALSE(m_resource->cachedMetadata(cacheTagParser)); | 109 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache())); |
122 EXPECT_FALSE(m_resource->cachedMetadata(cacheTagCode)); | 110 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache())); |
123 | 111 |
124 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); | 112 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); |
125 EXPECT_FALSE(m_resource->cachedMetadata(cacheTagParser)); | 113 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache())); |
126 EXPECT_FALSE(m_resource->cachedMetadata(cacheTagCode)); | 114 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache())); |
127 } | 115 } |
128 | 116 |
129 TEST_F(V8ScriptRunnerTest, defaultOptions) | 117 TEST_F(V8ScriptRunnerTest, defaultOptions) |
130 { | 118 { |
131 setResource(); | 119 setResource(); |
132 EXPECT_TRUE(compileScript(V8CacheOptionsOff)); | 120 EXPECT_TRUE(compileScript(V8CacheOptionsOff)); |
133 EXPECT_TRUE(m_resource->cachedMetadata(cacheTagParser)); | 121 EXPECT_TRUE(m_resource->cachedMetadata(tagForParserCache())); |
134 EXPECT_FALSE(m_resource->cachedMetadata(cacheTagCode)); | 122 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache())); |
135 } | 123 } |
136 | 124 |
137 TEST_F(V8ScriptRunnerTest, parseOptions) | 125 TEST_F(V8ScriptRunnerTest, parseOptions) |
138 { | 126 { |
139 setResource(); | 127 setResource(); |
140 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); | 128 EXPECT_TRUE(compileScript(V8CacheOptionsParse)); |
141 EXPECT_TRUE(m_resource->cachedMetadata(cacheTagParser)); | 129 EXPECT_TRUE(m_resource->cachedMetadata(tagForParserCache())); |
142 EXPECT_FALSE(m_resource->cachedMetadata(cacheTagCode)); | 130 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache())); |
143 } | 131 } |
144 | 132 |
145 TEST_F(V8ScriptRunnerTest, codeOptions) | 133 TEST_F(V8ScriptRunnerTest, codeOptions) |
146 { | 134 { |
147 setResource(); | 135 setResource(); |
148 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); | 136 EXPECT_TRUE(compileScript(V8CacheOptionsCode)); |
149 EXPECT_FALSE(m_resource->cachedMetadata(cacheTagParser)); | 137 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache())); |
150 | 138 |
151 // TODO(vogelheim): Code caching is presently still disabled. | 139 // FIXME: Code caching is presently still disabled. |
152 // Enable EXPECT when code caching lands. | 140 // Enable EXPECT when code caching lands. |
153 // EXPECT_TRUE(m_resource->cachedMetadata(cacheTagCode)); | 141 // EXPECT_TRUE(m_resource->cachedMetadata(tagForCodeCache())); |
154 } | 142 } |
155 | 143 |
156 } // namespace | 144 } // namespace |
157 | 145 |
158 } // namespace blink | 146 } // namespace blink |
OLD | NEW |