diff options
author | nona@chromium.org <nona@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-18 04:11:48 +0000 |
---|---|---|
committer | nona@chromium.org <nona@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-18 04:11:48 +0000 |
commit | f2dca75a889abb7125ad89115021704d07916602 (patch) | |
tree | bd8f76c60d4d7297eea7b52636e643f7585a4fe8 /chromeos | |
parent | 078b495723f08970d364b8a6932533e3fe34aa5e (diff) | |
download | chromium_src-f2dca75a889abb7125ad89115021704d07916602.zip chromium_src-f2dca75a889abb7125ad89115021704d07916602.tar.gz chromium_src-f2dca75a889abb7125ad89115021704d07916602.tar.bz2 |
Implement IBusEngineFactoryService
With this CL, these classes are compiled and tested but not in use production binary at this moment.
BUG=126947
TEST=chromeos_unittests,ui_unittests
Review URL: https://chromiumcodereview.appspot.com/10693144
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@147176 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chromeos')
-rw-r--r-- | chromeos/chromeos.gyp | 5 | ||||
-rw-r--r-- | chromeos/dbus/ibus/ibus_constants.h | 10 | ||||
-rw-r--r-- | chromeos/dbus/ibus/ibus_engine_factory_service.cc | 124 | ||||
-rw-r--r-- | chromeos/dbus/ibus/ibus_engine_factory_service.h | 56 | ||||
-rw-r--r-- | chromeos/dbus/ibus/ibus_engine_factory_service_unittest.cc | 152 | ||||
-rw-r--r-- | chromeos/dbus/ibus/mock_ibus_engine_factory_service.cc | 22 | ||||
-rw-r--r-- | chromeos/dbus/ibus/mock_ibus_engine_factory_service.h | 26 |
7 files changed, 395 insertions, 0 deletions
diff --git a/chromeos/chromeos.gyp b/chromeos/chromeos.gyp index 7302a56..ce37e2e 100644 --- a/chromeos/chromeos.gyp +++ b/chromeos/chromeos.gyp @@ -79,6 +79,8 @@ 'dbus/ibus/ibus_constants.h', 'dbus/ibus/ibus_engine_service.cc', 'dbus/ibus/ibus_engine_service.h', + 'dbus/ibus/ibus_engine_factory_service.cc', + 'dbus/ibus/ibus_engine_factory_service.h', 'dbus/ibus/ibus_lookup_table.cc', 'dbus/ibus/ibus_lookup_table.h', 'dbus/ibus/ibus_object.cc', @@ -199,6 +201,8 @@ 'dbus/ibus/mock_ibus_client.h', 'dbus/ibus/mock_ibus_input_context_client.cc', 'dbus/ibus/mock_ibus_input_context_client.h', + 'dbus/ibus/mock_ibus_engine_factory_service.cc', + 'dbus/ibus/mock_ibus_engine_factory_service.h', 'dbus/ibus/mock_ibus_engine_service.cc', 'dbus/ibus/mock_ibus_engine_service.h', ], @@ -231,6 +235,7 @@ 'dbus/gsm_sms_client_unittest.cc', 'dbus/ibus/ibus_client_unittest.cc', 'dbus/ibus/ibus_component_unittest.cc', + 'dbus/ibus/ibus_engine_factory_service_unittest.cc', 'dbus/ibus/ibus_lookup_table_unittest.cc', 'dbus/ibus/ibus_object_unittest.cc', 'dbus/ibus/ibus_property_unittest.cc', diff --git a/chromeos/dbus/ibus/ibus_constants.h b/chromeos/dbus/ibus/ibus_constants.h index 4890cb9..86e827e 100644 --- a/chromeos/dbus/ibus/ibus_constants.h +++ b/chromeos/dbus/ibus/ibus_constants.h @@ -33,6 +33,16 @@ const char kSetCursorLocationMethod[] = "SetCursorLocation"; const char kProcessKeyEventMethod[] = "ProcessKeyEvent"; } // namespace input_context +namespace engine_factory { +const char kServicePath[] = "/org/freedesktop/IBus/Factory"; +const char kServiceInterface[] = "org.freedesktop.IBus.Factory"; +const char kCreateEngineMethod[] = "CreateEngine"; +} // namespace engine factory + +namespace engine { +const char kServicePathPrefix[] = "/org/freedesktop/IBus/Engine/"; +} // namespace engine + } // namespace ibus } // namespace chromeos diff --git a/chromeos/dbus/ibus/ibus_engine_factory_service.cc b/chromeos/dbus/ibus/ibus_engine_factory_service.cc new file mode 100644 index 0000000..9178b4d --- /dev/null +++ b/chromeos/dbus/ibus/ibus_engine_factory_service.cc @@ -0,0 +1,124 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chromeos/dbus/ibus/ibus_engine_factory_service.h" + +#include "base/string_number_conversions.h" +#include "chromeos/dbus/ibus/ibus_constants.h" +#include "dbus/bus.h" +#include "dbus/message.h" +#include "dbus/exported_object.h" + +namespace chromeos { + +class IBusEngineFactoryServiceImpl : public IBusEngineFactoryService { + public: + explicit IBusEngineFactoryServiceImpl(dbus::Bus* bus) + : bus_(bus), + weak_ptr_factory_(this) { + + exported_object_ = bus_->GetExportedObject( + dbus::ObjectPath(ibus::engine_factory::kServicePath)); + + exported_object_->ExportMethod( + ibus::engine_factory::kServiceInterface, + ibus::engine_factory::kCreateEngineMethod, + base::Bind(&IBusEngineFactoryServiceImpl::CreateEngine, + weak_ptr_factory_.GetWeakPtr()), + base::Bind(&IBusEngineFactoryServiceImpl::CreateEngineExported, + weak_ptr_factory_.GetWeakPtr())); + } + + virtual ~IBusEngineFactoryServiceImpl() { + bus_->UnregisterExportedObject(dbus::ObjectPath( + ibus::engine_factory::kServicePath)); + } + + // IBusEngineFactoryService override. + virtual void SetCreateEngineHandler( + const CreateEngineHandler& create_engine_handler) OVERRIDE { + DCHECK(!create_engine_handler.is_null()); + create_engine_handler_ = create_engine_handler; + } + + // IBusEngineFactoryService override. + virtual void UnsetCreateEngineHandler() OVERRIDE { + create_engine_handler_.Reset(); + } + + private: + // Called when the ibus-daemon requires new engine instance. + void CreateEngine(dbus::MethodCall* method_call, + dbus::ExportedObject::ResponseSender response_sender) { + if (!method_call) { + LOG(ERROR) << "method call does not have any arguments."; + return; + } + dbus::MessageReader reader(method_call); + std::string engine_name; + + if (!reader.PopString(&engine_name)) { + LOG(ERROR) << "Expected argument is string."; + return; + } + if(create_engine_handler_.is_null()) { + LOG(WARNING) << "The CreateEngine handler is NULL."; + } else { + const dbus::ObjectPath path = create_engine_handler_.Run(engine_name); + scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); + dbus::MessageWriter writer(response.get()); + writer.AppendObjectPath(path); + response_sender.Run(response.get()); + } + } + + // Called when the CreateEngine method is exported. + void CreateEngineExported(const std::string& interface_name, + const std::string& method_name, + bool success) { + DCHECK(success) << "Failed to export: " + << interface_name << "." << method_name; + } + + // CreateEngine method call handler. + CreateEngineHandler create_engine_handler_; + + dbus::Bus* bus_; + scoped_refptr<dbus::ExportedObject> exported_object_; + base::WeakPtrFactory<IBusEngineFactoryServiceImpl> weak_ptr_factory_; + + DISALLOW_COPY_AND_ASSIGN(IBusEngineFactoryServiceImpl); +}; + +class IBusEngineFactoryServiceStubImpl : public IBusEngineFactoryService { + public: + IBusEngineFactoryServiceStubImpl() {} + virtual ~IBusEngineFactoryServiceStubImpl() {} + + // IBusEngineFactoryService overrides. + virtual void SetCreateEngineHandler( + const CreateEngineHandler& create_engine_handler) OVERRIDE {} + virtual void UnsetCreateEngineHandler() OVERRIDE {} + + private: + DISALLOW_COPY_AND_ASSIGN(IBusEngineFactoryServiceStubImpl); +}; + +IBusEngineFactoryService::IBusEngineFactoryService() { +} + +IBusEngineFactoryService::~IBusEngineFactoryService() { +} + +// static +IBusEngineFactoryService* IBusEngineFactoryService::Create( + dbus::Bus* bus, + DBusClientImplementationType type) { + if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) + return new IBusEngineFactoryServiceImpl(bus); + else + return new IBusEngineFactoryServiceStubImpl(); +} + +} // namespace chromeos diff --git a/chromeos/dbus/ibus/ibus_engine_factory_service.h b/chromeos/dbus/ibus/ibus_engine_factory_service.h new file mode 100644 index 0000000..43f2dd8 --- /dev/null +++ b/chromeos/dbus/ibus/ibus_engine_factory_service.h @@ -0,0 +1,56 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROMEOS_DBUS_IBUS_IBUS_ENGINE_FACTORY_SERVICE_H_ +#define CHROMEOS_DBUS_IBUS_IBUS_ENGINE_FACTORY_SERVICE_H_ + +#include "base/bind.h" +#include "base/callback.h" +#include "chromeos/chromeos_export.h" +#include "chromeos/dbus/dbus_client_implementation_type.h" + +namespace dbus { +class Bus; +class ObjectPath; +} // namespace dbus + +namespace chromeos { + +// A class to make the actual DBus method call handling for IBusEngineFactory +// service. The exported method call is used by ibus-daemon to create engine +// service if the extension IME is enabled. +class CHROMEOS_EXPORT IBusEngineFactoryService { + public: + // The CreateEngine message accepts |engine_id| which identifies the engine + // module(ex. "mozc"), and returns an ObjectPath. The ibus-daemon will send + // dbus message to returned ObjectPath for engine processing. + typedef base::Callback<dbus::ObjectPath (const std::string& engine_id)> + CreateEngineHandler; + + virtual ~IBusEngineFactoryService(); + + // Sets CreateEngine method call handler. + virtual void SetCreateEngineHandler( + const CreateEngineHandler& create_engine_handler) = 0; + + // Unsets CreateEngine method call handler. + virtual void UnsetCreateEngineHandler() = 0; + + // Factory function, creates a new instance and returns ownership. + // For normal usage, accesses the sigleton via DBusThreadManager::Get(). + static CHROMEOS_EXPORT IBusEngineFactoryService* Create( + dbus::Bus* bus, + DBusClientImplementationType type); + + protected: + // Create() should be used instead. + IBusEngineFactoryService(); + + private: + DISALLOW_COPY_AND_ASSIGN(IBusEngineFactoryService); +}; + +} // namespace chromeos + +#endif // CHROMEOS_DBUS_IBUS_IBUS_ENGINE_FACTORY_SERVICE_H_ diff --git a/chromeos/dbus/ibus/ibus_engine_factory_service_unittest.cc b/chromeos/dbus/ibus/ibus_engine_factory_service_unittest.cc new file mode 100644 index 0000000..adbe958 --- /dev/null +++ b/chromeos/dbus/ibus/ibus_engine_factory_service_unittest.cc @@ -0,0 +1,152 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chromeos/dbus/ibus/ibus_engine_factory_service.h" + +#include "base/message_loop.h" +#include "chromeos/dbus/ibus/ibus_constants.h" +#include "dbus/message.h" +#include "dbus/mock_bus.h" +#include "dbus/mock_exported_object.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +using ::testing::Invoke; +using ::testing::Return; +using ::testing::StrEq; +using ::testing::_; + +namespace chromeos { + +class MockCreateEngineHandler { + public: + MOCK_METHOD1(Run, dbus::ObjectPath(const std::string& engine_name)); +}; + +class MockCreateEngineResponseSender { + public: + MockCreateEngineResponseSender(const dbus::ObjectPath expected_path) + : expected_path_(expected_path) {} + MOCK_METHOD1(Run, void(dbus::Response*)); + + // Checks the given |response| meets expectation for the CreateEngine method. + void CheckCreateEngineResponse(dbus::Response* response) { + dbus::MessageReader reader(response); + dbus::ObjectPath actual_path; + ASSERT_TRUE(reader.PopObjectPath(&actual_path)); + EXPECT_EQ(expected_path_, actual_path); + } + + private: + dbus::ObjectPath expected_path_; +}; + +class IBusEngineFactoryServiceTest : public testing::Test { + public: + IBusEngineFactoryServiceTest() {} + + virtual void SetUp() OVERRIDE { + // Create a mock bus. + dbus::Bus::Options options; + options.bus_type = dbus::Bus::SYSTEM; + mock_bus_ = new dbus::MockBus(options); + + // Create a mock exported object. + mock_exported_object_ = new dbus::MockExportedObject( + mock_bus_.get(), + dbus::ObjectPath(ibus::engine_factory::kServicePath)); + + EXPECT_CALL(*mock_bus_, + GetExportedObject(dbus::ObjectPath( + ibus::engine_factory::kServicePath))) + .WillOnce(Return(mock_exported_object_.get())); + + EXPECT_CALL(*mock_bus_, AssertOnOriginThread()) + .WillRepeatedly(Return()); + + EXPECT_CALL(*mock_exported_object_, ExportMethod( + ibus::engine_factory::kServiceInterface, + ibus::engine_factory::kCreateEngineMethod, + _, + _)) + .WillRepeatedly( + Invoke(this, &IBusEngineFactoryServiceTest::OnMethodExported)); + + service_.reset(IBusEngineFactoryService::Create( + mock_bus_, + REAL_DBUS_CLIENT_IMPLEMENTATION)); + } + + protected: + // The service to be tested. + scoped_ptr<IBusEngineFactoryService> service_; + // The mock bus. + scoped_refptr<dbus::MockBus> mock_bus_; + // The mock exported object. + scoped_refptr<dbus::MockExportedObject> mock_exported_object_; + // The map from method name to method call handler. + std::map<std::string, dbus::ExportedObject::MethodCallCallback> + method_exported_map_; + // A message loop to emulate asynchronous behavior. + MessageLoop message_loop_; + + private: + // Used to implement the method call exportation. + void OnMethodExported( + const std::string& interface_name, + const std::string& method_name, + const dbus::ExportedObject::MethodCallCallback& method_callback, + const dbus::ExportedObject::OnExportedCallback& on_exported_callback) { + method_exported_map_[method_name] = method_callback; + const bool success = true; + message_loop_.PostTask(FROM_HERE, base::Bind(on_exported_callback, + interface_name, + method_name, + success)); + } +}; + +TEST_F(IBusEngineFactoryServiceTest, CreateEngineTest) { + // 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)); + + // Set handler expectations. + MockCreateEngineHandler handler; + EXPECT_CALL(handler, Run(StrEq(kSampleEngine))) + .WillOnce(Return(kObjectPath)); + service_->SetCreateEngineHandler(base::Bind(&MockCreateEngineHandler::Run, + base::Unretained(&handler))); + message_loop_.RunAllPending(); + + // Invoke method call. + dbus::MethodCall method_call( + ibus::engine_factory::kServiceInterface, + ibus::engine_factory::kCreateEngineMethod); + 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(); + method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run( + &method_call, + base::Bind(&MockCreateEngineResponseSender::CheckCreateEngineResponse, + base::Unretained(&response_sender))); + + message_loop_.RunAllPending(); +} + +} // namespace chromeos diff --git a/chromeos/dbus/ibus/mock_ibus_engine_factory_service.cc b/chromeos/dbus/ibus/mock_ibus_engine_factory_service.cc new file mode 100644 index 0000000..f840e75 --- /dev/null +++ b/chromeos/dbus/ibus/mock_ibus_engine_factory_service.cc @@ -0,0 +1,22 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chromeos/dbus/ibus/mock_ibus_engine_factory_service.h" + +namespace chromeos { + +MockIBusEngineFactoryService::MockIBusEngineFactoryService() { +} + +MockIBusEngineFactoryService::~MockIBusEngineFactoryService() { +} + +void MockIBusEngineFactoryService::SetCreateEngineHandler( + const CreateEngineHandler& create_engine_handler) { +} + +void MockIBusEngineFactoryService::UnsetCreateEngineHandler() { +} + +} // namespace chromeos diff --git a/chromeos/dbus/ibus/mock_ibus_engine_factory_service.h b/chromeos/dbus/ibus/mock_ibus_engine_factory_service.h new file mode 100644 index 0000000..02eb80c --- /dev/null +++ b/chromeos/dbus/ibus/mock_ibus_engine_factory_service.h @@ -0,0 +1,26 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROMEOS_DBUS_IBUS_MOCK_IBUS_ENGINE_FACTORY_SERVICE_H_ +#define CHROMEOS_DBUS_IBUS_MOCK_IBUS_ENGINE_FACTORY_SERVICE_H_ + +#include "base/basictypes.h" +#include "chromeos/dbus/ibus/ibus_engine_factory_service.h" + +namespace chromeos { +class MockIBusEngineFactoryService : public IBusEngineFactoryService { + public: + MockIBusEngineFactoryService(); + virtual ~MockIBusEngineFactoryService(); + + virtual void SetCreateEngineHandler( + const CreateEngineHandler& create_engine_handler) OVERRIDE; + virtual void UnsetCreateEngineHandler() OVERRIDE; + + DISALLOW_COPY_AND_ASSIGN(MockIBusEngineFactoryService); +}; + +} // namespace chromeos + +#endif // CHROMEOS_DBUS_IBUS_MOCK_IBUS_ENGINE_FACTORY_SERVICE_H_ |