[email protected] | 20bed01 | 2012-02-10 21:45:23 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 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/bus.h" |
| 6 | |
[email protected] | e20d39fe | 2011-09-02 06:56:23 | [diff] [blame] | 7 | #include "base/bind.h" |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 8 | #include "base/macros.h" |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 9 | #include "base/memory/ref_counted.h" |
[email protected] | 2a9ec0e | 2013-07-17 23:00:30 | [diff] [blame] | 10 | #include "base/message_loop/message_loop.h" |
[email protected] | 049616e | 2013-06-10 22:52:34 | [diff] [blame] | 11 | #include "base/run_loop.h" |
[email protected] | e20d39fe | 2011-09-02 06:56:23 | [diff] [blame] | 12 | #include "base/threading/thread.h" |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 13 | #include "dbus/exported_object.h" |
[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 14 | #include "dbus/object_path.h" |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 15 | #include "dbus/object_proxy.h" |
[email protected] | bae0f88 | 2013-01-31 06:08:02 | [diff] [blame] | 16 | #include "dbus/scoped_dbus_error.h" |
[email protected] | 049616e | 2013-06-10 22:52:34 | [diff] [blame] | 17 | #include "dbus/test_service.h" |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 18 | |
| 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 21 | namespace dbus { |
| 22 | |
[email protected] | 12e2599 | 2011-10-06 00:20:53 | [diff] [blame] | 23 | namespace { |
| 24 | |
[email protected] | 049616e | 2013-06-10 22:52:34 | [diff] [blame] | 25 | // Test helper for BusTest.ListenForServiceOwnerChange that wraps a |
| 26 | // base::RunLoop. At Run() time, the caller pass in the expected number of |
| 27 | // quit calls, and at QuitIfConditionIsSatisified() time, only quit the RunLoop |
| 28 | // if the expected number of quit calls have been reached. |
| 29 | class RunLoopWithExpectedCount { |
| 30 | public: |
| 31 | RunLoopWithExpectedCount() : expected_quit_calls_(0), actual_quit_calls_(0) {} |
| 32 | ~RunLoopWithExpectedCount() {} |
| 33 | |
| 34 | void Run(int expected_quit_calls) { |
| 35 | DCHECK_EQ(0, expected_quit_calls_); |
| 36 | DCHECK_EQ(0, actual_quit_calls_); |
| 37 | expected_quit_calls_ = expected_quit_calls; |
| 38 | run_loop_.reset(new base::RunLoop()); |
| 39 | run_loop_->Run(); |
| 40 | } |
| 41 | |
| 42 | void QuitIfConditionIsSatisified() { |
| 43 | if (++actual_quit_calls_ != expected_quit_calls_) |
| 44 | return; |
| 45 | run_loop_->Quit(); |
| 46 | expected_quit_calls_ = 0; |
| 47 | actual_quit_calls_ = 0; |
| 48 | } |
| 49 | |
| 50 | private: |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 51 | std::unique_ptr<base::RunLoop> run_loop_; |
[email protected] | 049616e | 2013-06-10 22:52:34 | [diff] [blame] | 52 | int expected_quit_calls_; |
| 53 | int actual_quit_calls_; |
| 54 | |
| 55 | DISALLOW_COPY_AND_ASSIGN(RunLoopWithExpectedCount); |
| 56 | }; |
| 57 | |
| 58 | // Test helper for BusTest.ListenForServiceOwnerChange. |
| 59 | void OnServiceOwnerChanged(RunLoopWithExpectedCount* run_loop_state, |
| 60 | std::string* service_owner, |
| 61 | int* num_of_owner_changes, |
| 62 | const std::string& new_service_owner) { |
| 63 | *service_owner = new_service_owner; |
| 64 | ++(*num_of_owner_changes); |
| 65 | run_loop_state->QuitIfConditionIsSatisified(); |
| 66 | } |
| 67 | |
[email protected] | 12e2599 | 2011-10-06 00:20:53 | [diff] [blame] | 68 | } // namespace |
| 69 | |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 70 | TEST(BusTest, GetObjectProxy) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 71 | Bus::Options options; |
| 72 | scoped_refptr<Bus> bus = new Bus(options); |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 73 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 74 | ObjectProxy* object_proxy1 = |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 75 | bus->GetObjectProxy("org.chromium.TestService", |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 76 | ObjectPath("/org/chromium/TestObject")); |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 77 | ASSERT_TRUE(object_proxy1); |
| 78 | |
| 79 | // This should return the same object. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 80 | ObjectProxy* object_proxy2 = |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 81 | bus->GetObjectProxy("org.chromium.TestService", |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 82 | ObjectPath("/org/chromium/TestObject")); |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 83 | ASSERT_TRUE(object_proxy2); |
| 84 | EXPECT_EQ(object_proxy1, object_proxy2); |
| 85 | |
| 86 | // This should not. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 87 | ObjectProxy* object_proxy3 = |
[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 88 | bus->GetObjectProxy( |
| 89 | "org.chromium.TestService", |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 90 | ObjectPath("/org/chromium/DifferentTestObject")); |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 91 | ASSERT_TRUE(object_proxy3); |
| 92 | EXPECT_NE(object_proxy1, object_proxy3); |
[email protected] | 6477a41 | 2011-10-13 00:45:26 | [diff] [blame] | 93 | |
| 94 | bus->ShutdownAndBlock(); |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | 20bed01 | 2012-02-10 21:45:23 | [diff] [blame] | 97 | TEST(BusTest, GetObjectProxyIgnoreUnknownService) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 98 | Bus::Options options; |
| 99 | scoped_refptr<Bus> bus = new Bus(options); |
[email protected] | 20bed01 | 2012-02-10 21:45:23 | [diff] [blame] | 100 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 101 | ObjectProxy* object_proxy1 = |
[email protected] | 20bed01 | 2012-02-10 21:45:23 | [diff] [blame] | 102 | bus->GetObjectProxyWithOptions( |
| 103 | "org.chromium.TestService", |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 104 | ObjectPath("/org/chromium/TestObject"), |
| 105 | ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS); |
[email protected] | 20bed01 | 2012-02-10 21:45:23 | [diff] [blame] | 106 | ASSERT_TRUE(object_proxy1); |
| 107 | |
| 108 | // This should return the same object. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 109 | ObjectProxy* object_proxy2 = |
[email protected] | 20bed01 | 2012-02-10 21:45:23 | [diff] [blame] | 110 | bus->GetObjectProxyWithOptions( |
| 111 | "org.chromium.TestService", |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 112 | ObjectPath("/org/chromium/TestObject"), |
| 113 | ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS); |
[email protected] | 20bed01 | 2012-02-10 21:45:23 | [diff] [blame] | 114 | ASSERT_TRUE(object_proxy2); |
| 115 | EXPECT_EQ(object_proxy1, object_proxy2); |
| 116 | |
| 117 | // This should not. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 118 | ObjectProxy* object_proxy3 = |
[email protected] | 20bed01 | 2012-02-10 21:45:23 | [diff] [blame] | 119 | bus->GetObjectProxyWithOptions( |
| 120 | "org.chromium.TestService", |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 121 | ObjectPath("/org/chromium/DifferentTestObject"), |
| 122 | ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS); |
[email protected] | 20bed01 | 2012-02-10 21:45:23 | [diff] [blame] | 123 | ASSERT_TRUE(object_proxy3); |
| 124 | EXPECT_NE(object_proxy1, object_proxy3); |
| 125 | |
| 126 | bus->ShutdownAndBlock(); |
| 127 | } |
| 128 | |
[email protected] | 38ecdc8 | 2013-01-29 20:29:12 | [diff] [blame] | 129 | TEST(BusTest, RemoveObjectProxy) { |
| 130 | // Setup the current thread's MessageLoop. |
[email protected] | ff33b18e | 2013-05-01 16:10:30 | [diff] [blame] | 131 | base::MessageLoop message_loop; |
[email protected] | 38ecdc8 | 2013-01-29 20:29:12 | [diff] [blame] | 132 | |
| 133 | // Start the D-Bus thread. |
| 134 | base::Thread::Options thread_options; |
[email protected] | ff33b18e | 2013-05-01 16:10:30 | [diff] [blame] | 135 | thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
[email protected] | 38ecdc8 | 2013-01-29 20:29:12 | [diff] [blame] | 136 | base::Thread dbus_thread("D-Bus thread"); |
| 137 | dbus_thread.StartWithOptions(thread_options); |
| 138 | |
| 139 | // Create the bus. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 140 | Bus::Options options; |
skyostil | 8a033aa | 2015-06-17 15:46:04 | [diff] [blame] | 141 | options.dbus_task_runner = dbus_thread.task_runner(); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 142 | scoped_refptr<Bus> bus = new Bus(options); |
[email protected] | 38ecdc8 | 2013-01-29 20:29:12 | [diff] [blame] | 143 | ASSERT_FALSE(bus->shutdown_completed()); |
| 144 | |
| 145 | // Try to remove a non existant object proxy should return false. |
| 146 | ASSERT_FALSE( |
| 147 | bus->RemoveObjectProxy("org.chromium.TestService", |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 148 | ObjectPath("/org/chromium/TestObject"), |
[email protected] | 38ecdc8 | 2013-01-29 20:29:12 | [diff] [blame] | 149 | base::Bind(&base::DoNothing))); |
| 150 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 151 | ObjectProxy* object_proxy1 = |
[email protected] | 38ecdc8 | 2013-01-29 20:29:12 | [diff] [blame] | 152 | bus->GetObjectProxy("org.chromium.TestService", |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 153 | ObjectPath("/org/chromium/TestObject")); |
[email protected] | 38ecdc8 | 2013-01-29 20:29:12 | [diff] [blame] | 154 | ASSERT_TRUE(object_proxy1); |
| 155 | |
| 156 | // Increment the reference count to the object proxy to avoid destroying it |
| 157 | // while removing the object. |
| 158 | object_proxy1->AddRef(); |
| 159 | |
| 160 | // Remove the object from the bus. This will invalidate any other usage of |
| 161 | // object_proxy1 other than destroy it. We keep this object for a comparison |
| 162 | // at a later time. |
| 163 | ASSERT_TRUE( |
| 164 | bus->RemoveObjectProxy("org.chromium.TestService", |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 165 | ObjectPath("/org/chromium/TestObject"), |
[email protected] | 38ecdc8 | 2013-01-29 20:29:12 | [diff] [blame] | 166 | base::Bind(&base::DoNothing))); |
| 167 | |
| 168 | // This should return a different object because the first object was removed |
| 169 | // from the bus, but not deleted from memory. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 170 | ObjectProxy* object_proxy2 = |
[email protected] | 38ecdc8 | 2013-01-29 20:29:12 | [diff] [blame] | 171 | bus->GetObjectProxy("org.chromium.TestService", |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 172 | ObjectPath("/org/chromium/TestObject")); |
[email protected] | 38ecdc8 | 2013-01-29 20:29:12 | [diff] [blame] | 173 | ASSERT_TRUE(object_proxy2); |
| 174 | |
| 175 | // Compare the new object with the first object. The first object still exists |
| 176 | // thanks to the increased reference. |
| 177 | EXPECT_NE(object_proxy1, object_proxy2); |
| 178 | |
| 179 | // Release object_proxy1. |
| 180 | object_proxy1->Release(); |
| 181 | |
| 182 | // Shut down synchronously. |
| 183 | bus->ShutdownOnDBusThreadAndBlock(); |
| 184 | EXPECT_TRUE(bus->shutdown_completed()); |
| 185 | dbus_thread.Stop(); |
| 186 | } |
| 187 | |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 188 | TEST(BusTest, GetExportedObject) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 189 | Bus::Options options; |
| 190 | scoped_refptr<Bus> bus = new Bus(options); |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 191 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 192 | ExportedObject* object_proxy1 = |
| 193 | bus->GetExportedObject(ObjectPath("/org/chromium/TestObject")); |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 194 | ASSERT_TRUE(object_proxy1); |
| 195 | |
| 196 | // This should return the same object. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 197 | ExportedObject* object_proxy2 = |
| 198 | bus->GetExportedObject(ObjectPath("/org/chromium/TestObject")); |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 199 | ASSERT_TRUE(object_proxy2); |
| 200 | EXPECT_EQ(object_proxy1, object_proxy2); |
| 201 | |
| 202 | // This should not. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 203 | ExportedObject* object_proxy3 = |
[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 204 | bus->GetExportedObject( |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 205 | ObjectPath("/org/chromium/DifferentTestObject")); |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 206 | ASSERT_TRUE(object_proxy3); |
| 207 | EXPECT_NE(object_proxy1, object_proxy3); |
[email protected] | 6477a41 | 2011-10-13 00:45:26 | [diff] [blame] | 208 | |
| 209 | bus->ShutdownAndBlock(); |
[email protected] | 2ef498f | 2011-08-23 19:25:20 | [diff] [blame] | 210 | } |
[email protected] | e20d39fe | 2011-09-02 06:56:23 | [diff] [blame] | 211 | |
[email protected] | bda5735 | 2013-01-24 00:58:35 | [diff] [blame] | 212 | TEST(BusTest, UnregisterExportedObject) { |
[email protected] | d7361fdc | 2012-03-14 01:18:35 | [diff] [blame] | 213 | // Start the D-Bus thread. |
| 214 | base::Thread::Options thread_options; |
[email protected] | ff33b18e | 2013-05-01 16:10:30 | [diff] [blame] | 215 | thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
[email protected] | d7361fdc | 2012-03-14 01:18:35 | [diff] [blame] | 216 | base::Thread dbus_thread("D-Bus thread"); |
| 217 | dbus_thread.StartWithOptions(thread_options); |
| 218 | |
| 219 | // Create the bus. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 220 | Bus::Options options; |
skyostil | 8a033aa | 2015-06-17 15:46:04 | [diff] [blame] | 221 | options.dbus_task_runner = dbus_thread.task_runner(); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 222 | scoped_refptr<Bus> bus = new Bus(options); |
[email protected] | d7361fdc | 2012-03-14 01:18:35 | [diff] [blame] | 223 | ASSERT_FALSE(bus->shutdown_completed()); |
| 224 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 225 | ExportedObject* object_proxy1 = |
| 226 | bus->GetExportedObject(ObjectPath("/org/chromium/TestObject")); |
[email protected] | d7361fdc | 2012-03-14 01:18:35 | [diff] [blame] | 227 | ASSERT_TRUE(object_proxy1); |
| 228 | |
[email protected] | bda5735 | 2013-01-24 00:58:35 | [diff] [blame] | 229 | // Increment the reference count to the object proxy to avoid destroying it |
| 230 | // calling UnregisterExportedObject. This ensures the dbus::ExportedObject is |
| 231 | // not freed from memory. See http://crbug.com/137846 for details. |
| 232 | object_proxy1->AddRef(); |
| 233 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 234 | bus->UnregisterExportedObject(ObjectPath("/org/chromium/TestObject")); |
[email protected] | d7361fdc | 2012-03-14 01:18:35 | [diff] [blame] | 235 | |
[email protected] | bda5735 | 2013-01-24 00:58:35 | [diff] [blame] | 236 | // This should return a new object because the object_proxy1 is still in |
| 237 | // alloc'ed memory. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 238 | ExportedObject* object_proxy2 = |
| 239 | bus->GetExportedObject(ObjectPath("/org/chromium/TestObject")); |
[email protected] | d7361fdc | 2012-03-14 01:18:35 | [diff] [blame] | 240 | ASSERT_TRUE(object_proxy2); |
| 241 | EXPECT_NE(object_proxy1, object_proxy2); |
| 242 | |
[email protected] | bda5735 | 2013-01-24 00:58:35 | [diff] [blame] | 243 | // Release the incremented reference. |
| 244 | object_proxy1->Release(); |
| 245 | |
[email protected] | d7361fdc | 2012-03-14 01:18:35 | [diff] [blame] | 246 | // Shut down synchronously. |
| 247 | bus->ShutdownOnDBusThreadAndBlock(); |
| 248 | EXPECT_TRUE(bus->shutdown_completed()); |
| 249 | dbus_thread.Stop(); |
| 250 | } |
| 251 | |
[email protected] | e20d39fe | 2011-09-02 06:56:23 | [diff] [blame] | 252 | TEST(BusTest, ShutdownAndBlock) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 253 | Bus::Options options; |
| 254 | scoped_refptr<Bus> bus = new Bus(options); |
[email protected] | e20d39fe | 2011-09-02 06:56:23 | [diff] [blame] | 255 | ASSERT_FALSE(bus->shutdown_completed()); |
| 256 | |
| 257 | // Shut down synchronously. |
| 258 | bus->ShutdownAndBlock(); |
| 259 | EXPECT_TRUE(bus->shutdown_completed()); |
| 260 | } |
| 261 | |
| 262 | TEST(BusTest, ShutdownAndBlockWithDBusThread) { |
| 263 | // Start the D-Bus thread. |
| 264 | base::Thread::Options thread_options; |
[email protected] | ff33b18e | 2013-05-01 16:10:30 | [diff] [blame] | 265 | thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
[email protected] | e20d39fe | 2011-09-02 06:56:23 | [diff] [blame] | 266 | base::Thread dbus_thread("D-Bus thread"); |
| 267 | dbus_thread.StartWithOptions(thread_options); |
| 268 | |
| 269 | // Create the bus. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 270 | Bus::Options options; |
skyostil | 8a033aa | 2015-06-17 15:46:04 | [diff] [blame] | 271 | options.dbus_task_runner = dbus_thread.task_runner(); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 272 | scoped_refptr<Bus> bus = new Bus(options); |
[email protected] | e20d39fe | 2011-09-02 06:56:23 | [diff] [blame] | 273 | ASSERT_FALSE(bus->shutdown_completed()); |
| 274 | |
| 275 | // Shut down synchronously. |
| 276 | bus->ShutdownOnDBusThreadAndBlock(); |
| 277 | EXPECT_TRUE(bus->shutdown_completed()); |
| 278 | dbus_thread.Stop(); |
| 279 | } |
[email protected] | 12e2599 | 2011-10-06 00:20:53 | [diff] [blame] | 280 | |
[email protected] | bae0f88 | 2013-01-31 06:08:02 | [diff] [blame] | 281 | TEST(BusTest, DoubleAddAndRemoveMatch) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 282 | Bus::Options options; |
| 283 | scoped_refptr<Bus> bus = new Bus(options); |
| 284 | ScopedDBusError error; |
[email protected] | bae0f88 | 2013-01-31 06:08:02 | [diff] [blame] | 285 | |
| 286 | bus->Connect(); |
| 287 | |
| 288 | // Adds the same rule twice. |
| 289 | bus->AddMatch( |
| 290 | "type='signal',interface='org.chromium.TestService',path='/'", |
| 291 | error.get()); |
| 292 | ASSERT_FALSE(error.is_set()); |
| 293 | |
| 294 | bus->AddMatch( |
| 295 | "type='signal',interface='org.chromium.TestService',path='/'", |
| 296 | error.get()); |
| 297 | ASSERT_FALSE(error.is_set()); |
| 298 | |
| 299 | // Removes the same rule twice. |
| 300 | ASSERT_TRUE(bus->RemoveMatch( |
| 301 | "type='signal',interface='org.chromium.TestService',path='/'", |
| 302 | error.get())); |
| 303 | ASSERT_FALSE(error.is_set()); |
| 304 | |
| 305 | // The rule should be still in the bus since it was removed only once. |
| 306 | // A second removal shouldn't give an error. |
| 307 | ASSERT_TRUE(bus->RemoveMatch( |
| 308 | "type='signal',interface='org.chromium.TestService',path='/'", |
| 309 | error.get())); |
| 310 | ASSERT_FALSE(error.is_set()); |
| 311 | |
| 312 | // A third attemp to remove the same rule should fail. |
| 313 | ASSERT_FALSE(bus->RemoveMatch( |
| 314 | "type='signal',interface='org.chromium.TestService',path='/'", |
| 315 | error.get())); |
| 316 | |
| 317 | bus->ShutdownAndBlock(); |
| 318 | } |
[email protected] | 049616e | 2013-06-10 22:52:34 | [diff] [blame] | 319 | |
| 320 | TEST(BusTest, ListenForServiceOwnerChange) { |
| 321 | // Setup the current thread's MessageLoop. Must be of TYPE_IO for the |
| 322 | // listeners to work. |
| 323 | base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 324 | RunLoopWithExpectedCount run_loop_state; |
| 325 | |
| 326 | // Create the bus. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 327 | Bus::Options bus_options; |
| 328 | scoped_refptr<Bus> bus = new Bus(bus_options); |
[email protected] | 049616e | 2013-06-10 22:52:34 | [diff] [blame] | 329 | |
| 330 | // Add a listener. |
| 331 | std::string service_owner1; |
| 332 | int num_of_owner_changes1 = 0; |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 333 | Bus::GetServiceOwnerCallback callback1 = |
[email protected] | 049616e | 2013-06-10 22:52:34 | [diff] [blame] | 334 | base::Bind(&OnServiceOwnerChanged, |
| 335 | &run_loop_state, |
| 336 | &service_owner1, |
| 337 | &num_of_owner_changes1); |
| 338 | bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1); |
| 339 | // This should be a no-op. |
| 340 | bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1); |
| 341 | base::RunLoop().RunUntilIdle(); |
| 342 | |
| 343 | // Nothing has happened yet. Check initial state. |
| 344 | EXPECT_TRUE(service_owner1.empty()); |
| 345 | EXPECT_EQ(0, num_of_owner_changes1); |
| 346 | |
| 347 | // Make an ownership change. |
[email protected] | e282490 | 2013-07-31 06:34:59 | [diff] [blame] | 348 | ASSERT_TRUE(bus->RequestOwnershipAndBlock("org.chromium.TestService", |
| 349 | Bus::REQUIRE_PRIMARY)); |
[email protected] | 049616e | 2013-06-10 22:52:34 | [diff] [blame] | 350 | run_loop_state.Run(1); |
| 351 | |
| 352 | { |
| 353 | // Get the current service owner and check to make sure the listener got |
| 354 | // the right value. |
| 355 | std::string current_service_owner = |
| 356 | bus->GetServiceOwnerAndBlock("org.chromium.TestService", |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 357 | Bus::REPORT_ERRORS); |
[email protected] | 049616e | 2013-06-10 22:52:34 | [diff] [blame] | 358 | ASSERT_FALSE(current_service_owner.empty()); |
| 359 | |
| 360 | // Make sure the listener heard about the new owner. |
| 361 | EXPECT_EQ(current_service_owner, service_owner1); |
| 362 | |
| 363 | // Test the second ListenForServiceOwnerChange() above is indeed a no-op. |
| 364 | EXPECT_EQ(1, num_of_owner_changes1); |
| 365 | } |
| 366 | |
| 367 | // Add a second listener. |
| 368 | std::string service_owner2; |
| 369 | int num_of_owner_changes2 = 0; |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 370 | Bus::GetServiceOwnerCallback callback2 = |
[email protected] | 049616e | 2013-06-10 22:52:34 | [diff] [blame] | 371 | base::Bind(&OnServiceOwnerChanged, |
| 372 | &run_loop_state, |
| 373 | &service_owner2, |
| 374 | &num_of_owner_changes2); |
| 375 | bus->ListenForServiceOwnerChange("org.chromium.TestService", callback2); |
| 376 | base::RunLoop().RunUntilIdle(); |
| 377 | |
| 378 | // Release the ownership and make sure the service owner listeners fire with |
| 379 | // the right values and the right number of times. |
| 380 | ASSERT_TRUE(bus->ReleaseOwnership("org.chromium.TestService")); |
| 381 | run_loop_state.Run(2); |
| 382 | |
| 383 | EXPECT_TRUE(service_owner1.empty()); |
| 384 | EXPECT_TRUE(service_owner2.empty()); |
| 385 | EXPECT_EQ(2, num_of_owner_changes1); |
| 386 | EXPECT_EQ(1, num_of_owner_changes2); |
| 387 | |
| 388 | // Unlisten so shutdown can proceed correctly. |
| 389 | bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback1); |
| 390 | bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback2); |
| 391 | base::RunLoop().RunUntilIdle(); |
| 392 | |
| 393 | // Shut down synchronously. |
| 394 | bus->ShutdownAndBlock(); |
| 395 | EXPECT_TRUE(bus->shutdown_completed()); |
| 396 | } |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 397 | |
zqiu | f63bfe5e | 2015-07-08 02:08:30 | [diff] [blame] | 398 | TEST(BusTest, GetConnectionName) { |
| 399 | Bus::Options options; |
| 400 | scoped_refptr<Bus> bus = new Bus(options); |
| 401 | |
| 402 | // Connection name is empty since bus is not connected. |
| 403 | EXPECT_FALSE(bus->is_connected()); |
| 404 | EXPECT_TRUE(bus->GetConnectionName().empty()); |
| 405 | |
| 406 | // Connect bus to D-Bus. |
| 407 | bus->Connect(); |
| 408 | |
| 409 | // Connection name is not empty after connection is established. |
| 410 | EXPECT_TRUE(bus->is_connected()); |
| 411 | EXPECT_FALSE(bus->GetConnectionName().empty()); |
| 412 | |
| 413 | // Shut down synchronously. |
| 414 | bus->ShutdownAndBlock(); |
| 415 | EXPECT_TRUE(bus->shutdown_completed()); |
| 416 | } |
| 417 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 418 | } // namespace dbus |