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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/exo/text_input.h"
#include <memory>
#include <string>
#include "ash/constants/ash_features.h"
#include "ash/keyboard/ui/keyboard_ui_controller.h"
#include "ash/test/ash_test_helper.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "components/exo/buffer.h"
#include "components/exo/seat.h"
#include "components/exo/shell_surface.h"
#include "components/exo/surface.h"
#include "components/exo/test/exo_test_base.h"
#include "components/exo/test/exo_test_helper.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/focus_client.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/ime/ash/input_method_manager.h"
#include "ui/base/ime/ash/mock_input_method_manager_impl.h"
#include "ui/base/ime/composition_text.h"
#include "ui/base/ime/input_method_observer.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/ozone/events_ozone.h"
#include "ui/views/widget/widget.h"
using testing::_;
namespace exo {
namespace {
ui::CompositionText GenerateCompositionText(const std::u16string& text) {
ui::CompositionText t;
t.text = text;
t.selection = gfx::Range(text.size());
t.ime_text_spans.emplace_back(ui::ImeTextSpan::Type::kComposition, 0,
t.text.size(),
ui::ImeTextSpan::Thickness::kThick);
return t;
}
class MockTextInputDelegate : public TextInput::Delegate {
public:
MockTextInputDelegate() = default;
MockTextInputDelegate(const MockTextInputDelegate&) = delete;
MockTextInputDelegate& operator=(const MockTextInputDelegate&) = delete;
~MockTextInputDelegate() override = default;
// TextInput::Delegate:
MOCK_METHOD(void, Activated, (), (override));
MOCK_METHOD(void, Deactivated, (), (override));
MOCK_METHOD(void, OnVirtualKeyboardVisibilityChanged, (bool), (override));
MOCK_METHOD(void,
OnVirtualKeyboardOccludedBoundsChanged,
(const gfx::Rect&),
(override));
MOCK_METHOD(bool, SupportsFinalizeVirtualKeyboardChanges, (), (override));
MOCK_METHOD(void,
SetCompositionText,
(const ui::CompositionText&),
(override));
MOCK_METHOD(void, Commit, (base::StringPiece16), (override));
MOCK_METHOD(void,
SetCursor,
(base::StringPiece16, const gfx::Range&),
(override));
MOCK_METHOD(void,
DeleteSurroundingText,
(base::StringPiece16, const gfx::Range&),
(override));
MOCK_METHOD(void, SendKey, (const ui::KeyEvent&), (override));
MOCK_METHOD(void,
OnTextDirectionChanged,
(base::i18n::TextDirection direction),
(override));
MOCK_METHOD(void,
SetCompositionFromExistingText,
(base::StringPiece16,
const gfx::Range&,
const gfx::Range&,
const std::vector<ui::ImeTextSpan>& ui_ime_text_spans),
(override));
MOCK_METHOD(void,
ClearGrammarFragments,
(base::StringPiece16, const gfx::Range&),
(override));
MOCK_METHOD(void,
AddGrammarFragment,
(base::StringPiece16, const ui::GrammarFragment&),
(override));
MOCK_METHOD(void,
SetAutocorrectRange,
(base::StringPiece16, const gfx::Range&),
(override));
MOCK_METHOD(bool, ConfirmComposition, (bool), (override));
MOCK_METHOD(bool, SupportsConfirmPreedit, (), (override));
MOCK_METHOD(bool, HasImageInsertSupport, (), (override));
MOCK_METHOD(void, InsertImage, (const GURL& src), (override));
};
class TestingInputMethodObserver : public ui::InputMethodObserver {
public:
explicit TestingInputMethodObserver(ui::InputMethod* input_method)
: input_method_(input_method) {
input_method_->AddObserver(this);
}
TestingInputMethodObserver(const TestingInputMethodObserver&) = delete;
TestingInputMethodObserver& operator=(const TestingInputMethodObserver&) =
delete;
~TestingInputMethodObserver() override {
input_method_->RemoveObserver(this);
}
// ui::InputMethodObserver
MOCK_METHOD(void, OnFocus, (), (override));
MOCK_METHOD(void, OnBlur, (), (override));
MOCK_METHOD(void,
OnCaretBoundsChanged,
(const ui::TextInputClient*),
(override));
MOCK_METHOD(void,
OnTextInputStateChanged,
(const ui::TextInputClient*),
(override));
MOCK_METHOD(void,
OnInputMethodDestroyed,
(const ui::InputMethod*),
(override));
MOCK_METHOD(void,
OnVirtualKeyboardVisibilityChangedIfEnabled,
(bool),
(override));
private:
raw_ptr<ui::InputMethod, ExperimentalAsh> input_method_ = nullptr;
};
class TextInputTest : public test::ExoTestBase {
public:
class TestSurface {
public:
void SetUp(test::ExoTestHelper* exo_test_helper);
void TearDown();
Surface* surface() { return surface_.get(); }
private:
std::unique_ptr<Buffer> buffer_;
std::unique_ptr<Surface> surface_;
std::unique_ptr<ShellSurface> shell_surface_;
};
TextInputTest() = default;
TextInputTest(const TextInputTest&) = delete;
TextInputTest& operator=(const TextInputTest&) = delete;
void SetUp() override {
test::ExoTestBase::SetUp();
text_input_ = std::make_unique<TextInput>(
std::make_unique<testing::NiceMock<MockTextInputDelegate>>());
seat_ = std::make_unique<Seat>();
test_surface_.SetUp(exo_test_helper());
ON_CALL(*delegate(), SupportsFinalizeVirtualKeyboardChanges())
.WillByDefault(testing::Return(false));
}
void TearDown() override {
test_surface_.TearDown();
seat_.reset();
text_input_.reset();
test::ExoTestBase::TearDown();
}
protected:
TextInput* text_input() { return text_input_.get(); }
void DestroyTextInput() { text_input_.reset(); }
MockTextInputDelegate* delegate() {
return static_cast<MockTextInputDelegate*>(text_input_->delegate());
}
Surface* surface() { return test_surface_.surface(); }
Seat* seat() { return seat_.get(); }
ui::InputMethod* GetInputMethod() {
return surface()->window()->GetHost()->GetInputMethod();
}
void SetCompositionText(const std::u16string& utf16) {
ui::CompositionText t = GenerateCompositionText(utf16);
EXPECT_CALL(*delegate(), SetCompositionText(t)).Times(1);
text_input()->SetCompositionText(t);
}
private:
std::unique_ptr<TextInput> text_input_;
std::unique_ptr<Seat> seat_;
TestSurface test_surface_;
};
void TextInputTest::TestSurface::SetUp(test::ExoTestHelper* exo_test_helper) {
gfx::Size buffer_size(32, 32);
buffer_ = std::make_unique<Buffer>(
exo_test_helper->CreateGpuMemoryBuffer(buffer_size));
surface_ = std::make_unique<Surface>();
shell_surface_ = std::make_unique<ShellSurface>(surface_.get());
surface_->Attach(buffer_.get());
surface_->Commit();
gfx::Point origin(100, 100);
shell_surface_->SetGeometry(gfx::Rect(origin, buffer_size));
}
void TextInputTest::TestSurface::TearDown() {
shell_surface_.reset();
surface_.reset();
buffer_.reset();
}
// Test for both kExoConsumedByImeByFlag enabled and disabled.
class TextInputTestWithConsumedByIme
: public TextInputTest,
public testing::WithParamInterface<bool> {
public:
void SetUp() override {
if (GetParam()) {
feature_list_.InitAndEnableFeature(
ash::features::kExoConsumedByImeByFlag);
} else {
feature_list_.InitAndDisableFeature(
ash::features::kExoConsumedByImeByFlag);
}
TextInputTest::SetUp();
}
private:
base::test::ScopedFeatureList feature_list_;
};
INSTANTIATE_TEST_SUITE_P(, TextInputTestWithConsumedByIme, ::testing::Bool());
// Test for both kExoExtendedConfirmComposition enabled and disabled.
class TextInputTestWithExtendedConfirmComposition
: public TextInputTest,
public testing::WithParamInterface<bool> {
public:
void SetUp() override {
if (GetParam()) {
feature_list_.InitAndEnableFeature(
ash::features::kExoExtendedConfirmComposition);
} else {
feature_list_.InitAndDisableFeature(
ash::features::kExoExtendedConfirmComposition);
}
TextInputTest::SetUp();
}
private:
base::test::ScopedFeatureList feature_list_;
};
INSTANTIATE_TEST_SUITE_P(,
TextInputTestWithExtendedConfirmComposition,
::testing::Bool());
TEST_F(TextInputTest, Activate) {
EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, text_input()->GetTextInputType());
EXPECT_EQ(ui::TEXT_INPUT_MODE_DEFAULT, text_input()->GetTextInputMode());
EXPECT_CALL(*delegate(), Activated).Times(1);
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
testing::Mock::VerifyAndClearExpectations(delegate());
EXPECT_EQ(ui::TEXT_INPUT_TYPE_TEXT, text_input()->GetTextInputType());
EXPECT_EQ(ui::TEXT_INPUT_MODE_TEXT, text_input()->GetTextInputMode());
EXPECT_EQ(0, text_input()->GetTextInputFlags());
EXPECT_CALL(*delegate(), Deactivated).Times(1);
text_input()->Deactivate();
testing::Mock::VerifyAndClearExpectations(delegate());
EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, text_input()->GetTextInputType());
EXPECT_EQ(ui::TEXT_INPUT_MODE_DEFAULT, text_input()->GetTextInputMode());
}
TEST_F(TextInputTest, ActivationRequiresFocus) {
TestingInputMethodObserver observer(GetInputMethod());
// Activation doesn't occur until the surface (window) is actually focused.
aura::client::FocusClient* focus_client =
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
focus_client->FocusWindow(nullptr);
EXPECT_CALL(observer, OnTextInputStateChanged(_)).Times(0);
EXPECT_CALL(*delegate(), Activated).Times(0);
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
testing::Mock::VerifyAndClearExpectations(&observer);
testing::Mock::VerifyAndClearExpectations(delegate());
EXPECT_CALL(observer, OnTextInputStateChanged(text_input())).Times(1);
EXPECT_CALL(*delegate(), Activated).Times(1);
focus_client->FocusWindow(surface()->window());
testing::Mock::VerifyAndClearExpectations(&observer);
testing::Mock::VerifyAndClearExpectations(delegate());
// Deactivation occurs on blur even if TextInput::Deactivate() isn't called.
EXPECT_CALL(observer, OnTextInputStateChanged(nullptr)).Times(1);
EXPECT_CALL(*delegate(), Deactivated).Times(1);
focus_client->FocusWindow(nullptr);
testing::Mock::VerifyAndClearExpectations(&observer);
testing::Mock::VerifyAndClearExpectations(delegate());
EXPECT_CALL(observer, OnTextInputStateChanged(nullptr)).Times(0);
EXPECT_CALL(*delegate(), Deactivated).Times(0);
text_input()->Deactivate();
testing::Mock::VerifyAndClearExpectations(&observer);
testing::Mock::VerifyAndClearExpectations(delegate());
}
TEST_F(TextInputTest, MultipleActivations) {
TestingInputMethodObserver observer(GetInputMethod());
aura::client::FocusClient* focus_client =
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
TestSurface surface2;
surface2.SetUp(exo_test_helper());
// Activate surface 1.
focus_client->FocusWindow(surface()->window());
EXPECT_CALL(observer, OnTextInputStateChanged(text_input())).Times(1);
EXPECT_CALL(*delegate(), Activated).Times(1);
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
testing::Mock::VerifyAndClearExpectations(&observer);
testing::Mock::VerifyAndClearExpectations(delegate());
// Attempting to activate the same surface is a no-op.
EXPECT_CALL(*delegate(), Activated).Times(0);
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
testing::Mock::VerifyAndClearExpectations(delegate());
// Activating a non-focused surface causes deactivation until focus.
EXPECT_CALL(observer, OnTextInputStateChanged(nullptr)).Times(1);
EXPECT_CALL(*delegate(), Deactivated).Times(1);
text_input()->Activate(seat(), surface2.surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
testing::Mock::VerifyAndClearExpectations(&observer);
testing::Mock::VerifyAndClearExpectations(delegate());
EXPECT_CALL(observer, OnTextInputStateChanged(text_input())).Times(1);
EXPECT_CALL(*delegate(), Activated).Times(1);
focus_client->FocusWindow(surface2.surface()->window());
testing::Mock::VerifyAndClearExpectations(&observer);
testing::Mock::VerifyAndClearExpectations(delegate());
}
TEST_F(TextInputTest, ShowVirtualKeyboardIfEnabled) {
TestingInputMethodObserver observer(GetInputMethod());
EXPECT_CALL(observer, OnTextInputStateChanged(text_input())).Times(1);
EXPECT_CALL(*delegate(), Activated).Times(1);
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
testing::Mock::VerifyAndClearExpectations(&observer);
testing::Mock::VerifyAndClearExpectations(delegate());
// Currently, Virtual Keyboard Controller is not set up, and so
// the virtual keyboard events are gone. Here, we capture the callback
// from the observer and translate it to the ones of
// VirtualKeyboardControllerObserver event as if it is done via
// real VirtualKeyboardController implementation.
EXPECT_CALL(observer, OnVirtualKeyboardVisibilityChangedIfEnabled)
.WillOnce(testing::Invoke([this](bool should_show) {
if (should_show)
text_input()->OnKeyboardVisible(gfx::Rect());
else
text_input()->OnKeyboardHidden();
}));
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged(true)).Times(1);
text_input()->ShowVirtualKeyboardIfEnabled();
testing::Mock::VerifyAndClearExpectations(&observer);
testing::Mock::VerifyAndClearExpectations(delegate());
EXPECT_CALL(observer, OnTextInputStateChanged(nullptr)).Times(1);
EXPECT_CALL(*delegate(), Deactivated).Times(1);
text_input()->Deactivate();
testing::Mock::VerifyAndClearExpectations(&observer);
testing::Mock::VerifyAndClearExpectations(delegate());
}
TEST_F(TextInputTest, ShowVirtualKeyboardIfEnabledBeforeActivated) {
TestingInputMethodObserver observer(GetInputMethod());
// ShowVirtualKeyboardIfEnabled before activation.
text_input()->ShowVirtualKeyboardIfEnabled();
EXPECT_CALL(observer, OnTextInputStateChanged(text_input())).Times(1);
// Currently, Virtual Keyboard Controller is not set up, and so
// the virtual keyboard events are gone. Here, we capture the callback
// from the observer and translate it to the ones of
// VirtualKeyboardControllerObserver event as if it is done via
// real VirtualKeyboardController implementation.
EXPECT_CALL(observer, OnVirtualKeyboardVisibilityChangedIfEnabled)
.WillOnce(testing::Invoke([this](bool should_show) {
if (should_show)
text_input()->OnKeyboardVisible(gfx::Rect());
else
text_input()->OnKeyboardHidden();
}));
EXPECT_CALL(*delegate(), Activated).Times(1);
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged(true)).Times(1);
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
testing::Mock::VerifyAndClearExpectations(&observer);
testing::Mock::VerifyAndClearExpectations(delegate());
EXPECT_CALL(*delegate(), Deactivated).Times(1);
}
TEST_F(TextInputTest, VirtualKeyboardObserver) {
EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, text_input()->GetTextInputType());
EXPECT_EQ(ui::TEXT_INPUT_MODE_DEFAULT, text_input()->GetTextInputMode());
EXPECT_CALL(*delegate(), Activated).Times(1);
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
testing::Mock::VerifyAndClearExpectations(delegate());
EXPECT_EQ(ui::TEXT_INPUT_TYPE_TEXT, text_input()->GetTextInputType());
EXPECT_EQ(ui::TEXT_INPUT_MODE_TEXT, text_input()->GetTextInputMode());
EXPECT_EQ(0, text_input()->GetTextInputFlags());
EXPECT_CALL(*delegate(), Deactivated).Times(1);
text_input()->Deactivate();
testing::Mock::VerifyAndClearExpectations(delegate());
EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, text_input()->GetTextInputType());
EXPECT_EQ(ui::TEXT_INPUT_MODE_DEFAULT, text_input()->GetTextInputMode());
// Destroy the text_input.
DestroyTextInput();
}
TEST_F(TextInputTest, SetTypeModeFlag) {
TestingInputMethodObserver observer(GetInputMethod());
EXPECT_CALL(observer, OnTextInputStateChanged(text_input())).Times(1);
EXPECT_CALL(*delegate(), Activated).Times(1);
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
testing::Mock::VerifyAndClearExpectations(&observer);
testing::Mock::VerifyAndClearExpectations(delegate());
EXPECT_EQ(ui::TEXT_INPUT_TYPE_TEXT, text_input()->GetTextInputType());
EXPECT_EQ(ui::TEXT_INPUT_MODE_TEXT, text_input()->GetTextInputMode());
EXPECT_EQ(0, text_input()->GetTextInputFlags());
EXPECT_TRUE(text_input()->ShouldDoLearning());
EXPECT_TRUE(text_input()->CanComposeInline());
int flags = ui::TEXT_INPUT_FLAG_AUTOCOMPLETE_OFF |
ui::TEXT_INPUT_FLAG_AUTOCAPITALIZE_NONE;
EXPECT_CALL(observer, OnTextInputStateChanged(text_input())).Times(1);
text_input()->SetTypeModeFlags(
ui::TEXT_INPUT_TYPE_URL, ui::TEXT_INPUT_MODE_URL, flags,
/*should_do_learning=*/false, /*can_compose_inline=*/false,
/*surrounding_text_supported=*/true);
testing::Mock::VerifyAndClearExpectations(&observer);
EXPECT_EQ(ui::TEXT_INPUT_TYPE_URL, text_input()->GetTextInputType());
EXPECT_EQ(ui::TEXT_INPUT_MODE_URL, text_input()->GetTextInputMode());
EXPECT_EQ(flags, text_input()->GetTextInputFlags());
EXPECT_FALSE(text_input()->ShouldDoLearning());
EXPECT_FALSE(text_input()->CanComposeInline());
EXPECT_CALL(*delegate(), Deactivated).Times(1);
}
TEST_F(TextInputTest, FocusReason) {
EXPECT_EQ(ui::TextInputClient::FOCUS_REASON_NONE,
text_input()->GetFocusReason());
EXPECT_CALL(*delegate(), Activated).Times(1);
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
testing::Mock::VerifyAndClearExpectations(delegate());
EXPECT_EQ(ui::TextInputClient::FOCUS_REASON_OTHER,
text_input()->GetFocusReason());
EXPECT_CALL(*delegate(), Deactivated).Times(1);
text_input()->Deactivate();
testing::Mock::VerifyAndClearExpectations(delegate());
EXPECT_CALL(*delegate(), Activated).Times(1);
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_PEN);
testing::Mock::VerifyAndClearExpectations(delegate());
EXPECT_EQ(ui::TextInputClient::FOCUS_REASON_PEN,
text_input()->GetFocusReason());
EXPECT_CALL(*delegate(), Deactivated).Times(1);
}
TEST_F(TextInputTest, CaretBounds) {
TestingInputMethodObserver observer(GetInputMethod());
EXPECT_CALL(observer, OnTextInputStateChanged(text_input())).Times(1);
EXPECT_CALL(*delegate(), Activated).Times(1);
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
testing::Mock::VerifyAndClearExpectations(&observer);
testing::Mock::VerifyAndClearExpectations(delegate());
gfx::Rect bounds(10, 10, 0, 16);
EXPECT_CALL(observer, OnCaretBoundsChanged(text_input())).Times(1);
text_input()->SetCaretBounds(bounds);
testing::Mock::VerifyAndClearExpectations(&observer);
EXPECT_EQ(bounds.size().ToString(),
text_input()->GetCaretBounds().size().ToString());
gfx::Point origin = surface()->window()->GetBoundsInScreen().origin();
origin += bounds.OffsetFromOrigin();
EXPECT_EQ(origin.ToString(),
text_input()->GetCaretBounds().origin().ToString());
EXPECT_CALL(*delegate(), Deactivated).Times(1);
}
TEST_F(TextInputTest, CompositionText) {
EXPECT_FALSE(text_input()->HasCompositionText());
SetCompositionText(u"composition");
EXPECT_TRUE(text_input()->HasCompositionText());
ui::CompositionText empty;
EXPECT_CALL(*delegate(), SetCompositionText(empty)).Times(1);
text_input()->ClearCompositionText();
EXPECT_FALSE(text_input()->HasCompositionText());
}
TEST_F(TextInputTest, CompositionTextEmpty) {
SetCompositionText(u"");
EXPECT_CALL(*delegate(), SetCompositionText(_)).Times(0);
text_input()->ClearCompositionText();
}
TEST_P(TextInputTestWithExtendedConfirmComposition,
ConfirmCompositionTextDontKeepSelection) {
SetCompositionText(u"composition");
if (GetParam()) {
EXPECT_CALL(*delegate(), ConfirmComposition(/*keep_selection=*/false))
.Times(1);
} else {
EXPECT_CALL(*delegate(), Commit(base::StringPiece16(u"composition")));
}
const size_t composition_text_length =
text_input()->ConfirmCompositionText(/*keep_selection=*/false);
EXPECT_EQ(composition_text_length, 11u);
testing::Mock::VerifyAndClearExpectations(delegate());
// Second call should be the empty commit string.
EXPECT_EQ(0u, text_input()->ConfirmCompositionText(/*keep_selection=*/false));
EXPECT_FALSE(text_input()->HasCompositionText());
}
TEST_P(TextInputTestWithExtendedConfirmComposition,
ConfirmCompositionTextKeepSelection) {
constexpr char16_t kCompositionText[] = u"composition";
SetCompositionText(kCompositionText);
text_input()->SetEditableSelectionRange(gfx::Range(2, 3));
text_input()->SetSurroundingText(kCompositionText, 0u, gfx::Range(2, 3),
absl::nullopt, absl::nullopt);
if (GetParam()) {
EXPECT_CALL(*delegate(), ConfirmComposition(/*keep_selection=*/true))
.Times(1);
} else {
EXPECT_CALL(*delegate(), SetCursor(base::StringPiece16(kCompositionText),
gfx::Range(2, 3)))
.Times(1);
EXPECT_CALL(*delegate(), Commit(base::StringPiece16(kCompositionText)));
}
const uint32_t composition_text_length =
text_input()->ConfirmCompositionText(/*keep_selection=*/true);
EXPECT_EQ(composition_text_length, static_cast<uint32_t>(11));
testing::Mock::VerifyAndClearExpectations(delegate());
// Second call should be the empty commit string.
EXPECT_EQ(0u, text_input()->ConfirmCompositionText(/*keep_selection=*/true));
EXPECT_FALSE(text_input()->HasCompositionText());
}
TEST_F(TextInputTest, ResetCompositionText) {
SetCompositionText(u"composition");
text_input()->Reset();
EXPECT_EQ(0u, text_input()->ConfirmCompositionText(/*keep_selection=*/false));
EXPECT_FALSE(text_input()->HasCompositionText());
}
TEST_F(TextInputTest, Commit) {
constexpr char16_t s[] = u"commit text";
EXPECT_CALL(*delegate(), Commit(base::StringPiece16(s))).Times(1);
text_input()->InsertText(
s, ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
EXPECT_FALSE(text_input()->HasCompositionText());
}
TEST_P(TextInputTestWithConsumedByIme, InsertChar) {
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
ui::KeyEvent ev(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0);
ui::SetKeyboardImeFlags(&ev, ui::kPropertyKeyboardImeHandledFlag);
EXPECT_CALL(*delegate(), SendKey(testing::Ref(ev))).Times(1);
text_input()->InsertChar(ev);
}
TEST_P(TextInputTestWithConsumedByIme, InsertCharCtrlV) {
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
// CTRL+V is interpreted as non-IME consumed KeyEvent, so should
// not be sent.
ui::KeyEvent ev(ui::ET_KEY_PRESSED, ui::VKEY_V, ui::EF_CONTROL_DOWN);
EXPECT_CALL(*delegate(), SendKey(_)).Times(0);
text_input()->InsertChar(ev);
}
TEST_P(TextInputTestWithConsumedByIme, InsertCharNormalKey) {
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
char16_t ch = 'x';
ui::KeyEvent ev =
ui::KeyEvent::FromCharacter(ch, ui::VKEY_X, ui::DomCode::US_X, 0);
ui::SetKeyboardImeFlags(&ev, ui::kPropertyKeyboardImeHandledFlag);
EXPECT_CALL(*delegate(), SendKey(testing::Ref(ev))).Times(1);
text_input()->InsertChar(ev);
}
TEST_P(TextInputTestWithConsumedByIme, InsertCharNumpadEqual) {
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
// NUMPAD_EQUAL is set key_code to VKEY_UNKNOWN, but code t- NUMPAD_EQUAL.
ui::KeyEvent ev(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN,
ui::DomCode::NUMPAD_EQUAL, /*flags=*/0, /*key=*/0,
base::TimeTicks());
ev.set_character(u'=');
if (GetParam()) {
// If ConsumedByIme fix is enabled, InsertChar should ignore it (because
// it is not consumed by IME), and exo::Keyboard is expected to handle the
// case.
EXPECT_CALL(*delegate(), SendKey(_)).Times(0);
EXPECT_CALL(*delegate(), Commit(_)).Times(0);
} else {
// If ConsumedByIme fix is disabled, the event is (wrongly) interpreted
// as consumed by IME, so exo::Keyboard does not send key events.
// Instead, InsertChar here is expected to send the event via Commit().
EXPECT_CALL(*delegate(), SendKey(_)).Times(0);
EXPECT_CALL(*delegate(), Commit(base::StringPiece16(u"="))).Times(1);
}
text_input()->InsertChar(ev);
testing::Mock::VerifyAndClearExpectations(delegate());
}
TEST_F(TextInputTest, SurroundingText) {
TestingInputMethodObserver observer(GetInputMethod());
gfx::Range range;
EXPECT_TRUE(text_input()->GetTextRange(&range));
EXPECT_EQ(gfx::Range(0, 0), range);
EXPECT_FALSE(text_input()->GetCompositionTextRange(&range));
EXPECT_TRUE(text_input()->GetEditableSelectionRange(&range));
EXPECT_EQ(gfx::Range(0, 0), range);
std::u16string got_text;
EXPECT_FALSE(text_input()->GetTextFromRange(gfx::Range(0, 1), &got_text));
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
EXPECT_CALL(observer, OnCaretBoundsChanged(text_input())).Times(1);
std::u16string text = u"surrounding\u3000text";
text_input()->SetSurroundingText(text, 0u, gfx::Range(11, 12), absl::nullopt,
absl::nullopt);
testing::Mock::VerifyAndClearExpectations(&observer);
EXPECT_TRUE(text_input()->GetTextRange(&range));
EXPECT_EQ(gfx::Range(0, text.size()).ToString(), range.ToString());
EXPECT_FALSE(text_input()->GetCompositionTextRange(&range));
EXPECT_TRUE(text_input()->GetEditableSelectionRange(&range));
EXPECT_EQ(gfx::Range(11, 12).ToString(), range.ToString());
EXPECT_TRUE(text_input()->GetTextFromRange(gfx::Range(11, 12), &got_text));
EXPECT_EQ(text.substr(11, 1), got_text);
EXPECT_CALL(*delegate(), DeleteSurroundingText(base::StringPiece16(text),
gfx::Range(11, 12)))
.Times(1);
text_input()->ExtendSelectionAndDelete(0, 0);
testing::Mock::VerifyAndClearExpectations(delegate());
size_t composition_size = std::string("composition").size();
SetCompositionText(u"composition");
EXPECT_TRUE(text_input()->GetCompositionTextRange(&range));
EXPECT_EQ(gfx::Range(11, 11 + composition_size), range);
EXPECT_TRUE(text_input()->GetEditableSelectionRange(&range));
EXPECT_EQ(gfx::Range(11 + composition_size), range);
}
TEST_F(TextInputTest, SetEditableSelectionRange) {
SetCompositionText(u"text");
text_input()->SetSurroundingText(u"text", 0u, gfx::Range(4, 4), absl::nullopt,
absl::nullopt);
// Should commit composition text and set selection range.
EXPECT_CALL(*delegate(),
SetCursor(base::StringPiece16(u"text"), gfx::Range(0, 3)))
.Times(1);
EXPECT_CALL(*delegate(), Commit(base::StringPiece16(u"text"))).Times(1);
EXPECT_TRUE(text_input()->SetEditableSelectionRange(gfx::Range(0, 3)));
testing::Mock::VerifyAndClearExpectations(delegate());
}
TEST_F(TextInputTest, GetTextFromRange) {
std::u16string text = u"surrounding text";
text_input()->SetEditableSelectionRange(gfx::Range(11, 12));
text_input()->SetSurroundingText(text, 0u, gfx::Range(11, 12), absl::nullopt,
absl::nullopt);
const struct {
gfx::Range range;
std::u16string expected;
} kTestCases[] = {
{gfx::Range(0, 3), u"sur"},
{gfx::Range(10, 16), u"g text"},
{gfx::Range(6, 9), u"ndi"},
};
for (auto& c : kTestCases) {
std::u16string result;
EXPECT_TRUE(text_input()->GetTextFromRange(c.range, &result))
<< c.range.ToString();
EXPECT_EQ(c.expected, result) << c.range.ToString();
}
}
TEST_F(TextInputTest, GetTextFromRangeWithOffset) {
std::u16string text = u"surrounding text";
text_input()->SetEditableSelectionRange(gfx::Range(11, 12));
text_input()->SetSurroundingText(text, 5u, gfx::Range(11, 12), absl::nullopt,
absl::nullopt);
gfx::Range text_range;
ASSERT_TRUE(text_input()->GetTextRange(&text_range));
EXPECT_EQ(gfx::Range(5, 21), text_range);
const struct {
gfx::Range range;
std::u16string expected;
} kTestCases[] = {
{gfx::Range(5, 8), u"sur"},
{gfx::Range(15, 21), u"g text"},
{gfx::Range(11, 14), u"ndi"},
};
for (auto& c : kTestCases) {
std::u16string result;
EXPECT_TRUE(text_input()->GetTextFromRange(c.range, &result))
<< c.range.ToString();
EXPECT_EQ(c.expected, result) << c.range.ToString();
}
}
TEST_F(TextInputTest, SetCompositionFromExistingText) {
// Try invalid cases fist. No delegate invocation is expected.
EXPECT_CALL(*delegate(), SetCompositionFromExistingText(_, _, _, _)).Times(0);
// Not set up surrounding text yet, so any request should fail.
EXPECT_FALSE(text_input()->SetCompositionFromExistingText(
gfx::Range::InvalidRange(), {}));
EXPECT_FALSE(
text_input()->SetCompositionFromExistingText(gfx::Range(0, 1), {}));
text_input()->SetSurroundingText(u"surrounding text", 0u, gfx::Range(5, 5),
absl::nullopt, absl::nullopt);
// Invalid range.
EXPECT_FALSE(text_input()->SetCompositionFromExistingText(
gfx::Range::InvalidRange(), {}));
// Outside of surrounding text.
EXPECT_FALSE(
text_input()->SetCompositionFromExistingText(gfx::Range(100, 200), {}));
// Crossing the boundary of surrounding text.
EXPECT_FALSE(
text_input()->SetCompositionFromExistingText(gfx::Range(5, 100), {}));
// Span has the range outside of the new composition.
EXPECT_FALSE(text_input()->SetCompositionFromExistingText(
gfx::Range(3, 10),
{ui::ImeTextSpan(ui::ImeTextSpan::Type::kComposition, 7, 10)}));
// Span has the range crossing the composition boundary.
EXPECT_FALSE(text_input()->SetCompositionFromExistingText(
gfx::Range(3, 10),
{ui::ImeTextSpan(ui::ImeTextSpan::Type::kComposition, 2, 10)}));
// Verify mock behavior. No delegate call is expected until now.
testing::Mock::VerifyAndClearExpectations(delegate());
// Checking a simple valid case.
EXPECT_CALL(*delegate(), SetCompositionFromExistingText(_, _, _, _)).Times(1);
EXPECT_TRUE(
text_input()->SetCompositionFromExistingText(gfx::Range(3, 10), {}));
testing::Mock::VerifyAndClearExpectations(delegate());
// Anothe valid case with span.
EXPECT_CALL(*delegate(), SetCompositionFromExistingText(_, _, _, _)).Times(1);
EXPECT_TRUE(text_input()->SetCompositionFromExistingText(
gfx::Range(3, 10),
{ui::ImeTextSpan(ui::ImeTextSpan::Type::kComposition, 1, 5)}));
testing::Mock::VerifyAndClearExpectations(delegate());
}
TEST_F(TextInputTest,
CompositionRangeSetFromCursorWhenSetCompositionTextCalled) {
text_input()->SetSurroundingText(u"surrounding text", 0u, gfx::Range(5, 5),
absl::nullopt, absl::nullopt);
std::u16string composition_text = u"composing";
SetCompositionText(composition_text);
gfx::Range composition_range;
EXPECT_TRUE(text_input()->HasCompositionText());
EXPECT_TRUE(text_input()->GetCompositionTextRange(&composition_range));
EXPECT_EQ(composition_range, gfx::Range(5, 5 + composition_text.length()));
}
TEST_F(TextInputTest,
CompositionRangeSetWhenSetCompositionFromExistingTextCalled) {
text_input()->SetSurroundingText(u"surrounding text", 0u, gfx::Range(5, 5),
absl::nullopt, absl::nullopt);
text_input()->SetCompositionFromExistingText(gfx::Range(3, 6),
std::vector<ui::ImeTextSpan>{});
gfx::Range composition_range;
EXPECT_TRUE(text_input()->HasCompositionText());
EXPECT_TRUE(text_input()->GetCompositionTextRange(&composition_range));
EXPECT_EQ(composition_range, gfx::Range(3, 6));
}
TEST_F(TextInputTest, CorrectTextReturnedAfterSetCompositionTextCalled) {
gfx::Range cursor_pos = gfx::Range(11, 11);
std::u16string surrounding_text = u"surrounding text";
std::u16string composition_text = u" and composition";
ui::CompositionText t = GenerateCompositionText(composition_text);
EXPECT_CALL(*delegate(), SetCompositionText(_)).Times(1);
text_input()->SetSurroundingText(surrounding_text, 0u, cursor_pos,
absl::nullopt, absl::nullopt);
text_input()->SetCompositionText(t);
// Simulate surrounding text update from wayland.
auto before = surrounding_text.substr(0, cursor_pos.GetMin());
auto after = surrounding_text.substr(cursor_pos.GetMin());
auto new_surrounding = before + t.text + after;
auto new_cursor_pos = cursor_pos.GetMin() + t.text.length();
text_input()->SetSurroundingText(new_surrounding, 0u,
gfx::Range(new_cursor_pos, new_cursor_pos),
absl::nullopt, absl::nullopt);
gfx::Range text_range;
std::u16string text;
EXPECT_TRUE(text_input()->GetTextRange(&text_range));
EXPECT_TRUE(text_input()->GetTextFromRange(text_range, &text));
EXPECT_EQ(text, u"surrounding and composition text");
gfx::Range composing_text_range;
std::u16string composing_text;
EXPECT_TRUE(text_input()->HasCompositionText());
EXPECT_TRUE(text_input()->GetCompositionTextRange(&composing_text_range));
EXPECT_TRUE(
text_input()->GetTextFromRange(composing_text_range, &composing_text));
EXPECT_EQ(composing_text, u" and composition");
}
TEST_F(TextInputTest, SetsAndGetsGrammarFragmentAtCursor) {
ui::GrammarFragment sample_fragment(gfx::Range(1, 5), "sample-suggestion");
EXPECT_EQ(text_input()->GetGrammarFragmentAtCursor(), absl::nullopt);
text_input()->SetSurroundingText(u"Sample surrouding text.", 0u,
gfx::Range(2, 2), sample_fragment,
absl::nullopt);
EXPECT_EQ(text_input()->GetGrammarFragmentAtCursor(), sample_fragment);
}
TEST_F(TextInputTest, ClearGrammarFragments) {
std::u16string surrounding_text = u"Sample surrouding text.";
text_input()->SetSurroundingText(surrounding_text, 0u, gfx::Range(2, 2),
absl::nullopt, absl::nullopt);
gfx::Range range(3, 8);
EXPECT_CALL(*delegate(), ClearGrammarFragments(
base::StringPiece16(surrounding_text), range))
.Times(1);
text_input()->ClearGrammarFragments(range);
}
TEST_F(TextInputTest, AddGrammarFragments) {
std::u16string surrounding_text = u"Sample surrouding text.";
text_input()->SetSurroundingText(surrounding_text, 0u, gfx::Range(2, 2),
absl::nullopt, absl::nullopt);
std::vector<ui::GrammarFragment> fragments = {
ui::GrammarFragment(gfx::Range(0, 5), "one"),
ui::GrammarFragment(gfx::Range(10, 16), "two"),
};
EXPECT_CALL(
*delegate(),
AddGrammarFragment(base::StringPiece16(surrounding_text), fragments[0]))
.Times(1);
EXPECT_CALL(
*delegate(),
AddGrammarFragment(base::StringPiece16(surrounding_text), fragments[1]))
.Times(1);
text_input()->AddGrammarFragments(fragments);
}
TEST_F(TextInputTest, GetAutocorrect) {
std::u16string surrounding_text = u"Sample surrouding text.";
text_input()->SetSurroundingText(surrounding_text, 0u, gfx::Range(2, 2),
absl::nullopt, absl::nullopt);
std::vector<ui::GrammarFragment> fragments = {
ui::GrammarFragment(gfx::Range(0, 5), "one"),
ui::GrammarFragment(gfx::Range(10, 16), "two"),
};
EXPECT_CALL(
*delegate(),
AddGrammarFragment(base::StringPiece16(surrounding_text), fragments[0]))
.Times(1);
EXPECT_CALL(
*delegate(),
AddGrammarFragment(base::StringPiece16(surrounding_text), fragments[1]))
.Times(1);
text_input()->AddGrammarFragments(fragments);
}
TEST_F(TextInputTest, EnsureCaretNotInRect) {
const gfx::Rect bounds(10, 20, 300, 400);
EXPECT_CALL(*delegate(), OnVirtualKeyboardOccludedBoundsChanged(bounds));
text_input()->EnsureCaretNotInRect(bounds);
}
TEST_F(TextInputTest, OnKeyboardHidden) {
const gfx::Rect bounds;
EXPECT_CALL(*delegate(), OnVirtualKeyboardOccludedBoundsChanged(bounds));
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged(false));
text_input()->OnKeyboardHidden();
}
TEST_F(TextInputTest, FinalizeVirtualKeyboardChangesNotSupported) {
EXPECT_CALL(*delegate(), SupportsFinalizeVirtualKeyboardChanges())
.WillRepeatedly(testing::Return(false));
const gfx::Rect kBounds(10, 20, 300, 400);
testing::InSequence s;
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged(true));
EXPECT_CALL(*delegate(), OnVirtualKeyboardOccludedBoundsChanged(kBounds));
text_input()->OnKeyboardVisible(gfx::Rect());
text_input()->EnsureCaretNotInRect(kBounds);
EXPECT_CALL(*delegate(), OnVirtualKeyboardOccludedBoundsChanged(gfx::Rect()));
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged(false));
text_input()->OnKeyboardHidden();
}
TEST_F(TextInputTest, FinalizeVirtualKeyboardChanges) {
EXPECT_CALL(*delegate(), SupportsFinalizeVirtualKeyboardChanges())
.WillRepeatedly(testing::Return(true));
const gfx::Rect kBounds(10, 20, 300, 400);
const gfx::Rect kBounds2(20, 40, 500, 600);
testing::InSequence s;
// After the client requests a vk change, the server buffers vk updates.
text_input()->ShowVirtualKeyboardIfEnabled();
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged).Times(0);
EXPECT_CALL(*delegate(), OnVirtualKeyboardOccludedBoundsChanged).Times(0);
text_input()->OnKeyboardVisible(gfx::Rect());
text_input()->EnsureCaretNotInRect(kBounds);
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged(true));
EXPECT_CALL(*delegate(), OnVirtualKeyboardOccludedBoundsChanged(kBounds));
text_input()->FinalizeVirtualKeyboardChanges();
// The server can update the client immediately if the client hasn't requested
// any new changes.
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged).Times(0);
EXPECT_CALL(*delegate(), OnVirtualKeyboardOccludedBoundsChanged(kBounds2));
text_input()->EnsureCaretNotInRect(kBounds2);
// The client requests to hide vk.
text_input()->HideVirtualKeyboard();
EXPECT_CALL(*delegate(), OnVirtualKeyboardOccludedBoundsChanged).Times(0);
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged).Times(0);
text_input()->OnKeyboardHidden();
EXPECT_CALL(*delegate(), OnVirtualKeyboardOccludedBoundsChanged(gfx::Rect()));
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged(false));
text_input()->FinalizeVirtualKeyboardChanges();
}
TEST_F(TextInputTest, FinalizeVirtualKeyboardChangesWithMultipleChanges) {
EXPECT_CALL(*delegate(), SupportsFinalizeVirtualKeyboardChanges())
.WillRepeatedly(testing::Return(true));
const gfx::Rect kBounds(10, 20, 300, 400);
const gfx::Rect kBounds2(20, 40, 500, 600);
const gfx::Rect kBounds3(30, 50, 200, 100);
testing::InSequence s;
// After the client requests a vk change, the server buffers vk updates.
text_input()->ShowVirtualKeyboardIfEnabled();
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged).Times(0);
EXPECT_CALL(*delegate(), OnVirtualKeyboardOccludedBoundsChanged).Times(0);
text_input()->OnKeyboardVisible(gfx::Rect());
text_input()->EnsureCaretNotInRect(kBounds);
text_input()->EnsureCaretNotInRect(kBounds2);
text_input()->OnKeyboardHidden();
text_input()->OnKeyboardVisible(gfx::Rect());
text_input()->EnsureCaretNotInRect(kBounds3);
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged(true));
EXPECT_CALL(*delegate(), OnVirtualKeyboardOccludedBoundsChanged(kBounds3));
text_input()->FinalizeVirtualKeyboardChanges();
}
TEST_F(TextInputTest, FinalizeVirtualKeyboardChangesDoesntSendStaleBounds) {
EXPECT_CALL(*delegate(), SupportsFinalizeVirtualKeyboardChanges())
.WillRepeatedly(testing::Return(true));
const gfx::Rect kBounds(10, 20, 300, 400);
const gfx::Rect kBounds2(20, 40, 500, 600);
testing::InSequence s;
// After the client requests a vk change, the server buffers vk updates.
text_input()->ShowVirtualKeyboardIfEnabled();
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged).Times(0);
EXPECT_CALL(*delegate(), OnVirtualKeyboardOccludedBoundsChanged).Times(0);
text_input()->OnKeyboardVisible(gfx::Rect());
text_input()->EnsureCaretNotInRect(kBounds);
text_input()->OnKeyboardHidden();
text_input()->OnKeyboardVisible(gfx::Rect());
// Showing vk invalidates any previously staged bounds.
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged(true));
EXPECT_CALL(*delegate(), OnVirtualKeyboardOccludedBoundsChanged).Times(0);
text_input()->FinalizeVirtualKeyboardChanges();
// Bounds update doesn't change vk visibility.
EXPECT_CALL(*delegate(), OnVirtualKeyboardVisibilityChanged).Times(0);
EXPECT_CALL(*delegate(), OnVirtualKeyboardOccludedBoundsChanged(kBounds2));
text_input()->EnsureCaretNotInRect(kBounds2);
}
TEST_F(TextInputTest, SetSurroundingTextSupport) {
testing::NiceMock<TestingInputMethodObserver> observer(GetInputMethod());
constexpr bool kShouldDoLearning = true;
constexpr bool kCanComposeInline = true;
ash::input_method::MockInputMethodManagerImpl* input_method_manager =
ash_test_helper()->input_method_manager();
constexpr char kEnglishId[] =
"_comp_ime_jkghodnilhceideoidjikpgommlajknkxkb:us::eng";
constexpr char kJapaneseId[] =
"_comp_ime_jkghodnilhceideoidjikpgommlajknknacl_mozc_jp";
input_method_manager->SetCurrentInputMethodId(kEnglishId);
EXPECT_CALL(observer, OnTextInputStateChanged(text_input())).Times(1);
EXPECT_CALL(*delegate(), Activated).Times(1);
text_input()->Activate(seat(), surface(),
ui::TextInputClient::FOCUS_REASON_OTHER);
testing::Mock::VerifyAndClearExpectations(&observer);
testing::Mock::VerifyAndClearExpectations(delegate());
// When a client does not support surrounding text, we force a NULL input type
// for xkb input methods like English.
text_input()->SetTypeModeFlags(
ui::TEXT_INPUT_TYPE_SEARCH, ui::TEXT_INPUT_MODE_SEARCH,
ui::TEXT_INPUT_FLAG_NONE, kShouldDoLearning, kCanComposeInline,
/*surrounding_text_supported=*/false);
EXPECT_EQ(ui::TEXT_INPUT_TYPE_NULL, text_input()->GetTextInputType());
// When switching to a non-xkb input method, we restore the original input
// type requested.
EXPECT_CALL(observer, OnTextInputStateChanged(text_input())).Times(1);
input_method_manager->SetCurrentInputMethodId(kJapaneseId);
// The MockInputMethodManagerImpl doesn't currently fire this observer
// callback, so do it explicitly.
text_input()->InputMethodChanged(nullptr, nullptr, false);
testing::Mock::VerifyAndClearExpectations(&observer);
EXPECT_EQ(ui::TEXT_INPUT_TYPE_SEARCH, text_input()->GetTextInputType());
EXPECT_CALL(observer, OnTextInputStateChanged(text_input())).Times(1);
input_method_manager->SetCurrentInputMethodId(kEnglishId);
text_input()->InputMethodChanged(nullptr, nullptr, false);
testing::Mock::VerifyAndClearExpectations(&observer);
EXPECT_EQ(ui::TEXT_INPUT_TYPE_NULL, text_input()->GetTextInputType());
// When surrounding text is supported, the requested input type is left
// untouched.
EXPECT_CALL(observer, OnTextInputStateChanged(text_input())).Times(1);
text_input()->SetTypeModeFlags(
ui::TEXT_INPUT_TYPE_TEXT, ui::TEXT_INPUT_MODE_TEXT,
ui::TEXT_INPUT_FLAG_NONE, kShouldDoLearning, kCanComposeInline,
/*surrounding_text_supported=*/true);
testing::Mock::VerifyAndClearExpectations(&observer);
EXPECT_EQ(ui::TEXT_INPUT_TYPE_TEXT, text_input()->GetTextInputType());
EXPECT_CALL(*delegate(), Deactivated).Times(1);
}
} // anonymous namespace
} // namespace exo
|