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