• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cmath>
17 #include <cstdio>
18 #include <unistd.h>
19 #include <gtest/gtest.h>
20 #include <securec.h>
21 #include <vector>
22 #include "hdf_base.h"
23 #include "osal_time.h"
24 #include "v1_0/imotion_interface.h"
25 #include "motion_callback_service.h"
26 
27 using namespace OHOS::HDI::Motion::V1_0;
28 using namespace testing::ext;
29 
30 namespace {
31     sptr<IMotionInterface> g_motionInterface = nullptr;
32     sptr<IMotionCallback> g_motionCallback = new MotionCallbackService();
33     sptr<IMotionCallback> g_motionCallbackUnregistered = new MotionCallbackService();
34 }
35 
36 class HdfMotionTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp();
41     void TearDown();
42 };
43 
SetUpTestCase()44 void HdfMotionTest::SetUpTestCase()
45 {
46     g_motionInterface = IMotionInterface::Get();
47 }
48 
TearDownTestCase()49 void HdfMotionTest::TearDownTestCase()
50 {
51 }
52 
SetUp()53 void HdfMotionTest::SetUp()
54 {
55 }
56 
TearDown()57 void HdfMotionTest::TearDown()
58 {
59 }
60 
61 HWTEST_F(HdfMotionTest, GetMotionClient_001, TestSize.Level1)
62 {
63     ASSERT_NE(nullptr, g_motionInterface);
64 }
65 
66 HWTEST_F(HdfMotionTest, RegisterMotionDataCb_001, TestSize.Level1)
67 {
68     if (g_motionInterface == nullptr) {
69         ASSERT_NE(nullptr, g_motionInterface);
70         return;
71     }
72     int32_t ret = g_motionInterface->Register(g_motionCallback);
73     EXPECT_EQ(HDF_SUCCESS, ret);
74     ret = g_motionInterface->Unregister(g_motionCallback);
75     EXPECT_EQ(0, ret);
76 }
77 
78 HWTEST_F(HdfMotionTest, RegisterMotionDataCb_002, TestSize.Level1)
79 {
80     if (g_motionInterface == nullptr) {
81         ASSERT_NE(nullptr, g_motionInterface);
82         return;
83     }
84     int32_t ret = g_motionInterface->Register(g_motionCallback);
85     EXPECT_EQ(HDF_SUCCESS, ret);
86     ret = g_motionInterface->Register(g_motionCallback);
87     EXPECT_EQ(HDF_FAILURE, ret);
88     ret = g_motionInterface->Unregister(g_motionCallback);
89     EXPECT_EQ(HDF_SUCCESS, ret);
90 }
91 
92 HWTEST_F(HdfMotionTest, RegisterMotionDataCb_003, TestSize.Level1)
93 {
94     if (g_motionInterface == nullptr) {
95         ASSERT_NE(nullptr, g_motionInterface);
96         return;
97     }
98     int32_t ret = g_motionInterface->Unregister(g_motionCallbackUnregistered);
99     EXPECT_EQ(HDF_FAILURE, ret);
100 }
101 
102 HWTEST_F(HdfMotionTest, EnableMotion_001, TestSize.Level1)
103 {
104     if (g_motionInterface == nullptr) {
105         ASSERT_NE(nullptr, g_motionInterface);
106         return;
107     }
108 
109     vector<int32_t> vec;
110     vec.push_back(HDF_MOTION_TYPE_PICKUP);
111     vec.push_back(HDF_MOTION_TYPE_FLIP);
112     vec.push_back(HDF_MOTION_TYPE_SHAKE);
113     vec.push_back(HDF_MOTION_TYPE_ROTATION);
114 
115     int32_t ret = g_motionInterface->Register(g_motionCallback);
116     EXPECT_EQ(HDF_SUCCESS, ret);
117 
118     for (size_t i = 0; i < vec.size(); i++) {
119         ret = g_motionInterface->EnableMotion(vec[i]);
120         if (ret == HDF_SUCCESS) {
121             printf("Motion %d enabled successfully\n", vec[i]);
122         } else {
123             printf("Motion %d enable failed\n", vec[i]);
124         }
125         EXPECT_EQ(HDF_SUCCESS, ret);
126         OsalSleep(15);
127 
128         ret = g_motionInterface->DisableMotion(vec[i]);
129         if (ret == HDF_SUCCESS) {
130             printf("Motion %d disabled successfully\n", vec[i]);
131         } else {
132             printf("Motion %d disable failed\n", vec[i]);
133         }
134         EXPECT_EQ(HDF_SUCCESS, ret);
135         OsalSleep(2);
136     }
137 
138     ret = g_motionInterface->Unregister(g_motionCallback);
139     EXPECT_EQ(HDF_SUCCESS, ret);
140 }
141 
142 HWTEST_F(HdfMotionTest, EnableMotion_002, TestSize.Level1)
143 {
144     if (g_motionInterface == nullptr) {
145         ASSERT_NE(nullptr, g_motionInterface);
146         return;
147     }
148     int32_t ret = g_motionInterface->EnableMotion(HDF_MOTION_TYPE_MAX);
149     EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
150 }
151 
152 HWTEST_F(HdfMotionTest, DisableMotion_001, TestSize.Level1)
153 {
154     if (g_motionInterface == nullptr) {
155         ASSERT_NE(nullptr, g_motionInterface);
156         return;
157     }
158     int32_t ret = g_motionInterface->Register(g_motionCallback);
159     EXPECT_EQ(HDF_SUCCESS, ret);
160 
161     ret = g_motionInterface->EnableMotion(HDF_MOTION_TYPE_PICKUP);
162     EXPECT_EQ(HDF_SUCCESS, ret);
163     ret = g_motionInterface->DisableMotion(HDF_MOTION_TYPE_PICKUP);
164     EXPECT_EQ(HDF_SUCCESS, ret);
165 
166     ret = g_motionInterface->Unregister(g_motionCallback);
167     EXPECT_EQ(HDF_SUCCESS, ret);
168 }
169 
170 HWTEST_F(HdfMotionTest, DisableMotion_002, TestSize.Level1)
171 {
172     if (g_motionInterface == nullptr) {
173         ASSERT_NE(nullptr, g_motionInterface);
174         return;
175     }
176     int32_t ret = g_motionInterface->DisableMotion(HDF_MOTION_TYPE_MAX);
177     EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
178 }