key_accessibility_enabler_unittest.cc 2.9 KB

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