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