blob: 129113efb6506d2287982a4f00c7c66d9f302ca9 [file] [log] [blame]
Shawn McCarney1e5f9932019-10-31 12:03:27 -05001/**
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 "action_environment.hpp"
17#include "device.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060018#include "i2c_interface.hpp"
Shawn McCarney1e5f9932019-10-31 12:03:27 -050019#include "id_map.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060020#include "mocked_i2c_interface.hpp"
Shawn McCarney1e5f9932019-10-31 12:03:27 -050021#include "set_device_action.hpp"
22
23#include <exception>
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060024#include <memory>
25#include <utility>
Shawn McCarney1e5f9932019-10-31 12:03:27 -050026
27#include <gtest/gtest.h>
28
29using namespace phosphor::power::regulators;
30
31TEST(SetDeviceActionTests, Constructor)
32{
33 SetDeviceAction action{"regulator1"};
34 EXPECT_EQ(action.getDeviceID(), "regulator1");
35}
36
37TEST(SetDeviceActionTests, Execute)
38{
39 // Create IDMap
40 IDMap idMap{};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060041
42 // Create Device regulator1 and add to IDMap
43 std::unique_ptr<i2c::I2CInterface> i2cInterface =
44 i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
Bob Kinga76898f2020-10-13 15:08:33 +080045 Device reg1{
46 "regulator1", true,
47 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
48 std::move(i2cInterface)};
Shawn McCarney1e5f9932019-10-31 12:03:27 -050049 idMap.addDevice(reg1);
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060050
51 // Create Device regulator2 and add to IDMap
52 i2cInterface =
53 i2c::create(1, 0x72, i2c::I2CInterface::InitialState::CLOSED);
Bob Kinga76898f2020-10-13 15:08:33 +080054 Device reg2{
55 "regulator2", true,
56 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
57 std::move(i2cInterface)};
Shawn McCarney1e5f9932019-10-31 12:03:27 -050058 idMap.addDevice(reg2);
59
60 // Create ActionEnvironment
61 ActionEnvironment env{idMap, "regulator1"};
62
63 // Create action
64 SetDeviceAction action{"regulator2"};
65
66 // Execute action
67 try
68 {
69 EXPECT_EQ(env.getDeviceID(), "regulator1");
70 EXPECT_EQ(action.execute(env), true);
71 EXPECT_EQ(env.getDeviceID(), "regulator2");
72 }
73 catch (const std::exception& error)
74 {
75 ADD_FAILURE() << "Should not have caught exception.";
76 }
77}
78
79TEST(SetDeviceActionTests, GetDeviceID)
80{
81 SetDeviceAction action{"io_expander_0"};
82 EXPECT_EQ(action.getDeviceID(), "io_expander_0");
83}
Shawn McCarney8a3db362020-02-05 16:24:16 -060084
85TEST(SetDeviceActionTests, ToString)
86{
87 SetDeviceAction action{"regulator1"};
88 EXPECT_EQ(action.toString(), "set_device: regulator1");
89}