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 "rewriter/command_rewriter.h"
31 
32 #include <string>
33 
34 #include "base/system_util.h"
35 #include "config/config_handler.h"
36 #include "converter/segments.h"
37 #include "protocol/commands.pb.h"
38 #include "protocol/config.pb.h"
39 #include "request/conversion_request.h"
40 #include "testing/base/public/gunit.h"
41 
42 DECLARE_string(test_tmpdir);
43 
44 namespace mozc {
45 namespace {
46 
CommandCandidatesSize(const Segment & segment)47 size_t CommandCandidatesSize(const Segment &segment) {
48   size_t result = 0;
49   for (int i = 0; i < segment.candidates_size(); ++i) {
50     if (segment.candidate(i).attributes &
51         Segment::Candidate::COMMAND_CANDIDATE) {
52       result++;
53     }
54   }
55   return result;
56 }
57 
GetCommandCandidateValue(const Segment & segment)58 string GetCommandCandidateValue(const Segment &segment) {
59   for (int i = 0; i < segment.candidates_size(); ++i) {
60     if (segment.candidate(i).attributes &
61         Segment::Candidate::COMMAND_CANDIDATE) {
62       return segment.candidate(i).value;
63     }
64   }
65   return "";
66 }
67 
68 class CommandRewriterTest : public ::testing::Test {
69  protected:
CommandRewriterTest()70   CommandRewriterTest() {
71     convreq_.set_request(&request_);
72     convreq_.set_config(&config_);
73   }
74 
SetUp()75   virtual void SetUp() {
76     SystemUtil::SetUserProfileDirectory(FLAGS_test_tmpdir);
77     config::ConfigHandler::GetDefaultConfig(&config_);
78     request_.Clear();
79   }
80 
TearDown()81   virtual void TearDown() {
82     config::ConfigHandler::GetDefaultConfig(&config_);
83     request_.Clear();
84   }
85 
86   ConversionRequest convreq_;
87   commands::Request request_;
88   config::Config config_;
89 };
90 
TEST_F(CommandRewriterTest,Rewrite)91 TEST_F(CommandRewriterTest, Rewrite) {
92   CommandRewriter rewriter;
93   Segments segments;
94   Segment *seg = segments.push_back_segment();
95 
96   EXPECT_FALSE(rewriter.Rewrite(convreq_, &segments));
97 
98   {
99     Segment::Candidate *candidate = seg->add_candidate();
100     seg->set_key("こまんど");
101     candidate->value = "コマンド";
102     EXPECT_TRUE(rewriter.Rewrite(convreq_, &segments));
103     EXPECT_EQ(2, CommandCandidatesSize(*seg));
104     seg->clear_candidates();
105   }
106 
107   {
108     Segment::Candidate *candidate = seg->add_candidate();
109     seg->set_key("さじぇすと");
110     candidate->value = "サジェスト";
111     EXPECT_TRUE(rewriter.Rewrite(convreq_, &segments));
112     EXPECT_EQ(1, CommandCandidatesSize(*seg));
113     seg->clear_candidates();
114   }
115 
116   {
117     Segment::Candidate *candidate = seg->add_candidate();
118     seg->set_key("ひみつ");
119     candidate->value = "秘密";
120     EXPECT_TRUE(rewriter.Rewrite(convreq_, &segments));
121     EXPECT_EQ(1, CommandCandidatesSize(*seg));
122     seg->clear_candidates();
123   }
124 
125   {
126     Segment::Candidate *candidate = seg->add_candidate();
127     seg->set_key("きょうと");
128     candidate->value = "京都";
129     EXPECT_FALSE(rewriter.Rewrite(convreq_, &segments));
130     EXPECT_EQ(0, CommandCandidatesSize(*seg));
131     seg->clear_candidates();
132   }
133 
134   {
135     // don't trigger when multiple segments.
136     Segment::Candidate *candidate = seg->add_candidate();
137     seg->set_key("こまんど");
138     candidate->value = "コマンド";
139     Segment *seg2 = segments.push_back_segment();
140     Segment::Candidate *candidate2 = seg2->add_candidate();
141     seg2->set_key("です");
142     candidate2->value = "です";
143     EXPECT_FALSE(rewriter.Rewrite(convreq_, &segments));
144     EXPECT_EQ(0, CommandCandidatesSize(*seg));
145   }
146 }
147 
TEST_F(CommandRewriterTest,ValueCheck)148 TEST_F(CommandRewriterTest, ValueCheck) {
149   CommandRewriter rewriter;
150   Segments segments;
151   Segment *seg = segments.push_back_segment();
152 
153   {
154     Segment::Candidate *candidate = seg->add_candidate();
155     seg->set_key("さじぇすと");
156     candidate->value = "サジェスト";
157     config_.set_presentation_mode(false);
158     EXPECT_TRUE(rewriter.Rewrite(convreq_, &segments));
159     EXPECT_EQ("サジェスト機能の一時停止", GetCommandCandidateValue(*seg));
160     seg->clear_candidates();
161   }
162 
163   {
164     Segment::Candidate *candidate = seg->add_candidate();
165     seg->set_key("さじぇすと");
166     candidate->value = "サジェスト";
167     config_.set_presentation_mode(true);
168     EXPECT_TRUE(rewriter.Rewrite(convreq_, &segments));
169     EXPECT_EQ("サジェスト機能を元に戻す", GetCommandCandidateValue(*seg));
170     seg->clear_candidates();
171   }
172 
173   {
174     Segment::Candidate *candidate = seg->add_candidate();
175     seg->set_key("ひみつ");
176     candidate->value = "秘密";
177     config_.set_incognito_mode(false);
178     EXPECT_TRUE(rewriter.Rewrite(convreq_, &segments));
179     EXPECT_EQ("シークレットモードをオン", GetCommandCandidateValue(*seg));
180     seg->clear_candidates();
181   }
182 
183   {
184     Segment::Candidate *candidate = seg->add_candidate();
185     seg->set_key("ひみつ");
186     candidate->value = "秘密";
187     config_.set_incognito_mode(true);
188     EXPECT_TRUE(rewriter.Rewrite(convreq_, &segments));
189     EXPECT_EQ("シークレットモードをオフ", GetCommandCandidateValue(*seg));
190     seg->clear_candidates();
191   }
192 }
193 
194 }  // namespace
195 }  // namespace mozc
196