| OLD | NEW |
| (Empty) |
| 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 "chrome/browser/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 "dbus/message.h" | |
| 11 #include "dbus/mock_bus.h" | |
| 12 #include "dbus/mock_object_proxy.h" | |
| 13 #include "dbus/object_path.h" | |
| 14 #include "testing/gmock/include/gmock/gmock.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 using ::testing::_; | |
| 18 using ::testing::Invoke; | |
| 19 using ::testing::Return; | |
| 20 | |
| 21 namespace chromeos { | |
| 22 | |
| 23 class BlockingMethodCallerTest : public testing::Test { | |
| 24 public: | |
| 25 BlockingMethodCallerTest() { | |
| 26 } | |
| 27 | |
| 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); | |
| 33 | |
| 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")); | |
| 39 | |
| 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( | |
| 44 this, &BlockingMethodCallerTest::CreateMockProxyResponse)); | |
| 45 | |
| 46 // Set an expectation so mock_bus's GetObjectProxy() for the given | |
| 47 // service name and the object path will return mock_proxy_. | |
| 48 EXPECT_CALL(*mock_bus_, GetObjectProxy( | |
| 49 "org.chromium.TestService", | |
| 50 dbus::ObjectPath("/org/chromium/TestObject"))) | |
| 51 .WillOnce(Return(mock_proxy_.get())); | |
| 52 | |
| 53 // Set an expectation so mock_bus's PostTaskToDBusThread() will run the | |
| 54 // given task. | |
| 55 EXPECT_CALL(*mock_bus_, PostTaskToDBusThread(_, _)) | |
| 56 .WillRepeatedly(Invoke( | |
| 57 this, &BlockingMethodCallerTest::RunTask)); | |
| 58 | |
| 59 // ShutdownAndBlock() will be called in TearDown(). | |
| 60 EXPECT_CALL(*mock_bus_, ShutdownAndBlock()).WillOnce(Return()); | |
| 61 } | |
| 62 | |
| 63 virtual void TearDown() { | |
| 64 mock_bus_->ShutdownAndBlock(); | |
| 65 } | |
| 66 | |
| 67 protected: | |
| 68 scoped_refptr<dbus::MockBus> mock_bus_; | |
| 69 scoped_refptr<dbus::MockObjectProxy> mock_proxy_; | |
| 70 | |
| 71 private: | |
| 72 // Returns a response for the given method call. Used to implement | |
| 73 // CallMethodAndBlock() for |mock_proxy_|. | |
| 74 dbus::Response* CreateMockProxyResponse(dbus::MethodCall* method_call, | |
| 75 int timeout_ms) { | |
| 76 if (method_call->GetInterface() == "org.chromium.TestInterface" && | |
| 77 method_call->GetMember() == "Echo") { | |
| 78 dbus::MessageReader reader(method_call); | |
| 79 std::string text_message; | |
| 80 if (reader.PopString(&text_message)) { | |
| 81 dbus::Response* response = dbus::Response::CreateEmpty(); | |
| 82 dbus::MessageWriter writer(response); | |
| 83 writer.AppendString(text_message); | |
| 84 return response; | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); | |
| 89 return NULL; | |
| 90 } | |
| 91 | |
| 92 // Runs the given task. | |
| 93 void RunTask(const tracked_objects::Location& from_here, | |
| 94 const base::Closure& task) { | |
| 95 task.Run(); | |
| 96 } | |
| 97 }; | |
| 98 | |
| 99 TEST_F(BlockingMethodCallerTest, Echo) { | |
| 100 const char kHello[] = "Hello"; | |
| 101 // Get an object proxy from the mock bus. | |
| 102 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy( | |
| 103 "org.chromium.TestService", | |
| 104 dbus::ObjectPath("/org/chromium/TestObject")); | |
| 105 | |
| 106 // Create a method call. | |
| 107 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); | |
| 108 dbus::MessageWriter writer(&method_call); | |
| 109 writer.AppendString(kHello); | |
| 110 | |
| 111 // Call the method. | |
| 112 BlockingMethodCaller blocking_method_caller(mock_bus_.get(), proxy); | |
| 113 scoped_ptr<dbus::Response> response( | |
| 114 blocking_method_caller.CallMethodAndBlock(&method_call)); | |
| 115 | |
| 116 // Check the response. | |
| 117 ASSERT_TRUE(response.get()); | |
| 118 dbus::MessageReader reader(response.get()); | |
| 119 std::string text_message; | |
| 120 ASSERT_TRUE(reader.PopString(&text_message)); | |
| 121 // The text message should be echo'ed back. | |
| 122 EXPECT_EQ(kHello, text_message); | |
| 123 } | |
| 124 | |
| 125 } // namespace chromeos | |
| OLD | NEW |