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_log.h"
28 #include "power_mgr_client.h"
29 #include "power_mgr_service.h"
30 #include "power_mgr_system_test.h"
31 #include "power_mode_policy.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 static int32_t g_sleepTime;
45 static int32_t g_getSleepTimeResult;
46 const int32_t SLEEPTIME_ID = PowerModePolicy::ServiceType::SLEEPTIME;
47 } // namespace
48 
SetUpTestCase(void)49 void PowerMgrSystemTest::SetUpTestCase(void)
50 {
51     system("mount -o rw,remount /vendor");
52 }
53 
UpdateGlobalSleepTime(std::list<ModePolicy> & info)54 static void UpdateGlobalSleepTime(std::list<ModePolicy>& info)
55 {
56     for (std::list<ModePolicy>::iterator it = info.begin(); it != info.end(); ++it) {
57         if (it->id == SLEEPTIME_ID) {
58             g_sleepTime = it->value;
59         }
60     }
61 }
62 
SetPolicyMode(const int32_t & proxyId)63 static void SetPolicyMode(const int32_t& proxyId)
64 {
65     std::map<int32_t, std::list<ModePolicy>> policyCache;
66     std::unique_ptr<PowerSaveMode> mode = std::make_unique<PowerSaveMode>();
67     policyCache = mode->GetPolicyCache();
68     for (auto info = policyCache.begin(); info != policyCache.end(); ++info) {
69         if (info->first == proxyId) {
70             UpdateGlobalSleepTime(info->second);
71         }
72     }
73     g_getSleepTimeResult = mode->GetSleepTime(proxyId);
74 }
75 
76 namespace {
77 /**
78  * @tc.name: PowerMgrSystemTest_001
79  * @tc.desc: test SetDeviceMode in proxy
80  * @tc.type: FUNC
81  */
82 HWTEST_F(PowerMgrSystemTest, PowerMgrSystemTest_001, TestSize.Level2)
83 {
84     PowerMode mode = PowerMode::POWER_SAVE_MODE;
85     SetPolicyMode(static_cast<uint32_t>(mode));
86     EXPECT_EQ(g_sleepTime, g_getSleepTimeResult) << "PowerMgrSystemTest_001 fail to SetDeviceMode";
87 }
88 
89 /**
90  * @tc.name: PowerMgrSystemTest_002
91  * @tc.desc: test SetDeviceMode in proxy
92  * @tc.type: FUNC
93  */
94 HWTEST_F(PowerMgrSystemTest, PowerMgrSystemTest_002, TestSize.Level2)
95 {
96     PowerMode mode = PowerMode::POWER_MODE_MIN;
97     SetPolicyMode(static_cast<uint32_t>(mode));
98     EXPECT_EQ(g_sleepTime, g_getSleepTimeResult) << "PowerMgrSystemTest_002 fail to SetDeviceMode";
99 }
100 
101 /**
102  * @tc.name: PowerMgrSystemTest_003
103  * @tc.desc: test SetDeviceMode in proxy
104  * @tc.type: FUNC
105  */
106 HWTEST_F(PowerMgrSystemTest, PowerMgrSystemTest_003, TestSize.Level2)
107 {
108     PowerMode mode = PowerMode::PERFORMANCE_MODE;
109     SetPolicyMode(static_cast<uint32_t>(mode));
110     EXPECT_EQ(g_sleepTime, g_getSleepTimeResult) << "PowerMgrSystemTest_003 fail to SetDeviceMode";
111 }
112 
113 /**
114  * @tc.name: PowerMgrSystemTest_004
115  * @tc.desc: test SetDeviceMode in proxy
116  * @tc.type: FUNC
117  */
118 HWTEST_F(PowerMgrSystemTest, PowerMgrSystemTest_004, TestSize.Level2)
119 {
120     PowerMode mode = PowerMode::EXTREME_POWER_SAVE_MODE;
121     SetPolicyMode(static_cast<uint32_t>(mode));
122     EXPECT_EQ(g_sleepTime, g_getSleepTimeResult) << "PowerMgrSystemTest_004 fail to SetDeviceMode";
123 }
124 } // namespace
125