Upload upstream chromium 67.0.3396
[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   // Returns true on success.
60   bool WaitUntilServiceIsStarted() WARN_UNUSED_RESULT;
61
62   // Shuts down the service and blocks until it's done.
63   void ShutdownAndBlock();
64
65   // Returns true if the bus has the D-Bus thread.
66   bool HasDBusThread();
67
68   // Sends "Test" signal with the given message from the exported object.
69   void SendTestSignal(const std::string& message);
70
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);
74
75   // Request the ownership of a well-known name "TestService".
76   // |callback| will be called with the result when an ownership request is
77   // completed.
78   void RequestOwnership(base::Callback<void(bool)> callback);
79
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);
83
84   // Returns the name of this service.
85   const std::string& service_name() const { return service_name_; }
86
87   // Returns whether this instance has the name ownership or not.
88   bool has_ownership() const { return has_ownership_; }
89
90  private:
91   // Helper function for SendTestSignal().
92   void SendTestSignalInternal(const std::string& message);
93
94   // Helper function for SendTestSignalFromRoot.
95   void SendTestSignalFromRootInternal(const std::string& message);
96
97   // Helper function for ShutdownAndBlock().
98   void ShutdownAndBlockInternal();
99
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,
107                    bool success);
108
109   // Called when a method is exported.
110   void OnExported(const std::string& interface_name,
111                   const std::string& method_name,
112                   bool success);
113
114   // base::Thread override.
115   void Run(base::RunLoop* run_loop) override;
116
117   //
118   // Exported methods.
119   //
120
121   // Echos the text message received from the method call.
122   void Echo(MethodCall* method_call,
123             dbus::ExportedObject::ResponseSender response_sender);
124
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);
129
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);
134
135   // Returns NULL, instead of a valid Response.
136   void BrokenMethod(MethodCall* method_call,
137                     dbus::ExportedObject::ResponseSender response_sender);
138
139   // Returns a set of property values for testing.
140   void GetAllProperties(MethodCall* method_call,
141                         dbus::ExportedObject::ResponseSender response_sender);
142
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);
146
147   // Allows the name property to be changed, errors otherwise.
148   void SetProperty(MethodCall* method_call,
149                    dbus::ExportedObject::ResponseSender response_sender);
150
151   // Performs an action for testing.
152   void PerformAction(MethodCall* method_call,
153                      dbus::ExportedObject::ResponseSender response_sender);
154
155   // Object Manager: returns the set of objects and properties.
156   void GetManagedObjects(MethodCall* method_call,
157                          dbus::ExportedObject::ResponseSender response_sender);
158
159   // Add a properties dictionary to a message writer.
160   void AddPropertiesToWriter(MessageWriter* writer);
161
162   // Add a new object to the manager.
163   void AddObject(const dbus::ObjectPath& object_path);
164   void AddObjectInternal(const dbus::ObjectPath& object_path);
165
166   // Remove an object from the manager.
167   void RemoveObject(const dbus::ObjectPath& object_path);
168   void RemoveObjectInternal(const dbus::ObjectPath& object_path);
169
170   // Sends a property changed signal for the name property.
171   void SendPropertyChangedSignal(const std::string& name);
172
173   // Helper function for SendPropertyChangedSignal().
174   void SendPropertyChangedSignalInternal(const std::string& name);
175
176   // Sends a property invalidated signal for the name property.
177   void SendPropertyInvalidatedSignal();
178
179   // Helper function for SendPropertyInvalidatedSignal().
180   void SendPropertyInvalidatedSignalInternal();
181
182   // Helper function for RequestOwnership().
183   void RequestOwnershipInternal(base::Callback<void(bool)> callback);
184
185   // Helper function for ReleaseOwnership().
186   void ReleaseOwnershipInternal(base::Closure callback);
187
188   // Configures the test service to send a PropertiesChanged signal for the
189   // "Name" property immediately after a call to GetManagedObjects.
190   void SetSendImmediatePropertiesChanged();
191
192   // Sends the response on completion of the performed action.
193   void PerformActionResponse(
194       MethodCall* method_call,
195       dbus::ExportedObject::ResponseSender response_sender);
196
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);
201
202   // Sends the action response after regaining the well-known name.
203   void OwnershipRegained(
204       MethodCall* method_call,
205       dbus::ExportedObject::ResponseSender response_sender,
206       bool success);
207
208   // Name of this service.
209   std::string service_name_;
210
211   // Options to use when requesting service ownership.
212   Bus::ServiceOwnershipOptions request_ownership_options_;
213
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_;
218
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_;
222
223   // True iff this instance has successfully acquired the name ownership.
224   bool has_ownership_;
225
226   scoped_refptr<Bus> bus_;
227   ExportedObject* exported_object_;
228   ExportedObject* exported_object_manager_;
229 };
230
231 }  // namespace dbus
232
233 #endif  // DBUS_TEST_SERVICE_H_