Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / V8ScriptRunnerTest.cpp
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 "bindings/core/v8/V8Binding.h"
9 #include "core/fetch/ScriptResource.h"
10 #include "platform/heap/Handle.h"
11 #include <gtest/gtest.h>
12 #include <v8.h>
13
14 namespace blink {
15
16 namespace {
17
18 class V8ScriptRunnerTest : public ::testing::Test {
19 public:
20     V8ScriptRunnerTest() : m_scope(v8::Isolate::GetCurrent()) { }
21     virtual ~V8ScriptRunnerTest() { }
22
23     virtual void SetUp() OVERRIDE
24     {
25         // To trick various layers of caching, increment a counter for each
26         // test and use it in code(), fielname() and url().
27         counter++;
28     }
29
30     virtual void TearDown() OVERRIDE
31     {
32         m_resourceRequest.clear();
33         m_resource.clear();
34     }
35
36     v8::Isolate* isolate() const
37     {
38         return m_scope.isolate();
39     }
40     WTF::String code() const
41     {
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);
46     }
47     WTF::String filename() const
48     {
49         return WTF::String::format("whatever%d.js", counter);
50     }
51     WTF::String url() const
52     {
53         return WTF::String::format("http://bla.com/bla%d", counter);
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     }
63
64     bool compileScript(V8CacheOptions cacheOptions)
65     {
66         return !V8ScriptRunner::compileScript(
67             v8String(isolate(), code()), filename(), WTF::TextPosition(),
68             m_resource.get(), 0, isolate(), NotSharableCrossOrigin, cacheOptions)
69             .IsEmpty();
70     }
71
72     void setEmptyResource()
73     {
74         m_resourceRequest = WTF::adoptPtr(new ResourceRequest);
75         m_resource = adoptPtrWillBeNoop(new ScriptResource(*m_resourceRequest.get(), "text/utf-8"));
76     }
77
78     void setResource()
79     {
80         m_resourceRequest = WTF::adoptPtr(new ResourceRequest(url()));
81         m_resource = adoptPtrWillBeNoop(new ScriptResource(*m_resourceRequest.get(), "text/utf-8"));
82     }
83
84 protected:
85     WTF::OwnPtr<ResourceRequest> m_resourceRequest;
86     OwnPtrWillBePersistent<ScriptResource> m_resource;
87     V8TestingScope m_scope;
88
89     static int counter;
90 };
91
92 int V8ScriptRunnerTest::counter = 0;
93
94 TEST_F(V8ScriptRunnerTest, resourcelessShouldPass)
95 {
96     EXPECT_TRUE(compileScript(V8CacheOptionsOff));
97     EXPECT_TRUE(compileScript(V8CacheOptionsParse));
98     EXPECT_TRUE(compileScript(V8CacheOptionsCode));
99 }
100
101 TEST_F(V8ScriptRunnerTest, emptyResourceDoesNothing)
102 {
103     setEmptyResource();
104     EXPECT_TRUE(compileScript(V8CacheOptionsOff));
105     EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache()));
106     EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache()));
107
108     EXPECT_TRUE(compileScript(V8CacheOptionsParse));
109     EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache()));
110     EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache()));
111
112     EXPECT_TRUE(compileScript(V8CacheOptionsCode));
113     EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache()));
114     EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache()));
115 }
116
117 TEST_F(V8ScriptRunnerTest, defaultOptions)
118 {
119     setResource();
120     EXPECT_TRUE(compileScript(V8CacheOptionsOff));
121     EXPECT_TRUE(m_resource->cachedMetadata(tagForParserCache()));
122     EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache()));
123 }
124
125 TEST_F(V8ScriptRunnerTest, parseOptions)
126 {
127     setResource();
128     EXPECT_TRUE(compileScript(V8CacheOptionsParse));
129     EXPECT_TRUE(m_resource->cachedMetadata(tagForParserCache()));
130     EXPECT_FALSE(m_resource->cachedMetadata(tagForCodeCache()));
131 }
132
133 TEST_F(V8ScriptRunnerTest, codeOptions)
134 {
135     setResource();
136     EXPECT_TRUE(compileScript(V8CacheOptionsCode));
137     EXPECT_FALSE(m_resource->cachedMetadata(tagForParserCache()));
138
139     // FIXME: Code caching is presently still disabled.
140     //        Enable EXPECT when code caching lands.
141     // EXPECT_TRUE(m_resource->cachedMetadata(tagForCodeCache()));
142 }
143
144 } // namespace
145
146 } // namespace blink