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 18 #include "mmi_log.h" 19 #include "input_replay_command.h" 20 21 namespace OHOS { 22 namespace MMI { 23 namespace { 24 using namespace testing::ext; 25 } // namespace 26 27 class InputReplayCommandTest : public testing::Test { 28 public: SetUpTestCase(void)29 static void SetUpTestCase(void) {} TearDownTestCase(void)30 static void TearDownTestCase(void) {} SetUp()31 void SetUp() {} TearDown()32 void TearDown() {} 33 }; 34 35 /** 36 * @tc.name: InputReplayCommandTest_Constructor 37 * @tc.desc: Test constructor of InputReplayCommand 38 * @tc.type: FUNC 39 * @tc.require: 40 */ 41 HWTEST_F(InputReplayCommandTest, InputReplayCommandTest_Constructor, TestSize.Level1) 42 { 43 char programName[] = {"program_name"}; 44 char* argv[] = {programName, nullptr}; 45 InputReplayCommand command(1, argv); 46 SUCCEED(); 47 } 48 49 /** 50 * @tc.name: InputReplayCommandTest_ParseDeviceMapping_Valid 51 * @tc.desc: Test parsing valid device mapping 52 * @tc.type: FUNC 53 * @tc.require: 54 */ 55 HWTEST_F(InputReplayCommandTest, InputReplayCommandTest_ParseDeviceMapping_Valid, TestSize.Level1) 56 { 57 char programName[] = {"program_name"}; 58 char mapArg[] = {"--map"}; 59 char mapValue[] = {"0:1,2:3,4:5"}; 60 char recordArg[] = {"record"}; 61 char outputPath[] = {"output.bin"}; 62 char devicePath[] = {"/dev/input/event0"}; 63 64 char* mappingArgv[] = { 65 programName, 66 mapArg, mapValue, 67 recordArg, outputPath, 68 devicePath 69 }; 70 InputReplayCommand mappingCommand(6, mappingArgv); 71 EXPECT_TRUE(mappingCommand.Parse()); 72 } 73 74 /** 75 * @tc.name: InputReplayCommandTest_ParseDeviceMapping_Invalid 76 * @tc.desc: Test parsing invalid device mapping 77 * @tc.type: FUNC 78 * @tc.require: 79 */ 80 HWTEST_F(InputReplayCommandTest, InputReplayCommandTest_ParseDeviceMapping_Invalid, TestSize.Level1) 81 { 82 char programName[] = {"program_name"}; 83 char mapArg[] = {"--map"}; 84 char invalidMapValue[] = {"0:1,invalid,4:5"}; 85 char invalidFormatValue[] = {"0:1:2"}; 86 char emptyMapValue[] = {""}; 87 char recordArg[] = {"record"}; 88 char outputPath[] = {"output.bin"}; 89 char devicePath[] = {"/dev/input/event0"}; 90 91 // Invalid mapping value 92 char* invalidMappingArgv[] = { 93 programName, 94 mapArg, invalidMapValue, 95 recordArg, outputPath, 96 devicePath 97 }; 98 InputReplayCommand invalidCommand(6, invalidMappingArgv); 99 EXPECT_FALSE(invalidCommand.Parse()); 100 101 // Invalid format 102 char* invalidFormatArgv[] = { 103 programName, 104 mapArg, invalidFormatValue, 105 recordArg, outputPath, 106 devicePath 107 }; 108 InputReplayCommand invalidFormatCommand(6, invalidFormatArgv); 109 EXPECT_FALSE(invalidFormatCommand.Parse()); 110 111 // Empty mapping 112 char* emptyMappingArgv[] = { 113 programName, 114 mapArg, emptyMapValue, 115 recordArg, outputPath, 116 devicePath 117 }; 118 InputReplayCommand emptyMappingCommand(6, emptyMappingArgv); 119 EXPECT_FALSE(emptyMappingCommand.Parse()); 120 } 121 122 /** 123 * @tc.name: InputReplayCommandTest_ParseRecord_WithDevices 124 * @tc.desc: Test parsing record command with specific devices 125 * @tc.type: FUNC 126 * @tc.require: 127 */ 128 HWTEST_F(InputReplayCommandTest, InputReplayCommandTest_ParseRecord_WithDevices, TestSize.Level1) 129 { 130 char programName[] = {"program_name"}; 131 char recordArg[] = {"record"}; 132 char outputPath[] = {"output.bin"}; 133 char device1[] = {"/dev/input/event0"}; 134 char device2[] = {"/dev/input/event1"}; 135 136 char* argv[] = { 137 programName, 138 recordArg, outputPath, 139 device1, device2 140 }; 141 InputReplayCommand command(5, argv); 142 EXPECT_TRUE(command.Parse()); 143 } 144 145 /** 146 * @tc.name: InputReplayCommandTest_ParseRecord_AllDevices 147 * @tc.desc: Test parsing record command with all devices flag 148 * @tc.type: FUNC 149 * @tc.require: 150 */ 151 HWTEST_F(InputReplayCommandTest, InputReplayCommandTest_ParseRecord_AllDevices, TestSize.Level1) 152 { 153 char programName[] = {"program_name"}; 154 char allArg[] = {"--all"}; 155 char recordArg[] = {"record"}; 156 char outputPath[] = {"output.bin"}; 157 158 char* argv[] = { 159 programName, 160 allArg, 161 recordArg, outputPath 162 }; 163 InputReplayCommand command(4, argv); 164 EXPECT_TRUE(command.Parse()); 165 } 166 167 /** 168 * @tc.name: InputReplayCommandTest_ParseRecord_NoDevices 169 * @tc.desc: Test parsing record command with no devices specified 170 * @tc.type: FUNC 171 * @tc.require: 172 */ 173 HWTEST_F(InputReplayCommandTest, InputReplayCommandTest_ParseRecord_NoDevices, TestSize.Level1) 174 { 175 char programName[] = {"program_name"}; 176 char recordArg[] = {"record"}; 177 char outputPath[] = {"output.bin"}; 178 179 char* argv[] = { 180 programName, 181 recordArg, outputPath 182 }; 183 InputReplayCommand command(3, argv); 184 EXPECT_FALSE(command.Parse()); 185 } 186 187 /** 188 * @tc.name: InputReplayCommandTest_ParseReplay_Valid 189 * @tc.desc: Test parsing replay command with valid arguments 190 * @tc.type: FUNC 191 * @tc.require: 192 */ 193 HWTEST_F(InputReplayCommandTest, InputReplayCommandTest_ParseReplay_Valid, TestSize.Level1) 194 { 195 char programName[] = {"program_name"}; 196 char replayArg[] = {"replay"}; 197 char inputPath[] = {"input.bin"}; 198 199 char* argv[] = { 200 programName, 201 replayArg, inputPath 202 }; 203 InputReplayCommand command(3, argv); 204 EXPECT_TRUE(command.Parse()); 205 } 206 207 /** 208 * @tc.name: InputReplayCommandTest_ParseReplay_Invalid 209 * @tc.desc: Test parsing replay command with extra arguments 210 * @tc.type: FUNC 211 * @tc.require: 212 */ 213 HWTEST_F(InputReplayCommandTest, InputReplayCommandTest_ParseReplay_Invalid, TestSize.Level1) 214 { 215 char programName[] = {"program_name"}; 216 char replayArg[] = {"replay"}; 217 char inputPath[] = {"input.bin"}; 218 char extraArg[] = {"extra"}; 219 220 char* argv[] = { 221 programName, 222 replayArg, inputPath, extraArg 223 }; 224 InputReplayCommand command(4, argv); 225 EXPECT_FALSE(command.Parse()); 226 } 227 228 /** 229 * @tc.name: InputReplayCommandTest_Parse_MissingCommand 230 * @tc.desc: Test parsing with missing command 231 * @tc.type: FUNC 232 * @tc.require: 233 */ 234 HWTEST_F(InputReplayCommandTest, InputReplayCommandTest_Parse_MissingCommand, TestSize.Level1) 235 { 236 char programName[] = {"program_name"}; 237 char* argv[] = {programName, nullptr}; 238 InputReplayCommand command(1, argv); 239 EXPECT_FALSE(command.Parse()); 240 } 241 242 /** 243 * @tc.name: InputReplayCommandTest_Parse_MissingFilePath 244 * @tc.desc: Test parsing with missing file path 245 * @tc.type: FUNC 246 * @tc.require: 247 */ 248 HWTEST_F(InputReplayCommandTest, InputReplayCommandTest_Parse_MissingFilePath, TestSize.Level1) 249 { 250 char programName[] = {"program_name"}; 251 char recordArg[] = {"record"}; 252 char* argv[] = {programName, recordArg, nullptr}; 253 InputReplayCommand command(2, argv); 254 EXPECT_FALSE(command.Parse()); 255 } 256 257 /** 258 * @tc.name: InputReplayCommandTest_Parse_InvalidCommand 259 * @tc.desc: Test parsing with invalid command 260 * @tc.type: FUNC 261 * @tc.require: 262 */ 263 HWTEST_F(InputReplayCommandTest, InputReplayCommandTest_Parse_InvalidCommand, TestSize.Level1) 264 { 265 char programName[] = {"program_name"}; 266 char invalidArg[] = {"invalid"}; 267 char filePath[] = {"file.bin"}; 268 269 char* argv[] = { 270 programName, 271 invalidArg, filePath 272 }; 273 InputReplayCommand command(3, argv); 274 EXPECT_FALSE(command.Parse()); 275 } 276 277 /** 278 * @tc.name: InputReplayCommandTest_HandleCommand 279 * @tc.desc: Test the static HandleRecordReplayCommand function with error cases 280 * @tc.type: FUNC 281 * @tc.require: 282 */ 283 HWTEST_F(InputReplayCommandTest, InputReplayCommandTest_HandleCommand, TestSize.Level1) 284 { 285 // Invalid command 286 char programName1[] = {"program_name"}; 287 char invalidArg[] = {"invalid"}; 288 char* invalidArgv[] = { 289 programName1, 290 invalidArg 291 }; 292 EXPECT_EQ(RET_ERR, InputReplayCommand::HandleRecordReplayCommand(2, invalidArgv)); 293 294 // Incomplete arguments 295 char programName2[] = {"program_name"}; 296 char* incompleteArgv[] = { 297 programName2 298 }; 299 EXPECT_EQ(RET_ERR, InputReplayCommand::HandleRecordReplayCommand(1, incompleteArgv)); 300 } 301 302 /** 303 * @tc.name: InputReplayCommandTest_ParseRecord_ExtraArguments 304 * @tc.desc: Test parsing record command with extra arguments 305 * @tc.type: FUNC 306 * @tc.require: 307 */ 308 HWTEST_F(InputReplayCommandTest, InputReplayCommandTest_ParseRecord_ExtraArguments, TestSize.Level1) 309 { 310 char programName[] = {"program_name"}; 311 char allArg[] = {"--all"}; 312 char recordArg[] = {"record"}; 313 char outputPath[] = {"output.bin"}; 314 char extraArg[] = {"extra_arg"}; // Adding redundant parameter 315 316 char* argv[] = { 317 programName, 318 recordArg, outputPath, 319 allArg, 320 extraArg 321 }; 322 InputReplayCommand command(5, argv); 323 EXPECT_FALSE(command.Parse()); // should fail due to extra argument 324 } 325 326 /** 327 * @tc.name: InputReplayCommandTest_ParseReplay_WithAllDevicesFlag 328 * @tc.desc: Test parsing replay command with all devices flag 329 * @tc.type: FUNC 330 * @tc.require: 331 */ 332 HWTEST_F(InputReplayCommandTest, InputReplayCommandTest_ParseReplay_WithAllDevicesFlag, TestSize.Level1) 333 { 334 char programName[] = {"program_name"}; 335 char allArg[] = {"--all"}; 336 char replayArg[] = {"replay"}; 337 char inputPath[] = {"input.bin"}; 338 339 char* argv[] = { 340 programName, 341 replayArg, inputPath, 342 allArg 343 }; 344 345 InputReplayCommand command(4, argv); 346 EXPECT_FALSE(command.Parse()); 347 } 348 } // namespace MMI 349 } // namespace OHOS