1 /*
2  * Copyright (c) 2021-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 "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  */
HWTEST_F(PowerDeviceModeTest, PowerDeviceModeCallback001, TestSize.Level0)53 HWTEST_F (PowerDeviceModeTest, PowerDeviceModeCallback001, TestSize.Level0)
54 {
55     POWER_HILOGD(LABEL_TEST, "PowerDeviceModeTest::PowerDeviceModeCallback001 start.");
56     bool ret = 0;
57     auto& powerMgrClient = PowerMgrClient::GetInstance();
58     sptr<IPowerModeCallback> cb1 = new PowerModeTest1Callback();
59     ret = powerMgrClient.RegisterPowerModeCallback(cb1);
60     EXPECT_TRUE(ret);
61     ret = 0;
62     POWER_HILOGD(LABEL_TEST, "PowerDeviceModeCallback001 1.");
63     {
64         sptr<IPowerModeCallback> cb2 = new PowerModeTest2Callback();
65         ret = powerMgrClient.UnRegisterPowerModeCallback(cb2);
66         EXPECT_TRUE(ret);
67         ret = 0;
68         POWER_HILOGD(LABEL_TEST, "PowerDeviceModeCallback001 2.");
69         ret = powerMgrClient.RegisterPowerModeCallback(cb2);
70         EXPECT_TRUE(ret);
71         ret = 0;
72         POWER_HILOGD(LABEL_TEST, "PowerDeviceModeCallback001 3.");
73         ret = powerMgrClient.RegisterPowerModeCallback(cb2);
74         EXPECT_TRUE(ret);
75         ret = 0;
76         POWER_HILOGD(LABEL_TEST, "PowerDeviceModeCallback001 4.");
77     }
78     ret = powerMgrClient.UnRegisterPowerModeCallback(cb1);
79     EXPECT_TRUE(ret);
80     ret = 0;
81     POWER_HILOGD(LABEL_TEST, "PowerDeviceModeTest::PowerDeviceModeCallback001 end.");
82 }
83 
84 /**
85  * @tc.name: SetDeviceModeTest001
86  * @tc.desc: test SetDeviceMode in proxy
87  * @tc.type: FUNC
88  */
HWTEST_F(PowerDeviceModeTest, SetDeviceModeTest001, TestSize.Level2)89 HWTEST_F (PowerDeviceModeTest, SetDeviceModeTest001, TestSize.Level2)
90 {
91     POWER_HILOGD(LABEL_TEST, "SetDeviceModeTest001 start");
92     sleep(SLEEP_WAIT_TIME_S);
93     GTEST_LOG_(INFO) << "SetDeviceModeTest001: SetDeviceMode start.";
94     auto& powerMgrClient = PowerMgrClient::GetInstance();
95 
96     PowerMode mode1 = powerMgrClient.GetDeviceMode();
97     PowerMode mode = PowerMode::POWER_SAVE_MODE;
98     powerMgrClient.SetDeviceMode(mode);
99     EXPECT_EQ(powerMgrClient.GetDeviceMode(), mode);
100     powerMgrClient.SetDeviceMode(mode1);
101     POWER_HILOGD(LABEL_TEST, "SetDeviceModeTest001 end");
102     GTEST_LOG_(INFO) << "SetDeviceModeTest001: SetDeviceMode end.";
103 }
104 
105 /**
106  * @tc.name: GetDeviceModeTest001
107  * @tc.desc: test GetDeviceMode in proxy
108  * @tc.type: FUNC
109  */
HWTEST_F(PowerDeviceModeTest, GetDeviceModeTest001, TestSize.Level2)110 HWTEST_F (PowerDeviceModeTest, GetDeviceModeTest001, TestSize.Level2)
111 {
112     POWER_HILOGD(LABEL_TEST, "GetDeviceModeTest001 start");
113     PowerMode mode = PowerMode::NORMAL_MODE;
114     PowerMode mode1 = PowerMode::NORMAL_MODE;
115     PowerMode mode2 = PowerMode::NORMAL_MODE;
116     sleep(SLEEP_WAIT_TIME_S);
117     GTEST_LOG_(INFO) << "GetDeviceModeTest001: GetDeviceMode start.";
118     auto& powerMgrClient = PowerMgrClient::GetInstance();
119     mode = powerMgrClient.GetDeviceMode();
120 
121     powerMgrClient.SetDeviceMode(mode1);
122     mode2 = powerMgrClient.GetDeviceMode();
123     EXPECT_EQ(mode1, mode2);
124     powerMgrClient.SetDeviceMode(mode);
125     POWER_HILOGD(LABEL_TEST, "GetDeviceModeTest001 end");
126 }
127 }