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 <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_on.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 PowerLevelEventSystemTestOn::SetUpTestCase(void)
51 {
52 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
53 pms->OnStart();
54 SystemAbility::MakeAndRegisterAbility(pms.GetRefPtr());
55 }
56
57 class CommonEventScreenOnTest : public CommonEventSubscriber {
58 public:
59 CommonEventScreenOnTest() = default;
60 explicit CommonEventScreenOnTest(const CommonEventSubscribeInfo& subscriberInfo);
~CommonEventScreenOnTest()61 virtual ~CommonEventScreenOnTest() {};
62 virtual void OnReceiveEvent(const CommonEventData& data);
63 static shared_ptr<CommonEventScreenOnTest> RegisterEvent();
64 };
65
CommonEventScreenOnTest(const CommonEventSubscribeInfo & subscriberInfo)66 CommonEventScreenOnTest::CommonEventScreenOnTest(const CommonEventSubscribeInfo& subscriberInfo)
67 : CommonEventSubscriber(subscriberInfo)
68 {
69 }
70
OnReceiveEvent(const CommonEventData & data)71 void CommonEventScreenOnTest::OnReceiveEvent(const CommonEventData& data)
72 {
73 g_action = data.GetWant().GetAction();
74 g_cv.notify_one();
75 }
76
RegisterEvent()77 shared_ptr<CommonEventScreenOnTest> CommonEventScreenOnTest::RegisterEvent()
78 {
79 int32_t retryTimes = 2;
80 bool succeed = false;
81 MatchingSkills matchingSkills;
82 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
83 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
84 auto subscriberPtr = std::make_shared<CommonEventScreenOnTest>(subscribeInfo);
85 for (int32_t tryTimes = 0; tryTimes < retryTimes; tryTimes++) {
86 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
87 }
88 if (!succeed) {
89 return nullptr;
90 }
91 return subscriberPtr;
92 }
93
94 namespace {
95 /**
96 * @tc.name: PowerSavemode_001
97 * @tc.desc: ReceiveEvent
98 * @tc.type: FUNC
99 * @tc.require: issueI5HUVS
100 */
101 HWTEST_F(PowerLevelEventSystemTestOn, PowerSavemode_001, TestSize.Level0)
102 {
103 GTEST_LOG_(INFO) << "PowerScreenOn_001 start";
104 shared_ptr<CommonEventScreenOnTest> subscriber = CommonEventScreenOnTest::RegisterEvent();
105 auto& powerMgrClient = PowerMgrClient::GetInstance();
106 powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION);
107 powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION);
108 std::unique_lock<std::mutex> lck(g_mtx);
109 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
110 g_cv.notify_one();
111 }
112 CommonEventManager::UnSubscribeCommonEvent(subscriber);
113 EXPECT_EQ(CommonEventSupport::COMMON_EVENT_SCREEN_ON, g_action);
114 POWER_HILOGI(LABEL_TEST, "PowerScreenOn_001_end");
115 }
116 } // namespace
117