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