1 /*
2 * Copyright (c) 2025 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 <condition_variable>
18 #include <datetime_ex.h>
19 #include <gtest/gtest.h>
20 #include <if_system_ability_manager.h>
21 #include <iostream>
22 #include <ipc_skeleton.h>
23 #include <mutex>
24 #include <string_ex.h>
25
26 #include "common_event_manager.h"
27 #include "ipower_mode_callback.h"
28 #include "power_common.h"
29 #include "power_mgr_client.h"
30 #include "power_mgr_setdevicemode_test.h"
31 #include "power_mgr_service.h"
32 #include "power_state_machine.h"
33 #include "power_state_machine_info.h"
34 #include "running_lock.h"
35 #include "running_lock_info.h"
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::string g_action = "";
46 } // namespace
47
48 static uint32_t g_i = 0;
49 static int g_judgeNum = 2;
50
SetUpTestCase(void)51 void PowerMgrSetDeviceModeTest::SetUpTestCase(void)
52 {
53 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
54 pms->OnStart();
55 SystemAbility::MakeAndRegisterAbility(pms.GetRefPtr());
56 }
57
CommonEventServiCesSystemTest(const CommonEventSubscribeInfo & subscriberInfo)58 PowerMgrSetDeviceModeTest::CommonEventServiCesSystemTest::CommonEventServiCesSystemTest(
59 const CommonEventSubscribeInfo& subscriberInfo) : CommonEventSubscriber(subscriberInfo)
60 {
61 POWER_HILOGI(LABEL_TEST, "CommonEventSubscriber.");
62 }
63
OnReceiveEvent(const CommonEventData & data)64 void PowerMgrSetDeviceModeTest::CommonEventServiCesSystemTest::OnReceiveEvent(const CommonEventData& data)
65 {
66 std::string action = data.GetWant().GetAction();
67 if (action == CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED) {
68 POWER_HILOGI(LABEL_TEST, "CommonEventServiCesSystemTest::OnReceiveEvent.");
69 g_i = g_judgeNum;
70 }
71 uint32_t j = 2;
72 EXPECT_EQ(g_i, j) << "PowerSavemode_022 fail to PowerModeCallback";
73 POWER_HILOGI(LABEL_TEST, "CommonEventServiCesSystemTest::OnReceiveEvent other.");
74 }
75
76 class CommonEventSaveModeTest : public EventFwk::CommonEventSubscriber {
77 public:
78 CommonEventSaveModeTest() = default;
79 explicit CommonEventSaveModeTest(const EventFwk::CommonEventSubscribeInfo& subscriberInfo);
~CommonEventSaveModeTest()80 virtual ~CommonEventSaveModeTest() {};
81 virtual void OnReceiveEvent(const EventFwk::CommonEventData& data);
82 static shared_ptr<CommonEventSaveModeTest> RegisterEvent();
83 };
84
CommonEventSaveModeTest(const CommonEventSubscribeInfo & subscriberInfo)85 CommonEventSaveModeTest::CommonEventSaveModeTest(const CommonEventSubscribeInfo& subscriberInfo)
86 : CommonEventSubscriber(subscriberInfo)
87 {
88 }
89
OnReceiveEvent(const CommonEventData & data)90 void CommonEventSaveModeTest::OnReceiveEvent(const CommonEventData& data)
91 {
92 g_action = data.GetWant().GetAction();
93 g_cv.notify_one();
94 }
95
RegisterEvent()96 shared_ptr<CommonEventSaveModeTest> CommonEventSaveModeTest::RegisterEvent()
97 {
98 int32_t retryTimes = 2;
99 bool succeed = false;
100 MatchingSkills matchingSkills;
101 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED);
102 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
103 auto subscriberPtr = std::make_shared<CommonEventSaveModeTest>(subscribeInfo);
104 for (int32_t tryTimes = 0; tryTimes < retryTimes; tryTimes++) {
105 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
106 }
107 if (!succeed) {
108 return nullptr;
109 }
110 return subscriberPtr;
111 }
112
113 namespace {
114 /**
115 * @tc.name: PowerSetdevicemode_001
116 * @tc.desc: ReceiveEvent
117 * @tc.type: FUNC
118 * @tc.require: issueI5MJZJ
119 */
120 HWTEST_F(PowerMgrSetDeviceModeTest, PowerSetdevicemode_001, TestSize.Level0)
121 {
122 GTEST_LOG_(INFO) << "PowerSetdevicemode_001: UnRegisterPowerModeCallback start.";
123 POWER_HILOGI(LABEL_TEST, "PowerSetdevicemode_001 UnRegisterPowerModeCallback start.");
124 bool result = false;
125 MatchingSkills matchingSkills;
126 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED);
127 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
128 auto subscriberPtr = std::make_shared<CommonEventServiCesSystemTest>(subscribeInfo);
129 result = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
130 EXPECT_TRUE(result);
131 auto& powerMgrClient = PowerMgrClient::GetInstance();
132 PowerMode mode = PowerMode::PERFORMANCE_MODE;
133 powerMgrClient.SetDeviceMode(mode);
134 CommonEventManager::UnSubscribeCommonEvent(subscriberPtr);
135
136 POWER_HILOGI(LABEL_TEST, "PowerSetdevicemode_001 UnRegisterPowerModeCallback end.");
137 GTEST_LOG_(INFO) << "PowerSetdevicemode_001: UnRegisterPowerModeCallback end.";
138 }
139
140 /**
141 * @tc.name: PowerSetdevicemode_002
142 * @tc.desc: ReceiveEvent
143 * @tc.type: FUNC
144 * @tc.require: issueI5MJZJ
145 */
146 HWTEST_F(PowerMgrSetDeviceModeTest, PowerSetdevicemode_002, TestSize.Level0)
147 {
148 GTEST_LOG_(INFO) << "PowerSetdevicemode_002: UnRegisterPowerModeCallback start.";
149 POWER_HILOGI(LABEL_TEST, "PowerSetdevicemode_002 start.");
150 bool result = false;
151 MatchingSkills matchingSkills;
152 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED);
153 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
154 auto subscriberPtr = std::make_shared<CommonEventServiCesSystemTest>(subscribeInfo);
155 result = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
156 EXPECT_TRUE(result);
157 auto& powerMgrClient = PowerMgrClient::GetInstance();
158 PowerMode mode = PowerMode::EXTREME_POWER_SAVE_MODE;
159 powerMgrClient.SetDeviceMode(mode);
160 CommonEventManager::UnSubscribeCommonEvent(subscriberPtr);
161 POWER_HILOGI(LABEL_TEST, "PowerSetdevicemode_002 end.");
162 GTEST_LOG_(INFO) << "PowerSetdevicemode_002: UnRegisterPowerModeCallback start.";
163 }
164 } // namespace