1 /* 2 * Copyright (c) 2021-2022 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 "manage_inject_device.h" 19 #include "msg_head.h" 20 #include "processing_keyboard_device.h" 21 #include "proto.h" 22 23 namespace OHOS { 24 namespace MMI { 25 namespace { 26 using namespace testing::ext; 27 using namespace OHOS::MMI; 28 } // namespace 29 30 class ProcessingKeyboardDeviceTest : public testing::Test { 31 public: SetUpTestCase(void)32 static void SetUpTestCase(void) {} TearDownTestCase(void)33 static void TearDownTestCase(void) {} 34 }; 35 36 /** 37 * @tc.name:Test_TransformKeyBoardJsonDataToInputData 38 * @tc.desc:Verify ManageInjectDevice function TransformJsonData 39 * @tc.type: FUNC 40 * @tc.require: 41 */ 42 HWTEST_F(ProcessingKeyboardDeviceTest, Test_TransformKeyBoardJsonDataToInputData, TestSize.Level1) 43 { 44 const std::string path = "/data/json/Test_TransformKeyBoardJsonDataToInputData.json"; 45 std::string startDeviceCmd = "vuinput start keyboard & "; 46 std::string closeDeviceCmd = "vuinput close all"; 47 FILE* startDevice = popen(startDeviceCmd.c_str(), "rw"); 48 if (!startDevice) { 49 ASSERT_TRUE(false) << "Start device failed"; 50 } 51 pclose(startDevice); 52 std::this_thread::sleep_for(std::chrono::seconds(1)); 53 std::string jsonBuf = ReadJsonFile(path); 54 if (jsonBuf.empty()) { 55 ASSERT_TRUE(false) << "Read file failed" << path; 56 } 57 ManageInjectDevice manageInjectDevice; 58 auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false)); 59 FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw"); 60 if (!closeDevice) { 61 ASSERT_TRUE(false) << "Close device failed"; 62 } 63 pclose(closeDevice); 64 std::this_thread::sleep_for(std::chrono::seconds(1)); 65 EXPECT_EQ(ret, RET_OK); 66 } 67 68 /** 69 * @tc.name:Test_TransformKeyBoardJsonDataToInputDataEventsIsEmpty 70 * @tc.desc:Verify ManageInjectDevice function TransformJsonData 71 * @tc.type: FUNC 72 * @tc.require: 73 */ 74 HWTEST_F(ProcessingKeyboardDeviceTest, Test_TransformKeyBoardJsonDataToInputDataEventsIsEmpty, TestSize.Level1) 75 { 76 const std::string path = "/data/json/Test_TransformKeyBoardJsonDataToInputDataEventsIsEmpty.json"; 77 std::string startDeviceCmd = "vuinput start keyboard & "; 78 std::string closeDeviceCmd = "vuinput close all"; 79 FILE* startDevice = popen(startDeviceCmd.c_str(), "rw"); 80 if (!startDevice) { 81 ASSERT_TRUE(false) << "Start device failed"; 82 } 83 pclose(startDevice); 84 std::string jsonBuf = ReadJsonFile(path); 85 if (jsonBuf.empty()) { 86 ASSERT_TRUE(false) << "Read file failed" << path; 87 } 88 ManageInjectDevice manageInjectDevice; 89 auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false)); 90 FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw"); 91 if (!closeDevice) { 92 ASSERT_TRUE(false) << "Close device failed"; 93 } 94 pclose(closeDevice); 95 std::this_thread::sleep_for(std::chrono::seconds(1)); 96 EXPECT_EQ(ret, RET_ERR); 97 } 98 } // namespace MMI 99 } // namespace OHOS 100