Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / extensions / renderer / activity_log_converter_strategy_unittest.cc
blob1c089bfca618d73ad91473e9ae27a1704d678960
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 {
16 public:
17 ActivityLogConverterStrategyTest()
18 : isolate_(v8::Isolate::GetCurrent()),
19 handle_scope_(isolate_),
20 context_(isolate_, v8::Context::New(isolate_)),
21 context_scope_(context()) {}
23 protected:
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,
40 bool expected) {
41 bool out;
42 scoped_ptr<base::Value> value(
43 converter_->FromV8Value(v8_value, context()));
44 if (value->IsType(base::Value::TYPE_BOOLEAN)
45 && value->GetAsBoolean(&out)
46 && out == expected)
47 return testing::AssertionSuccess();
48 return testing::AssertionFailure();
51 testing::AssertionResult VerifyInteger(v8::Local<v8::Value> v8_value,
52 int expected) {
53 int out;
54 scoped_ptr<base::Value> value(
55 converter_->FromV8Value(v8_value, context()));
56 if (value->IsType(base::Value::TYPE_INTEGER)
57 && value->GetAsInteger(&out)
58 && out == expected)
59 return testing::AssertionSuccess();
60 return testing::AssertionFailure();
63 testing::AssertionResult VerifyDouble(v8::Local<v8::Value> v8_value,
64 double expected) {
65 double out;
66 scoped_ptr<base::Value> value(
67 converter_->FromV8Value(v8_value, context()));
68 if (value->IsType(base::Value::TYPE_DOUBLE)
69 && value->GetAsDouble(&out)
70 && out == expected)
71 return testing::AssertionSuccess();
72 return testing::AssertionFailure();
75 testing::AssertionResult VerifyString(v8::Local<v8::Value> v8_value,
76 const std::string& expected) {
77 std::string out;
78 scoped_ptr<base::Value> value(
79 converter_->FromV8Value(v8_value, context()));
80 if (value->IsType(base::Value::TYPE_STRING)
81 && value->GetAsString(&out)
82 && out == expected)
83 return testing::AssertionSuccess();
84 return testing::AssertionFailure();
87 v8::Local<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() {"
101 "function foo() {}"
102 "return {"
103 "null: null,"
104 "true: true,"
105 "false: false,"
106 "positive_int: 42,"
107 "negative_int: -42,"
108 "zero: 0,"
109 "double: 88.8,"
110 "big_integral_double: 9007199254740992.0," // 2.0^53
111 "string: \"foobar\","
112 "empty_string: \"\","
113 "dictionary: {"
114 "foo: \"bar\","
115 "hot: \"dog\","
116 "},"
117 "empty_dictionary: {},"
118 "list: [ \"monkey\", \"balls\" ],"
119 "empty_list: [],"
120 "function: function() {},"
121 "named_function: foo"
122 "};"
123 "})();";
125 v8::Local<v8::Script> script(
126 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source)));
127 v8::Local<v8::Object> v8_object = script->Run().As<v8::Object>();
129 EXPECT_TRUE(VerifyString(v8_object, "[Object]"));
130 EXPECT_TRUE(
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")),
153 "[Object]"));
154 EXPECT_TRUE(VerifyString(
155 v8_object->Get(v8::String::NewFromUtf8(isolate_, "empty_dictionary")),
156 "[Object]"));
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")),
161 "[Array]"));
162 EXPECT_TRUE(VerifyString(
163 v8_object->Get(v8::String::NewFromUtf8(isolate_, "function")),
164 "[Function]"));
165 EXPECT_TRUE(VerifyString(
166 v8_object->Get(v8::String::NewFromUtf8(isolate_, "named_function")),
167 "[Function foo()]"));
170 } // namespace extensions