| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/input_method/input_method_engine.h" | 5 #include "chrome/browser/chromeos/input_method/input_method_engine.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/metrics/histogram_samples.h" | 12 #include "base/metrics/histogram_samples.h" |
| 13 #include "base/metrics/statistics_recorder.h" | 13 #include "base/metrics/statistics_recorder.h" |
| 14 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
| 15 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" | 15 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" |
| 16 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h" | 16 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h" |
| 17 #include "chrome/browser/input_method/input_method_engine_base.h" |
| 17 #include "chrome/browser/profiles/profile_manager.h" | 18 #include "chrome/browser/profiles/profile_manager.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "ui/base/ime/chromeos/extension_ime_util.h" | 20 #include "ui/base/ime/chromeos/extension_ime_util.h" |
| 20 #include "ui/base/ime/chromeos/mock_component_extension_ime_manager_delegate.h" | 21 #include "ui/base/ime/chromeos/mock_component_extension_ime_manager_delegate.h" |
| 21 #include "ui/base/ime/chromeos/mock_ime_input_context_handler.h" | 22 #include "ui/base/ime/chromeos/mock_ime_input_context_handler.h" |
| 22 #include "ui/base/ime/ime_bridge.h" | 23 #include "ui/base/ime/ime_bridge.h" |
| 23 #include "ui/base/ime/ime_engine_handler_interface.h" | 24 #include "ui/base/ime/ime_engine_handler_interface.h" |
| 24 #include "ui/base/ime/ime_engine_observer.h" | 25 #include "ui/base/ime/ime_engine_observer.h" |
| 25 #include "ui/base/ime/text_input_flags.h" | 26 #include "ui/base/ime/text_input_flags.h" |
| 26 #include "ui/gfx/geometry/rect.h" | 27 #include "ui/gfx/geometry/rect.h" |
| 27 | 28 |
| 29 using input_method::InputMethodEngineBase; |
| 30 |
| 28 namespace chromeos { | 31 namespace chromeos { |
| 29 | 32 |
| 30 namespace input_method { | 33 namespace input_method { |
| 31 namespace { | 34 namespace { |
| 32 | 35 |
| 33 const char kTestExtensionId[] = "mppnpdlheglhdfmldimlhpnegondlapf"; | 36 const char kTestExtensionId[] = "mppnpdlheglhdfmldimlhpnegondlapf"; |
| 34 const char kTestExtensionId2[] = "dmpipdbjkoajgdeppkffbjhngfckdloi"; | 37 const char kTestExtensionId2[] = "dmpipdbjkoajgdeppkffbjhngfckdloi"; |
| 35 const char kTestImeComponentId[] = "test_engine_id"; | 38 const char kTestImeComponentId[] = "test_engine_id"; |
| 36 | 39 |
| 37 enum CallsBitmap { | 40 enum CallsBitmap { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 63 delegate->set_ime_list(ime_list); | 66 delegate->set_ime_list(ime_list); |
| 64 comp_ime_manager->Initialize( | 67 comp_ime_manager->Initialize( |
| 65 scoped_ptr<ComponentExtensionIMEManagerDelegate>(delegate)); | 68 scoped_ptr<ComponentExtensionIMEManagerDelegate>(delegate)); |
| 66 | 69 |
| 67 MockInputMethodManager* manager = new MockInputMethodManager; | 70 MockInputMethodManager* manager = new MockInputMethodManager; |
| 68 manager->SetComponentExtensionIMEManager( | 71 manager->SetComponentExtensionIMEManager( |
| 69 scoped_ptr<ComponentExtensionIMEManager>(comp_ime_manager)); | 72 scoped_ptr<ComponentExtensionIMEManager>(comp_ime_manager)); |
| 70 InitializeForTesting(manager); | 73 InitializeForTesting(manager); |
| 71 } | 74 } |
| 72 | 75 |
| 73 class TestObserver : public ui::IMEEngineObserver { | 76 class TestObserver : public InputMethodEngineBase::Observer { |
| 74 public: | 77 public: |
| 75 TestObserver() : calls_bitmap_(NONE) {} | 78 TestObserver() : calls_bitmap_(NONE) {} |
| 76 ~TestObserver() override {} | 79 ~TestObserver() override {} |
| 77 | 80 |
| 78 void OnActivate(const std::string& engine_id) override { | 81 void OnActivate(const std::string& engine_id) override { |
| 79 calls_bitmap_ |= ACTIVATE; | 82 calls_bitmap_ |= ACTIVATE; |
| 80 } | 83 } |
| 81 void OnDeactivated(const std::string& engine_id) override { | 84 void OnDeactivated(const std::string& engine_id) override { |
| 82 calls_bitmap_ |= DEACTIVATED; | 85 calls_bitmap_ |= DEACTIVATED; |
| 83 } | 86 } |
| 84 void OnFocus( | 87 void OnFocus( |
| 85 const ui::IMEEngineHandlerInterface::InputContext& context) override { | 88 const ui::IMEEngineHandlerInterface::InputContext& context) override { |
| 86 calls_bitmap_ |= ONFOCUS; | 89 calls_bitmap_ |= ONFOCUS; |
| 87 } | 90 } |
| 88 void OnBlur(int context_id) override { calls_bitmap_ |= ONBLUR; } | 91 void OnBlur(int context_id) override { calls_bitmap_ |= ONBLUR; } |
| 89 bool IsInterestedInKeyEvent() const override { return true; } | 92 bool IsInterestedInKeyEvent() const override { return true; } |
| 90 void OnKeyEvent( | 93 void OnKeyEvent( |
| 91 const std::string& engine_id, | 94 const std::string& engine_id, |
| 92 const ui::IMEEngineHandlerInterface::KeyboardEvent& event, | 95 const InputMethodEngineBase::KeyboardEvent& event, |
| 93 ui::IMEEngineHandlerInterface::KeyEventDoneCallback& key_data) override {} | 96 ui::IMEEngineHandlerInterface::KeyEventDoneCallback& key_data) override {} |
| 94 void OnInputContextUpdate( | 97 void OnInputContextUpdate( |
| 95 const ui::IMEEngineHandlerInterface::InputContext& context) override {} | 98 const ui::IMEEngineHandlerInterface::InputContext& context) override {} |
| 96 void OnCandidateClicked(const std::string& engine_id, | 99 void OnCandidateClicked( |
| 97 int candidate_id, | 100 const std::string& engine_id, |
| 98 MouseButtonEvent button) override {} | 101 int candidate_id, |
| 102 InputMethodEngineBase::MouseButtonEvent button) override {} |
| 99 void OnMenuItemActivated(const std::string& engine_id, | 103 void OnMenuItemActivated(const std::string& engine_id, |
| 100 const std::string& menu_id) override {} | 104 const std::string& menu_id) override {} |
| 101 void OnSurroundingTextChanged(const std::string& engine_id, | 105 void OnSurroundingTextChanged(const std::string& engine_id, |
| 102 const std::string& text, | 106 const std::string& text, |
| 103 int cursor_pos, | 107 int cursor_pos, |
| 104 int anchor_pos, | 108 int anchor_pos, |
| 105 int offset) override {} | 109 int offset) override {} |
| 106 void OnCompositionBoundsChanged( | 110 void OnCompositionBoundsChanged( |
| 107 const std::vector<gfx::Rect>& bounds) override { | 111 const std::vector<gfx::Rect>& bounds) override { |
| 108 calls_bitmap_ |= ONCOMPOSITIONBOUNDSCHANGED; | 112 calls_bitmap_ |= ONCOMPOSITIONBOUNDSCHANGED; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 135 ~InputMethodEngineTest() override { | 139 ~InputMethodEngineTest() override { |
| 136 ui::IMEBridge::Get()->SetInputContextHandler(NULL); | 140 ui::IMEBridge::Get()->SetInputContextHandler(NULL); |
| 137 engine_.reset(); | 141 engine_.reset(); |
| 138 Shutdown(); | 142 Shutdown(); |
| 139 } | 143 } |
| 140 | 144 |
| 141 protected: | 145 protected: |
| 142 void CreateEngine(bool whitelisted) { | 146 void CreateEngine(bool whitelisted) { |
| 143 engine_.reset(new InputMethodEngine()); | 147 engine_.reset(new InputMethodEngine()); |
| 144 observer_ = new TestObserver(); | 148 observer_ = new TestObserver(); |
| 145 scoped_ptr<ui::IMEEngineObserver> observer_ptr(observer_); | 149 scoped_ptr<InputMethodEngineBase::Observer> observer_ptr(observer_); |
| 146 engine_->Initialize(std::move(observer_ptr), | 150 engine_->Initialize(std::move(observer_ptr), |
| 147 whitelisted ? kTestExtensionId : kTestExtensionId2, | 151 whitelisted ? kTestExtensionId : kTestExtensionId2, |
| 148 ProfileManager::GetActiveUserProfile()); | 152 ProfileManager::GetActiveUserProfile()); |
| 149 } | 153 } |
| 150 | 154 |
| 151 void FocusIn(ui::TextInputType input_type) { | 155 void FocusIn(ui::TextInputType input_type) { |
| 152 ui::IMEEngineHandlerInterface::InputContext input_context( | 156 ui::IMEEngineHandlerInterface::InputContext input_context( |
| 153 input_type, ui::TEXT_INPUT_MODE_DEFAULT, ui::TEXT_INPUT_FLAG_NONE); | 157 input_type, ui::TEXT_INPUT_MODE_DEFAULT, ui::TEXT_INPUT_FLAG_NONE); |
| 154 engine_->FocusIn(input_context); | 158 engine_->FocusIn(input_context); |
| 155 ui::IMEBridge::Get()->SetCurrentInputContext(input_context); | 159 ui::IMEBridge::Get()->SetCurrentInputContext(input_context); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 FocusIn(ui::TEXT_INPUT_TYPE_PASSWORD); | 242 FocusIn(ui::TEXT_INPUT_TYPE_PASSWORD); |
| 239 EXPECT_EQ(ONFOCUS, observer_->GetCallsBitmapAndReset()); | 243 EXPECT_EQ(ONFOCUS, observer_->GetCallsBitmapAndReset()); |
| 240 engine_->Disable(); | 244 engine_->Disable(); |
| 241 EXPECT_EQ(DEACTIVATED, observer_->GetCallsBitmapAndReset()); | 245 EXPECT_EQ(DEACTIVATED, observer_->GetCallsBitmapAndReset()); |
| 242 } | 246 } |
| 243 | 247 |
| 244 TEST_F(InputMethodEngineTest, TestHistograms) { | 248 TEST_F(InputMethodEngineTest, TestHistograms) { |
| 245 CreateEngine(true); | 249 CreateEngine(true); |
| 246 FocusIn(ui::TEXT_INPUT_TYPE_TEXT); | 250 FocusIn(ui::TEXT_INPUT_TYPE_TEXT); |
| 247 engine_->Enable(kTestImeComponentId); | 251 engine_->Enable(kTestImeComponentId); |
| 248 std::vector<ui::IMEEngineHandlerInterface::SegmentInfo> segments; | 252 std::vector<InputMethodEngineBase::SegmentInfo> segments; |
| 249 int context = engine_->GetCotextIdForTesting(); | 253 int context = engine_->GetCotextIdForTesting(); |
| 250 std::string error; | 254 std::string error; |
| 251 base::HistogramTester histograms; | 255 base::HistogramTester histograms; |
| 252 engine_->SetComposition(context, "test", 0, 0, 0, segments, NULL); | 256 engine_->SetComposition(context, "test", 0, 0, 0, segments, NULL); |
| 253 engine_->CommitText(context, "input", &error); | 257 engine_->CommitText(context, "input", &error); |
| 254 engine_->SetComposition(context, "test", 0, 0, 0, segments, NULL); | 258 engine_->SetComposition(context, "test", 0, 0, 0, segments, NULL); |
| 255 engine_->CommitText(context, | 259 engine_->CommitText(context, |
| 256 "\xE5\x85\xA5\xE5\x8A\x9B", // 2 UTF-8 characters | 260 "\xE5\x85\xA5\xE5\x8A\x9B", // 2 UTF-8 characters |
| 257 &error); | 261 &error); |
| 258 engine_->SetComposition(context, "test", 0, 0, 0, segments, NULL); | 262 engine_->SetComposition(context, "test", 0, 0, 0, segments, NULL); |
| 259 engine_->CommitText(context, "input\xE5\x85\xA5\xE5\x8A\x9B", &error); | 263 engine_->CommitText(context, "input\xE5\x85\xA5\xE5\x8A\x9B", &error); |
| 260 histograms.ExpectTotalCount("InputMethod.CommitLength", 3); | 264 histograms.ExpectTotalCount("InputMethod.CommitLength", 3); |
| 261 histograms.ExpectBucketCount("InputMethod.CommitLength", 5, 1); | 265 histograms.ExpectBucketCount("InputMethod.CommitLength", 5, 1); |
| 262 histograms.ExpectBucketCount("InputMethod.CommitLength", 2, 1); | 266 histograms.ExpectBucketCount("InputMethod.CommitLength", 2, 1); |
| 263 histograms.ExpectBucketCount("InputMethod.CommitLength", 7, 1); | 267 histograms.ExpectBucketCount("InputMethod.CommitLength", 7, 1); |
| 264 } | 268 } |
| 265 | 269 |
| 266 TEST_F(InputMethodEngineTest, TestCompositionBoundsChanged) { | 270 TEST_F(InputMethodEngineTest, TestCompositionBoundsChanged) { |
| 267 CreateEngine(true); | 271 CreateEngine(true); |
| 268 // Enable/disable with focus. | 272 // Enable/disable with focus. |
| 269 std::vector<gfx::Rect> rects; | 273 std::vector<gfx::Rect> rects; |
| 270 rects.push_back(gfx::Rect()); | 274 rects.push_back(gfx::Rect()); |
| 271 engine_->SetCompositionBounds(rects); | 275 engine_->SetCompositionBounds(rects); |
| 272 EXPECT_EQ(ONCOMPOSITIONBOUNDSCHANGED, observer_->GetCallsBitmapAndReset()); | 276 EXPECT_EQ(ONCOMPOSITIONBOUNDSCHANGED, observer_->GetCallsBitmapAndReset()); |
| 273 } | 277 } |
| 274 | 278 |
| 275 } // namespace input_method | 279 } // namespace input_method |
| 276 } // namespace chromeos | 280 } // namespace chromeos |
| OLD | NEW |