Roll android_tools repo.
[chromium-blink-merge.git] / dbus / dbus_statistics_unittest.cc
blob6260a63ebe93a8f10baf5f4fac590a2336543509
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"
11 namespace dbus {
13 class DBusStatisticsTest : public testing::Test {
14 public:
15 DBusStatisticsTest() {
18 void SetUp() override { statistics::Initialize(); }
20 void TearDown() override { statistics::Shutdown(); }
22 protected:
23 void AddTestMethodCalls() {
24 statistics::AddSentMethodCall(
25 "service1", "service1.interface1", "method1");
26 statistics::AddReceivedSignal(
27 "service1", "service1.interface1", "method1");
28 statistics::AddBlockingSentMethodCall(
29 "service1", "service1.interface1", "method1");
31 statistics::AddSentMethodCall(
32 "service1", "service1.interface1", "method2");
33 statistics::AddSentMethodCall(
34 "service1", "service1.interface1", "method2");
35 statistics::AddReceivedSignal(
36 "service1", "service1.interface1", "method2");
38 statistics::AddSentMethodCall(
39 "service1", "service1.interface1", "method3");
40 statistics::AddSentMethodCall(
41 "service1", "service1.interface1", "method3");
42 statistics::AddSentMethodCall(
43 "service1", "service1.interface1", "method3");
45 statistics::AddSentMethodCall(
46 "service1", "service1.interface2", "method1");
48 statistics::AddSentMethodCall(
49 "service1", "service1.interface2", "method2");
51 statistics::AddSentMethodCall(
52 "service2", "service2.interface1", "method1");
55 private:
56 DISALLOW_COPY_AND_ASSIGN(DBusStatisticsTest);
59 TEST_F(DBusStatisticsTest, TestDBusStatsBasic) {
60 int sent = 0, received = 0, block = 0;
62 // Add a sent call
63 statistics::AddSentMethodCall("service1", "service1.interface1", "method1");
64 ASSERT_TRUE(statistics::testing::GetCalls(
65 "service1", "service1.interface1", "method1", &sent, &received, &block));
66 EXPECT_EQ(1, sent);
67 EXPECT_EQ(0, received);
68 EXPECT_EQ(0, block);
70 // Add a received call
71 statistics::AddReceivedSignal("service1", "service1.interface1", "method1");
72 ASSERT_TRUE(statistics::testing::GetCalls(
73 "service1", "service1.interface1", "method1", &sent, &received, &block));
74 EXPECT_EQ(1, sent);
75 EXPECT_EQ(1, received);
76 EXPECT_EQ(0, block);
78 // Add a block call
79 statistics::AddBlockingSentMethodCall(
80 "service1", "service1.interface1", "method1");
81 ASSERT_TRUE(statistics::testing::GetCalls(
82 "service1", "service1.interface1", "method1", &sent, &received, &block));
83 EXPECT_EQ(1, sent);
84 EXPECT_EQ(1, received);
85 EXPECT_EQ(1, block);
88 TEST_F(DBusStatisticsTest, TestDBusStatsMulti) {
89 int sent = 0, received = 0, block = 0;
91 // Add some more stats to exercise accessing multiple different stats.
92 AddTestMethodCalls();
94 // Make sure all entries can be found in the set and their counts were
95 // incremented correctly.
96 ASSERT_TRUE(statistics::testing::GetCalls(
97 "service1", "service1.interface1", "method1", &sent, &received, &block));
98 EXPECT_EQ(1, sent);
99 EXPECT_EQ(1, received);
100 ASSERT_TRUE(statistics::testing::GetCalls(
101 "service1", "service1.interface1", "method2", &sent, &received, &block));
102 EXPECT_EQ(2, sent);
103 EXPECT_EQ(1, received);
104 ASSERT_TRUE(statistics::testing::GetCalls(
105 "service1", "service1.interface1", "method3", &sent, &received, &block));
106 EXPECT_EQ(3, sent);
107 EXPECT_EQ(0, received);
108 ASSERT_TRUE(statistics::testing::GetCalls(
109 "service1", "service1.interface2", "method1", &sent, &received, &block));
110 EXPECT_EQ(1, sent);
111 EXPECT_EQ(0, received);
112 ASSERT_TRUE(statistics::testing::GetCalls(
113 "service1", "service1.interface2", "method2", &sent, &received, &block));
114 EXPECT_EQ(1, sent);
115 EXPECT_EQ(0, received);
116 ASSERT_TRUE(statistics::testing::GetCalls(
117 "service2", "service2.interface1", "method1", &sent, &received, &block));
118 EXPECT_EQ(1, sent);
119 EXPECT_EQ(0, received);
121 ASSERT_FALSE(statistics::testing::GetCalls(
122 "service1", "service1.interface3", "method2", &sent, &received, &block));
125 TEST_F(DBusStatisticsTest, TestGetAsString) {
126 std::string output_none = GetAsString(statistics::SHOW_SERVICE,
127 statistics::FORMAT_TOTALS);
128 EXPECT_EQ("No DBus calls.", output_none);
130 AddTestMethodCalls();
132 std::string output_service = GetAsString(statistics::SHOW_SERVICE,
133 statistics::FORMAT_TOTALS);
134 const std::string expected_output_service(
135 "service1: Sent (BLOCKING): 1 Sent: 8 Received: 2\n"
136 "service2: Sent: 1\n");
137 EXPECT_EQ(expected_output_service, output_service);
139 std::string output_interface = GetAsString(statistics::SHOW_INTERFACE,
140 statistics::FORMAT_TOTALS);
141 const std::string expected_output_interface(
142 "service1.interface1: Sent (BLOCKING): 1 Sent: 6 Received: 2\n"
143 "service1.interface2: Sent: 2\n"
144 "service2.interface1: Sent: 1\n");
145 EXPECT_EQ(expected_output_interface, output_interface);
147 std::string output_per_minute = GetAsString(statistics::SHOW_INTERFACE,
148 statistics::FORMAT_PER_MINUTE);
149 const std::string expected_output_per_minute(
150 "service1.interface1: Sent (BLOCKING): 1/min Sent: 6/min"
151 " Received: 2/min\n"
152 "service1.interface2: Sent: 2/min\n"
153 "service2.interface1: Sent: 1/min\n");
154 EXPECT_EQ(expected_output_per_minute, output_per_minute);
156 std::string output_all = GetAsString(statistics::SHOW_INTERFACE,
157 statistics::FORMAT_ALL);
158 const std::string expected_output_all(
159 "service1.interface1: Sent (BLOCKING): 1 (1/min) Sent: 6 (6/min)"
160 " Received: 2 (2/min)\n"
161 "service1.interface2: Sent: 2 (2/min)\n"
162 "service2.interface1: Sent: 1 (1/min)\n");
163 EXPECT_EQ(expected_output_all, output_all);
166 std::string output_method = GetAsString(statistics::SHOW_METHOD,
167 statistics::FORMAT_TOTALS);
168 const std::string expected_output_method(
169 "service1.interface1.method1: Sent (BLOCKING): 1 Sent: 1 Received: 1\n"
170 "service1.interface1.method2: Sent: 2 Received: 1\n"
171 "service1.interface1.method3: Sent: 3\n"
172 "service1.interface2.method1: Sent: 1\n"
173 "service1.interface2.method2: Sent: 1\n"
174 "service2.interface1.method1: Sent: 1\n");
175 EXPECT_EQ(expected_output_method, output_method);
179 } // namespace dbus