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/ime_switch_util.h"
31 
32 #include <string>
33 
34 #include "base/system_util.h"
35 #include "composer/key_parser.h"
36 #include "config/config_handler.h"
37 #include "protocol/commands.pb.h"
38 #include "protocol/config.pb.h"
39 #include "session/internal/keymap.h"
40 #include "testing/base/public/googletest.h"
41 #include "testing/base/public/gunit.h"
42 
43 namespace mozc {
44 namespace config {
45 
TEST(ImeSwitchUtilTest,PresetTest)46 TEST(ImeSwitchUtilTest, PresetTest) {
47   Config config;
48   ConfigHandler::GetDefaultConfig(&config);
49   config.set_session_keymap(Config::ATOK);
50   ImeSwitchUtil::ReloadConfig(config);
51   {
52     commands::KeyEvent key;
53     KeyParser::ParseKey("HENKAN", &key);
54     EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
55   }
56   {
57     commands::KeyEvent key;
58     KeyParser::ParseKey("EISU", &key);
59     EXPECT_FALSE(ImeSwitchUtil::IsDirectModeCommand(key));
60   }
61   {
62     commands::KeyEvent key;
63     KeyParser::ParseKey("ON", &key);
64     EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
65   }
66   {
67     // Reconcersion
68     commands::KeyEvent key;
69     KeyParser::ParseKey("Shift HENKAN", &key);
70     EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
71   }
72 
73   config.set_session_keymap(Config::MSIME);
74   ImeSwitchUtil::ReloadConfig(config);
75   {
76     commands::KeyEvent key;
77     KeyParser::ParseKey("HENKAN", &key);
78     EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
79   }
80   {
81     commands::KeyEvent key;
82     KeyParser::ParseKey("EISU", &key);
83     EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
84   }
85   {
86     commands::KeyEvent key;
87     KeyParser::ParseKey("ON", &key);
88     EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
89   }
90 
91   config.set_session_keymap(Config::KOTOERI);
92   ImeSwitchUtil::ReloadConfig(config);
93   {
94     commands::KeyEvent key;
95     KeyParser::ParseKey("HENKAN", &key);
96     EXPECT_FALSE(ImeSwitchUtil::IsDirectModeCommand(key));
97   }
98   {
99     commands::KeyEvent key;
100     KeyParser::ParseKey("EISU", &key);
101     EXPECT_FALSE(ImeSwitchUtil::IsDirectModeCommand(key));
102   }
103   {
104     commands::KeyEvent key;
105     KeyParser::ParseKey("ON", &key);
106     EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
107   }
108   {
109     // Reconcersion
110     commands::KeyEvent key;
111     KeyParser::ParseKey("Ctrl Shift r", &key);
112     EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
113   }
114 }
115 
TEST(ImeSwitchUtilTest,DefaultTest)116 TEST(ImeSwitchUtilTest, DefaultTest) {
117   Config config;
118   ConfigHandler::GetDefaultConfig(&config);
119   config.set_session_keymap(Config::NONE);
120   ImeSwitchUtil::ReloadConfig(config);
121   {
122     commands::KeyEvent key;
123     KeyParser::ParseKey("HENKAN", &key);
124     // HENKAN key in MSIME is TurnOn key while it's not in KOTOERI.
125     if (ConfigHandler::GetDefaultKeyMap() == config::Config::MSIME) {
126       EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
127     } else {
128       EXPECT_FALSE(ImeSwitchUtil::IsDirectModeCommand(key));
129     }
130   }
131   {
132     commands::KeyEvent key;
133     KeyParser::ParseKey("EISU", &key);
134     if (ConfigHandler::GetDefaultKeyMap() == config::Config::MSIME) {
135       EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
136     } else {
137       EXPECT_FALSE(ImeSwitchUtil::IsDirectModeCommand(key));
138     }
139   }
140   {
141     commands::KeyEvent key;
142     KeyParser::ParseKey("ON", &key);
143     key.set_special_key(commands::KeyEvent::ON);
144     if (ConfigHandler::GetDefaultKeyMap() == config::Config::CHROMEOS) {
145       EXPECT_FALSE(ImeSwitchUtil::IsDirectModeCommand(key));
146     } else {
147       EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
148     }
149   }
150 }
151 
TEST(ImeSwitchUtilTest,CustomTest)152 TEST(ImeSwitchUtilTest, CustomTest) {
153   Config config;
154   ConfigHandler::GetDefaultConfig(&config);
155 
156   const string custom_keymap_table =
157       "status\tkey\tcommand\n"
158       "DirectInput\tHenkan\tIMEOn\n"
159       "DirectInput\tCtrl j\tIMEOn\n"
160       "DirectInput\tCtrl k\tIMEOff\n"
161       "DirectInput\tCtrl l\tLaunchWordRegisterDialog\n"
162       "Precomposition\tCtrl m\tIMEOn\n";
163 
164   config.set_session_keymap(Config::CUSTOM);
165   config.set_custom_keymap_table(custom_keymap_table);
166   ImeSwitchUtil::ReloadConfig(config);
167   {
168     commands::KeyEvent key;
169     KeyParser::ParseKey("HENKAN", &key);
170     EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
171   }
172   {
173     commands::KeyEvent key;
174     KeyParser::ParseKey("EISU", &key);
175     EXPECT_FALSE(ImeSwitchUtil::IsDirectModeCommand(key));
176   }
177   {
178     commands::KeyEvent key;
179     KeyParser::ParseKey("ctrl j", &key);
180     EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
181   }
182   {
183     commands::KeyEvent key;
184     KeyParser::ParseKey("ctrl k", &key);
185     EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
186   }
187   {
188     commands::KeyEvent key;
189     KeyParser::ParseKey("ctrl l", &key);
190     EXPECT_TRUE(ImeSwitchUtil::IsDirectModeCommand(key));
191   }
192   {
193     commands::KeyEvent key;
194     KeyParser::ParseKey("ctrl m", &key);
195     EXPECT_FALSE(ImeSwitchUtil::IsDirectModeCommand(key));
196   }
197 }
198 
199 }  // namespace config
200 }  // namespace mozc
201