File: input_method_engine_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (261 lines) | stat: -rw-r--r-- 8,816 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2016 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 "base/message_loop/message_loop.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/input_method/input_method_engine.h"
#include "chrome/browser/ui/input_method/input_method_engine_base.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ime/ime_bridge.h"
#include "ui/base/ime/ime_engine_handler_interface.h"
#include "ui/base/ime/mock_ime_input_context_handler.h"
#include "ui/base/ime/text_input_flags.h"

namespace input_method {
namespace {
const char kTestExtensionId[] = "test_extension";

enum CallsBitmap {
  NONE = 0U,
  ONACTIVATE = 1U,
  ONDEACTIVATED = 2U,
  ONFOCUS = 4U,
  ONBLUR = 8U,
  ONKEYEVENT = 16U,
  ONRESET = 32U,
  ONCOMPOSITIONBOUNDSCHANGED = 64U,
  ONSURROUNDINGTEXTCHANGED = 128U
};

// The surrounding information.
struct SurroundingInfo {
  std::string text;
  int focus;
  int anchor;
  int offset;
};

class KeyEventDoneCallback {
 public:
  explicit KeyEventDoneCallback(bool expected_argument)
      : expected_argument_(expected_argument), is_called_(false) {}
  ~KeyEventDoneCallback() {}

  void Run(bool consumed) {
    if (consumed == expected_argument_) {
      base::MessageLoop::current()->QuitWhenIdle();
      is_called_ = true;
    }
  }

  void WaitUntilCalled() {
    while (!is_called_)
      content::RunMessageLoop();
  }

 private:
  bool expected_argument_;
  bool is_called_;

  DISALLOW_COPY_AND_ASSIGN(KeyEventDoneCallback);
};

class TestObserver : public InputMethodEngineBase::Observer {
 public:
  TestObserver() : calls_bitmap_(NONE) {}
  ~TestObserver() override {}

  // InputMethodEngineBase::Observer:
  void OnActivate(const std::string& engine_id) override {
    calls_bitmap_ |= ONACTIVATE;
  }
  void OnFocus(
      const ui::IMEEngineHandlerInterface::InputContext& context) override {
    calls_bitmap_ |= ONFOCUS;
  }
  void OnBlur(int context_id) override { calls_bitmap_ |= ONBLUR; }
  void OnKeyEvent(
      const std::string& engine_id,
      const InputMethodEngineBase::KeyboardEvent& event,
      ui::IMEEngineHandlerInterface::KeyEventDoneCallback& key_data) override {
    calls_bitmap_ |= ONKEYEVENT;
    key_event_ = event;
  }
  void OnReset(const std::string& engine_id) override {
    calls_bitmap_ |= ONRESET;
  }
  void OnDeactivated(const std::string& engine_id) override {
    calls_bitmap_ |= ONDEACTIVATED;
  }
  void OnCompositionBoundsChanged(
      const std::vector<gfx::Rect>& bounds) override {
    calls_bitmap_ |= ONCOMPOSITIONBOUNDSCHANGED;
    composition_bounds_ = bounds;
  }
  bool IsInterestedInKeyEvent() const override { return true; }
  void OnSurroundingTextChanged(const std::string& engine_id,
                                const std::string& text,
                                int cursor_pos,
                                int anchor_pos,
                                int offset) override {
    calls_bitmap_ |= ONSURROUNDINGTEXTCHANGED;
    surrounding_info_.text = text;
    surrounding_info_.focus = cursor_pos;
    surrounding_info_.anchor = anchor_pos;
    surrounding_info_.offset = offset;
  }
  void OnRequestEngineSwitch() override {}

  // Returns and resets the bitmap |calls_bitmap_|.
  unsigned char GetCallsBitmapAndReset() {
    unsigned char ret = calls_bitmap_;
    calls_bitmap_ = NONE;
    return ret;
  }

  const InputMethodEngineBase::KeyboardEvent& GetKeyEvent() {
    return key_event_;
  }

  const std::vector<gfx::Rect>& GetCompositionBounds() {
    return composition_bounds_;
  }

  const SurroundingInfo& GetSurroundingInfo() { return surrounding_info_; }

 private:
  unsigned char calls_bitmap_;
  InputMethodEngineBase::KeyboardEvent key_event_;
  std::vector<gfx::Rect> composition_bounds_;
  SurroundingInfo surrounding_info_;

  DISALLOW_COPY_AND_ASSIGN(TestObserver);
};

class InputMethodEngineTest : public testing::Test {
 public:
  InputMethodEngineTest() : observer_(nullptr) {
    ui::IMEBridge::Initialize();
    mock_ime_input_context_handler_.reset(new ui::MockIMEInputContextHandler());
    ui::IMEBridge::Get()->SetInputContextHandler(
        mock_ime_input_context_handler_.get());
    CreateEngine(kTestExtensionId);
  }
  ~InputMethodEngineTest() override {
    ui::IMEBridge::Get()->SetInputContextHandler(nullptr);
    engine_.reset();
  }

 protected:
  void CreateEngine(const char* extension_id) {
    engine_.reset(new InputMethodEngine());
    observer_ = new TestObserver();
    std::unique_ptr<InputMethodEngineBase::Observer> observer_ptr(observer_);
    engine_->Initialize(std::move(observer_ptr), extension_id, profile_.get());
  }

