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 | |
| 7 | using mems_setup::mocks::MockDelegate; |
| 8 | using mems_setup::mocks::MockIioChannel; |
| 9 | using mems_setup::mocks::MockIioContext; |
| 10 | using mems_setup::mocks::MockIioDevice; |
| 11 | |
| 12 | namespace mems_setup { |
| 13 | namespace testing { |
| 14 | |
| 15 | bool MockSysfsTrigger::WriteNumberAttribute(const std::string& name, |
| 16 | int64_t value) { |
| 17 | bool ok = this->MockIioDevice::WriteNumberAttribute(name, value); |
| 18 | if (ok && name == "add_trigger" && value == 0) { |
| 19 | mock_context_->AddDevice(mock_trigger0_); |
| 20 | } |
| 21 | return ok; |
| 22 | } |
| 23 | |
| 24 | SensorTestBase::SensorTestBase(const char* name, |
| 25 | const char* id, |
| 26 | SensorKind kind) |
| 27 | : mock_context_(new MockIioContext), |
| 28 | mock_delegate_(new MockDelegate), |
| 29 | mock_device_( |
| 30 | std::make_unique<MockIioDevice>(mock_context_.get(), name, id)), |
| 31 | mock_trigger0_(std::make_unique<MockIioDevice>( |
| 32 | mock_context_.get(), "trigger0", "trigger0")), |
| 33 | mock_sysfs_trigger_(std::make_unique<MockSysfsTrigger>( |
| 34 | mock_context_.get(), mock_trigger0_.get())), |
| 35 | sensor_kind_(kind) { |
| 36 | mock_context_->AddDevice(mock_device_.get()); |
| 37 | mock_context_->AddDevice(mock_sysfs_trigger_.get()); |
| 38 | } |
| 39 | |
| 40 | void SensorTestBase::SetSingleSensor(const char* location) { |
| 41 | mock_device_->WriteStringAttribute("location", location); |
| 42 | } |
| 43 | |
| 44 | void SensorTestBase::ConfigureVpd( |
| 45 | std::initializer_list<std::pair<const char*, const char*>> values) { |
| 46 | for (const auto& value : values) { |
| 47 | mock_delegate_->SetVpdValue(value.first, value.second); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | Configuration* SensorTestBase::GetConfiguration() { |
| 52 | if (config_ == nullptr) { |
| 53 | config_.reset(new Configuration(mock_device_.get(), sensor_kind_, |
| 54 | mock_delegate_.get())); |
| 55 | } |
| 56 | |
| 57 | return config_.get(); |
| 58 | } |
| 59 | |
| 60 | } // namespace testing |
| 61 | } // namespace mems_setup |