1 /*
2  * Copyright (c) 2022-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 "battery_srv_stub_test.h"
17 
18 #include "ipc_types.h"
19 #include "parcel.h"
20 
21 #include "battery_log.h"
22 #include "battery_manager_ipc_interface_code.h"
23 #include "battery_service.h"
24 #include "battery_srv_proxy.h"
25 #include "ibattery_srv.h"
26 #include "power_mgr_errors.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS::PowerMgr;
30 using namespace OHOS;
31 using namespace std;
32 
33 namespace {
34 sptr<BatteryService> g_service;
35 MessageParcel g_reply;
36 MessageOption g_option;
37 MessageParcel g_data;
38 } // namespace
39 
SetUpTestCase()40 void BatterySrvStubTest::SetUpTestCase()
41 {
42     g_service = DelayedSpSingleton<BatteryService>::GetInstance();
43     g_service->OnStart();
44 }
45 
TearDownTestCase()46 void BatterySrvStubTest::TearDownTestCase()
47 {
48     g_service = nullptr;
49 }
50 
51 namespace {
52 /**
53  * @tc.name: BatterySrvStub001
54  * @tc.desc: Test functions OnRemoteRequest no WriteInterfaceToken
55  * @tc.type: FUNC
56  * @tc.require: issueI6KRS8
57  */
HWTEST_F(BatterySrvStubTest, BatterySrvStub001, TestSize.Level1)58 static HWTEST_F(BatterySrvStubTest, BatterySrvStub001, TestSize.Level1)
59 {
60     BATTERY_HILOGI(LABEL_TEST, "BatterySrvStub001 start.");
61     uint32_t code = 0;
62     MessageParcel data;
63     int32_t ret = g_service->OnRemoteRequest(code, data, g_reply, g_option);
64     EXPECT_EQ(ret, E_GET_POWER_SERVICE_FAILED);
65     BATTERY_HILOGI(LABEL_TEST, "BatterySrvStub001 end.");
66 }
67 
68 /**
69  * @tc.name: BatterySrvStub002
70  * @tc.desc: Test BatterySrvInterfaceCode BATT_GET_CAPACITY to BATT_GET_BATTERY_REMAIN_ENERGY code
71  * @tc.type: FUNC
72  * @tc.require: issueI6KRS8
73  */
HWTEST_F(BatterySrvStubTest, BatterySrvStub002, TestSize.Level1)74 static HWTEST_F(BatterySrvStubTest, BatterySrvStub002, TestSize.Level1)
75 {
76     BATTERY_HILOGI(LABEL_TEST, "BatterySrvStub002 start.");
77     uint32_t begin = static_cast<uint32_t>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_CAPACITY);
78     uint32_t end = static_cast<uint32_t>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_REMAIN_ENERGY);
79     for (uint32_t code = begin; code <= end; ++code) {
80         g_data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor());
81         int32_t ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
82         EXPECT_EQ(ret, ERR_OK) << "ret: " << ret << " code: " << code;
83     }
84 
85     BATTERY_HILOGI(LABEL_TEST, "BatterySrvStub002 end.");
86 }
87 
88 /**
89  * @tc.name: BatterySrvStub003
90  * @tc.desc: Test ResetProxy Invalid code
91  * @tc.type: FUNC
92  * @tc.require: issueI6KRS8
93  */
HWTEST_F(BatterySrvStubTest, BatterySrvStub003, TestSize.Level1)94 static HWTEST_F(BatterySrvStubTest, BatterySrvStub003, TestSize.Level1)
95 {
96     BATTERY_HILOGI(LABEL_TEST, "BatterySrvStub003 start.");
97     uint32_t code = -100;
98     g_data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor());
99     int32_t ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
100     EXPECT_EQ(ret, IPC_STUB_UNKNOW_TRANS_ERR) << "ret: " << ret << " code: " << code;
101     BATTERY_HILOGI(LABEL_TEST, "BatterySrvStub003 end.");
102 }
103 
104 /**
105  * @tc.name: BatterySrvStub004
106  * @tc.desc: Test BatterySrvInterfaceCode SET_BATTERY_CONFIG
107  * @tc.type: FUNC
108  * @tc.require: issueI6KRS8
109  */
HWTEST_F(BatterySrvStubTest, BatterySrvStub004, TestSize.Level1)110 static HWTEST_F(BatterySrvStubTest, BatterySrvStub004, TestSize.Level1)
111 {
112     BATTERY_HILOGI(LABEL_TEST, "BatterySrvStub004 start.");
113     g_data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor());
114     u16string sceneName = Str8ToStr16("BatterySrvStub004");
115     u16string value = Str8ToStr16("0");
116     g_data.WriteString16(sceneName);
117     g_data.WriteString16(value);
118     uint32_t code = static_cast<uint32_t>(PowerMgr::BatterySrvInterfaceCode::SET_BATTERY_CONFIG);
119     int32_t ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
120     EXPECT_EQ(ret, ERR_OK) << "ret: " << ret << " code: " << code;
121     BATTERY_HILOGI(LABEL_TEST, "BatterySrvStub004 end.");
122 }
123 
124 /**
125  * @tc.name: BatterySrvStub005
126  * @tc.desc: Test BatterySrvInterfaceCode GET_BATTERY_CONFIG
127  * @tc.type: FUNC
128  * @tc.require: issueI6KRS8
129  */
HWTEST_F(BatterySrvStubTest, BatterySrvStub005, TestSize.Level1)130 static HWTEST_F(BatterySrvStubTest, BatterySrvStub005, TestSize.Level1)
131 {
132     BATTERY_HILOGI(LABEL_TEST, "BatterySrvStub005 start.");
133     g_data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor());
134     u16string sceneName = Str8ToStr16("BatterySrvStub005");
135     g_data.WriteString16(sceneName);
136     uint32_t code = static_cast<uint32_t>(PowerMgr::BatterySrvInterfaceCode::GET_BATTERY_CONFIG);
137     int32_t ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
138     EXPECT_EQ(ret, ERR_OK) << "ret: " << ret << " code: " << code;
139     BATTERY_HILOGI(LABEL_TEST, "BatterySrvStub005 end.");
140 }
141 
142 /**
143  * @tc.name: BatterySrvStub006
144  * @tc.desc: Test BatterySrvInterfaceCode SUPPORT_BATTERY_CONFIG
145  * @tc.type: FUNC
146  * @tc.require: issueI6KRS8
147  */
HWTEST_F(BatterySrvStubTest, BatterySrvStub006, TestSize.Level1)148 static HWTEST_F(BatterySrvStubTest, BatterySrvStub006, TestSize.Level1)
149 {
150     BATTERY_HILOGI(LABEL_TEST, "BatterySrvStub006 start.");
151     g_data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor());
152     u16string sceneName = Str8ToStr16("BatterySrvStub006");
153     g_data.WriteString16(sceneName);
154     uint32_t code = static_cast<uint32_t>(PowerMgr::BatterySrvInterfaceCode::SUPPORT_BATTERY_CONFIG);
155     int32_t ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
156     EXPECT_EQ(ret, ERR_OK) << "ret: " << ret << " code: " << code;
157     BATTERY_HILOGI(LABEL_TEST, "BatterySrvStub006 end.");
158 }
159 } // namespace
160