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 17 matching lines...) Expand all Loading... | |
28 | 28 |
29 // Make the main thread not to allow IO. | 29 // Make the main thread not to allow IO. |
30 base::ThreadRestrictions::SetIOAllowed(false); | 30 base::ThreadRestrictions::SetIOAllowed(false); |
31 | 31 |
32 // Start the D-Bus thread. | 32 // Start the D-Bus thread. |
33 dbus_thread_.reset(new base::Thread("D-Bus Thread")); | 33 dbus_thread_.reset(new base::Thread("D-Bus Thread")); |
34 base::Thread::Options thread_options; | 34 base::Thread::Options thread_options; |
35 thread_options.message_loop_type = MessageLoop::TYPE_IO; | 35 thread_options.message_loop_type = MessageLoop::TYPE_IO; |
36 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); | 36 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); |
37 | 37 |
38 // Start the test service, using the D-Bus thread. | |
39 dbus::TestService::Options options; | |
40 options.dbus_thread_message_loop_proxy = dbus_thread_->message_loop_proxy(); | |
41 test_service_.reset(new dbus::TestService(options)); | |
42 ASSERT_TRUE(test_service_->StartService()); | |
43 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); | |
44 ASSERT_TRUE(test_service_->HasDBusThread()); | |
45 | |
46 // Same setup for the second TestService. This service should not have the | |
47 // ownership of the name at this point. | |
48 test_service2_.reset(new dbus::TestService(options)); | |
49 ASSERT_TRUE(test_service2_->StartService()); | |
50 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted()); | |
51 ASSERT_TRUE(test_service2_->HasDBusThread()); | |
52 | |
53 // Create the client, using the D-Bus thread. | 38 // Create the client, using the D-Bus thread. |
54 dbus::Bus::Options bus_options; | 39 dbus::Bus::Options bus_options; |
55 bus_options.bus_type = dbus::Bus::SESSION; | 40 bus_options.bus_type = dbus::Bus::SESSION; |
56 bus_options.connection_type = dbus::Bus::PRIVATE; | 41 bus_options.connection_type = dbus::Bus::PRIVATE; |
57 bus_options.dbus_thread_message_loop_proxy = | 42 bus_options.dbus_thread_message_loop_proxy = |
58 dbus_thread_->message_loop_proxy(); | 43 dbus_thread_->message_loop_proxy(); |
59 bus_ = new dbus::Bus(bus_options); | 44 bus_ = new dbus::Bus(bus_options); |
60 object_proxy_ = bus_->GetObjectProxy( | 45 object_proxy_ = bus_->GetObjectProxy( |
61 "org.chromium.TestService", | 46 "org.chromium.TestService", |
62 dbus::ObjectPath("/org/chromium/TestObject")); | 47 dbus::ObjectPath("/org/chromium/TestObject")); |
63 ASSERT_TRUE(bus_->HasDBusThread()); | 48 ASSERT_TRUE(bus_->HasDBusThread()); |
64 | 49 |
50 object_proxy_->SetNameOwnerChangedCallback( | |
51 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged, | |
52 base::Unretained(this))); | |
53 | |
65 // Connect to the "Test" signal of "org.chromium.TestInterface" from | 54 // Connect to the "Test" signal of "org.chromium.TestInterface" from |
66 // the remote object. | 55 // the remote object. |
67 object_proxy_->ConnectToSignal( | 56 object_proxy_->ConnectToSignal( |
68 "org.chromium.TestInterface", | 57 "org.chromium.TestInterface", |
69 "Test", | 58 "Test", |
70 base::Bind(&SignalSenderVerificationTest::OnTestSignal, | 59 base::Bind(&SignalSenderVerificationTest::OnTestSignal, |
71 base::Unretained(this)), | 60 base::Unretained(this)), |
72 base::Bind(&SignalSenderVerificationTest::OnConnected, | 61 base::Bind(&SignalSenderVerificationTest::OnConnected, |
73 base::Unretained(this))); | 62 base::Unretained(this))); |
74 // Wait until the object proxy is connected to the signal. | 63 // Wait until the object proxy is connected to the signal. |
75 message_loop_.Run(); | 64 message_loop_.Run(); |
65 | |
66 // Start the test service, using the D-Bus thread. | |
67 dbus::TestService::Options options; | |
68 options.dbus_thread_message_loop_proxy = dbus_thread_->message_loop_proxy(); | |
69 test_service_.reset(new dbus::TestService(options)); | |
70 ASSERT_TRUE(test_service_->StartService()); | |
71 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); | |
72 ASSERT_TRUE(test_service_->HasDBusThread()); | |
73 ASSERT_TRUE(test_service_->has_ownership()); | |
74 | |
75 // Same setup for the second TestService. This service should not have the | |
76 // ownership of the name at this point. | |
77 test_service2_.reset(new dbus::TestService(options)); | |
78 ASSERT_TRUE(test_service2_->StartService()); | |
79 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted()); | |
80 ASSERT_TRUE(test_service2_->HasDBusThread()); | |
81 ASSERT_FALSE(test_service2_->has_ownership()); | |
82 | |
83 // The name should be owned and known at this point. | |
84 if (!on_name_owner_changed_called_) | |
85 message_loop_.Run(); | |
86 ASSERT_FALSE(latest_name_owner_.empty()); | |
87 | |
76 } | 88 } |
77 | 89 |
78 virtual void TearDown() { | 90 virtual void TearDown() { |
79 bus_->ShutdownOnDBusThreadAndBlock(); | 91 bus_->ShutdownOnDBusThreadAndBlock(); |
80 | 92 |
81 // Shut down the service. | 93 // Shut down the service. |
82 test_service_->ShutdownAndBlock(); | 94 test_service_->ShutdownAndBlock(); |
83 test_service2_->ShutdownAndBlock(); | 95 test_service2_->ShutdownAndBlock(); |
84 | 96 |
85 // Reset to the default. | 97 // Reset to the default. |
86 base::ThreadRestrictions::SetIOAllowed(true); | 98 base::ThreadRestrictions::SetIOAllowed(true); |
87 | 99 |
88 // Stopping a thread is considered an IO operation, so do this after | 100 // Stopping a thread is considered an IO operation, so do this after |
89 // allowing IO. | 101 // allowing IO. |
90 test_service_->Stop(); | 102 test_service_->Stop(); |
91 test_service2_->Stop(); | 103 test_service2_->Stop(); |
92 } | 104 } |
93 | 105 |
106 void OnOwnership(bool expected, bool success) { | |
107 ASSERT_EQ(expected, success); | |
108 // PostTask to quit the MessageLoop as this is called from D-Bus thread. | |
109 message_loop_.PostTask( | |
110 FROM_HERE, | |
111 base::Bind(&SignalSenderVerificationTest::OnOwnershipInternal, | |
112 base::Unretained(this))); | |
113 } | |
114 | |
115 void OnOwnershipInternal() { | |
116 on_ownership_called_ = true; | |
117 message_loop_.Quit(); | |
118 } | |
119 | |
120 void OnNameOwnerChanged(dbus::Signal* signal) { | |
121 dbus::MessageReader reader(signal); | |
122 std::string name, old_owner, new_owner; | |
123 ASSERT_TRUE(reader.PopString(&name)); | |
124 ASSERT_TRUE(reader.PopString(&old_owner)); | |
125 ASSERT_TRUE(reader.PopString(&new_owner)); | |
126 latest_name_owner_ = new_owner; | |
127 on_name_owner_changed_called_ = true; | |
128 message_loop_.Quit(); | |
129 } | |
130 | |
94 protected: | 131 protected: |
95 | 132 |
96 // Called when the "Test" signal is received, in the main thread. | 133 // Called when the "Test" signal is received, in the main thread. |
97 // Copy the string payload to |test_signal_string_|. | 134 // Copy the string payload to |test_signal_string_|. |
98 void OnTestSignal(dbus::Signal* signal) { | 135 void OnTestSignal(dbus::Signal* signal) { |
99 dbus::MessageReader reader(signal); | 136 dbus::MessageReader reader(signal); |
100 ASSERT_TRUE(reader.PopString(&test_signal_string_)); | 137 ASSERT_TRUE(reader.PopString(&test_signal_string_)); |
101 message_loop_.Quit(); | 138 message_loop_.Quit(); |
102 } | 139 } |
103 | 140 |
(...skipping 12 matching lines...) Expand all Loading... | |
116 } | 153 } |
117 | 154 |
118 MessageLoop message_loop_; | 155 MessageLoop message_loop_; |
119 scoped_ptr<base::Thread> dbus_thread_; | 156 scoped_ptr<base::Thread> dbus_thread_; |
120 scoped_refptr<dbus::Bus> bus_; | 157 scoped_refptr<dbus::Bus> bus_; |
121 dbus::ObjectProxy* object_proxy_; | 158 dbus::ObjectProxy* object_proxy_; |
122 scoped_ptr<dbus::TestService> test_service_; | 159 scoped_ptr<dbus::TestService> test_service_; |
123 scoped_ptr<dbus::TestService> test_service2_; | 160 scoped_ptr<dbus::TestService> test_service2_; |
124 // Text message from "Test" signal. | 161 // Text message from "Test" signal. |
125 std::string test_signal_string_; | 162 std::string test_signal_string_; |
163 | |
164 // The known latest name owner of TestService. Updated in OnNameOwnerChanged. | |
165 std::string latest_name_owner_; | |
166 | |
167 // Boolean flags to record callback calls. | |
168 bool on_name_owner_changed_called_; | |
169 bool on_ownership_called_; | |
satorux1
2012/11/14 07:56:10
Please initialize them to false in the constructor
Haruki Sato
2012/11/14 08:25:20
Done. Thanks.
| |
126 }; | 170 }; |
127 | 171 |
128 TEST_F(SignalSenderVerificationTest, TestSignalAccepted) { | 172 TEST_F(SignalSenderVerificationTest, TestSignalAccepted) { |
129 const char kMessage[] = "hello, world"; | 173 const char kMessage[] = "hello, world"; |
130 // Send the test signal from the exported object. | 174 // Send the test signal from the exported object. |
131 test_service_->SendTestSignal(kMessage); | 175 test_service_->SendTestSignal(kMessage); |
132 // Receive the signal with the object proxy. The signal is handled in | 176 // Receive the signal with the object proxy. The signal is handled in |
133 // SignalSenderVerificationTest::OnTestSignal() in the main thread. | 177 // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
134 WaitForTestSignal(); | 178 WaitForTestSignal(); |
135 ASSERT_EQ(kMessage, test_signal_string_); | 179 ASSERT_EQ(kMessage, test_signal_string_); |
(...skipping 14 matching lines...) Expand all Loading... | |
150 // Sleep to have message delivered to the client via the D-Bus service. | 194 // Sleep to have message delivered to the client via the D-Bus service. |
151 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); | 195 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); |
152 | 196 |
153 scoped_ptr<base::HistogramSamples> samples2( | 197 scoped_ptr<base::HistogramSamples> samples2( |
154 reject_signal_histogram->SnapshotSamples()); | 198 reject_signal_histogram->SnapshotSamples()); |
155 | 199 |
156 ASSERT_EQ("", test_signal_string_); | 200 ASSERT_EQ("", test_signal_string_); |
157 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount()); | 201 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount()); |
158 } | 202 } |
159 | 203 |
160 TEST_F(SignalSenderVerificationTest, DISABLED_TestOwnerChanged) { | 204 TEST_F(SignalSenderVerificationTest, TestOwnerChanged) { |
161 const char kMessage[] = "hello, world"; | 205 const char kMessage[] = "hello, world"; |
162 | 206 |
163 // Send the test signal from the exported object. | 207 // Send the test signal from the exported object. |
164 test_service_->SendTestSignal(kMessage); | 208 test_service_->SendTestSignal(kMessage); |
165 // Receive the signal with the object proxy. The signal is handled in | 209 // Receive the signal with the object proxy. The signal is handled in |
166 // SignalSenderVerificationTest::OnTestSignal() in the main thread. | 210 // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
167 WaitForTestSignal(); | 211 WaitForTestSignal(); |
168 ASSERT_EQ(kMessage, test_signal_string_); | 212 ASSERT_EQ(kMessage, test_signal_string_); |
169 | 213 |
170 // Release and aquire the name ownership. | 214 // Release and acquire the name ownership. |
215 // latest_name_owner_ should be non empty as |test_service_| owns the name. | |
216 ASSERT_FALSE(latest_name_owner_.empty()); | |
171 test_service_->ShutdownAndBlock(); | 217 test_service_->ShutdownAndBlock(); |
172 test_service2_->RequestOwnership(); | 218 // OnNameOwnerChanged will PostTask to quit the message loop. |
219 message_loop_.Run(); | |
220 // latest_name_owner_ should be empty as the owner is gone. | |
221 ASSERT_TRUE(latest_name_owner_.empty()); | |
222 | |
223 // Reset the flag as NameOwnerChanged is already received in setup. | |
224 on_name_owner_changed_called_ = false; | |
225 test_service2_->RequestOwnership( | |
226 base::Bind(&SignalSenderVerificationTest::OnOwnership, | |
227 base::Unretained(this), true)); | |
228 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop, | |
229 // but there's no expected order of those 2 event. | |
230 message_loop_.Run(); | |
231 if (!on_name_owner_changed_called_ || !on_ownership_called_) | |
232 message_loop_.Run(); | |
233 ASSERT_TRUE(on_name_owner_changed_called_); | |
234 ASSERT_TRUE(on_ownership_called_); | |
235 | |
236 // latest_name_owner_ becomes non empty as the new owner appears. | |
237 ASSERT_FALSE(latest_name_owner_.empty()); | |
173 | 238 |
174 // Now the second service owns the name. | 239 // Now the second service owns the name. |
175 const char kNewMessage[] = "hello, new world"; | 240 const char kNewMessage[] = "hello, new world"; |
176 | 241 |
177 test_service2_->SendTestSignal(kNewMessage); | 242 test_service2_->SendTestSignal(kNewMessage); |
178 WaitForTestSignal(); | 243 WaitForTestSignal(); |
179 ASSERT_EQ(kNewMessage, test_signal_string_); | 244 ASSERT_EQ(kNewMessage, test_signal_string_); |
180 } | 245 } |
OLD | NEW |