1 // Copyright 2014 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.
5
6 #include "ui/base/ime/candidate_window.h"
7
8 #include <string>
9
10 #include "base/compiler_specific.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace ui {
17
18 namespace {
19
20 const size_t kSampleCandidateSize = 3;
21 const char* kSampleCandidate[] = {
22 "Sample Candidate 1",
23 "Sample Candidate 2",
24 "Sample Candidate 3",
25 };
26 const char* kSampleDescriptionTitle[] = {
27 "Sample Description Title 1",
28 "Sample Description Title 2",
29 "Sample Description Title 3",
30 };
31 const char* kSampleDescriptionBody[] = {
32 "Sample Description Body 1",
33 "Sample Description Body 2",
34 "Sample Description Body 3",
35 };
36
37 }
38
TEST(CandidateWindow,IsEqualTest)39 TEST(CandidateWindow, IsEqualTest) {
40 CandidateWindow cw1;
41 CandidateWindow cw2;
42
43 const base::string16 kSampleString1 = base::UTF8ToUTF16("Sample 1");
44 const base::string16 kSampleString2 = base::UTF8ToUTF16("Sample 2");
45
46 EXPECT_TRUE(cw1.IsEqual(cw2));
47 EXPECT_TRUE(cw2.IsEqual(cw1));
48
49 cw1.set_page_size(1);
50 cw2.set_page_size(2);
51 EXPECT_FALSE(cw1.IsEqual(cw2));
52 EXPECT_FALSE(cw2.IsEqual(cw1));
53 cw2.set_page_size(1);
54
55 cw1.set_cursor_position(1);
56 cw2.set_cursor_position(2);
57 EXPECT_FALSE(cw1.IsEqual(cw2));
58 EXPECT_FALSE(cw2.IsEqual(cw1));
59 cw2.set_cursor_position(1);
60
61 cw1.set_is_cursor_visible(true);
62 cw2.set_is_cursor_visible(false);
63 EXPECT_FALSE(cw1.IsEqual(cw2));
64 EXPECT_FALSE(cw2.IsEqual(cw1));
65 cw2.set_is_cursor_visible(true);
66
67 cw1.set_orientation(CandidateWindow::HORIZONTAL);
68 cw2.set_orientation(CandidateWindow::VERTICAL);
69 EXPECT_FALSE(cw1.IsEqual(cw2));
70 EXPECT_FALSE(cw2.IsEqual(cw1));
71 cw2.set_orientation(CandidateWindow::HORIZONTAL);
72
73 cw1.set_show_window_at_composition(true);
74 cw2.set_show_window_at_composition(false);
75 EXPECT_FALSE(cw1.IsEqual(cw2));
76 EXPECT_FALSE(cw2.IsEqual(cw1));
77 cw2.set_show_window_at_composition(true);
78
79 // Check equality for candidates member variable.
80 CandidateWindow::Entry entry1;
81 CandidateWindow::Entry entry2;
82
83 cw1.mutable_candidates()->push_back(entry1);
84 EXPECT_FALSE(cw1.IsEqual(cw2));
85 EXPECT_FALSE(cw2.IsEqual(cw1));
86 cw2.mutable_candidates()->push_back(entry2);
87 EXPECT_TRUE(cw1.IsEqual(cw2));
88 EXPECT_TRUE(cw2.IsEqual(cw1));
89
90 entry1.value = kSampleString1;
91 entry2.value = kSampleString2;
92 cw1.mutable_candidates()->push_back(entry1);
93 cw2.mutable_candidates()->push_back(entry2);
94 EXPECT_FALSE(cw1.IsEqual(cw2));
95 EXPECT_FALSE(cw2.IsEqual(cw1));
96 cw1.mutable_candidates()->clear();
97 cw2.mutable_candidates()->clear();
98
99 entry1.label = kSampleString1;
100 entry2.label = kSampleString2;
101 cw1.mutable_candidates()->push_back(entry1);
102 cw2.mutable_candidates()->push_back(entry2);
103 EXPECT_FALSE(cw1.IsEqual(cw2));
104 EXPECT_FALSE(cw2.IsEqual(cw1));
105 cw1.mutable_candidates()->clear();
106 cw2.mutable_candidates()->clear();
107
108 entry1.annotation = kSampleString1;
109 entry2.annotation = kSampleString2;
110 cw1.mutable_candidates()->push_back(entry1);
111 cw2.mutable_candidates()->push_back(entry2);
112 EXPECT_FALSE(cw1.IsEqual(cw2));
113 EXPECT_FALSE(cw2.IsEqual(cw1));
114 cw1.mutable_candidates()->clear();
115 cw2.mutable_candidates()->clear();
116
117 entry1.description_title = kSampleString1;
118 entry2.description_title = kSampleString2;
119 cw1.mutable_candidates()->push_back(entry1);
120 cw2.mutable_candidates()->push_back(entry2);
121 EXPECT_FALSE(cw1.IsEqual(cw2));
122 EXPECT_FALSE(cw2.IsEqual(cw1));
123 cw1.mutable_candidates()->clear();
124 cw2.mutable_candidates()->clear();
125
126 entry1.description_body = kSampleString1;
127 entry2.description_body = kSampleString2;
128 cw1.mutable_candidates()->push_back(entry1);
129 cw2.mutable_candidates()->push_back(entry2);
130 EXPECT_FALSE(cw1.IsEqual(cw2));
131 EXPECT_FALSE(cw2.IsEqual(cw1));
132 cw1.mutable_candidates()->clear();
133 cw2.mutable_candidates()->clear();
134 }
135
TEST(CandidateWindow,CopyFromTest)136 TEST(CandidateWindow, CopyFromTest) {
137 CandidateWindow cw1;
138 CandidateWindow cw2;
139
140 const base::string16 kSampleString = base::UTF8ToUTF16("Sample");
141
142 cw1.set_page_size(1);
143 cw1.set_cursor_position(2);
144 cw1.set_is_cursor_visible(false);
145 cw1.set_orientation(CandidateWindow::HORIZONTAL);
146 cw1.set_show_window_at_composition(false);
147
148 CandidateWindow::Entry entry;
149 entry.value = kSampleString;
150 entry.label = kSampleString;
151 entry.annotation = kSampleString;
152 entry.description_title = kSampleString;
153 entry.description_body = kSampleString;
154 cw1.mutable_candidates()->push_back(entry);
155
156 cw2.CopyFrom(cw1);
157 EXPECT_TRUE(cw1.IsEqual(cw2));
158 }
159
TEST(CandidateWindow,GetInfolistEntries_DenseCase)160 TEST(CandidateWindow, GetInfolistEntries_DenseCase) {
161 CandidateWindow candidate_window;
162 candidate_window.set_page_size(10);
163 for (size_t i = 0; i < kSampleCandidateSize; ++i) {
164 CandidateWindow::Entry entry;
165 entry.value = base::UTF8ToUTF16(kSampleCandidate[i]);
166 entry.description_title = base::UTF8ToUTF16(kSampleDescriptionTitle[i]);
167 entry.description_body = base::UTF8ToUTF16(kSampleDescriptionBody[i]);
168 candidate_window.mutable_candidates()->push_back(entry);
169 }
170 candidate_window.set_cursor_position(1);
171
172 std::vector<InfolistEntry> infolist_entries;
173 bool has_highlighted = false;
174
175 candidate_window.GetInfolistEntries(&infolist_entries, &has_highlighted);
176
177 EXPECT_EQ(kSampleCandidateSize, infolist_entries.size());
178 EXPECT_TRUE(has_highlighted);
179 EXPECT_TRUE(infolist_entries[1].highlighted);
180 }
181
TEST(CandidateWindow,GetInfolistEntries_SparseCase)182 TEST(CandidateWindow, GetInfolistEntries_SparseCase) {
183 CandidateWindow candidate_window;
184 candidate_window.set_page_size(10);
185 for (size_t i = 0; i < kSampleCandidateSize; ++i) {
186 CandidateWindow::Entry entry;
187 entry.value = base::UTF8ToUTF16(kSampleCandidate[i]);
188 candidate_window.mutable_candidates()->push_back(entry);
189 }
190
191 std::vector<CandidateWindow::Entry>* candidates =
192 candidate_window.mutable_candidates();
193 (*candidates)[2].description_title =
194 base::UTF8ToUTF16(kSampleDescriptionTitle[2]);
195 (*candidates)[2].description_body =
196 base::UTF8ToUTF16(kSampleDescriptionBody[2]);
197
198 candidate_window.set_cursor_position(2);
199
200 std::vector<InfolistEntry> infolist_entries;
201 bool has_highlighted = false;
202
203 candidate_window.GetInfolistEntries(&infolist_entries, &has_highlighted);
204
205 // Infolist entries skips empty descriptions, so expected entry size is 1.
206 EXPECT_EQ(1UL, infolist_entries.size());
207 EXPECT_TRUE(has_highlighted);
208 EXPECT_TRUE(infolist_entries[0].highlighted);
209 }
210
TEST(CandidateWindow,GetInfolistEntries_SparseNoSelectionCase)211 TEST(CandidateWindow, GetInfolistEntries_SparseNoSelectionCase) {
212 CandidateWindow candidate_window;
213 candidate_window.set_page_size(10);
214
215 for (size_t i = 0; i < kSampleCandidateSize; ++i) {
216 CandidateWindow::Entry entry;
217 entry.value = base::UTF8ToUTF16(kSampleCandidate[i]);
218 candidate_window.mutable_candidates()->push_back(entry);
219 }
220
221 std::vector<CandidateWindow::Entry>* candidates =
222 candidate_window.mutable_candidates();
223 (*candidates)[2].description_title =
224 base::UTF8ToUTF16(kSampleDescriptionTitle[2]);
225 (*candidates)[2].description_body =
226 base::UTF8ToUTF16(kSampleDescriptionBody[2]);
227
228 candidate_window.set_cursor_position(0);
229
230 std::vector<InfolistEntry> infolist_entries;
231 bool has_highlighted;
232
233 candidate_window.GetInfolistEntries(&infolist_entries, &has_highlighted);
234
235 // Infolist entries skips empty descriptions, so expected entry size is 1 and
236 // no highlighted entries.
237 EXPECT_EQ(1UL, infolist_entries.size());
238 EXPECT_FALSE(has_highlighted);
239 EXPECT_FALSE(infolist_entries[0].highlighted);
240 }
241
TEST(CandidateWindow,GetInfolistEntries_NoInfolistCase)242 TEST(CandidateWindow, GetInfolistEntries_NoInfolistCase) {
243 CandidateWindow candidate_window;
244 candidate_window.set_page_size(10);
245
246 for (size_t i = 0; i < kSampleCandidateSize; ++i) {
247 CandidateWindow::Entry entry;
248 entry.value = base::UTF8ToUTF16(kSampleCandidate[i]);
249 candidate_window.mutable_candidates()->push_back(entry);
250 }
251 candidate_window.set_cursor_position(1);
252
253 std::vector<InfolistEntry> infolist_entries;
254 bool has_highlighted = false;
255
256 candidate_window.GetInfolistEntries(&infolist_entries, &has_highlighted);
257
258 EXPECT_TRUE(infolist_entries.empty());
259 EXPECT_FALSE(has_highlighted);
260 }
261
262 } // namespace ui
263