Adding myself as an owner to chrome/android.
[chromium-blink-merge.git] / dbus / test_service.h
blobb9e14b55d1572087aa0235182971a1a54ccbd3cd
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"
12 #include "dbus/exported_object.h"
14 namespace base {
15 class SequencedTaskRunner;
18 namespace dbus {
20 class Bus;
21 class MethodCall;
22 class MessageWriter;
23 class Response;
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.
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();
38 // NULL by default (i.e. don't use the D-Bus thread).
39 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner;
42 // The number of methods we'll export.
43 static const int kNumMethodsToExport;
45 explicit TestService(const Options& options);
46 virtual ~TestService();
48 // Starts the service in a separate thread.
49 // Returns true if the thread is started successfully.
50 bool StartService();
52 // Waits until the service is started (i.e. all methods are exported).
53 // Returns true on success.
54 bool WaitUntilServiceIsStarted() WARN_UNUSED_RESULT;
56 // Shuts down the service and blocks until it's done.
57 void ShutdownAndBlock();
59 // Returns true if the bus has the D-Bus thread.
60 bool HasDBusThread();
62 // Sends "Test" signal with the given message from the exported object.
63 void SendTestSignal(const std::string& message);
65 // Sends "Test" signal with the given message from the root object ("/").
66 // This function emulates dbus-send's behavior.
67 void SendTestSignalFromRoot(const std::string& message);
69 // Request the ownership of a well-known name "TestService".
70 // |callback| will be called with the result when an ownership request is
71 // completed.
72 void RequestOwnership(base::Callback<void(bool)> callback);
74 // Returns whether this instance has the name ownership or not.
75 bool has_ownership() const { return has_ownership_; }
77 private:
78 // Helper function for SendTestSignal().
79 void SendTestSignalInternal(const std::string& message);
81 // Helper function for SendTestSignalFromRoot.
82 void SendTestSignalFromRootInternal(const std::string& message);
84 // Helper function for ShutdownAndBlock().
85 void ShutdownAndBlockInternal();
87 // Called when an ownership request is completed.
88 // |callback| is the callback to be called with the result. |service_name| is
89 // the requested well-known bus name. |callback| and |service_name| are bound
90 // when the service requests the ownership. |success| is the result of the
91 // completed request, and is propagated to |callback|.
92 void OnOwnership(base::Callback<void(bool)> callback,
93 const std::string& service_name,
94 bool success);
96 // Called when a method is exported.
97 void OnExported(const std::string& interface_name,
98 const std::string& method_name,
99 bool success);
101 // base::Thread override.
102 virtual void Run(MessageLoop* message_loop) OVERRIDE;
105 // Exported methods.
108 // Echos the text message received from the method call.
109 void Echo(MethodCall* method_call,
110 dbus::ExportedObject::ResponseSender response_sender);
112 // Echos the text message received from the method call, but sleeps for
113 // TestTimeouts::tiny_timeout_ms() before returning the response.
114 void SlowEcho(MethodCall* method_call,
115 dbus::ExportedObject::ResponseSender response_sender);
117 // Echos the text message received from the method call, but sends its
118 // response asynchronously after this callback has returned.
119 void AsyncEcho(MethodCall* method_call,
120 dbus::ExportedObject::ResponseSender response_sender);
122 // Returns NULL, instead of a valid Response.
123 void BrokenMethod(MethodCall* method_call,
124 dbus::ExportedObject::ResponseSender response_sender);
126 // Returns a set of property values for testing.
127 void GetAllProperties(MethodCall* method_call,
128 dbus::ExportedObject::ResponseSender response_sender);
130 // Returns a new value of 20 for the Version property when called.
131 void GetProperty(MethodCall* method_call,
132 dbus::ExportedObject::ResponseSender response_sender);
134 // Allows the name property to be changed, errors otherwise.
135 void SetProperty(MethodCall* method_call,
136 dbus::ExportedObject::ResponseSender response_sender);
138 // Performs an action for testing.
139 void PerformAction(MethodCall* method_call,
140 dbus::ExportedObject::ResponseSender response_sender);
142 // Object Manager: returns the set of objects and properties.
143 void GetManagedObjects(MethodCall* method_call,
144 dbus::ExportedObject::ResponseSender response_sender);
146 // Add a properties dictionary to a message writer.
147 void AddPropertiesToWriter(MessageWriter* writer);
149 // Add a new object to the manager.
150 void AddObject(const dbus::ObjectPath& object_path);
151 void AddObjectInternal(const dbus::ObjectPath& object_path);
153 // Remove an object from the manager.
154 void RemoveObject(const dbus::ObjectPath& object_path);
155 void RemoveObjectInternal(const dbus::ObjectPath& object_path);
157 // Sends a property changed signal for the name property.
158 void SendPropertyChangedSignal(const std::string& name);
160 // Helper function for SendPropertyChangedSignal().
161 void SendPropertyChangedSignalInternal(const std::string& name);
163 // Helper function for RequestOwnership().
164 void RequestOwnershipInternal(base::Callback<void(bool)> callback);
166 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
167 base::WaitableEvent on_all_methods_exported_;
168 // The number of methods actually exported.
169 int num_exported_methods_;
171 // True iff this instance has successfully acquired the name ownership.
172 bool has_ownership_;
174 scoped_refptr<Bus> bus_;
175 ExportedObject* exported_object_;
176 ExportedObject* exported_object_manager_;
179 } // namespace dbus
181 #endif // DBUS_TEST_SERVICE_H_