• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 #define LOG_TAG "Sensor_test"
18 
19 #include <sensor/Sensor.h>
20 #include <hardware/sensors.h>
21 #include <utils/Errors.h>
22 
23 #include <gtest/gtest.h>
24 
25 namespace android {
26 
27 // Returns true if the two sensors have the same attributes. Does not compare
28 // UUID since that should not be transmitted via flatten/unflatten.
sensorsMatch(const Sensor & a,const Sensor & b)29 static bool sensorsMatch(const Sensor& a, const Sensor& b) {
30     return a.getName() == b.getName () &&
31         a.getVendor() == b.getVendor () &&
32         a.getHandle() == b.getHandle () &&
33         a.getType() == b.getType () &&
34         a.getMinValue() == b.getMinValue () &&
35         a.getMaxValue() == b.getMaxValue () &&
36         a.getResolution() == b.getResolution () &&
37         a.getPowerUsage() == b.getPowerUsage () &&
38         a.getMinDelay() == b.getMinDelay () &&
39         a.getMinDelayNs() == b.getMinDelayNs () &&
40         a.getVersion() == b.getVersion () &&
41         a.getFifoReservedEventCount() == b.getFifoReservedEventCount () &&
42         a.getFifoMaxEventCount() == b.getFifoMaxEventCount () &&
43         a.getStringType() == b.getStringType () &&
44         a.getRequiredPermission() == b.getRequiredPermission () &&
45         a.isRequiredPermissionRuntime() == b.isRequiredPermissionRuntime () &&
46         a.getRequiredAppOp() == b.getRequiredAppOp () &&
47         a.getMaxDelay() == b.getMaxDelay () &&
48         a.getFlags() == b.getFlags () &&
49         a.isWakeUpSensor() == b.isWakeUpSensor () &&
50         a.isDynamicSensor() == b.isDynamicSensor () &&
51         a.hasAdditionalInfo() == b.hasAdditionalInfo () &&
52         a.getReportingMode() == b.getReportingMode();
53 }
54 
55 // Creates and returns a sensor_t struct with some default values filled in.
getTestSensorT()56 static sensor_t getTestSensorT() {
57     sensor_t hwSensor = {};
58     hwSensor.name = "Test Sensor";
59     hwSensor.vendor = "Test Vendor";
60     hwSensor.version = 1;
61     hwSensor.handle = 2;
62     hwSensor.type = SENSOR_TYPE_ACCELEROMETER;
63     hwSensor.maxRange = 10.f;
64     hwSensor.resolution = 1.f;
65     hwSensor.power = 5.f;
66     hwSensor.minDelay = 1000;
67     hwSensor.fifoReservedEventCount = 50;
68     hwSensor.fifoMaxEventCount = 100;
69     hwSensor.stringType = SENSOR_STRING_TYPE_ACCELEROMETER;
70     hwSensor.requiredPermission = "";
71     hwSensor.maxDelay = 5000;
72     hwSensor.flags = SENSOR_FLAG_CONTINUOUS_MODE;
73     return hwSensor;
74 }
75 
TEST(SensorTest,FlattenAndUnflatten)76 TEST(SensorTest, FlattenAndUnflatten) {
77     sensor_t hwSensor = getTestSensorT();
78     Sensor sensor1(&hwSensor, SENSORS_DEVICE_API_VERSION_1_4);
79     Sensor sensor2;
80 
81     std::vector<uint8_t> buffer(sensor1.getFlattenedSize());
82     ASSERT_EQ(OK, sensor1.flatten(buffer.data(), buffer.size()));
83     ASSERT_EQ(OK, sensor2.unflatten(buffer.data(), buffer.size()));
84 
85     EXPECT_TRUE(sensorsMatch(sensor1, sensor2));
86 }
87 
88 } // namespace android
89