• 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 <iostream>
17 #include <string_ex.h>
18 
19 #include <bundle_mgr_proxy.h>
20 #include <condition_variable>
21 #include <datetime_ex.h>
22 #include <if_system_ability_manager.h>
23 #include <ipc_skeleton.h>
24 #include <mutex>
25 
26 #include "common_event_manager.h"
27 #include "common_event_support.h"
28 #include "ipower_mode_callback.h"
29 #include "power_mgr_shutdown_fast_test.h"
30 #include "power_common.h"
31 #include "power_mgr_client.h"
32 #include "power_mgr_service.h"
33 #include "power_state_machine.h"
34 #include "power_state_machine_info.h"
35 #include "running_lock_info.h"
36 #include "running_lock.h"
37 
38 using namespace testing::ext;
39 using namespace OHOS::PowerMgr;
40 using namespace OHOS::EventFwk;
41 using namespace OHOS;
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()50 void PowerMgrShutDownFast::SetUpTestCase()
51 {
52 }
53 
TearDownTestCase()54 void PowerMgrShutDownFast::TearDownTestCase()
55 {
56 }
57 
SetUp()58 void PowerMgrShutDownFast::SetUp()
59 {
60     g_action = "";
61     auto& powerMgrClient = PowerMgrClient::GetInstance();
62     powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("test call SetUp"));
63 }
64 
TearDown()65 void PowerMgrShutDownFast::TearDown()
66 {
67     auto& powerMgrClient = PowerMgrClient::GetInstance();
68     powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("test call TearDown"));
69 }
70 
CommonEventServiceSystemTest()71 PowerMgrShutDownFast::CommonEventServiceSystemTest::CommonEventServiceSystemTest()
72 {
73 }
74 
CommonEventServiceSystemTest(const CommonEventSubscribeInfo & subscriberInfo)75 PowerMgrShutDownFast::CommonEventServiceSystemTest::CommonEventServiceSystemTest(
76     const CommonEventSubscribeInfo& subscriberInfo)
77     : CommonEventSubscriber(subscriberInfo)
78 {
79 }
80 
81 std::shared_ptr<PowerMgrShutDownFast::CommonEventServiceSystemTest>
OnRegisterEvent(const std::string & eventStr)82     PowerMgrShutDownFast::CommonEventServiceSystemTest::OnRegisterEvent(const std::string& eventStr)
83 {
84     bool result = false;
85     MatchingSkills matchingSkills;
86     matchingSkills.AddEvent(eventStr);
87     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
88     auto subscriberPtr = std::make_shared<CommonEventServiceSystemTest>(subscribeInfo);
89     result = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
90     EXPECT_TRUE(result);
91     return subscriberPtr;
92 }
93 
OnReceiveEvent(const CommonEventData & data)94 void PowerMgrShutDownFast::CommonEventServiceSystemTest::OnReceiveEvent(const CommonEventData& data)
95 {
96     g_action = data.GetWant().GetAction();
97     GTEST_LOG_(INFO) << "PowerMgr_FastShutDown_action:" << g_action;
98     g_cv.notify_one();
99 }
100 
101 namespace {
102 /**
103  * @tc.name: PowerMgr_FastShutDown_001
104  * @tc.desc: Test whether fast shutdown can trigger screen off event
105  * @tc.type: FUNC
106  * @tc.require: issueI5I9EI
107  */
108 HWTEST_F(PowerMgrShutDownFast, PowerMgr_FastShutDown_001, TestSize.Level2)
109 {
110     auto eventPtr = std::make_unique<PowerMgrShutDownFast::CommonEventServiceSystemTest>();
111     auto subscriberPtr = eventPtr->OnRegisterEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
112 
113     auto& powerMgrClient = PowerMgrClient::GetInstance();
114     powerMgrClient.ShutDownDevice(SHUTDOWN_FAST_REASON);
115     std::unique_lock<std::mutex> lck(g_mtx);
116     if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
117         g_cv.notify_one();
118     }
119     CommonEventManager::UnSubscribeCommonEvent(subscriberPtr);
120 
121     EXPECT_EQ(PowerState::INACTIVE, powerMgrClient.GetState());
122     EXPECT_EQ(CommonEventSupport::COMMON_EVENT_SCREEN_OFF, g_action);
123 }
124 
125 /**
126  * @tc.name: PowerMgr_FastShutDown_002
127  * @tc.desc: Test whether fast shutdown can trigger shut down event
128  * @tc.type: FUNC
129  * @tc.require: issueI5I9EI
130  */
131 HWTEST_F(PowerMgrShutDownFast, PowerMgr_FastShutDown_002, TestSize.Level2)
132 {
133     auto eventPtr = std::make_unique<PowerMgrShutDownFast::CommonEventServiceSystemTest>();
134     auto subscriberPtr = eventPtr->OnRegisterEvent(CommonEventSupport::COMMON_EVENT_SHUTDOWN);
135     auto& powerMgrClient = PowerMgrClient::GetInstance();
136     powerMgrClient.ShutDownDevice(SHUTDOWN_FAST_REASON);
137     CommonEventManager::UnSubscribeCommonEvent(subscriberPtr);
138 
139     EXPECT_EQ(PowerState::INACTIVE, powerMgrClient.GetState());
140     EXPECT_TRUE(g_action.empty());
141 }
142 }
143