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. 4 5 #include "dbus/dbus_statistics.h" 6 7 #include "base/basictypes.h" 8 #include "base/compiler_specific.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 namespace dbus { 12 13 class DBusStatisticsTest : public testing::Test { 14 public: 15 DBusStatisticsTest() { 16 } 17 18 virtual void SetUp() OVERRIDE { 19 statistics::Initialize(); 20 } 21 22 virtual void TearDown() OVERRIDE { 23 statistics::Shutdown(); 24 } 25 26 protected: 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"); 34 35 statistics::AddSentMethodCall( 36 "service1", "service1.interface1", "method2"); 37 statistics::AddSentMethodCall( 38 "service1", "service1.interface1", "method2"); 39 statistics::AddReceivedSignal( 40 "service1", "service1.interface1", "method2"); 41 42 statistics::AddSentMethodCall( 43 "service1", "service1.interface1", "method3"); 44 statistics::AddSentMethodCall( 45 "service1", "service1.interface1", "method3"); 46 statistics::AddSentMethodCall( 47 "service1", "service1.interface1", "method3"); 48 49 statistics::AddSentMethodCall( 50 "service1", "service1.interface2", "method1"); 51 52 statistics::AddSentMethodCall( 53 "service1", "service1.interface2", "method2"); 54 55 statistics::AddSentMethodCall( 56 "service2", "service2.interface1", "method1"); 57 } 58 59 private: 60 DISALLOW_COPY_AND_ASSIGN(DBusStatisticsTest); 61 }; 62 63 TEST_F(DBusStatisticsTest, TestDBusStatsBasic) { 64 int sent = 0, received = 0, block = 0; 65 66 // Add a sent call 67 statistics::AddSentMethodCall("service1", "service1.interface1", "method1"); 68 ASSERT_TRUE(statistics::testing::GetCalls( 69 "service1", "service1.interface1", "method1", &sent, &received, &block)); 70 EXPECT_EQ(1, sent); 71 EXPECT_EQ(0, received); 72 EXPECT_EQ(0, block); 73 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)); 78 EXPECT_EQ(1, sent); 79 EXPECT_EQ(1, received); 80 EXPECT_EQ(0, block); 81 82 // Add a block call 83 statistics::AddBlockingSentMethodCall( 84 "service1", "service1.interface1", "method1"); 85 ASSERT_TRUE(statistics::testing::GetCalls( 86 "service1", "service1.interface1", "method1", &sent, &received, &block)); 87 EXPECT_EQ(1, sent); 88 EXPECT_EQ(1, received); 89 EXPECT_EQ(1, block); 90 } 91 92 TEST_F(DBusStatisticsTest, TestDBusStatsMulti) { 93 int sent = 0, received = 0, block = 0; 94 95 // Add some more stats to exercise accessing multiple different stats. 96 AddTestMethodCalls(); 97 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)); 102 EXPECT_EQ(1, sent); 103 EXPECT_EQ(1, received); 104 ASSERT_TRUE(statistics::testing::GetCalls( 105 "service1", "service1.interface1", "method2", &sent, &received, &block)); 106 EXPECT_EQ(2, sent); 107 EXPECT_EQ(1, received); 108 ASSERT_TRUE(statistics::testing::GetCalls( 109 "service1", "service1.interface1", "method3", &sent, &received, &block)); 110 EXPECT_EQ(3, sent); 111 EXPECT_EQ(0, received); 112 ASSERT_TRUE(statistics::testing::GetCalls( 113 "service1", "service1.interface2", "method1", &sent, &received, &block)); 114 EXPECT_EQ(1, sent); 115 EXPECT_EQ(0, received); 116 ASSERT_TRUE(statistics::testing::GetCalls( 117 "service1", "service1.interface2", "method2", &sent, &received, &block)); 118 EXPECT_EQ(1, sent); 119 EXPECT_EQ(0, received); 120 ASSERT_TRUE(statistics::testing::GetCalls( 121 "service2", "service2.interface1", "method1", &sent, &received, &block)); 122 EXPECT_EQ(1, sent); 123 EXPECT_EQ(0, received); 124 125 ASSERT_FALSE(statistics::testing::GetCalls( 126 "service1", "service1.interface3", "method2", &sent, &received, &block)); 127 } 128 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); 133 134 AddTestMethodCalls(); 135 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); 142 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); 150 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" 155 " Received: 2/min\n" 156 "service1.interface2: Sent: 2/min\n" 157 "service2.interface1: Sent: 1/min\n"); 158 EXPECT_EQ(expected_output_per_minute, output_per_minute); 159 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); 168 169 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); 180 181 } 182 183 } // namespace dbus 184