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