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 26 TEST(SensorType, LosslessSensorHandleToSensorTypeAndBack) { 27 // Verify that converting a sensor to a handle and from a handle back to a 28 // sensor is a lossless conversion. The specific value of the handle is 29 // unimportant, as long as it can be consistently converted back and forth. 30 SensorType sensorType = SensorType::Pressure; 31 uint32_t sensorHandle = getSensorHandleFromSensorType(sensorType); 32 sensorType = chre::getSensorTypeFromSensorHandle(sensorHandle); 33 EXPECT_EQ(sensorType, SensorType::Pressure); 34 35 sensorType = SensorType::Proximity; 36 sensorHandle = getSensorHandleFromSensorType(sensorType); 37 sensorType = chre::getSensorTypeFromSensorHandle(sensorHandle); 38 EXPECT_EQ(sensorType, SensorType::Proximity); 39 } 40 41 TEST(SensorType, SensorHandleToSensorTypeUnknownHandles) { 42 EXPECT_EQ(chre::getSensorTypeFromSensorHandle(0), SensorType::Unknown); 43 EXPECT_EQ(chre::getSensorTypeFromSensorHandle(10000), SensorType::Unknown); 44 } 45 46 TEST(SensorRequest, DefaultMinimalPriority) { 47 SensorRequest request; 48 EXPECT_EQ(request.getInterval(), Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT)); 49 EXPECT_EQ(request.getLatency(), Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT)); 50 EXPECT_EQ(request.getMode(), SensorMode::Off); 51 } 52 53 TEST(SensorRequest, ActiveContinuousIsHigherPriorityThanActiveOneShot) { 54 SensorRequest activeContinuous(SensorMode::ActiveContinuous, 55 Nanoseconds(0), Nanoseconds(0)); 56 SensorRequest activeOneShot(SensorMode::ActiveOneShot, 57 Nanoseconds(0), Nanoseconds(0)); 58 SensorRequest mergedRequest; 59 EXPECT_TRUE(mergedRequest.mergeWith(activeContinuous)); 60 EXPECT_FALSE(mergedRequest.mergeWith(activeOneShot)); 61 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(0)); 62 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0)); 63 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 64 } 65 66 TEST(SensorRequest, ActiveOneShotIsHigherPriorityThanPassiveContinuous) { 67 SensorRequest activeOneShot(SensorMode::ActiveOneShot, 68 Nanoseconds(0), Nanoseconds(0)); 69 SensorRequest passiveContinuous(SensorMode::PassiveContinuous, 70 Nanoseconds(0), Nanoseconds(0)); 71 SensorRequest mergedRequest; 72 EXPECT_TRUE(mergedRequest.mergeWith(activeOneShot)); 73 EXPECT_FALSE(mergedRequest.mergeWith(passiveContinuous)); 74 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(0)); 75 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0)); 76 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveOneShot); 77 78 } 79 80 TEST(SensorRequest, PassiveContinuousIsHigherPriorityThanPassiveOneShot) { 81 SensorRequest passiveContinuous(SensorMode::PassiveContinuous, 82 Nanoseconds(0), Nanoseconds(0)); 83 SensorRequest passiveOneShot(SensorMode::PassiveOneShot, 84 Nanoseconds(0), Nanoseconds(0)); 85 SensorRequest mergedRequest; 86 EXPECT_TRUE(mergedRequest.mergeWith(passiveContinuous)); 87 EXPECT_FALSE(mergedRequest.mergeWith(passiveOneShot)); 88 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(0)); 89 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0)); 90 EXPECT_EQ(mergedRequest.getMode(), SensorMode::PassiveContinuous); 91 } 92 93 TEST(SensorRequest, PassiveOneShotIsHigherPriorityThanOff) { 94 SensorRequest passiveOneShot(SensorMode::PassiveOneShot, 95 Nanoseconds(0), Nanoseconds(0)); 96 SensorRequest off(SensorMode::Off, Nanoseconds(0), Nanoseconds(0)); 97 SensorRequest mergedRequest; 98 EXPECT_TRUE(mergedRequest.mergeWith(passiveOneShot)); 99 EXPECT_FALSE(mergedRequest.mergeWith(off)); 100 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(0)); 101 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0)); 102 EXPECT_EQ(mergedRequest.getMode(), SensorMode::PassiveOneShot); 103 } 104 105 TEST(SensorRequest, LowerLatencyIsHigherPriorityThanHigherLatency) { 106 SensorRequest lowLatencyRequest(SensorMode::ActiveContinuous, 107 Nanoseconds(10), Nanoseconds(10)); 108 SensorRequest highLatencyRequest(SensorMode::ActiveOneShot, 109 Nanoseconds(10), Nanoseconds(100)); 110 SensorRequest mergedRequest; 111 EXPECT_TRUE(mergedRequest.mergeWith(lowLatencyRequest)); 112 EXPECT_FALSE(mergedRequest.mergeWith(highLatencyRequest)); 113 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 114 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(10)); 115 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 116 } 117 118 TEST(SensorRequest, HigherFrequencyIsHigherPriorityThanLowerFrequency) { 119 SensorRequest lowFreqRequest(SensorMode::ActiveOneShot, 120 Nanoseconds(100), Nanoseconds(10)); 121 SensorRequest highFreqRequest(SensorMode::ActiveContinuous, 122 Nanoseconds(10), Nanoseconds(10)); 123 SensorRequest mergedRequest; 124 EXPECT_TRUE(mergedRequest.mergeWith(lowFreqRequest)); 125 EXPECT_TRUE(mergedRequest.mergeWith(highFreqRequest)); 126 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 127 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(10)); 128 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 129 } 130 131 TEST(SensorRequest, OnlyDefaultFrequency) { 132 SensorRequest defaultFreqRequest(SensorMode::ActiveContinuous, 133 Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT), 134 Nanoseconds(0)); 135 SensorRequest mergedRequest; 136 EXPECT_TRUE(mergedRequest.mergeWith(defaultFreqRequest)); 137 EXPECT_EQ(mergedRequest.getInterval(), 138 Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT)); 139 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0)); 140 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 141 } 142 143 TEST(SensorRequest, NonDefaultAndDefaultFrequency) { 144 SensorRequest defaultFreqRequest(SensorMode::ActiveContinuous, 145 Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT), 146 Nanoseconds(0)); 147 SensorRequest nonDefaultFreqRequest(SensorMode::ActiveContinuous, 148 Nanoseconds(20000000), Nanoseconds(0)); 149 SensorRequest mergedRequest; 150 EXPECT_TRUE(mergedRequest.mergeWith(defaultFreqRequest)); 151 EXPECT_TRUE(mergedRequest.mergeWith(nonDefaultFreqRequest)); 152 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(20000000)); 153 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0)); 154 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 155 } 156 157 TEST(SensorRequest, OnlyAsapLatency) { 158 SensorRequest asapLatencyRequest(SensorMode::ActiveContinuous, 159 Nanoseconds(10), 160 Nanoseconds(CHRE_SENSOR_LATENCY_ASAP)); 161 SensorRequest mergedRequest; 162 EXPECT_TRUE(mergedRequest.mergeWith(asapLatencyRequest)); 163 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 164 EXPECT_EQ(mergedRequest.getLatency(), 165 Nanoseconds(CHRE_SENSOR_LATENCY_ASAP)); 166 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 167 } 168 169 TEST(SensorRequest, NonAsapAndAsapLatency) { 170 SensorRequest asapLatencyRequest(SensorMode::ActiveContinuous, 171 Nanoseconds(10), 172 Nanoseconds(CHRE_SENSOR_LATENCY_ASAP)); 173 SensorRequest nonAsapLatencyRequest(SensorMode::ActiveContinuous, 174 Nanoseconds(10), 175 Nanoseconds(2000)); 176 SensorRequest mergedRequest; 177 EXPECT_TRUE(mergedRequest.mergeWith(asapLatencyRequest)); 178 EXPECT_FALSE(mergedRequest.mergeWith(nonAsapLatencyRequest)); 179 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 180 EXPECT_EQ(mergedRequest.getLatency(), 181 Nanoseconds(CHRE_SENSOR_LATENCY_ASAP)); 182 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 183 } 184 185 TEST(SensorRequest, OnlyDefaultLatency) { 186 SensorRequest defaultLatencyRequest(SensorMode::ActiveContinuous, 187 Nanoseconds(10), 188 Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT)); 189 SensorRequest mergedRequest; 190 EXPECT_TRUE(mergedRequest.mergeWith(defaultLatencyRequest)); 191 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 192 EXPECT_EQ(mergedRequest.getLatency(), 193 Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT)); 194 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 195 } 196 197 TEST(SensorRequest, NonDefaultAndDefaultLatency) { 198 SensorRequest defaultLatencyRequest(SensorMode::ActiveContinuous, 199 Nanoseconds(10), 200 Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT)); 201 SensorRequest nonDefaultLatencyRequest(SensorMode::ActiveContinuous, 202 Nanoseconds(10), 203 Nanoseconds(2000)); 204 SensorRequest mergedRequest; 205 EXPECT_TRUE(mergedRequest.mergeWith(defaultLatencyRequest)); 206 EXPECT_TRUE(mergedRequest.mergeWith(nonDefaultLatencyRequest)); 207 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 208 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(2000)); 209 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 210 } 211