1 /*
2 * Copyright (c) 2021-2023 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 "power_device_mode_test.h"
17
18 #include <iostream>
19
20 #include <datetime_ex.h>
21 #include <gtest/gtest.h>
22 #include <if_system_ability_manager.h>
23 #include <ipc_skeleton.h>
24 #include <string_ex.h>
25
26 #include "power_common.h"
27 #include "power_mgr_client.h"
28 #include "power_mgr_service.h"
29 #include "power_state_machine.h"
30
31 using namespace testing::ext;
32 using namespace OHOS::PowerMgr;
33 using namespace OHOS;
34 using namespace std;
35
36
OnPowerModeChanged(PowerMode mode)37 void PowerDeviceModeTest::PowerModeTest1Callback::OnPowerModeChanged(PowerMode mode)
38 {
39 POWER_HILOGD(LABEL_TEST, "PowerModeTest1Callback::OnPowerModeChanged.");
40 }
41
OnPowerModeChanged(PowerMode mode)42 void PowerDeviceModeTest::PowerModeTest2Callback::OnPowerModeChanged(PowerMode mode)
43 {
44 POWER_HILOGD(LABEL_TEST, "PowerModeTest2Callback::OnPowerModeChanged.");
45 }
46
47 namespace {
48 /**
49 * @tc.name: PowerStateCallback001
50 * @tc.desc: test PowerStateCallback
51 * @tc.type: FUNC
52 */
53 HWTEST_F (PowerDeviceModeTest, PowerDeviceModeCallback001, TestSize.Level0)
54 {
55 bool ret = 0;
56 auto& powerMgrClient = PowerMgrClient::GetInstance();
57 sptr<IPowerModeCallback> cb1 = new PowerModeTest1Callback();
58 ret = powerMgrClient.RegisterPowerModeCallback(cb1);
59 EXPECT_TRUE(ret);
60 ret = 0;
61 POWER_HILOGD(LABEL_TEST, "PowerDeviceModeCallback001 1.");
62 {
63 sptr<IPowerModeCallback> cb2 = new PowerModeTest2Callback();
64 ret = powerMgrClient.UnRegisterPowerModeCallback(cb2);
65 EXPECT_TRUE(ret);
66 ret = 0;
67 POWER_HILOGD(LABEL_TEST, "PowerDeviceModeCallback001 2.");
68 ret = powerMgrClient.RegisterPowerModeCallback(cb2);
69 EXPECT_TRUE(ret);
70 ret = 0;
71 POWER_HILOGD(LABEL_TEST, "PowerDeviceModeCallback001 3.");
72 ret = powerMgrClient.RegisterPowerModeCallback(cb2);
73 EXPECT_TRUE(ret);
74 ret = 0;
75 POWER_HILOGD(LABEL_TEST, "PowerDeviceModeCallback001 4.");
76 }
77 ret = powerMgrClient.UnRegisterPowerModeCallback(cb1);
78 EXPECT_TRUE(ret);
79 ret = 0;
80 POWER_HILOGD(LABEL_TEST, "PowerDeviceModeTest::PowerDeviceModeCallback001 end.");
81 }
82
83 /**
84 * @tc.name: SetDeviceModeTest001
85 * @tc.desc: test SetDeviceMode in proxy
86 * @tc.type: FUNC
87 */
88 HWTEST_F (PowerDeviceModeTest, SetDeviceModeTest001, TestSize.Level2)
89 {
90 sleep(SLEEP_WAIT_TIME_S);
91 GTEST_LOG_(INFO) << "SetDeviceModeTest001: SetDeviceMode start.";
92 auto& powerMgrClient = PowerMgrClient::GetInstance();
93
94 PowerMode mode1 = powerMgrClient.GetDeviceMode();
95 PowerMode mode = PowerMode::POWER_SAVE_MODE;
96 powerMgrClient.SetDeviceMode(mode);
97 EXPECT_EQ(powerMgrClient.GetDeviceMode(), mode);
98 powerMgrClient.SetDeviceMode(mode1);
99
100 GTEST_LOG_(INFO) << "SetDeviceModeTest001: SetDeviceMode end.";
101 }
102
103 /**
104 * @tc.name: GetDeviceModeTest001
105 * @tc.desc: test GetDeviceMode in proxy
106 * @tc.type: FUNC
107 */
108 HWTEST_F (PowerDeviceModeTest, GetDeviceModeTest001, TestSize.Level2)
109 {
110 PowerMode mode = PowerMode::NORMAL_MODE;
111 PowerMode mode1 = PowerMode::NORMAL_MODE;
112 PowerMode mode2 = PowerMode::NORMAL_MODE;
113 sleep(SLEEP_WAIT_TIME_S);
114 GTEST_LOG_(INFO) << "GetDeviceModeTest001: GetDeviceMode start.";
115 auto& powerMgrClient = PowerMgrClient::GetInstance();
116 mode = powerMgrClient.GetDeviceMode();
117
118 powerMgrClient.SetDeviceMode(mode1);
119 mode2 = powerMgrClient.GetDeviceMode();
120 EXPECT_EQ(mode1, mode2);
121 powerMgrClient.SetDeviceMode(mode);
122 }
123 }