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 | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 23 | #include "test_utils.hpp" |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 24 | |
| 25 | #include <memory> |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 26 | #include <optional> |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 27 | #include <utility> |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 28 | #include <vector> |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 29 | |
| 30 | #include <gtest/gtest.h> |
| 31 | |
| 32 | using namespace phosphor::power::regulators; |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 33 | using namespace phosphor::power::regulators::test_utils; |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 34 | |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 35 | TEST(DeviceTests, Constructor) |
| 36 | { |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 37 | // Test where only required parameters are specified |
| 38 | { |
| 39 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 40 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
| 41 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 42 | std::move(i2cInterface)}; |
| 43 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 44 | EXPECT_EQ(device.isRegulator(), true); |
| 45 | EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg2"); |
| 46 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
| 47 | EXPECT_EQ(device.getPresenceDetection(), nullptr); |
| 48 | EXPECT_EQ(device.getConfiguration(), nullptr); |
| 49 | EXPECT_EQ(device.getRails().size(), 0); |
| 50 | } |
| 51 | |
| 52 | // Test where all parameters are specified |
| 53 | { |
| 54 | // Create I2CInterface |
| 55 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 56 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
| 57 | |
| 58 | // Create PresenceDetection |
| 59 | std::vector<std::unique_ptr<Action>> actions{}; |
| 60 | actions.push_back(std::make_unique<MockAction>()); |
| 61 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 62 | std::make_unique<PresenceDetection>(std::move(actions)); |
| 63 | |
| 64 | // Create Configuration |
| 65 | std::optional<double> volts{}; |
| 66 | actions.clear(); |
| 67 | actions.push_back(std::make_unique<MockAction>()); |
| 68 | actions.push_back(std::make_unique<MockAction>()); |
| 69 | std::unique_ptr<Configuration> configuration = |
| 70 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 71 | |
| 72 | // Create vector of Rail objects |
| 73 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 74 | rails.push_back(std::make_unique<Rail>("vdd0")); |
| 75 | rails.push_back(std::make_unique<Rail>("vdd1")); |
| 76 | |
| 77 | // Create Device |
| 78 | Device device{"vdd_reg", |
| 79 | false, |
| 80 | "/system/chassis/motherboard/reg1", |
| 81 | std::move(i2cInterface), |
| 82 | std::move(presenceDetection), |
| 83 | std::move(configuration), |
| 84 | std::move(rails)}; |
| 85 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 86 | EXPECT_EQ(device.isRegulator(), false); |
| 87 | EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg1"); |
| 88 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
| 89 | EXPECT_NE(device.getPresenceDetection(), nullptr); |
| 90 | EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1); |
| 91 | EXPECT_NE(device.getConfiguration(), nullptr); |
| 92 | EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), false); |
| 93 | EXPECT_EQ(device.getConfiguration()->getActions().size(), 2); |
| 94 | EXPECT_EQ(device.getRails().size(), 2); |
| 95 | } |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 96 | } |
| 97 | |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 98 | TEST(DeviceTests, GetConfiguration) |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 99 | { |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 100 | // Test where Configuration was not specified in constructor |
| 101 | { |
| 102 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 103 | std::move(createI2CInterface())}; |
| 104 | EXPECT_EQ(device.getConfiguration(), nullptr); |
| 105 | } |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 106 | |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 107 | // Test where Configuration was specified in constructor |
| 108 | { |
| 109 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 110 | |
| 111 | // Create Configuration |
| 112 | std::optional<double> volts{3.2}; |
| 113 | std::vector<std::unique_ptr<Action>> actions{}; |
| 114 | actions.push_back(std::make_unique<MockAction>()); |
| 115 | actions.push_back(std::make_unique<MockAction>()); |
| 116 | std::unique_ptr<Configuration> configuration = |
| 117 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 118 | |
| 119 | // Create Device |
| 120 | Device device{"vdd_reg", |
| 121 | true, |
| 122 | "/system/chassis/motherboard/reg2", |
| 123 | std::move(createI2CInterface()), |
| 124 | std::move(presenceDetection), |
| 125 | std::move(configuration)}; |
| 126 | EXPECT_NE(device.getConfiguration(), nullptr); |
| 127 | EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), true); |
| 128 | EXPECT_EQ(device.getConfiguration()->getVolts().value(), 3.2); |
| 129 | EXPECT_EQ(device.getConfiguration()->getActions().size(), 2); |
| 130 | } |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TEST(DeviceTests, GetFRU) |
| 134 | { |
| 135 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 136 | std::move(createI2CInterface())}; |
| 137 | EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg2"); |
| 138 | } |
| 139 | |
| 140 | TEST(DeviceTests, GetI2CInterface) |
| 141 | { |
| 142 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 143 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
| 144 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 145 | std::move(i2cInterface)}; |
| 146 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 147 | } |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 148 | |
| 149 | TEST(DeviceTests, GetID) |
| 150 | { |
| 151 | Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2", |
| 152 | std::move(createI2CInterface())}; |
| 153 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 154 | } |
| 155 | |
| 156 | TEST(DeviceTests, GetPresenceDetection) |
| 157 | { |
| 158 | // Test where PresenceDetection was not specified in constructor |
| 159 | { |
| 160 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 161 | std::move(createI2CInterface())}; |
| 162 | EXPECT_EQ(device.getPresenceDetection(), nullptr); |
| 163 | } |
| 164 | |
| 165 | // Test where PresenceDetection was specified in constructor |
| 166 | { |
| 167 | // Create PresenceDetection |
| 168 | std::vector<std::unique_ptr<Action>> actions{}; |
| 169 | actions.push_back(std::make_unique<MockAction>()); |
| 170 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 171 | std::make_unique<PresenceDetection>(std::move(actions)); |
| 172 | |
| 173 | // Create Device |
| 174 | Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2", |
| 175 | std::move(createI2CInterface()), |
| 176 | std::move(presenceDetection)}; |
| 177 | EXPECT_NE(device.getPresenceDetection(), nullptr); |
| 178 | EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | TEST(DeviceTests, GetRails) |
| 183 | { |
| 184 | // Test where no rails were specified in constructor |
| 185 | { |
| 186 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 187 | std::move(createI2CInterface())}; |
| 188 | EXPECT_EQ(device.getRails().size(), 0); |
| 189 | } |
| 190 | |
| 191 | // Test where rails were specified in constructor |
| 192 | { |
| 193 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 194 | std::unique_ptr<Configuration> configuration{}; |
| 195 | |
| 196 | // Create vector of Rail objects |
| 197 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 198 | rails.push_back(std::make_unique<Rail>("vdd0")); |
| 199 | rails.push_back(std::make_unique<Rail>("vdd1")); |
| 200 | |
| 201 | // Create Device |
| 202 | Device device{"vdd_reg", |
| 203 | false, |
| 204 | "/system/chassis/motherboard/reg2", |
| 205 | std::move(createI2CInterface()), |
| 206 | std::move(presenceDetection), |
| 207 | std::move(configuration), |
| 208 | std::move(rails)}; |
| 209 | EXPECT_EQ(device.getRails().size(), 2); |
| 210 | EXPECT_EQ(device.getRails()[0]->getID(), "vdd0"); |
| 211 | EXPECT_EQ(device.getRails()[1]->getID(), "vdd1"); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | TEST(DeviceTests, IsRegulator) |
| 216 | { |
| 217 | Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2", |
| 218 | std::move(createI2CInterface())}; |
| 219 | EXPECT_EQ(device.isRegulator(), false); |
| 220 | } |