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)); | |
| 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->SetFunctionAllowed(true); | |
| 73 converter->SetStrategy(&strategy); | |
| 74 | |
| 75 bool out_boolean; | |
| 76 int out_integer; | |
| 77 double out_double; | |
| 78 std::string out_string; | |
| 79 | |
| 80 v8::Handle<v8::Context> context = | |
| 81 v8::Handle<v8::Context>::New(isolate_, context_); | |
| 82 | |
| 83 scoped_ptr<base::Value> value(converter->FromV8Value(v8_object, context)); | |
| 84 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) | |
| 85 && value->GetAsString(&out_string) | |
| 86 && out_string == std::string("[Object]")); | |
| 87 value.reset(converter->FromV8Value( | |
| 88 v8_object->Get(v8::String::New("null")), context)); | |
| 89 ASSERT_TRUE(value->IsType(base::Value::TYPE_NULL)); | |
| 90 value.reset(converter->FromV8Value( | |
| 91 v8_object->Get(v8::String::New("true")), context)); | |
| 92 ASSERT_TRUE(value->IsType(base::Value::TYPE_BOOLEAN) | |
| 93 && value->GetAsBoolean(&out_boolean) | |
| 94 && out_boolean == true); | |
| 95 value.reset(converter->FromV8Value( | |
| 96 v8_object->Get(v8::String::New("false")), context)); | |
| 97 ASSERT_TRUE(value->IsType(base::Value::TYPE_BOOLEAN) | |
| 98 && value->GetAsBoolean(&out_boolean) | |
| 99 && out_boolean == false); | |
| 100 value.reset(converter->FromV8Value( | |
| 101 v8_object->Get(v8::String::New("positive_int")), context)); | |
| 102 ASSERT_TRUE(value->IsType(base::Value::TYPE_INTEGER) | |
| 103 && value->GetAsInteger(&out_integer) | |
| 104 && out_integer == 42); | |
| 105 value.reset(converter->FromV8Value( | |
| 106 v8_object->Get(v8::String::New("negative_int")), context)); | |
| 107 ASSERT_TRUE(value->IsType(base::Value::TYPE_INTEGER) | |
| 108 && value->GetAsInteger(&out_integer) | |
| 109 && out_integer == -42); | |
| 110 value.reset(converter->FromV8Value( | |
| 111 v8_object->Get(v8::String::New("zero")), context)); | |
| 112 ASSERT_TRUE(value->IsType(base::Value::TYPE_INTEGER) | |
| 113 && value->GetAsInteger(&out_integer) | |
| 114 && out_integer == 0); | |
| 115 value.reset(converter->FromV8Value( | |
| 116 v8_object->Get(v8::String::New("double")), context)); | |
| 117 ASSERT_TRUE(value->IsType(base::Value::TYPE_DOUBLE) | |
| 118 && value->GetAsDouble(&out_double) | |
| 119 && out_double == 88.8); | |
| 120 value.reset(converter->FromV8Value( | |
| 121 v8_object->Get(v8::String::New("big_integral_double")), context)); | |
| 122 ASSERT_TRUE(value->IsType(base::Value::TYPE_DOUBLE) | |
| 123 && value->GetAsDouble(&out_double) | |
| 124 && out_double == 9007199254740992.0); | |
| 125 value.reset(converter->FromV8Value( | |
| 126 v8_object->Get(v8::String::New("string")), context)); | |
| 127 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) | |
| 128 && value->GetAsString(&out_string) | |
| 129 && out_string == std::string("foobar")); | |
| 130 value.reset(converter->FromV8Value( | |
| 131 v8_object->Get(v8::String::New("empty_string")), context)); | |
| 132 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) | |
| 133 && value->GetAsString(&out_string) | |
| 134 && out_string == std::string()); | |
| 135 value.reset(converter->FromV8Value( | |
| 136 v8_object->Get(v8::String::New("dictionary")), context)); | |
| 137 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) | |
| 138 && value->GetAsString(&out_string) | |
| 139 && out_string == std::string("[Object]")); | |
| 140 value.reset(converter->FromV8Value( | |
| 141 v8_object->Get(v8::String::New("empty_dictionary")), context)); | |
| 142 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) | |
| 143 && value->GetAsString(&out_string) | |
| 144 && out_string == std::string("[Object]")); | |
| 145 value.reset(converter->FromV8Value( | |
| 146 v8_object->Get(v8::String::New("list")), context)); | |
| 147 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) | |
| 148 && value->GetAsString(&out_string) | |
| 149 && out_string == std::string("[Array]")); | |
| 150 value.reset(converter->FromV8Value( | |
| 151 v8_object->Get(v8::String::New("empty_list")), context)); | |
| 152 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) | |
| 153 && value->GetAsString(&out_string) | |
| 154 && out_string == std::string("[Array]")); | |
| 155 value.reset(converter->FromV8Value( | |
| 156 v8_object->Get(v8::String::New("function")), context)); | |
| 157 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) | |
| 158 && value->GetAsString(&out_string) | |
| 159 && out_string == std::string("[Function]")); | |
| 160 value.reset(converter->FromV8Value( | |
| 161 v8_object->Get(v8::String::New("named_function")), context)); | |
| 162 ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) | |
| 163 && value->GetAsString(&out_string) | |
| 164 && out_string == std::string("[Function foo()]")); | |
|
not at google - send to devlin
2013/08/07 22:07:56
These can all be done more concisely if you pull t
pmarch
2013/08/08 00:53:07
Done.
| |
| 165 } | |
| 166 | |
| 167 } // namespace extensions | |
| 168 | |
| OLD | NEW |