Flutter Windows Embedder
text_input_plugin_unittest.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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.
5 
6 #include <rapidjson/document.h>
7 #include <windows.h>
8 #include <memory>
9 
10 #include "flutter/fml/macros.h"
14 #include "flutter/shell/platform/windows/testing/engine_modifier.h"
15 #include "flutter/shell/platform/windows/testing/flutter_windows_engine_builder.h"
16 #include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h"
17 #include "flutter/shell/platform/windows/testing/test_binary_messenger.h"
18 #include "flutter/shell/platform/windows/testing/windows_test.h"
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 
22 namespace flutter {
23 
25  public:
26  explicit TextInputPluginModifier(TextInputPlugin* text_input_plugin)
27  : text_input_plugin(text_input_plugin) {}
28 
29  void SetViewId(FlutterViewId view_id) {
30  text_input_plugin->view_id_ = view_id;
31  }
32 
33  FlutterViewId GetViewId() { return text_input_plugin->view_id_; }
34 
35  bool HasActiveModel() { return text_input_plugin->active_model_ != nullptr; }
36 
37  const TextInputModel* GetActiveModel() const {
38  return text_input_plugin->active_model_.get();
39  }
40 
41  private:
42  TextInputPlugin* text_input_plugin;
43 
44  FML_DISALLOW_COPY_AND_ASSIGN(TextInputPluginModifier);
45 };
46 
47 namespace testing {
48 
49 namespace {
50 using ::testing::Return;
51 
52 static constexpr char kScanCodeKey[] = "scanCode";
53 static constexpr int kHandledScanCode = 20;
54 static constexpr int kUnhandledScanCode = 21;
55 static constexpr char kTextPlainFormat[] = "text/plain";
56 static constexpr int kDefaultClientId = 42;
57 // Should be identical to constants in text_input_plugin.cc.
58 static constexpr char kChannelName[] = "flutter/textinput";
59 static constexpr char kEnableDeltaModel[] = "enableDeltaModel";
60 static constexpr char kViewId[] = "viewId";
61 static constexpr char kSetClientMethod[] = "TextInput.setClient";
62 static constexpr char kSetEditingStateMethod[] = "TextInput.setEditingState";
63 static constexpr char kAffinityDownstream[] = "TextAffinity.downstream";
64 static constexpr char kTextKey[] = "text";
65 static constexpr char kSelectionBaseKey[] = "selectionBase";
66 static constexpr char kSelectionExtentKey[] = "selectionExtent";
67 static constexpr char kSelectionAffinityKey[] = "selectionAffinity";
68 static constexpr char kSelectionIsDirectionalKey[] = "selectionIsDirectional";
69 static constexpr char kComposingBaseKey[] = "composingBase";
70 static constexpr char kComposingExtentKey[] = "composingExtent";
71 static constexpr char kUpdateEditingStateMethod[] =
72  "TextInputClient.updateEditingState";
73 
74 static std::unique_ptr<std::vector<uint8_t>> CreateResponse(bool handled) {
75  auto response_doc =
76  std::make_unique<rapidjson::Document>(rapidjson::kObjectType);
77  auto& allocator = response_doc->GetAllocator();
78  response_doc->AddMember("handled", handled, allocator);
79  return JsonMessageCodec::GetInstance().EncodeMessage(*response_doc);
80 }
81 
82 static std::unique_ptr<rapidjson::Document> EncodedClientConfig(
83  std::string type_name,
84  std::string input_action) {
85  auto arguments = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
86  auto& allocator = arguments->GetAllocator();
87  arguments->PushBack(kDefaultClientId, allocator);
88 
89  rapidjson::Value config(rapidjson::kObjectType);
90  config.AddMember("inputAction", input_action, allocator);
91  config.AddMember(kEnableDeltaModel, false, allocator);
92  config.AddMember(kViewId, 456, allocator);
93  rapidjson::Value type_info(rapidjson::kObjectType);
94  type_info.AddMember("name", type_name, allocator);
95  config.AddMember("inputType", type_info, allocator);
96  arguments->PushBack(config, allocator);
97 
98  return arguments;
99 }
100 
101 static std::unique_ptr<rapidjson::Document> EncodedEditingState(
102  std::string text,
103  TextRange selection) {
104  auto model = std::make_unique<TextInputModel>();
105  model->SetText(text);
106  model->SetSelection(selection);
107 
108  auto arguments = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
109  auto& allocator = arguments->GetAllocator();
110  arguments->PushBack(kDefaultClientId, allocator);
111 
112  rapidjson::Value editing_state(rapidjson::kObjectType);
113  editing_state.AddMember(kSelectionAffinityKey, kAffinityDownstream,
114  allocator);
115  editing_state.AddMember(kSelectionBaseKey, selection.base(), allocator);
116  editing_state.AddMember(kSelectionExtentKey, selection.extent(), allocator);
117  editing_state.AddMember(kSelectionIsDirectionalKey, false, allocator);
118 
119  int composing_base =
120  model->composing() ? model->composing_range().base() : -1;
121  int composing_extent =
122  model->composing() ? model->composing_range().extent() : -1;
123  editing_state.AddMember(kComposingBaseKey, composing_base, allocator);
124  editing_state.AddMember(kComposingExtentKey, composing_extent, allocator);
125  editing_state.AddMember(kTextKey,
126  rapidjson::Value(model->GetText(), allocator).Move(),
127  allocator);
128  arguments->PushBack(editing_state, allocator);
129 
130  return arguments;
131 }
132 
133 class MockFlutterWindowsView : public FlutterWindowsView {
134  public:
135  MockFlutterWindowsView(FlutterWindowsEngine* engine,
136  std::unique_ptr<WindowBindingHandler> window)
138  engine,
139  std::move(window),
140  false,
141  BoxConstraints()) {}
142  virtual ~MockFlutterWindowsView() = default;
143 
144  MOCK_METHOD(void, OnCursorRectUpdated, (const Rect&), (override));
145  MOCK_METHOD(void, OnResetImeComposing, (), (override));
146 
147  private:
148  FML_DISALLOW_COPY_AND_ASSIGN(MockFlutterWindowsView);
149 };
150 
151 } // namespace
152 
153 class TextInputPluginTest : public WindowsTest {
154  public:
155  TextInputPluginTest() = default;
156  virtual ~TextInputPluginTest() = default;
157 
158  protected:
159  FlutterWindowsEngine* engine() { return engine_.get(); }
160  MockFlutterWindowsView* view() { return view_.get(); }
161  MockWindowBindingHandler* window() { return window_; }
162 
164  FlutterWindowsEngineBuilder builder{GetContext()};
165 
166  engine_ = builder.Build();
167  }
168 
170  FlutterWindowsEngineBuilder builder{GetContext()};
171 
172  auto window = std::make_unique<MockWindowBindingHandler>();
173 
174  window_ = window.get();
175  EXPECT_CALL(*window_, SetView).Times(1);
176  EXPECT_CALL(*window, GetWindowHandle).WillRepeatedly(Return(nullptr));
177 
178  engine_ = builder.Build();
179  view_ = std::make_unique<MockFlutterWindowsView>(engine_.get(),
180  std::move(window));
181 
182  EngineModifier modifier{engine_.get()};
183  modifier.SetViewById(view_.get(), 456);
184  }
185 
186  std::unique_ptr<MockFlutterWindowsView> AddViewWithId(int view_id) {
187  EXPECT_NE(engine_, nullptr);
188  auto window = std::make_unique<MockWindowBindingHandler>();
189  EXPECT_CALL(*window, SetView).Times(1);
190  EXPECT_CALL(*window, GetWindowHandle).WillRepeatedly(Return(nullptr));
191  auto view = std::make_unique<MockFlutterWindowsView>(engine_.get(),
192  std::move(window));
193 
194  EngineModifier modifier{engine_.get()};
195  modifier.SetViewById(view_.get(), view_id);
196  return view;
197  }
198 
199  private:
200  std::unique_ptr<FlutterWindowsEngine> engine_;
201  std::unique_ptr<MockFlutterWindowsView> view_;
202  MockWindowBindingHandler* window_;
203 
204  FML_DISALLOW_COPY_AND_ASSIGN(TextInputPluginTest);
205 };
206 
207 TEST_F(TextInputPluginTest, TextMethodsWorksWithEmptyModel) {
208  UseEngineWithView();
209 
210  auto handled_message = CreateResponse(true);
211  auto unhandled_message = CreateResponse(false);
212  int received_scancode = 0;
213 
214  TestBinaryMessenger messenger(
215  [&received_scancode, &handled_message, &unhandled_message](
216  const std::string& channel, const uint8_t* message,
217  size_t message_size, BinaryReply reply) {});
218 
219  int redispatch_scancode = 0;
220  TextInputPlugin handler(&messenger, engine());
221 
222  handler.KeyboardHook(VK_RETURN, 100, WM_KEYDOWN, '\n', false, false);
223  handler.ComposeBeginHook();
224  std::u16string text;
225  text.push_back('\n');
226  handler.ComposeChangeHook(text, 1);
227  handler.ComposeEndHook();
228 
229  // Passes if it did not crash
230 }
231 
232 TEST_F(TextInputPluginTest, ClearClientResetsComposing) {
233  UseEngineWithView();
234 
235  TestBinaryMessenger messenger([](const std::string& channel,
236  const uint8_t* message, size_t message_size,
237  BinaryReply reply) {});
238  BinaryReply reply_handler = [](const uint8_t* reply, size_t reply_size) {};
239 
240  TextInputPlugin handler(&messenger, engine());
241  TextInputPluginModifier modifier(&handler);
242  modifier.SetViewId(456);
243 
244  EXPECT_CALL(*view(), OnResetImeComposing());
245 
246  auto& codec = JsonMethodCodec::GetInstance();
247  auto message = codec.EncodeMethodCall({"TextInput.clearClient", nullptr});
248  messenger.SimulateEngineMessage(kChannelName, message->data(),
249  message->size(), reply_handler);
250 }
251 
252 // Verify that clear client fails if in headless mode.
253 TEST_F(TextInputPluginTest, ClearClientRequiresView) {
254  UseHeadlessEngine();
255 
256  TestBinaryMessenger messenger([](const std::string& channel,
257  const uint8_t* message, size_t message_size,
258  BinaryReply reply) {});
259 
260  std::string reply;
261  BinaryReply reply_handler = [&reply](const uint8_t* reply_bytes,
262  size_t reply_size) {
263  reply = std::string(reinterpret_cast<const char*>(reply_bytes), reply_size);
264  };
265 
266  TextInputPlugin handler(&messenger, engine());
267 
268  auto& codec = JsonMethodCodec::GetInstance();
269  auto message = codec.EncodeMethodCall({"TextInput.clearClient", nullptr});
270  messenger.SimulateEngineMessage(kChannelName, message->data(),
271  message->size(), reply_handler);
272 
273  EXPECT_EQ(
274  reply,
275  "[\"Internal Consistency Error\",\"Text input is not available because "
276  "view with view_id=0 cannot be found\",null]");
277 }
278 
279 // Verify that the embedder sends state update messages to the framework during
280 // IME composing.
281 TEST_F(TextInputPluginTest, VerifyComposingSendStateUpdate) {
282  UseEngineWithView();
283 
284  bool sent_message = false;
285  TestBinaryMessenger messenger(
286  [&sent_message](const std::string& channel, const uint8_t* message,
287  size_t message_size,
288  BinaryReply reply) { sent_message = true; });
289  BinaryReply reply_handler = [](const uint8_t* reply, size_t reply_size) {};
290 
291  TextInputPlugin handler(&messenger, engine());
292 
293  auto& codec = JsonMethodCodec::GetInstance();
294 
295  // Call TextInput.setClient to initialize the TextInputModel.
296  auto arguments = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
297  auto& allocator = arguments->GetAllocator();
298  arguments->PushBack(kDefaultClientId, allocator);
299  rapidjson::Value config(rapidjson::kObjectType);
300  config.AddMember("inputAction", "done", allocator);
301  config.AddMember("inputType", "text", allocator);
302  config.AddMember(kEnableDeltaModel, false, allocator);
303  config.AddMember(kViewId, 456, allocator);
304  arguments->PushBack(config, allocator);
305  auto message =
306  codec.EncodeMethodCall({"TextInput.setClient", std::move(arguments)});
307  messenger.SimulateEngineMessage("flutter/textinput", message->data(),
308  message->size(), reply_handler);
309 
310  // ComposeBeginHook should send state update.
311  sent_message = false;
312  handler.ComposeBeginHook();
313  EXPECT_TRUE(sent_message);
314 
315  // ComposeChangeHook should send state update.
316  sent_message = false;
317  handler.ComposeChangeHook(u"4", 1);
318  EXPECT_TRUE(sent_message);
319 
320  // ComposeCommitHook should NOT send state update.
321  //
322  // Commit messages are always immediately followed by a change message or an
323  // end message, both of which will send an update. Sending intermediate state
324  // with a collapsed composing region will trigger the framework to assume
325  // composing has ended, which is not the case until a WM_IME_ENDCOMPOSING
326  // event is received in the main event loop, which will trigger a call to
327  // ComposeEndHook.
328  sent_message = false;
329  handler.ComposeCommitHook();
330  EXPECT_FALSE(sent_message);
331 
332  // ComposeEndHook should send state update.
333  sent_message = false;
334  handler.ComposeEndHook();
335  EXPECT_TRUE(sent_message);
336 }
337 
338 TEST_F(TextInputPluginTest, VerifyInputActionNewlineInsertNewLine) {
339  UseEngineWithView();
340 
341  // Store messages as std::string for convenience.
342  std::vector<std::string> messages;
343 
344  TestBinaryMessenger messenger(
345  [&messages](const std::string& channel, const uint8_t* message,
346  size_t message_size, BinaryReply reply) {
347  std::string last_message(reinterpret_cast<const char*>(message),
348  message_size);
349  messages.push_back(last_message);
350  });
351  BinaryReply reply_handler = [](const uint8_t* reply, size_t reply_size) {};
352 
353  TextInputPlugin handler(&messenger, engine());
354 
355  auto& codec = JsonMethodCodec::GetInstance();
356 
357  // Call TextInput.setClient to initialize the TextInputModel.
358  auto set_client_arguments =
359  EncodedClientConfig("TextInputType.multiline", "TextInputAction.newline");
360  auto message = codec.EncodeMethodCall(
361  {"TextInput.setClient", std::move(set_client_arguments)});
362  messenger.SimulateEngineMessage("flutter/textinput", message->data(),
363  message->size(), reply_handler);
364 
365  // Simulate a key down event for '\n'.
366  handler.KeyboardHook(VK_RETURN, 100, WM_KEYDOWN, '\n', false, false);
367 
368  // Two messages are expected, the first is TextInput.updateEditingState and
369  // the second is TextInputClient.performAction.
370  EXPECT_EQ(messages.size(), 2);
371 
372  // Editing state should have been updated.
373  auto encoded_arguments = EncodedEditingState("\n", TextRange(1));
374  auto update_state_message = codec.EncodeMethodCall(
375  {kUpdateEditingStateMethod, std::move(encoded_arguments)});
376 
377  EXPECT_TRUE(std::equal(update_state_message->begin(),
378  update_state_message->end(),
379  messages.front().begin()));
380 
381  // TextInputClient.performAction should have been called.
382  auto arguments = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
383  auto& allocator = arguments->GetAllocator();
384  arguments->PushBack(kDefaultClientId, allocator);
385  arguments->PushBack(
386  rapidjson::Value("TextInputAction.newline", allocator).Move(), allocator);
387  auto invoke_action_message = codec.EncodeMethodCall(
388  {"TextInputClient.performAction", std::move(arguments)});
389 
390  EXPECT_TRUE(std::equal(invoke_action_message->begin(),
391  invoke_action_message->end(),
392  messages.back().begin()));
393 }
394 
395 // Regression test for https://github.com/flutter/flutter/issues/125879.
396 TEST_F(TextInputPluginTest, VerifyInputActionSendDoesNotInsertNewLine) {
397  UseEngineWithView();
398 
399  std::vector<std::vector<uint8_t>> messages;
400 
401  TestBinaryMessenger messenger(
402  [&messages](const std::string& channel, const uint8_t* message,
403  size_t message_size, BinaryReply reply) {
404  int length = static_cast<int>(message_size);
405  std::vector<uint8_t> last_message(length);
406  memcpy(&last_message[0], &message[0], length * sizeof(uint8_t));
407  messages.push_back(last_message);
408  });
409  BinaryReply reply_handler = [](const uint8_t* reply, size_t reply_size) {};
410 
411  TextInputPlugin handler(&messenger, engine());
412 
413  auto& codec = JsonMethodCodec::GetInstance();
414 
415  // Call TextInput.setClient to initialize the TextInputModel.
416  auto set_client_arguments =
417  EncodedClientConfig("TextInputType.multiline", "TextInputAction.send");
418  auto message = codec.EncodeMethodCall(
419  {"TextInput.setClient", std::move(set_client_arguments)});
420  messenger.SimulateEngineMessage("flutter/textinput", message->data(),
421  message->size(), reply_handler);
422 
423  // Simulate a key down event for '\n'.
424  handler.KeyboardHook(VK_RETURN, 100, WM_KEYDOWN, '\n', false, false);
425 
426  // Only a call to TextInputClient.performAction is expected.
427  EXPECT_EQ(messages.size(), 1);
428 
429  // TextInputClient.performAction should have been called.
430  auto arguments = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
431  auto& allocator = arguments->GetAllocator();
432  arguments->PushBack(kDefaultClientId, allocator);
433  arguments->PushBack(
434  rapidjson::Value("TextInputAction.send", allocator).Move(), allocator);
435  auto invoke_action_message = codec.EncodeMethodCall(
436  {"TextInputClient.performAction", std::move(arguments)});
437 
438  EXPECT_TRUE(std::equal(invoke_action_message->begin(),
439  invoke_action_message->end(),
440  messages.front().begin()));
441 }
442 
443 TEST_F(TextInputPluginTest, SetClientRequiresViewId) {
444  UseEngineWithView();
445 
446  TestBinaryMessenger messenger([](const std::string& channel,
447  const uint8_t* message, size_t message_size,
448  BinaryReply reply) {});
449 
450  TextInputPlugin handler(&messenger, engine());
451 
452  auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
453  auto& allocator = args->GetAllocator();
454  args->PushBack(123, allocator); // client_id
455 
456  rapidjson::Value client_config(rapidjson::kObjectType);
457 
458  args->PushBack(client_config, allocator);
461 
462  std::string reply;
463  BinaryReply reply_handler = [&reply](const uint8_t* reply_bytes,
464  size_t reply_size) {
465  reply = std::string(reinterpret_cast<const char*>(reply_bytes), reply_size);
466  };
467 
468  EXPECT_TRUE(messenger.SimulateEngineMessage(kChannelName, encoded->data(),
469  encoded->size(), reply_handler));
470  EXPECT_EQ(
471  reply,
472  "[\"Bad Arguments\",\"Could not set client, view ID is null.\",null]");
473 }
474 
475 TEST_F(TextInputPluginTest, SetClientRequiresViewIdToBeInteger) {
476  UseEngineWithView();
477 
478  TestBinaryMessenger messenger([](const std::string& channel,
479  const uint8_t* message, size_t message_size,
480  BinaryReply reply) {});
481 
482  TextInputPlugin handler(&messenger, engine());
483 
484  auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
485  auto& allocator = args->GetAllocator();
486  args->PushBack(123, allocator); // client_id
487 
488  rapidjson::Value client_config(rapidjson::kObjectType);
489  client_config.AddMember(kViewId, "Not an integer", allocator); // view_id
490 
491  args->PushBack(client_config, allocator);
494 
495  std::string reply;
496  BinaryReply reply_handler = [&reply](const uint8_t* reply_bytes,
497  size_t reply_size) {
498  reply = std::string(reinterpret_cast<const char*>(reply_bytes), reply_size);
499  };
500 
501  EXPECT_TRUE(messenger.SimulateEngineMessage(kChannelName, encoded->data(),
502  encoded->size(), reply_handler));
503  EXPECT_EQ(
504  reply,
505  "[\"Bad Arguments\",\"Could not set client, view ID is null.\",null]");
506 }
507 
508 TEST_F(TextInputPluginTest, SetEditingStatePreservesComposingRange) {
509  UseEngineWithView();
510 
511  TestBinaryMessenger messenger(
512  [](const std::string&, const uint8_t*, size_t, BinaryReply) {});
513  BinaryReply reply_handler = [](const uint8_t*, size_t) {};
514 
515  TextInputPlugin handler(&messenger, engine());
516  TextInputPluginModifier modifier(&handler);
517  auto& codec = JsonMethodCodec::GetInstance();
518 
519  auto set_client_message = codec.EncodeMethodCall(
521  EncodedClientConfig("TextInputType.text", "TextInputAction.done")});
522  EXPECT_TRUE(messenger.SimulateEngineMessage(
523  kChannelName, set_client_message->data(), set_client_message->size(),
524  reply_handler));
525  ASSERT_TRUE(modifier.HasActiveModel());
526  handler.ComposeBeginHook();
527 
528  auto editing_state =
529  std::make_unique<rapidjson::Document>(rapidjson::kObjectType);
530  auto& allocator = editing_state->GetAllocator();
531  editing_state->AddMember(kTextKey, "abcd", allocator);
532  editing_state->AddMember(kSelectionBaseKey, 2, allocator);
533  editing_state->AddMember(kSelectionExtentKey, 2, allocator);
534  editing_state->AddMember(kComposingBaseKey, 1, allocator);
535  editing_state->AddMember(kComposingExtentKey, 3, allocator);
536  auto set_editing_state_message = codec.EncodeMethodCall(
537  {kSetEditingStateMethod, std::move(editing_state)});
538  EXPECT_TRUE(messenger.SimulateEngineMessage(
539  kChannelName, set_editing_state_message->data(),
540  set_editing_state_message->size(), reply_handler));
541 
542  EXPECT_TRUE(modifier.GetActiveModel()->composing());
543  EXPECT_EQ(modifier.GetActiveModel()->composing_range(), TextRange(1, 3));
544 }
545 
546 TEST_F(TextInputPluginTest, TextEditingWorksWithDeltaModel) {
547  UseEngineWithView();
548 
549  auto handled_message = CreateResponse(true);
550  auto unhandled_message = CreateResponse(false);
551  int received_scancode = 0;
552 
553  TestBinaryMessenger messenger(
554  [&received_scancode, &handled_message, &unhandled_message](
555  const std::string& channel, const uint8_t* message,
556  size_t message_size, BinaryReply reply) {});
557 
558  int redispatch_scancode = 0;
559  TextInputPlugin handler(&messenger, engine());
560 
561  auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
562  auto& allocator = args->GetAllocator();
563  args->PushBack(123, allocator); // client_id
564 
565  rapidjson::Value client_config(rapidjson::kObjectType);
566  client_config.AddMember(kEnableDeltaModel, true, allocator);
567  client_config.AddMember(kViewId, 456, allocator);
568 
569  args->PushBack(client_config, allocator);
572 
573  EXPECT_TRUE(messenger.SimulateEngineMessage(
574  kChannelName, encoded->data(), encoded->size(),
575  [](const uint8_t* reply, size_t reply_size) {}));
576 
577  handler.KeyboardHook(VK_RETURN, 100, WM_KEYDOWN, '\n', false, false);
578  handler.ComposeBeginHook();
579  std::u16string text;
580  text.push_back('\n');
581  handler.ComposeChangeHook(text, 1);
582  handler.ComposeEndHook();
583 
584  handler.KeyboardHook(0x4E, 100, WM_KEYDOWN, 'n', false, false);
585  handler.ComposeBeginHook();
586  std::u16string textN;
587  text.push_back('n');
588  handler.ComposeChangeHook(textN, 1);
589  handler.KeyboardHook(0x49, 100, WM_KEYDOWN, 'i', false, false);
590  std::u16string textNi;
591  text.push_back('n');
592  text.push_back('i');
593  handler.ComposeChangeHook(textNi, 2);
594  handler.KeyboardHook(VK_RETURN, 100, WM_KEYDOWN, '\n', false, false);
595  std::u16string textChineseCharacter;
596  text.push_back(u'\u4F60');
597  handler.ComposeChangeHook(textChineseCharacter, 1);
598  handler.ComposeCommitHook();
599  handler.ComposeEndHook();
600 
601  // Passes if it did not crash
602 }
603 
604 // Regression test for https://github.com/flutter/flutter/issues/123749
605 TEST_F(TextInputPluginTest, CompositionCursorPos) {
606  UseEngineWithView();
607 
608  int selection_base = -1;
609  TestBinaryMessenger messenger([&](const std::string& channel,
610  const uint8_t* message, size_t size,
611  BinaryReply reply) {
613  std::vector<uint8_t>(message, message + size));
614  if (method->method_name() == kUpdateEditingStateMethod) {
615  const auto& args = *method->arguments();
616  const auto& editing_state = args[1];
617  auto base = editing_state.FindMember(kSelectionBaseKey);
618  auto extent = editing_state.FindMember(kSelectionExtentKey);
619  ASSERT_NE(base, editing_state.MemberEnd());
620  ASSERT_TRUE(base->value.IsInt());
621  ASSERT_NE(extent, editing_state.MemberEnd());
622  ASSERT_TRUE(extent->value.IsInt());
623  selection_base = base->value.GetInt();
624  EXPECT_EQ(extent->value.GetInt(), selection_base);
625  }
626  });
627 
628  TextInputPlugin plugin(&messenger, engine());
629 
630  auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
631  auto& allocator = args->GetAllocator();
632  args->PushBack(123, allocator); // client_id
633  rapidjson::Value client_config(rapidjson::kObjectType);
634  client_config.AddMember(kViewId, 456, allocator);
635  args->PushBack(client_config, allocator);
638  EXPECT_TRUE(messenger.SimulateEngineMessage(
639  kChannelName, encoded->data(), encoded->size(),
640  [](const uint8_t* reply, size_t reply_size) {}));
641 
642  plugin.ComposeBeginHook();
643  EXPECT_EQ(selection_base, 0);
644  plugin.ComposeChangeHook(u"abc", 3);
645  EXPECT_EQ(selection_base, 3);
646 
647  plugin.ComposeCommitHook();
648  plugin.ComposeEndHook();
649  EXPECT_EQ(selection_base, 3);
650 
651  plugin.ComposeBeginHook();
652  plugin.ComposeChangeHook(u"1", 1);
653  EXPECT_EQ(selection_base, 4);
654 
655  plugin.ComposeChangeHook(u"12", 2);
656  EXPECT_EQ(selection_base, 5);
657 
658  plugin.ComposeChangeHook(u"12", 1);
659  EXPECT_EQ(selection_base, 4);
660 
661  plugin.ComposeChangeHook(u"12", 2);
662  EXPECT_EQ(selection_base, 5);
663 }
664 
665 TEST_F(TextInputPluginTest, TransformCursorRect) {
666  UseEngineWithView();
667 
668  // A position of `EditableText`.
669  double view_x = 100;
670  double view_y = 200;
671 
672  // A position and size of marked text, in `EditableText` local coordinates.
673  double ime_x = 3;
674  double ime_y = 4;
675  double ime_width = 50;
676  double ime_height = 60;
677 
678  // Transformation matrix.
679  std::array<std::array<double, 4>, 4> editabletext_transform = {
680  1.0, 0.0, 0.0, view_x, //
681  0.0, 1.0, 0.0, view_y, //
682  0.0, 0.0, 0.0, 0.0, //
683  0.0, 0.0, 0.0, 1.0};
684 
685  TestBinaryMessenger messenger([](const std::string& channel,
686  const uint8_t* message, size_t message_size,
687  BinaryReply reply) {});
688  BinaryReply reply_handler = [](const uint8_t* reply, size_t reply_size) {};
689 
690  TextInputPlugin handler(&messenger, engine());
691  TextInputPluginModifier modifier(&handler);
692  modifier.SetViewId(456);
693 
694  auto& codec = JsonMethodCodec::GetInstance();
695 
696  EXPECT_CALL(*view(), OnCursorRectUpdated(Rect{{view_x, view_y}, {0, 0}}));
697 
698  {
699  auto arguments =
700  std::make_unique<rapidjson::Document>(rapidjson::kObjectType);
701  auto& allocator = arguments->GetAllocator();
702 
703  rapidjson::Value transoform(rapidjson::kArrayType);
704  for (int i = 0; i < 4 * 4; i++) {
705  // Pack 2-dimensional array by column-major order.
706  transoform.PushBack(editabletext_transform[i % 4][i / 4], allocator);
707  }
708 
709  arguments->AddMember("transform", transoform, allocator);
710 
711  auto message = codec.EncodeMethodCall(
712  {"TextInput.setEditableSizeAndTransform", std::move(arguments)});
713  messenger.SimulateEngineMessage(kChannelName, message->data(),
714  message->size(), reply_handler);
715  }
716 
717  EXPECT_CALL(*view(),
718  OnCursorRectUpdated(Rect{{view_x + ime_x, view_y + ime_y},
719  {ime_width, ime_height}}));
720 
721  {
722  auto arguments =
723  std::make_unique<rapidjson::Document>(rapidjson::kObjectType);
724  auto& allocator = arguments->GetAllocator();
725 
726  arguments->AddMember("x", ime_x, allocator);
727  arguments->AddMember("y", ime_y, allocator);
728  arguments->AddMember("width", ime_width, allocator);
729  arguments->AddMember("height", ime_height, allocator);
730 
731  auto message = codec.EncodeMethodCall(
732  {"TextInput.setMarkedTextRect", std::move(arguments)});
733  messenger.SimulateEngineMessage(kChannelName, message->data(),
734  message->size(), reply_handler);
735  }
736 }
737 
738 TEST_F(TextInputPluginTest, SetMarkedTextRectRequiresView) {
739  UseHeadlessEngine();
740 
741  TestBinaryMessenger messenger([](const std::string& channel,
742  const uint8_t* message, size_t message_size,
743  BinaryReply reply) {});
744 
745  std::string reply;
746  BinaryReply reply_handler = [&reply](const uint8_t* reply_bytes,
747  size_t reply_size) {
748  reply = std::string(reinterpret_cast<const char*>(reply_bytes), reply_size);
749  };
750 
751  TextInputPlugin handler(&messenger, engine());
752 
753  auto& codec = JsonMethodCodec::GetInstance();
754 
755  auto arguments =
756  std::make_unique<rapidjson::Document>(rapidjson::kObjectType);
757  auto& allocator = arguments->GetAllocator();
758 
759  arguments->AddMember("x", 0, allocator);
760  arguments->AddMember("y", 0, allocator);
761  arguments->AddMember("width", 0, allocator);
762  arguments->AddMember("height", 0, allocator);
763 
764  auto message = codec.EncodeMethodCall(
765  {"TextInput.setMarkedTextRect", std::move(arguments)});
766  messenger.SimulateEngineMessage(kChannelName, message->data(),
767  message->size(), reply_handler);
768 
769  EXPECT_EQ(
770  reply,
771  "[\"Internal Consistency Error\",\"Text input is not available because "
772  "view with view_id=0 cannot be found\",null]");
773 }
774 
775 TEST_F(TextInputPluginTest, SetAndUseMultipleClients) {
776  UseEngineWithView(); // Creates the default view
777  AddViewWithId(789); // Creates the next view
778 
779  bool sent_message = false;
780  TestBinaryMessenger messenger(
781  [&sent_message](const std::string& channel, const uint8_t* message,
782  size_t message_size,
783  BinaryReply reply) { sent_message = true; });
784 
785  TextInputPlugin handler(&messenger, engine());
786 
787  auto const set_client_and_send_message = [&](int client_id, int view_id) {
788  auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
789  auto& allocator = args->GetAllocator();
790  args->PushBack(client_id, allocator); // client_id
791 
792  rapidjson::Value client_config(rapidjson::kObjectType);
793  client_config.AddMember(kViewId, view_id, allocator); // view_id
794 
795  args->PushBack(client_config, allocator);
798 
799  std::string reply;
800  BinaryReply reply_handler = [&reply](const uint8_t* reply_bytes,
801  size_t reply_size) {
802  reply =
803  std::string(reinterpret_cast<const char*>(reply_bytes), reply_size);
804  };
805 
806  EXPECT_TRUE(messenger.SimulateEngineMessage(
807  kChannelName, encoded->data(), encoded->size(), reply_handler));
808 
809  sent_message = false;
810  handler.ComposeBeginHook();
811  EXPECT_TRUE(sent_message);
812  sent_message = false;
813  handler.ComposeChangeHook(u"4", 1);
814  EXPECT_TRUE(sent_message);
815  sent_message = false;
816  handler.ComposeCommitHook();
817  EXPECT_FALSE(sent_message);
818  sent_message = false;
819  handler.ComposeEndHook();
820  EXPECT_TRUE(sent_message);
821  };
822 
823  set_client_and_send_message(123, 456); // Set and send for the first view
824  set_client_and_send_message(123, 789); // Set and send for the next view
825 }
826 
827 // Verify that OnViewRemoved resets active model and view id when the
828 // active view is removed.
829 TEST_F(TextInputPluginTest, OnViewRemovedResetsActiveView) {
830  UseEngineWithView();
831 
832  TestBinaryMessenger messenger([](const std::string& channel,
833  const uint8_t* message, size_t message_size,
834  BinaryReply reply) {});
835  BinaryReply reply_handler = [](const uint8_t* reply, size_t reply_size) {};
836 
837  TextInputPlugin handler(&messenger, engine());
838  TextInputPluginModifier modifier(&handler);
839  modifier.SetViewId(456);
840 
841  // Set a client to create an active model.
842  auto& codec = JsonMethodCodec::GetInstance();
843  auto set_client_message = codec.EncodeMethodCall(
844  {"TextInput.setClient",
845  EncodedClientConfig("TextInputType.text", "TextInputAction.done")});
846  messenger.SimulateEngineMessage(kChannelName, set_client_message->data(),
847  set_client_message->size(), reply_handler);
848 
849  EXPECT_TRUE(modifier.HasActiveModel());
850  EXPECT_EQ(modifier.GetViewId(), 456);
851 
852  // Remove the active view.
853  handler.OnViewRemoved(456);
854 
855  EXPECT_FALSE(modifier.HasActiveModel());
856  EXPECT_EQ(modifier.GetViewId(), 0);
857 }
858 
859 // Verify that OnViewRemoved does not reset state when a different view
860 // is removed.
861 TEST_F(TextInputPluginTest, OnViewRemovedIgnoresNonActiveView) {
862  UseEngineWithView();
863 
864  TestBinaryMessenger messenger([](const std::string& channel,
865  const uint8_t* message, size_t message_size,
866  BinaryReply reply) {});
867  BinaryReply reply_handler = [](const uint8_t* reply, size_t reply_size) {};
868 
869  TextInputPlugin handler(&messenger, engine());
870  TextInputPluginModifier modifier(&handler);
871  modifier.SetViewId(456);
872 
873  // Set a client to create an active model.
874  auto& codec = JsonMethodCodec::GetInstance();
875  auto set_client_message = codec.EncodeMethodCall(
876  {"TextInput.setClient",
877  EncodedClientConfig("TextInputType.text", "TextInputAction.done")});
878  messenger.SimulateEngineMessage(kChannelName, set_client_message->data(),
879  set_client_message->size(), reply_handler);
880 
881  EXPECT_TRUE(modifier.HasActiveModel());
882  EXPECT_EQ(modifier.GetViewId(), 456);
883 
884  // Remove a different view.
885  handler.OnViewRemoved(789);
886 
887  // State should be unchanged.
888  EXPECT_TRUE(modifier.HasActiveModel());
889  EXPECT_EQ(modifier.GetViewId(), 456);
890 }
891 
892 // Verify that OnViewRemoved does not reset state for the implicit view.
893 TEST_F(TextInputPluginTest, OnViewRemovedIgnoresImplicitView) {
894  UseEngineWithView();
895 
896  TestBinaryMessenger messenger([](const std::string& channel,
897  const uint8_t* message, size_t message_size,
898  BinaryReply reply) {});
899  BinaryReply reply_handler = [](const uint8_t* reply, size_t reply_size) {};
900 
901  TextInputPlugin handler(&messenger, engine());
902  TextInputPluginModifier modifier(&handler);
903 
904  // Set a client to create an active model. EncodedClientConfig sets view_id
905  // to 456, so we override it to the implicit view id afterwards.
906  auto& codec = JsonMethodCodec::GetInstance();
907  auto set_client_message = codec.EncodeMethodCall(
908  {"TextInput.setClient",
909  EncodedClientConfig("TextInputType.text", "TextInputAction.done")});
910  messenger.SimulateEngineMessage(kChannelName, set_client_message->data(),
911  set_client_message->size(), reply_handler);
912 
913  EXPECT_TRUE(modifier.HasActiveModel());
914  modifier.SetViewId(kImplicitViewId);
915 
916  // Attempt to remove the implicit view.
918 
919  // State should be unchanged for the implicit view.
920  EXPECT_TRUE(modifier.HasActiveModel());
921  EXPECT_EQ(modifier.GetViewId(), kImplicitViewId);
922 }
923 
924 } // namespace testing
925 } // namespace flutter
FlutterWindowsView(FlutterViewId view_id, FlutterWindowsEngine *engine, std::unique_ptr< WindowBindingHandler > window_binding, bool is_sized_to_content, const BoxConstraints &box_constraints, FlutterWindowsViewSizingDelegate *sizing_delegate=nullptr, std::shared_ptr< WindowsProcTable > windows_proc_table=nullptr)
static const JsonMessageCodec & GetInstance()
static const JsonMethodCodec & GetInstance()
std::unique_ptr< std::vector< uint8_t > > EncodeMessage(const T &message) const
Definition: message_codec.h:45
std::unique_ptr< MethodCall< T > > DecodeMethodCall(const uint8_t *message, size_t message_size) const
Definition: method_codec.h:32
std::unique_ptr< std::vector< uint8_t > > EncodeMethodCall(const MethodCall< T > &method_call) const
Definition: method_codec.h:48
TextRange composing_range() const
void OnViewRemoved(FlutterViewId view_id)
virtual void ComposeChangeHook(const std::u16string &text, int cursor_pos)
virtual void KeyboardHook(int key, int scancode, int action, char32_t character, bool extended, bool was_down)
const TextInputModel * GetActiveModel() const
TextInputPluginModifier(TextInputPlugin *text_input_plugin)
MOCK_METHOD(void, NotifyWinEventWrapper,(ui::AXPlatformNodeWin *, ax::mojom::Event),(override))
MockFlutterWindowsView(FlutterWindowsEngine *engine, std::unique_ptr< WindowBindingHandler > wbh)
std::unique_ptr< MockFlutterWindowsView > AddViewWithId(int view_id)
static constexpr char kChannelName[]
std::u16string text
Win32Message message
TEST_F(AccessibilityPluginTest, DirectAnnounceCall)
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
int64_t FlutterViewId
constexpr FlutterViewId kImplicitViewId
static constexpr char kTextPlainFormat[]
static constexpr char kTextKey[]
static constexpr char kEnableDeltaModel[]
static constexpr char kSelectionIsDirectionalKey[]
static constexpr char kSetEditingStateMethod[]
static constexpr char kSelectionExtentKey[]
static constexpr char kSetClientMethod[]
static constexpr char kUpdateEditingStateMethod[]
static constexpr char kComposingExtentKey[]
static constexpr char kComposingBaseKey[]
static constexpr char kAffinityDownstream[]
static constexpr char kSelectionAffinityKey[]
static constexpr char kViewId[]
static constexpr char kSelectionBaseKey[]