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 "thermal_mock_stub_getdescriptor_test.h"
17 
18 #include "ipc_types.h"
19 #include "thermal_action_callback_ipc_interface_code.h"
20 #include "thermal_action_callback_stub.h"
21 #include "thermal_level_callback_ipc_interface_code.h"
22 #include "thermal_level_callback_stub.h"
23 #include "thermal_log.h"
24 #include "thermal_mgr_errors.h"
25 #include "thermal_srv_stub.h"
26 #include "thermal_temp_callback_ipc_interface_code.h"
27 #include "thermal_temp_callback_stub.h"
28 
29 using namespace testing::ext;
30 using namespace OHOS::PowerMgr;
31 using namespace OHOS;
32 using namespace std;
33 
34 namespace {
35 /**
36  * @tc.name: ThermalMockStubGetdescriptorTest001
37  * @tc.desc: stub test
38  * @tc.type: FUNC
39  * @tc.require: issueI5YZQ2
40  */
41 HWTEST_F(ThermalMockStubGetdescriptorTest, ThermalMockStubGetdescriptorTest001, TestSize.Level0)
42 {
43     THERMAL_HILOGD(LABEL_TEST, "ThermalMockStubGetdescriptorTest001 start.");
44     uint32_t code = 0;
45     MessageParcel data;
46     MessageParcel reply;
47     MessageOption option;
48     sptr<ThermalActionCallbackStub> actionStub = new ThermalActionCallbackStub();
49     ASSERT_FALSE(actionStub == nullptr);
50     EXPECT_TRUE(E_GET_THERMAL_SERVICE_FAILED == actionStub->OnRemoteRequest(code, data, reply, option));
51     sptr<ThermalLevelCallbackStub> levelStub = new ThermalLevelCallbackStub();
52     ASSERT_FALSE(levelStub == nullptr);
53     EXPECT_TRUE(E_GET_THERMAL_SERVICE_FAILED == levelStub->OnRemoteRequest(code, data, reply, option));
54     sptr<ThermalTempCallbackStub> tempStub = new ThermalTempCallbackStub();
55     ASSERT_FALSE(tempStub == nullptr);
56     EXPECT_TRUE(E_GET_THERMAL_SERVICE_FAILED == tempStub->OnRemoteRequest(code, data, reply, option));
57     THERMAL_HILOGD(LABEL_TEST, "ThermalMockStubGetdescriptorTest001 end.");
58 }
59 
60 /**
61  * @tc.name: ThermalMockStubGetdescriptorTest002
62  * @tc.desc: stub CallbackStub
63  * @tc.type: FUNC
64  */
65 HWTEST_F(ThermalMockStubGetdescriptorTest, ThermalMockStubGetdescriptorTest002, TestSize.Level0)
66 {
67     THERMAL_HILOGD(LABEL_TEST, "ThermalMockStubGetdescriptorTest002 start.");
68     sptr<ThermalActionCallbackStub> actionStub = new ThermalActionCallbackStub();
69     IThermalActionCallback::ActionCallbackMap actionCbMap;
70     EXPECT_TRUE(actionStub->OnThermalActionChanged(actionCbMap));
71 
72     sptr<ThermalLevelCallbackStub> levelStub = new ThermalLevelCallbackStub();
73     ThermalLevel level = ThermalLevel::COOL;
74     EXPECT_TRUE(levelStub->OnThermalLevelChanged(level));
75 
76     sptr<ThermalTempCallbackStub> tempStub = new ThermalTempCallbackStub();
77     IThermalTempCallback::TempCallbackMap tempCbMap;
78     EXPECT_TRUE(tempStub->OnThermalTempChanged(tempCbMap));
79 
80     THERMAL_HILOGD(LABEL_TEST, "ThermalMockStubGetdescriptorTest002 end.");
81 }
82 
83 /**
84  * @tc.name: ThermalMockStubGetdescriptorTest003
85  * @tc.desc: stub OnRemoteRequest Invalid code
86  * @tc.type: FUNC
87  */
88 HWTEST_F(ThermalMockStubGetdescriptorTest, ThermalMockStubGetdescriptorTest003, TestSize.Level0)
89 {
90     THERMAL_HILOGD(LABEL_TEST, "ThermalMockStubGetdescriptorTest003 start.");
91     uint32_t code = 9999;
92     MessageParcel data;
93     MessageParcel reply;
94     MessageOption option;
95 
96     sptr<ThermalActionCallbackStub> actionStub = new ThermalActionCallbackStub();
97     data.WriteInterfaceToken(ThermalActionCallbackStub::GetDescriptor());
98     int32_t ret = actionStub->OnRemoteRequest(code, data, reply, option);
99     EXPECT_EQ(ret, IPC_STUB_UNKNOW_TRANS_ERR) << "ret: " << ret << " code: " << code;
100 
101     sptr<ThermalLevelCallbackStub> levelStub = new ThermalLevelCallbackStub();
102     data.WriteInterfaceToken(ThermalLevelCallbackStub::GetDescriptor());
103     ret = levelStub->OnRemoteRequest(code, data, reply, option);
104     EXPECT_EQ(ret, IPC_STUB_UNKNOW_TRANS_ERR) << "ret: " << ret << " code: " << code;
105 
106     sptr<ThermalTempCallbackStub> tempStub = new ThermalTempCallbackStub();
107     data.WriteInterfaceToken(ThermalTempCallbackStub::GetDescriptor());
108     ret = tempStub->OnRemoteRequest(code, data, reply, option);
109     EXPECT_EQ(ret, IPC_STUB_UNKNOW_TRANS_ERR) << "ret: " << ret << " code: " << code;
110 
111     THERMAL_HILOGD(LABEL_TEST, "ThermalMockStubGetdescriptorTest003 end.");
112 }
113 
114 /**
115  * @tc.name: ThermalMockStubGetdescriptorTest004
116  * @tc.desc: stub ThermalTempCallbackStub OnRemoteRequest THERMAL_TEMPERATURE_CHANGED
117  * @tc.type: FUNC
118  */
119 HWTEST_F(ThermalMockStubGetdescriptorTest, ThermalMockStubGetdescriptorTest004, TestSize.Level0)
120 {
121     THERMAL_HILOGD(LABEL_TEST, "ThermalMockStubGetdescriptorTest004 start.");
122     sptr<ThermalTempCallbackStub> tempStub = new ThermalTempCallbackStub();
123     uint32_t code =
124         static_cast<uint32_t>(PowerMgr::ThermalTempCallbackInterfaceCode::THERMAL_TEMPERATURE_CHANGED);
125     MessageParcel data;
126     MessageParcel reply;
127     MessageOption option;
128     data.WriteInterfaceToken(ThermalTempCallbackStub::GetDescriptor());
129     data.WriteUint32(1);
130     data.WriteString("ThermalMockStubGetdescriptorTest004");
131     data.WriteInt32(1024);
132     int32_t ret = tempStub->OnRemoteRequest(code, data, reply, option);
133     EXPECT_EQ(ret, ERR_OK) << "ret: " << ret << " code: " << code;
134 
135     // Read Uint32 parcel error
136     data.WriteInterfaceToken(ThermalTempCallbackStub::GetDescriptor());
137     ret = tempStub->OnRemoteRequest(code, data, reply, option);
138     EXPECT_EQ(ret, E_READ_PARCEL_ERROR_THERMAL) << "ret: " << ret << " code: " << code;
139 
140     // Read String parcel error
141     data.WriteInterfaceToken(ThermalTempCallbackStub::GetDescriptor());
142     data.WriteUint32(1);
143     ret = tempStub->OnRemoteRequest(code, data, reply, option);
144     EXPECT_EQ(ret, E_READ_PARCEL_ERROR_THERMAL) << "ret: " << ret << " code: " << code;
145 
146     // Read Int32 parcel error
147     data.WriteInterfaceToken(ThermalTempCallbackStub::GetDescriptor());
148     data.WriteUint32(1);
149     data.WriteString("ThermalMockStubGetdescriptorTest004");
150     ret = tempStub->OnRemoteRequest(code, data, reply, option);
151     EXPECT_EQ(ret, E_READ_PARCEL_ERROR_THERMAL) << "ret: " << ret << " code: " << code;
152 
153     THERMAL_HILOGD(LABEL_TEST, "ThermalMockStubGetdescriptorTest004 end.");
154 }
155 
156 /**
157  * @tc.name: ThermalMockStubGetdescriptorTest005
158  * @tc.desc: stub ThermalActionCallbackStub OnRemoteRequest THERMAL_ACTION_CHANGED
159  * @tc.type: FUNC
160  */
161 HWTEST_F(ThermalMockStubGetdescriptorTest, ThermalMockStubGetdescriptorTest005, TestSize.Level0)
162 {
163     THERMAL_HILOGD(LABEL_TEST, "ThermalMockStubGetdescriptorTest005 start.");
164     uint32_t code =
165         static_cast<uint32_t>(PowerMgr::ThermalActionCallbackInterfaceCode::THERMAL_ACTION_CHANGED);
166     MessageParcel data;
167     MessageParcel reply;
168     MessageOption option;
169 
170     sptr<ThermalActionCallbackStub> actionStub = new ThermalActionCallbackStub();
171     data.WriteInterfaceToken(ThermalActionCallbackStub::GetDescriptor());
172     data.WriteUint32(1);
173     data.WriteString("ThermalMockStubGetdescriptorTest005");
174     data.WriteString("3.14");
175     int32_t ret = actionStub->OnRemoteRequest(code, data, reply, option);
176     EXPECT_EQ(ret, ERR_OK) << "ret: " << ret << " code: " << code;
177 
178     // Read Uint32 parcel error
179     data.WriteInterfaceToken(ThermalActionCallbackStub::GetDescriptor());
180     ret = actionStub->OnRemoteRequest(code, data, reply, option);
181     EXPECT_EQ(ret, E_READ_PARCEL_ERROR_THERMAL) << "ret: " << ret << " code: " << code;
182 
183     // Read String parcel error
184     data.WriteInterfaceToken(ThermalActionCallbackStub::GetDescriptor());
185     data.WriteUint32(1);
186     ret = actionStub->OnRemoteRequest(code, data, reply, option);
187     EXPECT_EQ(ret, E_READ_PARCEL_ERROR_THERMAL) << "ret: " << ret << " code: " << code;
188 
189     // Read Float parcel error
190     data.WriteInterfaceToken(ThermalActionCallbackStub::GetDescriptor());
191     data.WriteUint32(1);
192     data.WriteString("ThermalMockStubGetdescriptorTest005");
193     ret = actionStub->OnRemoteRequest(code, data, reply, option);
194     EXPECT_EQ(ret, E_READ_PARCEL_ERROR_THERMAL) << "ret: " << ret << " code: " << code;
195 
196     THERMAL_HILOGD(LABEL_TEST, "ThermalMockStubGetdescriptorTest005 end.");
197 }
198 
199 /**
200  * @tc.name: ThermalMockStubGetdescriptorTest006
201  * @tc.desc: stub ThermalLevelCallbackStub OnRemoteRequest THERMAL_LEVEL_CHANGED Read Int32 parcel error
202  * @tc.type: FUNC
203  */
204 HWTEST_F(ThermalMockStubGetdescriptorTest, ThermalMockStubGetdescriptorTest006, TestSize.Level0)
205 {
206     THERMAL_HILOGD(LABEL_TEST, "ThermalMockStubGetdescriptorTest006 start.");
207     uint32_t code =
208         static_cast<uint32_t>(PowerMgr::ThermalLevelCallbackInterfaceCode::THERMAL_LEVEL_CHANGED);
209     MessageParcel data;
210     MessageParcel reply;
211     MessageOption option;
212 
213     sptr<ThermalLevelCallbackStub> actionStub = new ThermalLevelCallbackStub();
214     data.WriteInterfaceToken(ThermalLevelCallbackStub::GetDescriptor());
215     int32_t ret = actionStub->OnRemoteRequest(code, data, reply, option);
216     EXPECT_EQ(ret, E_READ_PARCEL_ERROR_THERMAL) << "ret: " << ret << " code: " << code;
217 
218     THERMAL_HILOGD(LABEL_TEST, "ThermalMockStubGetdescriptorTest006 end.");
219 }
220 } // namespace
221