blob: 3fa3e3b9ef336a3d80b83cf43f22c67d86048417 [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
9#include <gtest/gtest.h>
10
11#include <libmems/iio_context.h>
12#include <libmems/iio_device.h>
13#include <libmems/test_fakes.h>
14#include "mems_setup/configuration.h"
15#include "mems_setup/delegate.h"
16#include "mems_setup/sensor_location.h"
17#include "mems_setup/test_fakes.h"
18#include "mems_setup/test_helper.h"
19
20using mems_setup::testing::SensorTestBase;
21
22namespace mems_setup {
23
24namespace {
25
26static gid_t kIioserviceGroupId = 777;
27
28#if USE_IIOSERVICE
29constexpr char kAcpiAlsTriggerName[] = "iioservice-acpi-als";
30#endif // USE_IIOSERVICE
31
32class AlsTest : public SensorTestBase {
33 public:
34 AlsTest() : SensorTestBase("acpi-als", 5, SensorKind::LIGHT) {
35 mock_delegate_->AddGroup(Configuration::GetGroupNameForSysfs(),
36 kIioserviceGroupId);
37 mock_delegate_->SetMockContext(mock_context_.get());
38 }
39};
40
41#if USE_IIOSERVICE
42TEST_F(AlsTest, TriggerSet) {
43 SetSingleSensor(kBaseSensorLocation);
44 ConfigureVpd({{"als_cal_intercept", "100"}});
45
46 EXPECT_TRUE(GetConfiguration()->Configure());
47
48 EXPECT_TRUE(mock_device_->GetTrigger());
49 EXPECT_EQ(strcmp(mock_device_->GetTrigger()->GetName(), kAcpiAlsTriggerName),
50 0);
51}
52#endif // USE_IIOSERVICE
53
54TEST_F(AlsTest, PartialVpd) {
55 SetSingleSensor(kBaseSensorLocation);
56 ConfigureVpd({{"als_cal_intercept", "100"}});
57
58 EXPECT_TRUE(GetConfiguration()->Configure());
59
60 EXPECT_TRUE(mock_device_->GetChannel("illuminance")
61 ->ReadDoubleAttribute("calibbias")
62 .has_value());
63 EXPECT_EQ(100, mock_device_->GetChannel("illuminance")
64 ->ReadDoubleAttribute("calibbias")
65 .value());
66 EXPECT_FALSE(mock_device_->GetChannel("illuminance")
67 ->ReadDoubleAttribute("calibscale")
68 .has_value());
69}
70
71TEST_F(AlsTest, VpdFormatError) {
72 SetSingleSensor(kBaseSensorLocation);
73 ConfigureVpd({{"als_cal_slope", "abc"}});
74
75 EXPECT_TRUE(GetConfiguration()->Configure());
76
77 EXPECT_FALSE(mock_device_->GetChannel("illuminance")
78 ->ReadDoubleAttribute("calibbias")
79 .has_value());
80 EXPECT_FALSE(mock_device_->GetChannel("illuminance")
81 ->ReadDoubleAttribute("calibscale")
82 .has_value());
83}
84
85TEST_F(AlsTest, ValidVpd) {
86 SetSingleSensor(kBaseSensorLocation);
87 ConfigureVpd({{"als_cal_intercept", "1.25"}, {"als_cal_slope", "12.5"}});
88
89 EXPECT_TRUE(GetConfiguration()->Configure());
90
91 EXPECT_TRUE(mock_device_->GetChannel("illuminance")
92 ->ReadDoubleAttribute("calibbias")
93 .has_value());
94 EXPECT_EQ(1.25, mock_device_->GetChannel("illuminance")
95 ->ReadDoubleAttribute("calibbias")
96 .value());
97 EXPECT_TRUE(mock_device_->GetChannel("illuminance")
98 ->ReadDoubleAttribute("calibscale")
99 .has_value());
100 EXPECT_EQ(12.5, mock_device_->GetChannel("illuminance")
101 ->ReadDoubleAttribute("calibscale")
102 .value());
103}
104
105TEST_F(AlsTest, VpdCalSlopeColorGood) {
106 SetColorLightSensor();
107 ConfigureVpd({{"als_cal_slope_color", "1.1 1.2 1.3"}});
108
109 EXPECT_TRUE(GetConfiguration()->Configure());
110
111 EXPECT_TRUE(mock_device_->GetChannel("illuminance_red")
112 ->ReadDoubleAttribute("calibscale")
113 .has_value());
114 EXPECT_EQ(1.1, mock_device_->GetChannel("illuminance_red")
115 ->ReadDoubleAttribute("calibscale")
116 .value());
117
118 EXPECT_TRUE(mock_device_->GetChannel("illuminance_green")
119 ->ReadDoubleAttribute("calibscale")
120 .has_value());
121 EXPECT_EQ(1.2, mock_device_->GetChannel("illuminance_green")
122 ->ReadDoubleAttribute("calibscale")
123 .value());
124
125 EXPECT_TRUE(mock_device_->GetChannel("illuminance_blue")
126 ->ReadDoubleAttribute("calibscale")
127 .has_value());
128 EXPECT_EQ(1.3, mock_device_->GetChannel("illuminance_blue")
129 ->ReadDoubleAttribute("calibscale")
130 .value());
131}
132
133TEST_F(AlsTest, VpdCalSlopeColorCorrupted) {
134 SetColorLightSensor();
135 ConfigureVpd({{"als_cal_slope_color", "1.1 no 1.3"}});
136
137 EXPECT_TRUE(GetConfiguration()->Configure());
138
139 EXPECT_TRUE(mock_device_->GetChannel("illuminance_red")
140 ->ReadDoubleAttribute("calibscale")
141 .has_value());
142 EXPECT_EQ(1.1, mock_device_->GetChannel("illuminance_red")
143 ->ReadDoubleAttribute("calibscale")
144 .value());
145
146 EXPECT_FALSE(mock_device_->GetChannel("illuminance_green")
147 ->ReadDoubleAttribute("calibscale")
148 .has_value());
149
150 EXPECT_FALSE(mock_device_->GetChannel("illuminance_blue")
151 ->ReadDoubleAttribute("calibscale")
152 .has_value());
153}
154
155TEST_F(AlsTest, VpdCalSlopeColorIncomplete) {
156 SetColorLightSensor();
157 ConfigureVpd({{"als_cal_slope_color", "1.1"}});
158
159 EXPECT_TRUE(GetConfiguration()->Configure());
160
161 EXPECT_FALSE(mock_device_->GetChannel("illuminance_red")
162 ->ReadDoubleAttribute("calibscale")
163 .has_value());
164
165 EXPECT_FALSE(mock_device_->GetChannel("illuminance_green")
166 ->ReadDoubleAttribute("calibscale")
167 .has_value());
168
169 EXPECT_FALSE(mock_device_->GetChannel("illuminance_blue")
170 ->ReadDoubleAttribute("calibscale")
171 .has_value());
172}
173
174} // namespace
175
176} // namespace mems_setup