1 /*
2 * Copyright (c) 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 <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 #include "stationary_server.h"
23
24 #undef LOG_TAG
25 #define LOG_TAG "StationaryServerTest"
26
27 namespace OHOS {
28 namespace Msdp {
29 namespace DeviceStatus {
30 using namespace testing::ext;
31
32 CallingContext context_;
33 MessageParcel data_;
34 MessageParcel reply_;
35 StationaryServer stationary_;
36 uint32_t SUBSCRIBE_STATIONARY_ONE = 1U;
37 uint32_t SUBSCRIBE_STATIONARY_TWO = 2U;
38 uint32_t SUBSCRIBE_STATIONARY_THREE = 3U;
39
40 class StationaryServerTest : public testing::Test {
41 public:
SetUpTestCase()42 static void SetUpTestCase() {};
TearDownTestCase()43 static void TearDownTestCase() {};
SetUp()44 void SetUp() {};
TearDown()45 void TearDown() {};
46 public:
47 class StationaryServerTestCallback : public DeviceStatusCallbackStub {
48 public:
49 void OnDeviceStatusChanged(const Data& devicestatusData);
50 };
51 };
52
OnDeviceStatusChanged(const Data & devicestatusData)53 void StationaryServerTest::StationaryServerTestCallback::OnDeviceStatusChanged(const
54 Data& devicestatusData)
55 {
56 GTEST_LOG_(INFO) << "StationaryServerTestCallback type: " << devicestatusData.type;
57 GTEST_LOG_(INFO) << "StationaryServerTestCallback value: " << devicestatusData.value;
58 EXPECT_TRUE(devicestatusData.type == Type::TYPE_VERTICAL_POSITION &&
59 devicestatusData.value >= OnChangedValue::VALUE_INVALID &&
60 devicestatusData.value <= OnChangedValue::VALUE_EXIT) << "StationaryServerTestCallback failed";
61 }
62
63 /**
64 * @tc.name: EnableTest001
65 * @tc.desc: Test func named enable
66 * @tc.type: FUNC
67 */
68 HWTEST_F(StationaryServerTest, EnableTest001, TestSize.Level0)
69 {
70 CALL_TEST_DEBUG;
71 int32_t ret = stationary_.Enable(context_, data_, reply_);
72 EXPECT_EQ(ret, RET_ERR);
73 }
74
75 /**
76 * @tc.name: DisableTest001
77 * @tc.desc: Test func named disable
78 * @tc.type: FUNC
79 */
80 HWTEST_F(StationaryServerTest, DisableTest001, TestSize.Level0)
81 {
82 CALL_TEST_DEBUG;
83 int32_t ret = stationary_.Disable(context_, data_, reply_);
84 EXPECT_EQ(ret, RET_ERR);
85 }
86
87 /**
88 * @tc.name: StartTest001
89 * @tc.desc: Test func named start
90 * @tc.type: FUNC
91 */
92 HWTEST_F(StationaryServerTest, StartTest001, TestSize.Level0)
93 {
94 CALL_TEST_DEBUG;
95 int32_t ret = stationary_.Start(context_, data_, reply_);
96 EXPECT_EQ(ret, RET_ERR);
97 }
98
99 /**
100 * @tc.name: StopTest001
101 * @tc.desc: Test func named stop
102 * @tc.type: FUNC
103 */
104 HWTEST_F(StationaryServerTest, StopTest001, TestSize.Level0)
105 {
106 CALL_TEST_DEBUG;
107 int32_t ret = stationary_.Stop(context_, data_, reply_);
108 EXPECT_EQ(ret, RET_ERR);
109 }
110
111 /**
112 * @tc.name: SetParamTest001
113 * @tc.desc: Test func named SetParam
114 * @tc.type: FUNC
115 */
116 HWTEST_F(StationaryServerTest, SetParamTest001, TestSize.Level0)
117 {
118 CALL_TEST_DEBUG;
119 int32_t ret = stationary_.SetParam(context_, SUBSCRIBE_STATIONARY_ONE, data_, reply_);
120 EXPECT_EQ(ret, RET_ERR);
121 }
122
123 /**
124 * @tc.name: GetParamTest001
125 * @tc.desc: Test func named GetParam
126 * @tc.type: FUNC
127 */
128 HWTEST_F(StationaryServerTest, GetParamTest001, TestSize.Level0)
129 {
130 CALL_TEST_DEBUG;
131 int32_t ret = stationary_.GetParam(context_, SUBSCRIBE_STATIONARY_ONE, data_, reply_);
132 EXPECT_EQ(ret, RET_ERR);
133 ret = stationary_.GetParam(context_, SUBSCRIBE_STATIONARY_THREE, data_, reply_);
134 EXPECT_EQ(ret, RET_ERR);
135 }
136
137 /**
138 * @tc.name: ControlTest001
139 * @tc.desc: Test func named Control
140 * @tc.type: FUNC
141 */
142 HWTEST_F(StationaryServerTest, ControlTest001, TestSize.Level0)
143 {
144 CALL_TEST_DEBUG;
145 int32_t ret = stationary_.Control(context_, SUBSCRIBE_STATIONARY_ONE, data_, reply_);
146 EXPECT_EQ(ret, RET_ERR);
147 }
148
149 /**
150 * @tc.name: AddWatchTest001
151 * @tc.desc: Test func named addWatch success
152 * @tc.type: FUNC
153 */
154 HWTEST_F(StationaryServerTest, AddWatchTest001, TestSize.Level0)
155 {
156 CALL_TEST_DEBUG;
157 sptr<IRemoteDevStaCallback> cb = new (std::nothrow) StationaryServerTestCallback();
158 ASSERT_NE(cb, nullptr);
159 SubscribeStationaryParam param;
160 param.type_ = TYPE_HORIZONTAL_POSITION;
161 param.event_ = ENTER;
162 param.latency_ = SHORT;
163 param.callback_ = cb;
164 bool isSuccess = param.Marshalling(data_);
165 EXPECT_TRUE(isSuccess);
166 int32_t ret = stationary_.AddWatch(context_, SUBSCRIBE_STATIONARY_ONE, data_, reply_);
167 EXPECT_EQ(ret, RET_OK);
168 UnsubscribeStationaryParam param1;
169 param1 = param;
170 isSuccess = param1.Marshalling(data_);
171 EXPECT_TRUE(isSuccess);
172 ret = stationary_.RemoveWatch(context_, SUBSCRIBE_STATIONARY_TWO, data_, reply_);
173 EXPECT_EQ(ret, RET_OK);
174 }
175
176 /**
177 * @tc.name: AddWatchTest002
178 * @tc.desc: Test func named addWatch fail
179 * @tc.type: FUNC
180 */
181 HWTEST_F(StationaryServerTest, AddWatchTest002, TestSize.Level0)
182 {
183 CALL_TEST_DEBUG;
184 int32_t ret = stationary_.AddWatch(context_, SUBSCRIBE_STATIONARY_TWO, data_, reply_);
185 EXPECT_EQ(ret, RET_ERR);
186 }
187
188 /**
189 * @tc.name: RemoveWatchTest001
190 * @tc.desc: Test func named RemoveWatch
191 * @tc.type: FUNC
192 */
193 HWTEST_F(StationaryServerTest, RemoveWatchTest001, TestSize.Level0)
194 {
195 CALL_TEST_DEBUG;
196 int32_t ret = stationary_.RemoveWatch(context_, SUBSCRIBE_STATIONARY_ONE, data_, reply_);
197 EXPECT_EQ(ret, RET_ERR);
198 ret = stationary_.RemoveWatch(context_, SUBSCRIBE_STATIONARY_TWO, data_, reply_);
199 EXPECT_EQ(ret, RET_ERR);
200 }
201
202 } // namespace DeviceStatus
203 } // namespace Msdp
204 } // namespace OHOS