OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/scoped_ptr.h" | 5 #include "extensions/renderer/activity_log_converter_strategy.h" |
| 6 |
| 7 #include <memory> |
| 8 |
6 #include "base/values.h" | 9 #include "base/values.h" |
7 #include "extensions/renderer/activity_log_converter_strategy.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "v8/include/v8.h" | 11 #include "v8/include/v8.h" |
10 | 12 |
11 using content::V8ValueConverter; | 13 using content::V8ValueConverter; |
12 | 14 |
13 namespace extensions { | 15 namespace extensions { |
14 | 16 |
15 class ActivityLogConverterStrategyTest : public testing::Test { | 17 class ActivityLogConverterStrategyTest : public testing::Test { |
16 public: | 18 public: |
17 ActivityLogConverterStrategyTest() | 19 ActivityLogConverterStrategyTest() |
18 : isolate_(v8::Isolate::GetCurrent()), | 20 : isolate_(v8::Isolate::GetCurrent()), |
19 handle_scope_(isolate_), | 21 handle_scope_(isolate_), |
20 context_(isolate_, v8::Context::New(isolate_)), | 22 context_(isolate_, v8::Context::New(isolate_)), |
21 context_scope_(context()) {} | 23 context_scope_(context()) {} |
22 | 24 |
23 protected: | 25 protected: |
24 void SetUp() override { | 26 void SetUp() override { |
25 converter_.reset(V8ValueConverter::create()); | 27 converter_.reset(V8ValueConverter::create()); |
26 strategy_.reset(new ActivityLogConverterStrategy()); | 28 strategy_.reset(new ActivityLogConverterStrategy()); |
27 converter_->SetFunctionAllowed(true); | 29 converter_->SetFunctionAllowed(true); |
28 converter_->SetStrategy(strategy_.get()); | 30 converter_->SetStrategy(strategy_.get()); |
29 } | 31 } |
30 | 32 |
31 testing::AssertionResult VerifyNull(v8::Local<v8::Value> v8_value) { | 33 testing::AssertionResult VerifyNull(v8::Local<v8::Value> v8_value) { |
32 scoped_ptr<base::Value> value( | 34 std::unique_ptr<base::Value> value( |
33 converter_->FromV8Value(v8_value, context())); | 35 converter_->FromV8Value(v8_value, context())); |
34 if (value->IsType(base::Value::TYPE_NULL)) | 36 if (value->IsType(base::Value::TYPE_NULL)) |
35 return testing::AssertionSuccess(); | 37 return testing::AssertionSuccess(); |
36 return testing::AssertionFailure(); | 38 return testing::AssertionFailure(); |
37 } | 39 } |
38 | 40 |
39 testing::AssertionResult VerifyBoolean(v8::Local<v8::Value> v8_value, | 41 testing::AssertionResult VerifyBoolean(v8::Local<v8::Value> v8_value, |
40 bool expected) { | 42 bool expected) { |
41 bool out; | 43 bool out; |
42 scoped_ptr<base::Value> value( | 44 std::unique_ptr<base::Value> value( |
43 converter_->FromV8Value(v8_value, context())); | 45 converter_->FromV8Value(v8_value, context())); |
44 if (value->IsType(base::Value::TYPE_BOOLEAN) | 46 if (value->IsType(base::Value::TYPE_BOOLEAN) |
45 && value->GetAsBoolean(&out) | 47 && value->GetAsBoolean(&out) |
46 && out == expected) | 48 && out == expected) |
47 return testing::AssertionSuccess(); | 49 return testing::AssertionSuccess(); |
48 return testing::AssertionFailure(); | 50 return testing::AssertionFailure(); |
49 } | 51 } |
50 | 52 |
51 testing::AssertionResult VerifyInteger(v8::Local<v8::Value> v8_value, | 53 testing::AssertionResult VerifyInteger(v8::Local<v8::Value> v8_value, |
52 int expected) { | 54 int expected) { |
53 int out; | 55 int out; |
54 scoped_ptr<base::Value> value( | 56 std::unique_ptr<base::Value> value( |
55 converter_->FromV8Value(v8_value, context())); | 57 converter_->FromV8Value(v8_value, context())); |
56 if (value->IsType(base::Value::TYPE_INTEGER) | 58 if (value->IsType(base::Value::TYPE_INTEGER) |
57 && value->GetAsInteger(&out) | 59 && value->GetAsInteger(&out) |
58 && out == expected) | 60 && out == expected) |
59 return testing::AssertionSuccess(); | 61 return testing::AssertionSuccess(); |
60 return testing::AssertionFailure(); | 62 return testing::AssertionFailure(); |
61 } | 63 } |
62 | 64 |
63 testing::AssertionResult VerifyDouble(v8::Local<v8::Value> v8_value, | 65 testing::AssertionResult VerifyDouble(v8::Local<v8::Value> v8_value, |
64 double expected) { | 66 double expected) { |
65 double out; | 67 double out; |
66 scoped_ptr<base::Value> value( | 68 std::unique_ptr<base::Value> value( |
67 converter_->FromV8Value(v8_value, context())); | 69 converter_->FromV8Value(v8_value, context())); |
68 if (value->IsType(base::Value::TYPE_DOUBLE) | 70 if (value->IsType(base::Value::TYPE_DOUBLE) |
69 && value->GetAsDouble(&out) | 71 && value->GetAsDouble(&out) |
70 && out == expected) | 72 && out == expected) |
71 return testing::AssertionSuccess(); | 73 return testing::AssertionSuccess(); |
72 return testing::AssertionFailure(); | 74 return testing::AssertionFailure(); |
73 } | 75 } |
74 | 76 |
75 testing::AssertionResult VerifyString(v8::Local<v8::Value> v8_value, | 77 testing::AssertionResult VerifyString(v8::Local<v8::Value> v8_value, |
76 const std::string& expected) { | 78 const std::string& expected) { |
77 std::string out; | 79 std::string out; |
78 scoped_ptr<base::Value> value( | 80 std::unique_ptr<base::Value> value( |
79 converter_->FromV8Value(v8_value, context())); | 81 converter_->FromV8Value(v8_value, context())); |
80 if (value->IsType(base::Value::TYPE_STRING) | 82 if (value->IsType(base::Value::TYPE_STRING) |
81 && value->GetAsString(&out) | 83 && value->GetAsString(&out) |
82 && out == expected) | 84 && out == expected) |
83 return testing::AssertionSuccess(); | 85 return testing::AssertionSuccess(); |
84 return testing::AssertionFailure(); | 86 return testing::AssertionFailure(); |
85 } | 87 } |
86 | 88 |
87 v8::Local<v8::Context> context() const { | 89 v8::Local<v8::Context> context() const { |
88 return v8::Local<v8::Context>::New(isolate_, context_); | 90 return v8::Local<v8::Context>::New(isolate_, context_); |
89 } | 91 } |
90 | 92 |
91 v8::Isolate* isolate_; | 93 v8::Isolate* isolate_; |
92 v8::HandleScope handle_scope_; | 94 v8::HandleScope handle_scope_; |
93 v8::Global<v8::Context> context_; | 95 v8::Global<v8::Context> context_; |
94 v8::Context::Scope context_scope_; | 96 v8::Context::Scope context_scope_; |
95 scoped_ptr<V8ValueConverter> converter_; | 97 std::unique_ptr<V8ValueConverter> converter_; |
96 scoped_ptr<ActivityLogConverterStrategy> strategy_; | 98 std::unique_ptr<ActivityLogConverterStrategy> strategy_; |
97 }; | 99 }; |
98 | 100 |
99 TEST_F(ActivityLogConverterStrategyTest, ConversionTest) { | 101 TEST_F(ActivityLogConverterStrategyTest, ConversionTest) { |
100 const char* source = "(function() {" | 102 const char* source = "(function() {" |
101 "function foo() {}" | 103 "function foo() {}" |
102 "return {" | 104 "return {" |
103 "null: null," | 105 "null: null," |
104 "true: true," | 106 "true: true," |
105 "false: false," | 107 "false: false," |
106 "positive_int: 42," | 108 "positive_int: 42," |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 "[Array]")); | 165 "[Array]")); |
164 EXPECT_TRUE(VerifyString( | 166 EXPECT_TRUE(VerifyString( |
165 v8_object->Get(v8::String::NewFromUtf8(isolate_, "function")), | 167 v8_object->Get(v8::String::NewFromUtf8(isolate_, "function")), |
166 "[Function]")); | 168 "[Function]")); |
167 EXPECT_TRUE(VerifyString( | 169 EXPECT_TRUE(VerifyString( |
168 v8_object->Get(v8::String::NewFromUtf8(isolate_, "named_function")), | 170 v8_object->Get(v8::String::NewFromUtf8(isolate_, "named_function")), |
169 "[Function foo()]")); | 171 "[Function foo()]")); |
170 } | 172 } |
171 | 173 |
172 } // namespace extensions | 174 } // namespace extensions |
OLD | NEW |