test_service.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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
15namespace base {
16class SequencedTaskRunner;
17}
18
19namespace dbus {
20
21class MethodCall;
22class MessageWriter;
23class 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.
31class 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
45  // The number of methods we'll export.
46  static const int kNumMethodsToExport;
47
48  explicit TestService(const Options& options);
49  virtual ~TestService();
50
51  // Starts the service in a separate thread.
52  // Returns true if the thread is started successfully.
53  bool StartService();
54
55  // Waits until the service is started (i.e. all methods are exported).
56  // Returns true on success.
57  bool WaitUntilServiceIsStarted() WARN_UNUSED_RESULT;
58
59  // Shuts down the service and blocks until it's done.
60  void ShutdownAndBlock();
61
62  // Returns true if the bus has the D-Bus thread.
63  bool HasDBusThread();
64
65  // Sends "Test" signal with the given message from the exported object.
66  void SendTestSignal(const std::string& message);
67
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);
71
72  // Request the ownership of a well-known name "TestService".
73  // |callback| will be called with the result when an ownership request is
74  // completed.
75  void RequestOwnership(base::Callback<void(bool)> callback);
76
77  // Returns whether this instance has the name ownership or not.
78  bool has_ownership() const { return has_ownership_; }
79
80 private:
81  // Helper function for SendTestSignal().
82  void SendTestSignalInternal(const std::string& message);
83
84  // Helper function for SendTestSignalFromRoot.
85  void SendTestSignalFromRootInternal(const std::string& message);
86
87  // Helper function for ShutdownAndBlock().
88  void ShutdownAndBlockInternal();
89
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,
97                   bool success);
98
99  // Called when a method is exported.
100  void OnExported(const std::string& interface_name,
101                  const std::string& method_name,
102                  bool success);
103
104  // base::Thread override.
105  virtual void Run(base::MessageLoop* message_loop) OVERRIDE;
106
107  //
108  // Exported methods.
109  //
110
111  // Echos the text message received from the method call.
112  void Echo(MethodCall* method_call,
113            dbus::ExportedObject::ResponseSender response_sender);
114
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);
119
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);
124
125  // Returns NULL, instead of a valid Response.
126  void BrokenMethod(MethodCall* method_call,
127                    dbus::ExportedObject::ResponseSender response_sender);
128
129  // Returns a set of property values for testing.
130  void GetAllProperties(MethodCall* method_call,
131                        dbus::ExportedObject::ResponseSender response_sender);
132
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);
136
137  // Allows the name property to be changed, errors otherwise.
138  void SetProperty(MethodCall* method_call,
139                   dbus::ExportedObject::ResponseSender response_sender);
140
141  // Performs an action for testing.
142  void PerformAction(MethodCall* method_call,
143                     dbus::ExportedObject::ResponseSender response_sender);
144
145  // Object Manager: returns the set of objects and properties.
146  void GetManagedObjects(MethodCall* method_call,
147                         dbus::ExportedObject::ResponseSender response_sender);
148
149  // Add a properties dictionary to a message writer.
150  void AddPropertiesToWriter(MessageWriter* writer);
151
152  // Add a new object to the manager.
153  void AddObject(const dbus::ObjectPath& object_path);
154  void AddObjectInternal(const dbus::ObjectPath& object_path);
155
156  // Remove an object from the manager.
157  void RemoveObject(const dbus::ObjectPath& object_path);
158  void RemoveObjectInternal(const dbus::ObjectPath& object_path);
159
160  // Sends a property changed signal for the name property.
161  void SendPropertyChangedSignal(const std::string& name);
162
163  // Helper function for SendPropertyChangedSignal().
164  void SendPropertyChangedSignalInternal(const std::string& name);
165
166  // Helper function for RequestOwnership().
167  void RequestOwnershipInternal(base::Callback<void(bool)> callback);
168
169  // Options to use when requesting service ownership.
170  Bus::ServiceOwnershipOptions request_ownership_options_;
171
172  scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
173  base::WaitableEvent on_name_obtained_;
174  // The number of methods actually exported.
175  int num_exported_methods_;
176
177  // True iff this instance has successfully acquired the name ownership.
178  bool has_ownership_;
179
180  scoped_refptr<Bus> bus_;
181  ExportedObject* exported_object_;
182  ExportedObject* exported_object_manager_;
183};
184
185}  // namespace dbus
186
187#endif  // DBUS_TEST_SERVICE_H_
188