• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "service_control.h"
20 #include "service_watcher.h"
21 #include "test_utils.h"
22 
23 using namespace testing::ext;
24 
25 namespace initModuleTest {
26 class ServiceWatcherModuleTest : public testing::Test {
27 public:
SetUpTestCase(void)28     static void SetUpTestCase(void) {};
TearDownTestCase(void)29     static void TearDownTestCase(void) {};
SetUp(void)30     void SetUp(void) {};
TearDown(void)31     void TearDown(void) {};
32 };
33 
ServiceStatusChange(const char * key,ServiceStatus status)34 static void ServiceStatusChange(const char *key, ServiceStatus status)
35 {
36     std::cout<<"service Name is: "<<key<<", ServiceStatus is: "<<status<<std::endl;
37 }
38 
39 HWTEST_F(ServiceWatcherModuleTest, serviceWatcher_test_001, TestSize.Level0)
40 {
41     GTEST_LOG_(INFO) << "serviceWatcher_test_001 start";
42     string serviceName = "test.Service";
43     int ret = ServiceWatchForStatus(serviceName.c_str(), ServiceStatusChange);
44     EXPECT_EQ(ret, 0); // No matter if service exist or not, ServiceWatchForStatus always success.
45     auto status = GetServiceStatus(serviceName);
46     EXPECT_TRUE(status == "idle");
47     GTEST_LOG_(INFO) << "serviceWatcher_test_001 end";
48 }
49 
50 HWTEST_F(ServiceWatcherModuleTest, serviceWatcher_test_002, TestSize.Level0)
51 {
52     GTEST_LOG_(INFO) << "serviceWatcher_test_002 start";
53     string serviceName = "media_service";
54     auto status = GetServiceStatus(serviceName);
55     if (status == "running") {
56         int ret = ServiceControl(serviceName.c_str(), STOP);
57         ASSERT_EQ(ret, 0);
58     } else if (status != "created" && status != "stopped") {
59         std::cout << serviceName << " in invalid status " << status << std::endl;
60         ASSERT_TRUE(0);
61     }
62     int ret = ServiceWatchForStatus(serviceName.c_str(), ServiceStatusChange);
63     EXPECT_EQ(ret, 0);
64     status = GetServiceStatus(serviceName);
65     EXPECT_TRUE(status == "stopped");
66     GTEST_LOG_(INFO) << "serviceWatcher_test_002 end";
67 }
68 }
69