summaryrefslogtreecommitdiffstats
path: root/ash/ime
diff options
context:
space:
mode:
authoruekawa@chromium.org <uekawa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-02-14 15:16:52 +0000
committeruekawa@chromium.org <uekawa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-02-14 15:16:52 +0000
commit5ade0d0be4a611201bfd53bc82b317e0e24ac5df (patch)
treeaac847f07e813b2cf57da6ea6c7e96394f500ca4 /ash/ime
parent649ee534865eb0b689cb8311f453974d97e0ab9a (diff)
downloadchromium_src-5ade0d0be4a611201bfd53bc82b317e0e24ac5df.zip
chromium_src-5ade0d0be4a611201bfd53bc82b317e0e24ac5df.tar.gz
chromium_src-5ade0d0be4a611201bfd53bc82b317e0e24ac5df.tar.bz2
Reland of: Split out InputMethodMenuManager from InputMethodManager.
Rename input_method_property to input_method_menu_item, and move to ash/ime. This is a reland of https://codereview.chromium.org/150203015,reverted by https://codereview.chromium.org/165453002 Use Singleton<> instead of trying to maintain my own singleton. BUG=342336, 343044 Review URL: https://codereview.chromium.org/165783002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@251327 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ash/ime')
-rw-r--r--ash/ime/input_method_menu_item.cc54
-rw-r--r--ash/ime/input_method_menu_item.h44
-rw-r--r--ash/ime/input_method_menu_item_unittest.cc32
-rw-r--r--ash/ime/input_method_menu_manager.cc57
-rw-r--r--ash/ime/input_method_menu_manager.h65
-rw-r--r--ash/ime/input_method_menu_manager_unittest.cc76
6 files changed, 328 insertions, 0 deletions
diff --git a/ash/ime/input_method_menu_item.cc b/ash/ime/input_method_menu_item.cc
new file mode 100644
index 0000000..6337ec5
--- /dev/null
+++ b/ash/ime/input_method_menu_item.cc
@@ -0,0 +1,54 @@
+// Copyright 2014 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 "ash/ime/input_method_menu_item.h"
+
+#include <sstream>
+
+#include "base/logging.h"
+
+namespace ash {
+namespace ime {
+
+InputMethodMenuItem::InputMethodMenuItem(const std::string& in_key,
+ const std::string& in_label,
+ bool in_is_selection_item,
+ bool in_is_selection_item_checked)
+ : key(in_key),
+ label(in_label),
+ is_selection_item(in_is_selection_item),
+ is_selection_item_checked(in_is_selection_item_checked) {
+ DCHECK(!key.empty());
+}
+
+InputMethodMenuItem::InputMethodMenuItem()
+ : is_selection_item(false),
+ is_selection_item_checked(false) {
+}
+
+InputMethodMenuItem::~InputMethodMenuItem() {
+}
+
+bool InputMethodMenuItem::operator==(const InputMethodMenuItem& other) const {
+ return key == other.key &&
+ label == other.label &&
+ is_selection_item == other.is_selection_item &&
+ is_selection_item_checked == other.is_selection_item_checked;
+}
+
+bool InputMethodMenuItem::operator!=(const InputMethodMenuItem& other) const {
+ return !(*this == other);
+}
+
+std::string InputMethodMenuItem::ToString() const {
+ std::stringstream stream;
+ stream << "key=" << key
+ << ", label=" << label
+ << ", is_selection_item=" << is_selection_item
+ << ", is_selection_item_checked=" << is_selection_item_checked;
+ return stream.str();
+}
+
+} // namespace ime
+} // namespace ash
diff --git a/ash/ime/input_method_menu_item.h b/ash/ime/input_method_menu_item.h
new file mode 100644
index 0000000..1407d0a
--- /dev/null
+++ b/ash/ime/input_method_menu_item.h
@@ -0,0 +1,44 @@
+// Copyright 2014 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 ASH_IME_INPUT_METHOD_MENU_ITEM_H_
+#define ASH_IME_INPUT_METHOD_MENU_ITEM_H_
+
+#include <string>
+#include <vector>
+#include "ash/ash_export.h"
+
+namespace ash {
+namespace ime {
+
+// A structure which represents a property for an input method engine.
+struct ASH_EXPORT InputMethodMenuItem {
+ InputMethodMenuItem(const std::string& in_key,
+ const std::string& in_label,
+ bool in_is_selection_item,
+ bool in_is_selection_item_checked);
+
+ InputMethodMenuItem();
+ ~InputMethodMenuItem();
+
+ bool operator==(const InputMethodMenuItem& other) const;
+ bool operator!=(const InputMethodMenuItem& other) const;
+
+ // Debug print function.
+ std::string ToString() const;
+
+ std::string key; // A key which identifies the property. Non-empty string.
+ // (e.g. "InputMode.HalfWidthKatakana")
+ std::string label; // A description of the property. Non-empty string.
+ // (e.g. "Switch to full punctuation mode", "Hiragana")
+ bool is_selection_item; // true if the property is a selection item.
+ bool is_selection_item_checked; // true if |is_selection_item| is true and
+ // the selection_item is selected.
+};
+typedef std::vector<InputMethodMenuItem> InputMethodMenuItemList;
+
+} // namespace ime
+} // namespace ash
+
+#endif // ASH_IME_INPUT_METHOD_MENU_ITEM_H_
diff --git a/ash/ime/input_method_menu_item_unittest.cc b/ash/ime/input_method_menu_item_unittest.cc
new file mode 100644
index 0000000..9ac7942
--- /dev/null
+++ b/ash/ime/input_method_menu_item_unittest.cc
@@ -0,0 +1,32 @@
+// Copyright 2014 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 "ash/ime/input_method_menu_item.h"
+
+#include "base/logging.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace ash {
+namespace ime {
+
+TEST(InputMethodMenuItemTest, TestOperatorEqual) {
+ InputMethodMenuItem empty;
+ InputMethodMenuItem reference("key", "label", true, true);
+
+ InputMethodMenuItem p1("X", "label", true, true);
+ InputMethodMenuItem p2("key", "X", true, true);
+ InputMethodMenuItem p3("key", "label", false, true);
+ InputMethodMenuItem p4("key", "label", true, false);
+
+ EXPECT_EQ(empty, empty);
+ EXPECT_EQ(reference, reference);
+ EXPECT_NE(reference, empty);
+ EXPECT_NE(reference, p1);
+ EXPECT_NE(reference, p2);
+ EXPECT_NE(reference, p3);
+ EXPECT_NE(reference, p4);
+}
+
+} // namespace ime
+} // namespace ash
diff --git a/ash/ime/input_method_menu_manager.cc b/ash/ime/input_method_menu_manager.cc
new file mode 100644
index 0000000..ee75621
--- /dev/null
+++ b/ash/ime/input_method_menu_manager.cc
@@ -0,0 +1,57 @@
+// Copyright 2014 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 "ash/ime/input_method_menu_manager.h"
+
+#include "base/logging.h"
+#include "base/memory/singleton.h"
+
+namespace ash {
+namespace ime {
+
+InputMethodMenuManager::InputMethodMenuManager()
+ : menu_list_(), observers_() {}
+
+InputMethodMenuManager::~InputMethodMenuManager() {}
+
+void InputMethodMenuManager::AddObserver(
+ InputMethodMenuManager::Observer* observer) {
+ observers_.AddObserver(observer);
+}
+
+void InputMethodMenuManager::RemoveObserver(
+ InputMethodMenuManager::Observer* observer) {
+ observers_.RemoveObserver(observer);
+}
+
+InputMethodMenuItemList
+InputMethodMenuManager::GetCurrentInputMethodMenuItemList() const {
+ return menu_list_;
+}
+
+void InputMethodMenuManager::SetCurrentInputMethodMenuItemList(
+ const InputMethodMenuItemList& menu_list) {
+ menu_list_ = menu_list;
+ FOR_EACH_OBSERVER(InputMethodMenuManager::Observer,
+ observers_,
+ InputMethodMenuItemChanged(this));
+}
+
+bool InputMethodMenuManager::HasInputMethodMenuItemForKey(
+ const std::string& key) const {
+ for (size_t i = 0; i < menu_list_.size(); ++i) {
+ if (menu_list_[i].key == key) {
+ return true;
+ }
+ }
+ return false;
+}
+
+// static
+InputMethodMenuManager* InputMethodMenuManager::GetInstance() {
+ return Singleton<InputMethodMenuManager>::get();
+}
+
+} // namespace ime
+} // namespace ash
diff --git a/ash/ime/input_method_menu_manager.h b/ash/ime/input_method_menu_manager.h
new file mode 100644
index 0000000..ffaeb480
--- /dev/null
+++ b/ash/ime/input_method_menu_manager.h
@@ -0,0 +1,65 @@
+// Copyright 2014 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 "ash/ash_export.h"
+
+#include "ash/ime/input_method_menu_item.h"
+#include "base/observer_list.h"
+
+#ifndef ASH_IME_INPUT_METHOD_MENU_MANAGER_H_
+#define ASH_IME_INPUT_METHOD_MENU_MANAGER_H_
+
+template<typename Type> struct DefaultSingletonTraits;
+
+namespace ash {
+namespace ime {
+
+class ASH_EXPORT InputMethodMenuManager {
+public:
+ class Observer {
+ public:
+ virtual ~Observer() {}
+
+ // Called when the list of menu items is changed.
+ virtual void InputMethodMenuItemChanged(
+ InputMethodMenuManager* manager) = 0;
+ };
+
+ ~InputMethodMenuManager();
+
+ void AddObserver(Observer* observer);
+ void RemoveObserver(Observer* observer);
+
+ // Obtains the singleton instance.
+ static InputMethodMenuManager* GetInstance();
+
+ // Sets the list of input method menu items. The list could be empty().
+ void SetCurrentInputMethodMenuItemList(
+ const InputMethodMenuItemList& menu_list);
+
+ // Gets the list of input method menu items. The list could be empty().
+ InputMethodMenuItemList GetCurrentInputMethodMenuItemList() const;
+
+ // True if the key exists in the menu_list_.
+ bool HasInputMethodMenuItemForKey(const std::string& key) const;
+
+ private:
+ InputMethodMenuManager();
+
+ // For Singleton to be able to construct an instance.
+ friend struct DefaultSingletonTraits<InputMethodMenuManager>;
+
+ // Menu item list of the input method. This is set by extension IMEs.
+ InputMethodMenuItemList menu_list_;
+
+ // Observers who will be notified when menu changes.
+ ObserverList<Observer> observers_;
+
+ DISALLOW_COPY_AND_ASSIGN(InputMethodMenuManager);
+};
+
+} // namespace ime
+} // namespace ash
+
+#endif // ASH_IME_INPUT_METHOD_MENU_MANAGER_H_
diff --git a/ash/ime/input_method_menu_manager_unittest.cc b/ash/ime/input_method_menu_manager_unittest.cc
new file mode 100644
index 0000000..ba92001
--- /dev/null
+++ b/ash/ime/input_method_menu_manager_unittest.cc
@@ -0,0 +1,76 @@
+// Copyright 2014 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 "ash/ime/input_method_menu_manager.h"
+
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace ash {
+namespace ime {
+
+TEST(InputMethodMenuManagerTest, TestGetSingleton) {
+ EXPECT_TRUE(InputMethodMenuManager::GetInstance());
+}
+
+class MockObserver : public InputMethodMenuManager::Observer {
+ public:
+ MockObserver() : input_method_menu_item_changed_count_(0) {}
+ virtual ~MockObserver() {}
+
+ // Called when the list of menu items is changed.
+ virtual void InputMethodMenuItemChanged(
+ InputMethodMenuManager* manager) OVERRIDE {
+ input_method_menu_item_changed_count_++;
+ }
+ int input_method_menu_item_changed_count_;
+};
+
+class InputMethodMenuManagerStatefulTest : public testing::Test{
+ public:
+ InputMethodMenuManagerStatefulTest()
+ : observer_(new MockObserver()) {}
+ virtual ~InputMethodMenuManagerStatefulTest() {}
+ virtual void SetUp() OVERRIDE {
+ menu_manager_ = InputMethodMenuManager::GetInstance();
+ menu_manager_->AddObserver(observer_.get());
+ }
+
+ virtual void TearDown() OVERRIDE {
+ menu_manager_->RemoveObserver(observer_.get());
+ }
+
+ InputMethodMenuManager* menu_manager_;
+ scoped_ptr<MockObserver> observer_;
+};
+
+TEST_F(InputMethodMenuManagerStatefulTest, AddAndObserve) {
+ EXPECT_EQ(observer_->input_method_menu_item_changed_count_, 0);
+ menu_manager_->SetCurrentInputMethodMenuItemList(InputMethodMenuItemList());
+ EXPECT_EQ(observer_->input_method_menu_item_changed_count_, 1);
+}
+
+TEST_F(InputMethodMenuManagerStatefulTest, AddAndCheckExists) {
+ InputMethodMenuItemList list;
+ list.push_back(InputMethodMenuItem("key1", "label1", false, false));
+ list.push_back(InputMethodMenuItem("key2", "label2", false, false));
+ menu_manager_->SetCurrentInputMethodMenuItemList(list);
+ EXPECT_EQ(menu_manager_->GetCurrentInputMethodMenuItemList().size(), 2U);
+ EXPECT_EQ(
+ menu_manager_->GetCurrentInputMethodMenuItemList().at(0).ToString(),
+ "key=key1, label=label1, "
+ "is_selection_item=0, is_selection_item_checked=0");
+ EXPECT_EQ(
+ menu_manager_->GetCurrentInputMethodMenuItemList().at(1).ToString(),
+ "key=key2, label=label2, "
+ "is_selection_item=0, is_selection_item_checked=0");
+
+ EXPECT_TRUE(menu_manager_->HasInputMethodMenuItemForKey("key1"));
+ EXPECT_TRUE(menu_manager_->HasInputMethodMenuItemForKey("key2"));
+ EXPECT_FALSE(menu_manager_->HasInputMethodMenuItemForKey("key-not-exist"));
+}
+
+} // namespace ime
+} // namespace ash