Harvey Yang | 06ce00f | 2020-12-01 18:05:23 +0800 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <map> |
| 6 | #include <memory> |
| 7 | #include <string> |
| 8 | |
Harvey Yang | ee5a26d | 2021-03-23 17:19:27 +0800 | [diff] [blame] | 9 | #include <base/strings/stringprintf.h> |
Harvey Yang | 06ce00f | 2020-12-01 18:05:23 +0800 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
| 11 | |
Harvey Yang | ee5a26d | 2021-03-23 17:19:27 +0800 | [diff] [blame] | 12 | #include <libmems/common_types.h> |
Harvey Yang | 06ce00f | 2020-12-01 18:05:23 +0800 | [diff] [blame] | 13 | #include <libmems/iio_context.h> |
| 14 | #include <libmems/iio_device.h> |
| 15 | #include <libmems/test_fakes.h> |
| 16 | #include "mems_setup/configuration.h" |
| 17 | #include "mems_setup/delegate.h" |
| 18 | #include "mems_setup/sensor_location.h" |
| 19 | #include "mems_setup/test_fakes.h" |
| 20 | #include "mems_setup/test_helper.h" |
| 21 | |
| 22 | using mems_setup::testing::SensorTestBase; |
| 23 | |
| 24 | namespace mems_setup { |
| 25 | |
| 26 | namespace { |
| 27 | |
Harvey Yang | ee5a26d | 2021-03-23 17:19:27 +0800 | [diff] [blame] | 28 | constexpr int kDeviceId = 5; |
Harvey Yang | 06ce00f | 2020-12-01 18:05:23 +0800 | [diff] [blame] | 29 | static gid_t kIioserviceGroupId = 777; |
| 30 | |
Harvey Yang | 06ce00f | 2020-12-01 18:05:23 +0800 | [diff] [blame] | 31 | class AlsTest : public SensorTestBase { |
| 32 | public: |
Harvey Yang | ee5a26d | 2021-03-23 17:19:27 +0800 | [diff] [blame] | 33 | AlsTest() : SensorTestBase("acpi-als", kDeviceId) { |
Harvey Yang | 06ce00f | 2020-12-01 18:05:23 +0800 | [diff] [blame] | 34 | mock_delegate_->AddGroup(Configuration::GetGroupNameForSysfs(), |
| 35 | kIioserviceGroupId); |
| 36 | mock_delegate_->SetMockContext(mock_context_.get()); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | #if USE_IIOSERVICE |
| 41 | TEST_F(AlsTest, TriggerSet) { |
| 42 | SetSingleSensor(kBaseSensorLocation); |
| 43 | ConfigureVpd({{"als_cal_intercept", "100"}}); |
| 44 | |
| 45 | EXPECT_TRUE(GetConfiguration()->Configure()); |
| 46 | |
Harvey Yang | ee5a26d | 2021-03-23 17:19:27 +0800 | [diff] [blame] | 47 | EXPECT_FALSE(mock_device_->GetTrigger()); |
| 48 | EXPECT_EQ(mock_context_ |
| 49 | ->GetTriggersByName(base::StringPrintf( |
| 50 | libmems::kHrtimerNameFormatString, kDeviceId)) |
| 51 | .size(), |
| 52 | 1); |
Harvey Yang | 06ce00f | 2020-12-01 18:05:23 +0800 | [diff] [blame] | 53 | } |
| 54 | #endif // USE_IIOSERVICE |
| 55 | |
| 56 | TEST_F(AlsTest, PartialVpd) { |
| 57 | SetSingleSensor(kBaseSensorLocation); |
| 58 | ConfigureVpd({{"als_cal_intercept", "100"}}); |
| 59 | |
| 60 | EXPECT_TRUE(GetConfiguration()->Configure()); |
| 61 | |
| 62 | EXPECT_TRUE(mock_device_->GetChannel("illuminance") |
| 63 | ->ReadDoubleAttribute("calibbias") |
| 64 | .has_value()); |
| 65 | EXPECT_EQ(100, mock_device_->GetChannel("illuminance") |
| 66 | ->ReadDoubleAttribute("calibbias") |
| 67 | .value()); |
| 68 | EXPECT_FALSE(mock_device_->GetChannel("illuminance") |
| 69 | ->ReadDoubleAttribute("calibscale") |
| 70 | .has_value()); |
| 71 | } |
| 72 | |
| 73 | TEST_F(AlsTest, VpdFormatError) { |
| 74 | SetSingleSensor(kBaseSensorLocation); |
| 75 | ConfigureVpd({{"als_cal_slope", "abc"}}); |
| 76 | |
| 77 | EXPECT_TRUE(GetConfiguration()->Configure()); |
| 78 | |
| 79 | EXPECT_FALSE(mock_device_->GetChannel("illuminance") |
| 80 | ->ReadDoubleAttribute("calibbias") |
| 81 | .has_value()); |
| 82 | EXPECT_FALSE(mock_device_->GetChannel("illuminance") |
| 83 | ->ReadDoubleAttribute("calibscale") |
| 84 | .has_value()); |
| 85 | } |
| 86 | |
| 87 | TEST_F(AlsTest, ValidVpd) { |
| 88 | SetSingleSensor(kBaseSensorLocation); |
| 89 | ConfigureVpd({{"als_cal_intercept", "1.25"}, {"als_cal_slope", "12.5"}}); |
| 90 | |
| 91 | EXPECT_TRUE(GetConfiguration()->Configure()); |
| 92 | |
| 93 | EXPECT_TRUE(mock_device_->GetChannel("illuminance") |
| 94 | ->ReadDoubleAttribute("calibbias") |
| 95 | .has_value()); |
| 96 | EXPECT_EQ(1.25, mock_device_->GetChannel("illuminance") |
| 97 | ->ReadDoubleAttribute("calibbias") |
| 98 | .value()); |
| 99 | EXPECT_TRUE(mock_device_->GetChannel("illuminance") |
| 100 | ->ReadDoubleAttribute("calibscale") |
| 101 | .has_value()); |
| 102 | EXPECT_EQ(12.5, mock_device_->GetChannel("illuminance") |
| 103 | ->ReadDoubleAttribute("calibscale") |
| 104 | .value()); |
| 105 | } |
| 106 | |
| 107 | TEST_F(AlsTest, VpdCalSlopeColorGood) { |
| 108 | SetColorLightSensor(); |
| 109 | ConfigureVpd({{"als_cal_slope_color", "1.1 1.2 1.3"}}); |
| 110 | |
| 111 | EXPECT_TRUE(GetConfiguration()->Configure()); |
| 112 | |
| 113 | EXPECT_TRUE(mock_device_->GetChannel("illuminance_red") |
| 114 | ->ReadDoubleAttribute("calibscale") |
| 115 | .has_value()); |
| 116 | EXPECT_EQ(1.1, mock_device_->GetChannel("illuminance_red") |
| 117 | ->ReadDoubleAttribute("calibscale") |
| 118 | .value()); |
| 119 | |
| 120 | EXPECT_TRUE(mock_device_->GetChannel("illuminance_green") |
| 121 | ->ReadDoubleAttribute("calibscale") |
| 122 | .has_value()); |
| 123 | EXPECT_EQ(1.2, mock_device_->GetChannel("illuminance_green") |
| 124 | ->ReadDoubleAttribute("calibscale") |
| 125 | .value()); |
| 126 | |
| 127 | EXPECT_TRUE(mock_device_->GetChannel("illuminance_blue") |
| 128 | ->ReadDoubleAttribute("calibscale") |
| 129 | .has_value()); |
| 130 | EXPECT_EQ(1.3, mock_device_->GetChannel("illuminance_blue") |
| 131 | ->ReadDoubleAttribute("calibscale") |
| 132 | .value()); |
| 133 | } |
| 134 | |
| 135 | TEST_F(AlsTest, VpdCalSlopeColorCorrupted) { |
| 136 | SetColorLightSensor(); |
| 137 | ConfigureVpd({{"als_cal_slope_color", "1.1 no 1.3"}}); |
| 138 | |
| 139 | EXPECT_TRUE(GetConfiguration()->Configure()); |
| 140 | |
| 141 | EXPECT_TRUE(mock_device_->GetChannel("illuminance_red") |
| 142 | ->ReadDoubleAttribute("calibscale") |
| 143 | .has_value()); |
| 144 | EXPECT_EQ(1.1, mock_device_->GetChannel("illuminance_red") |
| 145 | ->ReadDoubleAttribute("calibscale") |
| 146 | .value()); |
| 147 | |
| 148 | EXPECT_FALSE(mock_device_->GetChannel("illuminance_green") |
| 149 | ->ReadDoubleAttribute("calibscale") |
| 150 | .has_value()); |
| 151 | |
| 152 | EXPECT_FALSE(mock_device_->GetChannel("illuminance_blue") |
| 153 | ->ReadDoubleAttribute("calibscale") |
| 154 | .has_value()); |
| 155 | } |
| 156 | |
| 157 | TEST_F(AlsTest, VpdCalSlopeColorIncomplete) { |
| 158 | SetColorLightSensor(); |
| 159 | ConfigureVpd({{"als_cal_slope_color", "1.1"}}); |
| 160 | |
| 161 | EXPECT_TRUE(GetConfiguration()->Configure()); |
| 162 | |
| 163 | EXPECT_FALSE(mock_device_->GetChannel("illuminance_red") |
| 164 | ->ReadDoubleAttribute("calibscale") |
| 165 | .has_value()); |
| 166 | |
| 167 | EXPECT_FALSE(mock_device_->GetChannel("illuminance_green") |
| 168 | ->ReadDoubleAttribute("calibscale") |
| 169 | .has_value()); |
| 170 | |
| 171 | EXPECT_FALSE(mock_device_->GetChannel("illuminance_blue") |
| 172 | ->ReadDoubleAttribute("calibscale") |
| 173 | .has_value()); |
| 174 | } |
| 175 | |
| 176 | } // namespace |
| 177 | |
| 178 | } // namespace mems_setup |