Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Unified Diff: mojo/common/common_custom_types_unittest.cc

Issue 2013433002: [Mojo] Add [Native] type traits for base::Values (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/common/common_custom_types_unittest.cc
diff --git a/mojo/common/common_custom_types_unittest.cc b/mojo/common/common_custom_types_unittest.cc
index 2223c26c2d0956d45ff268bfad9029b87be61397..01176be4fb0df68868114d7948a0b7fcab0f0772 100644
--- a/mojo/common/common_custom_types_unittest.cc
+++ b/mojo/common/common_custom_types_unittest.cc
@@ -2,9 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/bind.h"
#include "base/files/file_path.h"
+#include "base/json/json_writer.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
+#include "base/strings/stringprintf.h"
+#include "base/values.h"
#include "mojo/common/common_custom_types.mojom.h"
#include "mojo/common/test_common_custom_types.mojom.h"
#include "mojo/public/cpp/bindings/binding.h"
@@ -13,6 +17,7 @@
namespace mojo {
namespace common {
namespace test {
+namespace {
class TestFilePathImpl : public TestFilePath {
public:
@@ -29,6 +34,52 @@ class TestFilePathImpl : public TestFilePath {
mojo::Binding<TestFilePath> binding_;
};
+class TestValueImpl : public TestValue {
+ public:
+ explicit TestValueImpl(TestValueRequest request)
+ : binding_(this, std::move(request)) {}
+
+ // TestValue implementation:
+ void BounceDictionaryValue(
+ const base::DictionaryValue& in,
+ const BounceDictionaryValueCallback& callback) override {
+ callback.Run(in);
+ }
+ void BounceListValue(
+ const base::ListValue& in,
+ const BounceListValueCallback& callback) override {
+ callback.Run(in);
+ }
+
+ private:
+ mojo::Binding<TestValue> binding_;
+};
+
+template<typename C, typename V>
+bool IsBouncedValueEqual(const C& callback,
+ const V& value,
+ std::string* error) {
+ base::RunLoop run_loop;
+ bool is_equal = false;
+ callback.Run(value, [&run_loop, &value, &is_equal, &error](
+ const V& out) {
+ is_equal = value.Equals(&out);
+ if (!is_equal) {
+ std::string in_json;
+ base::JSONWriter::Write(value, &in_json);
+ std::string out_json;
+ base::JSONWriter::Write(out, &out_json);
+ *error = base::StringPrintf("Expected: '%s'; Found: '%s'",
+ in_json.c_str(), out_json.c_str());
+ }
+ run_loop.Quit();
+ });
+ run_loop.Run();
+ return is_equal;
+}
+
+} // namespace
+
TEST(CommonCustomTypesTest, FilePath) {
base::MessageLoop message_loop;
base::RunLoop run_loop;
@@ -47,6 +98,57 @@ TEST(CommonCustomTypesTest, FilePath) {
run_loop.Run();
}
+TEST(CommonCustomTypesTest, Value) {
+ base::MessageLoop message_loop;
yzshen1 2016/05/24 17:54:41 Please consider moving this into a test fixture cl
Devlin 2016/05/24 18:05:56 Done.
+
+ TestValuePtr ptr;
+ TestValueImpl impl(GetProxy(&ptr));
+ std::string error;
+
+ auto TestDictEqual = [&ptr, &error](const base::DictionaryValue& value) {
yzshen1 2016/05/24 17:54:41 I feel that this testing code is fairly hard to re
Devlin 2016/05/24 18:05:56 Agreed. This had a lot more value when we were te
+ return IsBouncedValueEqual(
+ base::Bind(&TestValue::BounceDictionaryValue,
+ base::Unretained(ptr.get())),
+ value, &error);
+ };
+ auto TestListEqual = [&ptr, &error](const base::ListValue& value) {
+ return IsBouncedValueEqual(
+ base::Bind(&TestValue::BounceListValue,
+ base::Unretained(ptr.get())),
+ value, &error);
+ };
+
+ base::DictionaryValue dict;
+ dict.SetBoolean("bool", false);
+ dict.SetInteger("int", 2);
+ dict.SetString("string", "some string");
+ dict.SetBoolean("nested.bool", true);
+ dict.SetInteger("nested.int", 9);
+ dict.Set("some_binary", base::WrapUnique(
+ base::BinaryValue::CreateWithCopiedBuffer("mojo", 4)));
+ {
+ std::unique_ptr<base::ListValue> dict_list(new base::ListValue());
+ dict_list->AppendString("string");
+ dict_list->AppendBoolean(true);
+ dict.Set("list", std::move(dict_list));
+ }
+ EXPECT_TRUE(TestDictEqual(dict)) << error;
+
+ base::ListValue list;
+ list.AppendString("string");
+ list.AppendDouble(42.1);
+ list.AppendBoolean(true);
+ list.Append(base::WrapUnique(
+ base::BinaryValue::CreateWithCopiedBuffer("mojo", 4)));
+ {
+ std::unique_ptr<base::DictionaryValue> list_dict(
+ new base::DictionaryValue());
+ list_dict->SetString("string", "str");
+ list.Append(std::move(list_dict));
+ }
+ EXPECT_TRUE(TestListEqual(list)) << error;
+}
+
} // namespace test
} // namespace common
} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698