Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 1 | // 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 Granata | 9bb1752 | 2019-06-28 17:22:42 -0700 | [diff] [blame] | 7 | using libmems::fakes::FakeIioChannel; |
| 8 | using libmems::fakes::FakeIioContext; |
| 9 | using libmems::fakes::FakeIioDevice; |
| 10 | using mems_setup::fakes::FakeDelegate; |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 11 | |
| 12 | namespace mems_setup { |
| 13 | namespace testing { |
| 14 | |
Enrico Granata | 9bb1752 | 2019-06-28 17:22:42 -0700 | [diff] [blame] | 15 | bool FakeSysfsTrigger::WriteNumberAttribute(const std::string& name, |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 16 | int64_t value) { |
Enrico Granata | 9bb1752 | 2019-06-28 17:22:42 -0700 | [diff] [blame] | 17 | bool ok = this->FakeIioDevice::WriteNumberAttribute(name, value); |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 18 | if (ok && name == "add_trigger" && value == 0) { |
Gwendal Grignou | 1a66337 | 2020-01-24 09:36:49 -0800 | [diff] [blame] | 19 | mock_context_->AddTrigger(mock_trigger_); |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 20 | } |
| 21 | return ok; |
| 22 | } |
| 23 | |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 24 | SensorTestBase::SensorTestBase(const char* name, int id, SensorKind kind) |
Enrico Granata | 9bb1752 | 2019-06-28 17:22:42 -0700 | [diff] [blame] | 25 | : mock_context_(new FakeIioContext), |
| 26 | mock_delegate_(new FakeDelegate), |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 27 | mock_device_( |
Enrico Granata | 9bb1752 | 2019-06-28 17:22:42 -0700 | [diff] [blame] | 28 | std::make_unique<FakeIioDevice>(mock_context_.get(), name, id)), |
Gwendal Grignou | 1a66337 | 2020-01-24 09:36:49 -0800 | [diff] [blame] | 29 | mock_trigger1_( |
| 30 | std::make_unique<FakeIioDevice>(mock_context_.get(), |
| 31 | "sysfstrig0", 1)), |
Enrico Granata | 9bb1752 | 2019-06-28 17:22:42 -0700 | [diff] [blame] | 32 | mock_sysfs_trigger_(std::make_unique<FakeSysfsTrigger>( |
Gwendal Grignou | 1a66337 | 2020-01-24 09:36:49 -0800 | [diff] [blame] | 33 | mock_context_.get(), mock_trigger1_.get())), |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 34 | sensor_kind_(kind) { |
| 35 | mock_context_->AddDevice(mock_device_.get()); |
Gwendal Grignou | 7397fe7 | 2019-11-18 10:03:59 -0800 | [diff] [blame] | 36 | mock_device_->AddChannel(new FakeIioChannel("calibration", false)); |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 37 | mock_context_->AddTrigger(mock_sysfs_trigger_.get()); |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void SensorTestBase::SetSingleSensor(const char* location) { |
| 41 | mock_device_->WriteStringAttribute("location", location); |
Enrico Granata | 064a25c | 2019-07-15 15:48:03 -0700 | [diff] [blame] | 42 | |
| 43 | if (sensor_kind_ == SensorKind::ACCELEROMETER) { |
| 44 | channels_.push_back(std::make_unique<FakeIioChannel>("accel_x", false)); |
| 45 | channels_.push_back(std::make_unique<FakeIioChannel>("accel_y", false)); |
| 46 | channels_.push_back(std::make_unique<FakeIioChannel>("accel_z", false)); |
| 47 | |
| 48 | channels_.push_back(std::make_unique<FakeIioChannel>("timestamp", true)); |
| 49 | } |
| 50 | |
| 51 | for (const auto& channel : channels_) { |
| 52 | mock_device_->AddChannel(channel.get()); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void SensorTestBase::SetSharedSensor() { |
| 57 | if (sensor_kind_ == SensorKind::ACCELEROMETER) { |
| 58 | channels_.push_back( |
| 59 | std::make_unique<FakeIioChannel>("accel_x_base", false)); |
| 60 | channels_.push_back( |
| 61 | std::make_unique<FakeIioChannel>("accel_y_base", false)); |
| 62 | channels_.push_back( |
| 63 | std::make_unique<FakeIioChannel>("accel_z_base", false)); |
| 64 | |
| 65 | channels_.push_back(std::make_unique<FakeIioChannel>("accel_x_lid", false)); |
| 66 | channels_.push_back(std::make_unique<FakeIioChannel>("accel_y_lid", false)); |
| 67 | channels_.push_back(std::make_unique<FakeIioChannel>("accel_z_lid", false)); |
| 68 | |
| 69 | channels_.push_back(std::make_unique<FakeIioChannel>("timestamp", true)); |
| 70 | } |
| 71 | |
| 72 | for (const auto& channel : channels_) { |
| 73 | mock_device_->AddChannel(channel.get()); |
| 74 | } |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void SensorTestBase::ConfigureVpd( |
| 78 | std::initializer_list<std::pair<const char*, const char*>> values) { |
| 79 | for (const auto& value : values) { |
| 80 | mock_delegate_->SetVpdValue(value.first, value.second); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | Configuration* SensorTestBase::GetConfiguration() { |
| 85 | if (config_ == nullptr) { |
Gwendal Grignou | ba911fe | 2019-12-08 17:14:53 -0800 | [diff] [blame] | 86 | config_.reset(new Configuration(mock_context_.get(), mock_device_.get(), |
| 87 | sensor_kind_, mock_delegate_.get())); |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return config_.get(); |
| 91 | } |
| 92 | |
| 93 | } // namespace testing |
| 94 | } // namespace mems_setup |