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
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/accelerators/accelerator.h"
#include <memory>
#include <string_view>
#include "ash/accelerators/accelerator_controller_impl.h"
#include "ash/app_list/test/app_list_test_helper.h"
#include "ash/assistant/model/assistant_ui_model.h"
#include "ash/public/cpp/assistant/controller/assistant_ui_controller.h"
#include "ash/public/cpp/test/assistant_test_api.h"
#include "ash/shell.h"
#include "ash/shell_observer.h"
#include "ash/system/network/network_observer.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/overview/overview_observer.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "base/run_loop.h"
#include "base/test/metrics/user_action_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "chromeos/ash/components/dbus/shill/shill_clients.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/services/assistant/public/cpp/features.h"
#include "chromeos/ash/services/assistant/test_support/scoped_assistant_browser_delegate.h"
#include "ui/aura/window.h"
#include "ui/base/accelerators/test_accelerator_target.h"
#include "ui/events/event.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/events/test/event_generator.h"
namespace ash {
namespace {
constexpr std::string_view kNoAssistantForNewEntryPoint =
"Assistant is not available if new entry point is enabled. "
"crbug.com/388361414";
// A network observer to watch for the toggle wifi events.
class TestNetworkObserver : public NetworkObserver {
public:
TestNetworkObserver() = default;
TestNetworkObserver(const TestNetworkObserver&) = delete;
TestNetworkObserver& operator=(const TestNetworkObserver&) = delete;
~TestNetworkObserver() override = default;
// ash::NetworkObserver:
void RequestToggleWifi() override {
wifi_enabled_status_ = !wifi_enabled_status_;
}
bool wifi_enabled_status() const { return wifi_enabled_status_; }
private:
bool wifi_enabled_status_ = false;
};
} // namespace
////////////////////////////////////////////////////////////////////////////////
// This is intended to test few samples from each category of accelerators to
// make sure they work properly.
// This is to catch any future regressions (crbug.com/469235).
class AcceleratorTest : public AshTestBase, public OverviewObserver {
public:
AcceleratorTest() : is_in_overview_mode_(false) {}
AcceleratorTest(const AcceleratorTest&) = delete;
AcceleratorTest& operator=(const AcceleratorTest&) = delete;
void SetUp() override {
AshTestBase::SetUp();
Shell::Get()->overview_controller()->AddObserver(this);
}
void TearDown() override {
Shell::Get()->overview_controller()->RemoveObserver(this);
AshTestBase::TearDown();
}
// Sends a key press event and waits synchronously until it's completely
// processed.
void SendKeyPressSync(ui::KeyboardCode key,
bool control,
bool shift,
bool alt) {
ui::test::EmulateFullKeyPressReleaseSequence(
GetEventGenerator(), key, control, shift, alt, /*command=*/false);
}
// OverviewObserver:
void OnOverviewModeStarting() override { is_in_overview_mode_ = true; }
void OnOverviewModeEnded() override { is_in_overview_mode_ = false; }
protected:
bool is_in_overview_mode_;
};
////////////////////////////////////////////////////////////////////////////////
// Tests a sample of accelerators.
TEST_F(AcceleratorTest, Basic) {
// Test VOLUME_MUTE.
base::UserActionTester user_action_tester;
EXPECT_EQ(0, user_action_tester.GetActionCount("Accel_VolumeMute_F8"));
SendKeyPressSync(ui::VKEY_VOLUME_MUTE, false, false, false);
EXPECT_EQ(1, user_action_tester.GetActionCount("Accel_VolumeMute_F8"));
// Test VOLUME_DOWN.
EXPECT_EQ(0, user_action_tester.GetActionCount("Accel_VolumeDown_F9"));
SendKeyPressSync(ui::VKEY_VOLUME_DOWN, false, false, false);
EXPECT_EQ(1, user_action_tester.GetActionCount("Accel_VolumeDown_F9"));
// Test VOLUME_UP.
EXPECT_EQ(0, user_action_tester.GetActionCount("Accel_VolumeUp_F10"));
SendKeyPressSync(ui::VKEY_VOLUME_UP, false, false, false);
EXPECT_EQ(1, user_action_tester.GetActionCount("Accel_VolumeUp_F10"));
// Test TOGGLE_WIFI.
TestNetworkObserver network_observer;
Shell::Get()->system_tray_notifier()->AddNetworkObserver(&network_observer);
EXPECT_FALSE(network_observer.wifi_enabled_status());
SendKeyPressSync(ui::VKEY_WLAN, false, false, false);
EXPECT_TRUE(network_observer.wifi_enabled_status());
SendKeyPressSync(ui::VKEY_WLAN, false, false, false);
EXPECT_FALSE(network_observer.wifi_enabled_status());
Shell::Get()->system_tray_notifier()->RemoveNetworkObserver(
&network_observer);
}
// Tests a sample of the non-repeatable accelerators that need windows to be
// enabled.
TEST_F(AcceleratorTest, NonRepeatableNeedingWindowActions) {
// Create a bunch of windows to work with.
aura::Window* window_1 =
CreateTestWindowInShellWithBounds(gfx::Rect(0, 0, 100, 100));
aura::Window* window_2 =
CreateTestWindowInShellWithBounds(gfx::Rect(0, 0, 100, 100));
window_1->Show();
wm::ActivateWindow(window_1);
window_2->Show();
wm::ActivateWindow(window_2);
// Test TOGGLE_OVERVIEW.
EXPECT_FALSE(is_in_overview_mode_);
SendKeyPressSync(ui::VKEY_MEDIA_LAUNCH_APP1, false, false, false);
EXPECT_TRUE(is_in_overview_mode_);
SendKeyPressSync(ui::VKEY_MEDIA_LAUNCH_APP1, false, false, false);
EXPECT_FALSE(is_in_overview_mode_);
// Test CYCLE_FORWARD_MRU and CYCLE_BACKWARD_MRU.
wm::ActivateWindow(window_1);
EXPECT_TRUE(wm::IsActiveWindow(window_1));
EXPECT_FALSE(wm::IsActiveWindow(window_2));
SendKeyPressSync(ui::VKEY_TAB, false, false, true); // CYCLE_FORWARD_MRU.
EXPECT_TRUE(wm::IsActiveWindow(window_2));
EXPECT_FALSE(wm::IsActiveWindow(window_1));
SendKeyPressSync(ui::VKEY_TAB, false, true, true); // CYCLE_BACKWARD_MRU.
EXPECT_TRUE(wm::IsActiveWindow(window_1));
EXPECT_FALSE(wm::IsActiveWindow(window_2));
// Test TOGGLE_FULLSCREEN.
WindowState* active_window_state = WindowState::ForActiveWindow();
EXPECT_FALSE(active_window_state->IsFullscreen());
SendKeyPressSync(ui::VKEY_ZOOM, false, false, false);
EXPECT_TRUE(active_window_state->IsFullscreen());
SendKeyPressSync(ui::VKEY_ZOOM, false, false, false);
EXPECT_FALSE(active_window_state->IsFullscreen());
}
// Tests the app list accelerator.
TEST_F(AcceleratorTest, ToggleAppList) {
GetAppListTestHelper()->CheckVisibility(false);
SendKeyPressSync(ui::VKEY_LWIN, false, false, false);
base::RunLoop().RunUntilIdle();
GetAppListTestHelper()->CheckVisibility(true);
SendKeyPressSync(ui::VKEY_LWIN, false, false, false);
base::RunLoop().RunUntilIdle();
GetAppListTestHelper()->CheckVisibility(false);
}
TEST_F(AcceleratorTest, SearchPlusAWithNewEntryPointDisabled) {
if (ash::assistant::features::IsNewEntryPointEnabled()) {
GTEST_SKIP() << kNoAssistantForNewEntryPoint;
}
base::UserActionTester user_action_tester;
std::unique_ptr<AssistantTestApi> test_api = AssistantTestApi::Create();
test_api->EnableAssistantAndWait();
ui::test::EmulateFullKeyPressReleaseSequence(
GetEventGenerator(), ui::VKEY_A,
/*control=*/false, /*shift=*/false, /*alt=*/false, /*command=*/true);
AssistantUiController* ui_controller = AssistantUiController::Get();
CHECK(ui_controller);
EXPECT_EQ(AssistantVisibility::kVisible,
ui_controller->GetModel()->visibility());
EXPECT_EQ(1, user_action_tester.GetActionCount(
"VoiceInteraction.Started.Search_A"));
}
TEST_F(AcceleratorTest, AssistantKeyWithNewEntryPointDisabled) {
if (ash::assistant::features::IsNewEntryPointEnabled()) {
GTEST_SKIP() << kNoAssistantForNewEntryPoint;
}
base::UserActionTester user_action_tester;
std::unique_ptr<AssistantTestApi> test_api = AssistantTestApi::Create();
test_api->EnableAssistantAndWait();
ui::test::EmulateFullKeyPressReleaseSequence(
GetEventGenerator(), ui::VKEY_ASSISTANT,
/*control=*/false, /*shift=*/false, /*alt=*/false, /*command=*/false);
AssistantUiController* ui_controller = AssistantUiController::Get();
CHECK(ui_controller);
EXPECT_EQ(AssistantVisibility::kVisible,
ui_controller->GetModel()->visibility());
EXPECT_EQ(1, user_action_tester.GetActionCount(
"VoiceInteraction.Started.Assistant"));
EXPECT_EQ(0, user_action_tester.GetActionCount(
"Assistant.NewEntryPoint.AssistantKey"));
}
class AcceleratorNewEntryPointTest : public AcceleratorTest {
private:
// Accelerators registration happens at early stage. Test body is late to
// configure a flag.
base::test::ScopedFeatureList scoped_feature_list{
ash::assistant::features::kEnableNewEntryPoint};
};
TEST_F(AcceleratorNewEntryPointTest, AssistantKeyWithNewEntryPointEnabled) {
base::UserActionTester user_action_tester;
base::test::TestFuture<void> open_new_entry_point_future;
assistant::ScopedAssistantBrowserDelegate scoped_assistant_browser_delegate;
scoped_assistant_browser_delegate.SetOpenNewEntryPointClosure(
open_new_entry_point_future.GetCallback());
ui::test::EmulateFullKeyPressReleaseSequence(
GetEventGenerator(), ui::VKEY_ASSISTANT,
/*control=*/false, /*shift=*/false, /*alt=*/false, /*command=*/false);
EXPECT_TRUE(open_new_entry_point_future.Wait());
EXPECT_EQ(0, user_action_tester.GetActionCount(
"VoiceInteraction.Started.Assistant"));
EXPECT_EQ(1, user_action_tester.GetActionCount(
"Assistant.NewEntryPoint.AssistantKey"));
}
TEST_F(AcceleratorNewEntryPointTest, NoSearchPlusAWithNewEntryPointEnabled) {
base::UserActionTester user_action_tester;
base::test::TestFuture<void> open_new_entry_point_future;
assistant::ScopedAssistantBrowserDelegate scoped_assistant_browser_delegate;
scoped_assistant_browser_delegate.SetOpenNewEntryPointClosure(
open_new_entry_point_future.GetCallback());
ui::test::EmulateFullKeyPressReleaseSequence(
GetEventGenerator(), ui::VKEY_A,
/*control=*/false, /*shift=*/false, /*alt=*/false, /*command=*/true);
task_environment()->RunUntilIdle();
EXPECT_FALSE(open_new_entry_point_future.IsReady())
<< "Search+A shortcut won't be available if new entry point flag is on. "
"New entry point won't be expected to be opened.";
EXPECT_EQ(0, user_action_tester.GetActionCount(
"VoiceInteraction.Started.Search_A"));
}
} // namespace ash
|