OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/metrics/histogram_samples.h" | 9 #include "base/metrics/histogram_samples.h" |
10 #include "base/metrics/statistics_recorder.h" | 10 #include "base/metrics/statistics_recorder.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 bus_options.dbus_thread_message_loop_proxy = | 44 bus_options.dbus_thread_message_loop_proxy = |
45 dbus_thread_->message_loop_proxy(); | 45 dbus_thread_->message_loop_proxy(); |
46 bus_ = new dbus::Bus(bus_options); | 46 bus_ = new dbus::Bus(bus_options); |
47 object_proxy_ = bus_->GetObjectProxy( | 47 object_proxy_ = bus_->GetObjectProxy( |
48 "org.chromium.TestService", | 48 "org.chromium.TestService", |
49 dbus::ObjectPath("/org/chromium/TestObject")); | 49 dbus::ObjectPath("/org/chromium/TestObject")); |
50 ASSERT_TRUE(bus_->HasDBusThread()); | 50 ASSERT_TRUE(bus_->HasDBusThread()); |
51 | 51 |
52 object_proxy_->SetNameOwnerChangedCallback( | 52 object_proxy_->SetNameOwnerChangedCallback( |
53 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged, | 53 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged, |
54 base::Unretained(this))); | 54 base::Unretained(this), |
| 55 &on_name_owner_changed_called_)); |
55 | 56 |
56 // Connect to the "Test" signal of "org.chromium.TestInterface" from | 57 // Connect to the "Test" signal of "org.chromium.TestInterface" from |
57 // the remote object. | 58 // the remote object. |
58 object_proxy_->ConnectToSignal( | 59 object_proxy_->ConnectToSignal( |
59 "org.chromium.TestInterface", | 60 "org.chromium.TestInterface", |
60 "Test", | 61 "Test", |
61 base::Bind(&SignalSenderVerificationTest::OnTestSignal, | 62 base::Bind(&SignalSenderVerificationTest::OnTestSignal, |
62 base::Unretained(this)), | 63 base::Unretained(this)), |
63 base::Bind(&SignalSenderVerificationTest::OnConnected, | 64 base::Bind(&SignalSenderVerificationTest::OnConnected, |
64 base::Unretained(this))); | 65 base::Unretained(this))); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 FROM_HERE, | 113 FROM_HERE, |
113 base::Bind(&SignalSenderVerificationTest::OnOwnershipInternal, | 114 base::Bind(&SignalSenderVerificationTest::OnOwnershipInternal, |
114 base::Unretained(this))); | 115 base::Unretained(this))); |
115 } | 116 } |
116 | 117 |
117 void OnOwnershipInternal() { | 118 void OnOwnershipInternal() { |
118 on_ownership_called_ = true; | 119 on_ownership_called_ = true; |
119 message_loop_.Quit(); | 120 message_loop_.Quit(); |
120 } | 121 } |
121 | 122 |
122 void OnNameOwnerChanged(dbus::Signal* signal) { | 123 void OnNameOwnerChanged(bool* called_flag, dbus::Signal* signal) { |
123 dbus::MessageReader reader(signal); | 124 dbus::MessageReader reader(signal); |
124 std::string name, old_owner, new_owner; | 125 std::string name, old_owner, new_owner; |
125 ASSERT_TRUE(reader.PopString(&name)); | 126 ASSERT_TRUE(reader.PopString(&name)); |
126 ASSERT_TRUE(reader.PopString(&old_owner)); | 127 ASSERT_TRUE(reader.PopString(&old_owner)); |
127 ASSERT_TRUE(reader.PopString(&new_owner)); | 128 ASSERT_TRUE(reader.PopString(&new_owner)); |
128 latest_name_owner_ = new_owner; | 129 latest_name_owner_ = new_owner; |
129 on_name_owner_changed_called_ = true; | 130 *called_flag = true; |
130 message_loop_.Quit(); | 131 message_loop_.Quit(); |
131 } | 132 } |
132 | 133 |
133 protected: | |
134 | |
135 // Called when the "Test" signal is received, in the main thread. | 134 // Called when the "Test" signal is received, in the main thread. |
136 // Copy the string payload to |test_signal_string_|. | 135 // Copy the string payload to |test_signal_string_|. |
137 void OnTestSignal(dbus::Signal* signal) { | 136 void OnTestSignal(dbus::Signal* signal) { |
138 dbus::MessageReader reader(signal); | 137 dbus::MessageReader reader(signal); |
139 ASSERT_TRUE(reader.PopString(&test_signal_string_)); | 138 ASSERT_TRUE(reader.PopString(&test_signal_string_)); |
140 message_loop_.Quit(); | 139 message_loop_.Quit(); |
141 } | 140 } |
142 | 141 |
143 // Called when connected to the signal. | 142 // Called when connected to the signal. |
144 void OnConnected(const std::string& interface_name, | 143 void OnConnected(const std::string& interface_name, |
145 const std::string& signal_name, | 144 const std::string& signal_name, |
146 bool success) { | 145 bool success) { |
147 ASSERT_TRUE(success); | 146 ASSERT_TRUE(success); |
148 message_loop_.Quit(); | 147 message_loop_.Quit(); |
149 } | 148 } |
150 | 149 |
| 150 protected: |
| 151 |
151 // Wait for the hey signal to be received. | 152 // Wait for the hey signal to be received. |
152 void WaitForTestSignal() { | 153 void WaitForTestSignal() { |
153 // OnTestSignal() will quit the message loop. | 154 // OnTestSignal() will quit the message loop. |
154 message_loop_.Run(); | 155 message_loop_.Run(); |
155 } | 156 } |
156 | 157 |
157 MessageLoop message_loop_; | 158 MessageLoop message_loop_; |
158 scoped_ptr<base::Thread> dbus_thread_; | 159 scoped_ptr<base::Thread> dbus_thread_; |
159 scoped_refptr<dbus::Bus> bus_; | 160 scoped_refptr<dbus::Bus> bus_; |
160 dbus::ObjectProxy* object_proxy_; | 161 dbus::ObjectProxy* object_proxy_; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 // latest_name_owner_ becomes non empty as the new owner appears. | 239 // latest_name_owner_ becomes non empty as the new owner appears. |
239 ASSERT_FALSE(latest_name_owner_.empty()); | 240 ASSERT_FALSE(latest_name_owner_.empty()); |
240 | 241 |
241 // Now the second service owns the name. | 242 // Now the second service owns the name. |
242 const char kNewMessage[] = "hello, new world"; | 243 const char kNewMessage[] = "hello, new world"; |
243 | 244 |
244 test_service2_->SendTestSignal(kNewMessage); | 245 test_service2_->SendTestSignal(kNewMessage); |
245 WaitForTestSignal(); | 246 WaitForTestSignal(); |
246 ASSERT_EQ(kNewMessage, test_signal_string_); | 247 ASSERT_EQ(kNewMessage, test_signal_string_); |
247 } | 248 } |
| 249 |
| 250 TEST_F(SignalSenderVerificationTest, TestMultipleObjects) { |
| 251 const char kMessage[] = "hello, world"; |
| 252 |
| 253 dbus::ObjectProxy* object_proxy2 = bus_->GetObjectProxy( |
| 254 "org.chromium.TestService", |
| 255 dbus::ObjectPath("/org/chromium/DifferentObject")); |
| 256 |
| 257 bool second_name_owner_changed_called = false; |
| 258 object_proxy2->SetNameOwnerChangedCallback( |
| 259 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged, |
| 260 base::Unretained(this), |
| 261 &second_name_owner_changed_called)); |
| 262 |
| 263 // Connect to a signal on the additional remote object to trigger the |
| 264 // name owner matching. |
| 265 object_proxy2->ConnectToSignal( |
| 266 "org.chromium.DifferentTestInterface", |
| 267 "Test", |
| 268 base::Bind(&SignalSenderVerificationTest::OnTestSignal, |
| 269 base::Unretained(this)), |
| 270 base::Bind(&SignalSenderVerificationTest::OnConnected, |
| 271 base::Unretained(this))); |
| 272 // Wait until the object proxy is connected to the signal. |
| 273 message_loop_.Run(); |
| 274 |
| 275 // Send the test signal from the exported object. |
| 276 test_service_->SendTestSignal(kMessage); |
| 277 // Receive the signal with the object proxy. The signal is handled in |
| 278 // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
| 279 WaitForTestSignal(); |
| 280 ASSERT_EQ(kMessage, test_signal_string_); |
| 281 |
| 282 // Release and acquire the name ownership. |
| 283 // latest_name_owner_ should be non empty as |test_service_| owns the name. |
| 284 ASSERT_FALSE(latest_name_owner_.empty()); |
| 285 test_service_->ShutdownAndBlock(); |
| 286 // OnNameOwnerChanged will PostTask to quit the message loop. |
| 287 message_loop_.Run(); |
| 288 // latest_name_owner_ should be empty as the owner is gone. |
| 289 ASSERT_TRUE(latest_name_owner_.empty()); |
| 290 |
| 291 // Reset the flag as NameOwnerChanged is already received in setup. |
| 292 on_name_owner_changed_called_ = false; |
| 293 second_name_owner_changed_called = false; |
| 294 test_service2_->RequestOwnership( |
| 295 base::Bind(&SignalSenderVerificationTest::OnOwnership, |
| 296 base::Unretained(this), true)); |
| 297 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop, |
| 298 // but there's no expected order of those 2 event. |
| 299 message_loop_.Run(); |
| 300 if (!on_name_owner_changed_called_ || !on_ownership_called_) |
| 301 message_loop_.Run(); |
| 302 ASSERT_TRUE(on_name_owner_changed_called_); |
| 303 ASSERT_TRUE(on_ownership_called_); |
| 304 |
| 305 // The callback for the second object must have also been called in the |
| 306 // same dispatch as for the first. |
| 307 ASSERT_TRUE(second_name_owner_changed_called); |
| 308 |
| 309 // latest_name_owner_ becomes non empty as the new owner appears. |
| 310 ASSERT_FALSE(latest_name_owner_.empty()); |
| 311 |
| 312 // Now the second service owns the name. |
| 313 const char kNewMessage[] = "hello, new world"; |
| 314 |
| 315 test_service2_->SendTestSignal(kNewMessage); |
| 316 WaitForTestSignal(); |
| 317 ASSERT_EQ(kNewMessage, test_signal_string_); |
| 318 } |
OLD | NEW |