blob: 2967949a6d5e3c934c5c6b7bf84f8ddcb8260236 [file] [log] [blame]
Harvey Yang06ce00f2020-12-01 18:05:23 +08001// 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 Yangee5a26d2021-03-23 17:19:27 +08009#include <base/strings/stringprintf.h>
Harvey Yang06ce00f2020-12-01 18:05:23 +080010#include <gtest/gtest.h>
11
Harvey Yangee5a26d2021-03-23 17:19:27 +080012#include <libmems/common_types.h>
Harvey Yang06ce00f2020-12-01 18:05:23 +080013#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
22using mems_setup::testing::SensorTestBase;
23
24namespace mems_setup {
25
26namespace {
27
Harvey Yangee5a26d2021-03-23 17:19:27 +080028constexpr int kDeviceId = 5;
Harvey Yang06ce00f2020-12-01 18:05:23 +080029static gid_t kIioserviceGroupId = 777;
30
Harvey Yang06ce00f2020-12-01 18:05:23 +080031class AlsTest : public SensorTestBase {
32 public:
Harvey Yangee5a26d2021-03-23 17:19:27 +080033 AlsTest() : SensorTestBase("acpi-als", kDeviceId) {
Harvey Yang06ce00f2020-12-01 18:05:23 +080034 mock_delegate_->AddGroup(Configuration::GetGroupNameForSysfs(),
35 kIioserviceGroupId);
36 mock_delegate_->SetMockContext(mock_context_.get());
37 }
38};
39
40#if USE_IIOSERVICE
41TEST_F(AlsTest, TriggerSet) {
42 SetSingleSensor(kBaseSensorLocation);
43 ConfigureVpd({{"als_cal_intercept", "100"}});
44
45 EXPECT_TRUE(GetConfiguration()->Configure());
46
Harvey Yangee5a26d2021-03-23 17:19:27 +080047 EXPECT_FALSE(mock_device_->GetTrigger());
48 EXPECT_EQ(mock_context_
49 ->GetTriggersByName(base::StringPrintf(
50 libmems::kHrtimerNameFormatString, kDeviceId))
51 .size(),
52 1);
Harvey Yang06ce00f2020-12-01 18:05:23 +080053}
54#endif // USE_IIOSERVICE
55
56TEST_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
73TEST_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
87TEST_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
107TEST_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
135TEST_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
157TEST_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