Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 1 | #include "lpcsnoop/snoop.hpp" |
| 2 | |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 3 | #include <sdbusplus/bus.hpp> |
| 4 | #include <sdbusplus/test/sdbus_mock.hpp> |
Kun Yi | 2ae207c | 2018-06-11 16:09:46 -0700 | [diff] [blame] | 5 | |
Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 6 | #include <gmock/gmock.h> |
| 7 | #include <gtest/gtest.h> |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 8 | |
Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 9 | using ::testing::_; |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 10 | using ::testing::IsNull; |
| 11 | using ::testing::NiceMock; |
| 12 | using ::testing::Return; |
| 13 | using ::testing::StrEq; |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 14 | |
| 15 | namespace |
| 16 | { |
| 17 | |
| 18 | // Fixture for testing class PostReporter |
Kun Yi | 2ae207c | 2018-06-11 16:09:46 -0700 | [diff] [blame] | 19 | class PostReporterTest : public ::testing::Test |
| 20 | { |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 21 | protected: |
| 22 | PostReporterTest() : bus_mock(), bus(sdbusplus::get_mocked_new(&bus_mock)) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | ~PostReporterTest() |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | NiceMock<sdbusplus::SdBusMock> bus_mock; |
| 31 | sdbusplus::bus::bus bus; |
Kun Yi | 2ae207c | 2018-06-11 16:09:46 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 34 | TEST_F(PostReporterTest, EmitsObjectsOnExpectedDbusPath) |
Kun Yi | 2ae207c | 2018-06-11 16:09:46 -0700 | [diff] [blame] | 35 | { |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 36 | |
| 37 | EXPECT_CALL(bus_mock, |
| 38 | sd_bus_emit_object_added(IsNull(), StrEq(SNOOP_OBJECTPATH))) |
| 39 | .WillOnce(Return(0)); |
| 40 | |
| 41 | PostReporter testReporter(bus, SNOOP_OBJECTPATH, true); |
| 42 | testReporter.emit_object_added(); |
Kun Yi | 2ae207c | 2018-06-11 16:09:46 -0700 | [diff] [blame] | 43 | } |
Kun Yi | 35b6c67 | 2018-06-11 20:44:43 -0700 | [diff] [blame] | 44 | |
| 45 | TEST_F(PostReporterTest, AddsObjectWithExpectedName) |
| 46 | { |
| 47 | EXPECT_CALL(bus_mock, |
| 48 | sd_bus_add_object_vtable(IsNull(), _, StrEq(SNOOP_OBJECTPATH), |
| 49 | StrEq(SNOOP_BUSNAME), _, _)) |
| 50 | .WillOnce(Return(0)); |
| 51 | |
| 52 | PostReporter testReporter(bus, SNOOP_OBJECTPATH, true); |
| 53 | } |
| 54 | |
| 55 | TEST_F(PostReporterTest, ValueReadsDefaultToZero) |
| 56 | { |
| 57 | PostReporter testReporter(bus, SNOOP_OBJECTPATH, true); |
| 58 | EXPECT_EQ(0, testReporter.value()); |
| 59 | } |
| 60 | |
| 61 | TEST_F(PostReporterTest, SetValueToPositiveValueWorks) |
| 62 | { |
| 63 | PostReporter testReporter(bus, SNOOP_OBJECTPATH, true); |
| 64 | testReporter.value(65537); |
| 65 | EXPECT_EQ(65537, testReporter.value()); |
| 66 | } |
| 67 | |
| 68 | TEST_F(PostReporterTest, SetValueMultipleTimesWorks) |
| 69 | { |
| 70 | PostReporter testReporter(bus, SNOOP_OBJECTPATH, true); |
| 71 | testReporter.value(123); |
| 72 | EXPECT_EQ(123, testReporter.value()); |
| 73 | testReporter.value(456); |
| 74 | EXPECT_EQ(456, testReporter.value()); |
| 75 | testReporter.value(0); |
| 76 | EXPECT_EQ(0, testReporter.value()); |
| 77 | testReporter.value(456); |
| 78 | EXPECT_EQ(456, testReporter.value()); |
| 79 | testReporter.value(456); |
| 80 | EXPECT_EQ(456, testReporter.value()); |
| 81 | } |
| 82 | |
| 83 | } // namespace |