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
;
45 // The number of methods we'll export.
46 static const int kNumMethodsToExport
;
48 explicit TestService(const Options
& options
);
49 virtual ~TestService();
51 // Starts the service in a separate thread.
52 // Returns true if the thread is started successfully.
55 // Waits until the service is started (i.e. all methods are exported).
56 // Returns true on success.
57 bool WaitUntilServiceIsStarted() WARN_UNUSED_RESULT
;
59 // Shuts down the service and blocks until it's done.
60 void ShutdownAndBlock();
62 // Returns true if the bus has the D-Bus thread.
65 // Sends "Test" signal with the given message from the exported object.
66 void SendTestSignal(const std::string
& message
);
68 // Sends "Test" signal with the given message from the root object ("/").
69 // This function emulates dbus-send's behavior.
70 void SendTestSignalFromRoot(const std::string
& message
);
72 // Request the ownership of a well-known name "TestService".
73 // |callback| will be called with the result when an ownership request is
75 void RequestOwnership(base::Callback
<void(bool)> callback
);
77 // Returns whether this instance has the name ownership or not.
78 bool has_ownership() const { return has_ownership_
; }
81 // Helper function for SendTestSignal().
82 void SendTestSignalInternal(const std::string
& message
);
84 // Helper function for SendTestSignalFromRoot.
85 void SendTestSignalFromRootInternal(const std::string
& message
);
87 // Helper function for ShutdownAndBlock().
88 void ShutdownAndBlockInternal();
90 // Called when an ownership request is completed.
91 // |callback| is the callback to be called with the result. |service_name| is
92 // the requested well-known bus name. |callback| and |service_name| are bound
93 // when the service requests the ownership. |success| is the result of the
94 // completed request, and is propagated to |callback|.
95 void OnOwnership(base::Callback
<void(bool)> callback
,
96 const std::string
& service_name
,
99 // Called when a method is exported.
100 void OnExported(const std::string
& interface_name
,
101 const std::string
& method_name
,
104 // base::Thread override.
105 virtual void Run(base::MessageLoop
* message_loop
) OVERRIDE
;
111 // Echos the text message received from the method call.
112 void Echo(MethodCall
* method_call
,
113 dbus::ExportedObject::ResponseSender response_sender
);
115 // Echos the text message received from the method call, but sleeps for
116 // TestTimeouts::tiny_timeout_ms() before returning the response.
117 void SlowEcho(MethodCall
* method_call
,
118 dbus::ExportedObject::ResponseSender response_sender
);
120 // Echos the text message received from the method call, but sends its
121 // response asynchronously after this callback has returned.
122 void AsyncEcho(MethodCall
* method_call
,
123 dbus::ExportedObject::ResponseSender response_sender
);
125 // Returns NULL, instead of a valid Response.
126 void BrokenMethod(MethodCall
* method_call
,
127 dbus::ExportedObject::ResponseSender response_sender
);
129 // Returns a set of property values for testing.
130 void GetAllProperties(MethodCall
* method_call
,
131 dbus::ExportedObject::ResponseSender response_sender
);
133 // Returns a new value of 20 for the Version property when called.
134 void GetProperty(MethodCall
* method_call
,
135 dbus::ExportedObject::ResponseSender response_sender
);
137 // Allows the name property to be changed, errors otherwise.
138 void SetProperty(MethodCall
* method_call
,
139 dbus::ExportedObject::ResponseSender response_sender
);
141 // Performs an action for testing.
142 void PerformAction(MethodCall
* method_call
,
143 dbus::ExportedObject::ResponseSender response_sender
);
145 // Object Manager: returns the set of objects and properties.
146 void GetManagedObjects(MethodCall
* method_call
,
147 dbus::ExportedObject::ResponseSender response_sender
);
149 // Add a properties dictionary to a message writer.
150 void AddPropertiesToWriter(MessageWriter
* writer
);
152 // Add a new object to the manager.
153 void AddObject(const dbus::ObjectPath
& object_path
);
154 void AddObjectInternal(const dbus::ObjectPath
& object_path
);
156 // Remove an object from the manager.
157 void RemoveObject(const dbus::ObjectPath
& object_path
);
158 void RemoveObjectInternal(const dbus::ObjectPath
& object_path
);
160 // Sends a property changed signal for the name property.
161 void SendPropertyChangedSignal(const std::string
& name
);
163 // Helper function for SendPropertyChangedSignal().
164 void SendPropertyChangedSignalInternal(const std::string
& name
);
166 // Helper function for RequestOwnership().
167 void RequestOwnershipInternal(base::Callback
<void(bool)> callback
);
169 // Options to use when requesting service ownership.
170 Bus::ServiceOwnershipOptions request_ownership_options_
;
172 scoped_refptr
<base::SequencedTaskRunner
> dbus_task_runner_
;
173 base::WaitableEvent on_all_methods_exported_
;
174 // The number of methods actually exported.
175 int num_exported_methods_
;
177 // True iff this instance has successfully acquired the name ownership.
180 scoped_refptr
<Bus
> bus_
;
181 ExportedObject
* exported_object_
;
182 ExportedObject
* exported_object_manager_
;
187 #endif // DBUS_TEST_SERVICE_H_