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.
6 #include "bindings/core/v8/V8ScriptRunner.h"
8 #include "bindings/core/v8/V8Binding.h"
9 #include "core/fetch/ScriptResource.h"
10 #include "platform/heap/Handle.h"
11 #include <gtest/gtest.h>
18 class V8ScriptRunnerTest : public ::testing::Test {
20 V8ScriptRunnerTest() : m_scope(v8::Isolate::GetCurrent()) { }
21 virtual ~V8ScriptRunnerTest() { }
23 virtual void SetUp() OVERRIDE
25 // To trick various layers of caching, increment a counter for each
26 // test and use it in code(), fielname() and url().
30 virtual void TearDown() OVERRIDE
32 m_resourceRequest.clear();
36 v8::Isolate* isolate() const
38 return m_scope.isolate();
40 WTF::String code() const
42 // Simple function for testing. Note:
43 // - Add counter to trick V8 code cache.
44 // - Pad counter to 1000 digits, to trick minimal cacheability threshold.
45 return WTF::String::format("a = function() { 1 + 1; } // %01000d\n", counter);
47 WTF::String filename() const
49 return WTF::String::format("whatever%d.js", counter);
51 WTF::String url() const
53 return WTF::String::format("http://bla.com/bla%d", counter);
55 unsigned tagForParserCache() const
57 return StringHash::hash(v8::V8::GetVersion()) * 2;
59 unsigned tagForCodeCache() const
61 return tagForParserCache() + 1;
64 bool compileScript(V8CacheOptions cacheOptions)
66 return !V8ScriptRunner::compileScript(
67 v8String(isolate(), code()), filename(), WTF::TextPosition(),
68 m_resource.get(), 0, isolate(), NotSharableCrossOrigin, cacheOptions)
72 void setEmptyResource()
74 m_resourceRequest = WTF::adoptPtr(new ResourceRequest);
75 m_resource = adoptPtrWillBeNoop(new ScriptResource(*m_resourceRequest.get(), "text/utf-8"));
80 m_resourceRequest = WTF::adoptPtr(new ResourceRequest(url()));
81 m_resource = adoptPtrWillBeNoop(new ScriptResource(*m_resourceRequest.get(), "text/utf-8"));
85 WTF::OwnPtr<ResourceRequest> m_resourceRequest;
86 OwnPtrWillBePersistent<ScriptResource> m_resource;
87 V8TestingScope m_scope;
92 int V8ScriptRunnerTest::counter = 0;
94 TEST_F(V8ScriptRunnerTest, resourcelessShouldPass)
96 EXPECT_TRUE(compileScript(V8CacheOptionsOff));
97 EXPECT_TRUE(compileScript(V8CacheOptionsParse));
98 EXPECT_TRUE(compileScript(V8CacheOptionsCode));
101 TEST_F(V8ScriptRunnerTest, emptyResourceDoesNothing)
104 EXPECT_TRUE(compileScript(V8CacheOptionsOff));
105 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache()));
106 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache()));
108 EXPECT_TRUE(compileScript(V8CacheOptionsParse));
109 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache()));
110 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache()));
112 EXPECT_TRUE(compileScript(V8CacheOptionsCode));
113 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache()));
114 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache()));
117 TEST_F(V8ScriptRunnerTest, defaultOptions)
120 EXPECT_TRUE(compileScript(V8CacheOptionsOff));
121 EXPECT_TRUE(m_resource->cachedMetadata(tagForParserCache()));
122 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache()));
125 TEST_F(V8ScriptRunnerTest, parseOptions)
128 EXPECT_TRUE(compileScript(V8CacheOptionsParse));
129 EXPECT_TRUE(m_resource->cachedMetadata(tagForParserCache()));
130 EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache()));
133 TEST_F(V8ScriptRunnerTest, codeOptions)
136 EXPECT_TRUE(compileScript(V8CacheOptionsCode));
137 EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache()));
139 // FIXME: Code caching is presently still disabled.
140 // Enable EXPECT when code caching lands.
141 // EXPECT_TRUE(m_resource->cachedMetadata(tagForCodeCache()));