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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/values.h"
7 #include "extensions/renderer/activity_log_converter_strategy.h"
8 #include "extensions/renderer/scoped_persistent.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "v8/include/v8.h"
12 using content::V8ValueConverter
;
14 namespace extensions
{
16 class ActivityLogConverterStrategyTest
: public testing::Test
{
18 ActivityLogConverterStrategyTest()
19 : isolate_(v8::Isolate::GetCurrent())
20 , handle_scope_(isolate_
)
21 , context_(v8::Context::New(isolate_
))
22 , context_scope_(context()) {
26 virtual void SetUp() {
27 converter_
.reset(V8ValueConverter::create());
28 strategy_
.reset(new ActivityLogConverterStrategy());
29 converter_
->SetFunctionAllowed(true);
30 converter_
->SetStrategy(strategy_
.get());
33 testing::AssertionResult
VerifyNull(v8::Local
<v8::Value
> v8_value
) {
34 scoped_ptr
<base::Value
> value(
35 converter_
->FromV8Value(v8_value
, context()));
36 if (value
->IsType(base::Value::TYPE_NULL
))
37 return testing::AssertionSuccess();
38 return testing::AssertionFailure();
41 testing::AssertionResult
VerifyBoolean(v8::Local
<v8::Value
> v8_value
,
44 scoped_ptr
<base::Value
> value(
45 converter_
->FromV8Value(v8_value
, context()));
46 if (value
->IsType(base::Value::TYPE_BOOLEAN
)
47 && value
->GetAsBoolean(&out
)
49 return testing::AssertionSuccess();
50 return testing::AssertionFailure();
53 testing::AssertionResult
VerifyInteger(v8::Local
<v8::Value
> v8_value
,
56 scoped_ptr
<base::Value
> value(
57 converter_
->FromV8Value(v8_value
, context()));
58 if (value
->IsType(base::Value::TYPE_INTEGER
)
59 && value
->GetAsInteger(&out
)
61 return testing::AssertionSuccess();
62 return testing::AssertionFailure();
65 testing::AssertionResult
VerifyDouble(v8::Local
<v8::Value
> v8_value
,
68 scoped_ptr
<base::Value
> value(
69 converter_
->FromV8Value(v8_value
, context()));
70 if (value
->IsType(base::Value::TYPE_DOUBLE
)
71 && value
->GetAsDouble(&out
)
73 return testing::AssertionSuccess();
74 return testing::AssertionFailure();
77 testing::AssertionResult
VerifyString(v8::Local
<v8::Value
> v8_value
,
78 const std::string
& expected
) {
80 scoped_ptr
<base::Value
> value(
81 converter_
->FromV8Value(v8_value
, context()));
82 if (value
->IsType(base::Value::TYPE_STRING
)
83 && value
->GetAsString(&out
)
85 return testing::AssertionSuccess();
86 return testing::AssertionFailure();
89 v8::Handle
<v8::Context
> context() const {
90 return context_
.NewHandle(isolate_
);
93 v8::Isolate
* isolate_
;
94 v8::HandleScope handle_scope_
;
95 ScopedPersistent
<v8::Context
> context_
;
96 v8::Context::Scope context_scope_
;
97 scoped_ptr
<V8ValueConverter
> converter_
;
98 scoped_ptr
<ActivityLogConverterStrategy
> strategy_
;
101 TEST_F(ActivityLogConverterStrategyTest
, ConversionTest
) {
102 const char* source
= "(function() {"
112 "big_integral_double: 9007199254740992.0," // 2.0^53
113 "string: \"foobar\","
114 "empty_string: \"\","
119 "empty_dictionary: {},"
120 "list: [ \"monkey\", \"balls\" ],"
122 "function: function() {},"
123 "named_function: foo"
127 v8::Handle
<v8::Script
> script(
128 v8::Script::Compile(v8::String::NewFromUtf8(isolate_
, source
)));
129 v8::Handle
<v8::Object
> v8_object
= script
->Run().As
<v8::Object
>();
131 EXPECT_TRUE(VerifyString(v8_object
, "[Object]"));
133 VerifyNull(v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "null"))));
134 EXPECT_TRUE(VerifyBoolean(
135 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "true")), true));
136 EXPECT_TRUE(VerifyBoolean(
137 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "false")), false));
138 EXPECT_TRUE(VerifyInteger(
139 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "positive_int")), 42));
140 EXPECT_TRUE(VerifyInteger(
141 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "negative_int")), -42));
142 EXPECT_TRUE(VerifyInteger(
143 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "zero")), 0));
144 EXPECT_TRUE(VerifyDouble(
145 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "double")), 88.8));
146 EXPECT_TRUE(VerifyDouble(
147 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "big_integral_double")),
148 9007199254740992.0));
149 EXPECT_TRUE(VerifyString(
150 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "string")), "foobar"));
151 EXPECT_TRUE(VerifyString(
152 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "empty_string")), ""));
153 EXPECT_TRUE(VerifyString(
154 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "dictionary")),
156 EXPECT_TRUE(VerifyString(
157 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "empty_dictionary")),
159 EXPECT_TRUE(VerifyString(
160 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "list")), "[Array]"));
161 EXPECT_TRUE(VerifyString(
162 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "empty_list")),
164 EXPECT_TRUE(VerifyString(
165 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "function")),
167 EXPECT_TRUE(VerifyString(
168 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "named_function")),
169 "[Function foo()]"));
172 } // namespace extensions