1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/power/auto_screen_brightness/light_provider_mojo.h"
#include <map>
#include <memory>
#include <utility>
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "chrome/browser/ash/power/auto_screen_brightness/fake_observer.h"
#include "chromeos/components/sensors/ash/sensor_hal_dispatcher.h"
#include "chromeos/components/sensors/fake_sensor_device.h"
#include "chromeos/components/sensors/fake_sensor_hal_server.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace power {
namespace auto_screen_brightness {
namespace {
constexpr int32_t kFakeAcpiAlsId = 1;
constexpr int32_t kFakeBaseLightId = 2;
constexpr int32_t kFakeLidLightId = 3;
constexpr int64_t kFakeSampleData = 50;
constexpr char kCrosECLightName[] = "cros-ec-light";
constexpr char kAcpiAlsName[] = "acpi-als";
} // namespace
class LightProviderMojoTest : public testing::Test {
protected:
void SetUp() override {
chromeos::sensors::SensorHalDispatcher::Initialize();
sensor_hal_server_ =
std::make_unique<chromeos::sensors::FakeSensorHalServer>();
als_reader_ = std::make_unique<AlsReader>();
als_reader_->AddObserver(&fake_observer_);
}
void TearDown() override {
chromeos::sensors::SensorHalDispatcher::Shutdown();
}
void SetProvider() {
provider_ = std::make_unique<LightProviderMojo>(als_reader_.get());
}
void AddDevice(int32_t iio_device_id,
const std::optional<std::string> name,
const std::optional<std::string> location) {
std::vector<chromeos::sensors::FakeSensorDevice::ChannelData> channels_data(
1);
channels_data[0].id = chromeos::sensors::mojom::kLightChannel;
channels_data[0].sample_data = kFakeSampleData;
std::unique_ptr<chromeos::sensors::FakeSensorDevice> sensor_device(
new chromeos::sensors::FakeSensorDevice(std::move(channels_data)));
sensor_devices_[iio_device_id] = sensor_device.get();
if (name.has_value()) {
sensor_device->SetAttribute(chromeos::sensors::mojom::kDeviceName,
name.value());
}
if (location.has_value()) {
sensor_device->SetAttribute(chromeos::sensors::mojom::kLocation,
location.value());
}
sensor_hal_server_->GetSensorService()->SetDevice(
iio_device_id,
std::set<chromeos::sensors::mojom::DeviceType>{
chromeos::sensors::mojom::DeviceType::LIGHT},
std::move(sensor_device));
}
void StartConnection() {
chromeos::sensors::SensorHalDispatcher::GetInstance()->RegisterServer(
sensor_hal_server_->PassRemote());
}
void TriggerNewDevicesTimeout() { provider_->OnNewDevicesTimeout(); }
void CheckValues(int32_t iio_device_id) {
EXPECT_TRUE(sensor_hal_server_->GetSensorService()->HasReceivers());
EXPECT_TRUE(sensor_devices_.find(iio_device_id) != sensor_devices_.end());
EXPECT_TRUE(sensor_devices_[iio_device_id]->HasReceivers());
EXPECT_EQ(fake_observer_.status(), AlsReader::AlsInitStatus::kSuccess);
EXPECT_EQ(fake_observer_.num_received_ambient_lights(), ++num_samples_);
EXPECT_EQ(fake_observer_.ambient_light(), kFakeSampleData);
}
FakeObserver fake_observer_;
std::unique_ptr<chromeos::sensors::FakeSensorHalServer> sensor_hal_server_;
std::unique_ptr<AlsReader> als_reader_;
std::unique_ptr<LightProviderMojo> provider_;
std::map<int32_t,
raw_ptr<chromeos::sensors::FakeSensorDevice, CtnExperimental>>
sensor_devices_;
int num_samples_ = 0;
base::test::SingleThreadTaskEnvironment task_environment;
};
TEST_F(LightProviderMojoTest, AssumingAcpiAlsWithoutDeviceNameWithOneSensor) {
SetProvider();
AddDevice(kFakeAcpiAlsId, std::nullopt, std::nullopt);
StartConnection();
// Wait until a sample is received.
base::RunLoop().RunUntilIdle();
CheckValues(kFakeAcpiAlsId);
}
TEST_F(LightProviderMojoTest, PreferCrosECLight) {
SetProvider();
AddDevice(kFakeAcpiAlsId, kAcpiAlsName, std::nullopt);
AddDevice(kFakeLidLightId, kCrosECLightName, std::nullopt);
StartConnection();
// Wait until a sample is received.
base::RunLoop().RunUntilIdle();
CheckValues(kFakeLidLightId);
}
TEST_F(LightProviderMojoTest, GetSamplesFromLidLights) {
SetProvider();
AddDevice(kFakeAcpiAlsId, kAcpiAlsName, std::nullopt);
AddDevice(kFakeBaseLightId, kCrosECLightName,
chromeos::sensors::mojom::kLocationBase);
AddDevice(kFakeLidLightId, kCrosECLightName,
chromeos::sensors::mojom::kLocationLid);
StartConnection();
// Wait until a sample is received.
base::RunLoop().RunUntilIdle();
CheckValues(kFakeLidLightId);
// Simulate a disconnection of the accelerometer's mojo channel in IIO
// Service.
AddDevice(kFakeLidLightId, kCrosECLightName,
chromeos::sensors::mojom::kLocationLid);
// Wait until the disconnection is done.
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(sensor_hal_server_->GetSensorService()->HasReceivers());
// Simulate a disconnection of IIO Service.
sensor_hal_server_->GetSensorService()->ClearReceivers();
sensor_hal_server_->OnServerDisconnect();
// Wait until the disconnect arrives at SensorHalDispatcher.
base::RunLoop().RunUntilIdle();
StartConnection();
// Wait until samples are received.
base::RunLoop().RunUntilIdle();
CheckValues(kFakeLidLightId);
}
TEST_F(LightProviderMojoTest, PreferLateCrosECLight) {
SetProvider();
StartConnection();
// Wait until all tasks are done.
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(fake_observer_.has_status());
AddDevice(kFakeAcpiAlsId, kAcpiAlsName, std::nullopt);
// Wait until all tasks are done.
base::RunLoop().RunUntilIdle();
// Acpi-als is used.
CheckValues(kFakeAcpiAlsId);
AddDevice(kFakeLidLightId, kCrosECLightName, std::nullopt);
// Wait until all tasks are done.
base::RunLoop().RunUntilIdle();
// Simulate the timeout.
TriggerNewDevicesTimeout();
// Wait until all tasks are done.
base::RunLoop().RunUntilIdle();
// Cros-ec-light overwrites the acpi-als.
CheckValues(kFakeLidLightId);
}
TEST_F(LightProviderMojoTest, GetSamplesFromLateLidLights) {
SetProvider();
StartConnection();
// Wait until all tasks are done.
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(fake_observer_.has_status());
AddDevice(kFakeAcpiAlsId, kAcpiAlsName, std::nullopt);
AddDevice(kFakeBaseLightId, kCrosECLightName,
chromeos::sensors::mojom::kLocationBase);
// Wait until all tasks are done.
base::RunLoop().RunUntilIdle();
CheckValues(kFakeBaseLightId);
AddDevice(kFakeLidLightId, kCrosECLightName,
chromeos::sensors::mojom::kLocationLid);
// Wait until all tasks are done.
base::RunLoop().RunUntilIdle();
// Simulate the timeout.
TriggerNewDevicesTimeout();
// Wait until all tasks are done.
base::RunLoop().RunUntilIdle();
CheckValues(kFakeLidLightId);
}
TEST_F(LightProviderMojoTest, DeviceRemoved) {
SetProvider();
AddDevice(kFakeAcpiAlsId, kAcpiAlsName, std::nullopt);
AddDevice(kFakeBaseLightId, kCrosECLightName,
chromeos::sensors::mojom::kLocationBase);
AddDevice(kFakeLidLightId, kCrosECLightName,
chromeos::sensors::mojom::kLocationLid);
StartConnection();
// Wait until a sample is received.
base::RunLoop().RunUntilIdle();
// cros-ec-light on lid is the highest priority.
CheckValues(kFakeLidLightId);
sensor_devices_[kFakeAcpiAlsId]->ClearReceiversWithReason(
chromeos::sensors::mojom::SensorDeviceDisconnectReason::DEVICE_REMOVED,
"Device was removed");
// Wait until the disconnection is done.
base::RunLoop().RunUntilIdle();
// The sensor service is not reset with the reason: DEVICE_REMOVED.
EXPECT_TRUE(sensor_hal_server_->GetSensorService()->HasReceivers());
// Wait until samples are received.
base::RunLoop().RunUntilIdle();
sensor_devices_[kFakeLidLightId]->ClearReceiversWithReason(
chromeos::sensors::mojom::SensorDeviceDisconnectReason::DEVICE_REMOVED,
"Device was removed");
// Overwrite the lid light sensor in the iioservice.
AddDevice(kFakeLidLightId, "", std::nullopt);
// Wait until the disconnection and LightProviderMojo::ResetStates are done.
base::RunLoop().RunUntilIdle();
// Simulate the timeout.
TriggerNewDevicesTimeout();
// Wait until all tasks are done.
base::RunLoop().RunUntilIdle();
CheckValues(kFakeBaseLightId);
}
} // namespace auto_screen_brightness
} // namespace power
} // namespace ash
|