1 /*
2 * Copyright (c) 2024-2025 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 <gtest/gtest.h>
17
18 #include "devicestatus_callback_stub.h"
19 #include "devicestatus_define.h"
20 #include "fi_log.h"
21 #include "stationary_params.h"
22 #define private public
23 #include "stationary_server.h"
24 #undef private
25 #include "ipc_skeleton.h"
26
27 #undef LOG_TAG
28 #define LOG_TAG "StationaryServerTest"
29
30 namespace OHOS {
31 namespace Msdp {
32 namespace DeviceStatus {
33 using namespace testing::ext;
34 CallingContext context_ {
35 .intention = Intention::STATIONARY,
36 .tokenId = IPCSkeleton::GetCallingTokenID(),
37 .uid = IPCSkeleton::GetCallingUid(),
38 .pid = IPCSkeleton::GetCallingPid(),
39 };
40 StationaryServer stationary_;
41 int32_t TYPE_TYPE_STAND = 7;
42 int32_t ACTIVITYEVENT_ENTER = 1;
43 int32_t REPORTLATENCYNS_LATENCY_INVALID = -1;
44 int32_t RET_NO_SUPPORT = 801;
45 constexpr float DOUBLEPIMAX = 6.3F;
46 class StationaryServerTest : public testing::Test {
47 public:
SetUpTestCase()48 static void SetUpTestCase() {};
TearDownTestCase()49 static void TearDownTestCase() {};
SetUp()50 void SetUp() {};
TearDown()51 void TearDown() {};
52 public:
53 class StationaryServerTestCallback : public DeviceStatusCallbackStub {
54 public:
55 void OnDeviceStatusChanged(const Data& devicestatusData);
56 };
57 };
58
OnDeviceStatusChanged(const Data & devicestatusData)59 void StationaryServerTest::StationaryServerTestCallback::OnDeviceStatusChanged(const
60 Data& devicestatusData)
61 {
62 GTEST_LOG_(INFO) << "StationaryServerTestCallback type: " << devicestatusData.type;
63 GTEST_LOG_(INFO) << "StationaryServerTestCallback value: " << devicestatusData.value;
64 EXPECT_TRUE(devicestatusData.type == Type::TYPE_VERTICAL_POSITION &&
65 devicestatusData.value >= OnChangedValue::VALUE_INVALID &&
66 devicestatusData.value <= OnChangedValue::VALUE_EXIT) << "StationaryServerTestCallback failed";
67 }
68
69 /**
70 * @tc.name: SubscribeStationaryCallbackTest001
71 * @tc.desc: Test func named subscribeStationaryCallback
72 * @tc.type: FUNC
73 */
74 HWTEST_F(StationaryServerTest, SubscribeStationaryCallbackTest001, TestSize.Level0)
75 {
76 CALL_TEST_DEBUG;
77 sptr<IRemoteDevStaCallback> callback = new (std::nothrow) StationaryServerTestCallback();
78 ASSERT_NE(callback, nullptr);
79 int32_t ret = stationary_.SubscribeStationaryCallback(
80 context_, TYPE_TYPE_STAND, ACTIVITYEVENT_ENTER, REPORTLATENCYNS_LATENCY_INVALID, callback);
81 EXPECT_EQ(ret, RET_NO_SUPPORT);
82 }
83
84 /**
85 * @tc.name: SubscribeStationaryCallbackTest002
86 * @tc.desc: Test func named subscribeStationaryCallback
87 * @tc.type: FUNC
88 */
89 HWTEST_F(StationaryServerTest, SubscribeStationaryCallbackTest002, TestSize.Level0)
90 {
91 CALL_TEST_DEBUG;
92 sptr<IRemoteDevStaCallback> callback = new (std::nothrow) StationaryServerTestCallback();
93 ASSERT_NE(callback, nullptr);
94 int32_t ret = stationary_.SubscribeStationaryCallback(
95 context_, TYPE_INVALID, ACTIVITYEVENT_ENTER, REPORTLATENCYNS_LATENCY_INVALID, callback);
96 EXPECT_EQ(ret, RET_OK);
97 }
98
99 /**
100 * @tc.name: UnsubscribeStationaryCallbackTest001
101 * @tc.desc: Test func named unsubscribeStationaryCallback
102 * @tc.type: FUNC
103 */
104 HWTEST_F(StationaryServerTest, UnsubscribeStationaryCallbackTest001, TestSize.Level0)
105 {
106 CALL_TEST_DEBUG;
107 sptr<IRemoteDevStaCallback> callback = new (std::nothrow) StationaryServerTestCallback();
108 ASSERT_NE(callback, nullptr);
109 int32_t ret = stationary_.UnsubscribeStationaryCallback(
110 context_, TYPE_TYPE_STAND, REPORTLATENCYNS_LATENCY_INVALID, callback);
111 EXPECT_EQ(ret, RET_NO_SUPPORT);
112 }
113
114 /**
115 * @tc.name: UnsubscribeStationaryCallbackTest002
116 * @tc.desc: Test func named unsubscribeStationaryCallback
117 * @tc.type: FUNC
118 */
119 HWTEST_F(StationaryServerTest, UnsubscribeStationaryCallbackTest002, TestSize.Level0)
120 {
121 CALL_TEST_DEBUG;
122 sptr<IRemoteDevStaCallback> callback = new (std::nothrow) StationaryServerTestCallback();
123 ASSERT_NE(callback, nullptr);
124 int32_t ret = stationary_.UnsubscribeStationaryCallback(
125 context_, TYPE_INVALID, REPORTLATENCYNS_LATENCY_INVALID, callback);
126 EXPECT_EQ(ret, RET_OK);
127 }
128
129 /**
130 * @tc.name: GetDeviceStatusDataTest001
131 * @tc.desc: Test func named getDeviceStatusData
132 * @tc.type: FUNC
133 */
134 HWTEST_F(StationaryServerTest, GetDeviceStatusDataTest001, TestSize.Level0)
135 {
136 CALL_TEST_DEBUG;
137 int32_t replyType;
138 int32_t replyValue;
139 int32_t ret = stationary_.GetDeviceStatusData(context_, TYPE_TYPE_STAND, replyType, replyValue);
140 EXPECT_EQ(ret, RET_OK);
141 }
142
143 /**
144 * @tc.name: GetDevicePosureDataSyncTest001
145 * @tc.desc: Test func named GetDevicePosureDataSyncTest001
146 * @tc.type: FUNC
147 */
148 HWTEST_F(StationaryServerTest, GetDevicePosureDataSyncTest001, TestSize.Level0)
149 {
150 CALL_TEST_DEBUG;
151 DevicePostureData data;
152 int32_t ret = stationary_.GetDevicePostureDataSync(context_, data);
153 EXPECT_TRUE(ret == RET_NO_SUPPORT || ret == RET_OK);
154 EXPECT_TRUE(data.rollRad >= 0 && data.rollRad <= DOUBLEPIMAX && data.pitchRad >= 0 &&
155 data.pitchRad <= DOUBLEPIMAX && data.yawRad >= 0 && data.yawRad <= DOUBLEPIMAX);
156 }
157
158 /**
159 * @tc.name: SubscribeStationaryParamTest001
160 * @tc.desc: Test func named SubscribeStationaryParam
161 * @tc.type: FUNC
162 */
163 HWTEST_F(StationaryServerTest, SubscribeStationaryParamTest001, TestSize.Level0)
164 {
165 CALL_TEST_DEBUG;
166 Type type = TYPE_ABSOLUTE_STILL;
167 ActivityEvent event = ENTER;
168 ReportLatencyNs latency = SHORT;
169 sptr<IRemoteDevStaCallback> callback = nullptr;
170 SubscribeStationaryParam Param = { type, event, latency, callback};
171 MessageParcel parcel;
172 bool ret = Param.Marshalling(parcel);
173 EXPECT_EQ(ret, false);
174 ret = Param.Unmarshalling(parcel);
175 EXPECT_EQ(ret, false);
176 }
177
178 /**
179 * @tc.name: GetStaionaryDataParamTest001
180 * @tc.desc: Test func named GetStaionaryDataParam
181 * @tc.type: FUNC
182 */
183 HWTEST_F(StationaryServerTest, GetStaionaryDataParamTest001, TestSize.Level0)
184 {
185 CALL_TEST_DEBUG;
186 GetStaionaryDataParam param;
187 MessageParcel parcel;
188 bool ret = param.Marshalling(parcel);
189 EXPECT_EQ(ret, true);
190 ret = param.Unmarshalling(parcel);
191 EXPECT_EQ(ret, true);
192 }
193
194 /**
195 * @tc.name: GetStaionaryDataReplyTest001
196 * @tc.desc: Test func named GetStaionaryDataReply
197 * @tc.type: FUNC
198 */
199 HWTEST_F(StationaryServerTest, GetStaionaryDataReplyTest001, TestSize.Level0)
200 {
201 CALL_TEST_DEBUG;
202 Data data;
203 GetStaionaryDataReply param {data};
204 MessageParcel parcel;
205 bool ret = param.Marshalling(parcel);
206 EXPECT_EQ(ret, true);
207 ret = param.Unmarshalling(parcel);
208 EXPECT_EQ(ret, true);
209 }
210 /**
211 * @tc.name: DumpCurrentDeviceStatusTest001
212 * @tc.desc: Test func named DumpCurrentDeviceStatus
213 * @tc.type: FUNC
214 */
215 HWTEST_F(StationaryServerTest, DumpCurrentDeviceStatus001, TestSize.Level0)
216 {
217 CALL_TEST_DEBUG;
218 int32_t fd = 1;
219 ASSERT_NO_FATAL_FAILURE(stationary_.DumpCurrentDeviceStatus(fd));
220 }
221
222 /**
223 * @tc.name: SubscribeTest001
224 * @tc.desc: Test func named Subscribe
225 * @tc.type: FUNC
226 */
227 HWTEST_F(StationaryServerTest, Subscribe001, TestSize.Level0)
228 {
229 CALL_TEST_DEBUG;
230 sptr<IRemoteDevStaCallback> callback = new (std::nothrow) StationaryServerTestCallback();
231 ASSERT_NE(callback, nullptr);
232 ActivityEvent stationaryEvent = static_cast<ActivityEvent>(ACTIVITYEVENT_ENTER);
233 ReportLatencyNs stationaryLatency = static_cast<ReportLatencyNs>(REPORTLATENCYNS_LATENCY_INVALID);
234 ASSERT_NO_FATAL_FAILURE(stationary_.Subscribe(
235 context_, TYPE_INVALID, stationaryEvent, stationaryLatency, callback));
236 }
237 } // namespace DeviceStatus
238 } // namespace Msdp
239 } // namespace OHOS