Commit 20fd79ee authored by nona@chromium.org's avatar nona@chromium.org

Decouple l10n_util from InputMethodUtil.

This CL changes:
 - Moving localization related function into delegate class.
 - Removing GetLanguageDisplayNameFromCode and GetLanguageNativeDisplayNameFromCode from InputMethodUtil because they are just thin wrapper function of l10n_util and only used from CrosLanguageOptionHandler.

This CL does not change any test expectations except TestGetLanguageNativeDisplayNameFromCode for checking regressions.
And I'm going to refactor strings related stuff to more appropriate location.
So let me keep tests as is.

BUG=164375
TEST=ran unit_tests and also checked there is no regression with this CL on actual devices.

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@194927 0039d316-1c4b-4281-b951-d872f2087c98
parent 002aec6a
......@@ -8,6 +8,7 @@
#include "base/prefs/pref_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/pref_names.h"
#include "ui/base/l10n/l10n_util.h"
namespace chromeos {
namespace input_method {
......@@ -25,12 +26,17 @@ std::string InputMethodDelegateImpl::GetHardwareKeyboardLayout() const {
return std::string();
}
std::string InputMethodDelegateImpl::GetActiveLocale() const {
if (g_browser_process)
return g_browser_process->GetApplicationLocale();
string16 InputMethodDelegateImpl::GetLocalizedString(int resource_id) const {
return l10n_util::GetStringUTF16(resource_id);
}
NOTREACHED();
return std::string();
string16 InputMethodDelegateImpl::GetDisplayLanguageName(
const std::string& language_code) const {
DCHECK(g_browser_process);
return l10n_util::GetDisplayNameForLocale(
language_code,
g_browser_process->GetApplicationLocale(),
true);
}
} // namespace input_method
......
......@@ -22,7 +22,9 @@ class InputMethodDelegateImpl : public InputMethodDelegate {
// InputMethodDelegate implementation.
virtual std::string GetHardwareKeyboardLayout() const OVERRIDE;
virtual std::string GetActiveLocale() const OVERRIDE;
virtual string16 GetLocalizedString(int resource_id) const OVERRIDE;
virtual string16 GetDisplayLanguageName(
const std::string& language_code) const OVERRIDE;
private:
DISALLOW_COPY_AND_ASSIGN(InputMethodDelegateImpl);
......
......@@ -17,9 +17,9 @@
#include "chromeos/dbus/ibus/mock_ibus_input_context_client.h"
#include "chromeos/dbus/mock_dbus_thread_manager_without_gmock.h"
#include "chromeos/ime/extension_ime_util.h"
#include "chromeos/ime/fake_input_method_delegate.h"
#include "chromeos/ime/mock_component_extension_ime_manager_delegate.h"
#include "chromeos/ime/mock_ibus_daemon_controller.h"
#include "chromeos/ime/mock_input_method_delegate.h"
#include "chromeos/ime/mock_xkeyboard.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/accelerators/accelerator.h"
......@@ -52,7 +52,7 @@ class InputMethodManagerImplTest : public testing::Test {
mock_ibus_client_ = mock_dbus_thread_manager_->mock_ibus_client();
mock_ibus_input_context_client_ =
mock_dbus_thread_manager_->mock_ibus_input_context_client();
delegate_ = new MockInputMethodDelegate();
delegate_ = new FakeInputMethodDelegate();
manager_.reset(new InputMethodManagerImpl(
scoped_ptr<InputMethodDelegate>(delegate_)));
controller_ = new MockIBusController;
......@@ -113,7 +113,7 @@ class InputMethodManagerImplTest : public testing::Test {
}
scoped_ptr<InputMethodManagerImpl> manager_;
MockInputMethodDelegate* delegate_;
FakeInputMethodDelegate* delegate_;
MockIBusController* controller_;
MockCandidateWindowController* candidate_window_controller_;
MockIBusDaemonController* mock_ibus_daemon_controller_;
......
......@@ -18,10 +18,8 @@
#include "chromeos/ime/component_extension_ime_manager.h"
#include "chromeos/ime/extension_ime_util.h"
#include "chromeos/ime/input_method_delegate.h"
// TODO(nona): move this header from this file.
#include "grit/generated_resources.h"
#include "third_party/icu/public/common/unicode/uloc.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/l10n_util_collator.h"
namespace {
......@@ -259,29 +257,6 @@ const struct EnglishToResouceId {
const size_t kEnglishToResourceIdArraySize =
arraysize(kEnglishToResourceIdArray);
// The comparator is used for sorting language codes by their
// corresponding language names, using the ICU collator.
struct CompareLanguageCodesByLanguageName
: std::binary_function<const std::string&, const std::string&, bool> {
CompareLanguageCodesByLanguageName(InputMethodUtil* util,
icu::Collator* collator)
: util_(util), collator_(collator) {
}
// Calling GetLanguageDisplayNameFromCode() in the comparator is not
// efficient, but acceptable as the function is cheap, and the language
// list is short (about 60 at most).
bool operator()(const std::string& s1, const std::string& s2) const {
const string16 key1 = util_->GetLanguageDisplayNameFromCode(s1);
const string16 key2 = util_->GetLanguageDisplayNameFromCode(s2);
return l10n_util::StringComparator<string16>(collator_)(key1, key2);
}
private:
InputMethodUtil* util_;
icu::Collator* collator_;
};
// The list of language that do not have associated input methods in IBus.
// For these languages, we associate input methods here.
const struct ExtraLanguage {
......@@ -342,7 +317,7 @@ bool InputMethodUtil::TranslateStringInternal(
return false;
}
*out_string = l10n_util::GetStringUTF16(iter->second);
*out_string = delegate_->GetLocalizedString(iter->second);
return true;
}
......@@ -449,7 +424,7 @@ string16 InputMethodUtil::GetInputMethodMediumName(
for (size_t i = 0; i < kMappingImeIdToMediumLenNameResourceIdLen; ++i) {
if (kMappingImeIdToMediumLenNameResourceId[i].input_method_id ==
input_method.id()) {
return l10n_util::GetStringUTF16(
return delegate_->GetLocalizedString(
kMappingImeIdToMediumLenNameResourceId[i].resource_id);
}
}
......@@ -470,7 +445,7 @@ string16 InputMethodUtil::GetInputMethodLongName(
// keyboard layouts and share the same layout of keyboard (Belgian). We need
// to show explicitly the language for the layout. For Arabic, Amharic, and
// Indic languages: they share "Standard Input Method".
const string16 standard_input_method_text = l10n_util::GetStringUTF16(
const string16 standard_input_method_text = delegate_->GetLocalizedString(
IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD);
const std::string language_code = input_method.language_code();
......@@ -479,8 +454,8 @@ string16 InputMethodUtil::GetInputMethodLongName(
language_code == "de" ||
language_code == "fr" ||
language_code == "nl") {
const string16 language_name = l10n_util::GetDisplayNameForLocale(
language_code, delegate_->GetActiveLocale(), true);
const string16 language_name = delegate_->GetDisplayLanguageName(
language_code);
text = language_name + UTF8ToUTF16(" - ") + text;
}
......@@ -496,19 +471,6 @@ const InputMethodDescriptor* InputMethodUtil::GetInputMethodDescriptorFromId(
return (iter == id_to_descriptor_.end()) ? NULL : &(iter->second);
}
// static
string16 InputMethodUtil::GetLanguageDisplayNameFromCode(
const std::string& language_code) {
return l10n_util::GetDisplayNameForLocale(
language_code, delegate_->GetActiveLocale(), true);
}
// static
string16 InputMethodUtil::GetLanguageNativeDisplayNameFromCode(
const std::string& language_code) {
return l10n_util::GetDisplayNameForLocale(language_code, language_code, true);
}
std::vector<std::string> InputMethodUtil::GetExtraLanguageCodesFromId(
const std::string& input_method_id) const {
std::vector<std::string> result;
......
......@@ -103,21 +103,6 @@ class InputMethodUtil {
// Returns true if the given input method id is for a keyboard layout.
static bool IsKeyboardLayout(const std::string& input_method_id);
// Converts a language code to a language display name, using the
// current application locale. MaybeRewriteLanguageName() is called
// internally.
// Examples: "fi" => "Finnish"
// "en-US" => "English (United States)"
string16 GetLanguageDisplayNameFromCode(
const std::string& language_code);
// Converts a language code to a language native display name.
// MaybeRewriteLanguageName() is called internally.
// Examples: "fi" => "suomi" (rather than Finnish)
// "en-US" => "English (United States)"
static string16 GetLanguageNativeDisplayNameFromCode(
const std::string& language_code);
// Returns extra language code list associated with |input_method_id|. If
// there is no associated langauge code, this function returns empty list.
std::vector<std::string> GetExtraLanguageCodesFromId(
......
......@@ -6,11 +6,12 @@
#include <string>
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/chromeos/input_method/input_method_manager.h"
#include "chromeos/ime/fake_input_method_delegate.h"
#include "chromeos/ime/input_method_whitelist.h"
#include "chromeos/ime/mock_input_method_delegate.h"
#include "grit/generated_resources.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
......@@ -42,6 +43,10 @@ class InputMethodUtilTest : public testing::Test {
public:
InputMethodUtilTest()
: util_(&delegate_, whitelist_.GetSupportedInputMethods()) {
delegate_.set_get_localized_string_callback(
base::Bind(&l10n_util::GetStringUTF16));
delegate_.set_get_display_language_name_callback(
base::Bind(&InputMethodUtilTest::GetDisplayLanguageName));
}
InputMethodDescriptor GetDesc(const std::string& id,
......@@ -55,7 +60,11 @@ class InputMethodUtilTest : public testing::Test {
false);
}
MockInputMethodDelegate delegate_;
static string16 GetDisplayLanguageName(const std::string& language_code) {
return l10n_util::GetDisplayNameForLocale(language_code, "en", true);
}
FakeInputMethodDelegate delegate_;
InputMethodWhitelist whitelist_;
TestableInputMethodUtil util_;
};
......@@ -342,11 +351,6 @@ TEST_F(InputMethodUtilTest, TestGetInputMethodDescriptorFromId) {
EXPECT_EQ("zh-CN", descriptor->language_code());
}
TEST_F(InputMethodUtilTest, TestGetLanguageNativeDisplayNameFromCode) {
EXPECT_EQ(UTF8ToUTF16("suomi"),
InputMethodUtil::GetLanguageNativeDisplayNameFromCode("fi"));
}
TEST_F(InputMethodUtilTest, TestGetInputMethodIdsForLanguageCode) {
std::multimap<std::string, std::string> language_code_to_ids_map;
language_code_to_ids_map.insert(std::make_pair("ja", "mozc"));
......
......@@ -7,8 +7,8 @@
#include "chrome/browser/chromeos/input_method/input_method_manager.h"
#include "chrome/browser/chromeos/input_method/input_method_util.h"
#include "chromeos/ime/fake_input_method_delegate.h"
#include "chromeos/ime/input_method_whitelist.h"
#include "chromeos/ime/mock_input_method_delegate.h"
#include "chromeos/ime/mock_xkeyboard.h"
namespace chromeos {
......@@ -81,7 +81,7 @@ class MockInputMethodManager : public InputMethodManager {
std::string current_input_method_id_;
InputMethodWhitelist whitelist_;
MockInputMethodDelegate delegate_; // used by util_
FakeInputMethodDelegate delegate_; // used by util_
InputMethodUtil util_;
MockXKeyboard xkeyboard_;
......
......@@ -185,13 +185,11 @@ ListValue* CrosLanguageOptionsHandler::GetLanguageListInternal(
*iter) == base_language_codes.end()) {
continue;
}
input_method::InputMethodUtil* input_method_util =
input_method::GetInputMethodManager()->GetInputMethodUtil();
const string16 display_name =
input_method_util->GetLanguageDisplayNameFromCode(*iter);
l10n_util::GetDisplayNameForLocale(*iter, app_locale, true);
const string16 native_display_name =
input_method::InputMethodUtil::GetLanguageNativeDisplayNameFromCode(
*iter);
l10n_util::GetDisplayNameForLocale(*iter, *iter, true);
display_names.push_back(display_name);
language_map[display_name] =
std::make_pair(*iter, native_display_name);
......
......@@ -392,10 +392,10 @@
'dbus/ibus/mock_ibus_engine_service.h',
'dbus/ibus/mock_ibus_panel_service.cc',
'dbus/ibus/mock_ibus_panel_service.h',
'ime/fake_input_method_delegate.cc',
'ime/fake_input_method_delegate.h',
'ime/mock_ibus_daemon_controller.cc',
'ime/mock_ibus_daemon_controller.h',
'ime/mock_input_method_delegate.cc',
'ime/mock_input_method_delegate.h',
'ime/mock_xkeyboard.cc',
'ime/mock_xkeyboard.h',
],
......
......@@ -2,24 +2,33 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ime/mock_input_method_delegate.h"
#include "chromeos/ime/fake_input_method_delegate.h"
namespace chromeos {
namespace input_method {
MockInputMethodDelegate::MockInputMethodDelegate()
FakeInputMethodDelegate::FakeInputMethodDelegate()
: active_locale_("en") {
}
MockInputMethodDelegate::~MockInputMethodDelegate() {
FakeInputMethodDelegate::~FakeInputMethodDelegate() {
}
std::string MockInputMethodDelegate::GetHardwareKeyboardLayout() const {
std::string FakeInputMethodDelegate::GetHardwareKeyboardLayout() const {
return hardware_keyboard_layout_;
}
std::string MockInputMethodDelegate::GetActiveLocale() const {
return active_locale_;
string16 FakeInputMethodDelegate::GetLocalizedString(int resource_id) const {
if (!get_localized_string_callback_.is_null())
return get_localized_string_callback_.Run(resource_id);
return string16();
}
string16 FakeInputMethodDelegate::GetDisplayLanguageName(
const std::string& language_code) const {
if (!get_display_language_name_callback_.is_null())
return get_display_language_name_callback_.Run(language_code);
return string16();
}
} // namespace input_method
......
......@@ -2,10 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_IME_MOCK_INPUT_METHOD_DELEGATE_H_
#define CHROMEOS_IME_MOCK_INPUT_METHOD_DELEGATE_H_
#ifndef CHROMEOS_IME_FAKE_INPUT_METHOD_DELEGATE_H_
#define CHROMEOS_IME_FAKE_INPUT_METHOD_DELEGATE_H_
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/ime/input_method_delegate.h"
......@@ -13,14 +14,21 @@
namespace chromeos {
namespace input_method {
class CHROMEOS_EXPORT MockInputMethodDelegate : public InputMethodDelegate {
class CHROMEOS_EXPORT FakeInputMethodDelegate : public InputMethodDelegate {
public:
MockInputMethodDelegate();
virtual ~MockInputMethodDelegate();
typedef base::Callback<string16 (const std::string& language_code)>
LanguageNameLocalizationCallback;
typedef base::Callback<string16 (int resource_id)>
GetLocalizedStringCallback;
FakeInputMethodDelegate();
virtual ~FakeInputMethodDelegate();
// InputMethodDelegate implementation:
virtual std::string GetHardwareKeyboardLayout() const OVERRIDE;
virtual std::string GetActiveLocale() const OVERRIDE;
virtual string16 GetLocalizedString(int resource_id) const OVERRIDE;
virtual string16 GetDisplayLanguageName(
const std::string& language_code) const OVERRIDE;
void set_hardware_keyboard_layout(const std::string& value) {
hardware_keyboard_layout_ = value;
......@@ -30,13 +38,25 @@ class CHROMEOS_EXPORT MockInputMethodDelegate : public InputMethodDelegate {
active_locale_ = value;
}
void set_get_display_language_name_callback(
const LanguageNameLocalizationCallback& callback) {
get_display_language_name_callback_ = callback;
}
void set_get_localized_string_callback(
const GetLocalizedStringCallback& callback) {
get_localized_string_callback_ = callback;
}
private:
std::string hardware_keyboard_layout_;
std::string active_locale_;
DISALLOW_COPY_AND_ASSIGN(MockInputMethodDelegate);
LanguageNameLocalizationCallback get_display_language_name_callback_;
GetLocalizedStringCallback get_localized_string_callback_;
DISALLOW_COPY_AND_ASSIGN(FakeInputMethodDelegate);
};
} // namespace input_method
} // namespace chromeos
#endif // CHROMEOS_IME_MOCK_INPUT_METHOD_DELEGATE_H_
#endif // CHROMEOS_IME_FAKE_INPUT_METHOD_DELEGATE_H_
......@@ -8,6 +8,7 @@
#include <string>
#include "base/basictypes.h"
#include "base/string16.h"
namespace chromeos {
namespace input_method {
......@@ -21,8 +22,16 @@ class InputMethodDelegate {
// Retrieves the hardware keyboard layout ID. May return an empty string if
// the ID is unknown.
virtual std::string GetHardwareKeyboardLayout() const = 0;
// Retrieves the currently active UI locale.
virtual std::string GetActiveLocale() const = 0;
// Retrieves localized string for |resource_id|.
virtual string16 GetLocalizedString(int resource_id) const = 0;
// Converts a language code to a language display name, using the
// current application locale.
// Examples: "fi" => "Finnish"
// "en-US" => "English (United States)"
virtual string16 GetDisplayLanguageName(
const std::string& language_code) const = 0;
private:
DISALLOW_COPY_AND_ASSIGN(InputMethodDelegate);
......
......@@ -11,8 +11,6 @@
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "chromeos/ime/input_method_whitelist.h"
#include "chromeos/ime/mock_input_method_delegate.h"
#include "testing/gtest/include/gtest/gtest.h"
#include <X11/Xlib.h>
......@@ -35,7 +33,6 @@ class XKeyboardTest : public testing::Test {
xkey_.reset();
}
InputMethodWhitelist whitelist_;
scoped_ptr<XKeyboard> xkey_;
MessageLoopForUI message_loop_;
......
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