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 */ 16 #include "device.hpp" 17 #include "i2c_interface.hpp" 18 #include "mocked_i2c_interface.hpp" 19 20 #include <memory> 21 #include <utility> 22 23 #include <gtest/gtest.h> 24 25 using namespace phosphor::power::regulators; 26 27 /** 28 * Create an I2CInterface object with hard-coded bus and address values. 29 * 30 * @return I2CInterface object wrapped in a unique_ptr 31 */ 32 std::unique_ptr<i2c::I2CInterface> createI2CInterface() 33 { 34 std::unique_ptr<i2c::I2CInterface> i2cInterface = 35 i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED); 36 return i2cInterface; 37 } 38 39 TEST(DeviceTests, Constructor) 40 { 41 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); 42 i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); 43 Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", 44 std::move(i2cInterface)}; 45 EXPECT_EQ(device.getID(), "vdd_reg"); 46 EXPECT_EQ(device.isRegulator(), true); 47 EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg2"); 48 EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); 49 } 50 51 TEST(DeviceTests, GetID) 52 { 53 Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2", 54 std::move(createI2CInterface())}; 55 EXPECT_EQ(device.getID(), "vdd_reg"); 56 } 57 58 TEST(DeviceTests, IsRegulator) 59 { 60 Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2", 61 std::move(createI2CInterface())}; 62 EXPECT_EQ(device.isRegulator(), false); 63 } 64 65 TEST(DeviceTests, GetFRU) 66 { 67 Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", 68 std::move(createI2CInterface())}; 69 EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg2"); 70 } 71 72 TEST(DeviceTests, GetI2CInterface) 73 { 74 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); 75 i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); 76 Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", 77 std::move(i2cInterface)}; 78 EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); 79 } 80