3 * Copyright 2011, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "talk/base/dbus.h"
31 #include "talk/base/gunit.h"
32 #include "talk/base/thread.h"
36 #define SIG_NAME "NameAcquired"
38 static const uint32 kTimeoutMs = 5000U;
40 class DBusSigFilterTest : public DBusSigFilter {
42 // DBusSigFilterTest listens on DBus service itself for "NameAcquired" signal.
43 // This signal should be received when the application connects to DBus
44 // service and gains ownership of a name.
45 // http://dbus.freedesktop.org/doc/dbus-specification.html
47 : DBusSigFilter(GetFilter()),
48 message_received_(false) {
51 bool MessageReceived() {
52 return message_received_;
56 static std::string GetFilter() {
57 return talk_base::DBusSigFilter::BuildFilterString("", "", SIG_NAME);
60 // Implement virtual method of DBusSigFilter. On caller thread.
61 virtual void ProcessSignal(DBusMessage *message) {
62 EXPECT_TRUE(message != NULL);
63 message_received_ = true;
66 bool message_received_;
69 TEST(DBusMonitorTest, StartStopStartStop) {
70 DBusSigFilterTest filter;
71 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor;
72 monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
74 EXPECT_TRUE(monitor->AddFilter(&filter));
76 EXPECT_TRUE(monitor->StopMonitoring());
77 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_NOT_INITIALIZED);
79 EXPECT_TRUE(monitor->StartMonitoring());
80 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs);
81 EXPECT_TRUE(monitor->StopMonitoring());
82 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
83 EXPECT_TRUE(monitor->StopMonitoring());
84 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
86 EXPECT_TRUE(monitor->StartMonitoring());
87 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs);
88 EXPECT_TRUE(monitor->StartMonitoring());
89 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_RUNNING);
90 EXPECT_TRUE(monitor->StopMonitoring());
91 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
93 LOG(LS_WARNING) << "DBus Monitor not started. Skipping test.";
97 // DBusMonitorTest listens on DBus service itself for "NameAcquired" signal.
98 // This signal should be received when the application connects to DBus
99 // service and gains ownership of a name.
100 // This test is to make sure that we capture the "NameAcquired" signal.
101 TEST(DBusMonitorTest, ReceivedNameAcquiredSignal) {
102 DBusSigFilterTest filter;
103 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor;
104 monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
106 EXPECT_TRUE(monitor->AddFilter(&filter));
108 EXPECT_TRUE(monitor->StartMonitoring());
109 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs);
110 EXPECT_TRUE_WAIT(filter.MessageReceived(), kTimeoutMs);
111 EXPECT_TRUE(monitor->StopMonitoring());
112 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
114 LOG(LS_WARNING) << "DBus Monitor not started. Skipping test.";
118 TEST(DBusMonitorTest, ConcurrentMonitors) {
119 DBusSigFilterTest filter1;
120 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor1;
121 monitor1.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
123 EXPECT_TRUE(monitor1->AddFilter(&filter1));
124 DBusSigFilterTest filter2;
125 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor2;
126 monitor2.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
127 EXPECT_TRUE(monitor2->AddFilter(&filter2));
129 EXPECT_TRUE(monitor1->StartMonitoring());
130 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor1->GetStatus(), kTimeoutMs);
131 EXPECT_TRUE(monitor2->StartMonitoring());
132 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor2->GetStatus(), kTimeoutMs);
134 EXPECT_TRUE_WAIT(filter2.MessageReceived(), kTimeoutMs);
135 EXPECT_TRUE(monitor2->StopMonitoring());
136 EXPECT_EQ(monitor2->GetStatus(), DBusMonitor::DMS_STOPPED);
138 EXPECT_TRUE_WAIT(filter1.MessageReceived(), kTimeoutMs);
139 EXPECT_TRUE(monitor1->StopMonitoring());
140 EXPECT_EQ(monitor1->GetStatus(), DBusMonitor::DMS_STOPPED);
142 LOG(LS_WARNING) << "DBus Monitor not started. Skipping test.";
146 TEST(DBusMonitorTest, ConcurrentFilters) {
147 DBusSigFilterTest filter1;
148 DBusSigFilterTest filter2;
149 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor;
150 monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
152 EXPECT_TRUE(monitor->AddFilter(&filter1));
153 EXPECT_TRUE(monitor->AddFilter(&filter2));
155 EXPECT_TRUE(monitor->StartMonitoring());
156 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs);
158 EXPECT_TRUE_WAIT(filter1.MessageReceived(), kTimeoutMs);
159 EXPECT_TRUE_WAIT(filter2.MessageReceived(), kTimeoutMs);
161 EXPECT_TRUE(monitor->StopMonitoring());
162 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
164 LOG(LS_WARNING) << "DBus Monitor not started. Skipping test.";
168 TEST(DBusMonitorTest, NoAddFilterIfRunning) {
169 DBusSigFilterTest filter1;
170 DBusSigFilterTest filter2;
171 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor;
172 monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
174 EXPECT_TRUE(monitor->AddFilter(&filter1));
176 EXPECT_TRUE(monitor->StartMonitoring());
177 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs);
178 EXPECT_FALSE(monitor->AddFilter(&filter2));
180 EXPECT_TRUE(monitor->StopMonitoring());
181 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
183 LOG(LS_WARNING) << "DBus Monitor not started. Skipping test.";
187 TEST(DBusMonitorTest, AddFilterAfterStop) {
188 DBusSigFilterTest filter1;
189 DBusSigFilterTest filter2;
190 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor;
191 monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
193 EXPECT_TRUE(monitor->AddFilter(&filter1));
194 EXPECT_TRUE(monitor->StartMonitoring());
195 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs);
196 EXPECT_TRUE_WAIT(filter1.MessageReceived(), kTimeoutMs);
197 EXPECT_TRUE(monitor->StopMonitoring());
198 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
200 EXPECT_TRUE(monitor->AddFilter(&filter2));
201 EXPECT_TRUE(monitor->StartMonitoring());
202 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs);
203 EXPECT_TRUE_WAIT(filter1.MessageReceived(), kTimeoutMs);
204 EXPECT_TRUE_WAIT(filter2.MessageReceived(), kTimeoutMs);
205 EXPECT_TRUE(monitor->StopMonitoring());
206 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
208 LOG(LS_WARNING) << "DBus Monitor not started. Skipping test.";
212 TEST(DBusMonitorTest, StopRightAfterStart) {
213 DBusSigFilterTest filter;
214 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor;
215 monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
217 EXPECT_TRUE(monitor->AddFilter(&filter));
219 EXPECT_TRUE(monitor->StartMonitoring());
220 EXPECT_TRUE(monitor->StopMonitoring());
222 // Stop the monitoring thread right after it had been started.
223 // If the monitoring thread got a chance to receive a DBus signal, it would
224 // post a message to the main thread and signal the main thread wakeup.
225 // This message will be cleaned out automatically when the filter get
226 // destructed. Here we also consume the wakeup signal (if there is one) so
227 // that the testing (main) thread is reset to a clean state.
228 talk_base::Thread::Current()->ProcessMessages(1);
230 LOG(LS_WARNING) << "DBus Monitor not started.";
234 TEST(DBusSigFilter, BuildFilterString) {
235 EXPECT_EQ(DBusSigFilter::BuildFilterString("", "", ""),
236 (DBUS_TYPE "='" DBUS_SIGNAL "'"));
237 EXPECT_EQ(DBusSigFilter::BuildFilterString("p", "", ""),
238 (DBUS_TYPE "='" DBUS_SIGNAL "'," DBUS_PATH "='p'"));
239 EXPECT_EQ(DBusSigFilter::BuildFilterString("p","i", ""),
240 (DBUS_TYPE "='" DBUS_SIGNAL "'," DBUS_PATH "='p',"
241 DBUS_INTERFACE "='i'"));
242 EXPECT_EQ(DBusSigFilter::BuildFilterString("p","i","m"),
243 (DBUS_TYPE "='" DBUS_SIGNAL "'," DBUS_PATH "='p',"
244 DBUS_INTERFACE "='i'," DBUS_MEMBER "='m'"));
247 } // namespace talk_base
249 #endif // HAVE_DBUS_GLIB