1 /*
2 * Copyright (c) 2022-2023 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 <bundle_mgr_proxy.h>
17 #include <datetime_ex.h>
18 #include <gtest/gtest.h>
19 #include <if_system_ability_manager.h>
20 #include <iostream>
21 #include <ipc_skeleton.h>
22 #include <string_ex.h>
23
24 #include "common_event_manager.h"
25 #include "ipower_mode_callback.h"
26 #include "power_common.h"
27 #include "power_level_event_system_test_off.h"
28 #include "power_mgr_client.h"
29 #include "power_mgr_service.h"
30 #include "power_state_machine.h"
31 #include "power_state_machine_info.h"
32 #include "running_lock.h"
33 #include "running_lock_info.h"
34 #include <condition_variable>
35 #include <mutex>
36
37 using namespace testing::ext;
38 using namespace OHOS::PowerMgr;
39 using namespace OHOS::EventFwk;
40 using namespace OHOS;
41 using namespace std;
42
43 namespace {
44 std::condition_variable g_cv;
45 std::mutex g_mtx;
46 std::string g_action = "";
47 constexpr int64_t TIME_OUT = 1;
48 } // namespace
49
SetUpTestCase(void)50 void PowerLevelEventSystemTestOff::SetUpTestCase(void)
51 {
52 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
53 pms->OnStart();
54 SystemAbility::MakeAndRegisterAbility(pms.GetRefPtr());
55 }
56
57 class CommonEventScreenOffTest : public EventFwk::CommonEventSubscriber {
58 public:
59 explicit CommonEventScreenOffTest(const EventFwk::CommonEventSubscribeInfo& subscriberInfo);
~CommonEventScreenOffTest()60 virtual ~CommonEventScreenOffTest() {};
61 virtual void OnReceiveEvent(const EventFwk::CommonEventData& data);
62 static shared_ptr<CommonEventScreenOffTest> RegisterEvent();
63 };
64
CommonEventScreenOffTest(const CommonEventSubscribeInfo & subscriberInfo)65 CommonEventScreenOffTest::CommonEventScreenOffTest(const CommonEventSubscribeInfo& subscriberInfo)
66 : CommonEventSubscriber(subscriberInfo)
67 {
68 }
69
OnReceiveEvent(const CommonEventData & data)70 void CommonEventScreenOffTest::OnReceiveEvent(const CommonEventData& data)
71 {
72 g_action = data.GetWant().GetAction();
73 g_cv.notify_one();
74 }
75
RegisterEvent()76 shared_ptr<CommonEventScreenOffTest> CommonEventScreenOffTest::RegisterEvent()
77 {
78 int32_t retryTimes = 2;
79 bool succeed = false;
80 MatchingSkills matchingSkills;
81 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
82 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
83 auto subscriberPtr = std::make_shared<CommonEventScreenOffTest>(subscribeInfo);
84 for (int32_t tryTimes = 0; tryTimes < retryTimes; tryTimes++) {
85 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
86 }
87 if (!succeed) {
88 return nullptr;
89 }
90 return subscriberPtr;
91 }
92
93 namespace {
94 /**
95 * @tc.name: PowerSavemode_001
96 * @tc.desc: ReceiveEvent
97 * @tc.type: FUNC
98 * @tc.require: issueI5HUVS
99 */
100 HWTEST_F(PowerLevelEventSystemTestOff, PowerSavemode_001, TestSize.Level0)
101 {
102 GTEST_LOG_(INFO) << "PowerScreenOff_001 start";
103 shared_ptr<CommonEventScreenOffTest> subscriber = CommonEventScreenOffTest::RegisterEvent();
104 auto& powerMgrClient = PowerMgrClient::GetInstance();
105 powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION);
106 powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION);
107 std::unique_lock<std::mutex> lck(g_mtx);
108 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
109 g_cv.notify_one();
110 }
111 CommonEventManager::UnSubscribeCommonEvent(subscriber);
112 EXPECT_EQ(CommonEventSupport::COMMON_EVENT_SCREEN_OFF, g_action);
113 POWER_HILOGI(LABEL_TEST, "PowerScreenOff_001_end");
114 }
115 } // namespace
116