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 #include "base/memory/ref_counted.h"
6 #include "base/memory/scoped_ptr.h"
8 #include "dbus/message.h"
9 #include "dbus/object_path.h"
10 #include "dbus/object_proxy.h"
11 #include "dbus/test_service.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 // The end-to-end test exercises the synchronous APIs in ObjectProxy and
15 // ExportedObject. The test will launch a thread for the service side
16 // operations (i.e. ExportedObject side).
17 class EndToEndSyncTest
: public testing::Test
{
22 virtual void SetUp() {
23 // Start the test service;
24 dbus::TestService::Options options
;
25 test_service_
.reset(new dbus::TestService(options
));
26 ASSERT_TRUE(test_service_
->StartService());
27 ASSERT_TRUE(test_service_
->WaitUntilServiceIsStarted());
28 ASSERT_FALSE(test_service_
->HasDBusThread());
31 dbus::Bus::Options client_bus_options
;
32 client_bus_options
.bus_type
= dbus::Bus::SESSION
;
33 client_bus_options
.connection_type
= dbus::Bus::PRIVATE
;
34 client_bus_
= new dbus::Bus(client_bus_options
);
35 object_proxy_
= client_bus_
->GetObjectProxy(
36 "org.chromium.TestService",
37 dbus::ObjectPath("/org/chromium/TestObject"));
38 ASSERT_FALSE(client_bus_
->HasDBusThread());
41 virtual void TearDown() {
42 test_service_
->ShutdownAndBlock();
43 test_service_
->Stop();
44 client_bus_
->ShutdownAndBlock();
48 scoped_ptr
<dbus::TestService
> test_service_
;
49 scoped_refptr
<dbus::Bus
> client_bus_
;
50 dbus::ObjectProxy
* object_proxy_
;
53 TEST_F(EndToEndSyncTest
, Echo
) {
54 const std::string kHello
= "hello";
56 // Create the method call.
57 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
58 dbus::MessageWriter
writer(&method_call
);
59 writer
.AppendString(kHello
);
62 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
63 scoped_ptr
<dbus::Response
> response(
64 object_proxy_
->CallMethodAndBlock(&method_call
, timeout_ms
));
65 ASSERT_TRUE(response
.get());
67 // Check the response. kHello should be echoed back.
68 dbus::MessageReader
reader(response
.get());
69 std::string returned_message
;
70 ASSERT_TRUE(reader
.PopString(&returned_message
));
71 EXPECT_EQ(kHello
, returned_message
);
74 TEST_F(EndToEndSyncTest
, Timeout
) {
75 const std::string kHello
= "hello";
77 // Create the method call.
78 dbus::MethodCall
method_call("org.chromium.TestInterface", "DelayedEcho");
79 dbus::MessageWriter
writer(&method_call
);
80 writer
.AppendString(kHello
);
82 // Call the method with timeout of 0ms.
83 const int timeout_ms
= 0;
84 scoped_ptr
<dbus::Response
> response(
85 object_proxy_
->CallMethodAndBlock(&method_call
, timeout_ms
));
86 // Should fail because of timeout.
87 ASSERT_FALSE(response
.get());
90 TEST_F(EndToEndSyncTest
, NonexistentMethod
) {
91 dbus::MethodCall
method_call("org.chromium.TestInterface", "Nonexistent");
93 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
94 scoped_ptr
<dbus::Response
> response(
95 object_proxy_
->CallMethodAndBlock(&method_call
, timeout_ms
));
96 ASSERT_FALSE(response
.get());
99 TEST_F(EndToEndSyncTest
, BrokenMethod
) {
100 dbus::MethodCall
method_call("org.chromium.TestInterface", "BrokenMethod");
102 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
103 scoped_ptr
<dbus::Response
> response(
104 object_proxy_
->CallMethodAndBlock(&method_call
, timeout_ms
));
105 ASSERT_FALSE(response
.get());
108 TEST_F(EndToEndSyncTest
, InvalidObjectPath
) {
109 // Trailing '/' is only allowed for the root path.
110 const dbus::ObjectPath
invalid_object_path("/org/chromium/TestObject/");
112 // Replace object proxy with new one.
113 object_proxy_
= client_bus_
->GetObjectProxy("org.chromium.TestService",
114 invalid_object_path
);
116 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
118 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
119 scoped_ptr
<dbus::Response
> response(
120 object_proxy_
->CallMethodAndBlock(&method_call
, timeout_ms
));
121 ASSERT_FALSE(response
.get());
124 TEST_F(EndToEndSyncTest
, InvalidServiceName
) {
125 // Bus name cannot contain '/'.
126 const std::string invalid_service_name
= ":1/2";
128 // Replace object proxy with new one.
129 object_proxy_
= client_bus_
->GetObjectProxy(
130 invalid_service_name
, dbus::ObjectPath("org.chromium.TestObject"));
132 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
134 const int timeout_ms
= dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
;
135 scoped_ptr
<dbus::Response
> response(
136 object_proxy_
->CallMethodAndBlock(&method_call
, timeout_ms
));
137 ASSERT_FALSE(response
.get());