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.
6 #include "ui/base/ime/candidate_window.h"
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"
20 const size_t kSampleCandidateSize
= 3;
21 const char* kSampleCandidate
[] = {
26 const char* kSampleDescriptionTitle
[] = {
27 "Sample Description Title 1",
28 "Sample Description Title 2",
29 "Sample Description Title 3",
31 const char* kSampleDescriptionBody
[] = {
32 "Sample Description Body 1",
33 "Sample Description Body 2",
34 "Sample Description Body 3",
39 TEST(CandidateWindow
, IsEqualTest
) {
43 const base::string16 kSampleString1
= base::UTF8ToUTF16("Sample 1");
44 const base::string16 kSampleString2
= base::UTF8ToUTF16("Sample 2");
46 EXPECT_TRUE(cw1
.IsEqual(cw2
));
47 EXPECT_TRUE(cw2
.IsEqual(cw1
));
51 EXPECT_FALSE(cw1
.IsEqual(cw2
));
52 EXPECT_FALSE(cw2
.IsEqual(cw1
));
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);
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);
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
);
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);
79 // Check equality for candidates member variable.
80 CandidateWindow::Entry entry1
;
81 CandidateWindow::Entry entry2
;
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
));
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();
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();
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();
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();
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();
136 TEST(CandidateWindow
, CopyFromTest
) {
140 const base::string16 kSampleString
= base::UTF8ToUTF16("Sample");
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);
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
);
157 EXPECT_TRUE(cw1
.IsEqual(cw2
));
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
);
170 candidate_window
.set_cursor_position(1);
172 std::vector
<InfolistEntry
> infolist_entries
;
173 bool has_highlighted
= false;
175 candidate_window
.GetInfolistEntries(&infolist_entries
, &has_highlighted
);
177 EXPECT_EQ(kSampleCandidateSize
, infolist_entries
.size());
178 EXPECT_TRUE(has_highlighted
);
179 EXPECT_TRUE(infolist_entries
[1].highlighted
);
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
);
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]);
198 candidate_window
.set_cursor_position(2);
200 std::vector
<InfolistEntry
> infolist_entries
;
201 bool has_highlighted
= false;
203 candidate_window
.GetInfolistEntries(&infolist_entries
, &has_highlighted
);
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
);
211 TEST(CandidateWindow
, GetInfolistEntries_SparseNoSelectionCase
) {
212 CandidateWindow candidate_window
;
213 candidate_window
.set_page_size(10);
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
);
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]);
228 candidate_window
.set_cursor_position(0);
230 std::vector
<InfolistEntry
> infolist_entries
;
231 bool has_highlighted
;
233 candidate_window
.GetInfolistEntries(&infolist_entries
, &has_highlighted
);
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
);
242 TEST(CandidateWindow
, GetInfolistEntries_NoInfolistCase
) {
243 CandidateWindow candidate_window
;
244 candidate_window
.set_page_size(10);
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
);
251 candidate_window
.set_cursor_position(1);
253 std::vector
<InfolistEntry
> infolist_entries
;
254 bool has_highlighted
= false;
256 candidate_window
.GetInfolistEntries(&infolist_entries
, &has_highlighted
);
258 EXPECT_TRUE(infolist_entries
.empty());
259 EXPECT_FALSE(has_highlighted
);