test_service.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #ifndef DBUS_TEST_SERVICE_H_
  5. #define DBUS_TEST_SERVICE_H_
  6. #include "base/compiler_specific.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/synchronization/waitable_event.h"
  10. #include "base/threading/thread.h"
  11. #include "dbus/bus.h"
  12. #include "dbus/exported_object.h"
  13. namespace base {
  14. class SequencedTaskRunner;
  15. }
  16. namespace dbus {
  17. class MethodCall;
  18. class MessageWriter;
  19. class Response;
  20. // The test service is used for end-to-end tests. The service runs in a
  21. // separate thread, so it does not interfere the test code that runs in
  22. // the main thread.
  23. //
  24. // The test service exports an object with methods such as Echo() and
  25. // SlowEcho(). The object has ability to send "Test" signal.
  26. class TestService : public base::Thread {
  27. public:
  28. // Options for the test service.
  29. struct Options {
  30. Options();
  31. ~Options();
  32. // NULL by default (i.e. don't use the D-Bus thread).
  33. scoped_refptr<base::SequencedTaskRunner> dbus_task_runner;
  34. // Flags governing parameters of service ownership request.
  35. Bus::ServiceOwnershipOptions request_ownership_options;
  36. // Name of this service (randomly generated name will be used if empty).
  37. std::string service_name;
  38. };
  39. // The number of methods we'll export.
  40. static const int kNumMethodsToExport;
  41. explicit TestService(const Options& options);
  42. ~TestService() override;
  43. // Starts the service in a separate thread.
  44. // Returns true if the thread is started successfully.
  45. bool StartService();
  46. // Waits until the service is started (i.e. all methods are exported).
  47. void WaitUntilServiceIsStarted();
  48. // Shuts down the service and blocks until it's done.
  49. void ShutdownAndBlock();
  50. // Returns true if the bus has the D-Bus thread.
  51. bool HasDBusThread();
  52. // Sends "Test" signal with the given message from the exported object.
  53. void SendTestSignal(const std::string& message);
  54. // Sends "Test" signal with the given message from the root object ("/").
  55. // This function emulates dbus-send's behavior.
  56. void SendTestSignalFromRoot(const std::string& message);
  57. // Request the ownership of a well-known name "TestService".
  58. // |callback| will be called with the result when an ownership request is
  59. // completed.
  60. void RequestOwnership(base::OnceCallback<void(bool)> callback);
  61. // Release the ownership of the well-known name "TestService".
  62. // |callback| will be called when the ownership has been released.
  63. void ReleaseOwnership(base::OnceClosure callback);
  64. // Returns the name of this service.
  65. const std::string& service_name() const { return service_name_; }
  66. // Returns whether this instance has the name ownership or not.
  67. bool has_ownership() const { return has_ownership_; }
  68. private:
  69. // Helper function for SendTestSignal().
  70. void SendTestSignalInternal(const std::string& message);
  71. // Helper function for SendTestSignalFromRoot.
  72. void SendTestSignalFromRootInternal(const std::string& message);
  73. // Helper function for ShutdownAndBlock().
  74. void ShutdownAndBlockInternal();
  75. // Called when an ownership request is completed.
  76. // |callback| is the callback to be called with the result. |service_name| is
  77. // the requested well-known bus name. |callback| and |service_name| are bound
  78. // when the service requests the ownership. |success| is the result of the
  79. // completed request, and is propagated to |callback|.
  80. void OnOwnership(base::OnceCallback<void(bool)> callback,
  81. const std::string& service_name,
  82. bool success);
  83. // Called when a method is exported.
  84. void OnExported(const std::string& interface_name,
  85. const std::string& method_name,
  86. bool success);
  87. // base::Thread override.
  88. void Run(base::RunLoop* run_loop) override;
  89. //
  90. // Exported methods.
  91. //
  92. // Echos the text message received from the method call.
  93. void Echo(MethodCall* method_call,
  94. dbus::ExportedObject::ResponseSender response_sender);
  95. // Echos the text message received from the method call, but sleeps for
  96. // TestTimeouts::tiny_timeout_ms() before returning the response.
  97. void SlowEcho(MethodCall* method_call,
  98. dbus::ExportedObject::ResponseSender response_sender);
  99. // Echos the text message received from the method call, but sends its
  100. // response asynchronously after this callback has returned.
  101. void AsyncEcho(MethodCall* method_call,
  102. dbus::ExportedObject::ResponseSender response_sender);
  103. // Returns NULL, instead of a valid Response.
  104. void BrokenMethod(MethodCall* method_call,
  105. dbus::ExportedObject::ResponseSender response_sender);
  106. // Returns a set of property values for testing.
  107. void GetAllProperties(MethodCall* method_call,
  108. dbus::ExportedObject::ResponseSender response_sender);
  109. // Returns a new value of 20 for the Version property when called.
  110. void GetProperty(MethodCall* method_call,
  111. dbus::ExportedObject::ResponseSender response_sender);
  112. // Allows the name property to be changed, errors otherwise.
  113. void SetProperty(MethodCall* method_call,
  114. dbus::ExportedObject::ResponseSender response_sender);
  115. // Performs an action for testing.
  116. void PerformAction(MethodCall* method_call,
  117. dbus::ExportedObject::ResponseSender response_sender);
  118. // Object Manager: returns the set of objects and properties.
  119. void GetManagedObjects(MethodCall* method_call,
  120. dbus::ExportedObject::ResponseSender response_sender);
  121. // Add a properties dictionary to a message writer.
  122. void AddPropertiesToWriter(MessageWriter* writer);
  123. // Add a new object to the manager.
  124. void AddObject(const dbus::ObjectPath& object_path);
  125. void AddObjectInternal(const dbus::ObjectPath& object_path);
  126. // Remove an object from the manager.
  127. void RemoveObject(const dbus::ObjectPath& object_path);
  128. void RemoveObjectInternal(const dbus::ObjectPath& object_path);
  129. // Sends a property changed signal for the name property.
  130. void SendPropertyChangedSignal(const std::string& name);
  131. // Helper function for SendPropertyChangedSignal().
  132. void SendPropertyChangedSignalInternal(const std::string& name);
  133. // Sends a property invalidated signal for the name property.
  134. void SendPropertyInvalidatedSignal();
  135. // Helper function for SendPropertyInvalidatedSignal().
  136. void SendPropertyInvalidatedSignalInternal();
  137. // Helper function for RequestOwnership().
  138. void RequestOwnershipInternal(base::OnceCallback<void(bool)> callback);
  139. // Helper function for ReleaseOwnership().
  140. void ReleaseOwnershipInternal(base::OnceClosure callback);
  141. // Configures the test service to send a PropertiesChanged signal for the
  142. // "Name" property immediately after a call to GetManagedObjects.
  143. void SetSendImmediatePropertiesChanged();
  144. // Sends the response on completion of the performed action.
  145. void PerformActionResponse(
  146. MethodCall* method_call,
  147. dbus::ExportedObject::ResponseSender response_sender);
  148. // Re-requests ownership of the well-known name after releasing it.
  149. void OwnershipReleased(
  150. MethodCall* method_call,
  151. dbus::ExportedObject::ResponseSender response_sender);
  152. // Sends the action response after regaining the well-known name.
  153. void OwnershipRegained(
  154. MethodCall* method_call,
  155. dbus::ExportedObject::ResponseSender response_sender,
  156. bool success);
  157. // Name of this service.
  158. std::string service_name_;
  159. // Options to use when requesting service ownership.
  160. Bus::ServiceOwnershipOptions request_ownership_options_;
  161. scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
  162. base::WaitableEvent on_name_obtained_;
  163. // The number of methods actually exported.
  164. int num_exported_methods_;
  165. // True if a PropertiesChanged signal for the "Name" property should be sent
  166. // immediately following a call to GetManagedObjects.
  167. bool send_immediate_properties_changed_;
  168. // True iff this instance has successfully acquired the name ownership.
  169. bool has_ownership_;
  170. scoped_refptr<Bus> bus_;
  171. raw_ptr<ExportedObject> exported_object_;
  172. raw_ptr<ExportedObject> exported_object_manager_;
  173. };
  174. } // namespace dbus
  175. #endif // DBUS_TEST_SERVICE_H_