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 // Test of handwriting module manager
31 
32 #include "handwriting/handwriting_manager.h"
33 
34 #include <algorithm>
35 #include <string>
36 #include <vector>
37 
38 #include "base/logging.h"
39 #include "testing/base/public/googletest.h"
40 #include "testing/base/public/gunit.h"
41 
42 namespace mozc {
43 namespace handwriting {
44 namespace {
45 class MockHandwriting : public HandwritingInterface {
46  public:
MockHandwriting()47   MockHandwriting()
48       : commit_counter_(0),
49         return_status_(HANDWRITING_NO_ERROR) {
50   }
51 
Recognize(const Strokes & unused_strokes,std::vector<string> * candidates) const52   virtual HandwritingStatus Recognize(const Strokes &unused_strokes,
53                                       std::vector<string> *candidates) const {
54     CHECK(candidates);
55     candidates->clear();
56     for (size_t i = 0; i < candidates_.size(); ++i) {
57       candidates->push_back(candidates_[i]);
58     }
59     return return_status_;
60   }
61 
Commit(const Strokes & unused_strokes,const string & unused_result)62   virtual HandwritingStatus Commit(const Strokes &unused_strokes,
63                                    const string &unused_result) {
64     ++commit_counter_;
65     return return_status_;
66   }
67 
SetCandidates(const std::vector<string> & candidates)68   void SetCandidates(const std::vector<string> &candidates) {
69     candidates_.clear();
70     for (size_t i = 0; i < candidates.size(); ++i) {
71       candidates_.push_back(candidates[i]);
72     }
73   }
74 
GetCommitCounter()75   int GetCommitCounter() {
76     return commit_counter_;
77   }
78 
ClearCommitCounter()79   void ClearCommitCounter() {
80     commit_counter_ = 0;
81   }
82 
SetReturnStatus(HandwritingStatus status)83   void SetReturnStatus(HandwritingStatus status) {
84     return_status_ = status;
85   }
86 
87  private:
88   std::vector<string> candidates_;
89   int commit_counter_;
90   HandwritingStatus return_status_;
91 };
92 }  // namespace
93 
94 class HandwritingManagerTest : public testing::Test {
95  public:
SetUp()96   virtual void SetUp() {
97     HandwritingManager::SetHandwritingModule(&mock_handwriting_);
98   }
99 
TearDown()100   virtual void TearDown() {
101   }
102 
103  protected:
104   MockHandwriting mock_handwriting_;
105 };
106 
TEST_F(HandwritingManagerTest,Recognize)107 TEST_F(HandwritingManagerTest, Recognize) {
108   std::vector<string> expected_candidates;
109   expected_candidates.push_back("foo");
110   expected_candidates.push_back("bar");
111   expected_candidates.push_back("baz");
112   mock_handwriting_.SetCandidates(expected_candidates);
113 
114   std::vector<string> result;
115   Strokes dummy_strokes;
116   EXPECT_EQ(HANDWRITING_NO_ERROR,
117             HandwritingManager::Recognize(dummy_strokes, &result));
118   EXPECT_EQ(expected_candidates.size(), result.size());
119   for (size_t i = 0; i < expected_candidates.size(); ++i) {
120     EXPECT_EQ(expected_candidates[i], result[i])
121         << i << "-th candidate mismatch";
122   }
123 }
124 
TEST_F(HandwritingManagerTest,Commit)125 TEST_F(HandwritingManagerTest, Commit) {
126   mock_handwriting_.ClearCommitCounter();
127   EXPECT_EQ(0, mock_handwriting_.GetCommitCounter());
128 
129   Strokes dummy_strokes;
130   string dummy_result;
131   EXPECT_EQ(HANDWRITING_NO_ERROR,
132             HandwritingManager::Commit(dummy_strokes, dummy_result));
133   EXPECT_EQ(1, mock_handwriting_.GetCommitCounter());
134 }
135 
TEST_F(HandwritingManagerTest,RecognizeError)136 TEST_F(HandwritingManagerTest, RecognizeError) {
137   std::vector<string> result;
138   Strokes dummy_strokes;
139   mock_handwriting_.SetReturnStatus(HANDWRITING_ERROR);
140   EXPECT_EQ(HANDWRITING_ERROR,
141             HandwritingManager::Recognize(dummy_strokes, &result));
142   mock_handwriting_.SetReturnStatus(HANDWRITING_NETWORK_ERROR);
143   EXPECT_EQ(HANDWRITING_NETWORK_ERROR,
144             HandwritingManager::Recognize(dummy_strokes, &result));
145 }
146 
TEST_F(HandwritingManagerTest,CommitError)147 TEST_F(HandwritingManagerTest, CommitError) {
148   Strokes dummy_strokes;
149   string dummy_result;
150   mock_handwriting_.SetReturnStatus(HANDWRITING_ERROR);
151   EXPECT_EQ(HANDWRITING_ERROR,
152             HandwritingManager::Commit(dummy_strokes, dummy_result));
153   mock_handwriting_.SetReturnStatus(HANDWRITING_NETWORK_ERROR);
154   EXPECT_EQ(HANDWRITING_NETWORK_ERROR,
155             HandwritingManager::Commit(dummy_strokes, dummy_result));
156 }
157 }  // namespace handwriting
158 }  // namespace mozc
159