OLD | NEW |
(Empty) | |
| 1 |
| 2 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 // Use of this source code is governed by a BSD-style license that can be |
| 4 // found in the LICENSE file. |
| 5 |
| 6 #include "base/bind.h" |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/metrics/histogram.h" |
| 10 #include "base/metrics/histogram_samples.h" |
| 11 #include "base/metrics/statistics_recorder.h" |
| 12 #include "base/test/test_timeouts.h" |
| 13 #include "base/threading/platform_thread.h" |
| 14 #include "base/threading/thread_restrictions.h" |
| 15 #include "dbus/bus.h" |
| 16 #include "dbus/message.h" |
| 17 #include "dbus/object_proxy.h" |
| 18 #include "dbus/test_service.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 // The test for sender verification in ObjectProxy. |
| 22 class SignalSenderVerificationTest : public testing::Test { |
| 23 public: |
| 24 SignalSenderVerificationTest() { |
| 25 } |
| 26 |
| 27 virtual void SetUp() { |
| 28 base::StatisticsRecorder::Initialize(); |
| 29 |
| 30 // Make the main thread not to allow IO. |
| 31 base::ThreadRestrictions::SetIOAllowed(false); |
| 32 |
| 33 // Start the D-Bus thread. |
| 34 dbus_thread_.reset(new base::Thread("D-Bus Thread")); |
| 35 base::Thread::Options thread_options; |
| 36 thread_options.message_loop_type = MessageLoop::TYPE_IO; |
| 37 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); |
| 38 |
| 39 // Start the test service, using the D-Bus thread. |
| 40 dbus::TestService::Options options; |
| 41 options.dbus_thread_message_loop_proxy = dbus_thread_->message_loop_proxy(); |
| 42 test_service_.reset(new dbus::TestService(options)); |
| 43 ASSERT_TRUE(test_service_->StartService()); |
| 44 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); |
| 45 ASSERT_TRUE(test_service_->HasDBusThread()); |
| 46 |
| 47 // Same setup for the second TestService. This service should not have the |
| 48 // ownership of the name at this point. |
| 49 test_service2_.reset(new dbus::TestService(options)); |
| 50 ASSERT_TRUE(test_service2_->StartService()); |
| 51 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted()); |
| 52 ASSERT_TRUE(test_service2_->HasDBusThread()); |
| 53 |
| 54 // Create the client, using the D-Bus thread. |
| 55 dbus::Bus::Options bus_options; |
| 56 bus_options.bus_type = dbus::Bus::SESSION; |
| 57 bus_options.connection_type = dbus::Bus::PRIVATE; |
| 58 bus_options.dbus_thread_message_loop_proxy = |
| 59 dbus_thread_->message_loop_proxy(); |
| 60 bus_ = new dbus::Bus(bus_options); |
| 61 object_proxy_ = bus_->GetObjectProxy( |
| 62 "org.chromium.TestService", |
| 63 dbus::ObjectPath("/org/chromium/TestObject")); |
| 64 ASSERT_TRUE(bus_->HasDBusThread()); |
| 65 |
| 66 // Connect to the "Test" signal of "org.chromium.TestInterface" from |
| 67 // the remote object. |
| 68 object_proxy_->ConnectToSignal( |
| 69 "org.chromium.TestInterface", |
| 70 "Test", |
| 71 base::Bind(&SignalSenderVerificationTest::OnTestSignal, |
| 72 base::Unretained(this)), |
| 73 base::Bind(&SignalSenderVerificationTest::OnConnected, |
| 74 base::Unretained(this))); |
| 75 // Wait until the object proxy is connected to the signal. |
| 76 message_loop_.Run(); |
| 77 } |
| 78 |
| 79 virtual void TearDown() { |
| 80 bus_->ShutdownOnDBusThreadAndBlock(); |
| 81 |
| 82 // Shut down the service. |
| 83 test_service_->ShutdownAndBlock(); |
| 84 test_service2_->ShutdownAndBlock(); |
| 85 |
| 86 // Reset to the default. |
| 87 base::ThreadRestrictions::SetIOAllowed(true); |
| 88 |
| 89 // Stopping a thread is considered an IO operation, so do this after |
| 90 // allowing IO. |
| 91 test_service_->Stop(); |
| 92 test_service2_->Stop(); |
| 93 } |
| 94 |
| 95 protected: |
| 96 |
| 97 // Called when the "Test" signal is received, in the main thread. |
| 98 // Copy the string payload to |test_signal_string_|. |
| 99 void OnTestSignal(dbus::Signal* signal) { |
| 100 dbus::MessageReader reader(signal); |
| 101 ASSERT_TRUE(reader.PopString(&test_signal_string_)); |
| 102 message_loop_.Quit(); |
| 103 } |
| 104 |
| 105 // Called when connected to the signal. |
| 106 void OnConnected(const std::string& interface_name, |
| 107 const std::string& signal_name, |
| 108 bool success) { |
| 109 ASSERT_TRUE(success); |
| 110 message_loop_.Quit(); |
| 111 } |
| 112 |
| 113 // Wait for the hey signal to be received. |
| 114 void WaitForTestSignal() { |
| 115 // OnTestSignal() will quit the message loop. |
| 116 message_loop_.Run(); |
| 117 } |
| 118 |
| 119 MessageLoop message_loop_; |
| 120 scoped_ptr<base::Thread> dbus_thread_; |
| 121 scoped_refptr<dbus::Bus> bus_; |
| 122 dbus::ObjectProxy* object_proxy_; |
| 123 scoped_ptr<dbus::TestService> test_service_; |
| 124 scoped_ptr<dbus::TestService> test_service2_; |
| 125 // Text message from "Test" signal. |
| 126 std::string test_signal_string_; |
| 127 }; |
| 128 |
| 129 TEST_F(SignalSenderVerificationTest, TestSignalAccepted) { |
| 130 const char kMessage[] = "hello, world"; |
| 131 // Send the test signal from the exported object. |
| 132 test_service_->SendTestSignal(kMessage); |
| 133 // Receive the signal with the object proxy. The signal is handled in |
| 134 // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
| 135 WaitForTestSignal(); |
| 136 ASSERT_EQ(kMessage, test_signal_string_); |
| 137 } |
| 138 |
| 139 TEST_F(SignalSenderVerificationTest, TestSignalRejected) { |
| 140 // To make sure the histogram instance is created. |
| 141 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 0); |
| 142 base::Histogram* r_histogram = |
| 143 base::StatisticsRecorder::FindHistogram("DBus.RejectedSignalCount"); |
| 144 scoped_ptr<base::HistogramSamples> samples1( |
| 145 r_histogram->SnapshotSamples()); |
| 146 |
| 147 const char kNewMessage[] = "hello, new world"; |
| 148 test_service2_->SendTestSignal(kNewMessage); |
| 149 |
| 150 // This test tests that our callback is NOT called by the ObjectProxy. |
| 151 // We are giving some D-Bus service and thread and |
| 152 // Sleep to have message delivered to the client via the D-Bus service. |
| 153 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 154 // Let the callbacks handle responses (the test message and GetNameOwner |
| 155 // result). This may call GetNameOwner to retrieve the name owner. |
| 156 message_loop_.RunUntilIdle(); |
| 157 // Sleep again to wait for GetNameOwner response and handle the pending test |
| 158 // message. |
| 159 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 160 |
| 161 scoped_ptr<base::HistogramSamples> samples2( |
| 162 r_histogram->SnapshotSamples()); |
| 163 |
| 164 ASSERT_EQ("", test_signal_string_); |
| 165 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount()); |
| 166 } |
| 167 |
| 168 TEST_F(SignalSenderVerificationTest, TestOwnerChanged) { |
| 169 const char kMessage[] = "hello, world"; |
| 170 |
| 171 // Send the test signal from the exported object. |
| 172 test_service_->SendTestSignal(kMessage); |
| 173 // Receive the signal with the object proxy. The signal is handled in |
| 174 // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
| 175 WaitForTestSignal(); |
| 176 ASSERT_EQ(kMessage, test_signal_string_); |
| 177 |
| 178 // Release and aquire the name ownership. |
| 179 test_service_->ShutdownAndBlock(); |
| 180 test_service2_->RequestOwnership(); |
| 181 |
| 182 // Now the second service owns the name. |
| 183 const char kNewMessage[] = "hello, new world"; |
| 184 |
| 185 test_service2_->SendTestSignal(kNewMessage); |
| 186 WaitForTestSignal(); |
| 187 ASSERT_EQ(kNewMessage, test_signal_string_); |
| 188 } |
OLD | NEW |