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