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_value_converter.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "v8/include/v8.h" |
| 10 |
| 11 namespace extensions { |
| 12 |
| 13 class ActivityLogValueConverterTest : public testing::Test { |
| 14 public: |
| 15 ActivityLogValueConverterTest() |
| 16 : isolate_(v8::Isolate::GetCurrent()) { |
| 17 } |
| 18 |
| 19 protected: |
| 20 virtual void SetUp() { |
| 21 v8::HandleScope handle_scope(isolate_); |
| 22 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| 23 context_.Reset(isolate_, v8::Context::New(isolate_, NULL, global)); |
| 24 } |
| 25 |
| 26 virtual void TearDown() { |
| 27 context_.Dispose(); |
| 28 } |
| 29 |
| 30 v8::Isolate* isolate_; |
| 31 |
| 32 // Context for the JavaScript in the test. |
| 33 v8::Persistent<v8::Context> context_; |
| 34 }; |
| 35 |
| 36 TEST_F(ActivityLogValueConverterTest, ConversionTest) { |
| 37 const char* source = "(function() {" |
| 38 "function foo() {}" |
| 39 "return {" |
| 40 "null: null," |
| 41 "true: true," |
| 42 "false: false," |
| 43 "positive_int: 42," |
| 44 "negative_int: -42," |
| 45 "zero: 0," |
| 46 "double: 88.8," |
| 47 "big_integral_double: 9007199254740992.0," // 2.0^53 |
| 48 "string: \"foobar\"," |
| 49 "empty_string: \"\"," |
| 50 "dictionary: {" |
| 51 "foo: \"bar\"," |
| 52 "hot: \"dog\"," |
| 53 "}," |
| 54 "empty_dictionary: {}," |
| 55 "list: [ \"monkey\", \"balls\" ]," |
| 56 "empty_list: []," |
| 57 "function: function() {}," |
| 58 "named_function: foo" |
| 59 "};" |
| 60 "})();"; |
| 61 |
| 62 v8::HandleScope handle_scope(isolate_); |
| 63 v8::Context::Scope context_scope(isolate_, context_); |
| 64 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); |
| 65 v8::Handle<v8::Object> v8_object = script->Run().As<v8::Object>(); |
| 66 ASSERT_FALSE(v8_object.IsEmpty()); |
| 67 |
| 68 ActivityLogValueConverter converter; |
| 69 bool out_boolean; |
| 70 int out_integer; |
| 71 double out_double; |
| 72 std::string out_string; |
| 73 |
| 74 v8::Handle<v8::Context> context = |
| 75 v8::Handle<v8::Context>::New(isolate_, context_); |
| 76 |
| 77 scoped_ptr<base::Value> value(converter.FromV8Value(v8_object, context)); |
| 78 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 79 && value->GetAsString(&out_string) |
| 80 && out_string == std::string("[Object]")); |
| 81 value.reset(converter.FromV8Value( |
| 82 v8_object->Get(v8::String::New("null")), context)); |
| 83 ASSERT_TRUE(value->IsType(base::Value::TYPE_NULL)); |
| 84 value.reset(converter.FromV8Value( |
| 85 v8_object->Get(v8::String::New("true")), context)); |
| 86 ASSERT_TRUE(value->IsType(base::Value::TYPE_BOOLEAN) |
| 87 && value->GetAsBoolean(&out_boolean) |
| 88 && out_boolean == true); |
| 89 value.reset(converter.FromV8Value( |
| 90 v8_object->Get(v8::String::New("false")), context)); |
| 91 ASSERT_TRUE(value->IsType(base::Value::TYPE_BOOLEAN) |
| 92 && value->GetAsBoolean(&out_boolean) |
| 93 && out_boolean == false); |
| 94 value.reset(converter.FromV8Value( |
| 95 v8_object->Get(v8::String::New("positive_int")), context)); |
| 96 ASSERT_TRUE(value->IsType(base::Value::TYPE_INTEGER) |
| 97 && value->GetAsInteger(&out_integer) |
| 98 && out_integer == 42); |
| 99 value.reset(converter.FromV8Value( |
| 100 v8_object->Get(v8::String::New("negative_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("zero")), context)); |
| 106 ASSERT_TRUE(value->IsType(base::Value::TYPE_INTEGER) |
| 107 && value->GetAsInteger(&out_integer) |
| 108 && out_integer == 0); |
| 109 value.reset(converter.FromV8Value( |
| 110 v8_object->Get(v8::String::New("double")), context)); |
| 111 ASSERT_TRUE(value->IsType(base::Value::TYPE_DOUBLE) |
| 112 && value->GetAsDouble(&out_double) |
| 113 && out_double == 88.8); |
| 114 value.reset(converter.FromV8Value( |
| 115 v8_object->Get(v8::String::New("big_integral_double")), context)); |
| 116 ASSERT_TRUE(value->IsType(base::Value::TYPE_DOUBLE) |
| 117 && value->GetAsDouble(&out_double) |
| 118 && out_double == 9007199254740992.0); |
| 119 value.reset(converter.FromV8Value( |
| 120 v8_object->Get(v8::String::New("string")), context)); |
| 121 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 122 && value->GetAsString(&out_string) |
| 123 && out_string == std::string("foobar")); |
| 124 value.reset(converter.FromV8Value( |
| 125 v8_object->Get(v8::String::New("empty_string")), context)); |
| 126 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 127 && value->GetAsString(&out_string) |
| 128 && out_string == std::string()); |
| 129 value.reset(converter.FromV8Value( |
| 130 v8_object->Get(v8::String::New("dictionary")), context)); |
| 131 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 132 && value->GetAsString(&out_string) |
| 133 && out_string == std::string("[Object]")); |
| 134 value.reset(converter.FromV8Value( |
| 135 v8_object->Get(v8::String::New("empty_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("list")), context)); |
| 141 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 142 && value->GetAsString(&out_string) |
| 143 && out_string == std::string("[Array]")); |
| 144 value.reset(converter.FromV8Value( |
| 145 v8_object->Get(v8::String::New("empty_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("function")), context)); |
| 151 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 152 && value->GetAsString(&out_string) |
| 153 && out_string == std::string("[Function]")); |
| 154 value.reset(converter.FromV8Value( |
| 155 v8_object->Get(v8::String::New("named_function")), context)); |
| 156 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| 157 && value->GetAsString(&out_string) |
| 158 && out_string == std::string("[Function foo()]")); |
| 159 } |
| 160 |
| 161 } // namespace extensions |
| 162 |
OLD | NEW |