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 "testing/gtest/include/gtest/gtest.h"
9 #include "v8/include/v8.h"
11 using content::V8ValueConverter
;
13 namespace extensions
{
15 class ActivityLogConverterStrategyTest
: public testing::Test
{
17 ActivityLogConverterStrategyTest()
18 : isolate_(v8::Isolate::GetCurrent()),
19 handle_scope_(isolate_
),
20 context_(isolate_
, v8::Context::New(isolate_
)),
21 context_scope_(context()) {}
24 void SetUp() override
{
25 converter_
.reset(V8ValueConverter::create());
26 strategy_
.reset(new ActivityLogConverterStrategy());
27 converter_
->SetFunctionAllowed(true);
28 converter_
->SetStrategy(strategy_
.get());
31 testing::AssertionResult
VerifyNull(v8::Local
<v8::Value
> v8_value
) {
32 scoped_ptr
<base::Value
> value(
33 converter_
->FromV8Value(v8_value
, context()));
34 if (value
->IsType(base::Value::TYPE_NULL
))
35 return testing::AssertionSuccess();
36 return testing::AssertionFailure();
39 testing::AssertionResult
VerifyBoolean(v8::Local
<v8::Value
> v8_value
,
42 scoped_ptr
<base::Value
> value(
43 converter_
->FromV8Value(v8_value
, context()));
44 if (value
->IsType(base::Value::TYPE_BOOLEAN
)
45 && value
->GetAsBoolean(&out
)
47 return testing::AssertionSuccess();
48 return testing::AssertionFailure();
51 testing::AssertionResult
VerifyInteger(v8::Local
<v8::Value
> v8_value
,
54 scoped_ptr
<base::Value
> value(
55 converter_
->FromV8Value(v8_value
, context()));
56 if (value
->IsType(base::Value::TYPE_INTEGER
)
57 && value
->GetAsInteger(&out
)
59 return testing::AssertionSuccess();
60 return testing::AssertionFailure();
63 testing::AssertionResult
VerifyDouble(v8::Local
<v8::Value
> v8_value
,
66 scoped_ptr
<base::Value
> value(
67 converter_
->FromV8Value(v8_value
, context()));
68 if (value
->IsType(base::Value::TYPE_DOUBLE
)
69 && value
->GetAsDouble(&out
)
71 return testing::AssertionSuccess();
72 return testing::AssertionFailure();
75 testing::AssertionResult
VerifyString(v8::Local
<v8::Value
> v8_value
,
76 const std::string
& expected
) {
78 scoped_ptr
<base::Value
> value(
79 converter_
->FromV8Value(v8_value
, context()));
80 if (value
->IsType(base::Value::TYPE_STRING
)
81 && value
->GetAsString(&out
)
83 return testing::AssertionSuccess();
84 return testing::AssertionFailure();
87 v8::Handle
<v8::Context
> context() const {
88 return v8::Local
<v8::Context
>::New(isolate_
, context_
);
91 v8::Isolate
* isolate_
;
92 v8::HandleScope handle_scope_
;
93 v8::Global
<v8::Context
> context_
;
94 v8::Context::Scope context_scope_
;
95 scoped_ptr
<V8ValueConverter
> converter_
;
96 scoped_ptr
<ActivityLogConverterStrategy
> strategy_
;
99 TEST_F(ActivityLogConverterStrategyTest
, ConversionTest
) {
100 const char* source
= "(function() {"
110 "big_integral_double: 9007199254740992.0," // 2.0^53
111 "string: \"foobar\","
112 "empty_string: \"\","
117 "empty_dictionary: {},"
118 "list: [ \"monkey\", \"balls\" ],"
120 "function: function() {},"
121 "named_function: foo"
125 v8::Handle
<v8::Script
> script(
126 v8::Script::Compile(v8::String::NewFromUtf8(isolate_
, source
)));
127 v8::Handle
<v8::Object
> v8_object
= script
->Run().As
<v8::Object
>();
129 EXPECT_TRUE(VerifyString(v8_object
, "[Object]"));
131 VerifyNull(v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "null"))));
132 EXPECT_TRUE(VerifyBoolean(
133 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "true")), true));
134 EXPECT_TRUE(VerifyBoolean(
135 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "false")), false));
136 EXPECT_TRUE(VerifyInteger(
137 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "positive_int")), 42));
138 EXPECT_TRUE(VerifyInteger(
139 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "negative_int")), -42));
140 EXPECT_TRUE(VerifyInteger(
141 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "zero")), 0));
142 EXPECT_TRUE(VerifyDouble(
143 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "double")), 88.8));
144 EXPECT_TRUE(VerifyDouble(
145 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "big_integral_double")),
146 9007199254740992.0));
147 EXPECT_TRUE(VerifyString(
148 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "string")), "foobar"));
149 EXPECT_TRUE(VerifyString(
150 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "empty_string")), ""));
151 EXPECT_TRUE(VerifyString(
152 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "dictionary")),
154 EXPECT_TRUE(VerifyString(
155 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "empty_dictionary")),
157 EXPECT_TRUE(VerifyString(
158 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "list")), "[Array]"));
159 EXPECT_TRUE(VerifyString(
160 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "empty_list")),
162 EXPECT_TRUE(VerifyString(
163 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "function")),
165 EXPECT_TRUE(VerifyString(
166 v8_object
->Get(v8::String::NewFromUtf8(isolate_
, "named_function")),
167 "[Function foo()]"));
170 } // namespace extensions