Commit 9acbdd88 authored by nona@chromium.org's avatar nona@chromium.org

Revise IBus related DBus module.

- IBusEngineFactoryService takes the ownership of engine_id - CreateEngine handler mapping.
- Make IBusEngineFactoryService::CreateEngine asynchronous, and introduce asynchronous unittest.

BUG=None
TEST=ran chromeos_unittests


Review URL: https://chromiumcodereview.appspot.com/10836047

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@149579 0039d316-1c4b-4281-b951-d872f2087c98
parent 43d43c98
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
#include "chromeos/dbus/ibus/ibus_engine_factory_service.h" #include "chromeos/dbus/ibus/ibus_engine_factory_service.h"
#include <map>
#include <string>
#include "base/string_number_conversions.h" #include "base/string_number_conversions.h"
#include "chromeos/dbus/ibus/ibus_constants.h" #include "chromeos/dbus/ibus/ibus_constants.h"
#include "dbus/bus.h" #include "dbus/bus.h"
...@@ -17,7 +20,6 @@ class IBusEngineFactoryServiceImpl : public IBusEngineFactoryService { ...@@ -17,7 +20,6 @@ class IBusEngineFactoryServiceImpl : public IBusEngineFactoryService {
explicit IBusEngineFactoryServiceImpl(dbus::Bus* bus) explicit IBusEngineFactoryServiceImpl(dbus::Bus* bus)
: bus_(bus), : bus_(bus),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
exported_object_ = bus_->GetExportedObject( exported_object_ = bus_->GetExportedObject(
dbus::ObjectPath(ibus::engine_factory::kServicePath)); dbus::ObjectPath(ibus::engine_factory::kServicePath));
...@@ -37,14 +39,16 @@ class IBusEngineFactoryServiceImpl : public IBusEngineFactoryService { ...@@ -37,14 +39,16 @@ class IBusEngineFactoryServiceImpl : public IBusEngineFactoryService {
// IBusEngineFactoryService override. // IBusEngineFactoryService override.
virtual void SetCreateEngineHandler( virtual void SetCreateEngineHandler(
const std::string& engine_id,
const CreateEngineHandler& create_engine_handler) OVERRIDE { const CreateEngineHandler& create_engine_handler) OVERRIDE {
DCHECK(!create_engine_handler.is_null()); DCHECK(!create_engine_callback_map_[engine_id].is_null());
create_engine_handler_ = create_engine_handler; create_engine_callback_map_[engine_id] = create_engine_handler;
} }
// IBusEngineFactoryService override. // IBusEngineFactoryService override.
virtual void UnsetCreateEngineHandler() OVERRIDE { virtual void UnsetCreateEngineHandler(
create_engine_handler_.Reset(); const std::string& engine_id) OVERRIDE {
create_engine_callback_map_[engine_id].Reset();
} }
private: private:
...@@ -62,17 +66,28 @@ class IBusEngineFactoryServiceImpl : public IBusEngineFactoryService { ...@@ -62,17 +66,28 @@ class IBusEngineFactoryServiceImpl : public IBusEngineFactoryService {
LOG(ERROR) << "Expected argument is string."; LOG(ERROR) << "Expected argument is string.";
return; return;
} }
if(create_engine_handler_.is_null()) { if (create_engine_callback_map_[engine_name].is_null()) {
LOG(WARNING) << "The CreateEngine handler is NULL."; LOG(WARNING) << "The CreateEngine handler for " << engine_name
<< " is NULL.";
} else { } else {
dbus::Response* response = dbus::Response::FromMethodCall(method_call); create_engine_callback_map_[engine_name].Run(
dbus::MessageWriter writer(response); base::Bind(&IBusEngineFactoryServiceImpl::CreateEngineSendReply,
const dbus::ObjectPath path = create_engine_handler_.Run(engine_name); weak_ptr_factory_.GetWeakPtr(),
writer.AppendObjectPath(path); dbus::Response::FromMethodCall(method_call),
response_sender.Run(response); response_sender));
} }
} }
// Sends reply message for CreateEngine method call.
void CreateEngineSendReply(
dbus::Response* response,
const dbus::ExportedObject::ResponseSender response_sender,
const dbus::ObjectPath& path) {
dbus::MessageWriter writer(response);
writer.AppendObjectPath(path);
response_sender.Run(response);
}
// Called when the CreateEngine method is exported. // Called when the CreateEngine method is exported.
void CreateEngineExported(const std::string& interface_name, void CreateEngineExported(const std::string& interface_name,
const std::string& method_name, const std::string& method_name,
...@@ -82,7 +97,7 @@ class IBusEngineFactoryServiceImpl : public IBusEngineFactoryService { ...@@ -82,7 +97,7 @@ class IBusEngineFactoryServiceImpl : public IBusEngineFactoryService {
} }
// CreateEngine method call handler. // CreateEngine method call handler.
CreateEngineHandler create_engine_handler_; std::map<std::string, CreateEngineHandler> create_engine_callback_map_;
dbus::Bus* bus_; dbus::Bus* bus_;
scoped_refptr<dbus::ExportedObject> exported_object_; scoped_refptr<dbus::ExportedObject> exported_object_;
...@@ -98,8 +113,10 @@ class IBusEngineFactoryServiceStubImpl : public IBusEngineFactoryService { ...@@ -98,8 +113,10 @@ class IBusEngineFactoryServiceStubImpl : public IBusEngineFactoryService {
// IBusEngineFactoryService overrides. // IBusEngineFactoryService overrides.
virtual void SetCreateEngineHandler( virtual void SetCreateEngineHandler(
const std::string& engine_id,
const CreateEngineHandler& create_engine_handler) OVERRIDE {} const CreateEngineHandler& create_engine_handler) OVERRIDE {}
virtual void UnsetCreateEngineHandler() OVERRIDE {} virtual void UnsetCreateEngineHandler(
const std::string& engine_id) OVERRIDE {}
private: private:
DISALLOW_COPY_AND_ASSIGN(IBusEngineFactoryServiceStubImpl); DISALLOW_COPY_AND_ASSIGN(IBusEngineFactoryServiceStubImpl);
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROMEOS_DBUS_IBUS_IBUS_ENGINE_FACTORY_SERVICE_H_ #ifndef CHROMEOS_DBUS_IBUS_IBUS_ENGINE_FACTORY_SERVICE_H_
#define CHROMEOS_DBUS_IBUS_IBUS_ENGINE_FACTORY_SERVICE_H_ #define CHROMEOS_DBUS_IBUS_IBUS_ENGINE_FACTORY_SERVICE_H_
#include <string>
#include "base/bind.h" #include "base/bind.h"
#include "base/callback.h" #include "base/callback.h"
#include "chromeos/chromeos_export.h" #include "chromeos/chromeos_export.h"
...@@ -22,20 +23,22 @@ namespace chromeos { ...@@ -22,20 +23,22 @@ namespace chromeos {
// service if the extension IME is enabled. // service if the extension IME is enabled.
class CHROMEOS_EXPORT IBusEngineFactoryService { class CHROMEOS_EXPORT IBusEngineFactoryService {
public: public:
// The CreateEngine message accepts |engine_id| which identifies the engine typedef base::Callback<void(const dbus::ObjectPath& path)>
// module(ex. "mozc"), and returns an ObjectPath. The ibus-daemon will send CreateEngineResponseSender;
// dbus message to returned ObjectPath for engine processing. typedef base::Callback<void(const CreateEngineResponseSender& sender)>
typedef base::Callback<dbus::ObjectPath (const std::string& engine_id)>
CreateEngineHandler; CreateEngineHandler;
virtual ~IBusEngineFactoryService(); virtual ~IBusEngineFactoryService();
// Sets CreateEngine method call handler. // Sets CreateEngine method call handler for |engine_id|. If ibus-daemon calls
// CreateEngine message with |engine_id|, the |create_engine_handler| will be
// called.
virtual void SetCreateEngineHandler( virtual void SetCreateEngineHandler(
const std::string& engine_id,
const CreateEngineHandler& create_engine_handler) = 0; const CreateEngineHandler& create_engine_handler) = 0;
// Unsets CreateEngine method call handler. // Unsets CreateEngine method call handler for |engine_id|.
virtual void UnsetCreateEngineHandler() = 0; virtual void UnsetCreateEngineHandler(const std::string& engine_id) = 0;
// Factory function, creates a new instance and returns ownership. // Factory function, creates a new instance and returns ownership.
// For normal usage, accesses the sigleton via DBusThreadManager::Get(). // For normal usage, accesses the sigleton via DBusThreadManager::Get().
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chromeos/dbus/ibus/ibus_engine_factory_service.h" #include "chromeos/dbus/ibus/ibus_engine_factory_service.h"
#include <map>
#include <string>
#include "base/message_loop.h" #include "base/message_loop.h"
#include "chromeos/dbus/ibus/ibus_constants.h" #include "chromeos/dbus/ibus/ibus_constants.h"
#include "dbus/message.h" #include "dbus/message.h"
...@@ -19,14 +21,42 @@ using ::testing::_; ...@@ -19,14 +21,42 @@ using ::testing::_;
namespace chromeos { namespace chromeos {
class MockCreateEngineHandler { class SynchronousCreateEngineHandler {
public: public:
MOCK_METHOD1(Run, dbus::ObjectPath(const std::string& engine_name)); explicit SynchronousCreateEngineHandler(const dbus::ObjectPath& path)
: path_(path) {}
void Run(const IBusEngineFactoryService::CreateEngineResponseSender& sender) {
sender.Run(path_);
}
private:
dbus::ObjectPath path_;
DISALLOW_COPY_AND_ASSIGN(SynchronousCreateEngineHandler);
};
class AsynchronousCreateEngineHandler {
public:
AsynchronousCreateEngineHandler(const dbus::ObjectPath& path,
MessageLoop* message_loop)
: path_(path),
message_loop_(message_loop) {}
void Run(const IBusEngineFactoryService::CreateEngineResponseSender& sender) {
message_loop_->PostTask(FROM_HERE, base::Bind(sender, path_));
}
private:
dbus::ObjectPath path_;
MessageLoop* message_loop_;
DISALLOW_COPY_AND_ASSIGN(AsynchronousCreateEngineHandler);
}; };
class MockCreateEngineResponseSender { class MockCreateEngineResponseSender {
public: public:
MockCreateEngineResponseSender(const dbus::ObjectPath expected_path) explicit MockCreateEngineResponseSender(const dbus::ObjectPath& expected_path)
: expected_path_(expected_path) {} : expected_path_(expected_path) {}
MOCK_METHOD1(Run, void(dbus::Response*)); MOCK_METHOD1(Run, void(dbus::Response*));
...@@ -108,7 +138,7 @@ class IBusEngineFactoryServiceTest : public testing::Test { ...@@ -108,7 +138,7 @@ class IBusEngineFactoryServiceTest : public testing::Test {
} }
}; };
TEST_F(IBusEngineFactoryServiceTest, CreateEngineTest) { TEST_F(IBusEngineFactoryServiceTest, SyncCreateEngineTest) {
// Set expectations. // Set expectations.
const char kSampleEngine[] = "Sample Engine"; const char kSampleEngine[] = "Sample Engine";
const dbus::ObjectPath kObjectPath("/org/freedesktop/IBus/Engine/10"); const dbus::ObjectPath kObjectPath("/org/freedesktop/IBus/Engine/10");
...@@ -118,12 +148,12 @@ TEST_F(IBusEngineFactoryServiceTest, CreateEngineTest) { ...@@ -118,12 +148,12 @@ TEST_F(IBusEngineFactoryServiceTest, CreateEngineTest) {
Invoke(&response_sender, Invoke(&response_sender,
&MockCreateEngineResponseSender::CheckCreateEngineResponse)); &MockCreateEngineResponseSender::CheckCreateEngineResponse));
SynchronousCreateEngineHandler handler(kObjectPath);
// Set handler expectations. // Set handler expectations.
MockCreateEngineHandler handler; service_->SetCreateEngineHandler(
EXPECT_CALL(handler, Run(StrEq(kSampleEngine))) kSampleEngine,
.WillOnce(Return(kObjectPath)); base::Bind(&SynchronousCreateEngineHandler::Run,
service_->SetCreateEngineHandler(base::Bind(&MockCreateEngineHandler::Run, base::Unretained(&handler)));
base::Unretained(&handler)));
message_loop_.RunAllPending(); message_loop_.RunAllPending();
// Invoke method call. // Invoke method call.
...@@ -142,12 +172,53 @@ TEST_F(IBusEngineFactoryServiceTest, CreateEngineTest) { ...@@ -142,12 +172,53 @@ TEST_F(IBusEngineFactoryServiceTest, CreateEngineTest) {
base::Unretained(&response_sender))); base::Unretained(&response_sender)));
// Unset the handler so expect not calling handler. // Unset the handler so expect not calling handler.
service_->UnsetCreateEngineHandler(); service_->UnsetCreateEngineHandler(kSampleEngine);
method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run( method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
&method_call, &method_call,
base::Bind(&MockCreateEngineResponseSender::CheckCreateEngineResponse, base::Bind(&MockCreateEngineResponseSender::CheckCreateEngineResponse,
base::Unretained(&response_sender))); base::Unretained(&response_sender)));
message_loop_.RunAllPending();
}
TEST_F(IBusEngineFactoryServiceTest, AsyncCreateEngineTest) {
// Set expectations.
const char kSampleEngine[] = "Sample Engine";
const dbus::ObjectPath kObjectPath("/org/freedesktop/IBus/Engine/10");
MockCreateEngineResponseSender response_sender(kObjectPath);
EXPECT_CALL(response_sender, Run(_))
.WillOnce(
Invoke(&response_sender,
&MockCreateEngineResponseSender::CheckCreateEngineResponse));
AsynchronousCreateEngineHandler handler(kObjectPath, &message_loop_);
// Set handler expectations.
service_->SetCreateEngineHandler(
kSampleEngine,
base::Bind(&AsynchronousCreateEngineHandler::Run,
base::Unretained(&handler)));
message_loop_.RunAllPending();
// Invoke method call.
dbus::MethodCall method_call(
ibus::engine_factory::kServiceInterface,
ibus::engine_factory::kCreateEngineMethod);
method_call.SetSerial(10);
dbus::MessageWriter writer(&method_call);
writer.AppendString(kSampleEngine);
ASSERT_FALSE(
method_exported_map_[ibus::engine_factory::kCreateEngineMethod]
.is_null());
method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
&method_call,
base::Bind(&MockCreateEngineResponseSender::Run,
base::Unretained(&response_sender)));
// Unset the handler so expect not calling handler.
service_->UnsetCreateEngineHandler(kSampleEngine);
method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
&method_call,
base::Bind(&MockCreateEngineResponseSender::CheckCreateEngineResponse,
base::Unretained(&response_sender)));
message_loop_.RunAllPending(); message_loop_.RunAllPending();
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include <string>
#include "chromeos/dbus/ibus/mock_ibus_engine_factory_service.h" #include "chromeos/dbus/ibus/mock_ibus_engine_factory_service.h"
namespace chromeos { namespace chromeos {
...@@ -13,10 +14,12 @@ MockIBusEngineFactoryService::~MockIBusEngineFactoryService() { ...@@ -13,10 +14,12 @@ MockIBusEngineFactoryService::~MockIBusEngineFactoryService() {
} }
void MockIBusEngineFactoryService::SetCreateEngineHandler( void MockIBusEngineFactoryService::SetCreateEngineHandler(
const std::string& engine_id,
const CreateEngineHandler& create_engine_handler) { const CreateEngineHandler& create_engine_handler) {
} }
void MockIBusEngineFactoryService::UnsetCreateEngineHandler() { void MockIBusEngineFactoryService::UnsetCreateEngineHandler(
const std::string& engine_id) {
} }
} // namespace chromeos } // namespace chromeos
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROMEOS_DBUS_IBUS_MOCK_IBUS_ENGINE_FACTORY_SERVICE_H_ #ifndef CHROMEOS_DBUS_IBUS_MOCK_IBUS_ENGINE_FACTORY_SERVICE_H_
#define CHROMEOS_DBUS_IBUS_MOCK_IBUS_ENGINE_FACTORY_SERVICE_H_ #define CHROMEOS_DBUS_IBUS_MOCK_IBUS_ENGINE_FACTORY_SERVICE_H_
#include <string>
#include "base/basictypes.h" #include "base/basictypes.h"
#include "chromeos/dbus/ibus/ibus_engine_factory_service.h" #include "chromeos/dbus/ibus/ibus_engine_factory_service.h"
...@@ -15,8 +16,10 @@ class MockIBusEngineFactoryService : public IBusEngineFactoryService { ...@@ -15,8 +16,10 @@ class MockIBusEngineFactoryService : public IBusEngineFactoryService {
virtual ~MockIBusEngineFactoryService(); virtual ~MockIBusEngineFactoryService();
virtual void SetCreateEngineHandler( virtual void SetCreateEngineHandler(
const std::string& engine_id,
const CreateEngineHandler& create_engine_handler) OVERRIDE; const CreateEngineHandler& create_engine_handler) OVERRIDE;
virtual void UnsetCreateEngineHandler() OVERRIDE; virtual void UnsetCreateEngineHandler(
const std::string& engine_id) OVERRIDE;
DISALLOW_COPY_AND_ASSIGN(MockIBusEngineFactoryService); DISALLOW_COPY_AND_ASSIGN(MockIBusEngineFactoryService);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment