OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/logging.h" |
| 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "dbus/message.h" |
| 11 #include "dbus/mock_bus.h" |
| 12 #include "dbus/mock_object_proxy.h" |
| 13 #include "dbus/mock_exported_object.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 using ::testing::_; |
| 18 using ::testing::Invoke; |
| 19 using ::testing::Return; |
| 20 using ::testing::Unused; |
| 21 |
| 22 class MockTest : public testing::Test { |
| 23 public: |
| 24 MockTest() { |
| 25 } |
| 26 |
| 27 void SetUp() { |
| 28 // Create a mock bus. |
| 29 dbus::Bus::Options options; |
| 30 options.bus_type = dbus::Bus::SYSTEM; |
| 31 mock_bus_ = new dbus::MockBus(options); |
| 32 |
| 33 // Create a mock proxy. |
| 34 mock_proxy_ = new dbus::MockObjectProxy(mock_bus_.get(), |
| 35 "org.chromium.TestService", |
| 36 "/org/chromium/TestObject"); |
| 37 |
| 38 // Set an expectation so mock_proxy's CallMethodAndBlock() will use |
| 39 // CreateMockProxyResponse() to return responses. |
| 40 EXPECT_CALL(*mock_proxy_, CallMethodAndBlock(_, _)) |
| 41 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse)); |
| 42 |
| 43 // Set an expectation so mock_proxy's CallMethod() will use |
| 44 // HandleMockProxyResponseWithMessageLoop() to return responses. |
| 45 EXPECT_CALL(*mock_proxy_, CallMethod(_, _, _)) |
| 46 .WillRepeatedly( |
| 47 Invoke(this, |
| 48 &MockTest::HandleMockProxyResponseWithMessageLoop)); |
| 49 |
| 50 // Set an expectation so mock_bus's GetObjectProxy() for the given |
| 51 // service name and the object path will return mock_proxy_. |
| 52 EXPECT_CALL(*mock_bus_, GetObjectProxy("org.chromium.TestService", |
| 53 "/org/chromium/TestObject")) |
| 54 .WillOnce(Return(mock_proxy_.get())); |
| 55 } |
| 56 |
| 57 // Called when the response is received. |
| 58 void OnResponse(dbus::Response* response) { |
| 59 // |response| will be deleted on exit of the function. Copy the |
| 60 // payload to |response_string_|. |
| 61 if (response) { |
| 62 dbus::MessageReader reader(response); |
| 63 ASSERT_TRUE(reader.PopString(&response_string_)); |
| 64 } |
| 65 message_loop_.Quit(); |
| 66 }; |
| 67 |
| 68 protected: |
| 69 std::string response_string_; |
| 70 MessageLoop message_loop_; |
| 71 scoped_refptr<dbus::MockBus> mock_bus_; |
| 72 scoped_refptr<dbus::MockObjectProxy> mock_proxy_; |
| 73 |
| 74 private: |
| 75 // Returns a response for the given method call. Used to implement |
| 76 // CallMethodAndBlock() for |mock_proxy_|. |
| 77 dbus::Response* CreateMockProxyResponse(dbus::MethodCall* method_call, |
| 78 int timeout_ms) { |
| 79 if (method_call->GetInterface() == "org.chromium.TestInterface" && |
| 80 method_call->GetMember() == "Echo") { |
| 81 dbus::MessageReader reader(method_call); |
| 82 std::string text_message; |
| 83 if (reader.PopString(&text_message)) { |
| 84 dbus::Response* response = dbus::Response::CreateEmpty(); |
| 85 dbus::MessageWriter writer(response); |
| 86 writer.AppendString(text_message); |
| 87 return response; |
| 88 } |
| 89 } |
| 90 |
| 91 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); |
| 92 return NULL; |
| 93 } |
| 94 |
| 95 // Creates a response and runs the given response callback in the |
| 96 // message loop with the response. Used to implement for |mock_proxy_|. |
| 97 void HandleMockProxyResponseWithMessageLoop( |
| 98 dbus::MethodCall* method_call, |
| 99 int timeout_ms, |
| 100 dbus::ObjectProxy::ResponseCallback response_callback) { |
| 101 dbus::Response* response = CreateMockProxyResponse(method_call, |
| 102 timeout_ms); |
| 103 message_loop_.PostTask(FROM_HERE, |
| 104 base::Bind(&MockTest::RunResponseCallback, |
| 105 base::Unretained(this), |
| 106 response_callback, |
| 107 response)); |
| 108 } |
| 109 |
| 110 // Runs the given response callback with the given response. |
| 111 void RunResponseCallback( |
| 112 dbus::ObjectProxy::ResponseCallback response_callback, |
| 113 dbus::Response* response) { |
| 114 response_callback.Run(response); |
| 115 delete response; |
| 116 } |
| 117 }; |
| 118 |
| 119 // This test demonstrates how to mock a synchronos method call using the |
| 120 // mock classes. |
| 121 TEST_F(MockTest, CallMethodAndBlock) { |
| 122 const char kHello[] = "Hello"; |
| 123 // Get an object proxy from the mock bus. |
| 124 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
| 125 "org.chromium.TestService", |
| 126 "/org/chromium/TestObject"); |
| 127 |
| 128 // Create a method call. |
| 129 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 130 dbus::MessageWriter writer(&method_call); |
| 131 writer.AppendString(kHello); |
| 132 |
| 133 // Call the method. |
| 134 scoped_ptr<dbus::Response> response( |
| 135 proxy->CallMethodAndBlock(&method_call, |
| 136 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); |
| 137 |
| 138 // Check the response. |
| 139 ASSERT_TRUE(response.get()); |
| 140 dbus::MessageReader reader(response.get()); |
| 141 std::string text_message; |
| 142 ASSERT_TRUE(reader.PopString(&text_message)); |
| 143 // The text message should be echo'ed back. |
| 144 EXPECT_EQ(kHello, text_message); |
| 145 } |
| 146 |
| 147 // This test demonstrates how to mock an asynchronos method call using the |
| 148 // mock classes. |
| 149 TEST_F(MockTest, CallMethod) { |
| 150 const char kHello[] = "hello"; |
| 151 |
| 152 // Get an object proxy from the mock bus. |
| 153 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
| 154 "org.chromium.TestService", |
| 155 "/org/chromium/TestObject"); |
| 156 |
| 157 // Create a method call. |
| 158 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 159 dbus::MessageWriter writer(&method_call); |
| 160 writer.AppendString(kHello); |
| 161 |
| 162 // Call the method. |
| 163 proxy->CallMethod(&method_call, |
| 164 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 165 base::Bind(&MockTest::OnResponse, |
| 166 base::Unretained(this))); |
| 167 // Run the message loop to let OnResponse be called. |
| 168 message_loop_.Run(); |
| 169 |
| 170 EXPECT_EQ(kHello, response_string_); |
| 171 } |
OLD | NEW |