1 /*
2 * Copyright (c) 2024 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 #include <ctime>
18 #include <cstdint>
19 #include <cstdlib>
20 #include <unistd.h>
21
22 #include "service_watcher.h"
23
24 using namespace std;
25 using namespace testing::ext;
26
27 namespace init_ut {
28
ParameterChangeTest(const char *key, const char *value, void *context)29 static void ParameterChangeTest(const char *key, const char *value, void *context)
30 {
31 printf("ParameterChangeTest key:%s %s \n", key, value);
32 }
33
WatcherCallBackTest(const char *key, const ServiceInfo *status)34 static void WatcherCallBackTest(const char *key, const ServiceInfo *status)
35 {
36 printf("WatcherCallBackTest key:%s %d", key, status->status);
37 }
38
39 class ServiceWatcherUnitTest : public testing::Test {
40 public:
SetUpTestCase(void)41 static void SetUpTestCase(void) {};
TearDownTestCase(void)42 static void TearDownTestCase(void) {};
SetUp()43 void SetUp() {};
TearDown()44 void TearDown() {};
45 }
46
ServiceStatusChange(const char *key, const ServiceInfo *status)47 static void ServiceStatusChange(const char *key, const ServiceInfo *status)
48 {
49 std::cout <<"service Name is: " << key;
50 std::cout <<", ServiceStatus is: "<< status->status;
51 std::cout <<", pid is: "<< status->pid << std::endl;
52 }
53
HWTEST_F(ServiceWatcherUnitTest, ServiceWatchForStatus_001, TestSize.Level1)54 HWTEST_F(ServiceWatcherUnitTest, ServiceWatchForStatus_001, TestSize.Level1)
55 {
56 GTEST_LOG_(INFO) << "ServiceWatchForStatus_001 start";
57 string serviceName = "test.Service";
58 int ret = ServiceWatchForStatus(serviceName.c_str(), ServiceStatusChange);
59 EXPECT_EQ(ret, 0);
60 auto status = GetServiceStatus(serviceName);
61 EXPECT_TRUE(status == "idle");
62 GTEST_LOG_(INFO) << "ServiceWatchForStatus_001 end";
63 }
64 }