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" |
11 #include "base/test/test_timeouts.h" | 11 #include "base/test/test_timeouts.h" |
12 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
14 #include "dbus/bus.h" | 14 #include "dbus/bus.h" |
15 #include "dbus/message.h" | 15 #include "dbus/message.h" |
16 #include "dbus/object_proxy.h" | 16 #include "dbus/object_proxy.h" |
17 #include "dbus/test_service.h" | 17 #include "dbus/test_service.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
| 20 namespace dbus { |
| 21 |
20 // The test for sender verification in ObjectProxy. | 22 // The test for sender verification in ObjectProxy. |
21 class SignalSenderVerificationTest : public testing::Test { | 23 class SignalSenderVerificationTest : public testing::Test { |
22 public: | 24 public: |
23 SignalSenderVerificationTest() | 25 SignalSenderVerificationTest() |
24 : on_name_owner_changed_called_(false), | 26 : on_name_owner_changed_called_(false), |
25 on_ownership_called_(false) { | 27 on_ownership_called_(false) { |
26 } | 28 } |
27 | 29 |
28 virtual void SetUp() { | 30 virtual void SetUp() { |
29 base::StatisticsRecorder::Initialize(); | 31 base::StatisticsRecorder::Initialize(); |
30 | 32 |
31 // Make the main thread not to allow IO. | 33 // Make the main thread not to allow IO. |
32 base::ThreadRestrictions::SetIOAllowed(false); | 34 base::ThreadRestrictions::SetIOAllowed(false); |
33 | 35 |
34 // Start the D-Bus thread. | 36 // Start the D-Bus thread. |
35 dbus_thread_.reset(new base::Thread("D-Bus Thread")); | 37 dbus_thread_.reset(new base::Thread("D-Bus Thread")); |
36 base::Thread::Options thread_options; | 38 base::Thread::Options thread_options; |
37 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 39 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
38 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); | 40 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); |
39 | 41 |
40 // Create the client, using the D-Bus thread. | 42 // Create the client, using the D-Bus thread. |
41 dbus::Bus::Options bus_options; | 43 Bus::Options bus_options; |
42 bus_options.bus_type = dbus::Bus::SESSION; | 44 bus_options.bus_type = Bus::SESSION; |
43 bus_options.connection_type = dbus::Bus::PRIVATE; | 45 bus_options.connection_type = Bus::PRIVATE; |
44 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy(); | 46 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy(); |
45 bus_ = new dbus::Bus(bus_options); | 47 bus_ = new Bus(bus_options); |
46 object_proxy_ = bus_->GetObjectProxy( | 48 object_proxy_ = bus_->GetObjectProxy( |
47 "org.chromium.TestService", | 49 "org.chromium.TestService", |
48 dbus::ObjectPath("/org/chromium/TestObject")); | 50 ObjectPath("/org/chromium/TestObject")); |
49 ASSERT_TRUE(bus_->HasDBusThread()); | 51 ASSERT_TRUE(bus_->HasDBusThread()); |
50 | 52 |
51 object_proxy_->SetNameOwnerChangedCallback( | 53 object_proxy_->SetNameOwnerChangedCallback( |
52 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged, | 54 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged, |
53 base::Unretained(this), | 55 base::Unretained(this), |
54 &on_name_owner_changed_called_)); | 56 &on_name_owner_changed_called_)); |
55 | 57 |
56 // Connect to the "Test" signal of "org.chromium.TestInterface" from | 58 // Connect to the "Test" signal of "org.chromium.TestInterface" from |
57 // the remote object. | 59 // the remote object. |
58 object_proxy_->ConnectToSignal( | 60 object_proxy_->ConnectToSignal( |
59 "org.chromium.TestInterface", | 61 "org.chromium.TestInterface", |
60 "Test", | 62 "Test", |
61 base::Bind(&SignalSenderVerificationTest::OnTestSignal, | 63 base::Bind(&SignalSenderVerificationTest::OnTestSignal, |
62 base::Unretained(this)), | 64 base::Unretained(this)), |
63 base::Bind(&SignalSenderVerificationTest::OnConnected, | 65 base::Bind(&SignalSenderVerificationTest::OnConnected, |
64 base::Unretained(this))); | 66 base::Unretained(this))); |
65 // Wait until the object proxy is connected to the signal. | 67 // Wait until the object proxy is connected to the signal. |
66 message_loop_.Run(); | 68 message_loop_.Run(); |
67 | 69 |
68 // Start the test service, using the D-Bus thread. | 70 // Start the test service, using the D-Bus thread. |
69 dbus::TestService::Options options; | 71 TestService::Options options; |
70 options.dbus_task_runner = dbus_thread_->message_loop_proxy(); | 72 options.dbus_task_runner = dbus_thread_->message_loop_proxy(); |
71 test_service_.reset(new dbus::TestService(options)); | 73 test_service_.reset(new TestService(options)); |
72 ASSERT_TRUE(test_service_->StartService()); | 74 ASSERT_TRUE(test_service_->StartService()); |
73 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); | 75 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); |
74 ASSERT_TRUE(test_service_->HasDBusThread()); | 76 ASSERT_TRUE(test_service_->HasDBusThread()); |
75 ASSERT_TRUE(test_service_->has_ownership()); | 77 ASSERT_TRUE(test_service_->has_ownership()); |
76 | 78 |
77 // Same setup for the second TestService. This service should not have the | 79 // Same setup for the second TestService. This service should not have the |
78 // ownership of the name at this point. | 80 // ownership of the name at this point. |
79 test_service2_.reset(new dbus::TestService(options)); | 81 test_service2_.reset(new TestService(options)); |
80 ASSERT_TRUE(test_service2_->StartService()); | 82 ASSERT_TRUE(test_service2_->StartService()); |
81 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted()); | 83 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted()); |
82 ASSERT_TRUE(test_service2_->HasDBusThread()); | 84 ASSERT_TRUE(test_service2_->HasDBusThread()); |
83 ASSERT_FALSE(test_service2_->has_ownership()); | 85 ASSERT_FALSE(test_service2_->has_ownership()); |
84 | 86 |
85 // The name should be owned and known at this point. | 87 // The name should be owned and known at this point. |
86 if (!on_name_owner_changed_called_) | 88 if (!on_name_owner_changed_called_) |
87 message_loop_.Run(); | 89 message_loop_.Run(); |
88 ASSERT_FALSE(latest_name_owner_.empty()); | 90 ASSERT_FALSE(latest_name_owner_.empty()); |
89 } | 91 } |
(...skipping 21 matching lines...) Expand all Loading... |
111 FROM_HERE, | 113 FROM_HERE, |
112 base::Bind(&SignalSenderVerificationTest::OnOwnershipInternal, | 114 base::Bind(&SignalSenderVerificationTest::OnOwnershipInternal, |
113 base::Unretained(this))); | 115 base::Unretained(this))); |
114 } | 116 } |
115 | 117 |
116 void OnOwnershipInternal() { | 118 void OnOwnershipInternal() { |
117 on_ownership_called_ = true; | 119 on_ownership_called_ = true; |
118 message_loop_.Quit(); | 120 message_loop_.Quit(); |
119 } | 121 } |
120 | 122 |
121 void OnNameOwnerChanged(bool* called_flag, dbus::Signal* signal) { | 123 void OnNameOwnerChanged(bool* called_flag, Signal* signal) { |
122 dbus::MessageReader reader(signal); | 124 MessageReader reader(signal); |
123 std::string name, old_owner, new_owner; | 125 std::string name, old_owner, new_owner; |
124 ASSERT_TRUE(reader.PopString(&name)); | 126 ASSERT_TRUE(reader.PopString(&name)); |
125 ASSERT_TRUE(reader.PopString(&old_owner)); | 127 ASSERT_TRUE(reader.PopString(&old_owner)); |
126 ASSERT_TRUE(reader.PopString(&new_owner)); | 128 ASSERT_TRUE(reader.PopString(&new_owner)); |
127 latest_name_owner_ = new_owner; | 129 latest_name_owner_ = new_owner; |
128 *called_flag = true; | 130 *called_flag = true; |
129 message_loop_.Quit(); | 131 message_loop_.Quit(); |
130 } | 132 } |
131 | 133 |
132 // Called when the "Test" signal is received, in the main thread. | 134 // Called when the "Test" signal is received, in the main thread. |
133 // Copy the string payload to |test_signal_string_|. | 135 // Copy the string payload to |test_signal_string_|. |
134 void OnTestSignal(dbus::Signal* signal) { | 136 void OnTestSignal(Signal* signal) { |
135 dbus::MessageReader reader(signal); | 137 MessageReader reader(signal); |
136 ASSERT_TRUE(reader.PopString(&test_signal_string_)); | 138 ASSERT_TRUE(reader.PopString(&test_signal_string_)); |
137 message_loop_.Quit(); | 139 message_loop_.Quit(); |
138 } | 140 } |
139 | 141 |
140 // Called when connected to the signal. | 142 // Called when connected to the signal. |
141 void OnConnected(const std::string& interface_name, | 143 void OnConnected(const std::string& interface_name, |
142 const std::string& signal_name, | 144 const std::string& signal_name, |
143 bool success) { | 145 bool success) { |
144 ASSERT_TRUE(success); | 146 ASSERT_TRUE(success); |
145 message_loop_.Quit(); | 147 message_loop_.Quit(); |
146 } | 148 } |
147 | 149 |
148 protected: | 150 protected: |
149 // Wait for the hey signal to be received. | 151 // Wait for the hey signal to be received. |
150 void WaitForTestSignal() { | 152 void WaitForTestSignal() { |
151 // OnTestSignal() will quit the message loop. | 153 // OnTestSignal() will quit the message loop. |
152 message_loop_.Run(); | 154 message_loop_.Run(); |
153 } | 155 } |
154 | 156 |
155 base::MessageLoop message_loop_; | 157 base::MessageLoop message_loop_; |
156 scoped_ptr<base::Thread> dbus_thread_; | 158 scoped_ptr<base::Thread> dbus_thread_; |
157 scoped_refptr<dbus::Bus> bus_; | 159 scoped_refptr<Bus> bus_; |
158 dbus::ObjectProxy* object_proxy_; | 160 ObjectProxy* object_proxy_; |
159 scoped_ptr<dbus::TestService> test_service_; | 161 scoped_ptr<TestService> test_service_; |
160 scoped_ptr<dbus::TestService> test_service2_; | 162 scoped_ptr<TestService> test_service2_; |
161 // Text message from "Test" signal. | 163 // Text message from "Test" signal. |
162 std::string test_signal_string_; | 164 std::string test_signal_string_; |
163 | 165 |
164 // The known latest name owner of TestService. Updated in OnNameOwnerChanged. | 166 // The known latest name owner of TestService. Updated in OnNameOwnerChanged. |
165 std::string latest_name_owner_; | 167 std::string latest_name_owner_; |
166 | 168 |
167 // Boolean flags to record callback calls. | 169 // Boolean flags to record callback calls. |
168 bool on_name_owner_changed_called_; | 170 bool on_name_owner_changed_called_; |
169 bool on_ownership_called_; | 171 bool on_ownership_called_; |
170 }; | 172 }; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 | 243 |
242 test_service2_->SendTestSignal(kNewMessage); | 244 test_service2_->SendTestSignal(kNewMessage); |
243 WaitForTestSignal(); | 245 WaitForTestSignal(); |
244 ASSERT_EQ(kNewMessage, test_signal_string_); | 246 ASSERT_EQ(kNewMessage, test_signal_string_); |
245 } | 247 } |
246 | 248 |
247 // Fails on Linux ChromiumOS Tests | 249 // Fails on Linux ChromiumOS Tests |
248 TEST_F(SignalSenderVerificationTest, DISABLED_TestMultipleObjects) { | 250 TEST_F(SignalSenderVerificationTest, DISABLED_TestMultipleObjects) { |
249 const char kMessage[] = "hello, world"; | 251 const char kMessage[] = "hello, world"; |
250 | 252 |
251 dbus::ObjectProxy* object_proxy2 = bus_->GetObjectProxy( | 253 ObjectProxy* object_proxy2 = bus_->GetObjectProxy( |
252 "org.chromium.TestService", | 254 "org.chromium.TestService", |
253 dbus::ObjectPath("/org/chromium/DifferentObject")); | 255 ObjectPath("/org/chromium/DifferentObject")); |
254 | 256 |
255 bool second_name_owner_changed_called = false; | 257 bool second_name_owner_changed_called = false; |
256 object_proxy2->SetNameOwnerChangedCallback( | 258 object_proxy2->SetNameOwnerChangedCallback( |
257 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged, | 259 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged, |
258 base::Unretained(this), | 260 base::Unretained(this), |
259 &second_name_owner_changed_called)); | 261 &second_name_owner_changed_called)); |
260 | 262 |
261 // Connect to a signal on the additional remote object to trigger the | 263 // Connect to a signal on the additional remote object to trigger the |
262 // name owner matching. | 264 // name owner matching. |
263 object_proxy2->ConnectToSignal( | 265 object_proxy2->ConnectToSignal( |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 // latest_name_owner_ becomes non empty as the new owner appears. | 306 // latest_name_owner_ becomes non empty as the new owner appears. |
305 ASSERT_FALSE(latest_name_owner_.empty()); | 307 ASSERT_FALSE(latest_name_owner_.empty()); |
306 | 308 |
307 // Now the second service owns the name. | 309 // Now the second service owns the name. |
308 const char kNewMessage[] = "hello, new world"; | 310 const char kNewMessage[] = "hello, new world"; |
309 | 311 |
310 test_service2_->SendTestSignal(kNewMessage); | 312 test_service2_->SendTestSignal(kNewMessage); |
311 WaitForTestSignal(); | 313 WaitForTestSignal(); |
312 ASSERT_EQ(kNewMessage, test_signal_string_); | 314 ASSERT_EQ(kNewMessage, test_signal_string_); |
313 } | 315 } |
| 316 |
| 317 } // namespace dbus |
OLD | NEW |