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/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 class DBusStatisticsTest
: public testing::Test
{
15 DBusStatisticsTest() {
18 virtual void SetUp() OVERRIDE
{
19 statistics::Initialize();
22 virtual void TearDown() OVERRIDE
{
23 statistics::Shutdown();
27 void AddTestMethodCalls() {
28 statistics::AddSentMethodCall(
29 "service1", "service1.interface1", "method1");
30 statistics::AddReceivedSignal(
31 "service1", "service1.interface1", "method1");
32 statistics::AddBlockingSentMethodCall(
33 "service1", "service1.interface1", "method1");
35 statistics::AddSentMethodCall(
36 "service1", "service1.interface1", "method2");
37 statistics::AddSentMethodCall(
38 "service1", "service1.interface1", "method2");
39 statistics::AddReceivedSignal(
40 "service1", "service1.interface1", "method2");
42 statistics::AddSentMethodCall(
43 "service1", "service1.interface1", "method3");
44 statistics::AddSentMethodCall(
45 "service1", "service1.interface1", "method3");
46 statistics::AddSentMethodCall(
47 "service1", "service1.interface1", "method3");
49 statistics::AddSentMethodCall(
50 "service1", "service1.interface2", "method1");
52 statistics::AddSentMethodCall(
53 "service1", "service1.interface2", "method2");
55 statistics::AddSentMethodCall(
56 "service2", "service2.interface1", "method1");
60 DISALLOW_COPY_AND_ASSIGN(DBusStatisticsTest
);
63 TEST_F(DBusStatisticsTest
, TestDBusStatsBasic
) {
64 int sent
= 0, received
= 0, block
= 0;
67 statistics::AddSentMethodCall("service1", "service1.interface1", "method1");
68 ASSERT_TRUE(statistics::testing::GetCalls(
69 "service1", "service1.interface1", "method1", &sent
, &received
, &block
));
71 EXPECT_EQ(0, received
);
74 // Add a received call
75 statistics::AddReceivedSignal("service1", "service1.interface1", "method1");
76 ASSERT_TRUE(statistics::testing::GetCalls(
77 "service1", "service1.interface1", "method1", &sent
, &received
, &block
));
79 EXPECT_EQ(1, received
);
83 statistics::AddBlockingSentMethodCall(
84 "service1", "service1.interface1", "method1");
85 ASSERT_TRUE(statistics::testing::GetCalls(
86 "service1", "service1.interface1", "method1", &sent
, &received
, &block
));
88 EXPECT_EQ(1, received
);
92 TEST_F(DBusStatisticsTest
, TestDBusStatsMulti
) {
93 int sent
= 0, received
= 0, block
= 0;
95 // Add some more stats to exercise accessing multiple different stats.
98 // Make sure all entries can be found in the set and their counts were
99 // incremented correctly.
100 ASSERT_TRUE(statistics::testing::GetCalls(
101 "service1", "service1.interface1", "method1", &sent
, &received
, &block
));
103 EXPECT_EQ(1, received
);
104 ASSERT_TRUE(statistics::testing::GetCalls(
105 "service1", "service1.interface1", "method2", &sent
, &received
, &block
));
107 EXPECT_EQ(1, received
);
108 ASSERT_TRUE(statistics::testing::GetCalls(
109 "service1", "service1.interface1", "method3", &sent
, &received
, &block
));
111 EXPECT_EQ(0, received
);
112 ASSERT_TRUE(statistics::testing::GetCalls(
113 "service1", "service1.interface2", "method1", &sent
, &received
, &block
));
115 EXPECT_EQ(0, received
);
116 ASSERT_TRUE(statistics::testing::GetCalls(
117 "service1", "service1.interface2", "method2", &sent
, &received
, &block
));
119 EXPECT_EQ(0, received
);
120 ASSERT_TRUE(statistics::testing::GetCalls(
121 "service2", "service2.interface1", "method1", &sent
, &received
, &block
));
123 EXPECT_EQ(0, received
);
125 ASSERT_FALSE(statistics::testing::GetCalls(
126 "service1", "service1.interface3", "method2", &sent
, &received
, &block
));
129 TEST_F(DBusStatisticsTest
, TestGetAsString
) {
130 std::string output_none
= GetAsString(statistics::SHOW_SERVICE
,
131 statistics::FORMAT_TOTALS
);
132 EXPECT_EQ("No DBus calls.", output_none
);
134 AddTestMethodCalls();
136 std::string output_service
= GetAsString(statistics::SHOW_SERVICE
,
137 statistics::FORMAT_TOTALS
);
138 const std::string
expected_output_service(
139 "service1: Sent (BLOCKING): 1 Sent: 8 Received: 2\n"
140 "service2: Sent: 1\n");
141 EXPECT_EQ(expected_output_service
, output_service
);
143 std::string output_interface
= GetAsString(statistics::SHOW_INTERFACE
,
144 statistics::FORMAT_TOTALS
);
145 const std::string
expected_output_interface(
146 "service1.interface1: Sent (BLOCKING): 1 Sent: 6 Received: 2\n"
147 "service1.interface2: Sent: 2\n"
148 "service2.interface1: Sent: 1\n");
149 EXPECT_EQ(expected_output_interface
, output_interface
);
151 std::string output_per_minute
= GetAsString(statistics::SHOW_INTERFACE
,
152 statistics::FORMAT_PER_MINUTE
);
153 const std::string
expected_output_per_minute(
154 "service1.interface1: Sent (BLOCKING): 1/min Sent: 6/min"
156 "service1.interface2: Sent: 2/min\n"
157 "service2.interface1: Sent: 1/min\n");
158 EXPECT_EQ(expected_output_per_minute
, output_per_minute
);
160 std::string output_all
= GetAsString(statistics::SHOW_INTERFACE
,
161 statistics::FORMAT_ALL
);
162 const std::string
expected_output_all(
163 "service1.interface1: Sent (BLOCKING): 1 (1/min) Sent: 6 (6/min)"
164 " Received: 2 (2/min)\n"
165 "service1.interface2: Sent: 2 (2/min)\n"
166 "service2.interface1: Sent: 1 (1/min)\n");
167 EXPECT_EQ(expected_output_all
, output_all
);
170 std::string output_method
= GetAsString(statistics::SHOW_METHOD
,
171 statistics::FORMAT_TOTALS
);
172 const std::string
expected_output_method(
173 "service1.interface1.method1: Sent (BLOCKING): 1 Sent: 1 Received: 1\n"
174 "service1.interface1.method2: Sent: 2 Received: 1\n"
175 "service1.interface1.method3: Sent: 3\n"
176 "service1.interface2.method1: Sent: 1\n"
177 "service1.interface2.method2: Sent: 1\n"
178 "service2.interface1.method1: Sent: 1\n");
179 EXPECT_EQ(expected_output_method
, output_method
);