Update V8 to version 4.7.21.
[chromium-blink-merge.git] / dbus / mock_unittest.cc
blob6b2a521127aa1eca0e6dd4c17ef05131a65adb21
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/message_loop.h"
10 #include "base/run_loop.h"
11 #include "dbus/message.h"
12 #include "dbus/mock_bus.h"
13 #include "dbus/mock_exported_object.h"
14 #include "dbus/mock_object_proxy.h"
15 #include "dbus/object_path.h"
16 #include "dbus/scoped_dbus_error.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using ::testing::_;
21 using ::testing::Invoke;
22 using ::testing::Return;
23 using ::testing::Unused;
25 namespace dbus {
27 class MockTest : public testing::Test {
28 public:
29 MockTest() {
32 void SetUp() override {
33 // Create a mock bus.
34 Bus::Options options;
35 options.bus_type = Bus::SYSTEM;
36 mock_bus_ = new MockBus(options);
38 // Create a mock proxy.
39 mock_proxy_ = new MockObjectProxy(
40 mock_bus_.get(),
41 "org.chromium.TestService",
42 ObjectPath("/org/chromium/TestObject"));
44 // Set an expectation so mock_proxy's CallMethodAndBlock() will use
45 // CreateMockProxyResponse() to return responses.
46 EXPECT_CALL(*mock_proxy_.get(), MockCallMethodAndBlock(_, _))
47 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse));
48 EXPECT_CALL(*mock_proxy_.get(),
49 MockCallMethodAndBlockWithErrorDetails(_, _, _))
50 .WillRepeatedly(
51 Invoke(this, &MockTest::CreateMockProxyResponseWithErrorDetails));
53 // Set an expectation so mock_proxy's CallMethod() will use
54 // HandleMockProxyResponseWithMessageLoop() to return responses.
55 EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)).WillRepeatedly(
56 Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop));
58 // Set an expectation so mock_bus's GetObjectProxy() for the given
59 // service name and the object path will return mock_proxy_.
60 EXPECT_CALL(*mock_bus_.get(),
61 GetObjectProxy("org.chromium.TestService",
62 ObjectPath("/org/chromium/TestObject")))
63 .WillOnce(Return(mock_proxy_.get()));
65 // ShutdownAndBlock() will be called in TearDown().
66 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
69 void TearDown() override { mock_bus_->ShutdownAndBlock(); }
71 // Called when the response is received.
72 void OnResponse(Response* response) {
73 // |response| will be deleted on exit of the function. Copy the
74 // payload to |response_string_|.
75 if (response) {
76 MessageReader reader(response);
77 ASSERT_TRUE(reader.PopString(&response_string_));
79 run_loop_->Quit();
82 protected:
83 std::string response_string_;
84 base::MessageLoop message_loop_;
85 scoped_ptr<base::RunLoop> run_loop_;
86 scoped_refptr<MockBus> mock_bus_;
87 scoped_refptr<MockObjectProxy> mock_proxy_;
89 private:
90 // Returns a response for the given method call. Used to implement
91 // CallMethodAndBlock() for |mock_proxy_|.
92 Response* CreateMockProxyResponse(MethodCall* method_call,
93 int timeout_ms) {
94 if (method_call->GetInterface() == "org.chromium.TestInterface" &&
95 method_call->GetMember() == "Echo") {
96 MessageReader reader(method_call);
97 std::string text_message;
98 if (reader.PopString(&text_message)) {
99 scoped_ptr<Response> response = Response::CreateEmpty();
100 MessageWriter writer(response.get());
101 writer.AppendString(text_message);
102 return response.release();
106 LOG(ERROR) << "Unexpected method call: " << method_call->ToString();
107 return NULL;
110 Response* CreateMockProxyResponseWithErrorDetails(
111 MethodCall* method_call, int timeout_ms, ScopedDBusError* error) {
112 dbus_set_error(error->get(), DBUS_ERROR_NOT_SUPPORTED, "Not implemented");
113 return NULL;
116 // Creates a response and runs the given response callback in the
117 // message loop with the response. Used to implement for |mock_proxy_|.
118 void HandleMockProxyResponseWithMessageLoop(
119 MethodCall* method_call,
120 int timeout_ms,
121 ObjectProxy::ResponseCallback response_callback) {
122 Response* response = CreateMockProxyResponse(method_call, timeout_ms);
123 message_loop_.PostTask(FROM_HERE,
124 base::Bind(&MockTest::RunResponseCallback,
125 base::Unretained(this),
126 response_callback,
127 response));
130 // Runs the given response callback with the given response.
131 void RunResponseCallback(
132 ObjectProxy::ResponseCallback response_callback,
133 Response* response) {
134 response_callback.Run(response);
135 delete response;
139 // This test demonstrates how to mock a synchronous method call using the
140 // mock classes.
141 TEST_F(MockTest, CallMethodAndBlock) {
142 const char kHello[] = "Hello";
143 // Get an object proxy from the mock bus.
144 ObjectProxy* proxy = mock_bus_->GetObjectProxy(
145 "org.chromium.TestService",
146 ObjectPath("/org/chromium/TestObject"));
148 // Create a method call.
149 MethodCall method_call("org.chromium.TestInterface", "Echo");
150 MessageWriter writer(&method_call);
151 writer.AppendString(kHello);
153 // Call the method.
154 scoped_ptr<Response> response(
155 proxy->CallMethodAndBlock(&method_call,
156 ObjectProxy::TIMEOUT_USE_DEFAULT));
158 // Check the response.
159 ASSERT_TRUE(response.get());
160 MessageReader reader(response.get());
161 std::string text_message;
162 ASSERT_TRUE(reader.PopString(&text_message));
163 // The text message should be echo'ed back.
164 EXPECT_EQ(kHello, text_message);
167 TEST_F(MockTest, CallMethodAndBlockWithErrorDetails) {
168 // Get an object proxy from the mock bus.
169 ObjectProxy* proxy = mock_bus_->GetObjectProxy(
170 "org.chromium.TestService",
171 ObjectPath("/org/chromium/TestObject"));
173 // Create a method call.
174 MethodCall method_call("org.chromium.TestInterface", "Echo");
176 ScopedDBusError error;
177 // Call the method.
178 scoped_ptr<Response> response(
179 proxy->CallMethodAndBlockWithErrorDetails(
180 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT, &error));
182 // Check the response.
183 ASSERT_FALSE(response.get());
184 ASSERT_TRUE(error.is_set());
185 EXPECT_STREQ(DBUS_ERROR_NOT_SUPPORTED, error.name());
186 EXPECT_STREQ("Not implemented", error.message());
189 // This test demonstrates how to mock an asynchronous method call using the
190 // mock classes.
191 TEST_F(MockTest, CallMethod) {
192 const char kHello[] = "hello";
194 // Get an object proxy from the mock bus.
195 ObjectProxy* proxy = mock_bus_->GetObjectProxy(
196 "org.chromium.TestService",
197 ObjectPath("/org/chromium/TestObject"));
199 // Create a method call.
200 MethodCall method_call("org.chromium.TestInterface", "Echo");
201 MessageWriter writer(&method_call);
202 writer.AppendString(kHello);
204 // Call the method.
205 run_loop_.reset(new base::RunLoop);
206 proxy->CallMethod(&method_call,
207 ObjectProxy::TIMEOUT_USE_DEFAULT,
208 base::Bind(&MockTest::OnResponse,
209 base::Unretained(this)));
210 // Run the message loop to let OnResponse be called.
211 run_loop_->Run();
213 EXPECT_EQ(kHello, response_string_);
216 } // namespace dbus