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.
5 #ifndef DBUS_TEST_SERVICE_H_
6 #define DBUS_TEST_SERVICE_H_
9 #include "base/compiler_specific.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/threading/thread.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "dbus/exported_object.h"
16 class MessageLoopProxy
;
25 // The test service is used for end-to-end tests. The service runs in a
26 // separate thread, so it does not interfere the test code that runs in
29 // The test service exports an object with methods such as Echo() and
30 // SlowEcho(). The object has ability to send "Test" signal.
31 class TestService
: public base::Thread
{
33 // Options for the test service.
38 // NULL by default (i.e. don't use the D-Bus thread).
39 scoped_refptr
<base::MessageLoopProxy
> dbus_thread_message_loop_proxy
;
42 // The number of methods we'll export.
43 static const int kNumMethodsToExport
;
45 TestService(const Options
& options
);
46 virtual ~TestService();
48 // Starts the service in a separate thread.
49 // Returns true if the thread is started successfully.
52 // Waits until the service is started (i.e. all methods are exported).
53 // Returns true on success.
54 bool WaitUntilServiceIsStarted() WARN_UNUSED_RESULT
;
56 // Shuts down the service and blocks until it's done.
57 void ShutdownAndBlock();
59 // Returns true if the bus has the D-Bus thread.
62 // Sends "Test" signal with the given message from the exported object.
63 void SendTestSignal(const std::string
& message
);
65 // Sends "Test" signal with the given message from the root object ("/").
66 // This function emulates dbus-send's behavior.
67 void SendTestSignalFromRoot(const std::string
& message
);
70 // Helper function for SendTestSignal().
71 void SendTestSignalInternal(const std::string
& message
);
73 // Helper function for SendTestSignalFromRoot.
74 void SendTestSignalFromRootInternal(const std::string
& message
);
76 // Helper function for ShutdownAndBlock().
77 void ShutdownAndBlockInternal();
79 // Called when a method is exported.
80 void OnExported(const std::string
& interface_name
,
81 const std::string
& method_name
,
84 // base::Thread override.
85 virtual void Run(MessageLoop
* message_loop
) OVERRIDE
;
91 // Echos the text message received from the method call.
92 void Echo(MethodCall
* method_call
,
93 dbus::ExportedObject::ResponseSender response_sender
);
95 // Echos the text message received from the method call, but sleeps for
96 // TestTimeouts::tiny_timeout_ms() before returning the response.
97 void SlowEcho(MethodCall
* method_call
,
98 dbus::ExportedObject::ResponseSender response_sender
);
100 // Echos the text message received from the method call, but sends its
101 // response asynchronously after this callback has returned.
102 void AsyncEcho(MethodCall
* method_call
,
103 dbus::ExportedObject::ResponseSender response_sender
);
105 // Returns NULL, instead of a valid Response.
106 void BrokenMethod(MethodCall
* method_call
,
107 dbus::ExportedObject::ResponseSender response_sender
);
109 // Returns a set of property values for testing.
110 void GetAllProperties(MethodCall
* method_call
,
111 dbus::ExportedObject::ResponseSender response_sender
);
113 // Returns a new value of 20 for the Version property when called.
114 void GetProperty(MethodCall
* method_call
,
115 dbus::ExportedObject::ResponseSender response_sender
);
117 // Allows the name property to be changed, errors otherwise.
118 void SetProperty(MethodCall
* method_call
,
119 dbus::ExportedObject::ResponseSender response_sender
);
121 // Sends a property changed signal for the name property.
122 void SendPropertyChangedSignal(const std::string
& name
);
124 // Helper function for SendPropertyChangedSignal().
125 void SendPropertyChangedSignalInternal(const std::string
& name
);
127 scoped_refptr
<base::MessageLoopProxy
> dbus_thread_message_loop_proxy_
;
128 base::WaitableEvent on_all_methods_exported_
;
129 // The number of methods actually exported.
130 int num_exported_methods_
;
132 scoped_refptr
<Bus
> bus_
;
133 ExportedObject
* exported_object_
;
138 #endif // DBUS_TEST_SERVICE_H_