1/* 2 * Copyright (c) 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 <thread> 17#include <string> 18#include <gtest/gtest.h> 19 20#include "parameter.h" 21#include "service_control.h" 22#include "service_watcher.h" 23#include "test_utils.h" 24 25using namespace testing::ext; 26 27namespace initModuleTest { 28class ServiceWatcherModuleTest : public testing::Test { 29public: 30 static void SetUpTestCase(void) {}; 31 static void TearDownTestCase(void) {}; 32 void SetUp(void) {}; 33 void TearDown(void) {}; 34}; 35 36static void ServiceStatusChange(const char *key, const ServiceInfo *status) 37{ 38 std::cout <<"service Name is: " << key; 39 std::cout <<", ServiceStatus is: "<< status->status; 40 std::cout <<", pid is: "<< status->pid << std::endl; 41} 42 43HWTEST_F(ServiceWatcherModuleTest, serviceWatcher_test_001, TestSize.Level0) 44{ 45 GTEST_LOG_(INFO) << "serviceWatcher_test_001 start"; 46 string serviceName = "test.Service"; 47 int ret = ServiceWatchForStatus(serviceName.c_str(), ServiceStatusChange); 48 EXPECT_EQ(ret, 0); // No matter if service exist or not, ServiceWatchForStatus always success. 49 auto status = GetServiceStatus(serviceName); 50 EXPECT_TRUE(status == "idle"); 51 GTEST_LOG_(INFO) << "serviceWatcher_test_001 end"; 52} 53 54HWTEST_F(ServiceWatcherModuleTest, serviceWatcher_test_002, TestSize.Level0) 55{ 56 GTEST_LOG_(INFO) << "serviceWatcher_test_002 start"; 57 string serviceName = "media_service"; 58 auto status = GetServiceStatus(serviceName); 59 if (status == "running") { 60 int ret = ServiceControl(serviceName.c_str(), STOP); 61 ASSERT_EQ(ret, 0); 62 } else if (status != "created" && status != "stopped") { 63 std::cout << serviceName << " in invalid status " << status << std::endl; 64 ASSERT_TRUE(0); 65 } 66 int ret = ServiceWatchForStatus(serviceName.c_str(), ServiceStatusChange); 67 EXPECT_EQ(ret, 0); 68 status = GetServiceStatus(serviceName); 69 EXPECT_TRUE(status == "stopped"); 70 GTEST_LOG_(INFO) << "serviceWatcher_test_002 end"; 71} 72 73HWTEST_F(ServiceWatcherModuleTest, serviceWatcher_test_003, TestSize.Level0) 74{ 75 GTEST_LOG_(INFO) << "serviceWatcher_test_003 start"; 76 const std::string serviceName = "deviceinfoservice"; 77 // watcher service status 78 int ret = ServiceWatchForStatus(serviceName.c_str(), ServiceStatusChange); 79 EXPECT_EQ(ret, 0); // No matter if service exist or not, ServiceWatchForStatus always success. 80 // start service 81 char udid[65] = {}; // 65 udid len 82 ret = AclGetDevUdid(udid, sizeof(udid)); 83 EXPECT_NE(ret, 0); 84 auto status1 = GetServiceStatus(serviceName); 85 EXPECT_TRUE(status1 == "running"); 86 87 GTEST_LOG_(INFO) << "serviceWatcher_test_003 end"; 88} 89} 90