1 // Copyright 2017 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/input_method/mock_input_method_manager_impl.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "ui/base/ime/chromeos/input_method_util.h"
11 
12 namespace chromeos {
13 namespace input_method {
14 
State(MockInputMethodManagerImpl * manager)15 MockInputMethodManagerImpl::State::State(MockInputMethodManagerImpl* manager)
16     : manager_(manager) {
17   active_input_method_ids.emplace_back("xkb:us::eng");
18 }
19 
20 scoped_refptr<InputMethodManager::State>
Clone() const21 MockInputMethodManagerImpl::State::Clone() const {
22   NOTIMPLEMENTED();
23   return manager_->GetActiveIMEState();
24 }
25 
26 std::unique_ptr<InputMethodDescriptors>
GetActiveInputMethods() const27 MockInputMethodManagerImpl::State::GetActiveInputMethods() const {
28   std::unique_ptr<InputMethodDescriptors> result =
29       std::make_unique<InputMethodDescriptors>();
30   result->push_back(InputMethodUtil::GetFallbackInputMethodDescriptor());
31   return result;
32 }
33 
34 const InputMethodDescriptor*
GetInputMethodFromId(const std::string & input_method_id) const35 MockInputMethodManagerImpl::State::GetInputMethodFromId(
36     const std::string& input_method_id) const {
37   static const InputMethodDescriptor defaultInputMethod =
38       InputMethodUtil::GetFallbackInputMethodDescriptor();
39   for (const auto& active_input_method_id : active_input_method_ids) {
40     if (input_method_id == active_input_method_id) {
41       return &defaultInputMethod;
42     }
43   }
44   return nullptr;
45 }
46 
GetCurrentInputMethod() const47 InputMethodDescriptor MockInputMethodManagerImpl::State::GetCurrentInputMethod()
48     const {
49   InputMethodDescriptor descriptor =
50       InputMethodUtil::GetFallbackInputMethodDescriptor();
51   if (!current_input_method_id.empty()) {
52     return InputMethodDescriptor(
53         current_input_method_id, descriptor.name(), descriptor.indicator(),
54         descriptor.keyboard_layouts(), descriptor.language_codes(), true,
55         GURL(),   // options page url.
56         GURL());  // input view page url.
57   }
58   return descriptor;
59 }
60 
61 MockInputMethodManagerImpl::State::~State() = default;
62 
MockInputMethodManagerImpl()63 MockInputMethodManagerImpl::MockInputMethodManagerImpl()
64     : state_(new State(this)), util_(new InputMethodUtil(&delegate_)) {}
65 
66 MockInputMethodManagerImpl::~MockInputMethodManagerImpl() = default;
67 
AddObserver(InputMethodManager::Observer * observer)68 void MockInputMethodManagerImpl::AddObserver(
69     InputMethodManager::Observer* observer) {
70   ++add_observer_count_;
71 }
72 
AddImeMenuObserver(ImeMenuObserver * observer)73 void MockInputMethodManagerImpl::AddImeMenuObserver(ImeMenuObserver* observer) {
74   ++add_menu_observer_count_;
75 }
76 
RemoveObserver(InputMethodManager::Observer * observer)77 void MockInputMethodManagerImpl::RemoveObserver(
78     InputMethodManager::Observer* observer) {
79   ++remove_observer_count_;
80 }
81 
RemoveImeMenuObserver(ImeMenuObserver * observer)82 void MockInputMethodManagerImpl::RemoveImeMenuObserver(
83     ImeMenuObserver* observer) {
84   ++remove_menu_observer_count_;
85 }
86 
87 std::unique_ptr<InputMethodDescriptors>
GetSupportedInputMethods() const88 MockInputMethodManagerImpl::GetSupportedInputMethods() const {
89   std::unique_ptr<InputMethodDescriptors> result;
90 #if _LIBCPP_STD_VER > 11
91   result = std::make_unique<InputMethodDescriptors>();
92 #else
93   result.reset(new InputMethodDescriptors);
94 #endif
95   result->push_back(InputMethodUtil::GetFallbackInputMethodDescriptor());
96   return result;
97 }
98 
IsISOLevel5ShiftUsedByCurrentInputMethod() const99 bool MockInputMethodManagerImpl::IsISOLevel5ShiftUsedByCurrentInputMethod()
100     const {
101   return mod3_used_;
102 }
103 
GetImeKeyboard()104 ImeKeyboard* MockInputMethodManagerImpl::GetImeKeyboard() {
105   return &keyboard_;
106 }
107 
GetInputMethodUtil()108 InputMethodUtil* MockInputMethodManagerImpl::GetInputMethodUtil() {
109   return util_.get();
110 }
111 
112 ComponentExtensionIMEManager*
GetComponentExtensionIMEManager()113 MockInputMethodManagerImpl::GetComponentExtensionIMEManager() {
114   return comp_ime_manager_.get();
115 }
116 
117 scoped_refptr<InputMethodManager::State>
CreateNewState(Profile * profile)118 MockInputMethodManagerImpl::CreateNewState(Profile* profile) {
119   NOTIMPLEMENTED();
120   return state_;
121 }
122 
123 scoped_refptr<InputMethodManager::State>
GetActiveIMEState()124 MockInputMethodManagerImpl::GetActiveIMEState() {
125   return scoped_refptr<InputMethodManager::State>(state_.get());
126 }
127 
SetState(scoped_refptr<InputMethodManager::State> state)128 void MockInputMethodManagerImpl::SetState(
129     scoped_refptr<InputMethodManager::State> state) {
130   state_ = scoped_refptr<MockInputMethodManagerImpl::State>(
131       static_cast<MockInputMethodManagerImpl::State*>(state.get()));
132 }
133 
SetCurrentInputMethodId(const std::string & input_method_id)134 void MockInputMethodManagerImpl::SetCurrentInputMethodId(
135     const std::string& input_method_id) {
136   state_->current_input_method_id = input_method_id;
137 }
138 
SetComponentExtensionIMEManager(std::unique_ptr<ComponentExtensionIMEManager> comp_ime_manager)139 void MockInputMethodManagerImpl::SetComponentExtensionIMEManager(
140     std::unique_ptr<ComponentExtensionIMEManager> comp_ime_manager) {
141   comp_ime_manager_ = std::move(comp_ime_manager);
142 }
143 
set_application_locale(const std::string & value)144 void MockInputMethodManagerImpl::set_application_locale(
145     const std::string& value) {
146   delegate_.set_active_locale(value);
147 }
148 
149 }  // namespace input_method
150 }  // namespace chromeos
151