1 // Copyright 2018 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
5 #include "ash/accessibility/key_accessibility_enabler.h"
6
7 #include "ash/accessibility/accessibility_controller_impl.h"
8 #include "ash/accessibility/accessibility_observer.h"
9 #include "ash/shell.h"
10 #include "ash/test/ash_test_base.h"
11 #include "ash/wm/tablet_mode/tablet_mode_controller.h"
12 #include "base/run_loop.h"
13 #include "base/test/simple_test_tick_clock.h"
14 #include "ui/events/base_event_utils.h"
15 #include "ui/events/event.h"
16
17 namespace ash {
18
19 class KeyAccessibilityEnablerTest : public AshTestBase,
20 public AccessibilityObserver {
21 public:
KeyAccessibilityEnablerTest()22 KeyAccessibilityEnablerTest() {}
23
SetUp()24 void SetUp() override {
25 ui::SetEventTickClockForTesting(&clock_);
26 AshTestBase::SetUp();
27 Shell::Get()->accessibility_controller()->AddObserver(this);
28 key_accessibility_enabler_ = Shell::Get()->key_accessibility_enabler();
29 }
30
TearDown()31 void TearDown() override {
32 ui::SetEventTickClockForTesting(nullptr);
33 Shell::Get()->accessibility_controller()->RemoveObserver(this);
34 AshTestBase::TearDown();
35 }
36
SendKeyEvent(ui::KeyEvent * event)37 void SendKeyEvent(ui::KeyEvent* event) {
38 // Tablet mode gets exited elsewhere, so we must force it enabled before
39 // each key event.
40 Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
41 key_accessibility_enabler_->OnKeyEvent(event);
42 }
43
WaitForAccessibilityStatusChanged()44 void WaitForAccessibilityStatusChanged() {
45 run_loop_ = std::make_unique<base::RunLoop>();
46 clock_.Advance(base::TimeDelta::FromMilliseconds(5000));
47 run_loop_->Run();
48 }
49
50 private:
51 // AccessibilityObserver:
OnAccessibilityStatusChanged()52 void OnAccessibilityStatusChanged() override { run_loop_->Quit(); }
53
54 std::unique_ptr<base::RunLoop> run_loop_;
55 KeyAccessibilityEnabler* key_accessibility_enabler_;
56 base::SimpleTestTickClock clock_;
57 };
58
TEST_F(KeyAccessibilityEnablerTest,TwoVolumeKeyDown)59 TEST_F(KeyAccessibilityEnablerTest, TwoVolumeKeyDown) {
60 ui::KeyEvent vol_down_press(ui::ET_KEY_PRESSED, ui::VKEY_VOLUME_DOWN,
61 ui::EF_NONE);
62 ui::KeyEvent vol_up_press(ui::ET_KEY_PRESSED, ui::VKEY_VOLUME_UP,
63 ui::EF_NONE);
64 ui::KeyEvent vol_down_release(ui::ET_KEY_RELEASED, ui::VKEY_VOLUME_DOWN,
65 ui::EF_NONE);
66 ui::KeyEvent vol_up_release(ui::ET_KEY_RELEASED, ui::VKEY_VOLUME_UP,
67 ui::EF_NONE);
68
69 AccessibilityControllerImpl* controller =
70 Shell::Get()->accessibility_controller();
71
72 ASSERT_FALSE(controller->spoken_feedback().enabled());
73 SendKeyEvent(&vol_down_press);
74 SendKeyEvent(&vol_up_press);
75 WaitForAccessibilityStatusChanged();
76 ASSERT_TRUE(controller->spoken_feedback().enabled());
77 SendKeyEvent(&vol_down_release);
78 SendKeyEvent(&vol_up_release);
79
80 SendKeyEvent(&vol_down_press);
81 SendKeyEvent(&vol_up_press);
82 WaitForAccessibilityStatusChanged();
83 ASSERT_FALSE(controller->spoken_feedback().enabled());
84 }
85
86 } // namespace ash
87