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.
5 #include "ui/keyboard/keyboard_util.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 class KeyboardUtilTest
: public testing::Test
{
14 ~KeyboardUtilTest() override
{}
16 // Sets all flags controlling whether the keyboard should be shown to
17 // their disabled state.
18 void DisableAllFlags() {
19 keyboard::SetAccessibilityKeyboardEnabled(false);
20 keyboard::SetTouchKeyboardEnabled(false);
21 keyboard::SetKeyboardShowOverride(
22 keyboard::KEYBOARD_SHOW_OVERRIDE_DISABLED
);
23 keyboard::SetRequestedKeyboardState(keyboard::KEYBOARD_STATE_DISABLED
);
26 // Sets all flags controlling whether the keyboard should be shown to
27 // their enabled state.
28 void EnableAllFlags() {
29 keyboard::SetAccessibilityKeyboardEnabled(true);
30 keyboard::SetTouchKeyboardEnabled(true);
31 keyboard::SetKeyboardShowOverride(keyboard::KEYBOARD_SHOW_OVERRIDE_ENABLED
);
32 keyboard::SetRequestedKeyboardState(keyboard::KEYBOARD_STATE_ENABLED
);
35 // Sets all flags controlling whether the keyboard should be shown to
36 // their neutral state.
37 void ResetAllFlags() {
38 keyboard::SetAccessibilityKeyboardEnabled(false);
39 keyboard::SetTouchKeyboardEnabled(false);
40 keyboard::SetKeyboardShowOverride(keyboard::KEYBOARD_SHOW_OVERRIDE_NONE
);
41 keyboard::SetRequestedKeyboardState(keyboard::KEYBOARD_STATE_AUTO
);
44 void SetUp() override
{ ResetAllFlags(); }
47 DISALLOW_COPY_AND_ASSIGN(KeyboardUtilTest
);
50 // Tests that we respect the accessibility setting.
51 TEST_F(KeyboardUtilTest
, AlwaysShowIfA11yEnabled
) {
52 // Disabled by default.
53 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
54 // If enabled by accessibility, should ignore other flag values.
56 keyboard::SetAccessibilityKeyboardEnabled(true);
57 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
60 // Tests that we respect the policy setting.
61 TEST_F(KeyboardUtilTest
, AlwaysShowIfPolicyEnabled
) {
62 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
63 // If policy is enabled, should ignore other flag values.
65 keyboard::SetKeyboardShowOverride(keyboard::KEYBOARD_SHOW_OVERRIDE_ENABLED
);
66 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
69 // Tests that we respect the policy setting.
70 TEST_F(KeyboardUtilTest
, HidesIfPolicyDisabled
) {
71 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
73 // Set accessibility to neutral since accessibility has higher precedence.
74 keyboard::SetAccessibilityKeyboardEnabled(false);
75 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
76 // Disable policy. Keyboard should be disabled.
77 keyboard::SetKeyboardShowOverride(keyboard::KEYBOARD_SHOW_OVERRIDE_DISABLED
);
78 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
81 // Tests that the keyboard shows when requested state provided higher priority
82 // flags have not been set.
83 TEST_F(KeyboardUtilTest
, ShowKeyboardWhenRequested
) {
85 // Remove device policy, which has higher precedence than us.
86 keyboard::SetKeyboardShowOverride(keyboard::KEYBOARD_SHOW_OVERRIDE_NONE
);
87 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
88 // Requested should have higher precedence than all the remaining flags.
89 keyboard::SetRequestedKeyboardState(keyboard::KEYBOARD_STATE_ENABLED
);
90 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
93 // Tests that the touch keyboard is hidden when requested state is disabled and
94 // higher priority flags have not been set.
95 TEST_F(KeyboardUtilTest
, HideKeyboardWhenRequested
) {
97 // Remove higher precedence flags.
98 keyboard::SetKeyboardShowOverride(keyboard::KEYBOARD_SHOW_OVERRIDE_NONE
);
99 keyboard::SetAccessibilityKeyboardEnabled(false);
100 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
101 // Set requested state to disable. Keyboard should disable.
102 keyboard::SetRequestedKeyboardState(keyboard::KEYBOARD_STATE_DISABLED
);
103 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
106 // SetTouchKeyboardEnabled has the lowest priority, but should still work when
107 // none of the other flags are enabled.
108 TEST_F(KeyboardUtilTest
, HideKeyboardWhenTouchEnabled
) {
110 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
111 keyboard::SetTouchKeyboardEnabled(true);
112 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
115 } // namespace keyboard