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 } // namespace
28
29 class ProcessingKeyboardDeviceTest : 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_TransformKeyBoardJsonDataToInputData
37 * @tc.desc:Verify ManageInjectDevice function TransformJsonData
38 * @tc.type: FUNC
39 * @tc.require:
40 */
HWTEST_F(ProcessingKeyboardDeviceTest, Test_TransformKeyBoardJsonDataToInputData, TestSize.Level1)41 HWTEST_F(ProcessingKeyboardDeviceTest, Test_TransformKeyBoardJsonDataToInputData, TestSize.Level1)
42 {
43 const std::string path = "/data/json/Test_TransformKeyBoardJsonDataToInputData.json";
44 std::string startDeviceCmd = "vuinput start keyboard & ";
45 std::string closeDeviceCmd = "vuinput close all";
46 FILE* startDevice = popen(startDeviceCmd.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 jsonBuf = ReadJsonFile(path);
53 if (jsonBuf.empty()) {
54 ASSERT_TRUE(false) << "Read file failed" << path;
55 }
56 ManageInjectDevice manageInjectDevice;
57 auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
58 FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
59 if (!closeDevice) {
60 ASSERT_TRUE(false) << "Close device failed";
61 }
62 pclose(closeDevice);
63 std::this_thread::sleep_for(std::chrono::seconds(1));
64 EXPECT_EQ(ret, RET_OK);
65 }
66
67 /**
68 * @tc.name:Test_TransformKeyBoardJsonDataToInputDataEventsIsEmpty
69 * @tc.desc:Verify ManageInjectDevice function TransformJsonData
70 * @tc.type: FUNC
71 * @tc.require:
72 */
HWTEST_F(ProcessingKeyboardDeviceTest, Test_TransformKeyBoardJsonDataToInputDataEventsIsEmpty, TestSize.Level1)73 HWTEST_F(ProcessingKeyboardDeviceTest, Test_TransformKeyBoardJsonDataToInputDataEventsIsEmpty, TestSize.Level1)
74 {
75 const std::string path = "/data/json/Test_TransformKeyBoardJsonDataToInputDataEventsIsEmpty.json";
76 std::string startDeviceCmd = "vuinput start keyboard & ";
77 std::string closeDeviceCmd = "vuinput close all";
78 FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
79 if (!startDevice) {
80 ASSERT_TRUE(false) << "Start device failed";
81 }
82 pclose(startDevice);
83 std::string jsonBuf = ReadJsonFile(path);
84 if (jsonBuf.empty()) {
85 ASSERT_TRUE(false) << "Read file failed" << path;
86 }
87 ManageInjectDevice manageInjectDevice;
88 auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
89 FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
90 if (!closeDevice) {
91 ASSERT_TRUE(false) << "Close device failed";
92 }
93 pclose(closeDevice);
94 std::this_thread::sleep_for(std::chrono::seconds(1));
95 EXPECT_EQ(ret, RET_ERR);
96 }
97 } // namespace MMI
98 } // namespace OHOS
99