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::kMaxIntervalLatencyNs; 22 using chre::Nanoseconds; 23 using chre::SensorMode; 24 using chre::SensorRequest; 25 26 TEST(SensorRequest, DefaultMinimalPriority) { 27 SensorRequest request; 28 EXPECT_EQ(request.getInterval(), Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT)); 29 EXPECT_EQ(request.getLatency(), Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT)); 30 EXPECT_EQ(request.getMode(), SensorMode::Off); 31 } 32 33 TEST(SensorRequest, ActiveContinuousIsHigherPriorityThanActiveOneShot) { 34 SensorRequest activeContinuous(SensorMode::ActiveContinuous, Nanoseconds(0), 35 Nanoseconds(0)); 36 SensorRequest activeOneShot(SensorMode::ActiveOneShot, Nanoseconds(0), 37 Nanoseconds(0)); 38 SensorRequest mergedRequest; 39 EXPECT_TRUE(mergedRequest.mergeWith(activeContinuous)); 40 EXPECT_FALSE(mergedRequest.mergeWith(activeOneShot)); 41 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(0)); 42 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0)); 43 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 44 } 45 46 TEST(SensorRequest, ActiveOneShotIsHigherPriorityThanPassiveContinuous) { 47 SensorRequest activeOneShot(SensorMode::ActiveOneShot, Nanoseconds(0), 48 Nanoseconds(0)); 49 SensorRequest passiveContinuous(SensorMode::PassiveContinuous, Nanoseconds(0), 50 Nanoseconds(0)); 51 SensorRequest mergedRequest; 52 EXPECT_TRUE(mergedRequest.mergeWith(activeOneShot)); 53 EXPECT_FALSE(mergedRequest.mergeWith(passiveContinuous)); 54 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(0)); 55 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0)); 56 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveOneShot); 57 } 58 59 TEST(SensorRequest, PassiveContinuousIsHigherPriorityThanPassiveOneShot) { 60 SensorRequest passiveContinuous(SensorMode::PassiveContinuous, Nanoseconds(0), 61 Nanoseconds(0)); 62 SensorRequest passiveOneShot(SensorMode::PassiveOneShot, Nanoseconds(0), 63 Nanoseconds(0)); 64 SensorRequest mergedRequest; 65 EXPECT_TRUE(mergedRequest.mergeWith(passiveContinuous)); 66 EXPECT_FALSE(mergedRequest.mergeWith(passiveOneShot)); 67 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(0)); 68 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0)); 69 EXPECT_EQ(mergedRequest.getMode(), SensorMode::PassiveContinuous); 70 } 71 72 TEST(SensorRequest, PassiveOneShotIsHigherPriorityThanOff) { 73 SensorRequest passiveOneShot(SensorMode::PassiveOneShot, Nanoseconds(0), 74 Nanoseconds(0)); 75 SensorRequest off(SensorMode::Off, Nanoseconds(0), Nanoseconds(0)); 76 SensorRequest mergedRequest; 77 EXPECT_TRUE(mergedRequest.mergeWith(passiveOneShot)); 78 EXPECT_FALSE(mergedRequest.mergeWith(off)); 79 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(0)); 80 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0)); 81 EXPECT_EQ(mergedRequest.getMode(), SensorMode::PassiveOneShot); 82 } 83 84 TEST(SensorRequest, LowerLatencyIsHigherPriorityThanHigherLatency) { 85 SensorRequest lowLatencyRequest(SensorMode::ActiveContinuous, Nanoseconds(10), 86 Nanoseconds(10)); 87 SensorRequest highLatencyRequest(SensorMode::ActiveOneShot, Nanoseconds(10), 88 Nanoseconds(100)); 89 SensorRequest mergedRequest; 90 EXPECT_TRUE(mergedRequest.mergeWith(lowLatencyRequest)); 91 EXPECT_FALSE(mergedRequest.mergeWith(highLatencyRequest)); 92 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 93 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(10)); 94 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 95 } 96 97 TEST(SensorRequest, HigherFrequencyIsHigherPriorityThanLowerFrequency) { 98 SensorRequest lowFreqRequest(SensorMode::ActiveOneShot, Nanoseconds(100), 99 Nanoseconds(10)); 100 SensorRequest highFreqRequest(SensorMode::ActiveContinuous, Nanoseconds(10), 101 Nanoseconds(10)); 102 SensorRequest mergedRequest; 103 EXPECT_TRUE(mergedRequest.mergeWith(lowFreqRequest)); 104 EXPECT_TRUE(mergedRequest.mergeWith(highFreqRequest)); 105 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 106 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(10)); 107 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 108 } 109 110 TEST(SensorRequest, OnlyDefaultFrequency) { 111 SensorRequest defaultFreqRequest(SensorMode::ActiveContinuous, 112 Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT), 113 Nanoseconds(0)); 114 SensorRequest mergedRequest; 115 EXPECT_TRUE(mergedRequest.mergeWith(defaultFreqRequest)); 116 EXPECT_EQ(mergedRequest.getInterval(), 117 Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT)); 118 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0)); 119 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 120 } 121 122 TEST(SensorRequest, NonDefaultAndDefaultFrequency) { 123 SensorRequest defaultFreqRequest(SensorMode::ActiveContinuous, 124 Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT), 125 Nanoseconds(0)); 126 SensorRequest nonDefaultFreqRequest(SensorMode::ActiveContinuous, 127 Nanoseconds(20000000), Nanoseconds(0)); 128 SensorRequest mergedRequest; 129 EXPECT_TRUE(mergedRequest.mergeWith(defaultFreqRequest)); 130 EXPECT_TRUE(mergedRequest.mergeWith(nonDefaultFreqRequest)); 131 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(20000000)); 132 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0)); 133 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 134 } 135 136 TEST(SensorRequest, OnlyAsapLatency) { 137 SensorRequest asapLatencyRequest(SensorMode::ActiveContinuous, 138 Nanoseconds(10), 139 Nanoseconds(CHRE_SENSOR_LATENCY_ASAP)); 140 SensorRequest mergedRequest; 141 EXPECT_TRUE(mergedRequest.mergeWith(asapLatencyRequest)); 142 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 143 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(CHRE_SENSOR_LATENCY_ASAP)); 144 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 145 } 146 147 TEST(SensorRequest, NonAsapAndAsapLatency) { 148 SensorRequest asapLatencyRequest(SensorMode::ActiveContinuous, 149 Nanoseconds(10), 150 Nanoseconds(CHRE_SENSOR_LATENCY_ASAP)); 151 SensorRequest nonAsapLatencyRequest(SensorMode::ActiveContinuous, 152 Nanoseconds(10), Nanoseconds(2000)); 153 SensorRequest mergedRequest; 154 EXPECT_TRUE(mergedRequest.mergeWith(asapLatencyRequest)); 155 EXPECT_FALSE(mergedRequest.mergeWith(nonAsapLatencyRequest)); 156 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 157 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(CHRE_SENSOR_LATENCY_ASAP)); 158 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 159 } 160 161 TEST(SensorRequest, OnlyDefaultLatency) { 162 SensorRequest defaultLatencyRequest(SensorMode::ActiveContinuous, 163 Nanoseconds(10), 164 Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT)); 165 SensorRequest mergedRequest; 166 EXPECT_TRUE(mergedRequest.mergeWith(defaultLatencyRequest)); 167 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 168 EXPECT_EQ(mergedRequest.getLatency(), 169 Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT)); 170 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 171 } 172 173 TEST(SensorRequest, NonDefaultAndDefaultLatency) { 174 SensorRequest defaultLatencyRequest(SensorMode::ActiveContinuous, 175 Nanoseconds(10), 176 Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT)); 177 SensorRequest nonDefaultLatencyRequest(SensorMode::ActiveContinuous, 178 Nanoseconds(10), Nanoseconds(2000)); 179 SensorRequest mergedRequest; 180 EXPECT_TRUE(mergedRequest.mergeWith(defaultLatencyRequest)); 181 EXPECT_TRUE(mergedRequest.mergeWith(nonDefaultLatencyRequest)); 182 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 183 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(2000)); 184 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 185 } 186 187 TEST(SensorRequest, MergeWithOff) { 188 SensorRequest request(SensorMode::ActiveContinuous, Nanoseconds(10), 189 Nanoseconds(100)); 190 SensorRequest otherRequest(SensorMode::Off, Nanoseconds(1), Nanoseconds(1)); 191 EXPECT_FALSE(request.mergeWith(otherRequest)); 192 EXPECT_EQ(request.getMode(), SensorMode::ActiveContinuous); 193 EXPECT_EQ(request.getInterval(), Nanoseconds(10)); 194 EXPECT_EQ(request.getLatency(), Nanoseconds(100)); 195 } 196 197 TEST(SensorRequest, MaxNonDefaultIntervalAndLatency) { 198 SensorRequest request(SensorMode::ActiveContinuous, 199 Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT - 1), 200 Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT - 1)); 201 EXPECT_EQ(request.getMode(), SensorMode::ActiveContinuous); 202 EXPECT_EQ(request.getInterval(), Nanoseconds(kMaxIntervalLatencyNs)); 203 EXPECT_EQ(request.getLatency(), Nanoseconds(kMaxIntervalLatencyNs)); 204 } 205 206 TEST(SensorRequest, HighRateLowLatencyAndLowRateHighLatency) { 207 SensorRequest Request0(SensorMode::ActiveContinuous, Nanoseconds(100), 208 Nanoseconds(0)); 209 SensorRequest Request1(SensorMode::ActiveContinuous, Nanoseconds(10), 210 Nanoseconds(2000)); 211 SensorRequest mergedRequest; 212 EXPECT_TRUE(mergedRequest.mergeWith(Request0)); 213 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(100)); 214 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(0)); 215 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 216 217 EXPECT_TRUE(mergedRequest.mergeWith(Request1)); 218 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 219 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(90)); 220 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 221 } 222 223 TEST(SensorRequest, LowRateHighLatencyAndHighRateLowLatency) { 224 SensorRequest Request0(SensorMode::ActiveContinuous, Nanoseconds(100), 225 Nanoseconds(0)); 226 SensorRequest Request1(SensorMode::ActiveContinuous, Nanoseconds(10), 227 Nanoseconds(2000)); 228 SensorRequest mergedRequest; 229 EXPECT_TRUE(mergedRequest.mergeWith(Request1)); 230 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 231 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(2000)); 232 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 233 234 EXPECT_TRUE(mergedRequest.mergeWith(Request0)); 235 EXPECT_EQ(mergedRequest.getInterval(), Nanoseconds(10)); 236 EXPECT_EQ(mergedRequest.getLatency(), Nanoseconds(90)); 237 EXPECT_EQ(mergedRequest.getMode(), SensorMode::ActiveContinuous); 238 } 239