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 message_loop_.PostTask( | |
96 FROM_HERE, | |
97 base::Bind(&SignalSenderVerificationTest::QuitMessageLoop, | |
98 base::Unretained(this))); | |
satorux1
2012/11/12 07:56:43
Cannot we call QuitMessageLoop() directly here? Ot
Haruki Sato
2012/11/13 06:23:28
Done.
| |
99 } | |
100 | |
78 virtual void TearDown() { | 101 virtual void TearDown() { |
79 bus_->ShutdownOnDBusThreadAndBlock(); | 102 bus_->ShutdownOnDBusThreadAndBlock(); |
80 | 103 |
81 // Shut down the service. | 104 // Shut down the service. |
82 test_service_->ShutdownAndBlock(); | 105 test_service_->ShutdownAndBlock(); |
83 test_service2_->ShutdownAndBlock(); | 106 test_service2_->ShutdownAndBlock(); |
84 | 107 |
85 // Reset to the default. | 108 // Reset to the default. |
86 base::ThreadRestrictions::SetIOAllowed(true); | 109 base::ThreadRestrictions::SetIOAllowed(true); |
87 | 110 |
(...skipping 20 matching lines...) Expand all Loading... | |
108 ASSERT_TRUE(success); | 131 ASSERT_TRUE(success); |
109 message_loop_.Quit(); | 132 message_loop_.Quit(); |
110 } | 133 } |
111 | 134 |
112 // Wait for the hey signal to be received. | 135 // Wait for the hey signal to be received. |
113 void WaitForTestSignal() { | 136 void WaitForTestSignal() { |
114 // OnTestSignal() will quit the message loop. | 137 // OnTestSignal() will quit the message loop. |
115 message_loop_.Run(); | 138 message_loop_.Run(); |
116 } | 139 } |
117 | 140 |
141 void QuitMessageLoop() { | |
142 message_loop_.Quit(); | |
143 } | |
144 | |
118 MessageLoop message_loop_; | 145 MessageLoop message_loop_; |
119 scoped_ptr<base::Thread> dbus_thread_; | 146 scoped_ptr<base::Thread> dbus_thread_; |
120 scoped_refptr<dbus::Bus> bus_; | 147 scoped_refptr<dbus::Bus> bus_; |
121 dbus::ObjectProxy* object_proxy_; | 148 dbus::ObjectProxy* object_proxy_; |
122 scoped_ptr<dbus::TestService> test_service_; | 149 scoped_ptr<dbus::TestService> test_service_; |
123 scoped_ptr<dbus::TestService> test_service2_; | 150 scoped_ptr<dbus::TestService> test_service2_; |
124 // Text message from "Test" signal. | 151 // Text message from "Test" signal. |
125 std::string test_signal_string_; | 152 std::string test_signal_string_; |
153 | |
154 // The known latest name owner of TestService. Updated in OnNameOwnerChanged. | |
155 std::string latest_name_owner_; | |
126 }; | 156 }; |
127 | 157 |
128 TEST_F(SignalSenderVerificationTest, TestSignalAccepted) { | 158 TEST_F(SignalSenderVerificationTest, TestSignalAccepted) { |
129 const char kMessage[] = "hello, world"; | 159 const char kMessage[] = "hello, world"; |
130 // Send the test signal from the exported object. | 160 // Send the test signal from the exported object. |
131 test_service_->SendTestSignal(kMessage); | 161 test_service_->SendTestSignal(kMessage); |
132 // Receive the signal with the object proxy. The signal is handled in | 162 // Receive the signal with the object proxy. The signal is handled in |
133 // SignalSenderVerificationTest::OnTestSignal() in the main thread. | 163 // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
134 WaitForTestSignal(); | 164 WaitForTestSignal(); |
135 ASSERT_EQ(kMessage, test_signal_string_); | 165 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. | 180 // Sleep to have message delivered to the client via the D-Bus service. |
151 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); | 181 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); |
152 | 182 |
153 scoped_ptr<base::HistogramSamples> samples2( | 183 scoped_ptr<base::HistogramSamples> samples2( |
154 reject_signal_histogram->SnapshotSamples()); | 184 reject_signal_histogram->SnapshotSamples()); |
155 | 185 |
156 ASSERT_EQ("", test_signal_string_); | 186 ASSERT_EQ("", test_signal_string_); |
157 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount()); | 187 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount()); |
158 } | 188 } |
159 | 189 |
160 TEST_F(SignalSenderVerificationTest, DISABLED_TestOwnerChanged) { | 190 TEST_F(SignalSenderVerificationTest, TestOwnerChanged) { |
161 const char kMessage[] = "hello, world"; | 191 const char kMessage[] = "hello, world"; |
162 | 192 |
163 // Send the test signal from the exported object. | 193 // Send the test signal from the exported object. |
164 test_service_->SendTestSignal(kMessage); | 194 test_service_->SendTestSignal(kMessage); |
165 // Receive the signal with the object proxy. The signal is handled in | 195 // Receive the signal with the object proxy. The signal is handled in |
166 // SignalSenderVerificationTest::OnTestSignal() in the main thread. | 196 // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
167 WaitForTestSignal(); | 197 WaitForTestSignal(); |
168 ASSERT_EQ(kMessage, test_signal_string_); | 198 ASSERT_EQ(kMessage, test_signal_string_); |
169 | 199 |
170 // Release and aquire the name ownership. | 200 // Release and aquire the name ownership. |
201 latest_name_owner_ = "dummyowner"; | |
171 test_service_->ShutdownAndBlock(); | 202 test_service_->ShutdownAndBlock(); |
172 test_service2_->RequestOwnership(); | 203 // OnNameOwnerChanged will PostTask to quit the message loop. |
204 message_loop_.Run(); | |
205 ASSERT_TRUE(latest_name_owner_.empty()); | |
satorux1
2012/11/12 07:56:43
Add a comment like:
// latest_name_owner_ becomes
Haruki Sato
2012/11/13 06:23:28
Done.
| |
206 | |
207 test_service2_->RequestOwnership( | |
208 base::Bind(&SignalSenderVerificationTest::OnOwnership, | |
209 base::Unretained(this), true)); | |
210 // OnNameOwnerChanged will PostTask to quit the message loop. | |
211 message_loop_.Run(); | |
212 ASSERT_FALSE(latest_name_owner_.empty()); | |
satorux1
2012/11/12 07:56:43
// latest_name_owner_ becomes non empty as the new
Haruki Sato
2012/11/13 06:23:28
Added comment.
| |
173 | 213 |
174 // Now the second service owns the name. | 214 // Now the second service owns the name. |
175 const char kNewMessage[] = "hello, new world"; | 215 const char kNewMessage[] = "hello, new world"; |
176 | 216 |
177 test_service2_->SendTestSignal(kNewMessage); | 217 test_service2_->SendTestSignal(kNewMessage); |
178 WaitForTestSignal(); | 218 WaitForTestSignal(); |
179 ASSERT_EQ(kNewMessage, test_signal_string_); | 219 ASSERT_EQ(kNewMessage, test_signal_string_); |
180 } | 220 } |
OLD | NEW |