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 "msg_head.h" 19 #include "proto.h" 20 #define private public 21 #include "manage_inject_device.h" 22 #undef private 23 24 namespace OHOS { 25 namespace MMI { 26 namespace { 27 using namespace testing::ext; 28 using namespace OHOS::MMI; 29 } // namespace 30 class ManageInjectDeviceTest : 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_TransformJsonDataCheckFileIsEmpty 38 * @tc.desc:Verify ManageInjectDevice function TransformJsonData 39 * @tc.type: FUNC 40 * @tc.require: 41 */ 42 HWTEST_F(ManageInjectDeviceTest, Test_TransformJsonDataCheckFileIsEmpty, TestSize.Level1) 43 { 44 DeviceItems inputEventArrays; 45 inputEventArrays.clear(); 46 ManageInjectDevice manageInjectDevice; 47 auto ret = manageInjectDevice.TransformJsonData(inputEventArrays); 48 EXPECT_EQ(ret, RET_ERR); 49 } 50 51 /** 52 * @tc.name:Test_TransformJsonDataCheckFileNotEmpty 53 * @tc.desc:Verify ManageInjectDevice function TransformJsonData 54 * @tc.type: FUNC 55 * @tc.require: 56 */ 57 HWTEST_F(ManageInjectDeviceTest, Test_TransformJsonDataCheckFileNotEmpty, TestSize.Level1) 58 { 59 const std::string path = "/data/json/Test_TransformJsonDataCheckFileNotEmpty.json"; 60 std::string startDeviceCmd = "vuinput start all & "; 61 std::string closeDeviceCmd = "vuinput close all"; 62 FILE* startDevice = popen(startDeviceCmd.c_str(), "rw"); 63 if (!startDevice) { 64 ASSERT_TRUE(false) << "Start device failed"; 65 } 66 pclose(startDevice); 67 std::string jsonBuf = ReadJsonFile(path); 68 if (jsonBuf.empty()) { 69 ASSERT_TRUE(false) << "Read file failed" << path; 70 } 71 ManageInjectDevice manageInjectDevice; 72 auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false)); 73 FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw"); 74 if (!closeDevice) { 75 ASSERT_TRUE(false) << "Close device failed"; 76 } 77 pclose(closeDevice); 78 std::this_thread::sleep_for(std::chrono::seconds(1)); 79 EXPECT_EQ(ret, RET_OK); 80 } 81 82 /** 83 * @tc.name:Test_TransformJsonDataGetDeviceNodeError 84 * @tc.desc:Verify ManageInjectDevice function TransformJsonData 85 * @tc.type: FUNC 86 * @tc.require: 87 */ 88 HWTEST_F(ManageInjectDeviceTest, Test_TransformJsonDataGetDeviceNodeError, TestSize.Level1) 89 { 90 const std::string path = "/data/json/Test_TransformJsonDataGetDeviceNodeError.json"; 91 std::string startDeviceCmd = "vuinput start all & "; 92 std::string closeDeviceCmd = "vuinput close all"; 93 FILE* startDevice = popen(startDeviceCmd.c_str(), "rw"); 94 if (!startDevice) { 95 ASSERT_TRUE(false) << "Start device failed"; 96 } 97 pclose(startDevice); 98 std::string jsonBuf = ReadJsonFile(path); 99 if (jsonBuf.empty()) { 100 ASSERT_TRUE(false) << "Read file failed" << path; 101 } 102 ManageInjectDevice manageInjectDevice; 103 auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false)); 104 FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw"); 105 if (!closeDevice) { 106 ASSERT_TRUE(false) << "Close device failed"; 107 } 108 pclose(closeDevice); 109 std::this_thread::sleep_for(std::chrono::seconds(1)); 110 EXPECT_EQ(ret, RET_ERR); 111 } 112 113 /** 114 * @tc.name:Test_SendEventToDeviceNodeError 115 * @tc.desc:Verify ManageInjectDevice function SendEventToDeviceNode 116 * @tc.type: FUNC 117 * @tc.require: 118 */ 119 HWTEST_F(ManageInjectDeviceTest, Test_SendEventToDeviceNodeError, TestSize.Level1) 120 { 121 ManageInjectDevice manageInjectDevice; 122 InputEventArray inputEventArray = {}; 123 inputEventArray.target = ""; 124 auto ret = manageInjectDevice.SendEventToDeviceNode(inputEventArray); 125 EXPECT_EQ(ret, RET_ERR); 126 } 127 } // namespace MMI 128 } // namespace OHOS