Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chromeos / dbus / blocking_method_caller_unittest.cc
blob3decf12f545da417c7fbe678c8a2dc6c225eec15
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 "chromeos/dbus/blocking_method_caller.h"
7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/task_runner.h"
11 #include "dbus/message.h"
12 #include "dbus/mock_bus.h"
13 #include "dbus/mock_object_proxy.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;
22 namespace chromeos {
24 namespace {
26 class FakeTaskRunner : public base::TaskRunner {
27 public:
28 bool PostDelayedTask(const tracked_objects::Location& from_here,
29 const base::Closure& task,
30 base::TimeDelta delay) override {
31 task.Run();
32 return true;
34 bool RunsTasksOnCurrentThread() const override { return true; }
36 protected:
37 ~FakeTaskRunner() override {}
40 } // namespace
42 class BlockingMethodCallerTest : public testing::Test {
43 public:
44 BlockingMethodCallerTest() : task_runner_(new FakeTaskRunner) {
47 void SetUp() override {
48 // Create a mock bus.
49 dbus::Bus::Options options;
50 options.bus_type = dbus::Bus::SYSTEM;
51 mock_bus_ = new dbus::MockBus(options);
53 // Create a mock proxy.
54 mock_proxy_ = new dbus::MockObjectProxy(
55 mock_bus_.get(),
56 "org.chromium.TestService",
57 dbus::ObjectPath("/org/chromium/TestObject"));
59 // Set an expectation so mock_proxy's CallMethodAndBlock() will use
60 // CreateMockProxyResponse() to return responses.
61 EXPECT_CALL(*mock_proxy_.get(), MockCallMethodAndBlock(_, _))
62 .WillRepeatedly(
63 Invoke(this, &BlockingMethodCallerTest::CreateMockProxyResponse));
65 // Set an expectation so mock_bus's GetObjectProxy() for the given
66 // service name and the object path will return mock_proxy_.
67 EXPECT_CALL(*mock_bus_.get(),
68 GetObjectProxy("org.chromium.TestService",
69 dbus::ObjectPath("/org/chromium/TestObject")))
70 .WillOnce(Return(mock_proxy_.get()));
72 // Set an expectation so mock_bus's GetDBusTaskRunner will return the fake
73 // task runner.
74 EXPECT_CALL(*mock_bus_.get(), GetDBusTaskRunner())
75 .WillRepeatedly(Return(task_runner_.get()));
77 // ShutdownAndBlock() will be called in TearDown().
78 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
81 void TearDown() override { mock_bus_->ShutdownAndBlock(); }
83 protected:
84 scoped_refptr<FakeTaskRunner> task_runner_;
85 scoped_refptr<dbus::MockBus> mock_bus_;
86 scoped_refptr<dbus::MockObjectProxy> mock_proxy_;
88 private:
89 // Returns a response for the given method call. Used to implement
90 // CallMethodAndBlock() for |mock_proxy_|.
91 dbus::Response* CreateMockProxyResponse(dbus::MethodCall* method_call,
92 int timeout_ms) {
93 if (method_call->GetInterface() == "org.chromium.TestInterface" &&
94 method_call->GetMember() == "Echo") {
95 dbus::MessageReader reader(method_call);
96 std::string text_message;
97 if (reader.PopString(&text_message)) {
98 scoped_ptr<dbus::Response> response = dbus::Response::CreateEmpty();
99 dbus::MessageWriter writer(response.get());
100 writer.AppendString(text_message);
101 return response.release();
105 LOG(ERROR) << "Unexpected method call: " << method_call->ToString();
106 return NULL;
110 TEST_F(BlockingMethodCallerTest, Echo) {
111 const char kHello[] = "Hello";
112 // Get an object proxy from the mock bus.
113 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy(
114 "org.chromium.TestService",
115 dbus::ObjectPath("/org/chromium/TestObject"));
117 // Create a method call.
118 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
119 dbus::MessageWriter writer(&method_call);
120 writer.AppendString(kHello);
122 // Call the method.
123 BlockingMethodCaller blocking_method_caller(mock_bus_.get(), proxy);
124 scoped_ptr<dbus::Response> response(
125 blocking_method_caller.CallMethodAndBlock(&method_call));
127 // Check the response.
128 ASSERT_TRUE(response.get());
129 dbus::MessageReader reader(response.get());
130 std::string text_message;
131 ASSERT_TRUE(reader.PopString(&text_message));
132 // The text message should be echo'ed back.
133 EXPECT_EQ(kHello, text_message);
136 } // namespace chromeos