adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [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 | |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 7 | #include "base/bind.h" |
Peter Kasting | 88430fa | 2018-02-13 15:22:40 +0900 | [diff] [blame] | 8 | #include "base/bind_helpers.h" |
fdoray | c4aba52 | 2017-04-18 22:40:21 +0900 | [diff] [blame] | 9 | #include "base/files/file_descriptor_watcher_posix.h" |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 10 | #include "base/macros.h" |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
avi@chromium.org | a29af56 | 2013-07-18 08:00:30 +0900 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 13 | #include "base/run_loop.h" |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 14 | #include "base/threading/thread.h" |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 15 | #include "dbus/exported_object.h" |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 16 | #include "dbus/object_path.h" |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 17 | #include "dbus/object_proxy.h" |
deymo@chromium.org | 7894ebf | 2013-01-31 15:08:02 +0900 | [diff] [blame] | 18 | #include "dbus/scoped_dbus_error.h" |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 19 | #include "dbus/test_service.h" |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 20 | |
| 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 23 | namespace dbus { |
| 24 | |
satorux@chromium.org | 66bc4c2 | 2011-10-06 09:20:53 +0900 | [diff] [blame] | 25 | namespace { |
| 26 | |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 27 | // Test helper for BusTest.ListenForServiceOwnerChange that wraps a |
| 28 | // base::RunLoop. At Run() time, the caller pass in the expected number of |
| 29 | // quit calls, and at QuitIfConditionIsSatisified() time, only quit the RunLoop |
| 30 | // if the expected number of quit calls have been reached. |
| 31 | class RunLoopWithExpectedCount { |
| 32 | public: |
| 33 | RunLoopWithExpectedCount() : expected_quit_calls_(0), actual_quit_calls_(0) {} |
Chris Watkins | 635e890 | 2017-11-29 16:44:11 +0900 | [diff] [blame] | 34 | ~RunLoopWithExpectedCount() = default; |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 35 | |
| 36 | void Run(int expected_quit_calls) { |
| 37 | DCHECK_EQ(0, expected_quit_calls_); |
| 38 | DCHECK_EQ(0, actual_quit_calls_); |
| 39 | expected_quit_calls_ = expected_quit_calls; |
| 40 | run_loop_.reset(new base::RunLoop()); |
| 41 | run_loop_->Run(); |
| 42 | } |
| 43 | |
| 44 | void QuitIfConditionIsSatisified() { |
| 45 | if (++actual_quit_calls_ != expected_quit_calls_) |
| 46 | return; |
| 47 | run_loop_->Quit(); |
| 48 | expected_quit_calls_ = 0; |
| 49 | actual_quit_calls_ = 0; |
| 50 | } |
| 51 | |
| 52 | private: |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 53 | std::unique_ptr<base::RunLoop> run_loop_; |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 54 | int expected_quit_calls_; |
| 55 | int actual_quit_calls_; |
| 56 | |
| 57 | DISALLOW_COPY_AND_ASSIGN(RunLoopWithExpectedCount); |
| 58 | }; |
| 59 | |
| 60 | // Test helper for BusTest.ListenForServiceOwnerChange. |
| 61 | void OnServiceOwnerChanged(RunLoopWithExpectedCount* run_loop_state, |
| 62 | std::string* service_owner, |
| 63 | int* num_of_owner_changes, |
| 64 | const std::string& new_service_owner) { |
| 65 | *service_owner = new_service_owner; |
| 66 | ++(*num_of_owner_changes); |
| 67 | run_loop_state->QuitIfConditionIsSatisified(); |
| 68 | } |
| 69 | |
satorux@chromium.org | 66bc4c2 | 2011-10-06 09:20:53 +0900 | [diff] [blame] | 70 | } // namespace |
| 71 | |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 72 | TEST(BusTest, GetObjectProxy) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 73 | Bus::Options options; |
| 74 | scoped_refptr<Bus> bus = new Bus(options); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 75 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 76 | ObjectProxy* object_proxy1 = |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 77 | bus->GetObjectProxy("org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 78 | ObjectPath("/org/chromium/TestObject")); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 79 | ASSERT_TRUE(object_proxy1); |
| 80 | |
| 81 | // This should return the same object. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 82 | ObjectProxy* object_proxy2 = |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 83 | bus->GetObjectProxy("org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 84 | ObjectPath("/org/chromium/TestObject")); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 85 | ASSERT_TRUE(object_proxy2); |
| 86 | EXPECT_EQ(object_proxy1, object_proxy2); |
| 87 | |
| 88 | // This should not. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 89 | ObjectProxy* object_proxy3 = |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 90 | bus->GetObjectProxy( |
| 91 | "org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 92 | ObjectPath("/org/chromium/DifferentTestObject")); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 93 | ASSERT_TRUE(object_proxy3); |
| 94 | EXPECT_NE(object_proxy1, object_proxy3); |
satorux@chromium.org | f06eb89 | 2011-10-13 09:45:26 +0900 | [diff] [blame] | 95 | |
| 96 | bus->ShutdownAndBlock(); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 97 | } |
| 98 | |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 99 | TEST(BusTest, GetObjectProxyIgnoreUnknownService) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 100 | Bus::Options options; |
| 101 | scoped_refptr<Bus> bus = new Bus(options); |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 102 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 103 | ObjectProxy* object_proxy1 = |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 104 | bus->GetObjectProxyWithOptions( |
| 105 | "org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 106 | ObjectPath("/org/chromium/TestObject"), |
| 107 | ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS); |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 108 | ASSERT_TRUE(object_proxy1); |
| 109 | |
| 110 | // This should return the same object. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 111 | ObjectProxy* object_proxy2 = |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 112 | bus->GetObjectProxyWithOptions( |
| 113 | "org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 114 | ObjectPath("/org/chromium/TestObject"), |
| 115 | ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS); |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 116 | ASSERT_TRUE(object_proxy2); |
| 117 | EXPECT_EQ(object_proxy1, object_proxy2); |
| 118 | |
| 119 | // This should not. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 120 | ObjectProxy* object_proxy3 = |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 121 | bus->GetObjectProxyWithOptions( |
| 122 | "org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 123 | ObjectPath("/org/chromium/DifferentTestObject"), |
| 124 | ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS); |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 125 | ASSERT_TRUE(object_proxy3); |
| 126 | EXPECT_NE(object_proxy1, object_proxy3); |
| 127 | |
| 128 | bus->ShutdownAndBlock(); |
| 129 | } |
| 130 | |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [diff] [blame] | 131 | TEST(BusTest, RemoveObjectProxy) { |
| 132 | // Setup the current thread's MessageLoop. |
xhwang@chromium.org | dff6b13 | 2013-05-02 01:10:30 +0900 | [diff] [blame] | 133 | base::MessageLoop message_loop; |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [diff] [blame] | 134 | |
| 135 | // Start the D-Bus thread. |
| 136 | base::Thread::Options thread_options; |
xhwang@chromium.org | dff6b13 | 2013-05-02 01:10:30 +0900 | [diff] [blame] | 137 | thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [diff] [blame] | 138 | base::Thread dbus_thread("D-Bus thread"); |
| 139 | dbus_thread.StartWithOptions(thread_options); |
| 140 | |
| 141 | // Create the bus. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 142 | Bus::Options options; |
skyostil | e5a8dc4 | 2015-06-18 00:46:04 +0900 | [diff] [blame] | 143 | options.dbus_task_runner = dbus_thread.task_runner(); |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 144 | scoped_refptr<Bus> bus = new Bus(options); |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [diff] [blame] | 145 | ASSERT_FALSE(bus->shutdown_completed()); |
| 146 | |
| 147 | // Try to remove a non existant object proxy should return false. |
Peter Kasting | 24efe5e | 2018-02-24 09:03:01 +0900 | [diff] [blame^] | 148 | ASSERT_FALSE(bus->RemoveObjectProxy("org.chromium.TestService", |
| 149 | ObjectPath("/org/chromium/TestObject"), |
| 150 | base::DoNothing())); |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [diff] [blame] | 151 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 152 | ObjectProxy* object_proxy1 = |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [diff] [blame] | 153 | bus->GetObjectProxy("org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 154 | ObjectPath("/org/chromium/TestObject")); |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [diff] [blame] | 155 | ASSERT_TRUE(object_proxy1); |
| 156 | |
| 157 | // Increment the reference count to the object proxy to avoid destroying it |
| 158 | // while removing the object. |
| 159 | object_proxy1->AddRef(); |
| 160 | |
| 161 | // Remove the object from the bus. This will invalidate any other usage of |
| 162 | // object_proxy1 other than destroy it. We keep this object for a comparison |
| 163 | // at a later time. |
Peter Kasting | 24efe5e | 2018-02-24 09:03:01 +0900 | [diff] [blame^] | 164 | ASSERT_TRUE(bus->RemoveObjectProxy("org.chromium.TestService", |
| 165 | ObjectPath("/org/chromium/TestObject"), |
| 166 | base::DoNothing())); |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [diff] [blame] | 167 | |
| 168 | // This should return a different object because the first object was removed |
| 169 | // from the bus, but not deleted from memory. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 170 | ObjectProxy* object_proxy2 = |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [diff] [blame] | 171 | bus->GetObjectProxy("org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 172 | ObjectPath("/org/chromium/TestObject")); |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [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 | |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 188 | TEST(BusTest, GetExportedObject) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 189 | Bus::Options options; |
| 190 | scoped_refptr<Bus> bus = new Bus(options); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 191 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 192 | ExportedObject* object_proxy1 = |
| 193 | bus->GetExportedObject(ObjectPath("/org/chromium/TestObject")); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 194 | ASSERT_TRUE(object_proxy1); |
| 195 | |
| 196 | // This should return the same object. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 197 | ExportedObject* object_proxy2 = |
| 198 | bus->GetExportedObject(ObjectPath("/org/chromium/TestObject")); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 199 | ASSERT_TRUE(object_proxy2); |
| 200 | EXPECT_EQ(object_proxy1, object_proxy2); |
| 201 | |
| 202 | // This should not. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 203 | ExportedObject* object_proxy3 = |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 204 | bus->GetExportedObject( |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 205 | ObjectPath("/org/chromium/DifferentTestObject")); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 206 | ASSERT_TRUE(object_proxy3); |
| 207 | EXPECT_NE(object_proxy1, object_proxy3); |
satorux@chromium.org | f06eb89 | 2011-10-13 09:45:26 +0900 | [diff] [blame] | 208 | |
| 209 | bus->ShutdownAndBlock(); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 210 | } |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 211 | |
deymo@chromium.org | 03ec248 | 2013-01-24 09:58:35 +0900 | [diff] [blame] | 212 | TEST(BusTest, UnregisterExportedObject) { |
keybuk@chromium.org | d2ca8f3 | 2012-03-14 10:18:35 +0900 | [diff] [blame] | 213 | // Start the D-Bus thread. |
| 214 | base::Thread::Options thread_options; |
xhwang@chromium.org | dff6b13 | 2013-05-02 01:10:30 +0900 | [diff] [blame] | 215 | thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
keybuk@chromium.org | d2ca8f3 | 2012-03-14 10:18:35 +0900 | [diff] [blame] | 216 | base::Thread dbus_thread("D-Bus thread"); |
| 217 | dbus_thread.StartWithOptions(thread_options); |
| 218 | |
| 219 | // Create the bus. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 220 | Bus::Options options; |
skyostil | e5a8dc4 | 2015-06-18 00:46:04 +0900 | [diff] [blame] | 221 | options.dbus_task_runner = dbus_thread.task_runner(); |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 222 | scoped_refptr<Bus> bus = new Bus(options); |
keybuk@chromium.org | d2ca8f3 | 2012-03-14 10:18:35 +0900 | [diff] [blame] | 223 | ASSERT_FALSE(bus->shutdown_completed()); |
| 224 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 225 | ExportedObject* object_proxy1 = |
| 226 | bus->GetExportedObject(ObjectPath("/org/chromium/TestObject")); |
keybuk@chromium.org | d2ca8f3 | 2012-03-14 10:18:35 +0900 | [diff] [blame] | 227 | ASSERT_TRUE(object_proxy1); |
| 228 | |
deymo@chromium.org | 03ec248 | 2013-01-24 09:58:35 +0900 | [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 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 234 | bus->UnregisterExportedObject(ObjectPath("/org/chromium/TestObject")); |
keybuk@chromium.org | d2ca8f3 | 2012-03-14 10:18:35 +0900 | [diff] [blame] | 235 | |
deymo@chromium.org | 03ec248 | 2013-01-24 09:58:35 +0900 | [diff] [blame] | 236 | // This should return a new object because the object_proxy1 is still in |
| 237 | // alloc'ed memory. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 238 | ExportedObject* object_proxy2 = |
| 239 | bus->GetExportedObject(ObjectPath("/org/chromium/TestObject")); |
keybuk@chromium.org | d2ca8f3 | 2012-03-14 10:18:35 +0900 | [diff] [blame] | 240 | ASSERT_TRUE(object_proxy2); |
| 241 | EXPECT_NE(object_proxy1, object_proxy2); |
| 242 | |
deymo@chromium.org | 03ec248 | 2013-01-24 09:58:35 +0900 | [diff] [blame] | 243 | // Release the incremented reference. |
| 244 | object_proxy1->Release(); |
| 245 | |
keybuk@chromium.org | d2ca8f3 | 2012-03-14 10:18:35 +0900 | [diff] [blame] | 246 | // Shut down synchronously. |
| 247 | bus->ShutdownOnDBusThreadAndBlock(); |
| 248 | EXPECT_TRUE(bus->shutdown_completed()); |
| 249 | dbus_thread.Stop(); |
| 250 | } |
| 251 | |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 252 | TEST(BusTest, ShutdownAndBlock) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 253 | Bus::Options options; |
| 254 | scoped_refptr<Bus> bus = new Bus(options); |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [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; |
xhwang@chromium.org | dff6b13 | 2013-05-02 01:10:30 +0900 | [diff] [blame] | 265 | thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 266 | base::Thread dbus_thread("D-Bus thread"); |
| 267 | dbus_thread.StartWithOptions(thread_options); |
| 268 | |
| 269 | // Create the bus. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 270 | Bus::Options options; |
skyostil | e5a8dc4 | 2015-06-18 00:46:04 +0900 | [diff] [blame] | 271 | options.dbus_task_runner = dbus_thread.task_runner(); |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 272 | scoped_refptr<Bus> bus = new Bus(options); |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [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 | } |
satorux@chromium.org | 66bc4c2 | 2011-10-06 09:20:53 +0900 | [diff] [blame] | 280 | |
deymo@chromium.org | 7894ebf | 2013-01-31 15:08:02 +0900 | [diff] [blame] | 281 | TEST(BusTest, DoubleAddAndRemoveMatch) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 282 | Bus::Options options; |
| 283 | scoped_refptr<Bus> bus = new Bus(options); |
| 284 | ScopedDBusError error; |
deymo@chromium.org | 7894ebf | 2013-01-31 15:08:02 +0900 | [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 | } |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 319 | |
| 320 | TEST(BusTest, ListenForServiceOwnerChange) { |
fdoray | c4aba52 | 2017-04-18 22:40:21 +0900 | [diff] [blame] | 321 | base::MessageLoopForIO message_loop; |
| 322 | |
| 323 | // This enables FileDescriptorWatcher, which is required by dbus::Watch. |
| 324 | base::FileDescriptorWatcher file_descriptor_watcher(&message_loop); |
| 325 | |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 326 | RunLoopWithExpectedCount run_loop_state; |
| 327 | |
| 328 | // Create the bus. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 329 | Bus::Options bus_options; |
| 330 | scoped_refptr<Bus> bus = new Bus(bus_options); |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 331 | |
| 332 | // Add a listener. |
| 333 | std::string service_owner1; |
| 334 | int num_of_owner_changes1 = 0; |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 335 | Bus::GetServiceOwnerCallback callback1 = |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 336 | base::Bind(&OnServiceOwnerChanged, |
| 337 | &run_loop_state, |
| 338 | &service_owner1, |
| 339 | &num_of_owner_changes1); |
| 340 | bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1); |
| 341 | // This should be a no-op. |
| 342 | bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1); |
| 343 | base::RunLoop().RunUntilIdle(); |
| 344 | |
| 345 | // Nothing has happened yet. Check initial state. |
| 346 | EXPECT_TRUE(service_owner1.empty()); |
| 347 | EXPECT_EQ(0, num_of_owner_changes1); |
| 348 | |
| 349 | // Make an ownership change. |
cmasone@chromium.org | 989857e | 2013-07-31 15:34:59 +0900 | [diff] [blame] | 350 | ASSERT_TRUE(bus->RequestOwnershipAndBlock("org.chromium.TestService", |
| 351 | Bus::REQUIRE_PRIMARY)); |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 352 | run_loop_state.Run(1); |
| 353 | |
| 354 | { |
| 355 | // Get the current service owner and check to make sure the listener got |
| 356 | // the right value. |
| 357 | std::string current_service_owner = |
| 358 | bus->GetServiceOwnerAndBlock("org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 359 | Bus::REPORT_ERRORS); |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 360 | ASSERT_FALSE(current_service_owner.empty()); |
| 361 | |
| 362 | // Make sure the listener heard about the new owner. |
| 363 | EXPECT_EQ(current_service_owner, service_owner1); |
| 364 | |
| 365 | // Test the second ListenForServiceOwnerChange() above is indeed a no-op. |
| 366 | EXPECT_EQ(1, num_of_owner_changes1); |
| 367 | } |
| 368 | |
| 369 | // Add a second listener. |
| 370 | std::string service_owner2; |
| 371 | int num_of_owner_changes2 = 0; |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 372 | Bus::GetServiceOwnerCallback callback2 = |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 373 | base::Bind(&OnServiceOwnerChanged, |
| 374 | &run_loop_state, |
| 375 | &service_owner2, |
| 376 | &num_of_owner_changes2); |
| 377 | bus->ListenForServiceOwnerChange("org.chromium.TestService", callback2); |
| 378 | base::RunLoop().RunUntilIdle(); |
| 379 | |
| 380 | // Release the ownership and make sure the service owner listeners fire with |
| 381 | // the right values and the right number of times. |
| 382 | ASSERT_TRUE(bus->ReleaseOwnership("org.chromium.TestService")); |
| 383 | run_loop_state.Run(2); |
| 384 | |
| 385 | EXPECT_TRUE(service_owner1.empty()); |
| 386 | EXPECT_TRUE(service_owner2.empty()); |
| 387 | EXPECT_EQ(2, num_of_owner_changes1); |
| 388 | EXPECT_EQ(1, num_of_owner_changes2); |
| 389 | |
| 390 | // Unlisten so shutdown can proceed correctly. |
| 391 | bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback1); |
| 392 | bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback2); |
| 393 | base::RunLoop().RunUntilIdle(); |
| 394 | |
| 395 | // Shut down synchronously. |
| 396 | bus->ShutdownAndBlock(); |
| 397 | EXPECT_TRUE(bus->shutdown_completed()); |
| 398 | } |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 399 | |
zqiu | a84d25f | 2015-07-08 11:08:30 +0900 | [diff] [blame] | 400 | TEST(BusTest, GetConnectionName) { |
| 401 | Bus::Options options; |
| 402 | scoped_refptr<Bus> bus = new Bus(options); |
| 403 | |
| 404 | // Connection name is empty since bus is not connected. |
| 405 | EXPECT_FALSE(bus->is_connected()); |
| 406 | EXPECT_TRUE(bus->GetConnectionName().empty()); |
| 407 | |
| 408 | // Connect bus to D-Bus. |
| 409 | bus->Connect(); |
| 410 | |
| 411 | // Connection name is not empty after connection is established. |
| 412 | EXPECT_TRUE(bus->is_connected()); |
| 413 | EXPECT_FALSE(bus->GetConnectionName().empty()); |
| 414 | |
| 415 | // Shut down synchronously. |
| 416 | bus->ShutdownAndBlock(); |
| 417 | EXPECT_TRUE(bus->shutdown_completed()); |
| 418 | } |
| 419 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 420 | } // namespace dbus |