Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 16 | #include "action.hpp" |
| 17 | #include "configuration.hpp" |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 18 | #include "device.hpp" |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 19 | #include "i2c_interface.hpp" |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 20 | #include "mock_action.hpp" |
| 21 | #include "presence_detection.hpp" |
| 22 | #include "rail.hpp" |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 23 | |
| 24 | #include <memory> |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 25 | #include <optional> |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 26 | #include <utility> |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 27 | #include <vector> |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 28 | |
| 29 | #include <gtest/gtest.h> |
| 30 | |
| 31 | using namespace phosphor::power::regulators; |
| 32 | |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 33 | /** |
| 34 | * Create an I2CInterface object with hard-coded bus and address values. |
| 35 | * |
| 36 | * @return I2CInterface object wrapped in a unique_ptr |
| 37 | */ |
| 38 | std::unique_ptr<i2c::I2CInterface> createI2CInterface() |
| 39 | { |
| 40 | std::unique_ptr<i2c::I2CInterface> i2cInterface = |
| 41 | i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED); |
| 42 | return i2cInterface; |
| 43 | } |
| 44 | |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 45 | TEST(DeviceTests, Constructor) |
| 46 | { |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 47 | // Test where only required parameters are specified |
| 48 | { |
| 49 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 50 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
| 51 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 52 | std::move(i2cInterface)}; |
| 53 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 54 | EXPECT_EQ(device.isRegulator(), true); |
| 55 | EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg2"); |
| 56 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
| 57 | EXPECT_EQ(device.getPresenceDetection(), nullptr); |
| 58 | EXPECT_EQ(device.getConfiguration(), nullptr); |
| 59 | EXPECT_EQ(device.getRails().size(), 0); |
| 60 | } |
| 61 | |
| 62 | // Test where all parameters are specified |
| 63 | { |
| 64 | // Create I2CInterface |
| 65 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 66 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
| 67 | |
| 68 | // Create PresenceDetection |
| 69 | std::vector<std::unique_ptr<Action>> actions{}; |
| 70 | actions.push_back(std::make_unique<MockAction>()); |
| 71 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 72 | std::make_unique<PresenceDetection>(std::move(actions)); |
| 73 | |
| 74 | // Create Configuration |
| 75 | std::optional<double> volts{}; |
| 76 | actions.clear(); |
| 77 | actions.push_back(std::make_unique<MockAction>()); |
| 78 | actions.push_back(std::make_unique<MockAction>()); |
| 79 | std::unique_ptr<Configuration> configuration = |
| 80 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 81 | |
| 82 | // Create vector of Rail objects |
| 83 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 84 | rails.push_back(std::make_unique<Rail>("vdd0")); |
| 85 | rails.push_back(std::make_unique<Rail>("vdd1")); |
| 86 | |
| 87 | // Create Device |
| 88 | Device device{"vdd_reg", |
| 89 | false, |
| 90 | "/system/chassis/motherboard/reg1", |
| 91 | std::move(i2cInterface), |
| 92 | std::move(presenceDetection), |
| 93 | std::move(configuration), |
| 94 | std::move(rails)}; |
| 95 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 96 | EXPECT_EQ(device.isRegulator(), false); |
| 97 | EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg1"); |
| 98 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
| 99 | EXPECT_NE(device.getPresenceDetection(), nullptr); |
| 100 | EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1); |
| 101 | EXPECT_NE(device.getConfiguration(), nullptr); |
| 102 | EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), false); |
| 103 | EXPECT_EQ(device.getConfiguration()->getActions().size(), 2); |
| 104 | EXPECT_EQ(device.getRails().size(), 2); |
| 105 | } |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 108 | TEST(DeviceTests, GetConfiguration) |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 109 | { |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 110 | // Test where Configuration was not specified in constructor |
| 111 | { |
| 112 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 113 | std::move(createI2CInterface())}; |
| 114 | EXPECT_EQ(device.getConfiguration(), nullptr); |
| 115 | } |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 116 | |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 117 | // Test where Configuration was specified in constructor |
| 118 | { |
| 119 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 120 | |
| 121 | // Create Configuration |
| 122 | std::optional<double> volts{3.2}; |
| 123 | std::vector<std::unique_ptr<Action>> actions{}; |
| 124 | actions.push_back(std::make_unique<MockAction>()); |
| 125 | actions.push_back(std::make_unique<MockAction>()); |
| 126 | std::unique_ptr<Configuration> configuration = |
| 127 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 128 | |
| 129 | // Create Device |
| 130 | Device device{"vdd_reg", |
| 131 | true, |
| 132 | "/system/chassis/motherboard/reg2", |
| 133 | std::move(createI2CInterface()), |
| 134 | std::move(presenceDetection), |
| 135 | std::move(configuration)}; |
| 136 | EXPECT_NE(device.getConfiguration(), nullptr); |
| 137 | EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), true); |
| 138 | EXPECT_EQ(device.getConfiguration()->getVolts().value(), 3.2); |
| 139 | EXPECT_EQ(device.getConfiguration()->getActions().size(), 2); |
| 140 | } |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | TEST(DeviceTests, GetFRU) |
| 144 | { |
| 145 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 146 | std::move(createI2CInterface())}; |
| 147 | EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg2"); |
| 148 | } |
| 149 | |
| 150 | TEST(DeviceTests, GetI2CInterface) |
| 151 | { |
| 152 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 153 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
| 154 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 155 | std::move(i2cInterface)}; |
| 156 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 157 | } |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 158 | |
| 159 | TEST(DeviceTests, GetID) |
| 160 | { |
| 161 | Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2", |
| 162 | std::move(createI2CInterface())}; |
| 163 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 164 | } |
| 165 | |
| 166 | TEST(DeviceTests, GetPresenceDetection) |
| 167 | { |
| 168 | // Test where PresenceDetection was not specified in constructor |
| 169 | { |
| 170 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 171 | std::move(createI2CInterface())}; |
| 172 | EXPECT_EQ(device.getPresenceDetection(), nullptr); |
| 173 | } |
| 174 | |
| 175 | // Test where PresenceDetection was specified in constructor |
| 176 | { |
| 177 | // Create PresenceDetection |
| 178 | std::vector<std::unique_ptr<Action>> actions{}; |
| 179 | actions.push_back(std::make_unique<MockAction>()); |
| 180 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 181 | std::make_unique<PresenceDetection>(std::move(actions)); |
| 182 | |
| 183 | // Create Device |
| 184 | Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2", |
| 185 | std::move(createI2CInterface()), |
| 186 | std::move(presenceDetection)}; |
| 187 | EXPECT_NE(device.getPresenceDetection(), nullptr); |
| 188 | EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | TEST(DeviceTests, GetRails) |
| 193 | { |
| 194 | // Test where no rails were specified in constructor |
| 195 | { |
| 196 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 197 | std::move(createI2CInterface())}; |
| 198 | EXPECT_EQ(device.getRails().size(), 0); |
| 199 | } |
| 200 | |
| 201 | // Test where rails were specified in constructor |
| 202 | { |
| 203 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 204 | std::unique_ptr<Configuration> configuration{}; |
| 205 | |
| 206 | // Create vector of Rail objects |
| 207 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 208 | rails.push_back(std::make_unique<Rail>("vdd0")); |
| 209 | rails.push_back(std::make_unique<Rail>("vdd1")); |
| 210 | |
| 211 | // Create Device |
| 212 | Device device{"vdd_reg", |
| 213 | false, |
| 214 | "/system/chassis/motherboard/reg2", |
| 215 | std::move(createI2CInterface()), |
| 216 | std::move(presenceDetection), |
| 217 | std::move(configuration), |
| 218 | std::move(rails)}; |
| 219 | EXPECT_EQ(device.getRails().size(), 2); |
| 220 | EXPECT_EQ(device.getRails()[0]->getID(), "vdd0"); |
| 221 | EXPECT_EQ(device.getRails()[1]->getID(), "vdd1"); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | TEST(DeviceTests, IsRegulator) |
| 226 | { |
| 227 | Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2", |
| 228 | std::move(createI2CInterface())}; |
| 229 | EXPECT_EQ(device.isRegulator(), false); |
| 230 | } |