Chromium Code Reviews| Index: chrome/renderer/extensions/activity_log_value_converter_unittest.cc |
| diff --git a/chrome/renderer/extensions/activity_log_value_converter_unittest.cc b/chrome/renderer/extensions/activity_log_value_converter_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f096fce70f365df3bf8c7cfa1166080d1eda76bd |
| --- /dev/null |
| +++ b/chrome/renderer/extensions/activity_log_value_converter_unittest.cc |
| @@ -0,0 +1,161 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/values.h" |
| +#include "chrome/renderer/extensions/activity_log_value_converter.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "v8/include/v8.h" |
| + |
| +namespace extensions { |
| + |
| +class ActivityLogValueConverterTest : public testing::Test { |
| + public: |
| + ActivityLogValueConverterTest() |
| + : isolate_(v8::Isolate::GetCurrent()) { |
| + } |
| + |
| + protected: |
| + virtual void SetUp() { |
| + v8::HandleScope handle_scope(isolate_); |
| + v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| + context_.Reset(isolate_, v8::Context::New(isolate_, NULL, global)); |
| + } |
| + |
| + virtual void TearDown() { |
| + context_.Dispose(); |
| + } |
| + |
| + v8::Isolate* isolate_; |
| + |
| + // Context for the JavaScript in the test. |
| + v8::Persistent<v8::Context> context_; |
| +}; |
| + |
| +TEST_F(ActivityLogValueConverterTest, ConversionTest) { |
| + const char* source = "(function() {" |
| + "function foo() {}" |
| + "return {" |
| + "null: null," |
| + "true: true," |
| + "false: false," |
| + "positive_int: 42," |
| + "negative_int: -42," |
| + "zero: 0," |
| + "double: 88.8," |
| + "big_integral_double: 9007199254740992.0," // 2.0^53 |
| + "string: \"foobar\"," |
| + "empty_string: \"\"," |
| + "dictionary: {" |
| + "foo: \"bar\"," |
| + "hot: \"dog\"," |
| + "}," |
| + "empty_dictionary: {}," |
| + "list: [ \"monkey\", \"balls\" ]," |
|
felt
2013/07/18 17:28:20
hahahaha interesting choice of words for your test
pmarch
2013/07/18 20:42:03
hahaha, I copied it over from another test and did
|
| + "empty_list: []," |
| + "function: function() {}," |
| + "named_function: foo" |
| + "};" |
| + "})();"; |
| + |
| + v8::HandleScope handle_scope(isolate_); |
| + v8::Context::Scope context_scope(isolate_, context_); |
| + v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); |
| + v8::Handle<v8::Object> v8_object = script->Run().As<v8::Object>(); |
| + ASSERT_FALSE(v8_object.IsEmpty()); |
| + |
| + ActivityLogValueConverter converter; |
| + bool out_boolean; |
| + int out_integer; |
| + double out_double; |
| + std::string out_string; |
| + |
| + v8::Handle<v8::Context> context = |
| + v8::Handle<v8::Context>::New(isolate_, context_); |
| + |
| + scoped_ptr<base::Value> value(converter.FromV8Value(v8_object, context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| + && value->GetAsString(&out_string) |
| + && out_string == std::string("[Object]")); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("null")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_NULL)); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("true")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_BOOLEAN) |
| + && value->GetAsBoolean(&out_boolean) |
| + && out_boolean == true); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("false")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_BOOLEAN) |
| + && value->GetAsBoolean(&out_boolean) |
| + && out_boolean == false); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("positive_int")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_INTEGER) |
| + && value->GetAsInteger(&out_integer) |
| + && out_integer == 42); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("negative_int")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_INTEGER) |
| + && value->GetAsInteger(&out_integer) |
| + && out_integer == -42); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("zero")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_INTEGER) |
| + && value->GetAsInteger(&out_integer) |
| + && out_integer == 0); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("double")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_DOUBLE) |
| + && value->GetAsDouble(&out_double) |
| + && out_double == 88.8); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("big_integral_double")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_DOUBLE) |
| + && value->GetAsDouble(&out_double) |
| + && out_double == 9007199254740992.0); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("string")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| + && value->GetAsString(&out_string) |
| + && out_string == std::string("foobar")); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("empty_string")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| + && value->GetAsString(&out_string) |
| + && out_string == std::string()); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("dictionary")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| + && value->GetAsString(&out_string) |
| + && out_string == std::string("[Object]")); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("empty_dictionary")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| + && value->GetAsString(&out_string) |
| + && out_string == std::string("[Object]")); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("list")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| + && value->GetAsString(&out_string) |
| + && out_string == std::string("[Array]")); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("empty_list")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| + && value->GetAsString(&out_string) |
| + && out_string == std::string("[Array]")); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("function")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| + && value->GetAsString(&out_string) |
| + && out_string == std::string("[Function]")); |
| + value.reset(converter.FromV8Value( |
| + v8_object->Get(v8::String::New("named_function")), context)); |
| + ASSERT_TRUE(value->IsType(base::Value::TYPE_STRING) |
| + && value->GetAsString(&out_string) |
| + && out_string == std::string("[Function foo()]")); |
| +} |
| + |
| +} // namespace extensions |
|
felt
2013/07/18 17:28:20
linter is complaining about lack of a new line at
pmarch
2013/07/18 20:42:03
Done.
|