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 24 matching lines...) Expand all Loading... | |
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. | 38 // Start the test service, using the D-Bus thread. |
39 dbus::TestService::Options options; | 39 dbus::TestService::Options options; |
40 options.dbus_thread_message_loop_proxy = dbus_thread_->message_loop_proxy(); | 40 options.dbus_thread_message_loop_proxy = dbus_thread_->message_loop_proxy(); |
41 test_service_.reset(new dbus::TestService(options)); | 41 test_service_.reset(new dbus::TestService(options)); |
42 ASSERT_TRUE(test_service_->StartService()); | 42 ASSERT_TRUE(test_service_->StartService()); |
43 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); | 43 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); |
44 ASSERT_TRUE(test_service_->HasDBusThread()); | 44 ASSERT_TRUE(test_service_->HasDBusThread()); |
45 ASSERT_TRUE(test_service_->has_ownership()); | |
45 | 46 |
46 // Same setup for the second TestService. This service should not have the | 47 // Same setup for the second TestService. This service should not have the |
47 // ownership of the name at this point. | 48 // ownership of the name at this point. |
48 test_service2_.reset(new dbus::TestService(options)); | 49 test_service2_.reset(new dbus::TestService(options)); |
49 ASSERT_TRUE(test_service2_->StartService()); | 50 ASSERT_TRUE(test_service2_->StartService()); |
50 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted()); | 51 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted()); |
51 ASSERT_TRUE(test_service2_->HasDBusThread()); | 52 ASSERT_TRUE(test_service2_->HasDBusThread()); |
53 ASSERT_FALSE(test_service2_->has_ownership()); | |
52 | 54 |
53 // Create the client, using the D-Bus thread. | 55 // Create the client, using the D-Bus thread. |
54 dbus::Bus::Options bus_options; | 56 dbus::Bus::Options bus_options; |
55 bus_options.bus_type = dbus::Bus::SESSION; | 57 bus_options.bus_type = dbus::Bus::SESSION; |
56 bus_options.connection_type = dbus::Bus::PRIVATE; | 58 bus_options.connection_type = dbus::Bus::PRIVATE; |
57 bus_options.dbus_thread_message_loop_proxy = | 59 bus_options.dbus_thread_message_loop_proxy = |
58 dbus_thread_->message_loop_proxy(); | 60 dbus_thread_->message_loop_proxy(); |
59 bus_ = new dbus::Bus(bus_options); | 61 bus_ = new dbus::Bus(bus_options); |
60 object_proxy_ = bus_->GetObjectProxy( | 62 object_proxy_ = bus_->GetObjectProxy( |
61 "org.chromium.TestService", | 63 "org.chromium.TestService", |
62 dbus::ObjectPath("/org/chromium/TestObject")); | 64 dbus::ObjectPath("/org/chromium/TestObject")); |
63 ASSERT_TRUE(bus_->HasDBusThread()); | 65 ASSERT_TRUE(bus_->HasDBusThread()); |
64 | 66 |
67 object_proxy_->SetNameOwnerChangedCallback( | |
68 base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged, | |
69 base::Unretained(this))); | |
70 | |
65 // Connect to the "Test" signal of "org.chromium.TestInterface" from | 71 // Connect to the "Test" signal of "org.chromium.TestInterface" from |
66 // the remote object. | 72 // the remote object. |
67 object_proxy_->ConnectToSignal( | 73 object_proxy_->ConnectToSignal( |
68 "org.chromium.TestInterface", | 74 "org.chromium.TestInterface", |
69 "Test", | 75 "Test", |
70 base::Bind(&SignalSenderVerificationTest::OnTestSignal, | 76 base::Bind(&SignalSenderVerificationTest::OnTestSignal, |
71 base::Unretained(this)), | 77 base::Unretained(this)), |
72 base::Bind(&SignalSenderVerificationTest::OnConnected, | 78 base::Bind(&SignalSenderVerificationTest::OnConnected, |
73 base::Unretained(this))); | 79 base::Unretained(this))); |
74 // Wait until the object proxy is connected to the signal. | 80 // Wait until the object proxy is connected to the signal. |
75 message_loop_.Run(); | 81 message_loop_.Run(); |
76 } | 82 } |
77 | 83 |
84 void OnOwnership(bool expected, bool success) { | |
85 ASSERT_EQ(expected, success); | |
86 } | |
87 | |
88 void OnNameOwnerChanged(dbus::Signal* signal) { | |
89 dbus::MessageReader reader(signal); | |
90 std::string name, old_owner, new_owner; | |
91 ASSERT_TRUE(reader.PopString(&name)); | |
92 ASSERT_TRUE(reader.PopString(&old_owner)); | |
93 ASSERT_TRUE(reader.PopString(&new_owner)); | |
94 latest_name_owner_ = new_owner; | |
95 // PostTask to quit the mMessageLoop as this is called from D-Bus thread. | |
satorux1
2012/11/13 06:32:03
mMessageLoop -> MessageLoop
Thank you for adding
Haruki Sato
2012/11/14 06:35:15
This part is changed. Thank you for the review!
| |
96 message_loop_.PostTask( | |
97 FROM_HERE, | |
98 base::Bind(&SignalSenderVerificationTest::QuitMessageLoop, | |
99 base::Unretained(this))); | |
100 } | |
101 | |
78 virtual void TearDown() { | 102 virtual void TearDown() { |
79 bus_->ShutdownOnDBusThreadAndBlock(); | 103 bus_->ShutdownOnDBusThreadAndBlock(); |
80 | 104 |
81 // Shut down the service. | 105 // Shut down the service. |
82 test_service_->ShutdownAndBlock(); | 106 test_service_->ShutdownAndBlock(); |
83 test_service2_->ShutdownAndBlock(); | 107 test_service2_->ShutdownAndBlock(); |
84 | 108 |
85 // Reset to the default. | 109 // Reset to the default. |
86 base::ThreadRestrictions::SetIOAllowed(true); | 110 base::ThreadRestrictions::SetIOAllowed(true); |
87 | 111 |
(...skipping 20 matching lines...) Expand all Loading... | |
108 ASSERT_TRUE(success); | 132 ASSERT_TRUE(success); |
109 message_loop_.Quit(); | 133 message_loop_.Quit(); |
110 } | 134 } |
111 | 135 |
112 // Wait for the hey signal to be received. | 136 // Wait for the hey signal to be received. |
113 void WaitForTestSignal() { | 137 void WaitForTestSignal() { |
114 // OnTestSignal() will quit the message loop. | 138 // OnTestSignal() will quit the message loop. |
115 message_loop_.Run(); | 139 message_loop_.Run(); |
116 } | 140 } |
117 | 141 |
142 void QuitMessageLoop() { | |
143 message_loop_.Quit(); | |
144 } | |
145 | |
118 MessageLoop message_loop_; | 146 MessageLoop message_loop_; |
119 scoped_ptr<base::Thread> dbus_thread_; | 147 scoped_ptr<base::Thread> dbus_thread_; |
120 scoped_refptr<dbus::Bus> bus_; | 148 scoped_refptr<dbus::Bus> bus_; |
121 dbus::ObjectProxy* object_proxy_; | 149 dbus::ObjectProxy* object_proxy_; |
122 scoped_ptr<dbus::TestService> test_service_; | 150 scoped_ptr<dbus::TestService> test_service_; |
123 scoped_ptr<dbus::TestService> test_service2_; | 151 scoped_ptr<dbus::TestService> test_service2_; |
124 // Text message from "Test" signal. | 152 // Text message from "Test" signal. |
125 std::string test_signal_string_; | 153 std::string test_signal_string_; |
154 | |
155 // The known latest name owner of TestService. Updated in OnNameOwnerChanged. | |
156 std::string latest_name_owner_; | |
126 }; | 157 }; |
127 | 158 |
128 TEST_F(SignalSenderVerificationTest, TestSignalAccepted) { | 159 TEST_F(SignalSenderVerificationTest, TestSignalAccepted) { |
129 const char kMessage[] = "hello, world"; | 160 const char kMessage[] = "hello, world"; |
130 // Send the test signal from the exported object. | 161 // Send the test signal from the exported object. |
131 test_service_->SendTestSignal(kMessage); | 162 test_service_->SendTestSignal(kMessage); |
132 // Receive the signal with the object proxy. The signal is handled in | 163 // Receive the signal with the object proxy. The signal is handled in |
133 // SignalSenderVerificationTest::OnTestSignal() in the main thread. | 164 // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
134 WaitForTestSignal(); | 165 WaitForTestSignal(); |
135 ASSERT_EQ(kMessage, test_signal_string_); | 166 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. | 181 // Sleep to have message delivered to the client via the D-Bus service. |
151 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); | 182 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); |
152 | 183 |
153 scoped_ptr<base::HistogramSamples> samples2( | 184 scoped_ptr<base::HistogramSamples> samples2( |
154 reject_signal_histogram->SnapshotSamples()); | 185 reject_signal_histogram->SnapshotSamples()); |
155 | 186 |
156 ASSERT_EQ("", test_signal_string_); | 187 ASSERT_EQ("", test_signal_string_); |
157 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount()); | 188 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount()); |
158 } | 189 } |
159 | 190 |
160 TEST_F(SignalSenderVerificationTest, DISABLED_TestOwnerChanged) { | 191 TEST_F(SignalSenderVerificationTest, TestOwnerChanged) { |
161 const char kMessage[] = "hello, world"; | 192 const char kMessage[] = "hello, world"; |
162 | 193 |
163 // Send the test signal from the exported object. | 194 // Send the test signal from the exported object. |
164 test_service_->SendTestSignal(kMessage); | 195 test_service_->SendTestSignal(kMessage); |
165 // Receive the signal with the object proxy. The signal is handled in | 196 // Receive the signal with the object proxy. The signal is handled in |
166 // SignalSenderVerificationTest::OnTestSignal() in the main thread. | 197 // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
167 WaitForTestSignal(); | 198 WaitForTestSignal(); |
168 ASSERT_EQ(kMessage, test_signal_string_); | 199 ASSERT_EQ(kMessage, test_signal_string_); |
169 | 200 |
170 // Release and aquire the name ownership. | 201 // Release and aquire the name ownership. |
202 latest_name_owner_ = "dummyowner"; | |
satorux1
2012/11/13 06:32:03
this looks rather unclean. At this moment, test_se
Haruki Sato
2012/11/14 06:35:15
Done.
| |
171 test_service_->ShutdownAndBlock(); | 203 test_service_->ShutdownAndBlock(); |
172 test_service2_->RequestOwnership(); | 204 // OnNameOwnerChanged will PostTask to quit the message loop. |
205 message_loop_.Run(); | |
206 // latest_name_owner_ should be empty as the owner is gone. | |
207 ASSERT_TRUE(latest_name_owner_.empty()); | |
208 | |
209 test_service2_->RequestOwnership( | |
210 base::Bind(&SignalSenderVerificationTest::OnOwnership, | |
211 base::Unretained(this), true)); | |
212 // OnNameOwnerChanged will PostTask to quit the message loop. | |
satorux1
2012/11/13 06:32:03
OnNameOwnerChanged()
Let's add () for function na
Haruki Sato
2012/11/14 06:35:15
Done.
| |
213 message_loop_.Run(); | |
satorux1
2012/11/13 06:32:03
What about calling this twice:
message_loop_.Run(
Haruki Sato
2012/11/14 06:35:15
As discussed offline, I added more checks for call
| |
214 // latest_name_owner_ becomes non empty as the new owner appears. | |
215 ASSERT_FALSE(latest_name_owner_.empty()); | |
173 | 216 |
174 // Now the second service owns the name. | 217 // Now the second service owns the name. |
175 const char kNewMessage[] = "hello, new world"; | 218 const char kNewMessage[] = "hello, new world"; |
176 | 219 |
177 test_service2_->SendTestSignal(kNewMessage); | 220 test_service2_->SendTestSignal(kNewMessage); |
178 WaitForTestSignal(); | 221 WaitForTestSignal(); |
179 ASSERT_EQ(kNewMessage, test_signal_string_); | 222 ASSERT_EQ(kNewMessage, test_signal_string_); |
180 } | 223 } |
OLD | NEW |