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 void WaitUntilServiceIsStarted();
61 // Shuts down the service and blocks until it's done.
62 void ShutdownAndBlock();
64 // Returns true if the bus has the D-Bus thread.
67 // Sends "Test" signal with the given message from the exported object.
68 void SendTestSignal(const std::string& message);
70 // Sends "Test" signal with the given message from the root object ("/").
71 // This function emulates dbus-send's behavior.
72 void SendTestSignalFromRoot(const std::string& message);
74 // Request the ownership of a well-known name "TestService".
75 // |callback| will be called with the result when an ownership request is
77 void RequestOwnership(base::OnceCallback<void(bool)> callback);
79 // Release the ownership of the well-known name "TestService".
80 // |callback| will be called when the ownership has been released.
81 void ReleaseOwnership(base::OnceClosure callback);
83 // Returns the name of this service.
84 const std::string& service_name() const { return service_name_; }
86 // Returns whether this instance has the name ownership or not.
87 bool has_ownership() const { return has_ownership_; }
90 // Helper function for SendTestSignal().
91 void SendTestSignalInternal(const std::string& message);
93 // Helper function for SendTestSignalFromRoot.
94 void SendTestSignalFromRootInternal(const std::string& message);
96 // Helper function for ShutdownAndBlock().
97 void ShutdownAndBlockInternal();
99 // Called when an ownership request is completed.
100 // |callback| is the callback to be called with the result. |service_name| is
101 // the requested well-known bus name. |callback| and |service_name| are bound
102 // when the service requests the ownership. |success| is the result of the
103 // completed request, and is propagated to |callback|.
104 void OnOwnership(base::OnceCallback<void(bool)> callback,
105 const std::string& service_name,
108 // Called when a method is exported.
109 void OnExported(const std::string& interface_name,
110 const std::string& method_name,
113 // base::Thread override.
114 void Run(base::RunLoop* run_loop) override;
120 // Echos the text message received from the method call.
121 void Echo(MethodCall* method_call,
122 dbus::ExportedObject::ResponseSender response_sender);
124 // Echos the text message received from the method call, but sleeps for
125 // TestTimeouts::tiny_timeout_ms() before returning the response.
126 void SlowEcho(MethodCall* method_call,
127 dbus::ExportedObject::ResponseSender response_sender);
129 // Echos the text message received from the method call, but sends its
130 // response asynchronously after this callback has returned.
131 void AsyncEcho(MethodCall* method_call,
132 dbus::ExportedObject::ResponseSender response_sender);
134 // Returns NULL, instead of a valid Response.
135 void BrokenMethod(MethodCall* method_call,
136 dbus::ExportedObject::ResponseSender response_sender);
138 // Returns a set of property values for testing.
139 void GetAllProperties(MethodCall* method_call,
140 dbus::ExportedObject::ResponseSender response_sender);
142 // Returns a new value of 20 for the Version property when called.
143 void GetProperty(MethodCall* method_call,
144 dbus::ExportedObject::ResponseSender response_sender);
146 // Allows the name property to be changed, errors otherwise.
147 void SetProperty(MethodCall* method_call,
148 dbus::ExportedObject::ResponseSender response_sender);
150 // Performs an action for testing.
151 void PerformAction(MethodCall* method_call,
152 dbus::ExportedObject::ResponseSender response_sender);
154 // Object Manager: returns the set of objects and properties.
155 void GetManagedObjects(MethodCall* method_call,
156 dbus::ExportedObject::ResponseSender response_sender);
158 // Add a properties dictionary to a message writer.
159 void AddPropertiesToWriter(MessageWriter* writer);
161 // Add a new object to the manager.
162 void AddObject(const dbus::ObjectPath& object_path);
163 void AddObjectInternal(const dbus::ObjectPath& object_path);
165 // Remove an object from the manager.
166 void RemoveObject(const dbus::ObjectPath& object_path);
167 void RemoveObjectInternal(const dbus::ObjectPath& object_path);
169 // Sends a property changed signal for the name property.
170 void SendPropertyChangedSignal(const std::string& name);
172 // Helper function for SendPropertyChangedSignal().
173 void SendPropertyChangedSignalInternal(const std::string& name);
175 // Sends a property invalidated signal for the name property.
176 void SendPropertyInvalidatedSignal();
178 // Helper function for SendPropertyInvalidatedSignal().
179 void SendPropertyInvalidatedSignalInternal();
181 // Helper function for RequestOwnership().
182 void RequestOwnershipInternal(base::OnceCallback<void(bool)> callback);
184 // Helper function for ReleaseOwnership().
185 void ReleaseOwnershipInternal(base::OnceClosure callback);
187 // Configures the test service to send a PropertiesChanged signal for the
188 // "Name" property immediately after a call to GetManagedObjects.
189 void SetSendImmediatePropertiesChanged();
191 // Sends the response on completion of the performed action.
192 void PerformActionResponse(
193 MethodCall* method_call,
194 dbus::ExportedObject::ResponseSender response_sender);
196 // Re-requests ownership of the well-known name after releasing it.
197 void OwnershipReleased(
198 MethodCall* method_call,
199 dbus::ExportedObject::ResponseSender response_sender);
201 // Sends the action response after regaining the well-known name.
202 void OwnershipRegained(
203 MethodCall* method_call,
204 dbus::ExportedObject::ResponseSender response_sender,
207 // Name of this service.
208 std::string service_name_;
210 // Options to use when requesting service ownership.
211 Bus::ServiceOwnershipOptions request_ownership_options_;
213 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
214 base::WaitableEvent on_name_obtained_;
215 // The number of methods actually exported.
216 int num_exported_methods_;
218 // True if a PropertiesChanged signal for the "Name" property should be sent
219 // immediately following a call to GetManagedObjects.
220 bool send_immediate_properties_changed_;
222 // True iff this instance has successfully acquired the name ownership.
225 scoped_refptr<Bus> bus_;
226 ExportedObject* exported_object_;
227 ExportedObject* exported_object_manager_;
232 #endif // DBUS_TEST_SERVICE_H_