1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include "global.h"
18 #include "input_method_manager_command.h"
19 #include "tdd_util.h"
20
21 namespace OHOS {
22 namespace MiscServices {
23 namespace {
24 using namespace testing::ext;
25 } // namespace
26 class InputMethodManagerCommandTest : public testing::Test {
27 public:
28 static void SetUpTestCase(void);
TearDownTestCase(void)29 static void TearDownTestCase(void) {}
30 };
31
SetUpTestCase(void)32 void InputMethodManagerCommandTest::SetUpTestCase(void)
33 {
34 IMSA_HILOGI("InputMethodManagerCommandTest::SetUpTestCase");
35 sleep(2);
36 }
37
38 /**
39 * @tc.name: InputMethodManagerCommand_ExeCmd_001
40 * @tc.desc: Test handling argument for -e option
41 * @tc.type: FUNC
42 * @tc.require:
43 */
44 HWTEST_F(InputMethodManagerCommandTest, ExeCmd_001, TestSize.Level1)
45 {
46 IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_001 START");
47 std::string result;
48 auto ret = TddUtil::ExecuteCmd("ime -e com.example.newTestIme", result);
49 EXPECT_TRUE(ret);
50 EXPECT_EQ("Succeeded in enabling IME. status:BASIC_MODE\n", result);
51 }
52
53 /**
54 * @tc.name: InputMethodManagerCommand_ExeCmd_002
55 * @tc.desc: Test handling argument for -e option
56 * @tc.type: FUNC
57 * @tc.require:
58 */
59 HWTEST_F(InputMethodManagerCommandTest, ExeCmd_002, TestSize.Level1)
60 {
61 IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_002 START");
62 std::string result;
63 auto ret = TddUtil::ExecuteCmd("ime -e com.example.test", result);
64 EXPECT_TRUE(ret);
65 EXPECT_EQ("Error: The input method does not exist.\n", result);
66 }
67
68 /**
69 * @tc.name: InputMethodManagerCommand_ExeCmd_003
70 * @tc.desc: Test handling argument for -d option
71 * @tc.type: FUNC
72 * @tc.require:
73 */
74 HWTEST_F(InputMethodManagerCommandTest, ExeCmd_003, TestSize.Level1)
75 {
76 IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_003 START");
77 std::string result;
78 auto ret = TddUtil::ExecuteCmd("ime -d com.example.newTestIme", result);
79 EXPECT_TRUE(ret);
80 EXPECT_EQ("Succeeded in disabling IME. status:DISABLED\n", result);
81 }
82
83 /**
84 * @tc.name: InputMethodManagerCommand_ExeCmd_004
85 * @tc.desc: Test handling argument for -e -f option
86 * @tc.type: FUNC
87 * @tc.require:
88 */
89 HWTEST_F(InputMethodManagerCommandTest, ExeCmd_004, TestSize.Level1)
90 {
91 IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_004 START");
92 std::string result;
93 auto ret = TddUtil::ExecuteCmd("ime -e com.example.newTestIme -f", result);
94 EXPECT_TRUE(ret);
95 EXPECT_EQ("Succeeded in enabling IME. status:FULL_EXPERIENCE_MODE\n", result);
96 }
97
98 /**
99 * @tc.name: InputMethodManagerCommand_ExeCmd_005
100 * @tc.desc: Test handling argument for -e -b option
101 * @tc.type: FUNC
102 * @tc.require:
103 */
104 HWTEST_F(InputMethodManagerCommandTest, ExeCmd_005, TestSize.Level1)
105 {
106 IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_005 START");
107 std::string result;
108 auto ret = TddUtil::ExecuteCmd("ime -e com.example.newTestIme -b", result);
109 EXPECT_TRUE(ret);
110 EXPECT_EQ("Succeeded in enabling IME. status:BASIC_MODE\n", result);
111 }
112
113 /**
114 * @tc.name: InputMethodManagerCommand_ExeCmd_006
115 * @tc.desc: Test handling invalid mode arguments after -e option
116 * @tc.type: FUNC
117 * @tc.require:
118 */
119 HWTEST_F(InputMethodManagerCommandTest, ExeCmd_006, TestSize.Level1)
120 {
121 IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_006 START");
122 std::string result;
123 auto ret = TddUtil::ExecuteCmd("ime -e com.example.newTestIme -a", result);
124 EXPECT_TRUE(ret);
125 EXPECT_EQ("Error: Invalid mode after -e. Use -b or -f\n", result);
126
127 ret = TddUtil::ExecuteCmd("ime -e com.example.newTestIme -fa", result);
128 EXPECT_TRUE(ret);
129 EXPECT_EQ("Error: Invalid mode after -e. Use -b or -f\n", result);
130
131 ret = TddUtil::ExecuteCmd("ime -e com.example.newTestIme ime -e com.example.newTestIme", result);
132 EXPECT_TRUE(ret);
133 EXPECT_EQ("Error: Invalid mode after -e. Use -b or -f\n", result);
134
135 ret = TddUtil::ExecuteCmd("ime -e com.example.newTestIme ime -e -d", result);
136 EXPECT_TRUE(ret);
137 EXPECT_EQ("Error: Invalid mode after -e. Use -b or -f\n", result);
138 }
139
140 /**
141 * @tc.name: InputMethodManagerCommand_ExeCmd_007
142 * @tc.desc: Test handling argument for -s option
143 * @tc.type: FUNC
144 * @tc.require:
145 */
146 HWTEST_F(InputMethodManagerCommandTest, ExeCmd_007, TestSize.Level1)
147 {
148 IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_007 START");
149 std::string result;
150 auto ret = TddUtil::ExecuteCmd("ime -s com.example.TestIme", result);
151 EXPECT_TRUE(ret);
152 EXPECT_EQ("Error: The input method does not exist.\n", result);
153 }
154
155 /**
156 * @tc.name: InputMethodManagerCommand_ExeCmd_008
157 * @tc.desc: Test handling argument for -s option
158 * @tc.type: FUNC
159 * @tc.require:
160 */
161 HWTEST_F(InputMethodManagerCommandTest, ExeCmd_008, TestSize.Level1)
162 {
163 IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_008 START");
164 std::string result;
165 auto ret = TddUtil::ExecuteCmd("ime -d com.example.newTestIme", result);
166 EXPECT_TRUE(ret);
167 sleep(1);
168 ret = TddUtil::ExecuteCmd("ime -e com.example.newTestIme", result);
169 EXPECT_TRUE(ret);
170 sleep(1);
171 ret = TddUtil::ExecuteCmd("ime -s com.example.newTestIme", result);
172 EXPECT_TRUE(ret);
173 EXPECT_EQ("Succeeded in switching the input method. IME:com.example.newTestIme\n", result);
174 }
175
176 /**
177 * @tc.name: InputMethodManagerCommand_ExeCmd_009
178 * @tc.desc: Test handling argument for -g option
179 * @tc.type: FUNC
180 * @tc.require:
181 */
182 HWTEST_F(InputMethodManagerCommandTest, ExeCmd_009, TestSize.Level1)
183 {
184 IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_009 START");
185 sleep(2);
186 std::string result;
187 auto ret = TddUtil::ExecuteCmd("ime -g", result);
188 EXPECT_TRUE(ret);
189 std::string expectStr = "The current input method is: com.example.newTestIm";
190 EXPECT_TRUE(result.find(expectStr) != std::string::npos);
191 }
192
193 /**
194 * @tc.name: InputMethodManagerCommand_ExeCmd_0010
195 * @tc.desc: Test handling situation when the parameters are empty
196 * @tc.type: FUNC
197 * @tc.require:
198 */
199 HWTEST_F(InputMethodManagerCommandTest, ExeCmd_0010, TestSize.Level1)
200 {
201 IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_0010 START");
202 sleep(2);
203 std::string result;
204 auto ret = TddUtil::ExecuteCmd("ime -e \"\"", result);
205 EXPECT_TRUE(ret);
206 EXPECT_EQ("Error: Invalid argument!\n", result);
207 }
208
209 /**
210 * @tc.name: InputMethodManagerCommand_ExeCmd_011
211 * @tc.desc: Test handling invalid mode arguments after -d option
212 * @tc.type: FUNC
213 * @tc.require:
214 */
215 HWTEST_F(InputMethodManagerCommandTest, ExeCmd_011, TestSize.Level1)
216 {
217 IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_011 START");
218 std::string result;
219 auto ret = TddUtil::ExecuteCmd("ime -d com.example.k", result);
220 EXPECT_TRUE(ret);
221 EXPECT_EQ("Error: The input method does not exist.\n", result);
222
223 ret = TddUtil::ExecuteCmd("ime -d com.example.newTestIme a", result);
224 EXPECT_TRUE(ret);
225 EXPECT_EQ("Error: Invalid command!\n", result);
226
227 ret = TddUtil::ExecuteCmd("ime -d com.example.newTestIme -d", result);
228 EXPECT_TRUE(ret);
229 EXPECT_EQ("Error: Invalid command!\n", result);
230
231 ret = TddUtil::ExecuteCmd("ime -d com.example.newTestIme ime -d com.example.newTestIme", result);
232 EXPECT_TRUE(ret);
233 EXPECT_EQ("Error: Invalid command!\n", result);
234 }
235
236 /**
237 * @tc.name: InputMethodManagerCommand_ExeCmd_012
238 * @tc.desc: Test -s option when screen locked
239 * @tc.type: FUNC
240 * @tc.require:
241 */
242 HWTEST_F(InputMethodManagerCommandTest, ExeCmd_012, TestSize.Level1)
243 {
244 IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_012 START");
245 std::string result;
246 auto ret = TddUtil::ExecuteCmd("ime -e com.example.newTestIme", result);
247 EXPECT_TRUE(ret);
248 ret = TddUtil::ExecuteCmd("power-shell suspend", result);
249 EXPECT_TRUE(ret);
250 sleep(1);
251 ret = TddUtil::ExecuteCmd("ime -s com.example.newTestIme", result);
252 EXPECT_TRUE(ret);
253 EXPECT_EQ("Error: Operation failed. Error code:" + std::to_string(ErrorCode::ERROR_SWITCH_IME) + "\n", result);
254
255 ret = TddUtil::ExecuteCmd("power-shell wakeup", result);
256 EXPECT_TRUE(ret);
257 ret = TddUtil::ExecuteCmd("power-shell setmode 602", result);
258 EXPECT_TRUE(ret);
259 ret = TddUtil::ExecuteCmd("uinput -T -m 300 720 300 360 200", result);
260 EXPECT_TRUE(ret);
261 sleep(1);
262 ret = TddUtil::ExecuteCmd("ime -s com.example.newTestIme", result);
263 EXPECT_TRUE(ret);
264 EXPECT_EQ("Succeeded in switching the input method. IME:com.example.newTestIme\n", result);
265 }
266 } // namespace MiscServices
267 } // namespace OHOS