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_
9 #include "base/compiler_specific.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/threading/thread.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "dbus/exported_object.h"
16 class MessageLoopProxy
;
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
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
{
33 // Options for the test service.
38 // NULL by default (i.e. don't use the D-Bus thread).
39 scoped_refptr
<base::MessageLoopProxy
> dbus_thread_message_loop_proxy
;
42 // The number of methods we'll export.
43 static const int kNumMethodsToExport
;
45 TestService(const Options
& options
);
46 virtual ~TestService();
48 // Starts the service in a separate thread.
49 // Returns true if the thread is started successfully.
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.
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
);
70 // Helper function for SendTestSignal().
71 void SendTestSignalInternal(const std::string
& message
);
73 // Helper function for SendTestSignalFromRoot.
74 void SendTestSignalFromRootInternal(const std::string
& message
);
76 // Helper function for ShutdownAndBlock().
77 void ShutdownAndBlockInternal();
79 // Called when an ownership request is completed.
80 void OnOwnership(const std::string
& service_name
,
83 // Called when a method is exported.
84 void OnExported(const std::string
& interface_name
,
85 const std::string
& method_name
,
88 // base::Thread override.
89 virtual void Run(MessageLoop
* message_loop
) OVERRIDE
;
95 // Echos the text message received from the method call.
96 void Echo(MethodCall
* method_call
,
97 dbus::ExportedObject::ResponseSender response_sender
);
99 // Echos the text message received from the method call, but sleeps for
100 // TestTimeouts::tiny_timeout_ms() before returning the response.
101 void SlowEcho(MethodCall
* method_call
,
102 dbus::ExportedObject::ResponseSender response_sender
);
104 // Echos the text message received from the method call, but sends its
105 // response asynchronously after this callback has returned.
106 void AsyncEcho(MethodCall
* method_call
,
107 dbus::ExportedObject::ResponseSender response_sender
);
109 // Returns NULL, instead of a valid Response.
110 void BrokenMethod(MethodCall
* method_call
,
111 dbus::ExportedObject::ResponseSender response_sender
);
113 // Returns a set of property values for testing.
114 void GetAllProperties(MethodCall
* method_call
,
115 dbus::ExportedObject::ResponseSender response_sender
);
117 // Returns a new value of 20 for the Version property when called.
118 void GetProperty(MethodCall
* method_call
,
119 dbus::ExportedObject::ResponseSender response_sender
);
121 // Allows the name property to be changed, errors otherwise.
122 void SetProperty(MethodCall
* method_call
,
123 dbus::ExportedObject::ResponseSender response_sender
);
125 // Sends a property changed signal for the name property.
126 void SendPropertyChangedSignal(const std::string
& name
);
128 // Helper function for SendPropertyChangedSignal().
129 void SendPropertyChangedSignalInternal(const std::string
& name
);
131 scoped_refptr
<base::MessageLoopProxy
> dbus_thread_message_loop_proxy_
;
132 base::WaitableEvent on_all_methods_exported_
;
133 // The number of methods actually exported.
134 int num_exported_methods_
;
136 scoped_refptr
<Bus
> bus_
;
137 ExportedObject
* exported_object_
;
142 #endif // DBUS_TEST_SERVICE_H_