1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "dbus/dbus_statistics.h"
7 #include "base/compiler_specific.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 class DBusStatisticsTest : public testing::Test {
14 DBusStatisticsTest() = default;
16 DBusStatisticsTest(const DBusStatisticsTest&) = delete;
17 DBusStatisticsTest& operator=(const DBusStatisticsTest&) = delete;
19 void SetUp() override { statistics::Initialize(); }
21 void TearDown() override { statistics::Shutdown(); }
24 void AddTestMethodCalls() {
25 statistics::AddSentMethodCall(
26 "service1", "service1.interface1", "method1");
27 statistics::AddReceivedSignal(
28 "service1", "service1.interface1", "method1");
29 statistics::AddBlockingSentMethodCall(
30 "service1", "service1.interface1", "method1");
32 statistics::AddSentMethodCall(
33 "service1", "service1.interface1", "method2");
34 statistics::AddSentMethodCall(
35 "service1", "service1.interface1", "method2");
36 statistics::AddReceivedSignal(
37 "service1", "service1.interface1", "method2");
39 statistics::AddSentMethodCall(
40 "service1", "service1.interface1", "method3");
41 statistics::AddSentMethodCall(
42 "service1", "service1.interface1", "method3");
43 statistics::AddSentMethodCall(
44 "service1", "service1.interface1", "method3");
46 statistics::AddSentMethodCall(
47 "service1", "service1.interface2", "method1");
49 statistics::AddSentMethodCall(
50 "service1", "service1.interface2", "method2");
52 statistics::AddSentMethodCall(
53 "service2", "service2.interface1", "method1");
57 TEST_F(DBusStatisticsTest, TestDBusStatsBasic) {
58 int sent = 0, received = 0, block = 0;
61 statistics::AddSentMethodCall("service1", "service1.interface1", "method1");
62 ASSERT_TRUE(statistics::testing::GetCalls(
63 "service1", "service1.interface1", "method1", &sent, &received, &block));
65 EXPECT_EQ(0, received);
68 // Add a received call
69 statistics::AddReceivedSignal("service1", "service1.interface1", "method1");
70 ASSERT_TRUE(statistics::testing::GetCalls(
71 "service1", "service1.interface1", "method1", &sent, &received, &block));
73 EXPECT_EQ(1, received);
77 statistics::AddBlockingSentMethodCall(
78 "service1", "service1.interface1", "method1");
79 ASSERT_TRUE(statistics::testing::GetCalls(
80 "service1", "service1.interface1", "method1", &sent, &received, &block));
82 EXPECT_EQ(1, received);
86 TEST_F(DBusStatisticsTest, TestDBusStatsMulti) {
87 int sent = 0, received = 0, block = 0;
89 // Add some more stats to exercise accessing multiple different stats.
92 // Make sure all entries can be found in the set and their counts were
93 // incremented correctly.
94 ASSERT_TRUE(statistics::testing::GetCalls(
95 "service1", "service1.interface1", "method1", &sent, &received, &block));
97 EXPECT_EQ(1, received);
98 ASSERT_TRUE(statistics::testing::GetCalls(
99 "service1", "service1.interface1", "method2", &sent, &received, &block));
101 EXPECT_EQ(1, received);
102 ASSERT_TRUE(statistics::testing::GetCalls(
103 "service1", "service1.interface1", "method3", &sent, &received, &block));
105 EXPECT_EQ(0, received);
106 ASSERT_TRUE(statistics::testing::GetCalls(
107 "service1", "service1.interface2", "method1", &sent, &received, &block));
109 EXPECT_EQ(0, received);
110 ASSERT_TRUE(statistics::testing::GetCalls(
111 "service1", "service1.interface2", "method2", &sent, &received, &block));
113 EXPECT_EQ(0, received);
114 ASSERT_TRUE(statistics::testing::GetCalls(
115 "service2", "service2.interface1", "method1", &sent, &received, &block));
117 EXPECT_EQ(0, received);
119 ASSERT_FALSE(statistics::testing::GetCalls(
120 "service1", "service1.interface3", "method2", &sent, &received, &block));
123 TEST_F(DBusStatisticsTest, TestGetAsString) {
124 std::string output_none = GetAsString(statistics::SHOW_SERVICE,
125 statistics::FORMAT_TOTALS);
126 EXPECT_EQ("No DBus calls.", output_none);
128 AddTestMethodCalls();
130 std::string output_service = GetAsString(statistics::SHOW_SERVICE,
131 statistics::FORMAT_TOTALS);
132 const std::string expected_output_service(
133 "service1: Sent (BLOCKING): 1 Sent: 8 Received: 2\n"
134 "service2: Sent: 1\n");
135 EXPECT_EQ(expected_output_service, output_service);
137 std::string output_interface = GetAsString(statistics::SHOW_INTERFACE,
138 statistics::FORMAT_TOTALS);
139 const std::string expected_output_interface(
140 "service1.interface1: Sent (BLOCKING): 1 Sent: 6 Received: 2\n"
141 "service1.interface2: Sent: 2\n"
142 "service2.interface1: Sent: 1\n");
143 EXPECT_EQ(expected_output_interface, output_interface);
145 std::string output_per_minute = GetAsString(statistics::SHOW_INTERFACE,
146 statistics::FORMAT_PER_MINUTE);
147 const std::string expected_output_per_minute(
148 "service1.interface1: Sent (BLOCKING): 1/min Sent: 6/min"
150 "service1.interface2: Sent: 2/min\n"
151 "service2.interface1: Sent: 1/min\n");
152 EXPECT_EQ(expected_output_per_minute, output_per_minute);
154 std::string output_all = GetAsString(statistics::SHOW_INTERFACE,
155 statistics::FORMAT_ALL);
156 const std::string expected_output_all(
157 "service1.interface1: Sent (BLOCKING): 1 (1/min) Sent: 6 (6/min)"
158 " Received: 2 (2/min)\n"
159 "service1.interface2: Sent: 2 (2/min)\n"
160 "service2.interface1: Sent: 1 (1/min)\n");
161 EXPECT_EQ(expected_output_all, output_all);
164 std::string output_method = GetAsString(statistics::SHOW_METHOD,
165 statistics::FORMAT_TOTALS);
166 const std::string expected_output_method(
167 "service1.interface1.method1: Sent (BLOCKING): 1 Sent: 1 Received: 1\n"
168 "service1.interface1.method2: Sent: 2 Received: 1\n"
169 "service1.interface1.method3: Sent: 3\n"
170 "service1.interface2.method1: Sent: 1\n"
171 "service1.interface2.method2: Sent: 1\n"
172 "service2.interface1.method1: Sent: 1\n");
173 EXPECT_EQ(expected_output_method, output_method);