1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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 "base/macros.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 class DBusStatisticsTest : public testing::Test {
15 DBusStatisticsTest() = default;
17 void SetUp() override { statistics::Initialize(); }
19 void TearDown() override { statistics::Shutdown(); }
22 void AddTestMethodCalls() {
23 statistics::AddSentMethodCall(
24 "service1", "service1.interface1", "method1");
25 statistics::AddReceivedSignal(
26 "service1", "service1.interface1", "method1");
27 statistics::AddBlockingSentMethodCall(
28 "service1", "service1.interface1", "method1");
30 statistics::AddSentMethodCall(
31 "service1", "service1.interface1", "method2");
32 statistics::AddSentMethodCall(
33 "service1", "service1.interface1", "method2");
34 statistics::AddReceivedSignal(
35 "service1", "service1.interface1", "method2");
37 statistics::AddSentMethodCall(
38 "service1", "service1.interface1", "method3");
39 statistics::AddSentMethodCall(
40 "service1", "service1.interface1", "method3");
41 statistics::AddSentMethodCall(
42 "service1", "service1.interface1", "method3");
44 statistics::AddSentMethodCall(
45 "service1", "service1.interface2", "method1");
47 statistics::AddSentMethodCall(
48 "service1", "service1.interface2", "method2");
50 statistics::AddSentMethodCall(
51 "service2", "service2.interface1", "method1");
55 DISALLOW_COPY_AND_ASSIGN(DBusStatisticsTest);
58 TEST_F(DBusStatisticsTest, TestDBusStatsBasic) {
59 int sent = 0, received = 0, block = 0;
62 statistics::AddSentMethodCall("service1", "service1.interface1", "method1");
63 ASSERT_TRUE(statistics::testing::GetCalls(
64 "service1", "service1.interface1", "method1", &sent, &received, &block));
66 EXPECT_EQ(0, received);
69 // Add a received call
70 statistics::AddReceivedSignal("service1", "service1.interface1", "method1");
71 ASSERT_TRUE(statistics::testing::GetCalls(
72 "service1", "service1.interface1", "method1", &sent, &received, &block));
74 EXPECT_EQ(1, received);
78 statistics::AddBlockingSentMethodCall(
79 "service1", "service1.interface1", "method1");
80 ASSERT_TRUE(statistics::testing::GetCalls(
81 "service1", "service1.interface1", "method1", &sent, &received, &block));
83 EXPECT_EQ(1, received);
87 TEST_F(DBusStatisticsTest, TestDBusStatsMulti) {
88 int sent = 0, received = 0, block = 0;
90 // Add some more stats to exercise accessing multiple different stats.
93 // Make sure all entries can be found in the set and their counts were
94 // incremented correctly.
95 ASSERT_TRUE(statistics::testing::GetCalls(
96 "service1", "service1.interface1", "method1", &sent, &received, &block));
98 EXPECT_EQ(1, received);
99 ASSERT_TRUE(statistics::testing::GetCalls(
100 "service1", "service1.interface1", "method2", &sent, &received, &block));
102 EXPECT_EQ(1, received);
103 ASSERT_TRUE(statistics::testing::GetCalls(
104 "service1", "service1.interface1", "method3", &sent, &received, &block));
106 EXPECT_EQ(0, received);
107 ASSERT_TRUE(statistics::testing::GetCalls(
108 "service1", "service1.interface2", "method1", &sent, &received, &block));
110 EXPECT_EQ(0, received);
111 ASSERT_TRUE(statistics::testing::GetCalls(
112 "service1", "service1.interface2", "method2", &sent, &received, &block));
114 EXPECT_EQ(0, received);
115 ASSERT_TRUE(statistics::testing::GetCalls(
116 "service2", "service2.interface1", "method1", &sent, &received, &block));
118 EXPECT_EQ(0, received);
120 ASSERT_FALSE(statistics::testing::GetCalls(
121 "service1", "service1.interface3", "method2", &sent, &received, &block));
124 TEST_F(DBusStatisticsTest, TestGetAsString) {
125 std::string output_none = GetAsString(statistics::SHOW_SERVICE,
126 statistics::FORMAT_TOTALS);
127 EXPECT_EQ("No DBus calls.", output_none);
129 AddTestMethodCalls();
131 std::string output_service = GetAsString(statistics::SHOW_SERVICE,
132 statistics::FORMAT_TOTALS);
133 const std::string expected_output_service(
134 "service1: Sent (BLOCKING): 1 Sent: 8 Received: 2\n"
135 "service2: Sent: 1\n");
136 EXPECT_EQ(expected_output_service, output_service);
138 std::string output_interface = GetAsString(statistics::SHOW_INTERFACE,
139 statistics::FORMAT_TOTALS);
140 const std::string expected_output_interface(
141 "service1.interface1: Sent (BLOCKING): 1 Sent: 6 Received: 2\n"
142 "service1.interface2: Sent: 2\n"
143 "service2.interface1: Sent: 1\n");
144 EXPECT_EQ(expected_output_interface, output_interface);
146 std::string output_per_minute = GetAsString(statistics::SHOW_INTERFACE,
147 statistics::FORMAT_PER_MINUTE);
148 const std::string expected_output_per_minute(
149 "service1.interface1: Sent (BLOCKING): 1/min Sent: 6/min"
151 "service1.interface2: Sent: 2/min\n"
152 "service2.interface1: Sent: 1/min\n");
153 EXPECT_EQ(expected_output_per_minute, output_per_minute);
155 std::string output_all = GetAsString(statistics::SHOW_INTERFACE,
156 statistics::FORMAT_ALL);
157 const std::string expected_output_all(
158 "service1.interface1: Sent (BLOCKING): 1 (1/min) Sent: 6 (6/min)"
159 " Received: 2 (2/min)\n"
160 "service1.interface2: Sent: 2 (2/min)\n"
161 "service2.interface1: Sent: 1 (1/min)\n");
162 EXPECT_EQ(expected_output_all, output_all);
165 std::string output_method = GetAsString(statistics::SHOW_METHOD,
166 statistics::FORMAT_TOTALS);
167 const std::string expected_output_method(
168 "service1.interface1.method1: Sent (BLOCKING): 1 Sent: 1 Received: 1\n"
169 "service1.interface1.method2: Sent: 2 Received: 1\n"
170 "service1.interface1.method3: Sent: 3\n"
171 "service1.interface2.method1: Sent: 1\n"
172 "service1.interface2.method2: Sent: 1\n"
173 "service2.interface1.method1: Sent: 1\n");
174 EXPECT_EQ(expected_output_method, output_method);