| OLD | NEW |
| 1 // Copyright (c) 2011 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "dbus/message.h" | 10 #include "dbus/message.h" |
| 11 #include "dbus/mock_bus.h" | 11 #include "dbus/mock_bus.h" |
| 12 #include "dbus/mock_object_proxy.h" | 12 #include "dbus/mock_object_proxy.h" |
| 13 #include "dbus/mock_exported_object.h" | 13 #include "dbus/mock_exported_object.h" |
| 14 #include "dbus/object_path.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 using ::testing::_; | 18 using ::testing::_; |
| 18 using ::testing::Invoke; | 19 using ::testing::Invoke; |
| 19 using ::testing::Return; | 20 using ::testing::Return; |
| 20 using ::testing::Unused; | 21 using ::testing::Unused; |
| 21 | 22 |
| 22 class MockTest : public testing::Test { | 23 class MockTest : public testing::Test { |
| 23 public: | 24 public: |
| 24 MockTest() { | 25 MockTest() { |
| 25 } | 26 } |
| 26 | 27 |
| 27 virtual void SetUp() { | 28 virtual void SetUp() { |
| 28 // Create a mock bus. | 29 // Create a mock bus. |
| 29 dbus::Bus::Options options; | 30 dbus::Bus::Options options; |
| 30 options.bus_type = dbus::Bus::SYSTEM; | 31 options.bus_type = dbus::Bus::SYSTEM; |
| 31 mock_bus_ = new dbus::MockBus(options); | 32 mock_bus_ = new dbus::MockBus(options); |
| 32 | 33 |
| 33 // Create a mock proxy. | 34 // Create a mock proxy. |
| 34 mock_proxy_ = new dbus::MockObjectProxy(mock_bus_.get(), | 35 mock_proxy_ = new dbus::MockObjectProxy( |
| 35 "org.chromium.TestService", | 36 mock_bus_.get(), |
| 36 "/org/chromium/TestObject"); | 37 "org.chromium.TestService", |
| 38 dbus::ObjectPath("/org/chromium/TestObject")); |
| 37 | 39 |
| 38 // Set an expectation so mock_proxy's CallMethodAndBlock() will use | 40 // Set an expectation so mock_proxy's CallMethodAndBlock() will use |
| 39 // CreateMockProxyResponse() to return responses. | 41 // CreateMockProxyResponse() to return responses. |
| 40 EXPECT_CALL(*mock_proxy_, CallMethodAndBlock(_, _)) | 42 EXPECT_CALL(*mock_proxy_, CallMethodAndBlock(_, _)) |
| 41 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse)); | 43 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse)); |
| 42 | 44 |
| 43 // Set an expectation so mock_proxy's CallMethod() will use | 45 // Set an expectation so mock_proxy's CallMethod() will use |
| 44 // HandleMockProxyResponseWithMessageLoop() to return responses. | 46 // HandleMockProxyResponseWithMessageLoop() to return responses. |
| 45 EXPECT_CALL(*mock_proxy_, CallMethod(_, _, _)) | 47 EXPECT_CALL(*mock_proxy_, CallMethod(_, _, _)) |
| 46 .WillRepeatedly( | 48 .WillRepeatedly( |
| 47 Invoke(this, | 49 Invoke(this, |
| 48 &MockTest::HandleMockProxyResponseWithMessageLoop)); | 50 &MockTest::HandleMockProxyResponseWithMessageLoop)); |
| 49 | 51 |
| 50 // Set an expectation so mock_bus's GetObjectProxy() for the given | 52 // Set an expectation so mock_bus's GetObjectProxy() for the given |
| 51 // service name and the object path will return mock_proxy_. | 53 // service name and the object path will return mock_proxy_. |
| 52 EXPECT_CALL(*mock_bus_, GetObjectProxy("org.chromium.TestService", | 54 EXPECT_CALL(*mock_bus_, GetObjectProxy( |
| 53 "/org/chromium/TestObject")) | 55 "org.chromium.TestService", |
| 56 dbus::ObjectPath("/org/chromium/TestObject"))) |
| 54 .WillOnce(Return(mock_proxy_.get())); | 57 .WillOnce(Return(mock_proxy_.get())); |
| 55 | 58 |
| 56 // ShutdownAndBlock() will be called in TearDown(). | 59 // ShutdownAndBlock() will be called in TearDown(). |
| 57 EXPECT_CALL(*mock_bus_, ShutdownAndBlock()).WillOnce(Return()); | 60 EXPECT_CALL(*mock_bus_, ShutdownAndBlock()).WillOnce(Return()); |
| 58 } | 61 } |
| 59 | 62 |
| 60 virtual void TearDown() { | 63 virtual void TearDown() { |
| 61 mock_bus_->ShutdownAndBlock(); | 64 mock_bus_->ShutdownAndBlock(); |
| 62 } | 65 } |
| 63 | 66 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 } | 126 } |
| 124 }; | 127 }; |
| 125 | 128 |
| 126 // This test demonstrates how to mock a synchronos method call using the | 129 // This test demonstrates how to mock a synchronos method call using the |
| 127 // mock classes. | 130 // mock classes. |
| 128 TEST_F(MockTest, CallMethodAndBlock) { | 131 TEST_F(MockTest, CallMethodAndBlock) { |
| 129 const char kHello[] = "Hello"; | 132 const char kHello[] = "Hello"; |
| 130 // Get an object proxy from the mock bus. | 133 // Get an object proxy from the mock bus. |
| 131 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy( | 134 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
| 132 "org.chromium.TestService", | 135 "org.chromium.TestService", |
| 133 "/org/chromium/TestObject"); | 136 dbus::ObjectPath("/org/chromium/TestObject")); |
| 134 | 137 |
| 135 // Create a method call. | 138 // Create a method call. |
| 136 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); | 139 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 137 dbus::MessageWriter writer(&method_call); | 140 dbus::MessageWriter writer(&method_call); |
| 138 writer.AppendString(kHello); | 141 writer.AppendString(kHello); |
| 139 | 142 |
| 140 // Call the method. | 143 // Call the method. |
| 141 scoped_ptr<dbus::Response> response( | 144 scoped_ptr<dbus::Response> response( |
| 142 proxy->CallMethodAndBlock(&method_call, | 145 proxy->CallMethodAndBlock(&method_call, |
| 143 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); | 146 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); |
| 144 | 147 |
| 145 // Check the response. | 148 // Check the response. |
| 146 ASSERT_TRUE(response.get()); | 149 ASSERT_TRUE(response.get()); |
| 147 dbus::MessageReader reader(response.get()); | 150 dbus::MessageReader reader(response.get()); |
| 148 std::string text_message; | 151 std::string text_message; |
| 149 ASSERT_TRUE(reader.PopString(&text_message)); | 152 ASSERT_TRUE(reader.PopString(&text_message)); |
| 150 // The text message should be echo'ed back. | 153 // The text message should be echo'ed back. |
| 151 EXPECT_EQ(kHello, text_message); | 154 EXPECT_EQ(kHello, text_message); |
| 152 } | 155 } |
| 153 | 156 |
| 154 // This test demonstrates how to mock an asynchronos method call using the | 157 // This test demonstrates how to mock an asynchronos method call using the |
| 155 // mock classes. | 158 // mock classes. |
| 156 TEST_F(MockTest, CallMethod) { | 159 TEST_F(MockTest, CallMethod) { |
| 157 const char kHello[] = "hello"; | 160 const char kHello[] = "hello"; |
| 158 | 161 |
| 159 // Get an object proxy from the mock bus. | 162 // Get an object proxy from the mock bus. |
| 160 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy( | 163 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
| 161 "org.chromium.TestService", | 164 "org.chromium.TestService", |
| 162 "/org/chromium/TestObject"); | 165 dbus::ObjectPath("/org/chromium/TestObject")); |
| 163 | 166 |
| 164 // Create a method call. | 167 // Create a method call. |
| 165 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); | 168 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 166 dbus::MessageWriter writer(&method_call); | 169 dbus::MessageWriter writer(&method_call); |
| 167 writer.AppendString(kHello); | 170 writer.AppendString(kHello); |
| 168 | 171 |
| 169 // Call the method. | 172 // Call the method. |
| 170 proxy->CallMethod(&method_call, | 173 proxy->CallMethod(&method_call, |
| 171 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 174 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 172 base::Bind(&MockTest::OnResponse, | 175 base::Bind(&MockTest::OnResponse, |
| 173 base::Unretained(this))); | 176 base::Unretained(this))); |
| 174 // Run the message loop to let OnResponse be called. | 177 // Run the message loop to let OnResponse be called. |
| 175 message_loop_.Run(); | 178 message_loop_.Run(); |
| 176 | 179 |
| 177 EXPECT_EQ(kHello, response_string_); | 180 EXPECT_EQ(kHello, response_string_); |
| 178 } | 181 } |
| OLD | NEW |