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 "converter/converter_mock.h"
31 
32 #include <memory>
33 #include <string>
34 
35 #include "converter/segments.h"
36 #include "request/conversion_request.h"
37 #include "testing/base/public/googletest.h"
38 #include "testing/base/public/gunit.h"
39 
40 namespace mozc {
41 namespace {
42 
SetSegments(Segments * segments,const string & cand_value)43 void SetSegments(Segments *segments, const string &cand_value) {
44   Segment *segment = segments->add_segment();
45   segment->set_key("Testてすと");
46   Segment::Candidate *candidate = segment->add_candidate();
47   candidate->value = cand_value;
48 
49   // Add meta candidates
50   Segment::Candidate *meta_cand = segment->add_meta_candidate();
51   meta_cand->Init();
52   meta_cand->value = "TestT13N";
53 }
54 
55 class ConverterMockTest : public ::testing::Test {
56  protected:
SetUp()57   void SetUp() override {
58     mock_.reset(new ConverterMock);
59   }
60 
GetMock()61   ConverterMock *GetMock() {
62     return mock_.get();
63   }
64 
65  private:
66   std::unique_ptr<ConverterMock> mock_;
67 };
68 
TEST_F(ConverterMockTest,CopySegment)69 TEST_F(ConverterMockTest, CopySegment) {
70   ConverterInterface *converter = GetMock();
71 
72   Segments output, expect;
73   SetSegments(&expect, "StartConvert");
74   GetMock()->SetStartConversion(&expect, true);
75   EXPECT_TRUE(converter->StartConversion(&output, "dummy"));
76   EXPECT_EQ(expect.DebugString(), output.DebugString());
77   EXPECT_EQ(1, output.segments_size());
78   const Segment &seg = output.segment(0);
79   EXPECT_EQ("Testてすと", seg.key());
80   EXPECT_EQ(1, seg.candidates_size());
81   EXPECT_EQ("StartConvert", seg.candidate(0).value);
82   EXPECT_EQ(1, seg.meta_candidates_size());
83   EXPECT_EQ("TestT13N", seg.meta_candidate(0).value);
84 }
85 
TEST_F(ConverterMockTest,SetStartConversion)86 TEST_F(ConverterMockTest, SetStartConversion) {
87   ConverterInterface *converter = GetMock();
88 
89   Segments output, expect;
90   SetSegments(&expect, "StartConversion");
91   GetMock()->SetStartConversion(&expect, true);
92   EXPECT_TRUE(converter->StartConversion(&output, "dummy"));
93   EXPECT_EQ(expect.DebugString(), output.DebugString());
94 }
95 
TEST_F(ConverterMockTest,SetStartReverseConvert)96 TEST_F(ConverterMockTest, SetStartReverseConvert) {
97   ConverterInterface *converter = GetMock();
98 
99   Segments output, expect;
100   SetSegments(&expect, "StartReverseConvert");
101   GetMock()->SetStartReverseConversion(&expect, true);
102   EXPECT_TRUE(converter->StartReverseConversion(&output, "dummy"));
103   EXPECT_EQ(expect.DebugString(), output.DebugString());
104 }
105 
TEST_F(ConverterMockTest,SetStartPrediction)106 TEST_F(ConverterMockTest, SetStartPrediction) {
107   ConverterInterface *converter = GetMock();
108 
109   Segments output, expect;
110   SetSegments(&expect, "StartPrediction");
111   GetMock()->SetStartPrediction(&expect, true);
112   EXPECT_TRUE(converter->StartPrediction(&output, "dummy"));
113   EXPECT_EQ(expect.DebugString(), output.DebugString());
114 }
115 
TEST_F(ConverterMockTest,SetStartSuggestion)116 TEST_F(ConverterMockTest, SetStartSuggestion) {
117   ConverterInterface *converter = GetMock();
118 
119   Segments output, expect;
120   SetSegments(&expect, "StartSuggestion");
121   GetMock()->SetStartSuggestion(&expect, true);
122   EXPECT_TRUE(converter->StartSuggestion(&output, "dummy"));
123   EXPECT_EQ(expect.DebugString(), output.DebugString());
124 }
125 
TEST_F(ConverterMockTest,SetStartPartialPrediction)126 TEST_F(ConverterMockTest, SetStartPartialPrediction) {
127   ConverterInterface *converter = GetMock();
128 
129   Segments output, expect;
130   SetSegments(&expect, "StartPartialPrediction");
131   GetMock()->SetStartPartialPrediction(&expect, true);
132   EXPECT_TRUE(converter->StartPartialPrediction(&output, "dummy"));
133   EXPECT_EQ(expect.DebugString(), output.DebugString());
134 }
135 
TEST_F(ConverterMockTest,SetStartPartialSuggestion)136 TEST_F(ConverterMockTest, SetStartPartialSuggestion) {
137   ConverterInterface *converter = GetMock();
138 
139   Segments output, expect;
140   SetSegments(&expect, "StartPartialSuggestion");
141   GetMock()->SetStartPartialSuggestion(&expect, true);
142   EXPECT_TRUE(converter->StartPartialSuggestion(&output, "dummy"));
143   EXPECT_EQ(expect.DebugString(), output.DebugString());
144 }
145 
TEST_F(ConverterMockTest,SetFinishConversion)146 TEST_F(ConverterMockTest, SetFinishConversion) {
147   ConverterInterface *converter = GetMock();
148 
149   Segments output, expect;
150   SetSegments(&expect, "FinishConversion");
151   GetMock()->SetFinishConversion(&expect, true);
152   const ConversionRequest default_request;
153   EXPECT_TRUE(converter->FinishConversion(default_request, &output));
154   EXPECT_EQ(expect.DebugString(), output.DebugString());
155 }
156 
TEST_F(ConverterMockTest,SetCancelConversion)157 TEST_F(ConverterMockTest, SetCancelConversion) {
158   ConverterInterface *converter = GetMock();
159 
160   Segments output, expect;
161   SetSegments(&expect, "CancelConversion");
162   GetMock()->SetCancelConversion(&expect, true);
163   EXPECT_TRUE(converter->CancelConversion(&output));
164   EXPECT_EQ(expect.DebugString(), output.DebugString());
165 }
166 
TEST_F(ConverterMockTest,SetResetConversion)167 TEST_F(ConverterMockTest, SetResetConversion) {
168   ConverterInterface *converter = GetMock();
169 
170   Segments output, expect;
171   SetSegments(&expect, "ResetConversion");
172   GetMock()->SetResetConversion(&expect, true);
173   EXPECT_TRUE(converter->ResetConversion(&output));
174   EXPECT_EQ(expect.DebugString(), output.DebugString());
175 }
176 
TEST_F(ConverterMockTest,SetCommitSegmentValue)177 TEST_F(ConverterMockTest, SetCommitSegmentValue) {
178   ConverterInterface *converter = GetMock();
179 
180   Segments output, expect;
181   SetSegments(&expect, "CommitSegmentValue");
182   GetMock()->SetCommitSegmentValue(&expect, true);
183   EXPECT_TRUE(converter->CommitSegmentValue(&output, 1, 10));
184   EXPECT_EQ(expect.DebugString(), output.DebugString());
185 }
186 
TEST_F(ConverterMockTest,SetFocusSegmentValue)187 TEST_F(ConverterMockTest, SetFocusSegmentValue) {
188   ConverterInterface *converter = GetMock();
189 
190   Segments output, expect;
191   SetSegments(&expect, "FocusSegmentValue");
192   GetMock()->SetFocusSegmentValue(&expect, true);
193   EXPECT_TRUE(converter->FocusSegmentValue(&output, 1, 10));
194   EXPECT_EQ(expect.DebugString(), output.DebugString());
195 }
196 
TEST_F(ConverterMockTest,SetFreeSegmentValue)197 TEST_F(ConverterMockTest, SetFreeSegmentValue) {
198   ConverterInterface *converter = GetMock();
199 
200   Segments output, expect;
201   SetSegments(&expect, "FreeSegmentValue");
202   GetMock()->SetFreeSegmentValue(&expect, true);
203   EXPECT_TRUE(converter->FreeSegmentValue(&output, 1));
204   EXPECT_EQ(expect.DebugString(), output.DebugString());
205 }
206 
TEST_F(ConverterMockTest,SetCommitSegments)207 TEST_F(ConverterMockTest, SetCommitSegments) {
208   ConverterInterface *converter = GetMock();
209 
210   Segments output, expect;
211   SetSegments(&expect, "CommitSegments");
212   GetMock()->SetCommitSegments(&expect, true);
213   std::vector<size_t> singleton_vector;
214   singleton_vector.push_back(1);
215   EXPECT_TRUE(converter->CommitSegments(&output, singleton_vector));
216   EXPECT_EQ(expect.DebugString(), output.DebugString());
217 }
218 
TEST_F(ConverterMockTest,SetResizeSegment1)219 TEST_F(ConverterMockTest, SetResizeSegment1) {
220   ConverterInterface *converter = GetMock();
221 
222   Segments output, expect;
223   SetSegments(&expect, "ResizeSegment1");
224   GetMock()->SetResizeSegment1(&expect, true);
225   const ConversionRequest default_request;
226   EXPECT_TRUE(converter->ResizeSegment(&output, default_request, 1, 5));
227   EXPECT_EQ(expect.DebugString(), output.DebugString());
228 }
229 
TEST_F(ConverterMockTest,SetResizeSegment2)230 TEST_F(ConverterMockTest, SetResizeSegment2) {
231   ConverterInterface *converter = GetMock();
232 
233   Segments output, expect;
234   SetSegments(&expect, "ResizeSegment2");
235   GetMock()->SetResizeSegment2(&expect, true);
236   uint8 size_array[] = {1, 2, 3};
237   const ConversionRequest default_request;
238   EXPECT_TRUE(converter->ResizeSegment(&output, default_request, 1, 5,
239                                        size_array, arraysize(size_array)));
240   EXPECT_EQ(expect.DebugString(), output.DebugString());
241 }
TEST_F(ConverterMockTest,GetStartConversion)242 TEST_F(ConverterMockTest, GetStartConversion) {
243   ConverterInterface *converter = GetMock();
244 
245   Segments input;
246   const string input_key = "Key";
247   SetSegments(&input, "StartConversion");
248   const string input_str = input.DebugString();
249   converter->StartConversion(&input, input_key);
250 
251   Segments last_segment;
252   string last_key;
253   GetMock()->GetStartConversion(&last_segment, &last_key);
254   const string last_segment_str = last_segment.DebugString();
255 
256   EXPECT_EQ(input_str, last_segment_str);
257   EXPECT_EQ(input_key, last_key);
258 }
259 
TEST_F(ConverterMockTest,GetStartReverseConversion)260 TEST_F(ConverterMockTest, GetStartReverseConversion) {
261   ConverterInterface *converter = GetMock();
262 
263   Segments input;
264   const string input_key = "Key";
265   SetSegments(&input, "StartReverseConversion");
266   const string input_str = input.DebugString();
267   converter->StartReverseConversion(&input, input_key);
268 
269   Segments last_segment;
270   string last_key;
271   GetMock()->GetStartReverseConversion(&last_segment, &last_key);
272   const string last_segment_str = last_segment.DebugString();
273 
274   EXPECT_EQ(input_str, last_segment_str);
275   EXPECT_EQ(input_key, last_key);
276 }
277 
TEST_F(ConverterMockTest,GetStartPrediction)278 TEST_F(ConverterMockTest, GetStartPrediction) {
279   ConverterInterface *converter = GetMock();
280 
281   Segments input;
282   const string input_key = "Key";
283   SetSegments(&input, "StartPrediction");
284   const string input_str = input.DebugString();
285   converter->StartPrediction(&input, input_key);
286 
287   Segments last_segment;
288   string last_key;
289   GetMock()->GetStartPrediction(&last_segment, &last_key);
290   const string last_segment_str = last_segment.DebugString();
291 
292   EXPECT_EQ(input_str, last_segment_str);
293   EXPECT_EQ(input_key, last_key);
294 }
295 
TEST_F(ConverterMockTest,GetStartSuggestion)296 TEST_F(ConverterMockTest, GetStartSuggestion) {
297   ConverterInterface *converter = GetMock();
298 
299   Segments input;
300   const string input_key = "Key";
301   SetSegments(&input, "StartSuggestion");
302   const string input_str = input.DebugString();
303   converter->StartSuggestion(&input, input_key);
304 
305   Segments last_segment;
306   string last_key;
307   GetMock()->GetStartSuggestion(&last_segment, &last_key);
308   const string last_segment_str = last_segment.DebugString();
309 
310   EXPECT_EQ(input_str, last_segment_str);
311   EXPECT_EQ(input_key, last_key);
312 }
313 
TEST_F(ConverterMockTest,GetStartPartialPrediction)314 TEST_F(ConverterMockTest, GetStartPartialPrediction) {
315   ConverterInterface *converter = GetMock();
316 
317   Segments input;
318   const string input_key = "Key";
319   SetSegments(&input, "StartPartialPrediction");
320   const string input_str = input.DebugString();
321   converter->StartPartialPrediction(&input, input_key);
322 
323   Segments last_segment;
324   string last_key;
325   GetMock()->GetStartPartialPrediction(&last_segment, &last_key);
326   const string last_segment_str = last_segment.DebugString();
327 
328   EXPECT_EQ(input_str, last_segment_str);
329   EXPECT_EQ(input_key, last_key);
330 }
331 
TEST_F(ConverterMockTest,GetStartPartialSuggestion)332 TEST_F(ConverterMockTest, GetStartPartialSuggestion) {
333   ConverterInterface *converter = GetMock();
334 
335   Segments input;
336   const string input_key = "Key";
337   SetSegments(&input, "StartPartialSuggestion");
338   const string input_str = input.DebugString();
339   converter->StartPartialSuggestion(&input, input_key);
340 
341   Segments last_segment;
342   string last_key;
343   GetMock()->GetStartPartialSuggestion(&last_segment, &last_key);
344   const string last_segment_str = last_segment.DebugString();
345 
346   EXPECT_EQ(input_str, last_segment_str);
347   EXPECT_EQ(input_key, last_key);
348 }
349 
TEST_F(ConverterMockTest,GetFinishConversion)350 TEST_F(ConverterMockTest, GetFinishConversion) {
351   ConverterInterface *converter = GetMock();
352 
353   Segments input;
354   SetSegments(&input, "FinishConversion");
355   const string input_str = input.DebugString();
356   ConversionRequest default_request;
357   converter->FinishConversion(default_request, &input);
358 
359   Segments last_segment;
360   GetMock()->GetFinishConversion(&last_segment);
361   const string last_segment_str = last_segment.DebugString();
362 
363   EXPECT_EQ(input_str, last_segment_str);
364 }
365 
TEST_F(ConverterMockTest,GetCancelConversion)366 TEST_F(ConverterMockTest, GetCancelConversion) {
367   ConverterInterface *converter = GetMock();
368 
369   Segments input;
370   SetSegments(&input, "CancelConversion");
371   const string input_str = input.DebugString();
372   converter->CancelConversion(&input);
373 
374   Segments last_segment;
375   GetMock()->GetCancelConversion(&last_segment);
376   const string last_segment_str = last_segment.DebugString();
377 
378   EXPECT_EQ(input_str, last_segment_str);
379 }
380 
TEST_F(ConverterMockTest,GetResetConversion)381 TEST_F(ConverterMockTest, GetResetConversion) {
382   ConverterInterface *converter = GetMock();
383 
384   Segments input;
385   SetSegments(&input, "ResetConversion");
386   const string input_str = input.DebugString();
387   converter->ResetConversion(&input);
388 
389   Segments last_segment;
390   GetMock()->GetResetConversion(&last_segment);
391   const string last_segment_str = last_segment.DebugString();
392 
393   EXPECT_EQ(input_str, last_segment_str);
394 }
395 
TEST_F(ConverterMockTest,GetCommitSegmentValue)396 TEST_F(ConverterMockTest, GetCommitSegmentValue) {
397   ConverterInterface *converter = GetMock();
398 
399   Segments input;
400   size_t input_idx = 1;
401   int input_cidx = 5;
402   SetSegments(&input, "CommitSegmentValue");
403   const string input_str = input.DebugString();
404   converter->CommitSegmentValue(&input, input_idx, input_cidx);
405 
406   Segments last_segment;
407   size_t last_idx;
408   int last_cidx;
409   GetMock()->GetCommitSegmentValue(&last_segment, &last_idx, &last_cidx);
410   const string last_segment_str = last_segment.DebugString();
411 
412   EXPECT_EQ(input_str, last_segment_str);
413   EXPECT_EQ(input_idx, last_idx);
414   EXPECT_EQ(input_cidx, last_cidx);
415 }
416 
TEST_F(ConverterMockTest,GetFocusSegmentValue)417 TEST_F(ConverterMockTest, GetFocusSegmentValue) {
418   ConverterInterface *converter = GetMock();
419 
420   Segments input;
421   size_t input_idx = 1;
422   int input_cidx = 5;
423   SetSegments(&input, "FocueSegmentValue");
424   const string input_str = input.DebugString();
425   converter->FocusSegmentValue(&input, input_idx, input_cidx);
426 
427   Segments last_segment;
428   size_t last_idx;
429   int last_cidx;
430   GetMock()->GetFocusSegmentValue(&last_segment, &last_idx, &last_cidx);
431   const string last_segment_str = last_segment.DebugString();
432 
433   EXPECT_EQ(input_str, last_segment_str);
434   EXPECT_EQ(input_idx, last_idx);
435   EXPECT_EQ(input_cidx, last_cidx);
436 }
437 
TEST_F(ConverterMockTest,GetFreeSegmentValue)438 TEST_F(ConverterMockTest, GetFreeSegmentValue) {
439   ConverterInterface *converter = GetMock();
440 
441   Segments input;
442   size_t input_idx = 1;
443   SetSegments(&input, "FreeSegmentValue");
444   const string input_str = input.DebugString();
445   converter->FreeSegmentValue(&input, input_idx);
446 
447   Segments last_segment;
448   size_t last_idx;
449   GetMock()->GetFreeSegmentValue(&last_segment, &last_idx);
450   const string last_segment_str = last_segment.DebugString();
451 
452   EXPECT_EQ(input_str, last_segment_str);
453   EXPECT_EQ(input_idx, last_idx);
454 }
455 
TEST_F(ConverterMockTest,GetCommitSegments)456 TEST_F(ConverterMockTest, GetCommitSegments) {
457   ConverterInterface *converter = GetMock();
458 
459   Segments input;
460   size_t input_idx1 = 1;
461   size_t input_idx2 = 2;
462   SetSegments(&input, "CommitSegments");
463   const string input_str = input.DebugString();
464   std::vector<size_t> index_list;
465   index_list.push_back(input_idx1);
466   index_list.push_back(input_idx2);
467   converter->CommitSegments(&input, index_list);
468 
469   Segments last_segment;
470   std::vector<size_t> last_idx;
471   GetMock()->GetCommitSegments(&last_segment, &last_idx);
472   const string last_segment_str = last_segment.DebugString();
473 
474   EXPECT_EQ(input_str, last_segment_str);
475   EXPECT_EQ(input_idx1, last_idx[0]);
476   EXPECT_EQ(input_idx2, last_idx[1]);
477 }
478 
TEST_F(ConverterMockTest,GetResizeSegment1)479 TEST_F(ConverterMockTest, GetResizeSegment1) {
480   ConverterInterface *converter = GetMock();
481 
482   Segments input;
483   size_t input_idx = 1;
484   int input_offset = 3;
485   SetSegments(&input, "ResizeSegment1");
486   const string input_str = input.DebugString();
487   const ConversionRequest default_request;
488   converter->ResizeSegment(
489       &input, default_request, input_idx, input_offset);
490 
491   Segments last_segment;
492   size_t last_idx;
493   int last_offset;
494   GetMock()->GetResizeSegment1(&last_segment, &last_idx, &last_offset);
495   const string last_segment_str = last_segment.DebugString();
496 
497   EXPECT_EQ(input_str, last_segment_str);
498   EXPECT_EQ(input_idx, last_idx);
499   EXPECT_EQ(input_offset, last_offset);
500 }
501 
TEST_F(ConverterMockTest,GetResizeSegment2)502 TEST_F(ConverterMockTest, GetResizeSegment2) {
503   ConverterInterface *converter = GetMock();
504 
505   Segments input;
506   size_t input_idx = 1, input_size = 3;
507   uint8 input_array[] = {1, 2, 3};
508   SetSegments(&input, "ResizeSegment2");
509   const string input_str = input.DebugString();
510   const ConversionRequest default_request;
511   converter->ResizeSegment(&input, default_request, input_idx, input_size,
512                            input_array, arraysize(input_array));
513 
514   Segments last_segment;
515   size_t last_idx, last_size;
516   uint8 *last_array;
517   size_t last_array_size;
518 
519   GetMock()->GetResizeSegment2(&last_segment, &last_idx, &last_size,
520                                &last_array, &last_array_size);
521   const string last_segment_str = last_segment.DebugString();
522 
523   EXPECT_EQ(input_str, last_segment_str);
524   EXPECT_EQ(input_idx, last_idx);
525   EXPECT_EQ(input_size, last_size);
526   EXPECT_EQ(arraysize(input_array), last_array_size);
527   for (int i = 0; i < arraysize(input_array); ++i) {
528     EXPECT_EQ(input_array[i], *(last_array + i));
529   }
530 }
531 
TEST_F(ConverterMockTest,DefaultBehavior)532 TEST_F(ConverterMockTest, DefaultBehavior) {
533   ConverterInterface *converter = GetMock();
534 
535   Segments input;
536   const string input_key = "Key";
537   SetSegments(&input, "StartConversion");
538   const string input_str = input.DebugString();
539   EXPECT_FALSE(converter->StartConversion(&input, input_key));
540 
541   const string last_str = input.DebugString();
542   EXPECT_EQ(input_str, last_str);
543 }
544 
545 }  // namespace
546 }  // namespace mozc
547