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