Chromium Code Reviews| Index: chrome/renderer/extensions/activity_log_converter_strategy_unittest.cc |
| diff --git a/chrome/renderer/extensions/activity_log_converter_strategy_unittest.cc b/chrome/renderer/extensions/activity_log_converter_strategy_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4772cc12ad02cef57c22e7af700c467c0060c7e7 |
| --- /dev/null |
| +++ b/chrome/renderer/extensions/activity_log_converter_strategy_unittest.cc |
| @@ -0,0 +1,168 @@ |
| +// 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_converter_strategy.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "v8/include/v8.h" |
| + |
| +using content::V8ValueConverter; |
| + |
| +namespace extensions { |
| + |
| +class ActivityLogConverterStrategyTest : public testing::Test { |
| + public: |
| + ActivityLogConverterStrategyTest() |
| + : 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(ActivityLogConverterStrategyTest, 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\" ]," |
| + "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()); |
| + |
| + scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| + ActivityLogConverterStrategy strategy; |
| + converter->SetFunctionAllowed(true); |
| + converter->SetStrategy(&strategy); |
| + |
| + 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()]")); |
|
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.
|
| +} |
| + |
| +} // namespace extensions |
| + |