Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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. | |
| 4 | |
| 5 #include "base/memory/scoped_ptr.h" | |
| 6 #include "base/values.h" | |
| 7 #include "chrome/renderer/extensions/activity_log_converter_strategy.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 #include "v8/include/v8.h" | |
| 10 | |
| 11 using content::V8ValueConverter; | |
| 12 | |
| 13 namespace extensions { | |
| 14 | |
| 15 class ActivityLogConverterStrategyTest : public testing::Test { | |
| 16 public: | |
| 17 ActivityLogConverterStrategyTest() | |
| 18 : isolate_(v8::Isolate::GetCurrent()) { | |
| 19 } | |
| 20 | |
| 21 protected: | |
| 22 virtual void SetUp() { | |
| 23 v8::HandleScope handle_scope(isolate_); | |
| 24 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | |
| 25 context_.Reset(isolate_, v8::Context::New(isolate_, NULL, global)); | |
|
not at google - send to devlin
2013/08/08 01:02:21
global not needed
pmarch
2013/08/08 03:20:18
Done.
| |
| 26 converter_.reset(V8ValueConverter::create()); | |
| 27 strategy_.reset(new ActivityLogConverterStrategy()); | |
| 28 converter_->SetFunctionAllowed(true); | |
| 29 converter_->SetStrategy(strategy_.get()); | |
| 30 } | |
| 31 | |
| 32 virtual void TearDown() { | |
| 33 context_.Dispose(); | |
| 34 } | |
| 35 | |
| 36 testing::AssertionResult VerifyNull(v8::Local<v8::Value> v8_value) { | |
| 37 v8::Handle<v8::Context> context = | |
| 38 v8::Handle<v8::Context>::New(isolate_, context_); | |
| 39 scoped_ptr<base::Value> value(converter_->FromV8Value(v8_value, context)); | |
|
not at google - send to devlin
2013/08/08 01:02:21
you should be able to pass context_ into these dir
pmarch
2013/08/08 03:20:18
Done.
| |
| 40 if (value->IsType(base::Value::TYPE_NULL)) | |
| 41 return testing::AssertionSuccess(); | |
| 42 return testing::AssertionFailure(); | |
| 43 } | |
| 44 | |
| 45 testing::AssertionResult VerifyBoolean(v8::Local<v8::Value> v8_value, | |
| 46 bool expected) { | |
| 47 bool out; | |
| 48 v8::Handle<v8::Context> context = | |
| 49 v8::Handle<v8::Context>::New(isolate_, context_); | |
| 50 scoped_ptr<base::Value> value(converter_->FromV8Value(v8_value, context)); | |
| 51 if (value->IsType(base::Value::TYPE_BOOLEAN) | |
| 52 && value->GetAsBoolean(&out) | |
| 53 && out == expected) | |
| 54 return testing::AssertionSuccess(); | |
| 55 return testing::AssertionFailure(); | |
| 56 } | |
| 57 | |
| 58 testing::AssertionResult VerifyInteger(v8::Local<v8::Value> v8_value, | |
| 59 int expected) { | |
| 60 int out; | |
| 61 v8::Handle<v8::Context> context = | |
| 62 v8::Handle<v8::Context>::New(isolate_, context_); | |
| 63 scoped_ptr<base::Value> value(converter_->FromV8Value(v8_value, context)); | |
| 64 if (value->IsType(base::Value::TYPE_INTEGER) | |
| 65 && value->GetAsInteger(&out) | |
| 66 && out == expected) | |
| 67 return testing::AssertionSuccess(); | |
| 68 return testing::AssertionFailure(); | |
| 69 } | |
| 70 | |
| 71 testing::AssertionResult VerifyDouble(v8::Local<v8::Value> v8_value, | |
| 72 double expected) { | |
| 73 double out; | |
| 74 v8::Handle<v8::Context> context = | |
| 75 v8::Handle<v8::Context>::New(isolate_, context_); | |
| 76 scoped_ptr<base::Value> value(converter_->FromV8Value(v8_value, context)); | |
| 77 if (value->IsType(base::Value::TYPE_DOUBLE) | |
| 78 && value->GetAsDouble(&out) | |
| 79 && out == expected) | |
| 80 return testing::AssertionSuccess(); | |
| 81 return testing::AssertionFailure(); | |
| 82 } | |
| 83 | |
| 84 testing::AssertionResult VerifyString(v8::Local<v8::Value> v8_value, | |
| 85 const std::string& expected) { | |
| 86 std::string out; | |
| 87 v8::Handle<v8::Context> context = | |
| 88 v8::Handle<v8::Context>::New(isolate_, context_); | |
| 89 scoped_ptr<base::Value> value(converter_->FromV8Value(v8_value, context)); | |
| 90 if (value->IsType(base::Value::TYPE_STRING) | |
| 91 && value->GetAsString(&out) | |
| 92 && out == expected) | |
| 93 return testing::AssertionSuccess(); | |
| 94 return testing::AssertionFailure(); | |
| 95 } | |
| 96 | |
| 97 v8::Isolate* isolate_; | |
| 98 // Context for the JavaScript in the test. | |
| 99 v8::Persistent<v8::Context> context_; | |
|
not at google - send to devlin
2013/08/08 01:02:21
use ScopedPersistent, don't need the Dispose
pmarch
2013/08/08 03:20:18
Done.
| |
| 100 scoped_ptr<V8ValueConverter> converter_; | |
| 101 scoped_ptr<ActivityLogConverterStrategy> strategy_; | |
| 102 }; | |
| 103 | |
| 104 TEST_F(ActivityLogConverterStrategyTest, ConversionTest) { | |
| 105 const char* source = "(function() {" | |
| 106 "function foo() {}" | |
| 107 "return {" | |
| 108 "null: null," | |
| 109 "true: true," | |
| 110 "false: false," | |
| 111 "positive_int: 42," | |
| 112 "negative_int: -42," | |
| 113 "zero: 0," | |
| 114 "double: 88.8," | |
| 115 "big_integral_double: 9007199254740992.0," // 2.0^53 | |
| 116 "string: \"foobar\"," | |
| 117 "empty_string: \"\"," | |
| 118 "dictionary: {" | |
| 119 "foo: \"bar\"," | |
| 120 "hot: \"dog\"," | |
| 121 "}," | |
| 122 "empty_dictionary: {}," | |
| 123 "list: [ \"monkey\", \"balls\" ]," | |
| 124 "empty_list: []," | |
| 125 "function: function() {}," | |
| 126 "named_function: foo" | |
| 127 "};" | |
| 128 "})();"; | |
| 129 | |
| 130 v8::HandleScope handle_scope(isolate_); | |
| 131 v8::Context::Scope context_scope(isolate_, context_); | |
| 132 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); | |
| 133 v8::Handle<v8::Object> v8_object = script->Run().As<v8::Object>(); | |
| 134 | |
| 135 EXPECT_TRUE(VerifyString(v8_object, "[Object]")); | |
| 136 EXPECT_TRUE(VerifyNull(v8_object->Get(v8::String::New("null")))); | |
| 137 EXPECT_TRUE(VerifyBoolean(v8_object->Get(v8::String::New("true")), true)); | |
| 138 EXPECT_TRUE(VerifyBoolean(v8_object->Get(v8::String::New("false")), false)); | |
| 139 EXPECT_TRUE(VerifyInteger(v8_object->Get(v8::String::New("positive_int")), | |
| 140 42)); | |
| 141 EXPECT_TRUE(VerifyInteger(v8_object->Get(v8::String::New("negative_int")), | |
| 142 -42)); | |
| 143 EXPECT_TRUE(VerifyInteger(v8_object->Get(v8::String::New("zero")), 0)); | |
| 144 EXPECT_TRUE(VerifyDouble(v8_object->Get(v8::String::New("double")), 88.8)); | |
| 145 EXPECT_TRUE(VerifyDouble( | |
| 146 v8_object->Get(v8::String::New("big_integral_double")), | |
| 147 9007199254740992.0)); | |
| 148 EXPECT_TRUE(VerifyString(v8_object->Get(v8::String::New("string")), | |
| 149 "foobar")); | |
| 150 EXPECT_TRUE(VerifyString(v8_object->Get(v8::String::New("empty_string")), | |
| 151 "")); | |
| 152 EXPECT_TRUE(VerifyString(v8_object->Get(v8::String::New("dictionary")), | |
| 153 "[Object]")); | |
| 154 EXPECT_TRUE(VerifyString(v8_object->Get(v8::String::New("empty_dictionary")), | |
| 155 "[Object]")); | |
| 156 EXPECT_TRUE(VerifyString(v8_object->Get(v8::String::New("list")), | |
| 157 "[Array]")); | |
| 158 EXPECT_TRUE(VerifyString(v8_object->Get(v8::String::New("empty_list")), | |
| 159 "[Array]")); | |
| 160 EXPECT_TRUE(VerifyString(v8_object->Get(v8::String::New("function")), | |
| 161 "[Function]")); | |
| 162 EXPECT_TRUE(VerifyString(v8_object->Get(v8::String::New("named_function")), | |
| 163 "[Function foo()]")); | |
| 164 } | |
| 165 | |
| 166 } // namespace extensions | |
| 167 | |
| OLD | NEW |