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_
8 #include "base/compiler_specific.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/threading/thread.h"
11 #include "base/synchronization/waitable_event.h"
13 #include "dbus/exported_object.h"
16 class SequencedTaskRunner;
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::SequencedTaskRunner> dbus_task_runner;
41 // Flags governing parameters of service ownership request.
42 Bus::ServiceOwnershipOptions request_ownership_options;
44 // Name of this service (randomly generated name will be used if empty).
45 std::string service_name;
48 // The number of methods we'll export.
49 static const int kNumMethodsToExport;
51 explicit TestService(const Options& options);
52 ~TestService() override;
54 // Starts the service in a separate thread.
55 // Returns true if the thread is started successfully.
58 // Waits until the service is started (i.e. all methods are exported).
59 // Returns true on success.
60 bool WaitUntilServiceIsStarted() WARN_UNUSED_RESULT;
62 // Shuts down the service and blocks until it's done.
63 void ShutdownAndBlock();
65 // Returns true if the bus has the D-Bus thread.
68 // Sends "Test" signal with the given message from the exported object.
69 void SendTestSignal(const std::string& message);
71 // Sends "Test" signal with the given message from the root object ("/").
72 // This function emulates dbus-send's behavior.
73 void SendTestSignalFromRoot(const std::string& message);
75 // Request the ownership of a well-known name "TestService".
76 // |callback| will be called with the result when an ownership request is
78 void RequestOwnership(base::Callback<void(bool)> callback);
80 // Release the ownership of the well-known name "TestService".
81 // |callback| will be called when the ownership has been released.
82 void ReleaseOwnership(base::Closure callback);
84 // Returns the name of this service.
85 const std::string& service_name() const { return service_name_; }
87 // Returns whether this instance has the name ownership or not.
88 bool has_ownership() const { return has_ownership_; }
91 // Helper function for SendTestSignal().
92 void SendTestSignalInternal(const std::string& message);
94 // Helper function for SendTestSignalFromRoot.
95 void SendTestSignalFromRootInternal(const std::string& message);
97 // Helper function for ShutdownAndBlock().
98 void ShutdownAndBlockInternal();
100 // Called when an ownership request is completed.
101 // |callback| is the callback to be called with the result. |service_name| is
102 // the requested well-known bus name. |callback| and |service_name| are bound
103 // when the service requests the ownership. |success| is the result of the
104 // completed request, and is propagated to |callback|.
105 void OnOwnership(base::Callback<void(bool)> callback,
106 const std::string& service_name,
109 // Called when a method is exported.
110 void OnExported(const std::string& interface_name,
111 const std::string& method_name,
114 // base::Thread override.
115 void Run(base::RunLoop* run_loop) override;
121 // Echos the text message received from the method call.
122 void Echo(MethodCall* method_call,
123 dbus::ExportedObject::ResponseSender response_sender);
125 // Echos the text message received from the method call, but sleeps for
126 // TestTimeouts::tiny_timeout_ms() before returning the response.
127 void SlowEcho(MethodCall* method_call,
128 dbus::ExportedObject::ResponseSender response_sender);
130 // Echos the text message received from the method call, but sends its
131 // response asynchronously after this callback has returned.
132 void AsyncEcho(MethodCall* method_call,
133 dbus::ExportedObject::ResponseSender response_sender);
135 // Returns NULL, instead of a valid Response.
136 void BrokenMethod(MethodCall* method_call,
137 dbus::ExportedObject::ResponseSender response_sender);
139 // Returns a set of property values for testing.
140 void GetAllProperties(MethodCall* method_call,
141 dbus::ExportedObject::ResponseSender response_sender);
143 // Returns a new value of 20 for the Version property when called.
144 void GetProperty(MethodCall* method_call,
145 dbus::ExportedObject::ResponseSender response_sender);
147 // Allows the name property to be changed, errors otherwise.
148 void SetProperty(MethodCall* method_call,
149 dbus::ExportedObject::ResponseSender response_sender);
151 // Performs an action for testing.
152 void PerformAction(MethodCall* method_call,
153 dbus::ExportedObject::ResponseSender response_sender);
155 // Object Manager: returns the set of objects and properties.
156 void GetManagedObjects(MethodCall* method_call,
157 dbus::ExportedObject::ResponseSender response_sender);
159 // Add a properties dictionary to a message writer.
160 void AddPropertiesToWriter(MessageWriter* writer);
162 // Add a new object to the manager.
163 void AddObject(const dbus::ObjectPath& object_path);
164 void AddObjectInternal(const dbus::ObjectPath& object_path);
166 // Remove an object from the manager.
167 void RemoveObject(const dbus::ObjectPath& object_path);
168 void RemoveObjectInternal(const dbus::ObjectPath& object_path);
170 // Sends a property changed signal for the name property.
171 void SendPropertyChangedSignal(const std::string& name);
173 // Helper function for SendPropertyChangedSignal().
174 void SendPropertyChangedSignalInternal(const std::string& name);
176 // Sends a property invalidated signal for the name property.
177 void SendPropertyInvalidatedSignal();
179 // Helper function for SendPropertyInvalidatedSignal().
180 void SendPropertyInvalidatedSignalInternal();
182 // Helper function for RequestOwnership().
183 void RequestOwnershipInternal(base::Callback<void(bool)> callback);
185 // Helper function for ReleaseOwnership().
186 void ReleaseOwnershipInternal(base::Closure callback);
188 // Configures the test service to send a PropertiesChanged signal for the
189 // "Name" property immediately after a call to GetManagedObjects.
190 void SetSendImmediatePropertiesChanged();
192 // Sends the response on completion of the performed action.
193 void PerformActionResponse(
194 MethodCall* method_call,
195 dbus::ExportedObject::ResponseSender response_sender);
197 // Re-requests ownership of the well-known name after releasing it.
198 void OwnershipReleased(
199 MethodCall* method_call,
200 dbus::ExportedObject::ResponseSender response_sender);
202 // Sends the action response after regaining the well-known name.
203 void OwnershipRegained(
204 MethodCall* method_call,
205 dbus::ExportedObject::ResponseSender response_sender,
208 // Name of this service.
209 std::string service_name_;
211 // Options to use when requesting service ownership.
212 Bus::ServiceOwnershipOptions request_ownership_options_;
214 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
215 base::WaitableEvent on_name_obtained_;
216 // The number of methods actually exported.
217 int num_exported_methods_;
219 // True if a PropertiesChanged signal for the "Name" property should be sent
220 // immediately following a call to GetManagedObjects.
221 bool send_immediate_properties_changed_;
223 // True iff this instance has successfully acquired the name ownership.
226 scoped_refptr<Bus> bus_;
227 ExportedObject* exported_object_;
228 ExportedObject* exported_object_manager_;
233 #endif // DBUS_TEST_SERVICE_H_