1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include <cmath>
17094332d3Sopenharmony_ci#include <cstdio>
18094332d3Sopenharmony_ci#include <unistd.h>
19094332d3Sopenharmony_ci#include <gtest/gtest.h>
20094332d3Sopenharmony_ci#include <securec.h>
21094332d3Sopenharmony_ci#include <vector>
22094332d3Sopenharmony_ci#include "hdf_base.h"
23094332d3Sopenharmony_ci#include "osal_time.h"
24094332d3Sopenharmony_ci#include "v1_1/imotion_interface.h"
25094332d3Sopenharmony_ci#include "motion_callback_impl.h"
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_ci#define DATA_NUM 12
28094332d3Sopenharmony_ci#define DATA_VALUE 6
29094332d3Sopenharmony_ci
30094332d3Sopenharmony_ciusing namespace OHOS::HDI::Motion::V1_1;
31094332d3Sopenharmony_ciusing namespace testing::ext;
32094332d3Sopenharmony_ci
33094332d3Sopenharmony_cinamespace {
34094332d3Sopenharmony_ci    sptr<OHOS::HDI::Motion::V1_1::IMotionInterface> g_motionInterface = nullptr;
35094332d3Sopenharmony_ci    sptr<IMotionCallback> g_motionCallback = new MotionCallbackImpl();
36094332d3Sopenharmony_ci    sptr<IMotionCallback> g_motionCallbackUnregistered = new MotionCallbackImpl();
37094332d3Sopenharmony_ci    std::vector<uint8_t> g_motionConfigData(DATA_NUM, DATA_VALUE);
38094332d3Sopenharmony_ci}
39094332d3Sopenharmony_ci
40094332d3Sopenharmony_ciclass HdfMotionTest : public testing::Test {
41094332d3Sopenharmony_cipublic:
42094332d3Sopenharmony_ci    static void SetUpTestCase();
43094332d3Sopenharmony_ci    static void TearDownTestCase();
44094332d3Sopenharmony_ci    void SetUp();
45094332d3Sopenharmony_ci    void TearDown();
46094332d3Sopenharmony_ci};
47094332d3Sopenharmony_ci
48094332d3Sopenharmony_civoid HdfMotionTest::SetUpTestCase()
49094332d3Sopenharmony_ci{
50094332d3Sopenharmony_ci    g_motionInterface = OHOS::HDI::Motion::V1_1::IMotionInterface::Get();
51094332d3Sopenharmony_ci}
52094332d3Sopenharmony_ci
53094332d3Sopenharmony_civoid HdfMotionTest::TearDownTestCase()
54094332d3Sopenharmony_ci{
55094332d3Sopenharmony_ci}
56094332d3Sopenharmony_ci
57094332d3Sopenharmony_civoid HdfMotionTest::SetUp()
58094332d3Sopenharmony_ci{
59094332d3Sopenharmony_ci}
60094332d3Sopenharmony_ci
61094332d3Sopenharmony_civoid HdfMotionTest::TearDown()
62094332d3Sopenharmony_ci{
63094332d3Sopenharmony_ci}
64094332d3Sopenharmony_ci
65094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, GetMotionClient_001, TestSize.Level1)
66094332d3Sopenharmony_ci{
67094332d3Sopenharmony_ci    ASSERT_NE(nullptr, g_motionInterface);
68094332d3Sopenharmony_ci}
69094332d3Sopenharmony_ci
70094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, RegisterMotionDataCb_001, TestSize.Level1)
71094332d3Sopenharmony_ci{
72094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
73094332d3Sopenharmony_ci        ASSERT_NE(nullptr, g_motionInterface);
74094332d3Sopenharmony_ci        return;
75094332d3Sopenharmony_ci    }
76094332d3Sopenharmony_ci    int32_t ret = g_motionInterface->Register(g_motionCallback);
77094332d3Sopenharmony_ci    EXPECT_EQ(HDF_SUCCESS, ret);
78094332d3Sopenharmony_ci    ret = g_motionInterface->Unregister(g_motionCallback);
79094332d3Sopenharmony_ci    EXPECT_EQ(0, ret);
80094332d3Sopenharmony_ci}
81094332d3Sopenharmony_ci
82094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, RegisterMotionDataCb_002, TestSize.Level1)
83094332d3Sopenharmony_ci{
84094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
85094332d3Sopenharmony_ci        ASSERT_NE(nullptr, g_motionInterface);
86094332d3Sopenharmony_ci        return;
87094332d3Sopenharmony_ci    }
88094332d3Sopenharmony_ci    int32_t ret = g_motionInterface->Register(g_motionCallback);
89094332d3Sopenharmony_ci    EXPECT_EQ(HDF_SUCCESS, ret);
90094332d3Sopenharmony_ci    ret = g_motionInterface->Register(g_motionCallback);
91094332d3Sopenharmony_ci    EXPECT_EQ(HDF_FAILURE, ret);
92094332d3Sopenharmony_ci    ret = g_motionInterface->Unregister(g_motionCallback);
93094332d3Sopenharmony_ci    EXPECT_EQ(HDF_SUCCESS, ret);
94094332d3Sopenharmony_ci}
95094332d3Sopenharmony_ci
96094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, RegisterMotionDataCb_003, TestSize.Level1)
97094332d3Sopenharmony_ci{
98094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
99094332d3Sopenharmony_ci        ASSERT_NE(nullptr, g_motionInterface);
100094332d3Sopenharmony_ci        return;
101094332d3Sopenharmony_ci    }
102094332d3Sopenharmony_ci    sptr<IMotionCallback> motionCallback = nullptr;
103094332d3Sopenharmony_ci    int32_t ret = g_motionInterface->Register(motionCallback);
104094332d3Sopenharmony_ci    EXPECT_NE(HDF_SUCCESS, ret);
105094332d3Sopenharmony_ci}
106094332d3Sopenharmony_ci
107094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, RegisterMotionDataCb_004, TestSize.Level1)
108094332d3Sopenharmony_ci{
109094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
110094332d3Sopenharmony_ci        ASSERT_NE(nullptr, g_motionInterface);
111094332d3Sopenharmony_ci        return;
112094332d3Sopenharmony_ci    }
113094332d3Sopenharmony_ci    int32_t ret = g_motionInterface->Unregister(g_motionCallbackUnregistered);
114094332d3Sopenharmony_ci    EXPECT_EQ(HDF_FAILURE, ret);
115094332d3Sopenharmony_ci}
116094332d3Sopenharmony_ci
117094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, RegisterMotionDataCb_005, TestSize.Level1)
118094332d3Sopenharmony_ci{
119094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
120094332d3Sopenharmony_ci        ASSERT_NE(nullptr, g_motionInterface);
121094332d3Sopenharmony_ci        return;
122094332d3Sopenharmony_ci    }
123094332d3Sopenharmony_ci    sptr<IMotionCallback> motionCallback = nullptr;
124094332d3Sopenharmony_ci    int32_t ret = g_motionInterface->Unregister(motionCallback);
125094332d3Sopenharmony_ci    EXPECT_NE(HDF_SUCCESS, ret);
126094332d3Sopenharmony_ci}
127094332d3Sopenharmony_ci
128094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, EnableMotion_001, TestSize.Level1)
129094332d3Sopenharmony_ci{
130094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
131094332d3Sopenharmony_ci        ASSERT_NE(nullptr, g_motionInterface);
132094332d3Sopenharmony_ci        return;
133094332d3Sopenharmony_ci    }
134094332d3Sopenharmony_ci
135094332d3Sopenharmony_ci    vector<int32_t> vec;
136094332d3Sopenharmony_ci    vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
137094332d3Sopenharmony_ci    vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_FLIP);
138094332d3Sopenharmony_ci    vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_SHAKE);
139094332d3Sopenharmony_ci    vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_ROTATION);
140094332d3Sopenharmony_ci
141094332d3Sopenharmony_ci    int32_t ret = g_motionInterface->Register(g_motionCallback);
142094332d3Sopenharmony_ci    EXPECT_EQ(HDF_SUCCESS, ret);
143094332d3Sopenharmony_ci
144094332d3Sopenharmony_ci    for (size_t i = 0; i < vec.size(); i++) {
145094332d3Sopenharmony_ci        ret = g_motionInterface->EnableMotion(vec[i]);
146094332d3Sopenharmony_ci        if (ret == HDF_SUCCESS) {
147094332d3Sopenharmony_ci            printf("Motion %d enabled successfully\n", vec[i]);
148094332d3Sopenharmony_ci        } else {
149094332d3Sopenharmony_ci            printf("Motion %d enable failed\n", vec[i]);
150094332d3Sopenharmony_ci        }
151094332d3Sopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
152094332d3Sopenharmony_ci        OsalSleep(15);
153094332d3Sopenharmony_ci
154094332d3Sopenharmony_ci        ret = g_motionInterface->DisableMotion(vec[i]);
155094332d3Sopenharmony_ci        if (ret == HDF_SUCCESS) {
156094332d3Sopenharmony_ci            printf("Motion %d disabled successfully\n", vec[i]);
157094332d3Sopenharmony_ci        } else {
158094332d3Sopenharmony_ci            printf("Motion %d disable failed\n", vec[i]);
159094332d3Sopenharmony_ci        }
160094332d3Sopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
161094332d3Sopenharmony_ci        OsalSleep(2);
162094332d3Sopenharmony_ci    }
163094332d3Sopenharmony_ci
164094332d3Sopenharmony_ci    ret = g_motionInterface->Unregister(g_motionCallback);
165094332d3Sopenharmony_ci    EXPECT_EQ(HDF_SUCCESS, ret);
166094332d3Sopenharmony_ci}
167094332d3Sopenharmony_ci
168094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, EnableMotion_002, TestSize.Level1)
169094332d3Sopenharmony_ci{
170094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
171094332d3Sopenharmony_ci        ASSERT_NE(nullptr, g_motionInterface);
172094332d3Sopenharmony_ci        return;
173094332d3Sopenharmony_ci    }
174094332d3Sopenharmony_ci    int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
175094332d3Sopenharmony_ci    EXPECT_NE(HDF_SUCCESS, ret);
176094332d3Sopenharmony_ci    OsalSleep(2);
177094332d3Sopenharmony_ci
178094332d3Sopenharmony_ci    ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
179094332d3Sopenharmony_ci    EXPECT_NE(HDF_SUCCESS, ret);
180094332d3Sopenharmony_ci}
181094332d3Sopenharmony_ci
182094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, EnableMotion_003, TestSize.Level1)
183094332d3Sopenharmony_ci{
184094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
185094332d3Sopenharmony_ci        ASSERT_NE(nullptr, g_motionInterface);
186094332d3Sopenharmony_ci        return;
187094332d3Sopenharmony_ci    }
188094332d3Sopenharmony_ci    int32_t ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_UP);
189094332d3Sopenharmony_ci    EXPECT_EQ(HDF_SUCCESS, ret);
190094332d3Sopenharmony_ci    OsalSleep(2);
191094332d3Sopenharmony_ci
192094332d3Sopenharmony_ci    ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_UP);
193094332d3Sopenharmony_ci    EXPECT_EQ(HDF_SUCCESS, ret);
194094332d3Sopenharmony_ci}
195094332d3Sopenharmony_ci
196094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, EnableMotion_004, TestSize.Level1)
197094332d3Sopenharmony_ci{
198094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
199094332d3Sopenharmony_ci        ASSERT_NE(nullptr, g_motionInterface);
200094332d3Sopenharmony_ci        return;
201094332d3Sopenharmony_ci    }
202094332d3Sopenharmony_ci    int32_t motionType = -1;
203094332d3Sopenharmony_ci    int32_t ret = g_motionInterface->EnableMotion(motionType);
204094332d3Sopenharmony_ci    EXPECT_NE(HDF_SUCCESS, ret);
205094332d3Sopenharmony_ci    OsalSleep(2);
206094332d3Sopenharmony_ci
207094332d3Sopenharmony_ci    ret = g_motionInterface->DisableMotion(motionType);
208094332d3Sopenharmony_ci    EXPECT_NE(HDF_SUCCESS, ret);
209094332d3Sopenharmony_ci}
210094332d3Sopenharmony_ci
211094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, DisableMotion_001, TestSize.Level1)
212094332d3Sopenharmony_ci{
213094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
214094332d3Sopenharmony_ci        ASSERT_NE(nullptr, g_motionInterface);
215094332d3Sopenharmony_ci        return;
216094332d3Sopenharmony_ci    }
217094332d3Sopenharmony_ci    int32_t ret = g_motionInterface->Register(g_motionCallback);
218094332d3Sopenharmony_ci    EXPECT_EQ(HDF_SUCCESS, ret);
219094332d3Sopenharmony_ci
220094332d3Sopenharmony_ci    ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
221094332d3Sopenharmony_ci    EXPECT_EQ(HDF_SUCCESS, ret);
222094332d3Sopenharmony_ci    ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
223094332d3Sopenharmony_ci    EXPECT_EQ(HDF_SUCCESS, ret);
224094332d3Sopenharmony_ci
225094332d3Sopenharmony_ci    ret = g_motionInterface->Unregister(g_motionCallback);
226094332d3Sopenharmony_ci    EXPECT_EQ(HDF_SUCCESS, ret);
227094332d3Sopenharmony_ci}
228094332d3Sopenharmony_ci
229094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, DisableMotion_002, TestSize.Level1)
230094332d3Sopenharmony_ci{
231094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
232094332d3Sopenharmony_ci        ASSERT_NE(nullptr, g_motionInterface);
233094332d3Sopenharmony_ci        return;
234094332d3Sopenharmony_ci    }
235094332d3Sopenharmony_ci    int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
236094332d3Sopenharmony_ci    EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
237094332d3Sopenharmony_ci}
238094332d3Sopenharmony_ci
239094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, DisableMotion_003, TestSize.Level1)
240094332d3Sopenharmony_ci{
241094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
242094332d3Sopenharmony_ci        ASSERT_NE(nullptr, g_motionInterface);
243094332d3Sopenharmony_ci        return;
244094332d3Sopenharmony_ci    }
245094332d3Sopenharmony_ci    int32_t ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
246094332d3Sopenharmony_ci    EXPECT_EQ(HDF_SUCCESS, ret);
247094332d3Sopenharmony_ci}
248094332d3Sopenharmony_ci
249094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, DisableMotion_004, TestSize.Level1)
250094332d3Sopenharmony_ci{
251094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
252094332d3Sopenharmony_ci        ASSERT_NE(nullptr, g_motionInterface);
253094332d3Sopenharmony_ci        return;
254094332d3Sopenharmony_ci    }
255094332d3Sopenharmony_ci    int32_t motionType = -1;
256094332d3Sopenharmony_ci    int32_t ret = g_motionInterface->DisableMotion(motionType);
257094332d3Sopenharmony_ci    EXPECT_NE(HDF_SUCCESS, ret);
258094332d3Sopenharmony_ci}
259094332d3Sopenharmony_ci
260094332d3Sopenharmony_ciHWTEST_F(HdfMotionTest, SetMotionConfig_001, TestSize.Level1)
261094332d3Sopenharmony_ci{
262094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
263094332d3Sopenharmony_ci        ASSERT_NE(nullptr, g_motionInterface);
264094332d3Sopenharmony_ci        return;
265094332d3Sopenharmony_ci    }
266094332d3Sopenharmony_ci    int32_t motionType = -1;
267094332d3Sopenharmony_ci    int32_t ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
268094332d3Sopenharmony_ci    EXPECT_NE(HDF_SUCCESS, ret);
269094332d3Sopenharmony_ci}