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)); |
| 26 } |
| 27 |
| 28 virtual void TearDown() { |
| 29 context_.Dispose(); |
| 30 } |
| 31 |
| 32 v8::Isolate* isolate_; |
| 33 |
| 34 // Context for the JavaScript in the test. |
| 35 v8::Persistent<v8::Context> context_; |
| 36 }; |
| 37 |
| 38 TEST_F(ActivityLogConverterStrategyTest, ConversionTest) { |
| 39 const char* source = "(function() {" |
| 40 "function foo() {}" |
| 41 "return {" |
| 42 "null: null," |
| 43 "true: true," |
| 44 "false: false," |
| 45 "positive_int: 42," |
| 46 "negative_int: -42," |
| 47 "zero: 0," |
| 48 "double: 88.8," |
| 49 "big_integral_double: 9007199254740992.0," // 2.0^53 |
| 50 "string: \"foobar\"," |
| 51 "empty_string: \"\"," |
| 52 "dictionary: {" |
| 53 "foo: \"bar\"," |
| 54 "hot: \"dog\"," |
| 55 "}," |
| 56 "empty_dictionary: {}," |
| 57 "list: [ \"monkey\", \"balls\" ]," |
| 58 "empty_list: []," |
| 59 "function: function() {}," |
| 60 "named_function: foo" |
| 61 "};" |
| 62 "})();"; |
| 63 |
| 64 v8::HandleScope handle_scope(isolate_); |
| 65 v8::Context::Scope context_scope(isolate_, context_); |
| 66 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); |
| 67 v8::Handle<v8::Object> v8_object = script->Run().As<v8::Object>(); |
| 68 ASSERT_FALSE(v8_object.IsEmpty()); |
| 69 |
| 70 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 71 ActivityLogConverterStrategy strategy; |
| 72 converter->SetStrategy(&strategy); |
| 73 |
| 74 bool out_boolean; |
| 75 int out_integer; |
| 76 double out_double; |
| 77 std::string out_string; |
| 78 |
| 79 v8::Handle<v8::Context> context = |
| 80 v8::Handle<v8::Context>::New(isolate_, context_); |
| 81 |
| 82 scoped_ptr<base::Value> value(converter->FromV8Value(v8_object, context)); |
| 83 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 84 && value->GetAsString(&out_string) |
| 85 && out_string == std::string("[Object]")); |
| 86 value.reset(converter->FromV8Value( |
| 87 v8_object->Get(v8::String::New("null")), context)); |
| 88 ASSERT_TRUE(value->IsType(base::Value::TYPE_NULL)); |
| 89 value.reset(converter->FromV8Value( |
| 90 v8_object->Get(v8::String::New("true")), context)); |
| 91 ASSERT_TRUE(value->IsType(base::Value::TYPE_BOOLEAN) |
| 92 && value->GetAsBoolean(&out_boolean) |
| 93 && out_boolean == true); |
| 94 value.reset(converter->FromV8Value( |
| 95 v8_object->Get(v8::String::New("false")), context)); |
| 96 ASSERT_TRUE(value->IsType(base::Value::TYPE_BOOLEAN) |
| 97 && value->GetAsBoolean(&out_boolean) |
| 98 && out_boolean == false); |
| 99 value.reset(converter->FromV8Value( |
| 100 v8_object->Get(v8::String::New("positive_int")), context)); |
| 101 ASSERT_TRUE(value->IsType(base::Value::TYPE_INTEGER) |
| 102 && value->GetAsInteger(&out_integer) |
| 103 && out_integer == 42); |
| 104 value.reset(converter->FromV8Value( |
| 105 v8_object->Get(v8::String::New("negative_int")), context)); |
| 106 ASSERT_TRUE(value->IsType(base::Value::TYPE_INTEGER) |
| 107 && value->GetAsInteger(&out_integer) |
| 108 && out_integer == -42); |
| 109 value.reset(converter->FromV8Value( |
| 110 v8_object->Get(v8::String::New("zero")), context)); |
| 111 ASSERT_TRUE(value->IsType(base::Value::TYPE_INTEGER) |
| 112 && value->GetAsInteger(&out_integer) |
| 113 && out_integer == 0); |
| 114 value.reset(converter->FromV8Value( |
| 115 v8_object->Get(v8::String::New("double")), context)); |
| 116 ASSERT_TRUE(value->IsType(base::Value::TYPE_DOUBLE) |
| 117 && value->GetAsDouble(&out_double) |
| 118 && out_double == 88.8); |
| 119 value.reset(converter->FromV8Value( |
| 120 v8_object->Get(v8::String::New("big_integral_double")), context)); |
| 121 ASSERT_TRUE(value->IsType(base::Value::TYPE_DOUBLE) |
| 122 && value->GetAsDouble(&out_double) |
| 123 && out_double == 9007199254740992.0); |
| 124 value.reset(converter->FromV8Value( |
| 125 v8_object->Get(v8::String::New("string")), context)); |
| 126 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 127 && value->GetAsString(&out_string) |
| 128 && out_string == std::string("foobar")); |
| 129 value.reset(converter->FromV8Value( |
| 130 v8_object->Get(v8::String::New("empty_string")), context)); |
| 131 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 132 && value->GetAsString(&out_string) |
| 133 && out_string == std::string()); |
| 134 value.reset(converter->FromV8Value( |
| 135 v8_object->Get(v8::String::New("dictionary")), context)); |
| 136 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 137 && value->GetAsString(&out_string) |
| 138 && out_string == std::string("[Object]")); |
| 139 value.reset(converter->FromV8Value( |
| 140 v8_object->Get(v8::String::New("empty_dictionary")), context)); |
| 141 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 142 && value->GetAsString(&out_string) |
| 143 && out_string == std::string("[Object]")); |
| 144 value.reset(converter->FromV8Value( |
| 145 v8_object->Get(v8::String::New("list")), context)); |
| 146 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 147 && value->GetAsString(&out_string) |
| 148 && out_string == std::string("[Array]")); |
| 149 value.reset(converter->FromV8Value( |
| 150 v8_object->Get(v8::String::New("empty_list")), context)); |
| 151 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 152 && value->GetAsString(&out_string) |
| 153 && out_string == std::string("[Array]")); |
| 154 value.reset(converter->FromV8Value( |
| 155 v8_object->Get(v8::String::New("function")), context)); |
| 156 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 157 && value->GetAsString(&out_string) |
| 158 && out_string == std::string("[Function]")); |
| 159 value.reset(converter->FromV8Value( |
| 160 v8_object->Get(v8::String::New("named_function")), context)); |
| 161 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 162 && value->GetAsString(&out_string) |
| 163 && out_string == std::string("[Function foo()]")); |
| 164 } |
| 165 |
| 166 } // namespace extensions |
| 167 |
OLD | NEW |