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"
15 class MessageLoopProxy
;
24 // The test service is used for end-to-end tests. The service runs in a
25 // separate thread, so it does not interfere the test code that runs in
28 // The test service exports an object with methods such as Echo() and
29 // SlowEcho(). The object has ability to send "Test" signal.
30 class TestService
: public base::Thread
{
32 // Options for the test service.
37 // NULL by default (i.e. don't use the D-Bus thread).
38 scoped_refptr
<base::MessageLoopProxy
> dbus_thread_message_loop_proxy
;
41 // The number of methods we'll export.
42 static const int kNumMethodsToExport
;
44 TestService(const Options
& options
);
45 virtual ~TestService();
47 // Starts the service in a separate thread.
48 // Returns true if the thread is started successfully.
51 // Waits until the service is started (i.e. all methods are exported).
52 // Returns true on success.
53 bool WaitUntilServiceIsStarted() WARN_UNUSED_RESULT
;
55 // Shuts down the service and blocks until it's done.
56 void ShutdownAndBlock();
58 // Returns true if the bus has the D-Bus thread.
61 // Sends "Test" signal with the given message from the exported object.
62 void SendTestSignal(const std::string
& message
);
64 // Sends "Test" signal with the given message from the root object ("/").
65 // This function emulates dbus-send's behavior.
66 void SendTestSignalFromRoot(const std::string
& message
);
69 // Helper function for SendTestSignal().
70 void SendTestSignalInternal(const std::string
& message
);
72 // Helper function for SendTestSignalFromRoot.
73 void SendTestSignalFromRootInternal(const std::string
& message
);
75 // Helper function for ShutdownAndBlock().
76 void ShutdownAndBlockInternal();
78 // Called when an ownership request is completed.
79 void OnOwnership(const std::string
& service_name
,
82 // Called when a method is exported.
83 void OnExported(const std::string
& interface_name
,
84 const std::string
& method_name
,
87 // base::Thread override.
88 virtual void Run(MessageLoop
* message_loop
) OVERRIDE
;
94 // Echos the text message received from the method call.
95 void Echo(MethodCall
* method_call
,
96 dbus::ExportedObject::ResponseSender response_sender
);
98 // Echos the text message received from the method call, but sleeps for
99 // TestTimeouts::tiny_timeout_ms() before returning the response.
100 void SlowEcho(MethodCall
* method_call
,
101 dbus::ExportedObject::ResponseSender response_sender
);
103 // Echos the text message received from the method call, but sends its
104 // response asynchronously after this callback has returned.
105 void AsyncEcho(MethodCall
* method_call
,
106 dbus::ExportedObject::ResponseSender response_sender
);
108 // Returns NULL, instead of a valid Response.
109 void BrokenMethod(MethodCall
* method_call
,
110 dbus::ExportedObject::ResponseSender response_sender
);
112 // Returns a set of property values for testing.
113 void GetAllProperties(MethodCall
* method_call
,
114 dbus::ExportedObject::ResponseSender response_sender
);
116 // Returns a new value of 20 for the Version property when called.
117 void GetProperty(MethodCall
* method_call
,
118 dbus::ExportedObject::ResponseSender response_sender
);
120 // Allows the name property to be changed, errors otherwise.
121 void SetProperty(MethodCall
* method_call
,
122 dbus::ExportedObject::ResponseSender response_sender
);
124 // Sends a property changed signal for the name property.
125 void SendPropertyChangedSignal(const std::string
& name
);
127 // Helper function for SendPropertyChangedSignal().
128 void SendPropertyChangedSignalInternal(const std::string
& name
);
130 scoped_refptr
<base::MessageLoopProxy
> dbus_thread_message_loop_proxy_
;
131 base::WaitableEvent on_all_methods_exported_
;
132 // The number of methods actually exported.
133 int num_exported_methods_
;
135 scoped_refptr
<Bus
> bus_
;
136 ExportedObject
* exported_object_
;
141 #endif // DBUS_TEST_SERVICE_H_