OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/values.h" |
8 #include "mojo/common/common_custom_types.mojom.h" | 9 #include "mojo/common/common_custom_types.mojom.h" |
9 #include "mojo/common/test_common_custom_types.mojom.h" | 10 #include "mojo/common/test_common_custom_types.mojom.h" |
10 #include "mojo/public/cpp/bindings/binding.h" | 11 #include "mojo/public/cpp/bindings/binding.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 namespace mojo { | 14 namespace mojo { |
14 namespace common { | 15 namespace common { |
15 namespace test { | 16 namespace test { |
| 17 namespace { |
16 | 18 |
17 class TestFilePathImpl : public TestFilePath { | 19 class TestFilePathImpl : public TestFilePath { |
18 public: | 20 public: |
19 explicit TestFilePathImpl(TestFilePathRequest request) | 21 explicit TestFilePathImpl(TestFilePathRequest request) |
20 : binding_(this, std::move(request)) {} | 22 : binding_(this, std::move(request)) {} |
21 | 23 |
22 // TestFilePath implementation: | 24 // TestFilePath implementation: |
23 void BounceFilePath(const base::FilePath& in, | 25 void BounceFilePath(const base::FilePath& in, |
24 const BounceFilePathCallback& callback) override { | 26 const BounceFilePathCallback& callback) override { |
25 callback.Run(in); | 27 callback.Run(in); |
26 } | 28 } |
27 | 29 |
28 private: | 30 private: |
29 mojo::Binding<TestFilePath> binding_; | 31 mojo::Binding<TestFilePath> binding_; |
30 }; | 32 }; |
31 | 33 |
32 TEST(CommonCustomTypesTest, FilePath) { | 34 class TestValueImpl : public TestValue { |
33 base::MessageLoop message_loop; | 35 public: |
| 36 explicit TestValueImpl(TestValueRequest request) |
| 37 : binding_(this, std::move(request)) {} |
| 38 |
| 39 // TestValue implementation: |
| 40 void BounceDictionaryValue( |
| 41 const base::DictionaryValue& in, |
| 42 const BounceDictionaryValueCallback& callback) override { |
| 43 callback.Run(in); |
| 44 } |
| 45 void BounceListValue(const base::ListValue& in, |
| 46 const BounceListValueCallback& callback) override { |
| 47 callback.Run(in); |
| 48 } |
| 49 |
| 50 private: |
| 51 mojo::Binding<TestValue> binding_; |
| 52 }; |
| 53 |
| 54 class CommonCustomTypesTest : public testing::Test { |
| 55 protected: |
| 56 CommonCustomTypesTest() {} |
| 57 ~CommonCustomTypesTest() override {} |
| 58 |
| 59 private: |
| 60 base::MessageLoop message_loop_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(CommonCustomTypesTest); |
| 63 }; |
| 64 |
| 65 } // namespace |
| 66 |
| 67 TEST_F(CommonCustomTypesTest, FilePath) { |
34 base::RunLoop run_loop; | 68 base::RunLoop run_loop; |
35 | 69 |
36 TestFilePathPtr ptr; | 70 TestFilePathPtr ptr; |
37 TestFilePathImpl impl(GetProxy(&ptr)); | 71 TestFilePathImpl impl(GetProxy(&ptr)); |
38 | 72 |
39 base::FilePath dir(FILE_PATH_LITERAL("hello")); | 73 base::FilePath dir(FILE_PATH_LITERAL("hello")); |
40 base::FilePath file = dir.Append(FILE_PATH_LITERAL("world")); | 74 base::FilePath file = dir.Append(FILE_PATH_LITERAL("world")); |
41 | 75 |
42 ptr->BounceFilePath(file, [&run_loop, &file](const base::FilePath& out) { | 76 ptr->BounceFilePath(file, [&run_loop, &file](const base::FilePath& out) { |
43 EXPECT_EQ(file, out); | 77 EXPECT_EQ(file, out); |
44 run_loop.Quit(); | 78 run_loop.Quit(); |
45 }); | 79 }); |
46 | 80 |
47 run_loop.Run(); | 81 run_loop.Run(); |
48 } | 82 } |
49 | 83 |
| 84 TEST_F(CommonCustomTypesTest, Value) { |
| 85 TestValuePtr ptr; |
| 86 TestValueImpl impl(GetProxy(&ptr)); |
| 87 |
| 88 base::DictionaryValue dict; |
| 89 dict.SetBoolean("bool", false); |
| 90 dict.SetInteger("int", 2); |
| 91 dict.SetString("string", "some string"); |
| 92 dict.SetBoolean("nested.bool", true); |
| 93 dict.SetInteger("nested.int", 9); |
| 94 dict.Set( |
| 95 "some_binary", |
| 96 base::WrapUnique(base::BinaryValue::CreateWithCopiedBuffer("mojo", 4))); |
| 97 { |
| 98 std::unique_ptr<base::ListValue> dict_list(new base::ListValue()); |
| 99 dict_list->AppendString("string"); |
| 100 dict_list->AppendBoolean(true); |
| 101 dict.Set("list", std::move(dict_list)); |
| 102 } |
| 103 { |
| 104 base::RunLoop run_loop; |
| 105 ptr->BounceDictionaryValue( |
| 106 dict, [&run_loop, &dict](const base::DictionaryValue& out) { |
| 107 EXPECT_TRUE(dict.Equals(&out)); |
| 108 run_loop.Quit(); |
| 109 }); |
| 110 run_loop.Run(); |
| 111 } |
| 112 |
| 113 base::ListValue list; |
| 114 list.AppendString("string"); |
| 115 list.AppendDouble(42.1); |
| 116 list.AppendBoolean(true); |
| 117 list.Append( |
| 118 base::WrapUnique(base::BinaryValue::CreateWithCopiedBuffer("mojo", 4))); |
| 119 { |
| 120 std::unique_ptr<base::DictionaryValue> list_dict( |
| 121 new base::DictionaryValue()); |
| 122 list_dict->SetString("string", "str"); |
| 123 list.Append(std::move(list_dict)); |
| 124 } |
| 125 { |
| 126 base::RunLoop run_loop; |
| 127 ptr->BounceListValue(list, [&run_loop, &list](const base::ListValue& out) { |
| 128 EXPECT_TRUE(list.Equals(&out)); |
| 129 run_loop.Quit(); |
| 130 }); |
| 131 run_loop.Run(); |
| 132 } |
| 133 } |
| 134 |
50 } // namespace test | 135 } // namespace test |
51 } // namespace common | 136 } // namespace common |
52 } // namespace mojo | 137 } // namespace mojo |
OLD | NEW |