• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     g_i = 0;
74     POWER_HILOGI(LABEL_TEST, "CommonEventServiCesSystemTest::OnReceiveEvent other.");
75 }
76 
77 class CommonEventSaveModeTest : public EventFwk::CommonEventSubscriber {
78 public:
79     CommonEventSaveModeTest() = default;
80     explicit CommonEventSaveModeTest(const EventFwk::CommonEventSubscribeInfo& subscriberInfo);
~CommonEventSaveModeTest()81     virtual ~CommonEventSaveModeTest() {};
82     virtual void OnReceiveEvent(const EventFwk::CommonEventData& data);
83     static shared_ptr<CommonEventSaveModeTest> RegisterEvent();
84 };
85 
CommonEventSaveModeTest(const CommonEventSubscribeInfo & subscriberInfo)86 CommonEventSaveModeTest::CommonEventSaveModeTest(const CommonEventSubscribeInfo& subscriberInfo)
87     : CommonEventSubscriber(subscriberInfo)
88 {
89 }
90 
OnReceiveEvent(const CommonEventData & data)91 void CommonEventSaveModeTest::OnReceiveEvent(const CommonEventData& data)
92 {
93     g_action = data.GetWant().GetAction();
94     g_cv.notify_one();
95 }
96 
RegisterEvent()97 shared_ptr<CommonEventSaveModeTest> CommonEventSaveModeTest::RegisterEvent()
98 {
99     int32_t retryTimes = 2;
100     bool succeed = false;
101     MatchingSkills matchingSkills;
102     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED);
103     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
104     auto subscriberPtr = std::make_shared<CommonEventSaveModeTest>(subscribeInfo);
105     for (int32_t tryTimes = 0; tryTimes < retryTimes; tryTimes++) {
106         succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
107     }
108     if (!succeed) {
109         return nullptr;
110     }
111     return subscriberPtr;
112 }
113 
114 namespace {
115 /**
116  * @tc.name: PowerSetdevicemode_001
117  * @tc.desc: ReceiveEvent
118  * @tc.type: FUNC
119  * @tc.require: issueI5MJZJ
120  */
121 HWTEST_F(PowerMgrSetDeviceModeTest, PowerSetdevicemode_001, TestSize.Level1)
122 {
123     GTEST_LOG_(INFO) << "PowerSetdevicemode_001: UnRegisterPowerModeCallback start.";
124     POWER_HILOGI(LABEL_TEST, "PowerSetdevicemode_001 function start!");
125     bool result = false;
126     MatchingSkills matchingSkills;
127     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED);
128     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
129     auto subscriberPtr = std::make_shared<CommonEventServiCesSystemTest>(subscribeInfo);
130     result = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
131     EXPECT_TRUE(result);
132     auto& powerMgrClient = PowerMgrClient::GetInstance();
133     PowerMode mode = PowerMode::PERFORMANCE_MODE;
134     powerMgrClient.SetDeviceMode(mode);
135     CommonEventManager::UnSubscribeCommonEvent(subscriberPtr);
136 
137     POWER_HILOGI(LABEL_TEST, "PowerSetdevicemode_001 function end!");
138     GTEST_LOG_(INFO) << "PowerSetdevicemode_001: UnRegisterPowerModeCallback end.";
139 }
140 
141 /**
142  * @tc.name: PowerSetdevicemode_002
143  * @tc.desc: ReceiveEvent
144  * @tc.type: FUNC
145  * @tc.require: issueI5MJZJ
146  */
147 HWTEST_F(PowerMgrSetDeviceModeTest, PowerSetdevicemode_002, TestSize.Level1)
148 {
149     GTEST_LOG_(INFO) << "PowerSetdevicemode_002: UnRegisterPowerModeCallback start.";
150     POWER_HILOGI(LABEL_TEST, "PowerSetdevicemode_002 function start!");
151     bool result = false;
152     MatchingSkills matchingSkills;
153     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED);
154     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
155     auto subscriberPtr = std::make_shared<CommonEventServiCesSystemTest>(subscribeInfo);
156     result = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
157     EXPECT_TRUE(result);
158     auto& powerMgrClient = PowerMgrClient::GetInstance();
159     PowerMode mode = PowerMode::EXTREME_POWER_SAVE_MODE;
160     powerMgrClient.SetDeviceMode(mode);
161     CommonEventManager::UnSubscribeCommonEvent(subscriberPtr);
162     POWER_HILOGI(LABEL_TEST, "PowerSetdevicemode_002 function end!");
163     GTEST_LOG_(INFO) << "PowerSetdevicemode_002: UnRegisterPowerModeCallback start.";
164 }
165 
166 /**
167  * @tc.name: PowerSetdevicemode_003
168  * @tc.desc: ReceiveEvent
169  * @tc.type: FUNC
170  * @tc.require: issueI5MJZJ
171  */
172 HWTEST_F(PowerMgrSetDeviceModeTest, PowerSetdevicemode_003, TestSize.Level1)
173 {
174     POWER_HILOGI(LABEL_TEST, "PowerSetdevicemode_003 function start!");
175     bool result = false;
176     MatchingSkills matchingSkills;
177     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED);
178     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
179     auto subscriberPtr = std::make_shared<CommonEventServiCesSystemTest>(subscribeInfo);
180     result = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
181     EXPECT_TRUE(result);
182     auto& powerMgrClient = PowerMgrClient::GetInstance();
183     PowerMode mode = PowerMode::CUSTOM_POWER_SAVE_MODE;
184     powerMgrClient.SetDeviceMode(mode);
185     CommonEventManager::UnSubscribeCommonEvent(subscriberPtr);
186     POWER_HILOGI(LABEL_TEST, "PowerSetdevicemode_003 function end!");
187 }
188 } // namespace