1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "session/output_util.h"
31 
32 #include "base/port.h"
33 #include "protocol/commands.pb.h"
34 #include "testing/base/public/googletest.h"
35 #include "testing/base/public/gunit.h"
36 
37 namespace mozc {
38 namespace {
39 
40 // TODO(yukawa): Add a deserialize method to OutputUtil so that we can use
41 //   text representation of the commands::Output to set up test data.
SetTestDataForConversion(commands::Output * output)42 void SetTestDataForConversion(commands::Output *output) {
43   output->set_mode(commands::HIRAGANA);
44   output->set_consumed(true);
45   {
46     commands::Preedit *preedit = output->mutable_preedit();
47     preedit->set_cursor(10);
48     {
49       commands::Preedit::Segment *segment = preedit->add_segment();
50       segment->set_annotation(commands::Preedit::Segment::UNDERLINE);
51       segment->set_value("Alpha");
52       segment->set_value_length(5);
53       segment->set_key("あるふぁ");
54     }
55     {
56       commands::Preedit::Segment *segment = preedit->add_segment();
57       segment->set_annotation(commands::Preedit::Segment::HIGHLIGHT);
58       segment->set_value("be-ta");
59       segment->set_value_length(5);
60       segment->set_key("べーた");
61     }
62     preedit->set_highlighted_position(5);
63   }
64   {
65     commands::Candidates *candidates = output->mutable_candidates();
66     candidates->set_focused_index(8);
67     candidates->set_size(9);
68     {
69       commands::Candidates::Candidate *candidate = candidates->add_candidate();
70       candidate->set_index(0);
71       candidate->set_value("BETA");
72       {
73         commands::Annotation *annotation = candidate->mutable_annotation();
74         annotation->set_description("[半] アルファベット");
75         annotation->set_shortcut("1");
76       }
77       candidate->set_id(0);
78     }
79     {
80       commands::Candidates::Candidate *candidate = candidates->add_candidate();
81       candidate->set_index(1);
82       candidate->set_value("ベータ");
83       {
84         commands::Annotation *annotation = candidate->mutable_annotation();
85         annotation->set_description("[半] カタカナ");
86         annotation->set_shortcut("2");
87       }
88       candidate->set_id(1);
89     }
90     {
91       commands::Candidates::Candidate *candidate = candidates->add_candidate();
92       candidate->set_index(2);
93       candidate->set_value("beta");
94       {
95         commands::Annotation *annotation = candidate->mutable_annotation();
96         annotation->set_description("[半] アルファベット");
97         annotation->set_shortcut("3");
98       }
99       candidate->set_id(2);
100     }
101     {
102       commands::Candidates::Candidate *candidate = candidates->add_candidate();
103       candidate->set_index(3);
104       candidate->set_value("β");
105       {
106         commands::Annotation *annotation = candidate->mutable_annotation();
107         annotation->set_description("ギリシャ文字(小文字)");
108         annotation->set_shortcut("4");
109       }
110       candidate->set_id(3);
111     }
112     {
113       commands::Candidates::Candidate *candidate = candidates->add_candidate();
114       candidate->set_index(4);
115       candidate->set_value("Β");
116       {
117         commands::Annotation *annotation = candidate->mutable_annotation();
118         annotation->set_description("ギリシャ文字(大文字)");
119         annotation->set_shortcut("5");
120       }
121       candidate->set_id(4);
122     }
123     {
124       commands::Candidates::Candidate *candidate = candidates->add_candidate();
125       candidate->set_index(5);
126       candidate->set_value("㌼");
127       {
128         commands::Annotation *annotation = candidate->mutable_annotation();
129         annotation->set_description("<機種依存文字>");
130         annotation->set_shortcut("6");
131       }
132       candidate->set_id(5);
133     }
134     {
135       commands::Candidates::Candidate *candidate = candidates->add_candidate();
136       candidate->set_index(6);
137       candidate->set_value("Beta");
138       {
139         commands::Annotation *annotation = candidate->mutable_annotation();
140         annotation->set_description("[半] アルファベット");
141         annotation->set_shortcut("7");
142       }
143       candidate->set_id(6);
144     }
145     {
146       commands::Candidates::Candidate *candidate = candidates->add_candidate();
147       candidate->set_index(7);
148       candidate->set_value("べーた");
149       {
150         commands::Annotation *annotation = candidate->mutable_annotation();
151         annotation->set_description("ひらがな");
152         annotation->set_shortcut("8");
153       }
154       candidate->set_id(7);
155     }
156     {
157       commands::Candidates::Candidate *candidate = candidates->add_candidate();
158       candidate->set_index(8);
159       candidate->set_value("そのほかの文字種");
160       {
161         commands::Annotation *annotation = candidate->mutable_annotation();
162         annotation->set_shortcut("9");
163       }
164       candidate->set_id(-3);
165     }
166     candidates->set_position(5);
167     {
168       commands::Candidates *sub_candidates =
169           candidates->mutable_subcandidates();
170       sub_candidates->set_focused_index(2);
171       sub_candidates->set_size(5);
172       {
173         {
174           commands::Candidates::Candidate *candidate =
175               sub_candidates->add_candidate();
176           candidate->set_index(0);
177           candidate->set_value("べーた");
178           {
179             commands::Annotation *annotation = candidate->mutable_annotation();
180             annotation->set_description("ひらがな");
181           }
182           candidate->set_id(-1);
183         }
184         {
185           commands::Candidates::Candidate *candidate =
186               sub_candidates->add_candidate();
187           candidate->set_index(1);
188           candidate->set_value("ベータ");
189           {
190             commands::Annotation *annotation = candidate->mutable_annotation();
191             annotation->set_description("[半] カタカナ");
192           }
193           candidate->set_id(-2);
194         }
195         {
196           commands::Candidates::Candidate *candidate =
197               sub_candidates->add_candidate();
198           candidate->set_index(2);
199           candidate->set_value("be-ta");
200           {
201             commands::Annotation *annotation = candidate->mutable_annotation();
202             annotation->set_description("[半]");
203           }
204           candidate->set_id(-3);
205         }
206         {
207           commands::Candidates::Candidate *candidate =
208               sub_candidates->add_candidate();
209           candidate->set_index(3);
210           candidate->set_value("be-ta");
211           {
212             commands::Annotation *annotation = candidate->mutable_annotation();
213             annotation->set_description("[全]");
214           }
215           candidate->set_id(-7);
216         }
217         {
218           commands::Candidates::Candidate *candidate =
219               sub_candidates->add_candidate();
220           candidate->set_index(4);
221           candidate->set_value("ベータ");
222           {
223             commands::Annotation *annotation = candidate->mutable_annotation();
224             annotation->set_description("[半] カタカナ");
225           }
226           candidate->set_id(-11);
227         }
228       }
229       sub_candidates->set_position(8);
230       sub_candidates->set_category(commands::TRANSLITERATION);
231       sub_candidates->set_display_type(commands::CASCADE);
232     }
233     candidates->set_category(commands::CONVERSION);
234     candidates->set_display_type(commands::MAIN);
235     {
236       commands::Footer *footer = candidates->mutable_footer();
237       footer->set_index_visible(true);
238       footer->set_logo_visible(true);
239       footer->set_sub_label("build 436");
240     }
241   }
242   {
243     commands::Status *status = output->mutable_status();
244     status->set_activated(true);
245     status->set_mode(commands::HIRAGANA);
246     status->set_comeback_mode(commands::HIRAGANA);
247   }
248   {
249     commands::CandidateList *candidate_list =
250         output->mutable_all_candidate_words();
251     candidate_list->set_focused_index(10);
252     {
253       commands::CandidateWord *candidate = candidate_list->add_candidates();
254       candidate->set_id(0);
255       candidate->set_index(0);
256       candidate->set_value("Beta");
257     }
258     {
259       commands::CandidateWord *candidate = candidate_list->add_candidates();
260       candidate->set_id(1);
261       candidate->set_index(1);
262       candidate->set_value("ベータ");
263     }
264     {
265       commands::CandidateWord *candidate = candidate_list->add_candidates();
266       candidate->set_id(2);
267       candidate->set_index(2);
268       candidate->set_value("BETA");
269     }
270     {
271       commands::CandidateWord *candidate = candidate_list->add_candidates();
272       candidate->set_id(3);
273       candidate->set_index(3);
274       candidate->set_value("beta");
275     }
276     {
277       commands::CandidateWord *candidate = candidate_list->add_candidates();
278       candidate->set_id(4);
279       candidate->set_index(4);
280       candidate->set_value("β");
281     }
282     {
283       commands::CandidateWord *candidate = candidate_list->add_candidates();
284       candidate->set_id(5);
285       candidate->set_index(5);
286       candidate->set_value("Β");
287     }
288     {
289       commands::CandidateWord *candidate = candidate_list->add_candidates();
290       candidate->set_id(6);
291       candidate->set_index(6);
292       candidate->set_value("㌼");
293     }
294     {
295       commands::CandidateWord *candidate = candidate_list->add_candidates();
296       candidate->set_id(7);
297       candidate->set_index(7);
298       candidate->set_value("べーた");
299     }
300     {
301       commands::CandidateWord *candidate = candidate_list->add_candidates();
302       candidate->set_id(-1);
303       candidate->set_index(8);
304       candidate->set_value("べーた");
305     }
306     {
307       commands::CandidateWord *candidate = candidate_list->add_candidates();
308       candidate->set_id(-2);
309       candidate->set_index(9);
310       candidate->set_value("ベータ");
311     }
312     {
313       commands::CandidateWord *candidate = candidate_list->add_candidates();
314       candidate->set_id(-3);
315       candidate->set_index(10);
316       candidate->set_value("be-ta");
317     }
318     {
319       commands::CandidateWord *candidate = candidate_list->add_candidates();
320       candidate->set_id(-7);
321       candidate->set_index(11);
322       candidate->set_value("be-ta");
323     }
324     {
325       commands::CandidateWord *candidate = candidate_list->add_candidates();
326       candidate->set_id(-11);
327       candidate->set_index(12);
328       candidate->set_value("ベータ");
329     }
330     candidate_list->set_category(commands::CONVERSION);
331   }
332 }
333 
TEST(OutputUtilTest,GetCandidateIndexById)334 TEST(OutputUtilTest, GetCandidateIndexById) {
335   commands::Output output;
336   SetTestDataForConversion(&output);
337 
338   // Existing ID
339   int32 candidate_index = 0;
340   EXPECT_TRUE(OutputUtil::GetCandidateIndexById(output, -2, &candidate_index));
341   EXPECT_EQ(9, candidate_index);
342 
343   // Not existing ID.
344   candidate_index = 0;
345   EXPECT_FALSE(OutputUtil::GetCandidateIndexById(
346       output, 100, &candidate_index));
347 }
348 
TEST(OutputUtilTest,GetCandidateIdByIndex)349 TEST(OutputUtilTest, GetCandidateIdByIndex) {
350   commands::Output output;
351   SetTestDataForConversion(&output);
352 
353   // Existing index
354   int32 candidate_id = 0;
355   EXPECT_TRUE(OutputUtil::GetCandidateIdByIndex(output, 9, &candidate_id));
356   EXPECT_EQ(-2, candidate_id);
357 
358   // Not existing index.
359   candidate_id = 0;
360   EXPECT_FALSE(OutputUtil::GetCandidateIdByIndex(
361       output, 100, &candidate_id));
362 }
363 
TEST(OutputUtilTest,GetFocusedCandidateId)364 TEST(OutputUtilTest, GetFocusedCandidateId) {
365   commands::Output output;
366   SetTestDataForConversion(&output);
367 
368   int32 candidate_id = 0;
369   EXPECT_TRUE(OutputUtil::GetFocusedCandidateId(output, &candidate_id));
370   EXPECT_EQ(-3, candidate_id);
371 }
372 
373 }  // namespace
374 }  // namespace mozc
375