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 "get_device_object.h" 19 #include "manage_inject_device.h" 20 #include "msg_head.h" 21 #include "proto.h" 22 23 24 namespace OHOS { 25 namespace MMI { 26 namespace { 27 using namespace testing::ext; 28 using namespace OHOS::MMI; 29 } // namespace 30 31 class GetDeviceObjectTest : public testing::Test { 32 public: SetUpTestCase(void)33 static void SetUpTestCase(void) {} TearDownTestCase(void)34 static void TearDownTestCase(void) {} 35 }; 36 37 /** 38 * @tc.name:Test_GetDeviceObjectTest 39 * @tc.desc:Verify TransformJsonData function 40 * @tc.type: FUNC 41 * @tc.require: 42 */ 43 HWTEST_F(GetDeviceObjectTest, Test_GetDeviceObjectTest, TestSize.Level1) 44 { 45 const std::string path = "/data/json/Test_GetDeviceObjectTest.json"; 46 std::string startDeviceCmd = "vuinput start all & "; 47 std::string closeDeviceCmd = "vuinput close all"; 48 FILE* startDevice = popen(startDeviceCmd.c_str(), "rw"); 49 if (!startDevice) { 50 ASSERT_TRUE(false) << "Can not failed"; 51 } 52 pclose(startDevice); 53 std::this_thread::sleep_for(std::chrono::seconds(1)); 54 std::string jsonBuf = ReadJsonFile(path); 55 if (jsonBuf.empty()) { 56 ASSERT_TRUE(false) << "Read file failed" << path; 57 } 58 ManageInjectDevice manageInjectDevice; 59 auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false)); 60 FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw"); 61 if (!closeDevice) { 62 ASSERT_TRUE(false) << "Close device failed"; 63 } 64 pclose(closeDevice); 65 std::this_thread::sleep_for(std::chrono::seconds(1)); 66 EXPECT_EQ(ret, RET_OK); 67 } 68 69 /** 70 * @tc.name:Test_GetDeviceObjectTestNotFindDevice 71 * @tc.desc:Verify CreateDeviceObject function 72 * @tc.type: FUNC 73 * @tc.require: 74 */ 75 HWTEST_F(GetDeviceObjectTest, Test_GetDeviceObjectTestNotFindDevice, TestSize.Level1) 76 { 77 const std::string deviceName = "temp"; 78 bool result; 79 auto ret = GetDeviceObject::CreateDeviceObject(deviceName); 80 if (ret == nullptr) { 81 result = true; 82 } else { 83 result = false; 84 } 85 EXPECT_EQ(result, true); 86 } 87 } // namespace MMI 88 } // namespace OHOS