OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/memory/scoped_ptr.h" |
6 #include "base/values.h" | 6 #include "base/values.h" |
7 #include "chrome/renderer/extensions/activity_log_converter_strategy.h" | 7 #include "chrome/renderer/extensions/activity_log_converter_strategy.h" |
8 #include "chrome/renderer/extensions/scoped_persistent.h" | 8 #include "chrome/renderer/extensions/scoped_persistent.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "v8/include/v8.h" | 10 #include "v8/include/v8.h" |
11 | 11 |
12 using content::V8ValueConverter; | 12 using content::V8ValueConverter; |
13 | 13 |
14 namespace extensions { | 14 namespace extensions { |
15 | 15 |
16 class ActivityLogConverterStrategyTest : public testing::Test { | 16 class ActivityLogConverterStrategyTest : public testing::Test { |
17 public: | 17 public: |
18 ActivityLogConverterStrategyTest() | 18 ActivityLogConverterStrategyTest() |
19 : isolate_(v8::Isolate::GetCurrent()) | 19 : isolate_(v8::Isolate::GetCurrent()) |
20 , handle_scope_(isolate_) | 20 , handle_scope_(isolate_) |
21 , context_(v8::Context::New(isolate_)) | 21 , context_(v8::Context::New(isolate_)) |
22 , context_scope_(context()) { | 22 , context_scope_(context_.get()) { |
23 } | 23 } |
24 | 24 |
25 protected: | 25 protected: |
26 virtual void SetUp() { | 26 virtual void SetUp() { |
27 converter_.reset(V8ValueConverter::create()); | 27 converter_.reset(V8ValueConverter::create()); |
28 strategy_.reset(new ActivityLogConverterStrategy()); | 28 strategy_.reset(new ActivityLogConverterStrategy()); |
29 converter_->SetFunctionAllowed(true); | 29 converter_->SetFunctionAllowed(true); |
30 converter_->SetStrategy(strategy_.get()); | 30 converter_->SetStrategy(strategy_.get()); |
31 } | 31 } |
32 | 32 |
33 testing::AssertionResult VerifyNull(v8::Local<v8::Value> v8_value) { | 33 testing::AssertionResult VerifyNull(v8::Local<v8::Value> v8_value) { |
34 scoped_ptr<base::Value> value( | 34 scoped_ptr<base::Value> value( |
35 converter_->FromV8Value(v8_value, context())); | 35 converter_->FromV8Value(v8_value, context_.get())); |
36 if (value->IsType(base::Value::TYPE_NULL)) | 36 if (value->IsType(base::Value::TYPE_NULL)) |
37 return testing::AssertionSuccess(); | 37 return testing::AssertionSuccess(); |
38 return testing::AssertionFailure(); | 38 return testing::AssertionFailure(); |
39 } | 39 } |
40 | 40 |
41 testing::AssertionResult VerifyBoolean(v8::Local<v8::Value> v8_value, | 41 testing::AssertionResult VerifyBoolean(v8::Local<v8::Value> v8_value, |
42 bool expected) { | 42 bool expected) { |
43 bool out; | 43 bool out; |
44 scoped_ptr<base::Value> value( | 44 scoped_ptr<base::Value> value( |
45 converter_->FromV8Value(v8_value, context())); | 45 converter_->FromV8Value(v8_value, context_.get())); |
46 if (value->IsType(base::Value::TYPE_BOOLEAN) | 46 if (value->IsType(base::Value::TYPE_BOOLEAN) |
47 && value->GetAsBoolean(&out) | 47 && value->GetAsBoolean(&out) |
48 && out == expected) | 48 && out == expected) |
49 return testing::AssertionSuccess(); | 49 return testing::AssertionSuccess(); |
50 return testing::AssertionFailure(); | 50 return testing::AssertionFailure(); |
51 } | 51 } |
52 | 52 |
53 testing::AssertionResult VerifyInteger(v8::Local<v8::Value> v8_value, | 53 testing::AssertionResult VerifyInteger(v8::Local<v8::Value> v8_value, |
54 int expected) { | 54 int expected) { |
55 int out; | 55 int out; |
56 scoped_ptr<base::Value> value( | 56 scoped_ptr<base::Value> value( |
57 converter_->FromV8Value(v8_value, context())); | 57 converter_->FromV8Value(v8_value, context_.get())); |
58 if (value->IsType(base::Value::TYPE_INTEGER) | 58 if (value->IsType(base::Value::TYPE_INTEGER) |
59 && value->GetAsInteger(&out) | 59 && value->GetAsInteger(&out) |
60 && out == expected) | 60 && out == expected) |
61 return testing::AssertionSuccess(); | 61 return testing::AssertionSuccess(); |
62 return testing::AssertionFailure(); | 62 return testing::AssertionFailure(); |
63 } | 63 } |
64 | 64 |
65 testing::AssertionResult VerifyDouble(v8::Local<v8::Value> v8_value, | 65 testing::AssertionResult VerifyDouble(v8::Local<v8::Value> v8_value, |
66 double expected) { | 66 double expected) { |
67 double out; | 67 double out; |
68 scoped_ptr<base::Value> value( | 68 scoped_ptr<base::Value> value( |
69 converter_->FromV8Value(v8_value, context())); | 69 converter_->FromV8Value(v8_value, context_.get())); |
70 if (value->IsType(base::Value::TYPE_DOUBLE) | 70 if (value->IsType(base::Value::TYPE_DOUBLE) |
71 && value->GetAsDouble(&out) | 71 && value->GetAsDouble(&out) |
72 && out == expected) | 72 && out == expected) |
73 return testing::AssertionSuccess(); | 73 return testing::AssertionSuccess(); |
74 return testing::AssertionFailure(); | 74 return testing::AssertionFailure(); |
75 } | 75 } |
76 | 76 |
77 testing::AssertionResult VerifyString(v8::Local<v8::Value> v8_value, | 77 testing::AssertionResult VerifyString(v8::Local<v8::Value> v8_value, |
78 const std::string& expected) { | 78 const std::string& expected) { |
79 std::string out; | 79 std::string out; |
80 scoped_ptr<base::Value> value( | 80 scoped_ptr<base::Value> value( |
81 converter_->FromV8Value(v8_value, context())); | 81 converter_->FromV8Value(v8_value, context_.get())); |
82 if (value->IsType(base::Value::TYPE_STRING) | 82 if (value->IsType(base::Value::TYPE_STRING) |
83 && value->GetAsString(&out) | 83 && value->GetAsString(&out) |
84 && out == expected) | 84 && out == expected) |
85 return testing::AssertionSuccess(); | 85 return testing::AssertionSuccess(); |
86 return testing::AssertionFailure(); | 86 return testing::AssertionFailure(); |
87 } | 87 } |
88 | 88 |
89 v8::Handle<v8::Context> context() const { | |
90 return context_.NewHandle(isolate_); | |
91 } | |
92 | |
93 v8::Isolate* isolate_; | 89 v8::Isolate* isolate_; |
94 v8::HandleScope handle_scope_; | 90 v8::HandleScope handle_scope_; |
95 ScopedPersistent<v8::Context> context_; | 91 ScopedPersistent<v8::Context> context_; |
96 v8::Context::Scope context_scope_; | 92 v8::Context::Scope context_scope_; |
97 scoped_ptr<V8ValueConverter> converter_; | 93 scoped_ptr<V8ValueConverter> converter_; |
98 scoped_ptr<ActivityLogConverterStrategy> strategy_; | 94 scoped_ptr<ActivityLogConverterStrategy> strategy_; |
99 }; | 95 }; |
100 | 96 |
101 TEST_F(ActivityLogConverterStrategyTest, ConversionTest) { | 97 TEST_F(ActivityLogConverterStrategyTest, ConversionTest) { |
102 const char* source = "(function() {" | 98 const char* source = "(function() {" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 "[Array]")); | 148 "[Array]")); |
153 EXPECT_TRUE(VerifyString(v8_object->Get(v8::String::New("empty_list")), | 149 EXPECT_TRUE(VerifyString(v8_object->Get(v8::String::New("empty_list")), |
154 "[Array]")); | 150 "[Array]")); |
155 EXPECT_TRUE(VerifyString(v8_object->Get(v8::String::New("function")), | 151 EXPECT_TRUE(VerifyString(v8_object->Get(v8::String::New("function")), |
156 "[Function]")); | 152 "[Function]")); |
157 EXPECT_TRUE(VerifyString(v8_object->Get(v8::String::New("named_function")), | 153 EXPECT_TRUE(VerifyString(v8_object->Get(v8::String::New("named_function")), |
158 "[Function foo()]")); | 154 "[Function foo()]")); |
159 } | 155 } |
160 | 156 |
161 } // namespace extensions | 157 } // namespace extensions |
| 158 |
OLD | NEW |