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 <algorithm> | |
6 //#include <string> | |
7 //#include <vector> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/message_loop.h" | |
12 #include "base/metrics/histogram.h" | |
13 #include "base/metrics/histogram_samples.h" | |
14 #include "base/metrics/statistics_recorder.h" | |
15 #include "base/threading/thread_restrictions.h" | |
16 #include "dbus/bus.h" | |
17 #include "dbus/message.h" | |
18 #include "dbus/object_proxy.h" | |
19 #include "dbus/test_service.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 namespace { | |
23 | |
24 // See comments in ObjectProxy::RunResponseCallback() for why the number was | |
25 // chosen. | |
26 const int kHugePayloadSize = 64 << 20; // 64 MB | |
27 | |
28 } // namespace | |
29 | |
30 // The test to for sender verification in ObjectProxy. | |
31 class SignalSenderVerificationTest : public testing::Test { | |
32 public: | |
33 SignalSenderVerificationTest() { | |
34 } | |
35 | |
36 virtual void SetUp() { | |
37 base::StatisticsRecorder::Initialize(); | |
38 | |
39 // Make the main thread not to allow IO. | |
40 base::ThreadRestrictions::SetIOAllowed(false); | |
41 | |
42 // Start the D-Bus thread. | |
43 dbus_thread_.reset(new base::Thread("D-Bus Thread")); | |
44 base::Thread::Options thread_options; | |
45 thread_options.message_loop_type = MessageLoop::TYPE_IO; | |
46 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); | |
47 | |
48 // Start the test service, using the D-Bus thread. | |
49 dbus::TestService::Options options; | |
50 options.dbus_thread_message_loop_proxy = dbus_thread_->message_loop_proxy(); | |
51 test_service_.reset(new dbus::TestService(options)); | |
52 ASSERT_TRUE(test_service_->StartService()); | |
53 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); | |
54 ASSERT_TRUE(test_service_->HasDBusThread()); | |
55 | |
56 // Same setup for the second TestService. This service should not have the | |
57 // ownership of the name at this point. | |
58 test_service2_.reset(new dbus::TestService(options)); | |
59 ASSERT_TRUE(test_service2_->StartService()); | |
60 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted()); | |
61 ASSERT_TRUE(test_service2_->HasDBusThread()); | |
62 | |
63 // Create the client, using the D-Bus thread. | |
64 dbus::Bus::Options bus_options; | |
65 bus_options.bus_type = dbus::Bus::SESSION; | |
66 bus_options.connection_type = dbus::Bus::PRIVATE; | |
67 bus_options.dbus_thread_message_loop_proxy = | |
68 dbus_thread_->message_loop_proxy(); | |
69 bus_ = new dbus::Bus(bus_options); | |
70 object_proxy_ = bus_->GetObjectProxy( | |
71 "org.chromium.TestService", | |
72 dbus::ObjectPath("/org/chromium/TestObject")); | |
73 ASSERT_TRUE(bus_->HasDBusThread()); | |
74 | |
75 // Connect to the "Test" signal of "org.chromium.TestInterface" from | |
76 // the remote object. | |
77 object_proxy_->ConnectToSignal( | |
78 "org.chromium.TestInterface", | |
79 "Test", | |
80 base::Bind(&SignalSenderVerificationTest::OnTestSignal, | |
81 base::Unretained(this)), | |
82 base::Bind(&SignalSenderVerificationTest::OnConnected, | |
83 base::Unretained(this))); | |
84 // Wait until the object proxy is connected to the signal. | |
85 message_loop_.Run(); | |
86 } | |
87 | |
88 virtual void TearDown() { | |
89 bus_->ShutdownOnDBusThreadAndBlock(); | |
90 | |
91 // Shut down the service. | |
92 test_service_->ShutdownAndBlock(); | |
93 test_service2_->ShutdownAndBlock(); | |
94 | |
95 // Reset to the default. | |
96 base::ThreadRestrictions::SetIOAllowed(true); | |
97 | |
98 // Stopping a thread is considered an IO operation, so do this after | |
99 // allowing IO. | |
100 test_service_->Stop(); | |
101 test_service2_->Stop(); | |
102 } | |
103 | |
104 protected: | |
105 | |
106 // Called when the "Test" signal is received, in the main thread. | |
107 // Copy the string payload to |test_signal_string_|. | |
108 void OnTestSignal(dbus::Signal* signal) { | |
109 dbus::MessageReader reader(signal); | |
110 ASSERT_TRUE(reader.PopString(&test_signal_string_)); | |
111 message_loop_.Quit(); | |
112 } | |
113 | |
114 // Called when connected to the signal. | |
115 void OnConnected(const std::string& interface_name, | |
116 const std::string& signal_name, | |
117 bool success) { | |
118 ASSERT_TRUE(success); | |
119 message_loop_.Quit(); | |
120 } | |
121 | |
122 // Wait for the hey signal to be received. | |
123 void WaitForTestSignal() { | |
124 // OnTestSignal() will quit the message loop. | |
125 message_loop_.Run(); | |
126 } | |
127 | |
128 MessageLoop message_loop_; | |
129 scoped_ptr<base::Thread> dbus_thread_; | |
130 scoped_refptr<dbus::Bus> bus_; | |
131 dbus::ObjectProxy* object_proxy_; | |
132 scoped_ptr<dbus::TestService> test_service_; | |
133 scoped_ptr<dbus::TestService> test_service2_; | |
134 // Text message from "Test" signal. | |
135 std::string test_signal_string_; | |
136 }; | |
137 | |
138 TEST_F(SignalSenderVerificationTest, TestSignal) { | |
satorux1
2012/10/22 05:23:32
Please split this into two separate test cases:
S
Haruki Sato
2012/10/24 08:28:05
Done.
| |
139 const char kMessage[] = "hello, world"; | |
140 // Send the test signal from the exported object. | |
141 test_service_->SendTestSignal(kMessage); | |
142 // Receive the signal with the object proxy. The signal is handled in | |
143 // SignalSenderVerificationTest::OnTestSignal() in the main thread. | |
144 WaitForTestSignal(); | |
145 ASSERT_EQ(kMessage, test_signal_string_); | |
146 | |
147 // To make sure the histogram instance is created. | |
148 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 0); | |
149 | |
150 base::Histogram* r_histogram = | |
151 base::StatisticsRecorder::FindHistogram("DBus.RejectedSignalCount"); | |
152 | |
153 scoped_ptr<base::HistogramSamples> samples1( | |
154 r_histogram->SnapshotSamples()); | |
155 | |
156 const char kNewMessage[] = "hello, new world"; | |
157 test_service2_->SendTestSignal(kNewMessage); | |
158 | |
159 sleep(1); | |
satorux1
2012/10/22 04:50:42
We shouldn't use sleep() in tests. Can we remove t
satorux1
2012/10/22 05:23:32
We should probably use
base::PlatformThread::Sle
Haruki Sato
2012/10/24 08:28:05
Done. This is the best I can think of for now.
Ple
| |
160 | |
161 scoped_ptr<base::HistogramSamples> samples2( | |
162 r_histogram->SnapshotSamples()); | |
163 | |
164 ASSERT_EQ(kMessage, 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 } | |
189 | |
190 // This is an identical copy of EndToEndAsyncTest.TestHugeSignal | |
191 // TODO(haruki): This does not pass somehow. Investigate! | |
192 // GetNameOwner does not reply (get org.freedesktop.DBus.Error.NoReply) and | |
193 // NameOwnerChanged does not come. | |
satorux1
2012/10/22 05:23:32
GetNameOwner() is calling a method synchronously a
Haruki Sato
2012/10/24 08:28:05
Thank you for the advice. As discussed offline, I'
| |
194 TEST_F(SignalSenderVerificationTest, TestHugeSignal) { | |
195 const std::string kHugeMessage(kHugePayloadSize, 'o'); | |
196 | |
197 // Send the huge signal from the exported object. | |
198 test_service_->SendTestSignal(kHugeMessage); | |
199 // This caused a DCHECK failure before. Ensure that the issue is fixed. | |
200 WaitForTestSignal(); | |
201 ASSERT_EQ(kHugeMessage, test_signal_string_); | |
202 } | |
OLD | NEW |