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