Home | History | Annotate | Download | only in dbus
      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.
      4 
      5 #include "chromeos/dbus/blocking_method_caller.h"
      6 
      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"
     17 
     18 using ::testing::_;
     19 using ::testing::Invoke;
     20 using ::testing::Return;
     21 
     22 namespace chromeos {
     23 
     24 namespace {
     25 
     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;
     33   }
     34   virtual bool RunsTasksOnCurrentThread() const OVERRIDE { return true; }
     35 
     36  protected:
     37   virtual ~FakeTaskRunner() {}
     38 };
     39 
     40 }  // namespace
     41 
     42 class BlockingMethodCallerTest : public testing::Test {
     43  public:
     44   BlockingMethodCallerTest() : task_runner_(new FakeTaskRunner) {
     45   }
     46 
     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);
     52 
     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"));
     58 
     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));
     64 
     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()));
     71 
     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_));
     76 
     77     // ShutdownAndBlock() will be called in TearDown().
     78     EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
     79   }
     80 
     81   virtual void TearDown() {
     82     mock_bus_->ShutdownAndBlock();
     83   }
     84 
     85  protected:
     86   scoped_refptr<FakeTaskRunner> task_runner_;
     87   scoped_refptr<dbus::MockBus> mock_bus_;
     88   scoped_refptr<dbus::MockObjectProxy> mock_proxy_;
     89 
     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();
    104       }
    105     }
    106 
    107     LOG(ERROR) << "Unexpected method call: " << method_call->ToString();
    108     return NULL;
    109   }
    110 };
    111 
    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"));
    118 
    119   // Create a method call.
    120   dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
    121   dbus::MessageWriter writer(&method_call);
    122   writer.AppendString(kHello);
    123 
    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));
    128 
    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);
    136 }
    137 
    138 }  // namespace chromeos
    139