OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // TODO(nona): Add unittests for UpdateCandidates. |
4 | 5 |
5 #include "chrome/browser/chromeos/input_method/candidate_window_view.h" | 6 #include "chrome/browser/chromeos/input_method/candidate_window_view.h" |
6 | 7 |
| 8 #include <string> |
| 9 |
7 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
8 | 11 |
9 namespace chromeos { | 12 namespace chromeos { |
10 namespace input_method { | 13 namespace input_method { |
11 | 14 |
| 15 namespace { |
| 16 |
| 17 void ClearInputMethodLookupTable(InputMethodLookupTable* table) { |
| 18 table->visible = false; |
| 19 table->cursor_absolute_index = 0; |
| 20 table->page_size = 10; |
| 21 table->candidates.clear(); |
| 22 table->orientation = InputMethodLookupTable::kVertical; |
| 23 table->labels.clear(); |
| 24 table->annotations.clear(); |
| 25 table->mozc_candidates.Clear(); |
| 26 } |
| 27 |
| 28 void InitializeMozcCandidates(InputMethodLookupTable* table) { |
| 29 table->mozc_candidates.Clear(); |
| 30 table->mozc_candidates.set_position(0); |
| 31 table->mozc_candidates.set_size(0); |
| 32 } |
| 33 |
| 34 void SetCaretRectIntoMozcCandidates( |
| 35 InputMethodLookupTable* table, |
| 36 mozc::commands::Candidates::CandidateWindowLocation location, |
| 37 int x, |
| 38 int y, |
| 39 int width, |
| 40 int height) { |
| 41 table->mozc_candidates.set_window_location(location); |
| 42 mozc::commands::Rectangle *rect = |
| 43 table->mozc_candidates.mutable_composition_rectangle(); |
| 44 rect->set_x(x); |
| 45 rect->set_y(y); |
| 46 rect->set_width(width); |
| 47 rect->set_height(height); |
| 48 } |
| 49 |
| 50 void AppendCandidateIntoMozcCandidates(InputMethodLookupTable* table, |
| 51 std::string value) { |
| 52 mozc::commands::Candidates::Candidate *candidate = |
| 53 table->mozc_candidates.add_candidate(); |
| 54 |
| 55 int current_entry_count = table->mozc_candidates.candidate_size(); |
| 56 candidate->set_index(current_entry_count); |
| 57 candidate->set_value(value); |
| 58 candidate->set_id(current_entry_count); |
| 59 candidate->set_information_id(current_entry_count); |
| 60 |
| 61 |
| 62 } |
| 63 |
| 64 } // namespace |
| 65 |
12 TEST(CandidateWindowViewTest, ShouldUpdateCandidateViewsTest) { | 66 TEST(CandidateWindowViewTest, ShouldUpdateCandidateViewsTest) { |
13 // This test verifies the process of judging update lookup-table or not. | 67 // This test verifies the process of judging update lookup-table or not. |
14 // This judgement is handled by ShouldUpdateCandidateViews, which returns true | 68 // This judgement is handled by ShouldUpdateCandidateViews, which returns true |
15 // if update is necessary and vice versa. | 69 // if update is necessary and vice versa. |
16 const char* kSampleCandidate1 = "Sample Candidate 1"; | 70 const char* kSampleCandidate1 = "Sample Candidate 1"; |
17 const char* kSampleCandidate2 = "Sample Candidate 2"; | 71 const char* kSampleCandidate2 = "Sample Candidate 2"; |
18 const char* kSampleCandidate3 = "Sample Candidate 3"; | 72 const char* kSampleCandidate3 = "Sample Candidate 3"; |
19 | 73 |
20 const char* kSampleAnnotation1 = "Sample Annotation 1"; | 74 const char* kSampleAnnotation1 = "Sample Annotation 1"; |
21 const char* kSampleAnnotation2 = "Sample Annotation 2"; | 75 const char* kSampleAnnotation2 = "Sample Annotation 2"; |
22 const char* kSampleAnnotation3 = "Sample Annotation 3"; | 76 const char* kSampleAnnotation3 = "Sample Annotation 3"; |
23 | 77 |
24 const char* kSampleLabel1 = "Sample Label 1"; | 78 const char* kSampleLabel1 = "Sample Label 1"; |
25 const char* kSampleLabel2 = "Sample Label 2"; | 79 const char* kSampleLabel2 = "Sample Label 2"; |
26 const char* kSampleLabel3 = "Sample Label 3"; | 80 const char* kSampleLabel3 = "Sample Label 3"; |
27 | 81 |
28 InputMethodLookupTable old_table; | 82 InputMethodLookupTable old_table; |
29 InputMethodLookupTable new_table; | 83 InputMethodLookupTable new_table; |
30 | 84 |
| 85 ClearInputMethodLookupTable(&old_table); |
| 86 ClearInputMethodLookupTable(&new_table); |
| 87 |
31 old_table.visible = true; | 88 old_table.visible = true; |
32 old_table.cursor_absolute_index = 0; | 89 old_table.cursor_absolute_index = 0; |
33 old_table.page_size = 1; | 90 old_table.page_size = 1; |
34 old_table.candidates.clear(); | 91 old_table.candidates.clear(); |
35 old_table.orientation = InputMethodLookupTable::kVertical; | 92 old_table.orientation = InputMethodLookupTable::kVertical; |
36 old_table.labels.clear(); | 93 old_table.labels.clear(); |
37 old_table.annotations.clear(); | 94 old_table.annotations.clear(); |
38 | 95 |
39 new_table = old_table; | 96 new_table = old_table; |
40 | 97 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 new_table)); | 169 new_table)); |
113 new_table.annotations.clear(); | 170 new_table.annotations.clear(); |
114 EXPECT_TRUE(CandidateWindowView::ShouldUpdateCandidateViews(old_table, | 171 EXPECT_TRUE(CandidateWindowView::ShouldUpdateCandidateViews(old_table, |
115 new_table)); | 172 new_table)); |
116 new_table.annotations.push_back(kSampleAnnotation2); | 173 new_table.annotations.push_back(kSampleAnnotation2); |
117 old_table.annotations.clear(); | 174 old_table.annotations.clear(); |
118 EXPECT_TRUE(CandidateWindowView::ShouldUpdateCandidateViews(old_table, | 175 EXPECT_TRUE(CandidateWindowView::ShouldUpdateCandidateViews(old_table, |
119 new_table)); | 176 new_table)); |
120 } | 177 } |
121 | 178 |
| 179 TEST(CandidateWindowViewTest, MozcSuggestWindowShouldUpdateTest) { |
| 180 // ShouldUpdateCandidateViews method should also judge with consideration of |
| 181 // the mozc specific candidate information. Following tests verify them. |
| 182 const char* kSampleCandidate1 = "Sample Candidate 1"; |
| 183 const char* kSampleCandidate2 = "Sample Candidate 2"; |
| 184 |
| 185 const int kCaretPositionX1 = 10; |
| 186 const int kCaretPositionY1 = 20; |
| 187 const int kCaretPositionWidth1 = 30; |
| 188 const int kCaretPositionHeight1 = 40; |
| 189 |
| 190 const int kCaretPositionX2 = 15; |
| 191 const int kCaretPositionY2 = 25; |
| 192 const int kCaretPositionWidth2 = 35; |
| 193 const int kCaretPositionHeight2 = 45; |
| 194 |
| 195 InputMethodLookupTable old_table; |
| 196 InputMethodLookupTable new_table; |
| 197 |
| 198 // State chagne from using non-mozc candidate to mozc candidate. |
| 199 ClearInputMethodLookupTable(&old_table); |
| 200 ClearInputMethodLookupTable(&new_table); |
| 201 |
| 202 old_table.candidates.push_back(kSampleCandidate1); |
| 203 InitializeMozcCandidates(&new_table); |
| 204 AppendCandidateIntoMozcCandidates(&new_table, kSampleCandidate1); |
| 205 SetCaretRectIntoMozcCandidates(&new_table, |
| 206 mozc::commands::Candidates::COMPOSITION, |
| 207 kCaretPositionX1, |
| 208 kCaretPositionY1, |
| 209 kCaretPositionWidth1, |
| 210 kCaretPositionHeight1); |
| 211 |
| 212 EXPECT_TRUE(CandidateWindowView::ShouldUpdateCandidateViews(old_table, |
| 213 new_table)); |
| 214 |
| 215 // State change from using mozc candidate to non-mozc candidate |
| 216 ClearInputMethodLookupTable(&old_table); |
| 217 ClearInputMethodLookupTable(&new_table); |
| 218 |
| 219 InitializeMozcCandidates(&old_table); |
| 220 AppendCandidateIntoMozcCandidates(&old_table, kSampleCandidate1); |
| 221 SetCaretRectIntoMozcCandidates(&old_table, |
| 222 mozc::commands::Candidates::COMPOSITION, |
| 223 kCaretPositionX1, |
| 224 kCaretPositionY1, |
| 225 kCaretPositionWidth1, |
| 226 kCaretPositionHeight1); |
| 227 |
| 228 new_table.candidates.push_back(kSampleCandidate1); |
| 229 |
| 230 EXPECT_TRUE(CandidateWindowView::ShouldUpdateCandidateViews(old_table, |
| 231 new_table)); |
| 232 |
| 233 // State change from using mozc candidate to mozc candidate |
| 234 |
| 235 // No change |
| 236 ClearInputMethodLookupTable(&old_table); |
| 237 ClearInputMethodLookupTable(&new_table); |
| 238 |
| 239 InitializeMozcCandidates(&old_table); |
| 240 AppendCandidateIntoMozcCandidates(&old_table, kSampleCandidate1); |
| 241 SetCaretRectIntoMozcCandidates(&old_table, |
| 242 mozc::commands::Candidates::COMPOSITION, |
| 243 kCaretPositionX1, |
| 244 kCaretPositionY1, |
| 245 kCaretPositionWidth1, |
| 246 kCaretPositionHeight1); |
| 247 |
| 248 InitializeMozcCandidates(&new_table); |
| 249 AppendCandidateIntoMozcCandidates(&new_table, kSampleCandidate1); |
| 250 SetCaretRectIntoMozcCandidates(&new_table, |
| 251 mozc::commands::Candidates::COMPOSITION, |
| 252 kCaretPositionX1, |
| 253 kCaretPositionY1, |
| 254 kCaretPositionWidth1, |
| 255 kCaretPositionHeight1); |
| 256 |
| 257 EXPECT_FALSE(CandidateWindowView::ShouldUpdateCandidateViews(old_table, |
| 258 new_table)); |
| 259 // Position change only |
| 260 ClearInputMethodLookupTable(&old_table); |
| 261 ClearInputMethodLookupTable(&new_table); |
| 262 |
| 263 InitializeMozcCandidates(&old_table); |
| 264 AppendCandidateIntoMozcCandidates(&old_table, kSampleCandidate1); |
| 265 SetCaretRectIntoMozcCandidates(&old_table, |
| 266 mozc::commands::Candidates::COMPOSITION, |
| 267 kCaretPositionX1, |
| 268 kCaretPositionY1, |
| 269 kCaretPositionWidth1, |
| 270 kCaretPositionHeight1); |
| 271 |
| 272 InitializeMozcCandidates(&new_table); |
| 273 AppendCandidateIntoMozcCandidates(&new_table, kSampleCandidate1); |
| 274 SetCaretRectIntoMozcCandidates(&new_table, |
| 275 mozc::commands::Candidates::COMPOSITION, |
| 276 kCaretPositionX2, |
| 277 kCaretPositionY2, |
| 278 kCaretPositionWidth2, |
| 279 kCaretPositionHeight2); |
| 280 |
| 281 EXPECT_TRUE(CandidateWindowView::ShouldUpdateCandidateViews(old_table, |
| 282 new_table)); |
| 283 // Candidate contents only |
| 284 ClearInputMethodLookupTable(&old_table); |
| 285 ClearInputMethodLookupTable(&new_table); |
| 286 |
| 287 InitializeMozcCandidates(&old_table); |
| 288 AppendCandidateIntoMozcCandidates(&old_table, kSampleCandidate1); |
| 289 SetCaretRectIntoMozcCandidates(&old_table, |
| 290 mozc::commands::Candidates::COMPOSITION, |
| 291 kCaretPositionX1, |
| 292 kCaretPositionY1, |
| 293 kCaretPositionWidth1, |
| 294 kCaretPositionHeight1); |
| 295 |
| 296 InitializeMozcCandidates(&new_table); |
| 297 AppendCandidateIntoMozcCandidates(&new_table, kSampleCandidate2); |
| 298 SetCaretRectIntoMozcCandidates(&new_table, |
| 299 mozc::commands::Candidates::COMPOSITION, |
| 300 kCaretPositionX1, |
| 301 kCaretPositionY1, |
| 302 kCaretPositionWidth1, |
| 303 kCaretPositionHeight1); |
| 304 |
| 305 EXPECT_TRUE(CandidateWindowView::ShouldUpdateCandidateViews(old_table, |
| 306 new_table)); |
| 307 |
| 308 // Both candidate and position |
| 309 ClearInputMethodLookupTable(&old_table); |
| 310 ClearInputMethodLookupTable(&new_table); |
| 311 |
| 312 InitializeMozcCandidates(&old_table); |
| 313 AppendCandidateIntoMozcCandidates(&old_table, kSampleCandidate1); |
| 314 SetCaretRectIntoMozcCandidates(&old_table, |
| 315 mozc::commands::Candidates::COMPOSITION, |
| 316 kCaretPositionX1, |
| 317 kCaretPositionY1, |
| 318 kCaretPositionWidth1, |
| 319 kCaretPositionHeight1); |
| 320 |
| 321 InitializeMozcCandidates(&new_table); |
| 322 AppendCandidateIntoMozcCandidates(&new_table, kSampleCandidate2); |
| 323 SetCaretRectIntoMozcCandidates(&new_table, |
| 324 mozc::commands::Candidates::COMPOSITION, |
| 325 kCaretPositionX2, |
| 326 kCaretPositionY2, |
| 327 kCaretPositionWidth2, |
| 328 kCaretPositionHeight2); |
| 329 |
| 330 EXPECT_TRUE(CandidateWindowView::ShouldUpdateCandidateViews(old_table, |
| 331 new_table)); |
| 332 } |
| 333 |
122 } // namespace input_method | 334 } // namespace input_method |
123 } // namespace chromeos | 335 } // namespace chromeos |
OLD | NEW |