| 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/text_input_flags.h" | 25 #include "ui/base/ime/text_input_flags.h" |
| 26 #include "ui/gfx/geometry/rect.h" | 26 #include "ui/gfx/geometry/rect.h" |
| 27 | 27 |
| 28 using input_method::InputMethodEngineBase; |
| 29 |
| 28 namespace chromeos { | 30 namespace chromeos { |
| 29 | 31 |
| 30 namespace input_method { | 32 namespace input_method { |
| 31 namespace { | 33 namespace { |
| 32 | 34 |
| 33 const char kTestExtensionId[] = "mppnpdlheglhdfmldimlhpnegondlapf"; | 35 const char kTestExtensionId[] = "mppnpdlheglhdfmldimlhpnegondlapf"; |
| 34 const char kTestExtensionId2[] = "dmpipdbjkoajgdeppkffbjhngfckdloi"; | 36 const char kTestExtensionId2[] = "dmpipdbjkoajgdeppkffbjhngfckdloi"; |
| 35 const char kTestImeComponentId[] = "test_engine_id"; | 37 const char kTestImeComponentId[] = "test_engine_id"; |
| 36 | 38 |
| 37 enum CallsBitmap { | 39 enum CallsBitmap { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 63 delegate->set_ime_list(ime_list); | 65 delegate->set_ime_list(ime_list); |
| 64 comp_ime_manager->Initialize( | 66 comp_ime_manager->Initialize( |
| 65 scoped_ptr<ComponentExtensionIMEManagerDelegate>(delegate)); | 67 scoped_ptr<ComponentExtensionIMEManagerDelegate>(delegate)); |
| 66 | 68 |
| 67 MockInputMethodManager* manager = new MockInputMethodManager; | 69 MockInputMethodManager* manager = new MockInputMethodManager; |
| 68 manager->SetComponentExtensionIMEManager( | 70 manager->SetComponentExtensionIMEManager( |
| 69 scoped_ptr<ComponentExtensionIMEManager>(comp_ime_manager)); | 71 scoped_ptr<ComponentExtensionIMEManager>(comp_ime_manager)); |
| 70 InitializeForTesting(manager); | 72 InitializeForTesting(manager); |
| 71 } | 73 } |
| 72 | 74 |
| 73 class TestObserver : public ui::IMEEngineObserver { | 75 class TestObserver : public InputMethodEngineBase::Observer { |
| 74 public: | 76 public: |
| 75 TestObserver() : calls_bitmap_(NONE) {} | 77 TestObserver() : calls_bitmap_(NONE) {} |
| 76 ~TestObserver() override {} | 78 ~TestObserver() override {} |
| 77 | 79 |
| 78 void OnActivate(const std::string& engine_id) override { | 80 void OnActivate(const std::string& engine_id) override { |
| 79 calls_bitmap_ |= ACTIVATE; | 81 calls_bitmap_ |= ACTIVATE; |
| 80 } | 82 } |
| 81 void OnDeactivated(const std::string& engine_id) override { | 83 void OnDeactivated(const std::string& engine_id) override { |
| 82 calls_bitmap_ |= DEACTIVATED; | 84 calls_bitmap_ |= DEACTIVATED; |
| 83 } | 85 } |
| 84 void OnFocus( | 86 void OnFocus( |
| 85 const ui::IMEEngineHandlerInterface::InputContext& context) override { | 87 const ui::IMEEngineHandlerInterface::InputContext& context) override { |
| 86 calls_bitmap_ |= ONFOCUS; | 88 calls_bitmap_ |= ONFOCUS; |
| 87 } | 89 } |
| 88 void OnBlur(int context_id) override { calls_bitmap_ |= ONBLUR; } | 90 void OnBlur(int context_id) override { calls_bitmap_ |= ONBLUR; } |
| 89 bool IsInterestedInKeyEvent() const override { return true; } | 91 bool IsInterestedInKeyEvent() const override { return true; } |
| 90 void OnKeyEvent( | 92 void OnKeyEvent( |
| 91 const std::string& engine_id, | 93 const std::string& engine_id, |
| 92 const ui::IMEEngineHandlerInterface::KeyboardEvent& event, | 94 const InputMethodEngineBase::KeyboardEvent& event, |
| 93 ui::IMEEngineHandlerInterface::KeyEventDoneCallback& key_data) override {} | 95 ui::IMEEngineHandlerInterface::KeyEventDoneCallback& key_data) override {} |
| 94 void OnInputContextUpdate( | 96 void OnInputContextUpdate( |
| 95 const ui::IMEEngineHandlerInterface::InputContext& context) override {} | 97 const ui::IMEEngineHandlerInterface::InputContext& context) override {} |
| 96 void OnCandidateClicked(const std::string& engine_id, | 98 void OnCandidateClicked( |
| 97 int candidate_id, | 99 const std::string& engine_id, |
| 98 MouseButtonEvent button) override {} | 100 int candidate_id, |
| 101 InputMethodEngineBase::MouseButtonEvent button) override {} |
| 99 void OnMenuItemActivated(const std::string& engine_id, | 102 void OnMenuItemActivated(const std::string& engine_id, |
| 100 const std::string& menu_id) override {} | 103 const std::string& menu_id) override {} |
| 101 void OnSurroundingTextChanged(const std::string& engine_id, | 104 void OnSurroundingTextChanged(const std::string& engine_id, |
| 102 const std::string& text, | 105 const std::string& text, |
| 103 int cursor_pos, | 106 int cursor_pos, |
| 104 int anchor_pos, | 107 int anchor_pos, |
| 105 int offset) override {} | 108 int offset) override {} |
| 106 void OnCompositionBoundsChanged( | 109 void OnCompositionBoundsChanged( |
| 107 const std::vector<gfx::Rect>& bounds) override { | 110 const std::vector<gfx::Rect>& bounds) override { |
| 108 calls_bitmap_ |= ONCOMPOSITIONBOUNDSCHANGED; | 111 calls_bitmap_ |= ONCOMPOSITIONBOUNDSCHANGED; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 135 ~InputMethodEngineTest() override { | 138 ~InputMethodEngineTest() override { |
| 136 ui::IMEBridge::Get()->SetInputContextHandler(NULL); | 139 ui::IMEBridge::Get()->SetInputContextHandler(NULL); |
| 137 engine_.reset(); | 140 engine_.reset(); |
| 138 Shutdown(); | 141 Shutdown(); |
| 139 } | 142 } |
| 140 | 143 |
| 141 protected: | 144 protected: |
| 142 void CreateEngine(bool whitelisted) { | 145 void CreateEngine(bool whitelisted) { |
| 143 engine_.reset(new InputMethodEngine()); | 146 engine_.reset(new InputMethodEngine()); |
| 144 observer_ = new TestObserver(); | 147 observer_ = new TestObserver(); |
| 145 scoped_ptr<ui::IMEEngineObserver> observer_ptr(observer_); | 148 scoped_ptr<InputMethodEngineBase::Observer> observer_ptr(observer_); |
| 146 engine_->Initialize(std::move(observer_ptr), | 149 engine_->Initialize(std::move(observer_ptr), |
| 147 whitelisted ? kTestExtensionId : kTestExtensionId2, | 150 whitelisted ? kTestExtensionId : kTestExtensionId2, |
| 148 ProfileManager::GetActiveUserProfile()); | 151 ProfileManager::GetActiveUserProfile()); |
| 149 } | 152 } |
| 150 | 153 |
| 151 void FocusIn(ui::TextInputType input_type) { | 154 void FocusIn(ui::TextInputType input_type) { |
| 152 ui::IMEEngineHandlerInterface::InputContext input_context( | 155 ui::IMEEngineHandlerInterface::InputContext input_context( |
| 153 input_type, ui::TEXT_INPUT_MODE_DEFAULT, ui::TEXT_INPUT_FLAG_NONE); | 156 input_type, ui::TEXT_INPUT_MODE_DEFAULT, ui::TEXT_INPUT_FLAG_NONE); |
| 154 engine_->FocusIn(input_context); | 157 engine_->FocusIn(input_context); |
| 155 ui::IMEBridge::Get()->SetCurrentInputContext(input_context); | 158 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); | 241 FocusIn(ui::TEXT_INPUT_TYPE_PASSWORD); |
| 239 EXPECT_EQ(ONFOCUS, observer_->GetCallsBitmapAndReset()); | 242 EXPECT_EQ(ONFOCUS, observer_->GetCallsBitmapAndReset()); |
| 240 engine_->Disable(); | 243 engine_->Disable(); |
| 241 EXPECT_EQ(DEACTIVATED, observer_->GetCallsBitmapAndReset()); | 244 EXPECT_EQ(DEACTIVATED, observer_->GetCallsBitmapAndReset()); |
| 242 } | 245 } |
| 243 | 246 |
| 244 TEST_F(InputMethodEngineTest, TestHistograms) { | 247 TEST_F(InputMethodEngineTest, TestHistograms) { |
| 245 CreateEngine(true); | 248 CreateEngine(true); |
| 246 FocusIn(ui::TEXT_INPUT_TYPE_TEXT); | 249 FocusIn(ui::TEXT_INPUT_TYPE_TEXT); |
| 247 engine_->Enable(kTestImeComponentId); | 250 engine_->Enable(kTestImeComponentId); |
| 248 std::vector<ui::IMEEngineHandlerInterface::SegmentInfo> segments; | 251 std::vector<InputMethodEngineBase::SegmentInfo> segments; |
| 249 int context = engine_->GetCotextIdForTesting(); | 252 int context = engine_->GetCotextIdForTesting(); |
| 250 std::string error; | 253 std::string error; |
| 251 base::HistogramTester histograms; | 254 base::HistogramTester histograms; |
| 252 engine_->SetComposition(context, "test", 0, 0, 0, segments, NULL); | 255 engine_->SetComposition(context, "test", 0, 0, 0, segments, NULL); |
| 253 engine_->CommitText(context, "input", &error); | 256 engine_->CommitText(context, "input", &error); |
| 254 engine_->SetComposition(context, "test", 0, 0, 0, segments, NULL); | 257 engine_->SetComposition(context, "test", 0, 0, 0, segments, NULL); |
| 255 engine_->CommitText(context, | 258 engine_->CommitText(context, |
| 256 "\xE5\x85\xA5\xE5\x8A\x9B", // 2 UTF-8 characters | 259 "\xE5\x85\xA5\xE5\x8A\x9B", // 2 UTF-8 characters |
| 257 &error); | 260 &error); |
| 258 engine_->SetComposition(context, "test", 0, 0, 0, segments, NULL); | 261 engine_->SetComposition(context, "test", 0, 0, 0, segments, NULL); |
| 259 engine_->CommitText(context, "input\xE5\x85\xA5\xE5\x8A\x9B", &error); | 262 engine_->CommitText(context, "input\xE5\x85\xA5\xE5\x8A\x9B", &error); |
| 260 histograms.ExpectTotalCount("InputMethod.CommitLength", 3); | 263 histograms.ExpectTotalCount("InputMethod.CommitLength", 3); |
| 261 histograms.ExpectBucketCount("InputMethod.CommitLength", 5, 1); | 264 histograms.ExpectBucketCount("InputMethod.CommitLength", 5, 1); |
| 262 histograms.ExpectBucketCount("InputMethod.CommitLength", 2, 1); | 265 histograms.ExpectBucketCount("InputMethod.CommitLength", 2, 1); |
| 263 histograms.ExpectBucketCount("InputMethod.CommitLength", 7, 1); | 266 histograms.ExpectBucketCount("InputMethod.CommitLength", 7, 1); |
| 264 } | 267 } |
| 265 | 268 |
| 266 TEST_F(InputMethodEngineTest, TestCompositionBoundsChanged) { | 269 TEST_F(InputMethodEngineTest, TestCompositionBoundsChanged) { |
| 267 CreateEngine(true); | 270 CreateEngine(true); |
| 268 // Enable/disable with focus. | 271 // Enable/disable with focus. |
| 269 std::vector<gfx::Rect> rects; | 272 std::vector<gfx::Rect> rects; |
| 270 rects.push_back(gfx::Rect()); | 273 rects.push_back(gfx::Rect()); |
| 271 engine_->SetCompositionBounds(rects); | 274 engine_->SetCompositionBounds(rects); |
| 272 EXPECT_EQ(ONCOMPOSITIONBOUNDSCHANGED, observer_->GetCallsBitmapAndReset()); | 275 EXPECT_EQ(ONCOMPOSITIONBOUNDSCHANGED, observer_->GetCallsBitmapAndReset()); |
| 273 } | 276 } |
| 274 | 277 |
| 275 } // namespace input_method | 278 } // namespace input_method |
| 276 } // namespace chromeos | 279 } // namespace chromeos |
| OLD | NEW |