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_)); | |
56 | 55 |
57 // Connect to the "Test" signal of "org.chromium.TestInterface" from | 56 // Connect to the "Test" signal of "org.chromium.TestInterface" from |
58 // the remote object. | 57 // the remote object. |
59 object_proxy_->ConnectToSignal( | 58 object_proxy_->ConnectToSignal( |
60 "org.chromium.TestInterface", | 59 "org.chromium.TestInterface", |
61 "Test", | 60 "Test", |
62 base::Bind(&SignalSenderVerificationTest::OnTestSignal, | 61 base::Bind(&SignalSenderVerificationTest::OnTestSignal, |
63 base::Unretained(this)), | 62 base::Unretained(this)), |
64 base::Bind(&SignalSenderVerificationTest::OnConnected, | 63 base::Bind(&SignalSenderVerificationTest::OnConnected, |
65 base::Unretained(this))); | 64 base::Unretained(this))); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 FROM_HERE, | 112 FROM_HERE, |
114 base::Bind(&SignalSenderVerificationTest::OnOwnershipInternal, | 113 base::Bind(&SignalSenderVerificationTest::OnOwnershipInternal, |
115 base::Unretained(this))); | 114 base::Unretained(this))); |
116 } | 115 } |
117 | 116 |
118 void OnOwnershipInternal() { | 117 void OnOwnershipInternal() { |
119 on_ownership_called_ = true; | 118 on_ownership_called_ = true; |
120 message_loop_.Quit(); | 119 message_loop_.Quit(); |
121 } | 120 } |
122 | 121 |
123 void OnNameOwnerChanged(bool* called_flag, dbus::Signal* signal) { | 122 void OnNameOwnerChanged(dbus::Signal* signal) { |
124 dbus::MessageReader reader(signal); | 123 dbus::MessageReader reader(signal); |
125 std::string name, old_owner, new_owner; | 124 std::string name, old_owner, new_owner; |
126 ASSERT_TRUE(reader.PopString(&name)); | 125 ASSERT_TRUE(reader.PopString(&name)); |
127 ASSERT_TRUE(reader.PopString(&old_owner)); | 126 ASSERT_TRUE(reader.PopString(&old_owner)); |
128 ASSERT_TRUE(reader.PopString(&new_owner)); | 127 ASSERT_TRUE(reader.PopString(&new_owner)); |
129 latest_name_owner_ = new_owner; | 128 latest_name_owner_ = new_owner; |
130 *called_flag = true; | 129 on_name_owner_changed_called_ = true; |
131 message_loop_.Quit(); | 130 message_loop_.Quit(); |
132 } | 131 } |
133 | 132 |
| 133 protected: |
| 134 |
134 // Called when the "Test" signal is received, in the main thread. | 135 // Called when the "Test" signal is received, in the main thread. |
135 // Copy the string payload to |test_signal_string_|. | 136 // Copy the string payload to |test_signal_string_|. |
136 void OnTestSignal(dbus::Signal* signal) { | 137 void OnTestSignal(dbus::Signal* signal) { |
137 dbus::MessageReader reader(signal); | 138 dbus::MessageReader reader(signal); |
138 ASSERT_TRUE(reader.PopString(&test_signal_string_)); | 139 ASSERT_TRUE(reader.PopString(&test_signal_string_)); |
139 message_loop_.Quit(); | 140 message_loop_.Quit(); |
140 } | 141 } |
141 | 142 |
142 // Called when connected to the signal. | 143 // Called when connected to the signal. |
143 void OnConnected(const std::string& interface_name, | 144 void OnConnected(const std::string& interface_name, |
144 const std::string& signal_name, | 145 const std::string& signal_name, |
145 bool success) { | 146 bool success) { |
146 ASSERT_TRUE(success); | 147 ASSERT_TRUE(success); |
147 message_loop_.Quit(); | 148 message_loop_.Quit(); |
148 } | 149 } |
149 | 150 |
150 protected: | |
151 | |
152 // Wait for the hey signal to be received. | 151 // Wait for the hey signal to be received. |
153 void WaitForTestSignal() { | 152 void WaitForTestSignal() { |
154 // OnTestSignal() will quit the message loop. | 153 // OnTestSignal() will quit the message loop. |
155 message_loop_.Run(); | 154 message_loop_.Run(); |
156 } | 155 } |
157 | 156 |
158 MessageLoop message_loop_; | 157 MessageLoop message_loop_; |
159 scoped_ptr<base::Thread> dbus_thread_; | 158 scoped_ptr<base::Thread> dbus_thread_; |
160 scoped_refptr<dbus::Bus> bus_; | 159 scoped_refptr<dbus::Bus> bus_; |
161 dbus::ObjectProxy* object_proxy_; | 160 dbus::ObjectProxy* object_proxy_; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 // latest_name_owner_ becomes non empty as the new owner appears. | 238 // latest_name_owner_ becomes non empty as the new owner appears. |
240 ASSERT_FALSE(latest_name_owner_.empty()); | 239 ASSERT_FALSE(latest_name_owner_.empty()); |
241 | 240 |
242 // Now the second service owns the name. | 241 // Now the second service owns the name. |
243 const char kNewMessage[] = "hello, new world"; | 242 const char kNewMessage[] = "hello, new world"; |
244 | 243 |
245 test_service2_->SendTestSignal(kNewMessage); | 244 test_service2_->SendTestSignal(kNewMessage); |
246 WaitForTestSignal(); | 245 WaitForTestSignal(); |
247 ASSERT_EQ(kNewMessage, test_signal_string_); | 246 ASSERT_EQ(kNewMessage, test_signal_string_); |
248 } | 247 } |
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 |