  void FocusIn(ui::TextInputType input_type) {
    ui::IMEEngineHandlerInterface::InputContext input_context(
        input_type, ui::TEXT_INPUT_MODE_DEFAULT, ui::TEXT_INPUT_FLAG_NONE);
    engine_->FocusIn(input_context);
    ui::IMEBridge::Get()->SetCurrentInputContext(input_context);
  }

  std::unique_ptr<InputMethodEngine> engine_;

  TestObserver* observer_;
  std::unique_ptr<ui::MockIMEInputContextHandler>
      mock_ime_input_context_handler_;
  // The associated testing profile.
  std::unique_ptr<TestingProfile> profile_;

 private:
  DISALLOW_COPY_AND_ASSIGN(InputMethodEngineTest);
};

}  // namespace

// Tests input.ime.onActivate/onDeactivate/onFocus/onBlur API.
TEST_F(InputMethodEngineTest, TestActivateAndFocus) {
  // Enables the extension with focus.
  engine_->Enable("");
  FocusIn(ui::TEXT_INPUT_TYPE_URL);
  EXPECT_EQ(ONACTIVATE | ONFOCUS, observer_->GetCallsBitmapAndReset());
  // Focus out.
  engine_->FocusOut();
  EXPECT_EQ(ONBLUR, observer_->GetCallsBitmapAndReset());
  // Disables the extension.
  engine_->Disable();
  EXPECT_EQ(ONDEACTIVATED, observer_->GetCallsBitmapAndReset());
}

// Tests input.ime.onKeyEvent API.
TEST_F(InputMethodEngineTest, TestKeyEvent) {
  // Enables the extension with focus.
  engine_->Enable("");
  FocusIn(ui::TEXT_INPUT_TYPE_URL);
  EXPECT_EQ(ONACTIVATE | ONFOCUS, observer_->GetCallsBitmapAndReset());
  // Sends key events.
  ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
  KeyEventDoneCallback callback(false);
  ui::IMEEngineHandlerInterface::KeyEventDoneCallback keyevent_callback =
      base::Bind(&KeyEventDoneCallback::Run, base::Unretained(&callback));
  engine_->ProcessKeyEvent(key_event, keyevent_callback);
  EXPECT_EQ(ONKEYEVENT, observer_->GetCallsBitmapAndReset());
  EXPECT_EQ("keydown", observer_->GetKeyEvent().type);
  EXPECT_EQ("a", observer_->GetKeyEvent().key);
  EXPECT_EQ("KeyA", observer_->GetKeyEvent().code);
  EXPECT_EQ('A', observer_->GetKeyEvent().key_code);
  EXPECT_FALSE(observer_->GetKeyEvent().alt_key);
  EXPECT_FALSE(observer_->GetKeyEvent().ctrl_key);
  EXPECT_FALSE(observer_->GetKeyEvent().shift_key);
  EXPECT_FALSE(observer_->GetKeyEvent().caps_lock);
}

// Tests input.ime.onReset API.
TEST_F(InputMethodEngineTest, TestReset) {
  // Enables the extension with focus.
  engine_->Enable("");
  FocusIn(ui::TEXT_INPUT_TYPE_URL);
  EXPECT_EQ(ONACTIVATE | ONFOCUS, observer_->GetCallsBitmapAndReset());

  // Resets the engine.
  engine_->Reset();
  EXPECT_EQ(ONRESET, observer_->GetCallsBitmapAndReset());
}

// Tests input.ime.onCompositionBoundsChanged API.
TEST_F(InputMethodEngineTest, TestCompositionBoundsChanged) {
  // Enables the extension with focus.
  engine_->Enable("");
  FocusIn(ui::TEXT_INPUT_TYPE_URL);
  EXPECT_EQ(ONACTIVATE | ONFOCUS, observer_->GetCallsBitmapAndReset());
  // Sets the composition to trigger the composition bounds changed event.
  std::vector<gfx::Rect> rects;
  rects.push_back(gfx::Rect());
  engine_->SetCompositionBounds(rects);
  EXPECT_EQ(ONCOMPOSITIONBOUNDSCHANGED, observer_->GetCallsBitmapAndReset());
  EXPECT_EQ(1U, observer_->GetCompositionBounds().size());
}

// Tests input.ime.onSurroundingTextChanged API.
TEST_F(InputMethodEngineTest, TestSurroundingTextChanged) {
  // Enables the extension with focus.
  engine_->Enable("");
  FocusIn(ui::TEXT_INPUT_TYPE_URL);
  EXPECT_EQ(ONACTIVATE | ONFOCUS, observer_->GetCallsBitmapAndReset());
  // Sets the surrounding text.
  engine_->SetSurroundingText("text" /* Surrounding text*/,
                              0 /*focused position*/, 1 /*anchor position*/,
                              0 /*offset position*/);
  EXPECT_EQ(ONSURROUNDINGTEXTCHANGED, observer_->GetCallsBitmapAndReset());
  EXPECT_EQ("text", observer_->GetSurroundingInfo().text);
  EXPECT_EQ(0, observer_->GetSurroundingInfo().focus);
  EXPECT_EQ(1, observer_->GetSurroundingInfo().anchor);
  EXPECT_EQ(0, observer_->GetSurroundingInfo().offset);
}

}  // namespace input_method