blob: d1a158e74a01261cece04fb66ee031db87d44378 [file] [log] [blame]
Kun Yi35b6c672018-06-11 20:44:43 -07001#include <gmock/gmock.h>
Kun Yi2ae207c2018-06-11 16:09:46 -07002#include <gtest/gtest.h>
Kun Yi35b6c672018-06-11 20:44:43 -07003#include <sdbusplus/bus.hpp>
4#include <sdbusplus/test/sdbus_mock.hpp>
Kun Yi2ae207c2018-06-11 16:09:46 -07005
Kun Yi35b6c672018-06-11 20:44:43 -07006#include "lpcsnoop/snoop.hpp"
7
8using ::testing::IsNull;
9using ::testing::NiceMock;
10using ::testing::Return;
11using ::testing::StrEq;
12using ::testing::_;
13
14namespace
15{
16
17// Fixture for testing class PostReporter
Kun Yi2ae207c2018-06-11 16:09:46 -070018class PostReporterTest : public ::testing::Test
19{
Kun Yi35b6c672018-06-11 20:44:43 -070020 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;
Kun Yi2ae207c2018-06-11 16:09:46 -070031};
32
Kun Yi35b6c672018-06-11 20:44:43 -070033TEST_F(PostReporterTest, EmitsObjectsOnExpectedDbusPath)
Kun Yi2ae207c2018-06-11 16:09:46 -070034{
Kun Yi35b6c672018-06-11 20:44:43 -070035
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();
Kun Yi2ae207c2018-06-11 16:09:46 -070042}
Kun Yi35b6c672018-06-11 20:44:43 -070043
44TEST_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
54TEST_F(PostReporterTest, ValueReadsDefaultToZero)
55{
56 PostReporter testReporter(bus, SNOOP_OBJECTPATH, true);
57 EXPECT_EQ(0, testReporter.value());
58}
59
60TEST_F(PostReporterTest, SetValueToPositiveValueWorks)
61{
62 PostReporter testReporter(bus, SNOOP_OBJECTPATH, true);
63 testReporter.value(65537);
64 EXPECT_EQ(65537, testReporter.value());
65}
66
67TEST_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