Upload upstream chromium 71.0.3578.0
[platform/framework/web/chromium-efl.git] / dbus / test_service.h
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.
4
5 #ifndef DBUS_TEST_SERVICE_H_
6 #define DBUS_TEST_SERVICE_H_
7
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"
12 #include "dbus/bus.h"
13 #include "dbus/exported_object.h"
14
15 namespace base {
16 class SequencedTaskRunner;
17 }
18
19 namespace dbus {
20
21 class MethodCall;
22 class MessageWriter;
23 class Response;
24
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
27 // the main thread.
28 //
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 {
32  public:
33   // Options for the test service.
34   struct Options {
35     Options();
36     ~Options();
37
38     // NULL by default (i.e. don't use the D-Bus thread).
39     scoped_refptr<base::SequencedTaskRunner> dbus_task_runner;
40
41     // Flags governing parameters of service ownership request.
42     Bus::ServiceOwnershipOptions request_ownership_options;
43
44     // Name of this service (randomly generated name will be used if empty).
45     std::string service_name;
46   };
47
48   // The number of methods we'll export.
49   static const int kNumMethodsToExport;
50
51   explicit TestService(const Options& options);
52   ~TestService() override;
53
54   // Starts the service in a separate thread.
55   // Returns true if the thread is started successfully.
56   bool StartService();
57
58   // Waits until the service is started (i.e. all methods are exported).
59   void WaitUntilServiceIsStarted();
60
61   // Shuts down the service and blocks until it's done.
62   void ShutdownAndBlock();
63
64   // Returns true if the bus has the D-Bus thread.
65   bool HasDBusThread();
66
67   // Sends "Test" signal with the given message from the exported object.
68   void SendTestSignal(const std::string& message);
69
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);
73
74   // Request the ownership of a well-known name "TestService".
75   // |callback| will be called with the result when an ownership request is
76   // completed.
77   void RequestOwnership(base::Callback<void(bool)> callback);
78
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::Closure callback);
82
83   // Returns the name of this service.
84   const std::string& service_name() const { return service_name_; }
85
86   // Returns whether this instance has the name ownership or not.
87   bool has_ownership() const { return has_ownership_; }
88
89  private:
90   // Helper function for SendTestSignal().
91   void SendTestSignalInternal(const std::string& message);
92
93   // Helper function for SendTestSignalFromRoot.
94   void SendTestSignalFromRootInternal(const std::string& message);
95
96   // Helper function for ShutdownAndBlock().
97   void ShutdownAndBlockInternal();
98
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::Callback<void(bool)> callback,
105                    const std::string& service_name,
106                    bool success);
107
108   // Called when a method is exported.
109   void OnExported(const std::string& interface_name,
110                   const std::string& method_name,
111                   bool success);
112
113   // base::Thread override.
114   void Run(base::RunLoop* run_loop) override;
115
116   //
117   // Exported methods.
118   //
119
120   // Echos the text message received from the method call.
121   void Echo(MethodCall* method_call,
122             dbus::ExportedObject::ResponseSender response_sender);
123
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);
128
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);
133
134   // Returns NULL, instead of a valid Response.
135   void BrokenMethod(MethodCall* method_call,
136                     dbus::ExportedObject::ResponseSender response_sender);
137
138   // Returns a set of property values for testing.
139   void GetAllProperties(MethodCall* method_call,
140                         dbus::ExportedObject::ResponseSender response_sender);
141
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);
145
146   // Allows the name property to be changed, errors otherwise.
147   void SetProperty(MethodCall* method_call,
148                    dbus::ExportedObject::ResponseSender response_sender);
149
150   // Performs an action for testing.
151   void PerformAction(MethodCall* method_call,
152                      dbus::ExportedObject::ResponseSender response_sender);
153
154   // Object Manager: returns the set of objects and properties.
155   void GetManagedObjects(MethodCall* method_call,
156                          dbus::ExportedObject::ResponseSender response_sender);
157
158   // Add a properties dictionary to a message writer.
159   void AddPropertiesToWriter(MessageWriter* writer);
160
161   // Add a new object to the manager.
162   void AddObject(const dbus::ObjectPath& object_path);
163   void AddObjectInternal(const dbus::ObjectPath& object_path);
164
165   // Remove an object from the manager.
166   void RemoveObject(const dbus::ObjectPath& object_path);
167   void RemoveObjectInternal(const dbus::ObjectPath& object_path);
168
169   // Sends a property changed signal for the name property.
170   void SendPropertyChangedSignal(const std::string& name);
171
172   // Helper function for SendPropertyChangedSignal().
173   void SendPropertyChangedSignalInternal(const std::string& name);
174
175   // Sends a property invalidated signal for the name property.
176   void SendPropertyInvalidatedSignal();
177
178   // Helper function for SendPropertyInvalidatedSignal().
179   void SendPropertyInvalidatedSignalInternal();
180
181   // Helper function for RequestOwnership().
182   void RequestOwnershipInternal(base::Callback<void(bool)> callback);
183
184   // Helper function for ReleaseOwnership().
185   void ReleaseOwnershipInternal(base::Closure callback);
186
187   // Configures the test service to send a PropertiesChanged signal for the
188   // "Name" property immediately after a call to GetManagedObjects.
189   void SetSendImmediatePropertiesChanged();
190
191   // Sends the response on completion of the performed action.
192   void PerformActionResponse(
193       MethodCall* method_call,
194       dbus::ExportedObject::ResponseSender response_sender);
195
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);
200
201   // Sends the action response after regaining the well-known name.
202   void OwnershipRegained(
203       MethodCall* method_call,
204       dbus::ExportedObject::ResponseSender response_sender,
205       bool success);
206
207   // Name of this service.
208   std::string service_name_;
209
210   // Options to use when requesting service ownership.
211   Bus::ServiceOwnershipOptions request_ownership_options_;
212
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_;
217
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_;
221
222   // True iff this instance has successfully acquired the name ownership.
223   bool has_ownership_;
224
225   scoped_refptr<Bus> bus_;
226   ExportedObject* exported_object_;
227   ExportedObject* exported_object_manager_;
228 };
229
230 }  // namespace dbus
231
232 #endif  // DBUS_TEST_SERVICE_H_