blob: 56592c25fa9e5185ee47c0f2eb7449ae7fbdd4b4 [file] [log] [blame]
Enrico Granata60b1cbc2019-05-20 11:06:46 -07001// Copyright 2019 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 "mems_setup/test_helper.h"
6
Enrico Granata9bb17522019-06-28 17:22:42 -07007using libmems::fakes::FakeIioChannel;
8using libmems::fakes::FakeIioContext;
9using libmems::fakes::FakeIioDevice;
10using mems_setup::fakes::FakeDelegate;
Enrico Granata60b1cbc2019-05-20 11:06:46 -070011
12namespace mems_setup {
13namespace testing {
14
Enrico Granata9bb17522019-06-28 17:22:42 -070015bool FakeSysfsTrigger::WriteNumberAttribute(const std::string& name,
Enrico Granata60b1cbc2019-05-20 11:06:46 -070016 int64_t value) {
Enrico Granata9bb17522019-06-28 17:22:42 -070017 bool ok = this->FakeIioDevice::WriteNumberAttribute(name, value);
Enrico Granata60b1cbc2019-05-20 11:06:46 -070018 if (ok && name == "add_trigger" && value == 0) {
Harvey Yang0c0dab52019-11-18 12:04:30 +080019 mock_context_->AddTrigger(mock_trigger0_);
Enrico Granata60b1cbc2019-05-20 11:06:46 -070020 }
21 return ok;
22}
23
Harvey Yang0c0dab52019-11-18 12:04:30 +080024SensorTestBase::SensorTestBase(const char* name, int id, SensorKind kind)
Enrico Granata9bb17522019-06-28 17:22:42 -070025 : mock_context_(new FakeIioContext),
26 mock_delegate_(new FakeDelegate),
Enrico Granata60b1cbc2019-05-20 11:06:46 -070027 mock_device_(
Enrico Granata9bb17522019-06-28 17:22:42 -070028 std::make_unique<FakeIioDevice>(mock_context_.get(), name, id)),
Harvey Yang0c0dab52019-11-18 12:04:30 +080029 mock_trigger0_(
30 std::make_unique<FakeIioDevice>(mock_context_.get(), "trigger0", 0)),
Enrico Granata9bb17522019-06-28 17:22:42 -070031 mock_sysfs_trigger_(std::make_unique<FakeSysfsTrigger>(
Enrico Granata60b1cbc2019-05-20 11:06:46 -070032 mock_context_.get(), mock_trigger0_.get())),
33 sensor_kind_(kind) {
34 mock_context_->AddDevice(mock_device_.get());
Gwendal Grignou7397fe72019-11-18 10:03:59 -080035 mock_device_->AddChannel(new FakeIioChannel("calibration", false));
Harvey Yang0c0dab52019-11-18 12:04:30 +080036 mock_context_->AddTrigger(mock_sysfs_trigger_.get());
Enrico Granata60b1cbc2019-05-20 11:06:46 -070037}
38
39void SensorTestBase::SetSingleSensor(const char* location) {
40 mock_device_->WriteStringAttribute("location", location);
Enrico Granata064a25c2019-07-15 15:48:03 -070041
42 if (sensor_kind_ == SensorKind::ACCELEROMETER) {
43 channels_.push_back(std::make_unique<FakeIioChannel>("accel_x", false));
44 channels_.push_back(std::make_unique<FakeIioChannel>("accel_y", false));
45 channels_.push_back(std::make_unique<FakeIioChannel>("accel_z", false));
46
47 channels_.push_back(std::make_unique<FakeIioChannel>("timestamp", true));
48 }
49
50 for (const auto& channel : channels_) {
51 mock_device_->AddChannel(channel.get());
52 }
53}
54
55void SensorTestBase::SetSharedSensor() {
56 if (sensor_kind_ == SensorKind::ACCELEROMETER) {
57 channels_.push_back(
58 std::make_unique<FakeIioChannel>("accel_x_base", false));
59 channels_.push_back(
60 std::make_unique<FakeIioChannel>("accel_y_base", false));
61 channels_.push_back(
62 std::make_unique<FakeIioChannel>("accel_z_base", false));
63
64 channels_.push_back(std::make_unique<FakeIioChannel>("accel_x_lid", false));
65 channels_.push_back(std::make_unique<FakeIioChannel>("accel_y_lid", false));
66 channels_.push_back(std::make_unique<FakeIioChannel>("accel_z_lid", false));
67
68 channels_.push_back(std::make_unique<FakeIioChannel>("timestamp", true));
69 }
70
71 for (const auto& channel : channels_) {
72 mock_device_->AddChannel(channel.get());
73 }
Enrico Granata60b1cbc2019-05-20 11:06:46 -070074}
75
76void SensorTestBase::ConfigureVpd(
77 std::initializer_list<std::pair<const char*, const char*>> values) {
78 for (const auto& value : values) {
79 mock_delegate_->SetVpdValue(value.first, value.second);
80 }
81}
82
83Configuration* SensorTestBase::GetConfiguration() {
84 if (config_ == nullptr) {
85 config_.reset(new Configuration(mock_device_.get(), sensor_kind_,
86 mock_delegate_.get()));
87 }
88
89 return config_.get();
90}
91
92} // namespace testing
93} // namespace mems_setup