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 namespace OHOS { 24 namespace MMI { 25 namespace { 26 using namespace testing::ext; 27 } // namespace 28 29 class GetDeviceObjectTest : 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_GetDeviceObjectTest 37 * @tc.desc:Verify TransformJsonData function 38 * @tc.type: FUNC 39 * @tc.require: 40 */ 41 HWTEST_F(GetDeviceObjectTest, Test_GetDeviceObjectTest, TestSize.Level1) 42 { 43 const std::string path = "/data/json/Test_GetDeviceObjectTest.json"; 44 std::string startDeviceCmd = "vuinput open for all & "; 45 std::string closeDeviceCmd = "vuinput is closed all"; 46 FILE* launchDevice = popen(startDeviceCmd.c_str(), "rw"); 47 if (!launchDevice) { 48 ASSERT_TRUE(false) << "Can not failed"; 49 } 50 pclose(launchDevice); 51 std::this_thread::sleep_for(std::chrono::seconds(1)); 52 std::string jsonBuf = ReadJsonFile(path); 53 if (jsonBuf.empty()) { 54 ASSERT_TRUE(false) << "Open file failed" << path; 55 } 56 ManageInjectDevice openInjectDevice; 57 auto ret = openInjectDevice.TransformJsonData(DataInit(jsonBuf, false)); 58 FILE* stopDevice = popen(closeDeviceCmd.c_str(), "rw"); 59 if (!stopDevice) { 60 ASSERT_TRUE(false) << "Close device failed"; 61 } 62 pclose(stopDevice); 63 std::this_thread::sleep_for(std::chrono::seconds(1)); 64 EXPECT_EQ(ret, RET_OK); 65 } 66 67 /** 68 * @tc.name:Test_GetDeviceObjectTestNotFindDevice 69 * @tc.desc:Verify CreateDeviceObject function 70 * @tc.type: FUNC 71 * @tc.require: 72 */ 73 HWTEST_F(GetDeviceObjectTest, Test_GetDeviceObjectTestNotFindDevice, TestSize.Level1) 74 { 75 const std::string deviceName = "temp"; 76 bool result; 77 auto ret = GetDeviceObject::CreateDeviceObject(deviceName); 78 if (ret == nullptr) { 79 result = true; 80 } else { 81 result = false; 82 } 83 EXPECT_EQ(result, true); 84 } 85 } // namespace MMI 86 } // namespace OHOS