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 tests.
6 #include "chromeos/dbus/ibus/ibus_lookup_table.h"
10 #include "base/compiler_specific.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "chromeos/dbus/ibus/ibus_object.h"
14 #include "chromeos/dbus/ibus/ibus_text.h"
15 #include "dbus/message.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
21 TEST(IBusLookupTable
, WriteReadTest
) {
22 const char kSampleText1
[] = "Sample Text 1";
23 const char kSampleText2
[] = "Sample Text 2";
24 const char kSampleLabel1
[] = "Sample Label 1";
25 const char kSampleLabel2
[] = "Sample Label 2";
26 const uint32 kPageSize
= 11;
27 const uint32 kCursorPosition
= 12;
28 const bool kIsCursorVisible
= true;
29 const IBusLookupTable::Orientation kOrientation
=
30 IBusLookupTable::VERTICAL
;
32 // Create IBusLookupTable.
33 IBusLookupTable lookup_table
;
34 lookup_table
.set_page_size(kPageSize
);
35 lookup_table
.set_cursor_position(kCursorPosition
);
36 lookup_table
.set_is_cursor_visible(kIsCursorVisible
);
37 lookup_table
.set_orientation(kOrientation
);
38 std::vector
<IBusLookupTable::Entry
>* candidates
=
39 lookup_table
.mutable_candidates();
40 IBusLookupTable::Entry entry1
;
41 entry1
.value
= kSampleText1
;
42 entry1
.label
= kSampleLabel1
;
43 candidates
->push_back(entry1
);
45 IBusLookupTable::Entry entry2
;
46 entry2
.value
= kSampleText2
;
47 entry2
.label
= kSampleLabel2
;
48 candidates
->push_back(entry2
);
50 // Write IBusLookupTable.
51 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
52 dbus::MessageWriter
writer(response
.get());
53 AppendIBusLookupTable(lookup_table
, &writer
);
55 // Read IBusLookupTable.
56 IBusLookupTable target_lookup_table
;
57 dbus::MessageReader
reader(response
.get());
58 PopIBusLookupTable(&reader
, &target_lookup_table
);
61 EXPECT_EQ(kPageSize
, target_lookup_table
.page_size());
62 EXPECT_EQ(kCursorPosition
, target_lookup_table
.cursor_position());
63 EXPECT_TRUE(kIsCursorVisible
== target_lookup_table
.is_cursor_visible());
64 EXPECT_EQ(kOrientation
, target_lookup_table
.orientation());
65 ASSERT_EQ(2UL, target_lookup_table
.candidates().size());
66 EXPECT_EQ(kSampleText1
, target_lookup_table
.candidates().at(0).value
);
67 EXPECT_EQ(kSampleText2
, target_lookup_table
.candidates().at(1).value
);
68 EXPECT_EQ(kSampleLabel1
, target_lookup_table
.candidates().at(0).label
);
69 EXPECT_EQ(kSampleLabel2
, target_lookup_table
.candidates().at(1).label
);
72 TEST(IBusLookupTable
, WriteReadWithoutLableTest
) {
73 const char kSampleText1
[] = "Sample Text 1";
74 const char kSampleText2
[] = "Sample Text 2";
75 const uint32 kPageSize
= 11;
76 const uint32 kCursorPosition
= 12;
77 const bool kIsCursorVisible
= true;
78 const IBusLookupTable::Orientation kOrientation
=
79 IBusLookupTable::VERTICAL
;
81 // Create IBusLookupTable.
82 IBusLookupTable lookup_table
;
83 lookup_table
.set_page_size(kPageSize
);
84 lookup_table
.set_cursor_position(kCursorPosition
);
85 lookup_table
.set_is_cursor_visible(kIsCursorVisible
);
86 lookup_table
.set_orientation(kOrientation
);
87 std::vector
<IBusLookupTable::Entry
>* candidates
=
88 lookup_table
.mutable_candidates();
89 IBusLookupTable::Entry entry1
;
90 entry1
.value
= kSampleText1
;
91 candidates
->push_back(entry1
);
93 IBusLookupTable::Entry entry2
;
94 entry2
.value
= kSampleText2
;
95 candidates
->push_back(entry2
);
97 // Write IBusLookupTable.
98 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
99 dbus::MessageWriter
writer(response
.get());
100 AppendIBusLookupTable(lookup_table
, &writer
);
102 // Read IBusLookupTable.
103 IBusLookupTable target_lookup_table
;
104 dbus::MessageReader
reader(response
.get());
105 PopIBusLookupTable(&reader
, &target_lookup_table
);
108 EXPECT_EQ(kPageSize
, target_lookup_table
.page_size());
109 EXPECT_EQ(kCursorPosition
, target_lookup_table
.cursor_position());
110 EXPECT_EQ(kIsCursorVisible
, target_lookup_table
.is_cursor_visible());
111 EXPECT_EQ(kOrientation
, target_lookup_table
.orientation());
112 ASSERT_EQ(2UL, target_lookup_table
.candidates().size());
113 EXPECT_EQ(kSampleText1
, target_lookup_table
.candidates().at(0).value
);
114 EXPECT_EQ(kSampleText2
, target_lookup_table
.candidates().at(1).value
);
115 EXPECT_TRUE(target_lookup_table
.candidates().at(0).label
.empty());
116 EXPECT_TRUE(target_lookup_table
.candidates().at(1).label
.empty());
119 TEST(IBusLookupTable
, ReadMozcCandidateTest
) {
120 // IBusObjectWriter does not support attachment field handling, so manually
121 // create IBusLookupTable with mozc.candidates attachment.
122 const char kSampleText1
[] = "Sample Text 1";
123 const char kSampleText2
[] = "Sample Text 2";
124 const char kSampleLabel1
[] = "Sample Label 1";
125 const char kSampleLabel2
[] = "Sample Label 2";
126 const uint32 kPageSize
= 11;
127 const uint32 kCursorPosition
= 12;
128 const bool kIsCursorVisible
= true;
129 const IBusLookupTable::Orientation kOrientation
=
130 IBusLookupTable::VERTICAL
;
131 const bool kShowWindowAtComposition
= false;
133 // Create IBusLookupTable.
134 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
135 dbus::MessageWriter
writer(response
.get());
136 dbus::MessageWriter
top_variant_writer(NULL
);
137 writer
.OpenVariant("(sa{sv}uubbiavav)", &top_variant_writer
);
138 dbus::MessageWriter
contents_writer(NULL
);
139 top_variant_writer
.OpenStruct(&contents_writer
);
140 contents_writer
.AppendString("IBusLookupTable");
142 // Write attachment field.
143 dbus::MessageWriter
attachment_array_writer(NULL
);
144 contents_writer
.OpenArray("{sv}", &attachment_array_writer
);
145 dbus::MessageWriter
entry_writer(NULL
);
146 attachment_array_writer
.OpenDictEntry(&entry_writer
);
147 entry_writer
.AppendString("show_window_at_composition");
148 dbus::MessageWriter
variant_writer(NULL
);
149 entry_writer
.OpenVariant("v", &variant_writer
);
150 dbus::MessageWriter
sub_variant_writer(NULL
);
151 variant_writer
.OpenVariant("b", &sub_variant_writer
);
152 sub_variant_writer
.AppendBool(kShowWindowAtComposition
);
154 // Close attachment field container.
155 variant_writer
.CloseContainer(&sub_variant_writer
);
156 entry_writer
.CloseContainer(&variant_writer
);
157 attachment_array_writer
.CloseContainer(&entry_writer
);
158 contents_writer
.CloseContainer(&attachment_array_writer
);
160 // Write IBusLookupTable contents.
161 contents_writer
.AppendUint32(kPageSize
);
162 contents_writer
.AppendUint32(kCursorPosition
);
163 contents_writer
.AppendBool(kIsCursorVisible
);
164 contents_writer
.AppendBool(false);
165 contents_writer
.AppendInt32(static_cast<int32
>(kOrientation
));
168 dbus::MessageWriter
text_writer(NULL
);
169 contents_writer
.OpenArray("v", &text_writer
);
170 AppendStringAsIBusText(kSampleText1
, &text_writer
);
171 AppendStringAsIBusText(kSampleText2
, &text_writer
);
172 contents_writer
.CloseContainer(&text_writer
);
173 dbus::MessageWriter
label_writer(NULL
);
174 contents_writer
.OpenArray("v", &label_writer
);
175 AppendStringAsIBusText(kSampleLabel1
, &label_writer
);
176 AppendStringAsIBusText(kSampleLabel2
, &label_writer
);
177 contents_writer
.CloseContainer(&label_writer
);
179 // Close all containers.
180 top_variant_writer
.CloseContainer(&contents_writer
);
181 writer
.CloseContainer(&top_variant_writer
);
183 // Read IBusLookupTable.
184 IBusLookupTable target_lookup_table
;
185 dbus::MessageReader
reader(response
.get());
186 PopIBusLookupTable(&reader
, &target_lookup_table
);
189 EXPECT_EQ(kPageSize
, target_lookup_table
.page_size());
190 EXPECT_EQ(kCursorPosition
, target_lookup_table
.cursor_position());
191 EXPECT_EQ(kIsCursorVisible
, target_lookup_table
.is_cursor_visible());
192 EXPECT_EQ(kOrientation
, target_lookup_table
.orientation());
193 ASSERT_EQ(2UL, target_lookup_table
.candidates().size());
194 EXPECT_EQ(kSampleText1
, target_lookup_table
.candidates().at(0).value
);
195 EXPECT_EQ(kSampleText2
, target_lookup_table
.candidates().at(1).value
);
196 EXPECT_EQ(kSampleLabel1
, target_lookup_table
.candidates().at(0).label
);
197 EXPECT_EQ(kSampleLabel2
, target_lookup_table
.candidates().at(1).label
);
198 EXPECT_TRUE(kShowWindowAtComposition
==
199 target_lookup_table
.show_window_at_composition());
202 TEST(IBusLookupTable
, IsEqualTest
) {
203 IBusLookupTable table1
;
204 IBusLookupTable table2
;
206 const char kSampleString1
[] = "Sample 1";
207 const char kSampleString2
[] = "Sample 2";
209 EXPECT_TRUE(table1
.IsEqual(table2
));
210 EXPECT_TRUE(table2
.IsEqual(table1
));
212 table1
.set_page_size(1);
213 table2
.set_page_size(2);
214 EXPECT_FALSE(table1
.IsEqual(table2
));
215 EXPECT_FALSE(table2
.IsEqual(table1
));
216 table2
.set_page_size(1);
218 table1
.set_cursor_position(1);
219 table2
.set_cursor_position(2);
220 EXPECT_FALSE(table1
.IsEqual(table2
));
221 EXPECT_FALSE(table2
.IsEqual(table1
));
222 table2
.set_cursor_position(1);
224 table1
.set_is_cursor_visible(true);
225 table2
.set_is_cursor_visible(false);
226 EXPECT_FALSE(table1
.IsEqual(table2
));
227 EXPECT_FALSE(table2
.IsEqual(table1
));
228 table2
.set_is_cursor_visible(true);
230 table1
.set_orientation(IBusLookupTable::HORIZONTAL
);
231 table2
.set_orientation(IBusLookupTable::VERTICAL
);
232 EXPECT_FALSE(table1
.IsEqual(table2
));
233 EXPECT_FALSE(table2
.IsEqual(table1
));
234 table2
.set_orientation(IBusLookupTable::HORIZONTAL
);
236 table1
.set_show_window_at_composition(true);
237 table2
.set_show_window_at_composition(false);
238 EXPECT_FALSE(table1
.IsEqual(table2
));
239 EXPECT_FALSE(table2
.IsEqual(table1
));
240 table2
.set_show_window_at_composition(true);
242 // Check equality for candidates member variable.
243 IBusLookupTable::Entry entry1
;
244 IBusLookupTable::Entry entry2
;
246 table1
.mutable_candidates()->push_back(entry1
);
247 EXPECT_FALSE(table1
.IsEqual(table2
));
248 EXPECT_FALSE(table2
.IsEqual(table1
));
249 table2
.mutable_candidates()->push_back(entry2
);
250 EXPECT_TRUE(table1
.IsEqual(table2
));
251 EXPECT_TRUE(table2
.IsEqual(table1
));
253 entry1
.value
= kSampleString1
;
254 entry2
.value
= kSampleString2
;
255 table1
.mutable_candidates()->push_back(entry1
);
256 table2
.mutable_candidates()->push_back(entry2
);
257 EXPECT_FALSE(table1
.IsEqual(table2
));
258 EXPECT_FALSE(table2
.IsEqual(table1
));
259 table1
.mutable_candidates()->clear();
260 table2
.mutable_candidates()->clear();
262 entry1
.label
= kSampleString1
;
263 entry2
.label
= kSampleString2
;
264 table1
.mutable_candidates()->push_back(entry1
);
265 table2
.mutable_candidates()->push_back(entry2
);
266 EXPECT_FALSE(table1
.IsEqual(table2
));
267 EXPECT_FALSE(table2
.IsEqual(table1
));
268 table1
.mutable_candidates()->clear();
269 table2
.mutable_candidates()->clear();
271 entry1
.annotation
= kSampleString1
;
272 entry2
.annotation
= kSampleString2
;
273 table1
.mutable_candidates()->push_back(entry1
);
274 table2
.mutable_candidates()->push_back(entry2
);
275 EXPECT_FALSE(table1
.IsEqual(table2
));
276 EXPECT_FALSE(table2
.IsEqual(table1
));
277 table1
.mutable_candidates()->clear();
278 table2
.mutable_candidates()->clear();
280 entry1
.description_title
= kSampleString1
;
281 entry2
.description_title
= kSampleString2
;
282 table1
.mutable_candidates()->push_back(entry1
);
283 table2
.mutable_candidates()->push_back(entry2
);
284 EXPECT_FALSE(table1
.IsEqual(table2
));
285 EXPECT_FALSE(table2
.IsEqual(table1
));
286 table1
.mutable_candidates()->clear();
287 table2
.mutable_candidates()->clear();
289 entry1
.description_body
= kSampleString1
;
290 entry2
.description_body
= kSampleString2
;
291 table1
.mutable_candidates()->push_back(entry1
);
292 table2
.mutable_candidates()->push_back(entry2
);
293 EXPECT_FALSE(table1
.IsEqual(table2
));
294 EXPECT_FALSE(table2
.IsEqual(table1
));
295 table1
.mutable_candidates()->clear();
296 table2
.mutable_candidates()->clear();
299 TEST(IBusLookupTable
, CopyFromTest
) {
300 IBusLookupTable table1
;
301 IBusLookupTable table2
;
303 const char kSampleString
[] = "Sample";
305 table1
.set_page_size(1);
306 table1
.set_cursor_position(2);
307 table1
.set_is_cursor_visible(false);
308 table1
.set_orientation(IBusLookupTable::HORIZONTAL
);
309 table1
.set_show_window_at_composition(false);
311 IBusLookupTable::Entry entry
;
312 entry
.value
= kSampleString
;
313 entry
.label
= kSampleString
;
314 entry
.annotation
= kSampleString
;
315 entry
.description_title
= kSampleString
;
316 entry
.description_body
= kSampleString
;
317 table1
.mutable_candidates()->push_back(entry
);
319 table2
.CopyFrom(table1
);
320 EXPECT_TRUE(table1
.IsEqual(table2
));
322 } // namespace chromeos