OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 // TODO(nona): Add more test case, especially fail case. | |
5 | |
6 #include "chromeos/dbus/ibus/ibus_object.h" | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 #include "base/logging.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/values.h" | |
13 #include "dbus/message.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace chromeos { | |
17 | |
18 TEST(IBusObjectTest, WriteReadTest) { | |
19 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); | |
20 dbus::MessageWriter writer(message.get()); | |
21 | |
22 const char kSampleTypeName1[] = "Sample Type Name 1"; | |
23 const char kSampleTypeName2[] = "Sample Type Name 2"; | |
24 const char kSampleText1[] = "Sample Text 1"; | |
25 const char kSampleText2[] = "Sample Text 2"; | |
26 const uint32 kSampleUint32 = 12345UL; | |
27 const int32 kSampleInt32 = 54321; | |
28 const bool kSampleBool = false; | |
29 const uint32 kSampleArrayOfUint32Count = 10UL; | |
30 const char kAttachmentKey[] = "key"; | |
31 const char kStringAttachment[] = "Attachment String Value"; | |
32 | |
33 // Create ibus object. | |
34 IBusObjectWriter ibus_object_writer(kSampleTypeName1, "suibauv", &writer); | |
35 | |
36 // Write attachment field. | |
37 scoped_ptr<base::Value> write_value( | |
38 base::Value::CreateStringValue(kStringAttachment)); | |
39 ibus_object_writer.AddAttachment(kAttachmentKey, *write_value.get()); | |
40 ibus_object_writer.CloseHeader(); | |
41 | |
42 // Write contents values. | |
43 ibus_object_writer.AppendString(kSampleText1); | |
44 ibus_object_writer.AppendUint32(kSampleUint32); | |
45 ibus_object_writer.AppendInt32(kSampleInt32); | |
46 ibus_object_writer.AppendBool(kSampleBool); | |
47 dbus::MessageWriter array_writer(NULL); | |
48 ibus_object_writer.OpenArray("u", &array_writer); | |
49 for (uint32 i = 0; i < kSampleArrayOfUint32Count; ++i) | |
50 array_writer.AppendUint32(i); | |
51 ibus_object_writer.CloseContainer(&array_writer); | |
52 IBusObjectWriter ibus_nested_object_writer(kSampleTypeName2, "s", NULL); | |
53 ibus_object_writer.AppendIBusObject(&ibus_nested_object_writer); | |
54 ibus_nested_object_writer.CloseHeader(); | |
55 ibus_nested_object_writer.AppendString(kSampleText2); | |
56 ibus_object_writer.CloseAll(); | |
57 | |
58 // Read ibus_object. | |
59 dbus::MessageReader reader(message.get()); | |
60 IBusObjectReader ibus_object_reader(kSampleTypeName1, &reader); | |
61 ASSERT_TRUE(ibus_object_reader.Init()); | |
62 // Check the attachment value; | |
63 const base::Value* read_value = | |
64 ibus_object_reader.GetAttachment(kAttachmentKey); | |
65 ASSERT_TRUE(read_value); | |
66 std::string attachment_value; | |
67 ASSERT_TRUE(read_value->GetAsString(&attachment_value)); | |
68 EXPECT_EQ(kStringAttachment, attachment_value); | |
69 // Check the first string value. | |
70 std::string expected_string; | |
71 ASSERT_TRUE(ibus_object_reader.PopString(&expected_string)); | |
72 EXPECT_EQ(kSampleText1, expected_string); | |
73 // Check the second uint32 value. | |
74 uint32 expected_uint32 = 0UL; | |
75 ASSERT_TRUE(ibus_object_reader.PopUint32(&expected_uint32)); | |
76 EXPECT_EQ(kSampleUint32, expected_uint32); | |
77 // Check the third int value. | |
78 int32 expected_int32 = 0; | |
79 ASSERT_TRUE(ibus_object_reader.PopInt32(&expected_int32)); | |
80 EXPECT_EQ(kSampleInt32, expected_int32); | |
81 // Check the fourth boolean value. | |
82 bool expected_bool = true; | |
83 ASSERT_TRUE(ibus_object_reader.PopBool(&expected_bool)); | |
84 EXPECT_TRUE(kSampleBool == expected_bool); | |
85 // Check the fifth value which is array of uint32. | |
86 dbus::MessageReader array_reader(NULL); | |
87 ASSERT_TRUE(ibus_object_reader.PopArray(&array_reader)); | |
88 for (uint32 i = 0; i < kSampleArrayOfUint32Count; ++i) { | |
89 uint32 expected_uint32 = 0; | |
90 ASSERT_TRUE(array_reader.PopUint32(&expected_uint32)); | |
91 EXPECT_EQ(i, expected_uint32); | |
92 } | |
93 // Check the sixth value which is IBusObject. | |
94 IBusObjectReader ibus_nested_object_reader(kSampleTypeName2, NULL); | |
95 ibus_object_reader.PopIBusObject(&ibus_nested_object_reader); | |
96 std::string expected_text2; | |
97 ASSERT_TRUE(ibus_nested_object_reader.PopString(&expected_text2)); | |
98 EXPECT_EQ(kSampleText2, expected_text2); | |
99 | |
100 EXPECT_FALSE(ibus_nested_object_reader.HasMoreData()); | |
101 EXPECT_FALSE(ibus_object_reader.HasMoreData()); | |
102 EXPECT_FALSE(array_reader.HasMoreData()); | |
103 EXPECT_FALSE(reader.HasMoreData()); | |
104 } | |
105 | |
106 TEST(IBusObjectTest, EmptyEntryTest) { | |
107 const char kSampleTypeName[] = "Empty IBusObject Name"; | |
108 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); | |
109 | |
110 // Write empty IBusObject. | |
111 dbus::MessageWriter writer(message.get()); | |
112 IBusObjectWriter ibus_object_writer(kSampleTypeName, "", &writer); | |
113 ibus_object_writer.CloseHeader(); | |
114 ibus_object_writer.CloseAll(); | |
115 | |
116 // Read empty IBusObject. | |
117 dbus::MessageReader reader(message.get()); | |
118 IBusObjectReader ibus_object_reader(kSampleTypeName, &reader); | |
119 ASSERT_TRUE(ibus_object_reader.Init()); | |
120 EXPECT_FALSE(ibus_object_reader.HasMoreData()); | |
121 } | |
122 | |
123 TEST(IBusObjectTest, PopAppendIBusTextTest) { | |
124 const char kSampleTypeName[] = "Empty IBusObject Name"; | |
125 const char kSampleString[] = "Sapmle String"; | |
126 IBusText::SelectionAttribute selection_attribute; | |
127 selection_attribute.start_index = 0UL; | |
128 selection_attribute.end_index = 10UL; | |
129 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); | |
130 | |
131 // Write IBusText. | |
132 dbus::MessageWriter writer(message.get()); | |
133 IBusObjectWriter ibus_object_writer(kSampleTypeName, "v", &writer); | |
134 ibus_object_writer.CloseHeader(); | |
135 IBusText ibus_text; | |
136 ibus_text.mutable_selection_attributes()->push_back(selection_attribute); | |
137 ibus_text.set_text(kSampleString); | |
138 ibus_object_writer.AppendIBusText(ibus_text); | |
139 ibus_object_writer.CloseAll(); | |
140 | |
141 // Read IBusText; | |
142 dbus::MessageReader reader(message.get()); | |
143 IBusObjectReader ibus_object_reader(kSampleTypeName, &reader); | |
144 IBusText result_text; | |
145 ASSERT_TRUE(ibus_object_reader.Init()); | |
146 ASSERT_TRUE(ibus_object_reader.PopIBusText(&result_text)); | |
147 EXPECT_FALSE(ibus_object_reader.HasMoreData()); | |
148 EXPECT_EQ(kSampleString, result_text.text()); | |
149 const std::vector<IBusText::SelectionAttribute>& selection_attributes = | |
150 result_text.selection_attributes(); | |
151 ASSERT_EQ(1UL, selection_attributes.size()); | |
152 EXPECT_EQ(selection_attribute.start_index, | |
153 selection_attributes[0].start_index); | |
154 EXPECT_EQ(selection_attribute.end_index, | |
155 selection_attributes[0].end_index); | |
156 } | |
157 | |
158 TEST(IBusObjectTest, PopAppendStringAsIBusText) { | |
159 const char kSampleTypeName[] = "Empty IBusObject Name"; | |
160 const char kSampleString[] = "Sapmle String"; | |
161 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); | |
162 | |
163 // Write string as IBusText. | |
164 dbus::MessageWriter writer(message.get()); | |
165 IBusObjectWriter ibus_object_writer(kSampleTypeName, "v", &writer); | |
166 ibus_object_writer.CloseHeader(); | |
167 ibus_object_writer.AppendStringAsIBusText(kSampleString); | |
168 ibus_object_writer.CloseAll(); | |
169 | |
170 // Read string from IBusText. | |
171 dbus::MessageReader reader(message.get()); | |
172 IBusObjectReader ibus_object_reader(kSampleTypeName, &reader); | |
173 std::string result_str; | |
174 ASSERT_TRUE(ibus_object_reader.Init()); | |
175 ASSERT_TRUE(ibus_object_reader.PopStringFromIBusText(&result_str)); | |
176 EXPECT_FALSE(ibus_object_reader.HasMoreData()); | |
177 EXPECT_EQ(kSampleString, result_str); | |
178 } | |
179 | |
180 } // namespace chromeos | |
OLD | NEW |