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_state_machine.h"
32 #include "power_state_machine_info.h"
33 #include "running_lock.h"
34 #include "running_lock_info.h"
35
36 using namespace testing::ext;
37 using namespace OHOS::PowerMgr;
38 using namespace OHOS::EventFwk;
39 using namespace OHOS;
40 using namespace std;
41
42 namespace {
43 static int32_t g_sleepTime;
44 const int32_t POWER_SAVEMODE_TIME = 8000;
45 const int32_t POWER_MODE_MIN_TIME = 20000;
46 const int32_t EXTREME_POWER_SAVE_MODE_TIME = 5000;
47 const int32_t PERFORMANCE_MODE_TIME = -1;
48 const int32_t DISPLAY_OFF_ID = 101;
49 } // namespace
50
SetUpTestCase(void)51 void PowerMgrSystemTest::SetUpTestCase(void)
52 {
53 system("mount -o rw,remount /vendor");
54 }
55
UpdateGlobalSleepTime(std::list<ModePolicy> & info)56 static void UpdateGlobalSleepTime(std::list<ModePolicy>& info)
57 {
58 for (std::list<ModePolicy>::iterator it = info.begin(); it != info.end(); ++it) {
59 if (it->id == DISPLAY_OFF_ID) {
60 g_sleepTime = it->value;
61 }
62 }
63 }
64
SetPolicyMode(const int32_t & proxyId)65 static void SetPolicyMode(const int32_t& proxyId)
66 {
67 std::map<int32_t, std::list<ModePolicy>> policyCache;
68 std::unique_ptr<PowerSaveMode> mode = std::make_unique<PowerSaveMode>();
69 policyCache = mode->GetPolicyCache();
70 for (auto info = policyCache.begin(); info != policyCache.end(); ++info) {
71 if (info->first == proxyId) {
72 UpdateGlobalSleepTime(info->second);
73 }
74 }
75 }
76
77 namespace {
78 /**
79 * @tc.name: PowerMgrSystemTest_001
80 * @tc.desc: test SetDeviceMode in proxy
81 * @tc.type: FUNC
82 */
83 HWTEST_F(PowerMgrSystemTest, PowerMgrSystemTest_001, TestSize.Level2)
84 {
85 PowerMode mode = PowerMode::POWER_SAVE_MODE;
86 SetPolicyMode(static_cast<uint32_t>(mode));
87 EXPECT_EQ(g_sleepTime, POWER_SAVEMODE_TIME) << "PowerMgrSystemTest_001 fail to SetDeviceMode";
88 }
89
90 /**
91 * @tc.name: PowerMgrSystemTest_002
92 * @tc.desc: test SetDeviceMode in proxy
93 * @tc.type: FUNC
94 */
95 HWTEST_F(PowerMgrSystemTest, PowerMgrSystemTest_002, TestSize.Level2)
96 {
97 PowerMode mode = PowerMode::POWER_MODE_MIN;
98 SetPolicyMode(static_cast<uint32_t>(mode));
99 EXPECT_EQ(g_sleepTime, POWER_MODE_MIN_TIME) << "PowerMgrSystemTest_002 fail to SetDeviceMode";
100 }
101
102 /**
103 * @tc.name: PowerMgrSystemTest_003
104 * @tc.desc: test SetDeviceMode in proxy
105 * @tc.type: FUNC
106 */
107 HWTEST_F(PowerMgrSystemTest, PowerMgrSystemTest_003, TestSize.Level2)
108 {
109 PowerMode mode = PowerMode::PERFORMANCE_MODE;
110 SetPolicyMode(static_cast<uint32_t>(mode));
111 EXPECT_EQ(g_sleepTime, PERFORMANCE_MODE_TIME) << "PowerMgrSystemTest_003 fail to SetDeviceMode";
112 }
113
114 /**
115 * @tc.name: PowerMgrSystemTest_004
116 * @tc.desc: test SetDeviceMode in proxy
117 * @tc.type: FUNC
118 */
119 HWTEST_F(PowerMgrSystemTest, PowerMgrSystemTest_004, TestSize.Level2)
120 {
121 PowerMode mode = PowerMode::EXTREME_POWER_SAVE_MODE;
122 SetPolicyMode(static_cast<uint32_t>(mode));
123 EXPECT_EQ(g_sleepTime, EXTREME_POWER_SAVE_MODE_TIME) << "PowerMgrSystemTest_004 fail to SetDeviceMode";
124 }
125 } // namespace