Track active references in ShillClientHelper (Take 2)
[chromium-blink-merge.git] / chromeos / dbus / blocking_method_caller_unittest.cc
blob2853dada1275983e0f9c004a6effcc97b0501576
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 virtual 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 virtual bool RunsTasksOnCurrentThread() const OVERRIDE { return true; }
36 protected:
37 virtual ~FakeTaskRunner() {}
40 } // namespace
42 class BlockingMethodCallerTest : public testing::Test {
43 public:
44 BlockingMethodCallerTest() : task_runner_(new FakeTaskRunner) {
47 virtual void SetUp() {
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_));
77 // ShutdownAndBlock() will be called in TearDown().
78 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
81 virtual void TearDown() {
82 mock_bus_->ShutdownAndBlock();
85 protected:
86 scoped_refptr<FakeTaskRunner> task_runner_;
87 scoped_refptr<dbus::MockBus> mock_bus_;
88 scoped_refptr<dbus::MockObjectProxy> mock_proxy_;
90 private:
91 // Returns a response for the given method call. Used to implement
92 // CallMethodAndBlock() for |mock_proxy_|.
93 dbus::Response* CreateMockProxyResponse(dbus::MethodCall* method_call,
94 int timeout_ms) {
95 if (method_call->GetInterface() == "org.chromium.TestInterface" &&
96 method_call->GetMember() == "Echo") {
97 dbus::MessageReader reader(method_call);
98 std::string text_message;
99 if (reader.PopString(&text_message)) {
100 scoped_ptr<dbus::Response> response = dbus::Response::CreateEmpty();
101 dbus::MessageWriter writer(response.get());
102 writer.AppendString(text_message);
103 return response.release();
107 LOG(ERROR) << "Unexpected method call: " << method_call->ToString();
108 return NULL;
112 TEST_F(BlockingMethodCallerTest, Echo) {
113 const char kHello[] = "Hello";
114 // Get an object proxy from the mock bus.
115 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy(
116 "org.chromium.TestService",
117 dbus::ObjectPath("/org/chromium/TestObject"));
119 // Create a method call.
120 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
121 dbus::MessageWriter writer(&method_call);
122 writer.AppendString(kHello);
124 // Call the method.
125 BlockingMethodCaller blocking_method_caller(mock_bus_.get(), proxy);
126 scoped_ptr<dbus::Response> response(
127 blocking_method_caller.CallMethodAndBlock(&method_call));
129 // Check the response.
130 ASSERT_TRUE(response.get());
131 dbus::MessageReader reader(response.get());
132 std::string text_message;
133 ASSERT_TRUE(reader.PopString(&text_message));
134 // The text message should be echo'ed back.
135 EXPECT_EQ(kHello, text_message);
138 } // namespace chromeos