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
|
// 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 <memory>
#include "ash/keyboard/ui/keyboard_ui.h"
#include "ash/keyboard/ui/keyboard_ui_controller.h"
#include "ash/keyboard/ui/keyboard_util.h"
#include "ash/keyboard/ui/test/keyboard_test_util.h"
#include "ash/keyboard/ui/test/test_keyboard_controller_observer.h"
#include "ash/keyboard/ui/test/test_keyboard_layout_delegate.h"
#include "ash/keyboard/ui/test/test_keyboard_ui_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/base/ime/dummy_input_method.h"
namespace keyboard {
namespace {
class KeyboardUtilTest : public aura::test::AuraTestBase {
public:
KeyboardUtilTest() = default;
KeyboardUtilTest(const KeyboardUtilTest&) = delete;
KeyboardUtilTest& operator=(const KeyboardUtilTest&) = delete;
~KeyboardUtilTest() override = default;
// Sets all flags controlling whether the keyboard should be shown to
// their disabled state.
void DisableAllFlags() {
ResetAllFlags();
keyboard::SetAccessibilityKeyboardEnabled(false);
keyboard::SetTouchKeyboardEnabled(false);
SetEnableFlag(KeyboardEnableFlag::kPolicyDisabled);
SetEnableFlag(KeyboardEnableFlag::kExtensionDisabled);
}
// Sets all flags controlling whether the keyboard should be shown to
// their enabled flag.
void EnableAllFlags() {
ResetAllFlags();
keyboard::SetAccessibilityKeyboardEnabled(true);
keyboard::SetTouchKeyboardEnabled(true);
SetEnableFlag(KeyboardEnableFlag::kPolicyEnabled);
SetEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
}
// Sets all flags controlling whether the keyboard should be shown to
// their neutral flag.
void ResetAllFlags() {
keyboard::SetAccessibilityKeyboardEnabled(false);
keyboard::SetTouchKeyboardEnabled(false);
ClearEnableFlag(KeyboardEnableFlag::kPolicyDisabled);
ClearEnableFlag(KeyboardEnableFlag::kExtensionDisabled);
ClearEnableFlag(KeyboardEnableFlag::kPolicyEnabled);
ClearEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
}
void SetUp() override {
aura::test::AuraTestBase::SetUp();
layout_delegate_ =
std::make_unique<TestKeyboardLayoutDelegate>(root_window());
keyboard_ui_controller_.Initialize(
std::make_unique<TestKeyboardUIFactory>(&input_method_),
layout_delegate_.get());
ResetAllFlags();
}
void TearDown() override {
ResetAllFlags();
aura::test::AuraTestBase::TearDown();
}
protected:
void SetEnableFlag(KeyboardEnableFlag flag) {
keyboard_ui_controller_.SetEnableFlag(flag);
}
void ClearEnableFlag(KeyboardEnableFlag flag) {
keyboard_ui_controller_.ClearEnableFlag(flag);
}
// Used indirectly by keyboard utils.
KeyboardUIController keyboard_ui_controller_;
ui::DummyInputMethod input_method_;
std::unique_ptr<TestKeyboardLayoutDelegate> layout_delegate_;
};
} // namespace
// Tests that we respect the accessibility setting.
TEST_F(KeyboardUtilTest, AlwaysShowIfA11yEnabled) {
// Disabled by default.
EXPECT_FALSE(IsKeyboardEnabled());
// If enabled by accessibility, should ignore other flag values.
DisableAllFlags();
SetAccessibilityKeyboardEnabled(true);
EXPECT_TRUE(IsKeyboardEnabled());
}
// Tests that we respect the policy setting.
TEST_F(KeyboardUtilTest, AlwaysShowIfPolicyEnabled) {
EXPECT_FALSE(IsKeyboardEnabled());
// If policy is enabled, should ignore other flag values.
DisableAllFlags();
SetEnableFlag(KeyboardEnableFlag::kPolicyEnabled);
EXPECT_TRUE(IsKeyboardEnabled());
}
// Tests that we respect the policy setting.
TEST_F(KeyboardUtilTest, HidesIfPolicyDisabled) {
EXPECT_FALSE(IsKeyboardEnabled());
EnableAllFlags();
// Set accessibility to neutral since accessibility has higher precedence.
SetAccessibilityKeyboardEnabled(false);
EXPECT_TRUE(IsKeyboardEnabled());
// Disable policy. Keyboard should be disabled.
SetEnableFlag(KeyboardEnableFlag::kPolicyDisabled);
EXPECT_FALSE(IsKeyboardEnabled());
}
// Tests that the keyboard shows when requested flag provided higher priority
// flags have not been set.
TEST_F(KeyboardUtilTest, ShowKeyboardWhenRequested) {
DisableAllFlags();
// Remove device policy, which has higher precedence than us.
ClearEnableFlag(KeyboardEnableFlag::kPolicyDisabled);
EXPECT_FALSE(IsKeyboardEnabled());
// Requested should have higher precedence than all the remaining flags.
SetEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
EXPECT_TRUE(IsKeyboardEnabled());
}
// Tests that the touch keyboard is hidden when requested flag is disabled and
// higher priority flags have not been set.
TEST_F(KeyboardUtilTest, HideKeyboardWhenRequested) {
EnableAllFlags();
// Remove higher precedence flags.
ClearEnableFlag(KeyboardEnableFlag::kPolicyEnabled);
SetAccessibilityKeyboardEnabled(false);
EXPECT_TRUE(IsKeyboardEnabled());
// Set requested flag to disable. Keyboard should disable.
SetEnableFlag(KeyboardEnableFlag::kExtensionDisabled);
EXPECT_FALSE(IsKeyboardEnabled());
}
// SetTouchKeyboardEnabled has the lowest priority, but should still work when
// none of the other flags are enabled.
TEST_F(KeyboardUtilTest, HideKeyboardWhenTouchEnabled) {
ResetAllFlags();
EXPECT_FALSE(IsKeyboardEnabled());
SetTouchKeyboardEnabled(true);
EXPECT_TRUE(IsKeyboardEnabled());
}
TEST_F(KeyboardUtilTest, UpdateKeyboardConfig) {
ResetAllFlags();
KeyboardConfig config = keyboard_ui_controller_.keyboard_config();
EXPECT_TRUE(config.spell_check);
EXPECT_FALSE(keyboard_ui_controller_.UpdateKeyboardConfig(config));
config.spell_check = false;
EXPECT_TRUE(keyboard_ui_controller_.UpdateKeyboardConfig(config));
EXPECT_FALSE(keyboard_ui_controller_.keyboard_config().spell_check);
EXPECT_FALSE(keyboard_ui_controller_.UpdateKeyboardConfig(config));
}
TEST_F(KeyboardUtilTest, IsOverscrollEnabled) {
ResetAllFlags();
// Return false when keyboard is disabled.
EXPECT_FALSE(keyboard_ui_controller_.IsKeyboardOverscrollEnabled());
// Enable the virtual keyboard.
SetTouchKeyboardEnabled(true);
EXPECT_TRUE(keyboard_ui_controller_.IsKeyboardOverscrollEnabled());
// Set overscroll enabled flag.
KeyboardConfig config = keyboard_ui_controller_.keyboard_config();
config.overscroll_behavior = KeyboardOverscrollBehavior::kDisabled;
keyboard_ui_controller_.UpdateKeyboardConfig(config);
EXPECT_FALSE(keyboard_ui_controller_.IsKeyboardOverscrollEnabled());
// Set default overscroll flag.
config.overscroll_behavior = KeyboardOverscrollBehavior::kDefault;
keyboard_ui_controller_.UpdateKeyboardConfig(config);
EXPECT_TRUE(keyboard_ui_controller_.IsKeyboardOverscrollEnabled());
// Set keyboard_locked() to true.
keyboard_ui_controller_.set_keyboard_locked(true);
EXPECT_TRUE(keyboard_ui_controller_.keyboard_locked());
EXPECT_FALSE(keyboard_ui_controller_.IsKeyboardOverscrollEnabled());
}
// See https://crbug.com/946358.
TEST_F(KeyboardUtilTest, RebuildsWhenChangingAccessibilityFlag) {
// Virtual keyboard enabled with compact layout.
SetTouchKeyboardEnabled(true);
TestKeyboardControllerObserver observer;
keyboard_ui_controller_.AddObserver(&observer);
// Virtual keyboard should rebuild to switch to a11y layout.
SetAccessibilityKeyboardEnabled(true);
EXPECT_EQ(1, observer.disabled_count);
EXPECT_EQ(1, observer.enabled_count);
// Virtual keyboard should rebuild to switch back to compact layout.
SetAccessibilityKeyboardEnabled(false);
EXPECT_EQ(2, observer.disabled_count);
EXPECT_EQ(2, observer.enabled_count);
keyboard_ui_controller_.RemoveObserver(&observer);
}
} // namespace keyboard
|