Make the fling acceleration curve be easily configurable.
[chromium-blink-merge.git] / dbus / mock_unittest.cc
blobcf644df0d861bc67dea4dba9cce47f82f0725cf2
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/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 "dbus/object_path.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using ::testing::_;
19 using ::testing::Invoke;
20 using ::testing::Return;
21 using ::testing::Unused;
23 class MockTest : public testing::Test {
24 public:
25 MockTest() {
28 virtual void SetUp() {
29 // Create a mock bus.
30 dbus::Bus::Options options;
31 options.bus_type = dbus::Bus::SYSTEM;
32 mock_bus_ = new dbus::MockBus(options);
34 // Create a mock proxy.
35 mock_proxy_ = new dbus::MockObjectProxy(
36 mock_bus_.get(),
37 "org.chromium.TestService",
38 dbus::ObjectPath("/org/chromium/TestObject"));
40 // Set an expectation so mock_proxy's CallMethodAndBlock() will use
41 // CreateMockProxyResponse() to return responses.
42 EXPECT_CALL(*mock_proxy_, CallMethodAndBlock(_, _))
43 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse));
45 // Set an expectation so mock_proxy's CallMethod() will use
46 // HandleMockProxyResponseWithMessageLoop() to return responses.
47 EXPECT_CALL(*mock_proxy_, CallMethod(_, _, _))
48 .WillRepeatedly(
49 Invoke(this,
50 &MockTest::HandleMockProxyResponseWithMessageLoop));
52 // Set an expectation so mock_bus's GetObjectProxy() for the given
53 // service name and the object path will return mock_proxy_.
54 EXPECT_CALL(*mock_bus_, GetObjectProxy(
55 "org.chromium.TestService",
56 dbus::ObjectPath("/org/chromium/TestObject")))
57 .WillOnce(Return(mock_proxy_.get()));
59 // ShutdownAndBlock() will be called in TearDown().
60 EXPECT_CALL(*mock_bus_, ShutdownAndBlock()).WillOnce(Return());
63 virtual void TearDown() {
64 mock_bus_->ShutdownAndBlock();
67 // Called when the response is received.
68 void OnResponse(dbus::Response* response) {
69 // |response| will be deleted on exit of the function. Copy the
70 // payload to |response_string_|.
71 if (response) {
72 dbus::MessageReader reader(response);
73 ASSERT_TRUE(reader.PopString(&response_string_));
75 message_loop_.Quit();
78 protected:
79 std::string response_string_;
80 MessageLoop message_loop_;
81 scoped_refptr<dbus::MockBus> mock_bus_;
82 scoped_refptr<dbus::MockObjectProxy> mock_proxy_;
84 private:
85 // Returns a response for the given method call. Used to implement
86 // CallMethodAndBlock() for |mock_proxy_|.
87 dbus::Response* CreateMockProxyResponse(dbus::MethodCall* method_call,
88 int timeout_ms) {
89 if (method_call->GetInterface() == "org.chromium.TestInterface" &&
90 method_call->GetMember() == "Echo") {
91 dbus::MessageReader reader(method_call);
92 std::string text_message;
93 if (reader.PopString(&text_message)) {
94 dbus::Response* response = dbus::Response::CreateEmpty();
95 dbus::MessageWriter writer(response);
96 writer.AppendString(text_message);
97 return response;
101 LOG(ERROR) << "Unexpected method call: " << method_call->ToString();
102 return NULL;
105 // Creates a response and runs the given response callback in the
106 // message loop with the response. Used to implement for |mock_proxy_|.
107 void HandleMockProxyResponseWithMessageLoop(
108 dbus::MethodCall* method_call,
109 int timeout_ms,
110 dbus::ObjectProxy::ResponseCallback response_callback) {
111 dbus::Response* response = CreateMockProxyResponse(method_call,
112 timeout_ms);
113 message_loop_.PostTask(FROM_HERE,
114 base::Bind(&MockTest::RunResponseCallback,
115 base::Unretained(this),
116 response_callback,
117 response));
120 // Runs the given response callback with the given response.
121 void RunResponseCallback(
122 dbus::ObjectProxy::ResponseCallback response_callback,
123 dbus::Response* response) {
124 response_callback.Run(response);
125 delete response;
129 // This test demonstrates how to mock a synchronos method call using the
130 // mock classes.
131 TEST_F(MockTest, CallMethodAndBlock) {
132 const char kHello[] = "Hello";
133 // Get an object proxy from the mock bus.
134 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy(
135 "org.chromium.TestService",
136 dbus::ObjectPath("/org/chromium/TestObject"));
138 // Create a method call.
139 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
140 dbus::MessageWriter writer(&method_call);
141 writer.AppendString(kHello);
143 // Call the method.
144 scoped_ptr<dbus::Response> response(
145 proxy->CallMethodAndBlock(&method_call,
146 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
148 // Check the response.
149 ASSERT_TRUE(response.get());
150 dbus::MessageReader reader(response.get());
151 std::string text_message;
152 ASSERT_TRUE(reader.PopString(&text_message));
153 // The text message should be echo'ed back.
154 EXPECT_EQ(kHello, text_message);
157 // This test demonstrates how to mock an asynchronos method call using the
158 // mock classes.
159 TEST_F(MockTest, CallMethod) {
160 const char kHello[] = "hello";
162 // Get an object proxy from the mock bus.
163 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy(
164 "org.chromium.TestService",
165 dbus::ObjectPath("/org/chromium/TestObject"));
167 // Create a method call.
168 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
169 dbus::MessageWriter writer(&method_call);
170 writer.AppendString(kHello);
172 // Call the method.
173 proxy->CallMethod(&method_call,
174 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
175 base::Bind(&MockTest::OnResponse,
176 base::Unretained(this)));
177 // Run the message loop to let OnResponse be called.
178 message_loop_.Run();
180 EXPECT_EQ(kHello, response_string_);