| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "dbus/bus.h" | 7 #include "dbus/bus.h" |
| 8 #include "dbus/message.h" | 8 #include "dbus/message.h" |
| 9 #include "dbus/object_path.h" | 9 #include "dbus/object_path.h" |
| 10 #include "dbus/object_proxy.h" | 10 #include "dbus/object_proxy.h" |
| 11 #include "dbus/test_service.h" | 11 #include "dbus/test_service.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace dbus { |
| 15 |
| 14 // The end-to-end test exercises the synchronous APIs in ObjectProxy and | 16 // The end-to-end test exercises the synchronous APIs in ObjectProxy and |
| 15 // ExportedObject. The test will launch a thread for the service side | 17 // ExportedObject. The test will launch a thread for the service side |
| 16 // operations (i.e. ExportedObject side). | 18 // operations (i.e. ExportedObject side). |
| 17 class EndToEndSyncTest : public testing::Test { | 19 class EndToEndSyncTest : public testing::Test { |
| 18 public: | 20 public: |
| 19 EndToEndSyncTest() { | 21 EndToEndSyncTest() { |
| 20 } | 22 } |
| 21 | 23 |
| 22 virtual void SetUp() { | 24 virtual void SetUp() { |
| 23 // Start the test service; | 25 // Start the test service; |
| 24 dbus::TestService::Options options; | 26 TestService::Options options; |
| 25 test_service_.reset(new dbus::TestService(options)); | 27 test_service_.reset(new TestService(options)); |
| 26 ASSERT_TRUE(test_service_->StartService()); | 28 ASSERT_TRUE(test_service_->StartService()); |
| 27 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); | 29 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); |
| 28 ASSERT_FALSE(test_service_->HasDBusThread()); | 30 ASSERT_FALSE(test_service_->HasDBusThread()); |
| 29 | 31 |
| 30 // Create the client. | 32 // Create the client. |
| 31 dbus::Bus::Options client_bus_options; | 33 Bus::Options client_bus_options; |
| 32 client_bus_options.bus_type = dbus::Bus::SESSION; | 34 client_bus_options.bus_type = Bus::SESSION; |
| 33 client_bus_options.connection_type = dbus::Bus::PRIVATE; | 35 client_bus_options.connection_type = Bus::PRIVATE; |
| 34 client_bus_ = new dbus::Bus(client_bus_options); | 36 client_bus_ = new Bus(client_bus_options); |
| 35 object_proxy_ = client_bus_->GetObjectProxy( | 37 object_proxy_ = client_bus_->GetObjectProxy( |
| 36 "org.chromium.TestService", | 38 "org.chromium.TestService", |
| 37 dbus::ObjectPath("/org/chromium/TestObject")); | 39 ObjectPath("/org/chromium/TestObject")); |
| 38 ASSERT_FALSE(client_bus_->HasDBusThread()); | 40 ASSERT_FALSE(client_bus_->HasDBusThread()); |
| 39 } | 41 } |
| 40 | 42 |
| 41 virtual void TearDown() { | 43 virtual void TearDown() { |
| 42 test_service_->ShutdownAndBlock(); | 44 test_service_->ShutdownAndBlock(); |
| 43 test_service_->Stop(); | 45 test_service_->Stop(); |
| 44 client_bus_->ShutdownAndBlock(); | 46 client_bus_->ShutdownAndBlock(); |
| 45 } | 47 } |
| 46 | 48 |
| 47 protected: | 49 protected: |
| 48 scoped_ptr<dbus::TestService> test_service_; | 50 scoped_ptr<TestService> test_service_; |
| 49 scoped_refptr<dbus::Bus> client_bus_; | 51 scoped_refptr<Bus> client_bus_; |
| 50 dbus::ObjectProxy* object_proxy_; | 52 ObjectProxy* object_proxy_; |
| 51 }; | 53 }; |
| 52 | 54 |
| 53 TEST_F(EndToEndSyncTest, Echo) { | 55 TEST_F(EndToEndSyncTest, Echo) { |
| 54 const std::string kHello = "hello"; | 56 const std::string kHello = "hello"; |
| 55 | 57 |
| 56 // Create the method call. | 58 // Create the method call. |
| 57 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); | 59 MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 58 dbus::MessageWriter writer(&method_call); | 60 MessageWriter writer(&method_call); |
| 59 writer.AppendString(kHello); | 61 writer.AppendString(kHello); |
| 60 | 62 |
| 61 // Call the method. | 63 // Call the method. |
| 62 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT; | 64 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
| 63 scoped_ptr<dbus::Response> response( | 65 scoped_ptr<Response> response( |
| 64 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); | 66 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
| 65 ASSERT_TRUE(response.get()); | 67 ASSERT_TRUE(response.get()); |
| 66 | 68 |
| 67 // Check the response. kHello should be echoed back. | 69 // Check the response. kHello should be echoed back. |
| 68 dbus::MessageReader reader(response.get()); | 70 MessageReader reader(response.get()); |
| 69 std::string returned_message; | 71 std::string returned_message; |
| 70 ASSERT_TRUE(reader.PopString(&returned_message)); | 72 ASSERT_TRUE(reader.PopString(&returned_message)); |
| 71 EXPECT_EQ(kHello, returned_message); | 73 EXPECT_EQ(kHello, returned_message); |
| 72 } | 74 } |
| 73 | 75 |
| 74 TEST_F(EndToEndSyncTest, Timeout) { | 76 TEST_F(EndToEndSyncTest, Timeout) { |
| 75 const std::string kHello = "hello"; | 77 const std::string kHello = "hello"; |
| 76 | 78 |
| 77 // Create the method call. | 79 // Create the method call. |
| 78 dbus::MethodCall method_call("org.chromium.TestInterface", "DelayedEcho"); | 80 MethodCall method_call("org.chromium.TestInterface", "DelayedEcho"); |
| 79 dbus::MessageWriter writer(&method_call); | 81 MessageWriter writer(&method_call); |
| 80 writer.AppendString(kHello); | 82 writer.AppendString(kHello); |
| 81 | 83 |
| 82 // Call the method with timeout of 0ms. | 84 // Call the method with timeout of 0ms. |
| 83 const int timeout_ms = 0; | 85 const int timeout_ms = 0; |
| 84 scoped_ptr<dbus::Response> response( | 86 scoped_ptr<Response> response( |
| 85 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); | 87 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
| 86 // Should fail because of timeout. | 88 // Should fail because of timeout. |
| 87 ASSERT_FALSE(response.get()); | 89 ASSERT_FALSE(response.get()); |
| 88 } | 90 } |
| 89 | 91 |
| 90 TEST_F(EndToEndSyncTest, NonexistentMethod) { | 92 TEST_F(EndToEndSyncTest, NonexistentMethod) { |
| 91 dbus::MethodCall method_call("org.chromium.TestInterface", "Nonexistent"); | 93 MethodCall method_call("org.chromium.TestInterface", "Nonexistent"); |
| 92 | 94 |
| 93 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT; | 95 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
| 94 scoped_ptr<dbus::Response> response( | 96 scoped_ptr<Response> response( |
| 95 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); | 97 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
| 96 ASSERT_FALSE(response.get()); | 98 ASSERT_FALSE(response.get()); |
| 97 } | 99 } |
| 98 | 100 |
| 99 TEST_F(EndToEndSyncTest, BrokenMethod) { | 101 TEST_F(EndToEndSyncTest, BrokenMethod) { |
| 100 dbus::MethodCall method_call("org.chromium.TestInterface", "BrokenMethod"); | 102 MethodCall method_call("org.chromium.TestInterface", "BrokenMethod"); |
| 101 | 103 |
| 102 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT; | 104 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
| 103 scoped_ptr<dbus::Response> response( | 105 scoped_ptr<Response> response( |
| 104 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); | 106 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
| 105 ASSERT_FALSE(response.get()); | 107 ASSERT_FALSE(response.get()); |
| 106 } | 108 } |
| 107 | 109 |
| 108 TEST_F(EndToEndSyncTest, InvalidObjectPath) { | 110 TEST_F(EndToEndSyncTest, InvalidObjectPath) { |
| 109 // Trailing '/' is only allowed for the root path. | 111 // Trailing '/' is only allowed for the root path. |
| 110 const dbus::ObjectPath invalid_object_path("/org/chromium/TestObject/"); | 112 const ObjectPath invalid_object_path("/org/chromium/TestObject/"); |
| 111 | 113 |
| 112 // Replace object proxy with new one. | 114 // Replace object proxy with new one. |
| 113 object_proxy_ = client_bus_->GetObjectProxy("org.chromium.TestService", | 115 object_proxy_ = client_bus_->GetObjectProxy("org.chromium.TestService", |
| 114 invalid_object_path); | 116 invalid_object_path); |
| 115 | 117 |
| 116 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); | 118 MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 117 | 119 |
| 118 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT; | 120 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
| 119 scoped_ptr<dbus::Response> response( | 121 scoped_ptr<Response> response( |
| 120 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); | 122 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
| 121 ASSERT_FALSE(response.get()); | 123 ASSERT_FALSE(response.get()); |
| 122 } | 124 } |
| 123 | 125 |
| 124 TEST_F(EndToEndSyncTest, InvalidServiceName) { | 126 TEST_F(EndToEndSyncTest, InvalidServiceName) { |
| 125 // Bus name cannot contain '/'. | 127 // Bus name cannot contain '/'. |
| 126 const std::string invalid_service_name = ":1/2"; | 128 const std::string invalid_service_name = ":1/2"; |
| 127 | 129 |
| 128 // Replace object proxy with new one. | 130 // Replace object proxy with new one. |
| 129 object_proxy_ = client_bus_->GetObjectProxy( | 131 object_proxy_ = client_bus_->GetObjectProxy( |
| 130 invalid_service_name, dbus::ObjectPath("org.chromium.TestObject")); | 132 invalid_service_name, ObjectPath("org.chromium.TestObject")); |
| 131 | 133 |
| 132 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); | 134 MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 133 | 135 |
| 134 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT; | 136 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
| 135 scoped_ptr<dbus::Response> response( | 137 scoped_ptr<Response> response( |
| 136 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); | 138 object_proxy_->CallMethodAndBlock(&method_call, timeout_ms)); |
| 137 ASSERT_FALSE(response.get()); | 139 ASSERT_FALSE(response.get()); |
| 138 } | 140 } |
| 141 |
| 142 } // namespace dbus |
| OLD | NEW |