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 <iostream>
17#include <string>
18
19#include "common_event_manager.h"
20#include "ces_system_test.h"
21#define private public
22#define protected public
23#undef private
24#undef protected
25
26#include "datetime_ex.h"
27
28#include "power_common.h"
29#include "power_mgr_client.h"
30#include "power_mgr_service.h"
31#include "power_state_machine.h"
32
33using namespace testing::ext;
34using namespace OHOS::EventFwk;
35using namespace OHOS::PowerMgr;
36
37CesSystemTest::CommonEventServiCesSystemTest::CommonEventServiCesSystemTest(
38    const CommonEventSubscribeInfo &subscriberInfo)
39    : CommonEventSubscriber(subscriberInfo)
40{}
41
42void CesSystemTest::CommonEventServiCesSystemTest::OnReceiveEvent(const CommonEventData &data)
43{
44    std::string action = data.GetWant().GetAction();
45    if (action == CommonEventSupport::COMMON_EVENT_SHUTDOWN) {
46        POWER_HILOGD(LABEL_TEST, "CommonEventServiCesSystemTest::OnReceiveEvent.");
47    }
48    POWER_HILOGD(LABEL_TEST, "CommonEventServiCesSystemTest::OnReceiveEvent other.");
49}
50
51void CesSystemTest::SetUpTestCase()
52{}
53
54void CesSystemTest::TearDownTestCase()
55{}
56
57void CesSystemTest::SetUp()
58{}
59
60void CesSystemTest::TearDown()
61{}
62
63namespace {
64/*
65 * @tc.number: CES_SubscriptionEvent_0100
66 * @tc.name: SubscribeCommonEvent
67 * @tc.desc: Verify the function when the input string is normal
68 */
69HWTEST_F(CesSystemTest, CES_SubscriptionEvent_0100, Function | MediumTest | Level1)
70{
71    bool result = false;
72    MatchingSkills matchingSkills;
73    matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SHUTDOWN);
74    CommonEventSubscribeInfo subscribeInfo(matchingSkills);
75    auto subscriberPtr = std::make_shared<CommonEventServiCesSystemTest>(subscribeInfo);
76    result = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
77    EXPECT_TRUE(result);
78    GTEST_LOG_(INFO) << "CES_SubscriptionEvent_0100: ShutDownDevice start.";
79    auto& powerMgrClient = PowerMgrClient::GetInstance();
80    if (false) {
81        powerMgrClient.ShutDownDevice(string("ShutDownDeviceTest001"));
82        sleep(SLEEP_WAIT_TIME_S);
83    }
84    CommonEventManager::UnSubscribeCommonEvent(subscriberPtr);
85}
86}