blob: c1dfaed36d087a8088c91d356e99c25f2c3746b4 [file] [log] [blame]
Enrico Granata51cdb942019-06-18 16:40:17 -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 "libmems/test_mocks.h"
6
7#include <base/logging.h>
8
9namespace libmems {
10namespace mocks {
11
12MockIioChannel::MockIioChannel(const std::string& id, bool enabled)
13 : id_(id), enabled_(enabled) {}
14
15bool MockIioChannel::SetEnabled(bool en) {
16 enabled_ = en;
17 return true;
18}
19
20MockIioDevice::MockIioDevice(MockIioContext* ctx,
21 const std::string& name,
22 const std::string& id)
23 : IioDevice(), context_(ctx), name_(name), id_(id) {}
24
25base::Optional<std::string> MockIioDevice::ReadStringAttribute(
26 const std::string& name) const {
27 auto k = text_attributes_.find(name);
28 if (k == text_attributes_.end())
29 return base::nullopt;
30 return k->second;
31}
32base::Optional<int64_t> MockIioDevice::ReadNumberAttribute(
33 const std::string& name) const {
34 auto k = numeric_attributes_.find(name);
35 if (k == numeric_attributes_.end())
36 return base::nullopt;
37 return k->second;
38}
39
40bool MockIioDevice::WriteStringAttribute(const std::string& name,
41 const std::string& value) {
42 text_attributes_[name] = value;
43 return true;
44}
45bool MockIioDevice::WriteNumberAttribute(const std::string& name,
46 int64_t value) {
47 numeric_attributes_[name] = value;
48 return true;
49}
50
51bool MockIioDevice::SetTrigger(IioDevice* trigger) {
52 trigger_ = trigger;
53 return true;
54}
55
56IioChannel* MockIioDevice::GetChannel(const std::string& id) {
57 auto k = channels_.find(id);
58 if (k == channels_.end())
59 return nullptr;
60 return k->second;
61}
62
63bool MockIioDevice::EnableBuffer(size_t n) {
64 buffer_length_ = n;
65 buffer_enabled_ = true;
66 return true;
67}
68bool MockIioDevice::DisableBuffer() {
69 buffer_enabled_ = false;
70 return true;
71}
72bool MockIioDevice::IsBufferEnabled(size_t* n) const {
73 if (n && buffer_enabled_)
74 *n = buffer_length_;
75 return buffer_enabled_;
76}
77
78void MockIioContext::AddDevice(MockIioDevice* device) {
79 CHECK(device);
80 devices_.emplace(device->GetName(), device);
81 devices_.emplace(device->GetId(), device);
82}
83
84IioDevice* MockIioContext::GetDevice(const std::string& name) {
85 auto k = devices_.find(name);
86 return (k == devices_.end()) ? nullptr : k->second;
87}
88
89} // namespace mocks
90} // namespace libmems