• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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     std::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     POWER_HILOGI(LABEL_TEST, "PowerMgrSystemTest_001 function start!");
85     PowerMode mode = PowerMode::POWER_SAVE_MODE;
86     SetPolicyMode(static_cast<uint32_t>(mode));
87     EXPECT_EQ(g_sleepTime, g_getSleepTimeResult) << "PowerMgrSystemTest_001 fail to SetDeviceMode";
88     POWER_HILOGI(LABEL_TEST, "PowerMgrSystemTest_001 function end!");
89 }
90 
91 /**
92  * @tc.name: PowerMgrSystemTest_002
93  * @tc.desc: test SetDeviceMode in proxy
94  * @tc.type: FUNC
95  */
96 HWTEST_F(PowerMgrSystemTest, PowerMgrSystemTest_002, TestSize.Level2)
97 {
98     POWER_HILOGI(LABEL_TEST, "PowerMgrSystemTest_002 function start!");
99     PowerMode mode = PowerMode::POWER_MODE_MIN;
100     SetPolicyMode(static_cast<uint32_t>(mode));
101     EXPECT_EQ(g_sleepTime, g_getSleepTimeResult) << "PowerMgrSystemTest_002 fail to SetDeviceMode";
102     POWER_HILOGI(LABEL_TEST, "PowerMgrSystemTest_002 function end!");
103 }
104 
105 /**
106  * @tc.name: PowerMgrSystemTest_003
107  * @tc.desc: test SetDeviceMode in proxy
108  * @tc.type: FUNC
109  */
110 HWTEST_F(PowerMgrSystemTest, PowerMgrSystemTest_003, TestSize.Level2)
111 {
112     POWER_HILOGI(LABEL_TEST, "PowerMgrSystemTest_003 function start!");
113     PowerMode mode = PowerMode::PERFORMANCE_MODE;
114     SetPolicyMode(static_cast<uint32_t>(mode));
115     EXPECT_EQ(g_sleepTime, g_getSleepTimeResult) << "PowerMgrSystemTest_003 fail to SetDeviceMode";
116     POWER_HILOGI(LABEL_TEST, "PowerMgrSystemTest_003 function end!");
117 }
118 
119 /**
120  * @tc.name: PowerMgrSystemTest_004
121  * @tc.desc: test SetDeviceMode in proxy
122  * @tc.type: FUNC
123  */
124 HWTEST_F(PowerMgrSystemTest, PowerMgrSystemTest_004, TestSize.Level2)
125 {
126     POWER_HILOGI(LABEL_TEST, "PowerMgrSystemTest_004 function start!");
127     PowerMode mode = PowerMode::EXTREME_POWER_SAVE_MODE;
128     SetPolicyMode(static_cast<uint32_t>(mode));
129     EXPECT_EQ(g_sleepTime, g_getSleepTimeResult) << "PowerMgrSystemTest_004 fail to SetDeviceMode";
130     POWER_HILOGI(LABEL_TEST, "PowerMgrSystemTest_004 function end!");
131 }
132 } // namespace
133