keyboard_util_unittest.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright (c) 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <memory>
  5. #include "ash/keyboard/ui/keyboard_ui.h"
  6. #include "ash/keyboard/ui/keyboard_ui_controller.h"
  7. #include "ash/keyboard/ui/keyboard_util.h"
  8. #include "ash/keyboard/ui/test/keyboard_test_util.h"
  9. #include "ash/keyboard/ui/test/test_keyboard_controller_observer.h"
  10. #include "ash/keyboard/ui/test/test_keyboard_layout_delegate.h"
  11. #include "ash/keyboard/ui/test/test_keyboard_ui_factory.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. #include "ui/aura/test/aura_test_base.h"
  14. #include "ui/base/ime/dummy_input_method.h"
  15. namespace keyboard {
  16. namespace {
  17. class KeyboardUtilTest : public aura::test::AuraTestBase {
  18. public:
  19. KeyboardUtilTest() = default;
  20. KeyboardUtilTest(const KeyboardUtilTest&) = delete;
  21. KeyboardUtilTest& operator=(const KeyboardUtilTest&) = delete;
  22. ~KeyboardUtilTest() override = default;
  23. // Sets all flags controlling whether the keyboard should be shown to
  24. // their disabled state.
  25. void DisableAllFlags() {
  26. ResetAllFlags();
  27. keyboard::SetAccessibilityKeyboardEnabled(false);
  28. keyboard::SetTouchKeyboardEnabled(false);
  29. SetEnableFlag(KeyboardEnableFlag::kPolicyDisabled);
  30. SetEnableFlag(KeyboardEnableFlag::kExtensionDisabled);
  31. }
  32. // Sets all flags controlling whether the keyboard should be shown to
  33. // their enabled flag.
  34. void EnableAllFlags() {
  35. ResetAllFlags();
  36. keyboard::SetAccessibilityKeyboardEnabled(true);
  37. keyboard::SetTouchKeyboardEnabled(true);
  38. SetEnableFlag(KeyboardEnableFlag::kPolicyEnabled);
  39. SetEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
  40. }
  41. // Sets all flags controlling whether the keyboard should be shown to
  42. // their neutral flag.
  43. void ResetAllFlags() {
  44. keyboard::SetAccessibilityKeyboardEnabled(false);
  45. keyboard::SetTouchKeyboardEnabled(false);
  46. ClearEnableFlag(KeyboardEnableFlag::kPolicyDisabled);
  47. ClearEnableFlag(KeyboardEnableFlag::kExtensionDisabled);
  48. ClearEnableFlag(KeyboardEnableFlag::kPolicyEnabled);
  49. ClearEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
  50. }
  51. void SetUp() override {
  52. aura::test::AuraTestBase::SetUp();
  53. layout_delegate_ =
  54. std::make_unique<TestKeyboardLayoutDelegate>(root_window());
  55. keyboard_ui_controller_.Initialize(
  56. std::make_unique<TestKeyboardUIFactory>(&input_method_),
  57. layout_delegate_.get());
  58. ResetAllFlags();
  59. }
  60. void TearDown() override {
  61. ResetAllFlags();
  62. aura::test::AuraTestBase::TearDown();
  63. }
  64. protected:
  65. void SetEnableFlag(KeyboardEnableFlag flag) {
  66. keyboard_ui_controller_.SetEnableFlag(flag);
  67. }
  68. void ClearEnableFlag(KeyboardEnableFlag flag) {
  69. keyboard_ui_controller_.ClearEnableFlag(flag);
  70. }
  71. // Used indirectly by keyboard utils.
  72. KeyboardUIController keyboard_ui_controller_;
  73. ui::DummyInputMethod input_method_;
  74. std::unique_ptr<TestKeyboardLayoutDelegate> layout_delegate_;
  75. };
  76. } // namespace
  77. // Tests that we respect the accessibility setting.
  78. TEST_F(KeyboardUtilTest, AlwaysShowIfA11yEnabled) {
  79. // Disabled by default.
  80. EXPECT_FALSE(IsKeyboardEnabled());
  81. // If enabled by accessibility, should ignore other flag values.
  82. DisableAllFlags();
  83. SetAccessibilityKeyboardEnabled(true);
  84. EXPECT_TRUE(IsKeyboardEnabled());
  85. }
  86. // Tests that we respect the policy setting.
  87. TEST_F(KeyboardUtilTest, AlwaysShowIfPolicyEnabled) {
  88. EXPECT_FALSE(IsKeyboardEnabled());
  89. // If policy is enabled, should ignore other flag values.
  90. DisableAllFlags();
  91. SetEnableFlag(KeyboardEnableFlag::kPolicyEnabled);
  92. EXPECT_TRUE(IsKeyboardEnabled());
  93. }
  94. // Tests that we respect the policy setting.
  95. TEST_F(KeyboardUtilTest, HidesIfPolicyDisabled) {
  96. EXPECT_FALSE(IsKeyboardEnabled());
  97. EnableAllFlags();
  98. // Set accessibility to neutral since accessibility has higher precedence.
  99. SetAccessibilityKeyboardEnabled(false);
  100. EXPECT_TRUE(IsKeyboardEnabled());
  101. // Disable policy. Keyboard should be disabled.
  102. SetEnableFlag(KeyboardEnableFlag::kPolicyDisabled);
  103. EXPECT_FALSE(IsKeyboardEnabled());
  104. }
  105. // Tests that the keyboard shows when requested flag provided higher priority
  106. // flags have not been set.
  107. TEST_F(KeyboardUtilTest, ShowKeyboardWhenRequested) {
  108. DisableAllFlags();
  109. // Remove device policy, which has higher precedence than us.
  110. ClearEnableFlag(KeyboardEnableFlag::kPolicyDisabled);
  111. EXPECT_FALSE(IsKeyboardEnabled());
  112. // Requested should have higher precedence than all the remaining flags.
  113. SetEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
  114. EXPECT_TRUE(IsKeyboardEnabled());
  115. }
  116. // Tests that the touch keyboard is hidden when requested flag is disabled and
  117. // higher priority flags have not been set.
  118. TEST_F(KeyboardUtilTest, HideKeyboardWhenRequested) {
  119. EnableAllFlags();
  120. // Remove higher precedence flags.
  121. ClearEnableFlag(KeyboardEnableFlag::kPolicyEnabled);
  122. SetAccessibilityKeyboardEnabled(false);
  123. EXPECT_TRUE(IsKeyboardEnabled());
  124. // Set requested flag to disable. Keyboard should disable.
  125. SetEnableFlag(KeyboardEnableFlag::kExtensionDisabled);
  126. EXPECT_FALSE(IsKeyboardEnabled());
  127. }
  128. // SetTouchKeyboardEnabled has the lowest priority, but should still work when
  129. // none of the other flags are enabled.
  130. TEST_F(KeyboardUtilTest, HideKeyboardWhenTouchEnabled) {
  131. ResetAllFlags();
  132. EXPECT_FALSE(IsKeyboardEnabled());
  133. SetTouchKeyboardEnabled(true);
  134. EXPECT_TRUE(IsKeyboardEnabled());
  135. }
  136. TEST_F(KeyboardUtilTest, UpdateKeyboardConfig) {
  137. ResetAllFlags();
  138. KeyboardConfig config = keyboard_ui_controller_.keyboard_config();
  139. EXPECT_TRUE(config.spell_check);
  140. EXPECT_FALSE(keyboard_ui_controller_.UpdateKeyboardConfig(config));
  141. config.spell_check = false;
  142. EXPECT_TRUE(keyboard_ui_controller_.UpdateKeyboardConfig(config));
  143. EXPECT_FALSE(keyboard_ui_controller_.keyboard_config().spell_check);
  144. EXPECT_FALSE(keyboard_ui_controller_.UpdateKeyboardConfig(config));
  145. }
  146. TEST_F(KeyboardUtilTest, IsOverscrollEnabled) {
  147. ResetAllFlags();
  148. // Return false when keyboard is disabled.
  149. EXPECT_FALSE(keyboard_ui_controller_.IsKeyboardOverscrollEnabled());
  150. // Enable the virtual keyboard.
  151. SetTouchKeyboardEnabled(true);
  152. EXPECT_TRUE(keyboard_ui_controller_.IsKeyboardOverscrollEnabled());
  153. // Set overscroll enabled flag.
  154. KeyboardConfig config = keyboard_ui_controller_.keyboard_config();
  155. config.overscroll_behavior = KeyboardOverscrollBehavior::kDisabled;
  156. keyboard_ui_controller_.UpdateKeyboardConfig(config);
  157. EXPECT_FALSE(keyboard_ui_controller_.IsKeyboardOverscrollEnabled());
  158. // Set default overscroll flag.
  159. config.overscroll_behavior = KeyboardOverscrollBehavior::kDefault;
  160. keyboard_ui_controller_.UpdateKeyboardConfig(config);
  161. EXPECT_TRUE(keyboard_ui_controller_.IsKeyboardOverscrollEnabled());
  162. // Set keyboard_locked() to true.
  163. keyboard_ui_controller_.set_keyboard_locked(true);
  164. EXPECT_TRUE(keyboard_ui_controller_.keyboard_locked());
  165. EXPECT_FALSE(keyboard_ui_controller_.IsKeyboardOverscrollEnabled());
  166. }
  167. // See https://crbug.com/946358.
  168. TEST_F(KeyboardUtilTest, RebuildsWhenChangingAccessibilityFlag) {
  169. // Virtual keyboard enabled with compact layout.
  170. SetTouchKeyboardEnabled(true);
  171. TestKeyboardControllerObserver observer;
  172. keyboard_ui_controller_.AddObserver(&observer);
  173. // Virtual keyboard should rebuild to switch to a11y layout.
  174. SetAccessibilityKeyboardEnabled(true);
  175. EXPECT_EQ(1, observer.disabled_count);
  176. EXPECT_EQ(1, observer.enabled_count);
  177. // Virtual keyboard should rebuild to switch back to compact layout.
  178. SetAccessibilityKeyboardEnabled(false);
  179. EXPECT_EQ(2, observer.disabled_count);
  180. EXPECT_EQ(2, observer.enabled_count);
  181. keyboard_ui_controller_.RemoveObserver(&observer);
  182. }
  183. } // namespace keyboard