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 */
58 static HWTEST_F(BatterySrvStubTest, BatterySrvStub001, TestSize.Level1)
59 {
60 BATTERY_HILOGD(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_HILOGD(LABEL_TEST, "BatterySrvStub001 end.");
66 }
67
68 /**
69 * @tc.name: BatteryClient020
70 * @tc.desc: Test ResetProxy code
71 * @tc.type: FUNC
72 * @tc.require: issueI6KRS8
73 */
74 static HWTEST_F(BatterySrvStubTest, BatterySrvStub002, TestSize.Level1)
75 {
76 BATTERY_HILOGD(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_HILOGD(LABEL_TEST, "BatterySrvStub002 end.");
86 }
87
88 /**
89 * @tc.name: BatteryClient020
90 * @tc.desc: Test ResetProxy Invalid code
91 * @tc.type: FUNC
92 * @tc.require: issueI6KRS8
93 */
94 static HWTEST_F(BatterySrvStubTest, BatterySrvStub003, TestSize.Level1)
95 {
96 BATTERY_HILOGD(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_HILOGD(LABEL_TEST, "BatterySrvStub003 end.");
102 }
103 } // namespace
104