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
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/arc/input_overlay/arc_input_overlay_manager.h"
#include <cstdint>
#include <memory>
#include <vector>
#include "ash/public/cpp/arc_game_controls_flag.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/tablet_mode/tablet_mode_controller_test_api.h"
#include "ash/wm/window_util.h"
#include "chrome/browser/ash/app_list/arc/arc_app_test.h"
#include "chrome/browser/ash/arc/input_overlay/actions/action.h"
#include "chrome/browser/ash/arc/input_overlay/arc_input_overlay_metrics.h"
#include "chrome/browser/ash/arc/input_overlay/display_overlay_controller.h"
#include "chrome/browser/ash/arc/input_overlay/test/arc_test_window.h"
#include "chrome/browser/ash/arc/input_overlay/test/event_capturer.h"
#include "chrome/browser/ash/arc/input_overlay/test/test_utils.h"
#include "chrome/browser/ash/arc/input_overlay/util.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/experiences/arc/test/fake_compatibility_mode_instance.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/test/browser_task_environment.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/window.h"
#include "ui/base/ime/dummy_text_input_client.h"
#include "ui/base/ime/init/input_method_factory.h"
#include "ui/gfx/geometry/test/geometry_util.h"
#include "ui/views/widget/widget.h"
namespace arc::input_overlay {
namespace {
// Package names for testing.
constexpr char kRandomPackageName[] =
"org.chromium.arc.testapp.inputoverlay_no_data";
constexpr char kRandomGamePackageName[] =
"org.chromium.arc.testapp.inputoverlay_game";
constexpr char kGameControlsOptOutPackageName[] =
"org.chromium.arc.testapp.inputoverlay_opt_out";
constexpr const float kTolerance = 0.999f;
constexpr const float kAction0PositionRatio = 0.5f;
constexpr const float kAction1PositionRatio = 0.9f;
constexpr const gfx::Rect window_bounds = gfx::Rect(10, 10, 100, 100);
// Simulates the feature (if `is_feature` is true) or hint (if `is_feature` is
// false) toggle on `window`. When toggling the feature, it also
// toggles the hint.
void ToggleGameControls(aura::Window* window, bool is_feature) {
const bool toggle_on =
!IsFlagSet(window->GetProperty(ash::kArcGameControlsFlagsKey),
is_feature ? ash::ArcGameControlsFlag::kEnabled
: ash::ArcGameControlsFlag::kHint);
window->SetProperty(
ash::kArcGameControlsFlagsKey,
UpdateFlag(window->GetProperty(ash::kArcGameControlsFlagsKey),
is_feature
? static_cast<ash::ArcGameControlsFlag>(
/*enable_flag=*/ash::ArcGameControlsFlag::kEnabled |
ash::ArcGameControlsFlag::kHint)
: ash::ArcGameControlsFlag::kHint,
toggle_on));
}
// Verifies UKM event entry size of ToggleWithMappingSource is
// `expected_entry_size` and the entry of `index` matches
// `expected_event_values`.
void VerifyToggleWithMappingSourceUkmEvent(
const ukm::TestAutoSetUkmRecorder& ukm_recorder,
size_t expected_entry_size,
size_t index,
std::map<std::string, int64_t> expected_event_values) {
DCHECK_LT(index, expected_entry_size);
const auto ukm_entries = ukm_recorder.GetEntriesByName(
BuildGameControlsUkmEventName(kToggleWithMappingSourceHistogram));
EXPECT_EQ(expected_entry_size, ukm_entries.size());
for (const auto& value_item : expected_event_values) {
ukm::TestAutoSetUkmRecorder::ExpectEntryMetric(
ukm_entries[index], value_item.first, value_item.second);
}
}
} // namespace
class TestArcInputOverlayManager : public ArcInputOverlayManager {
public:
TestArcInputOverlayManager()
: ArcInputOverlayManager(
/*BrowserContext=*/TestingProfile::Builder().Build().get(),
/*ArcBridgeService=*/nullptr) {}
~TestArcInputOverlayManager() override = default;
private:
// ArcInputOverlayManager:
void AddDisplayOverlayController(TouchInjector* touch_injector) override {
DCHECK(registered_top_level_window_);
DCHECK(touch_injector);
if (!registered_top_level_window_ || !touch_injector) {
return;
}
DCHECK(!display_overlay_controller_);
display_overlay_controller_ =
std::make_unique<DisplayOverlayController>(touch_injector);
}
};
// ArcInputOverlayManagerTest needs to run on MainThread when involving with
// profile and mojo connection.
class ArcInputOverlayManagerTest : public ash::AshTestBase {
public:
ArcInputOverlayManagerTest()
: ash::AshTestBase(std::unique_ptr<base::test::TaskEnvironment>(
std::make_unique<content::BrowserTaskEnvironment>(
base::test::TaskEnvironment::TimeSource::MOCK_TIME))) {}
bool IsInputOverlayEnabled(const aura::Window* window) const {
return arc_test_input_overlay_manager_->input_overlay_enabled_windows_
.contains(window);
}
ui::InputMethod* GetInputMethod() {
return arc_test_input_overlay_manager_->input_method_;
}
bool IsTextInputActive() {
return arc_test_input_overlay_manager_->is_text_input_active_;
}
int EnabledWindows() {
return arc_test_input_overlay_manager_->input_overlay_enabled_windows_
.size();
}
TouchInjector* GetTouchInjector(aura::Window* window) {
auto it =
arc_test_input_overlay_manager_->input_overlay_enabled_windows_.find(
window);
if (it !=
arc_test_input_overlay_manager_->input_overlay_enabled_windows_.end()) {
return it->second.get();
}
return nullptr;
}
aura::Window* GetRegisteredWindow() {
return arc_test_input_overlay_manager_->registered_top_level_window_;
}
KeyEventSourceRewriter* GetKeyEventSourceRewriter() {
return arc_test_input_overlay_manager_->key_event_source_rewriter_.get();
}
DisplayOverlayController* GetDisplayOverlayController() {
return arc_test_input_overlay_manager_->display_overlay_controller_.get();
}
bool IsObserving(aura::Window* window) const {
return arc_test_input_overlay_manager_->window_observations_
.IsObservingSource(window);
}
protected:
// ash::AshTestBase:
void SetUp() override {
ash::AshTestBase::SetUp();
arc_test_input_overlay_manager_ =
base::WrapUnique(new TestArcInputOverlayManager());
profile_ = std::make_unique<TestingProfile>();
arc_app_test_.set_wait_compatibility_mode(true);
arc_app_test_.SetUp(profile_.get());
SimulatedAppInstalled(task_environment(), arc_app_test_,
kEnabledPackageName,
/*is_gc_opt_out=*/false,
/*is_game=*/true);
SimulatedAppInstalled(task_environment(), arc_app_test_,
kRandomGamePackageName,
/*is_gc_opt_out=*/false,
/*is_game=*/true);
}
void TearDown() override {
arc_app_test_.TearDown();
profile_.reset();
arc_test_input_overlay_manager_->Shutdown();
arc_test_input_overlay_manager_.reset();
ash::AshTestBase::TearDown();
}
std::unique_ptr<ArcInputOverlayManager> arc_test_input_overlay_manager_;
ArcAppTest arc_app_test_;
private:
std::unique_ptr<TestingProfile> profile_;
};
TEST_F(ArcInputOverlayManagerTest, TestPropertyChangeAndWindowDestroy) {
aura::client::FocusClient* focus_client =
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
// Test app with input overlay data.
auto arc_window = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kEnabledPackageName);
auto* arc_window_ptr = arc_window->GetNativeWindow();
EXPECT_TRUE(IsObserving(arc_window_ptr));
EXPECT_TRUE(IsInputOverlayEnabled(arc_window_ptr));
// Input overlay registers the window after reading the data when the window
// is still focused. In the test, the arc_window is considered as focused now.
EXPECT_TRUE(GetRegisteredWindow());
focus_client->FocusWindow(arc_window_ptr);
EXPECT_TRUE(GetRegisteredWindow());
// Test app with input overlay data when window is destroyed.
arc_window.reset();
EXPECT_FALSE(IsObserving(arc_window_ptr));
EXPECT_FALSE(IsInputOverlayEnabled(arc_window_ptr));
// Test app without input overlay data.
auto arc_window_no_data = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kRandomPackageName);
EXPECT_FALSE(IsObserving(arc_window_ptr));
EXPECT_FALSE(IsInputOverlayEnabled(arc_window_no_data->GetNativeWindow()));
}
TEST_F(ArcInputOverlayManagerTest, TestWindowDestroyNoWait) {
// This test is to check UAF issue reported in crbug.com/1363030.
task_environment()->RunUntilIdle();
auto arc_window = CreateArcWindow(ash::Shell::GetPrimaryRootWindow(),
window_bounds, kEnabledPackageName);
const auto* arc_window_ptr = arc_window->GetNativeWindow();
// Destroy window before finishing I/O reading. The window can't be destroyed
// during ReadDefaultData(), but it can be destroyed before
// ReadCustomizedData() and TouchInjector.RecordMenuStateOnLaunch() would
// catch it.
arc_window.reset();
task_environment()->FastForwardBy(kIORead);
EXPECT_FALSE(IsInputOverlayEnabled(arc_window_ptr));
}
TEST_F(ArcInputOverlayManagerTest, TestInputMethodObsever) {
ASSERT_FALSE(GetInputMethod());
ASSERT_FALSE(IsTextInputActive());
aura::client::FocusClient* focus_client =
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
auto arc_window = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kEnabledPackageName);
focus_client->FocusWindow(arc_window->GetNativeWindow());
ui::InputMethod* input_method = GetInputMethod();
EXPECT_TRUE(GetInputMethod());
input_method->SetFocusedTextInputClient(nullptr);
EXPECT_FALSE(IsTextInputActive());
ui::DummyTextInputClient dummy_text_input_client(ui::TEXT_INPUT_TYPE_TEXT);
input_method->SetFocusedTextInputClient(&dummy_text_input_client);
EXPECT_TRUE(IsTextInputActive());
ui::DummyTextInputClient dummy_text_none_input_client(
ui::TEXT_INPUT_TYPE_NONE);
input_method->SetFocusedTextInputClient(&dummy_text_none_input_client);
EXPECT_FALSE(IsTextInputActive());
input_method->SetFocusedTextInputClient(nullptr);
}
TEST_F(ArcInputOverlayManagerTest, TestWindowFocusChange) {
aura::client::FocusClient* focus_client =
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
auto arc_window = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kEnabledPackageName);
auto arc_window_no_data = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kRandomPackageName);
EXPECT_EQ(1, EnabledWindows());
auto* injector = GetTouchInjector(arc_window->GetNativeWindow());
EXPECT_TRUE(injector);
// The action number should be adjusted with the data in the
// org.chromium.arc.testapp.inputoverlay.json.
EXPECT_EQ(3u, injector->actions().size());
EXPECT_TRUE(!GetRegisteredWindow() && !GetDisplayOverlayController());
focus_client->FocusWindow(arc_window->GetNativeWindow());
EXPECT_EQ(arc_window->GetNativeWindow(), GetRegisteredWindow());
EXPECT_TRUE(GetDisplayOverlayController());
focus_client->FocusWindow(arc_window_no_data->GetNativeWindow());
EXPECT_TRUE(!GetRegisteredWindow() && !GetDisplayOverlayController());
}
// This simulates the test for crash in b/344665489 to test focus change with
// a window without a widget.
TEST_F(ArcInputOverlayManagerTest, TestWindowFocusChangeWithNullWidget) {
auto arc_window = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kEnabledPackageName);
std::unique_ptr<aura::test::TestWindowDelegate> test_window_delegate =
std::make_unique<aura::test::TestWindowDelegate>();
test_window_delegate->set_window_component(HTCAPTION);
std::unique_ptr<aura::Window> window_no_widget(
CreateTestWindowInShellWithDelegateAndType(
test_window_delegate.get(), aura::client::WINDOW_TYPE_NORMAL, 0,
gfx::Rect(100, 100)));
EXPECT_FALSE(views::Widget::GetWidgetForNativeWindow(window_no_widget.get()));
// Focus on the window without widget.
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow())
->FocusWindow(window_no_widget.get());
// Close the focused window.
window_no_widget.reset();
// Focus is updated to `arc_window`.
EXPECT_EQ(arc_window->GetNativeWindow(), GetRegisteredWindow());
}
TEST_F(ArcInputOverlayManagerTest, TestTabletMode) {
// Launch app in tablet mode and switch to desktop mode.
ash::TabletModeControllerTestApi().EnterTabletMode();
auto arc_window = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kEnabledPackageName);
EXPECT_TRUE(IsInputOverlayEnabled(arc_window->GetNativeWindow()));
EXPECT_FALSE(GetRegisteredWindow());
ash::TabletModeControllerTestApi().LeaveTabletMode();
EXPECT_TRUE(GetRegisteredWindow());
arc_window.reset();
// Launch app in desktop mode and switch to tablet mode.
ash::TabletModeControllerTestApi().LeaveTabletMode();
arc_window = CreateArcWindowSyncAndWait(task_environment(),
ash::Shell::GetPrimaryRootWindow(),
window_bounds, kEnabledPackageName);
EXPECT_TRUE(IsInputOverlayEnabled(arc_window->GetNativeWindow()));
EXPECT_TRUE(GetRegisteredWindow());
ash::TabletModeControllerTestApi().EnterTabletMode();
EXPECT_FALSE(GetRegisteredWindow());
}
TEST_F(ArcInputOverlayManagerTest, TestKeyEventSourceRewriterForMultiDisplay) {
aura::client::FocusClient* focus_client =
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
UpdateDisplay("1000x900,1000x900");
aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows();
display::Display display0 = display::Screen::GetScreen()->GetDisplayMatching(
root_windows[0]->GetBoundsInScreen());
display::Display display1 = display::Screen::GetScreen()->GetDisplayMatching(
root_windows[1]->GetBoundsInScreen());
// Test when launching input overlay window on the secondary display, there
// should be `key_event_source_rewriter_` registered on the primary root
// window.
EXPECT_FALSE(GetKeyEventSourceRewriter());
task_environment()->RunUntilIdle();
auto screen_bounds = window_bounds;
const auto& display_bounds = display1.bounds();
screen_bounds.Offset(display_bounds.x(), display_bounds.y());
auto arc_window =
CreateArcWindow(root_windows[1], screen_bounds, kEnabledPackageName);
arc_window->GetNativeWindow()->SetBoundsInScreen(screen_bounds, display1);
// I/O takes time here.
task_environment()->FastForwardBy(kIORead);
// Make sure to dismiss the educational dialog in beforehand.
auto* injector = GetTouchInjector(arc_window->GetNativeWindow());
EXPECT_TRUE(injector);
focus_client->FocusWindow(arc_window->GetNativeWindow());
EXPECT_TRUE(GetKeyEventSourceRewriter());
// Simulate the fact that key events are only sent to primary root window
// when there is no text input focus. Make sure the input overlay window can
// receive simulated touch events on the secondary window.
auto event_generator =
std::make_unique<ui::test::EventGenerator>(root_windows[0]);
test::EventCapturer event_capturer;
root_windows[1]->AddPostTargetHandler(&event_capturer);
event_generator->PressKey(ui::VKEY_A, ui::EF_NONE, /*source_device_id=*/1);
EXPECT_TRUE(event_capturer.key_events().empty());
EXPECT_EQ(1u, event_capturer.touch_events().size());
event_generator->ReleaseKey(ui::VKEY_A, ui::EF_NONE, 1);
EXPECT_TRUE(event_capturer.key_events().empty());
EXPECT_EQ(2u, event_capturer.touch_events().size());
event_capturer.Clear();
root_windows[1]->RemovePostTargetHandler(&event_capturer);
// Move to the primary display.
arc_window->GetNativeWindow()->SetBoundsInScreen(window_bounds, display0);
EXPECT_FALSE(GetKeyEventSourceRewriter());
// Move back to the secondary display.
arc_window->GetNativeWindow()->SetBoundsInScreen(screen_bounds, display1);
EXPECT_TRUE(GetKeyEventSourceRewriter());
arc_window.reset();
// Test when launching input overlay window on the primary display, there
// shouldn't be `key_event_source_rewriter_`.
EXPECT_FALSE(GetKeyEventSourceRewriter());
arc_window = CreateArcWindowSyncAndWait(task_environment(),
ash::Shell::GetPrimaryRootWindow(),
window_bounds, kEnabledPackageName);
EXPECT_FALSE(GetKeyEventSourceRewriter());
// Move to the secondary display.
arc_window->GetNativeWindow()->SetBoundsInScreen(window_bounds, display1);
EXPECT_TRUE(GetKeyEventSourceRewriter());
// When losing focus, `key_event_source_rewriter_` should be destroyed too.
focus_client->FocusWindow(nullptr);
EXPECT_FALSE(GetKeyEventSourceRewriter());
arc_window.reset();
// Test when this is non input overlay window launched on the secondry
// display, there shouldn't be `key_event_source_rewriter_`.
auto arc_window_no_data = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kRandomPackageName);
focus_client->FocusWindow(arc_window_no_data->GetNativeWindow());
EXPECT_FALSE(GetKeyEventSourceRewriter());
arc_window_no_data.reset();
// Test with no text input focused, when input overlay window on the secondary
// root window is registered/focused, primary window shouldn't receive any key
// events. When input overlay window on the secondary root window is not
// registered/not focused, primary window should receive key events.
root_windows[0]->AddPostTargetHandler(&event_capturer);
arc_window = CreateArcWindowSyncAndWait(task_environment(), root_windows[1],
screen_bounds, kEnabledPackageName);
arc_window->GetNativeWindow()->SetBoundsInScreen(screen_bounds, display1);
arc_window_no_data = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kRandomPackageName);
// Focus on window without input overlay.
focus_client->FocusWindow(arc_window_no_data->GetNativeWindow());
event_generator->PressKey(ui::VKEY_A, ui::EF_NONE, 1 /* keyboard id */);
event_generator->ReleaseKey(ui::VKEY_A, ui::EF_NONE, 1 /* keyboard id */);
EXPECT_EQ(2u, event_capturer.key_events().size());
event_capturer.Clear();
// Focus input overlay window.
focus_client->FocusWindow(arc_window->GetNativeWindow());
EXPECT_TRUE(GetKeyEventSourceRewriter());
event_generator->PressKey(ui::VKEY_A, ui::EF_NONE, 1 /* keyboard id */);
event_generator->ReleaseKey(ui::VKEY_A, ui::EF_NONE, 1 /* keyboard id */);
EXPECT_TRUE(event_capturer.key_events().empty());
event_capturer.Clear();
root_windows[0]->RemovePostTargetHandler(&event_capturer);
}
TEST_F(ArcInputOverlayManagerTest, TestWindowBoundsChanged) {
auto* focus_client =
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
auto arc_window = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kEnabledPackageName);
// Make sure to dismiss the educational dialog in beforehand.
auto* injector = GetTouchInjector(arc_window->GetNativeWindow());
DCHECK(injector);
focus_client->FocusWindow(arc_window->GetNativeWindow());
int caption_height = -arc_window->non_client_view()
->frame_view()
->GetWindowBoundsForClientBounds(gfx::Rect())
.y();
auto expected_content_bounds = gfx::RectF(
window_bounds.x(), window_bounds.y() + caption_height,
window_bounds.width(), window_bounds.height() - caption_height);
EXPECT_EQ(injector->content_bounds_f(), expected_content_bounds);
EXPECT_POINTF_NEAR(
injector->actions()[0]->touch_down_positions()[0],
gfx::PointF(expected_content_bounds.x() +
expected_content_bounds.width() * kAction0PositionRatio,
expected_content_bounds.y() +
expected_content_bounds.height() * kAction0PositionRatio),
kTolerance);
EXPECT_POINTF_NEAR(
injector->actions()[1]->touch_down_positions()[0],
gfx::PointF(expected_content_bounds.x() +
expected_content_bounds.width() * kAction1PositionRatio,
expected_content_bounds.y() +
expected_content_bounds.height() * kAction1PositionRatio),
kTolerance);
// Confirm the content bounds and touch down positions are updated after
// window bounds changed.
auto display = display::Screen::GetScreen()->GetDisplayMatching(
ash::Shell::GetPrimaryRootWindow()->GetBoundsInScreen());
auto new_window_bounds = gfx::Rect(10, 10, 150, 150);
arc_window->GetNativeWindow()->SetBoundsInScreen(new_window_bounds, display);
expected_content_bounds = gfx::RectF(
new_window_bounds.x(), new_window_bounds.y() + caption_height,
new_window_bounds.width(), new_window_bounds.height() - caption_height);
EXPECT_EQ(injector->content_bounds_f(), expected_content_bounds);
EXPECT_POINTF_NEAR(
injector->actions()[0]->touch_down_positions()[0],
gfx::PointF(expected_content_bounds.x() +
expected_content_bounds.width() * kAction0PositionRatio,
expected_content_bounds.y() +
expected_content_bounds.height() * kAction0PositionRatio),
kTolerance);
EXPECT_POINTF_NEAR(
injector->actions()[1]->touch_down_positions()[0],
gfx::PointF(expected_content_bounds.x() +
expected_content_bounds.width() * kAction1PositionRatio,
expected_content_bounds.y() +
expected_content_bounds.height() * kAction1PositionRatio),
kTolerance);
}
TEST_F(ArcInputOverlayManagerTest, TestDisplayRotationChanged) {
aura::client::FocusClient* focus_client =
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
auto arc_window = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kEnabledPackageName);
// Make sure to dismiss the educational dialog in beforehand.
auto* injector = GetTouchInjector(arc_window->GetNativeWindow());
DCHECK(injector);
focus_client->FocusWindow(arc_window->GetNativeWindow());
EXPECT_FALSE(injector->rotation_transform());
int caption_height = -arc_window->non_client_view()
->frame_view()
->GetWindowBoundsForClientBounds(gfx::Rect())
.y();
auto expected_content_bounds = gfx::RectF(
window_bounds.x(), window_bounds.y() + caption_height,
window_bounds.width(), window_bounds.height() - caption_height);
EXPECT_EQ(injector->content_bounds_f(), expected_content_bounds);
auto expect_touch_a =
gfx::PointF(expected_content_bounds.x() +
expected_content_bounds.width() * kAction0PositionRatio,
expected_content_bounds.y() +
expected_content_bounds.height() * kAction0PositionRatio);
EXPECT_POINTF_NEAR(injector->actions()[0]->touch_down_positions()[0],
expect_touch_a, kTolerance);
auto expect_touch_b =
gfx::PointF(expected_content_bounds.x() +
expected_content_bounds.width() * kAction1PositionRatio,
expected_content_bounds.y() +
expected_content_bounds.height() * kAction1PositionRatio);
EXPECT_POINTF_NEAR(injector->actions()[1]->touch_down_positions()[0],
expect_touch_b, kTolerance);
// Confirm the touch down positions are updated after display rotated.
UpdateDisplay("800x600/r");
EXPECT_TRUE(injector->rotation_transform());
EXPECT_EQ(injector->content_bounds_f(), expected_content_bounds);
expect_touch_a = injector->rotation_transform()->MapPoint(expect_touch_a);
EXPECT_POINTF_NEAR(injector->actions()[0]->touch_down_positions()[0],
expect_touch_a, kTolerance);
expect_touch_b = injector->rotation_transform()->MapPoint(expect_touch_b);
EXPECT_POINTF_NEAR(injector->actions()[1]->touch_down_positions()[0],
expect_touch_b, kTolerance);
}
TEST_F(ArcInputOverlayManagerTest, TestNonGameApp) {
// Test a random non-game app.
auto window = CreateArcWindow(ash::Shell::GetPrimaryRootWindow(),
window_bounds, kRandomPackageName);
task_environment()->FastForwardBy(kIORead);
auto* injector = GetTouchInjector(window->GetNativeWindow());
EXPECT_FALSE(injector);
window.reset();
}
TEST_F(ArcInputOverlayManagerTest, TestGameWithDefaultMapping) {
// Test with a game with default mapping.
auto window = CreateArcWindow(ash::Shell::GetPrimaryRootWindow(),
window_bounds, kEnabledPackageName);
task_environment()->FastForwardBy(kIORead);
auto* injector = GetTouchInjector(window->GetNativeWindow());
EXPECT_TRUE(injector);
size_t actions_size = injector->actions().size();
const auto center =
gfx::Point(window_bounds.width() / 2, window_bounds.height() / 2);
// Add two new actions.
injector->AddNewAction(ActionType::TAP, center);
injector->AddNewAction(ActionType::TAP, center);
EXPECT_EQ(actions_size + 2, injector->actions().size());
// Relaunch the game to check whether previous customized data is loaded.
window.reset();
window = CreateArcWindow(ash::Shell::GetPrimaryRootWindow(), window_bounds,
kEnabledPackageName);
task_environment()->FastForwardBy(kIORead);
injector = GetTouchInjector(window->GetNativeWindow());
EXPECT_TRUE(injector);
EXPECT_EQ(actions_size + 2, injector->actions().size());
window.reset();
}
TEST_F(ArcInputOverlayManagerTest, TestGameWithoutDefaultMapping) {
// Test with a random non-O4C game.
auto game_window = CreateArcWindow(ash::Shell::GetPrimaryRootWindow(),
window_bounds, kRandomGamePackageName);
task_environment()->FastForwardBy(kIORead);
auto* injector = GetTouchInjector(game_window->GetNativeWindow());
EXPECT_TRUE(injector);
EXPECT_EQ(0u, injector->actions().size());
// Add two new actions.
const auto center =
gfx::Point(window_bounds.width() / 2, window_bounds.height() / 2);
injector->AddNewAction(ActionType::TAP, center);
injector->AddNewAction(ActionType::TAP, center);
injector->OnBindingSave();
// Relaunch the game to check whether previous customized_data is loaded.
game_window.reset();
task_environment()->RunUntilIdle();
game_window = CreateArcWindow(ash::Shell::GetPrimaryRootWindow(),
window_bounds, kRandomGamePackageName);
task_environment()->FastForwardBy(kIORead);
injector = GetTouchInjector(game_window->GetNativeWindow());
EXPECT_TRUE(injector);
EXPECT_EQ(2u, injector->actions().size());
game_window.reset();
}
TEST_F(ArcInputOverlayManagerTest, TestGameControlsOptOut) {
SimulatedAppInstalled(task_environment(), arc_app_test_,
kGameControlsOptOutPackageName,
/*is_gc_opt_out=*/true,
/*is_game=*/true);
auto game_window =
CreateArcWindow(ash::Shell::GetPrimaryRootWindow(), window_bounds,
kGameControlsOptOutPackageName);
EXPECT_FALSE(GetTouchInjector(game_window->GetNativeWindow()));
}
TEST_F(ArcInputOverlayManagerTest, TestO4CGame) {
// Test an O4C game without any input mapping.
arc_app_test_.compatibility_mode_instance()->set_o4c_pkg(
kRandomGamePackageName);
auto game_window = CreateArcWindow(ash::Shell::GetPrimaryRootWindow(),
window_bounds, kRandomGamePackageName);
task_environment()->FastForwardBy(kIORead);
auto* injector = GetTouchInjector(game_window->GetNativeWindow());
EXPECT_TRUE(injector);
game_window.reset();
task_environment()->RunUntilIdle();
// Test an O4C game with input mappings.
arc_app_test_.compatibility_mode_instance()->set_o4c_pkg(kEnabledPackageName);
game_window = CreateArcWindow(ash::Shell::GetPrimaryRootWindow(),
window_bounds, kEnabledPackageName);
task_environment()->FastForwardBy(kIORead);
injector = GetTouchInjector(game_window->GetNativeWindow());
EXPECT_TRUE(injector);
EXPECT_EQ(3u, injector->actions().size());
game_window.reset();
}
TEST_F(ArcInputOverlayManagerTest, TestOverviewMode) {
auto arc_window_widget = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kEnabledPackageName);
auto* arc_window = arc_window_widget->GetNativeWindow();
EXPECT_EQ(arc_window, GetRegisteredWindow());
EnterOverview();
EXPECT_EQ(nullptr, GetRegisteredWindow());
ExitOverview();
EXPECT_EQ(arc_window, GetRegisteredWindow());
// Test in edit mode.
UpdateFlagAndProperty(arc_window, ash::ArcGameControlsFlag::kEdit,
/*turn_on=*/true);
EnterOverview();
EXPECT_EQ(nullptr, GetRegisteredWindow());
ExitOverview();
EXPECT_EQ(arc_window, GetRegisteredWindow());
}
TEST_F(ArcInputOverlayManagerTest, TestFullscreen) {
auto arc_window_widget = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kEnabledPackageName);
auto* arc_window = arc_window_widget->GetNativeWindow();
EXPECT_EQ(arc_window, GetRegisteredWindow());
// Set it to fullscreen.
arc_window_widget->SetFullscreen(true);
EXPECT_TRUE(arc_window_widget->IsFullscreen());
EXPECT_EQ(arc_window, GetRegisteredWindow());
EXPECT_TRUE(arc_window_widget->IsFullscreen());
// Focus on another random window.
auto* primary_root_window = ash::Shell::GetPrimaryRootWindow();
auto random_window = CreateArcWindowSyncAndWait(
task_environment(), primary_root_window, gfx::Rect(310, 300, 300, 280),
kRandomPackageName);
auto* focus_client = aura::client::GetFocusClient(primary_root_window);
focus_client->FocusWindow(random_window->GetNativeWindow());
EXPECT_EQ(nullptr, GetRegisteredWindow());
// Focus back on the game window.
focus_client->FocusWindow(arc_window);
EXPECT_EQ(arc_window, GetRegisteredWindow());
// Test in edit mode.
UpdateFlagAndProperty(arc_window, ash::ArcGameControlsFlag::kEdit,
/*turn_on=*/true);
focus_client->FocusWindow(random_window->GetNativeWindow());
EXPECT_EQ(nullptr, GetRegisteredWindow());
}
TEST_F(ArcInputOverlayManagerTest, TestFullscreenToFloating) {
auto arc_window_widget = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kEnabledPackageName);
auto* arc_window = arc_window_widget->GetNativeWindow();
EXPECT_EQ(arc_window, GetRegisteredWindow());
// Set it to fullscreen.
arc_window_widget->SetFullscreen(true);
EXPECT_TRUE(arc_window_widget->IsFullscreen());
EXPECT_EQ(arc_window, GetRegisteredWindow());
// Set it to floating.
arc_window_widget->SetZOrderLevel(ui::ZOrderLevel::kFloatingWindow);
EXPECT_EQ(arc_window_widget->GetZOrderLevel(),
ui::ZOrderLevel::kFloatingWindow);
EXPECT_EQ(arc_window, GetRegisteredWindow());
// Set it back to fullscreen.
arc_window_widget->SetFullscreen(true);
EXPECT_TRUE(arc_window_widget->IsFullscreen());
EXPECT_EQ(arc_window, GetRegisteredWindow());
}
TEST_F(ArcInputOverlayManagerTest, TestHistograms) {
base::HistogramTester histograms;
ukm::TestAutoSetUkmRecorder ukm_recorder;
std::map<MappingSource, int> expected_histogram_values_for_hint_on;
std::map<MappingSource, int> expected_histogram_values_for_hint_off;
std::map<MappingSource, int> expected_histogram_values_for_feature_on;
std::map<MappingSource, int> expected_histogram_values_for_feature_off;
const std::string feature_on_histogram_name =
BuildGameControlsHistogramName(
base::JoinString(
std::vector<std::string>{kFeatureHistogramName,
kToggleWithMappingSourceHistogram},
""))
.append(kGameControlsHistogramSeparator)
.append(kToggleOnHistogramName);
const std::string feature_off_histogram_name =
BuildGameControlsHistogramName(
base::JoinString(
std::vector<std::string>{kFeatureHistogramName,
kToggleWithMappingSourceHistogram},
""))
.append(kGameControlsHistogramSeparator)
.append(kToggleOffHistogramName);
const std::string hint_on_histogram_name =
BuildGameControlsHistogramName(
base::JoinString(
std::vector<std::string>{kHintHistogramName,
kToggleWithMappingSourceHistogram},
""))
.append(kGameControlsHistogramSeparator)
.append(kToggleOnHistogramName);
const std::string hint_off_histogram_name =
BuildGameControlsHistogramName(
base::JoinString(
std::vector<std::string>{kHintHistogramName,
kToggleWithMappingSourceHistogram},
""))
.append(kGameControlsHistogramSeparator)
.append(kToggleOffHistogramName);
// 1. Test with the default mapping.
auto arc_window = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kEnabledPackageName);
// Toggle hint off.
ToggleGameControls(arc_window->GetNativeWindow(), /*is_feature=*/false);
MapIncreaseValueByOne(expected_histogram_values_for_hint_off,
MappingSource::kDefault);
VerifyHistogramValues(histograms, hint_off_histogram_name,
expected_histogram_values_for_hint_off);
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/1u, /*index=*/0u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kMappingHint)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/0},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kDefault)}});
// Toggle hint on.
ToggleGameControls(arc_window->GetNativeWindow(), /*is_feature=*/false);
MapIncreaseValueByOne(expected_histogram_values_for_hint_on,
MappingSource::kDefault);
VerifyHistogramValues(histograms, hint_on_histogram_name,
expected_histogram_values_for_hint_on);
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/2u, /*index=*/1u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kMappingHint)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/1},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kDefault)}});
// Toggle feature off.
ToggleGameControls(arc_window->GetNativeWindow(), /*is_feature=*/true);
MapIncreaseValueByOne(expected_histogram_values_for_feature_off,
MappingSource::kDefault);
// Hint is also toggle off with feature toggle off.
MapIncreaseValueByOne(expected_histogram_values_for_hint_off,
MappingSource::kDefault);
VerifyHistogramValues(histograms, feature_off_histogram_name,
expected_histogram_values_for_feature_off);
VerifyHistogramValues(histograms, hint_off_histogram_name,
expected_histogram_values_for_hint_off);
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/4u, /*index=*/2u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kFeature)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/0},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kDefault)}});
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/4u, /*index=*/3u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kMappingHint)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/0},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kDefault)}});
// Toggle feature on.
ToggleGameControls(arc_window->GetNativeWindow(), /*is_feature=*/true);
MapIncreaseValueByOne(expected_histogram_values_for_feature_on,
MappingSource::kDefault);
// Hint is also toggle on with feature toggle on.
MapIncreaseValueByOne(expected_histogram_values_for_hint_on,
MappingSource::kDefault);
VerifyHistogramValues(histograms, feature_on_histogram_name,
expected_histogram_values_for_feature_on);
VerifyHistogramValues(histograms, hint_on_histogram_name,
expected_histogram_values_for_hint_on);
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/6u, /*index=*/4u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kFeature)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/1},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kDefault)}});
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/6u, /*index=*/5u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kMappingHint)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/1},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kDefault)}});
// 2. Add the default mapping with extra user-added mapping.
auto* injector = GetTouchInjector(arc_window->GetNativeWindow());
injector->AddNewAction(ActionType::TAP,
arc_window->GetNativeWindow()->bounds().CenterPoint());
// Toggle hint off.
ToggleGameControls(arc_window->GetNativeWindow(), /*is_feature=*/false);
MapIncreaseValueByOne(expected_histogram_values_for_hint_off,
MappingSource::kDefaultAndUserAdded);
VerifyHistogramValues(histograms, hint_off_histogram_name,
expected_histogram_values_for_hint_off);
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/7u, /*index=*/6u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kMappingHint)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/0},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kDefaultAndUserAdded)}});
// Toggle hint on.
ToggleGameControls(arc_window->GetNativeWindow(), /*is_feature=*/false);
MapIncreaseValueByOne(expected_histogram_values_for_hint_on,
MappingSource::kDefaultAndUserAdded);
VerifyHistogramValues(histograms, hint_on_histogram_name,
expected_histogram_values_for_hint_on);
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/8u, /*index=*/7u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kMappingHint)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/1},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kDefaultAndUserAdded)}});
// Toggle feature off.
ToggleGameControls(arc_window->GetNativeWindow(), /*is_feature=*/true);
MapIncreaseValueByOne(expected_histogram_values_for_feature_off,
MappingSource::kDefaultAndUserAdded);
// Hint is also toggle off with feature toggle off.
MapIncreaseValueByOne(expected_histogram_values_for_hint_off,
MappingSource::kDefaultAndUserAdded);
VerifyHistogramValues(histograms, feature_off_histogram_name,
expected_histogram_values_for_feature_off);
VerifyHistogramValues(histograms, hint_off_histogram_name,
expected_histogram_values_for_hint_off);
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/10u, /*index=*/8u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kFeature)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/0},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kDefaultAndUserAdded)}});
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/10u, /*index=*/9u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kMappingHint)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/0},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kDefaultAndUserAdded)}});
// Toggle feature on.
ToggleGameControls(arc_window->GetNativeWindow(), /*is_feature=*/true);
MapIncreaseValueByOne(expected_histogram_values_for_feature_on,
MappingSource::kDefaultAndUserAdded);
// Hint is also toggle on with feature toggle on.
MapIncreaseValueByOne(expected_histogram_values_for_hint_on,
MappingSource::kDefaultAndUserAdded);
VerifyHistogramValues(histograms, feature_on_histogram_name,
expected_histogram_values_for_feature_on);
VerifyHistogramValues(histograms, hint_on_histogram_name,
expected_histogram_values_for_hint_on);
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/12u, /*index=*/10u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kFeature)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/1},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kDefaultAndUserAdded)}});
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/12u, /*index=*/11u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kMappingHint)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/1},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kDefaultAndUserAdded)}});
// 3. Test with user-added mapping only.
auto game_window = CreateArcWindowSyncAndWait(
task_environment(), ash::Shell::GetPrimaryRootWindow(), window_bounds,
kRandomGamePackageName);
injector = GetTouchInjector(game_window->GetNativeWindow());
injector->AddNewAction(
ActionType::TAP, game_window->GetNativeWindow()->bounds().CenterPoint());
// Toggle hint off.
ToggleGameControls(game_window->GetNativeWindow(), /*is_feature=*/false);
MapIncreaseValueByOne(expected_histogram_values_for_hint_off,
MappingSource::kUserAdded);
VerifyHistogramValues(histograms, hint_off_histogram_name,
expected_histogram_values_for_hint_off);
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/13u, /*index=*/12u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kMappingHint)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/0},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kUserAdded)}});
// Toggle hint on.
ToggleGameControls(game_window->GetNativeWindow(), /*is_feature=*/false);
MapIncreaseValueByOne(expected_histogram_values_for_hint_on,
MappingSource::kUserAdded);
VerifyHistogramValues(histograms, hint_on_histogram_name,
expected_histogram_values_for_hint_on);
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/14u, /*index=*/13u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kMappingHint)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/1},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kUserAdded)}});
// Toggle feature off.
ToggleGameControls(game_window->GetNativeWindow(), /*is_feature=*/true);
MapIncreaseValueByOne(expected_histogram_values_for_feature_off,
MappingSource::kUserAdded);
// Hint is also toggle off with feature toggle off.
MapIncreaseValueByOne(expected_histogram_values_for_hint_off,
MappingSource::kUserAdded);
VerifyHistogramValues(histograms, feature_off_histogram_name,
expected_histogram_values_for_feature_off);
VerifyHistogramValues(histograms, hint_off_histogram_name,
expected_histogram_values_for_hint_off);
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/16u, /*index=*/14u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kFeature)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/0},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kUserAdded)}});
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/16u, /*index=*/15u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kMappingHint)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/0},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kUserAdded)}});
// Toggle feature on.
ToggleGameControls(game_window->GetNativeWindow(), /*is_feature=*/true);
MapIncreaseValueByOne(expected_histogram_values_for_feature_on,
MappingSource::kUserAdded);
// Hint is also toggle on with feature toggle on.
MapIncreaseValueByOne(expected_histogram_values_for_hint_on,
MappingSource::kUserAdded);
VerifyHistogramValues(histograms, feature_on_histogram_name,
expected_histogram_values_for_feature_on);
VerifyHistogramValues(histograms, hint_on_histogram_name,
expected_histogram_values_for_hint_on);
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/18u, /*index=*/16u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kFeature)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/1},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kUserAdded)}});
VerifyToggleWithMappingSourceUkmEvent(
ukm_recorder, /*expected_entry_size=*/18u, /*index=*/17u,
{{ukm::builders::GameControls_ToggleWithMappingSource::kFunctionName,
static_cast<int64_t>(GameControlsToggleFunction::kMappingHint)},
{ukm::builders::GameControls_ToggleWithMappingSource::kToggleOnName,
/*toggle_on=*/1},
{ukm::builders::GameControls_ToggleWithMappingSource::kMappingSourceName,
static_cast<int64_t>(MappingSource::kUserAdded)}});
}
} // namespace arc::input_overlay
|