• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdint.h>
18 
19 #include <gtest/gtest.h>
20 #include <utils/Errors.h>
21 
22 #include <android/sensor.h>
23 #include <hardware/sensors-base.h>
24 #include <sensor/SensorManager.h>
25 #include <sensor/SensorEventQueue.h>
26 
27 namespace android {
28 
29 class SensorEventQueueTest : public ::testing::Test {
30 protected:
31     typedef std::vector<int32_t> Events;
32 
SensorEventQueueTest()33     SensorEventQueueTest() {};
34 
SetUp()35     virtual void SetUp() override {
36         SensorManager& manager = SensorManager::getInstanceForPackage(String16("SensorEventQueueTest"));
37         mQueue = manager.createEventQueue();
38     }
39 
configureAdditionalInfo(bool enable)40     void configureAdditionalInfo(bool enable) {
41         mQueue->requestAdditionalInfo = enable;
42     }
43 
filterEvents(const Events & types) const44     Events filterEvents(const Events &types) const {
45         // Convert the events into SensorEvent array
46         ASensorEvent* events = new ASensorEvent[types.size()];
47         for (size_t i = 0; i < types.size(); i++) {
48             events[i].type = types[i];
49         }
50 
51         // Filter the events
52         ssize_t filteredCount = mQueue->filterEvents(events, types.size());
53 
54         // Copy the result into an output vector
55         Events result;
56         for (size_t i = 0; i < filteredCount; i++) {
57             result.push_back(events[i].type);
58         }
59         delete[] events;
60 
61         return result;
62     }
63 
getExpectedEvents(const Events & events) const64     Events getExpectedEvents(const Events &events) const {
65         Events output;
66         for (size_t i = 0; i != events.size(); i++) {
67             // Copy events if the event queue is configured to receive them
68             if (events[i] != SENSOR_TYPE_ADDITIONAL_INFO || mQueue->requestAdditionalInfo) {
69                 output.push_back(events[i]);
70             }
71         }
72         return output;
73     }
74 
runFilterTest(const Events & events)75     void runFilterTest(const Events& events) {
76         Events filtered = filterEvents(events);
77         Events expected = getExpectedEvents(events);
78         EXPECT_EQ(expected.size(), filtered.size());
79         EXPECT_EQ(expected, filtered);
80     }
81 
82 private:
83     sp<SensorEventQueue> mQueue;
84 };
85 
TEST_F(SensorEventQueueTest,FilterZeroEvents)86 TEST_F(SensorEventQueueTest, FilterZeroEvents) {
87     configureAdditionalInfo(false /* enable */);
88     runFilterTest({});
89 }
90 
TEST_F(SensorEventQueueTest,FilterEvents_ReceiveAdditionalInfo)91 TEST_F(SensorEventQueueTest, FilterEvents_ReceiveAdditionalInfo) {
92     configureAdditionalInfo(true /* enable */);
93     runFilterTest({SENSOR_TYPE_ADDITIONAL_INFO,
94                    SENSOR_TYPE_ACCELEROMETER,
95                    SENSOR_TYPE_GYROSCOPE,
96                    SENSOR_TYPE_ADDITIONAL_INFO,
97                    SENSOR_TYPE_ADDITIONAL_INFO,
98                    SENSOR_TYPE_MAGNETIC_FIELD});
99 }
100 
TEST_F(SensorEventQueueTest,FilterEvents_RemoveAll)101 TEST_F(SensorEventQueueTest, FilterEvents_RemoveAll) {
102     configureAdditionalInfo(false /* enable */);
103     runFilterTest({SENSOR_TYPE_ADDITIONAL_INFO,
104                    SENSOR_TYPE_ADDITIONAL_INFO,
105                    SENSOR_TYPE_ADDITIONAL_INFO});
106 }
107 
TEST_F(SensorEventQueueTest,FilterEvents_RemoveFirst)108 TEST_F(SensorEventQueueTest, FilterEvents_RemoveFirst) {
109     configureAdditionalInfo(false /* enable */);
110     runFilterTest({SENSOR_TYPE_ADDITIONAL_INFO,
111                    SENSOR_TYPE_ACCELEROMETER,
112                    SENSOR_TYPE_GYROSCOPE,
113                    SENSOR_TYPE_MAGNETIC_FIELD});
114 }
115 
TEST_F(SensorEventQueueTest,FilterEvents_RemoveAllButOne)116 TEST_F(SensorEventQueueTest, FilterEvents_RemoveAllButOne) {
117     configureAdditionalInfo(false /* enable */);
118     runFilterTest({SENSOR_TYPE_ADDITIONAL_INFO,
119                    SENSOR_TYPE_ADDITIONAL_INFO,
120                    SENSOR_TYPE_ACCELEROMETER,
121                    SENSOR_TYPE_ADDITIONAL_INFO});
122 }
123 
TEST_F(SensorEventQueueTest,FilterEvents_RemoveLast)124 TEST_F(SensorEventQueueTest, FilterEvents_RemoveLast) {
125     configureAdditionalInfo(false /* enable */);
126     runFilterTest({SENSOR_TYPE_ACCELEROMETER,
127                    SENSOR_TYPE_GYROSCOPE,
128                    SENSOR_TYPE_MAGNETIC_FIELD,
129                    SENSOR_TYPE_ADDITIONAL_INFO});
130 }
131 
TEST_F(SensorEventQueueTest,FilterEvents_RemoveConsecutive)132 TEST_F(SensorEventQueueTest, FilterEvents_RemoveConsecutive) {
133     configureAdditionalInfo(false /* enable */);
134     runFilterTest({SENSOR_TYPE_MAGNETIC_FIELD,
135                    SENSOR_TYPE_ADDITIONAL_INFO,
136                    SENSOR_TYPE_ADDITIONAL_INFO,
137                    SENSOR_TYPE_ADDITIONAL_INFO,
138                    SENSOR_TYPE_ADDITIONAL_INFO,
139                    SENSOR_TYPE_ADDITIONAL_INFO,
140                    SENSOR_TYPE_ACCELEROMETER});
141 }
142 
TEST_F(SensorEventQueueTest,FilterEvents_RemoveInterleaved)143 TEST_F(SensorEventQueueTest, FilterEvents_RemoveInterleaved) {
144     configureAdditionalInfo(false /* enable */);
145     runFilterTest({SENSOR_TYPE_ACCELEROMETER,
146                    SENSOR_TYPE_GYROSCOPE,
147                    SENSOR_TYPE_ADDITIONAL_INFO,
148                    SENSOR_TYPE_ACCELEROMETER,
149                    SENSOR_TYPE_GYROSCOPE,
150                    SENSOR_TYPE_ADDITIONAL_INFO,
151                    SENSOR_TYPE_MAGNETIC_FIELD});
152 }
153 
TEST_F(SensorEventQueueTest,FilterEvents_ReconfigureAdditionalInfo)154 TEST_F(SensorEventQueueTest, FilterEvents_ReconfigureAdditionalInfo) {
155     configureAdditionalInfo(false /* enable */);
156     const Events events = {SENSOR_TYPE_ACCELEROMETER,
157                            SENSOR_TYPE_GYROSCOPE,
158                            SENSOR_TYPE_ADDITIONAL_INFO,
159                            SENSOR_TYPE_MAGNETIC_FIELD,
160                            SENSOR_TYPE_ADDITIONAL_INFO};
161     runFilterTest(events);
162 
163     // Update setting to request Additional Info
164     configureAdditionalInfo(true /* enable */);
165     runFilterTest(events);
166 
167     // Update setting to stop requesting Additional Info
168     configureAdditionalInfo(true /* enable */);
169     runFilterTest(events);
170 }
171 
172 } // namespace android
173