1 /*
2  * Copyright (C) 2016 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 "gtest/gtest.h"
18 
19 #include "chre/core/sensor_request.h"
20 
21 using chre::Nanoseconds;
22 using chre::SensorMode;
23 using chre::SensorRequest;
24 using chre::SensorType;
25 using chre::kMaxIntervalLatencyNs;
26 
TEST(SensorType,LosslessSensorHandleToSensorTypeAndBack)27 TEST(SensorType, LosslessSensorHandleToSensorTypeAndBack) {
28   // Verify that converting a sensor to a handle and from a handle back to a
29   // sensor is a lossless conversion. The specific value of the handle is
30   // unimportant, as long as it can be consistently converted back and forth.
31   SensorType sensorType = SensorType::Pressure;
32   uint32_t sensorHandle = getSensorHandleFromSensorType(sensorType);
33   sensorType = chre::getSensorTypeFromSensorHandle(sensorHandle);
34   EXPECT_EQ(sensorType, SensorType::Pressure);
35 
36   sensorType = SensorType::Proximity;
37   sensorHandle = getSensorHandleFromSensorType(sensorType);
38   sensorType = chre::getSensorTypeFromSensorHandle(sensorHandle);
39   EXPECT_EQ(sensorType, SensorType::Proximity);
40 }
41 
TEST(SensorType,SensorHandleToSensorTypeUnknownHandles)42 TEST(SensorType, SensorHandleToSensorTypeUnknownHandles) {
43   EXPECT_EQ(chre::getSensorTypeFromSensorHandle(0), SensorType::Unknown);
44   EXPECT_EQ(chre::getSensorTypeFromSensorHandle(10000), SensorType::Unknown);
45 }
46 
TEST(SensorRequest,DefaultMinimalPriority)47 TEST(SensorRequest, DefaultMinimalPriority) {
48   SensorRequest request;
49   EXPECT_EQ(request.getInterval(), Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT));
50   EXPECT_EQ(request.getLatency(), Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT));
51   EXPECT_EQ(request.getMode(), SensorMode::Off);
52 }
53 
TEST(SensorRequest,ActiveContinuousIsHigherPriorityThanActiveOneShot)54 TEST(SensorRequest, ActiveContinuousIsHigherPriorityThanActiveOneShot) {
55   SensorRequest activeContinuous(SensorMode::ActiveContinuous,
56                                  Nanoseconds(0), Nanoseconds(0));
57   SensorRequest activeOneShot(SensorMode::ActiveOneShot,
58                               Nanoseconds(0), Nanoseconds(0));
59   SensorRequest mergedRequest;
60   EXPECT_TRUE(mergedRequest.mergeWith(activeContinuous));
61   EXPECT_FALSE(mergedRequest.mergeWith(activeOneShot));
62   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(0));
63   EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0));
64   EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous);
65 }
66 
TEST(SensorRequest,ActiveOneShotIsHigherPriorityThanPassiveContinuous)67 TEST(SensorRequest, ActiveOneShotIsHigherPriorityThanPassiveContinuous) {
68   SensorRequest activeOneShot(SensorMode::ActiveOneShot,
69                               Nanoseconds(0), Nanoseconds(0));
70   SensorRequest passiveContinuous(SensorMode::PassiveContinuous,
71                                   Nanoseconds(0), Nanoseconds(0));
72   SensorRequest mergedRequest;
73   EXPECT_TRUE(mergedRequest.mergeWith(activeOneShot));
74   EXPECT_FALSE(mergedRequest.mergeWith(passiveContinuous));
75   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(0));
76   EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0));
77   EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveOneShot);
78 
79 }
80 
TEST(SensorRequest,PassiveContinuousIsHigherPriorityThanPassiveOneShot)81 TEST(SensorRequest, PassiveContinuousIsHigherPriorityThanPassiveOneShot) {
82   SensorRequest passiveContinuous(SensorMode::PassiveContinuous,
83                                   Nanoseconds(0), Nanoseconds(0));
84   SensorRequest passiveOneShot(SensorMode::PassiveOneShot,
85                                Nanoseconds(0), Nanoseconds(0));
86   SensorRequest mergedRequest;
87   EXPECT_TRUE(mergedRequest.mergeWith(passiveContinuous));
88   EXPECT_FALSE(mergedRequest.mergeWith(passiveOneShot));
89   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(0));
90   EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0));
91   EXPECT_EQ(mergedRequest.getMode(), SensorMode::PassiveContinuous);
92 }
93 
TEST(SensorRequest,PassiveOneShotIsHigherPriorityThanOff)94 TEST(SensorRequest, PassiveOneShotIsHigherPriorityThanOff) {
95   SensorRequest passiveOneShot(SensorMode::PassiveOneShot,
96                                Nanoseconds(0), Nanoseconds(0));
97   SensorRequest off(SensorMode::Off, Nanoseconds(0), Nanoseconds(0));
98   SensorRequest mergedRequest;
99   EXPECT_TRUE(mergedRequest.mergeWith(passiveOneShot));
100   EXPECT_FALSE(mergedRequest.mergeWith(off));
101   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(0));
102   EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0));
103   EXPECT_EQ(mergedRequest.getMode(), SensorMode::PassiveOneShot);
104 }
105 
TEST(SensorRequest,LowerLatencyIsHigherPriorityThanHigherLatency)106 TEST(SensorRequest, LowerLatencyIsHigherPriorityThanHigherLatency) {
107   SensorRequest lowLatencyRequest(SensorMode::ActiveContinuous,
108                                   Nanoseconds(10), Nanoseconds(10));
109   SensorRequest highLatencyRequest(SensorMode::ActiveOneShot,
110                                    Nanoseconds(10), Nanoseconds(100));
111   SensorRequest mergedRequest;
112   EXPECT_TRUE(mergedRequest.mergeWith(lowLatencyRequest));
113   EXPECT_FALSE(mergedRequest.mergeWith(highLatencyRequest));
114   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10));
115   EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(10));
116   EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous);
117 }
118 
TEST(SensorRequest,HigherFrequencyIsHigherPriorityThanLowerFrequency)119 TEST(SensorRequest, HigherFrequencyIsHigherPriorityThanLowerFrequency) {
120   SensorRequest lowFreqRequest(SensorMode::ActiveOneShot,
121                                Nanoseconds(100), Nanoseconds(10));
122   SensorRequest highFreqRequest(SensorMode::ActiveContinuous,
123                                 Nanoseconds(10), Nanoseconds(10));
124   SensorRequest mergedRequest;
125   EXPECT_TRUE(mergedRequest.mergeWith(lowFreqRequest));
126   EXPECT_TRUE(mergedRequest.mergeWith(highFreqRequest));
127   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10));
128   EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(10));
129   EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous);
130 }
131 
TEST(SensorRequest,OnlyDefaultFrequency)132 TEST(SensorRequest, OnlyDefaultFrequency) {
133   SensorRequest defaultFreqRequest(SensorMode::ActiveContinuous,
134                                    Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT),
135                                    Nanoseconds(0));
136   SensorRequest mergedRequest;
137   EXPECT_TRUE(mergedRequest.mergeWith(defaultFreqRequest));
138   EXPECT_EQ(mergedRequest.getInterval(),
139             Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT));
140   EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0));
141   EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous);
142 }
143 
TEST(SensorRequest,NonDefaultAndDefaultFrequency)144 TEST(SensorRequest, NonDefaultAndDefaultFrequency) {
145   SensorRequest defaultFreqRequest(SensorMode::ActiveContinuous,
146                                    Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT),
147                                    Nanoseconds(0));
148   SensorRequest nonDefaultFreqRequest(SensorMode::ActiveContinuous,
149                                       Nanoseconds(20000000), Nanoseconds(0));
150   SensorRequest mergedRequest;
151   EXPECT_TRUE(mergedRequest.mergeWith(defaultFreqRequest));
152   EXPECT_TRUE(mergedRequest.mergeWith(nonDefaultFreqRequest));
153   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(20000000));
154   EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0));
155   EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous);
156 }
157 
TEST(SensorRequest,OnlyAsapLatency)158 TEST(SensorRequest, OnlyAsapLatency) {
159   SensorRequest asapLatencyRequest(SensorMode::ActiveContinuous,
160                                    Nanoseconds(10),
161                                    Nanoseconds(CHRE_SENSOR_LATENCY_ASAP));
162   SensorRequest mergedRequest;
163   EXPECT_TRUE(mergedRequest.mergeWith(asapLatencyRequest));
164   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10));
165   EXPECT_EQ(mergedRequest.getLatency(),
166             Nanoseconds(CHRE_SENSOR_LATENCY_ASAP));
167   EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous);
168 }
169 
TEST(SensorRequest,NonAsapAndAsapLatency)170 TEST(SensorRequest, NonAsapAndAsapLatency) {
171   SensorRequest asapLatencyRequest(SensorMode::ActiveContinuous,
172                                    Nanoseconds(10),
173                                    Nanoseconds(CHRE_SENSOR_LATENCY_ASAP));
174   SensorRequest nonAsapLatencyRequest(SensorMode::ActiveContinuous,
175                                       Nanoseconds(10),
176                                       Nanoseconds(2000));
177   SensorRequest mergedRequest;
178   EXPECT_TRUE(mergedRequest.mergeWith(asapLatencyRequest));
179   EXPECT_FALSE(mergedRequest.mergeWith(nonAsapLatencyRequest));
180   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10));
181   EXPECT_EQ(mergedRequest.getLatency(),
182             Nanoseconds(CHRE_SENSOR_LATENCY_ASAP));
183   EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous);
184 }
185 
TEST(SensorRequest,OnlyDefaultLatency)186 TEST(SensorRequest, OnlyDefaultLatency) {
187   SensorRequest defaultLatencyRequest(SensorMode::ActiveContinuous,
188                                       Nanoseconds(10),
189                                       Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT));
190   SensorRequest mergedRequest;
191   EXPECT_TRUE(mergedRequest.mergeWith(defaultLatencyRequest));
192   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10));
193   EXPECT_EQ(mergedRequest.getLatency(),
194             Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT));
195   EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous);
196 }
197 
TEST(SensorRequest,NonDefaultAndDefaultLatency)198 TEST(SensorRequest, NonDefaultAndDefaultLatency) {
199   SensorRequest defaultLatencyRequest(SensorMode::ActiveContinuous,
200                                       Nanoseconds(10),
201                                       Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT));
202   SensorRequest nonDefaultLatencyRequest(SensorMode::ActiveContinuous,
203                                          Nanoseconds(10),
204                                          Nanoseconds(2000));
205   SensorRequest mergedRequest;
206   EXPECT_TRUE(mergedRequest.mergeWith(defaultLatencyRequest));
207   EXPECT_TRUE(mergedRequest.mergeWith(nonDefaultLatencyRequest));
208   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10));
209   EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(2000));
210   EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous);
211 }
212 
TEST(SensorRequest,MergeWithOff)213 TEST(SensorRequest, MergeWithOff) {
214   SensorRequest request(SensorMode::ActiveContinuous,
215                         Nanoseconds(10),
216                         Nanoseconds(100));
217   SensorRequest otherRequest(SensorMode::Off,
218                              Nanoseconds(1),
219                              Nanoseconds(1));
220   EXPECT_FALSE(request.mergeWith(otherRequest));
221   EXPECT_EQ(request.getMode(), SensorMode::ActiveContinuous);
222   EXPECT_EQ(request.getInterval(), Nanoseconds(10));
223   EXPECT_EQ(request.getLatency(), Nanoseconds(100));
224 }
225 
TEST(SensorRequest,MaxNonDefaultIntervalAndLatency)226 TEST(SensorRequest, MaxNonDefaultIntervalAndLatency) {
227   SensorRequest request(SensorMode::ActiveContinuous,
228                         Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT - 1),
229                         Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT - 1));
230   EXPECT_EQ(request.getMode(), SensorMode::ActiveContinuous);
231   EXPECT_EQ(request.getInterval(), Nanoseconds(kMaxIntervalLatencyNs));
232   EXPECT_EQ(request.getLatency(), Nanoseconds(kMaxIntervalLatencyNs));
233 }
234 
TEST(SensorRequest,HighRateLowLatencyAndLowRateHighLatency)235 TEST(SensorRequest, HighRateLowLatencyAndLowRateHighLatency) {
236   SensorRequest Request0(SensorMode::ActiveContinuous,
237                          Nanoseconds(100), Nanoseconds(0));
238   SensorRequest Request1(SensorMode::ActiveContinuous,
239                          Nanoseconds(10), Nanoseconds(2000));
240   SensorRequest mergedRequest;
241   EXPECT_TRUE(mergedRequest.mergeWith(Request0));
242   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(100));
243   EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0));
244   EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous);
245 
246   EXPECT_TRUE(mergedRequest.mergeWith(Request1));
247   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10));
248   EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(90));
249   EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous);
250 }
251 
TEST(SensorRequest,LowRateHighLatencyAndHighRateLowLatency)252 TEST(SensorRequest, LowRateHighLatencyAndHighRateLowLatency) {
253   SensorRequest Request0(SensorMode::ActiveContinuous,
254                          Nanoseconds(100), Nanoseconds(0));
255   SensorRequest Request1(SensorMode::ActiveContinuous,
256                          Nanoseconds(10), Nanoseconds(2000));
257   SensorRequest mergedRequest;
258   EXPECT_TRUE(mergedRequest.mergeWith(Request1));
259   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10));
260   EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(2000));
261   EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous);
262 
263   EXPECT_TRUE(mergedRequest.mergeWith(Request0));
264   EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10));
265   EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(90));
266   EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous);
267 }
268