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_touch_screen_device.h" 21 #include "proto.h" 22 23 namespace OHOS { 24 namespace MMI { 25 namespace { 26 using namespace testing::ext; 27 } // namespace 28 29 class ProcessingTouchScreenDeviceTest : public testing::Test { 30 public: SetUpTestCase(void)31 static void SetUpTestCase(void) {} TearDownTestCase(void)32 static void TearDownTestCase(void) {} 33 }; 34 35 /** 36 * @tc.name:Test_TransformJsonDataToInputData 37 * @tc.desc:Verify ManageInjectDevice function TransformJsonData 38 * @tc.type: FUNC 39 * @tc.require: 40 */ 41 HWTEST_F(ProcessingTouchScreenDeviceTest, Test_TransformJsonDataToInputData, TestSize.Level1) 42 { 43 const std::string path = "/data/json/Test_TransformTouchScreenJsonDataToInputData.json"; 44 std::string launchDeviceCmd = "vuinput launch touchscreen & "; 45 std::string closeDeviceCmd = "vuinput close all"; 46 FILE* startDevice = popen(launchDeviceCmd.c_str(), "rw"); 47 if (!startDevice) { 48 ASSERT_TRUE(false) << "Start device failed"; 49 } 50 pclose(startDevice); 51 std::this_thread::sleep_for(std::chrono::seconds(1)); 52 std::string jsonBuffer = ReadJsonFile(path); 53 if (jsonBuffer.empty()) { 54 ASSERT_TRUE(false) << "Read file failed" << path; 55 } 56 ManageInjectDevice manageInjectDevice; 57 auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuffer, false)); 58 FILE* closeDeviceCom = popen(closeDeviceCmd.c_str(), "rw"); 59 if (!closeDeviceCom) { 60 ASSERT_TRUE(false) << "Close device failed"; 61 } 62 pclose(closeDeviceCom); 63 std::this_thread::sleep_for(std::chrono::seconds(1)); 64 EXPECT_EQ(ret, RET_OK); 65 } 66 67 /** 68 * @tc.name:Test_TransformJsonDataToInputDataEventsIsEmpty 69 * @tc.desc:Verify ManageInjectDevice function TransformJsonData 70 * @tc.type: FUNC 71 * @tc.require: 72 */ 73 HWTEST_F(ProcessingTouchScreenDeviceTest, Test_TransformJsonDataToInputDataEventsIsEmpty, TestSize.Level1) 74 { 75 const std::string path = "/data/json/Test_TransformJsonDataToInputDataEventsIsEmpty.json"; 76 std::string launchDeviceCmd = "vuinput start touchscreen & "; 77 std::string closeDeviceCmd = "vuinput close all"; 78 FILE* launchDevice = popen(launchDeviceCmd.c_str(), "rw"); 79 if (!launchDevice) { 80 ASSERT_TRUE(false) << "Start device failed"; 81 } 82 pclose(launchDevice); 83 std::string jsonBuff = ReadJsonFile(path); 84 if (jsonBuff.empty()) { 85 ASSERT_TRUE(false) << "Read file failed" << path; 86 } 87 ManageInjectDevice manageInjectDevice; 88 auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuff, false)); 89 90 FILE* offDevice = popen(closeDeviceCmd.c_str(), "rw"); 91 if (!offDevice) { 92 ASSERT_TRUE(false) << "Close device failed"; 93 } 94 pclose(offDevice); 95 std::this_thread::sleep_for(std::chrono::seconds(1)); 96 EXPECT_EQ(ret, RET_ERR); 97 } 98 99 /** 100 * @tc.name:Test_TransformJsonDataToInputDataSingleEventsIsEmpty 101 * @tc.desc:Verify ManageInjectDevice function TransformJsonData 102 * @tc.type: FUNC 103 * @tc.require: 104 */ 105 HWTEST_F(ProcessingTouchScreenDeviceTest, Test_TransformJsonDataToInputDataSingleEventsIsEmpty, TestSize.Level1) 106 { 107 const std::string path = "/data/json/Test_TransformJsonDataToInputDataSingleEventsIsEmpty.json"; 108 std::string beginDeviceCmd = "vuinput start touchscreen & "; 109 std::string closeDeviceCmd = "vuinput close all"; 110 FILE* beginDevice = popen(beginDeviceCmd.c_str(), "rw"); 111 if (!beginDevice) { 112 ASSERT_TRUE(false) << "Start device failed"; 113 } 114 pclose(beginDevice); 115 std::string jsonSize = ReadJsonFile(path); 116 if (jsonSize.empty()) { 117 ASSERT_TRUE(false) << "Read file failed" << path; 118 } 119 ManageInjectDevice manageInjectDevice; 120 auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonSize, false)); 121 FILE* closeDeviceCommand = popen(closeDeviceCmd.c_str(), "rw"); 122 if (!closeDeviceCommand) { 123 ASSERT_TRUE(false) << "Close device failed"; 124 } 125 pclose(closeDeviceCommand); 126 std::this_thread::sleep_for(std::chrono::seconds(2)); 127 EXPECT_EQ(ret, RET_ERR); 128 } 129 } // namespace MMI 130 } // namespace OHOS