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

ash / accelerators / magnifier_key_scroller_unittest.cc [blame]

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/accelerators/magnifier_key_scroller.h"

#include <memory>

#include "ash/accessibility/magnifier/fullscreen_magnifier_controller.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/window_util.h"
#include "base/run_loop.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/events/event.h"
#include "ui/events/test/event_generator.h"

namespace ash {
namespace {

class KeyEventDelegate : public aura::test::TestWindowDelegate {
 public:
  KeyEventDelegate() = default;

  KeyEventDelegate(const KeyEventDelegate&) = delete;
  KeyEventDelegate& operator=(const KeyEventDelegate&) = delete;

  ~KeyEventDelegate() override = default;

  // ui::EventHandler overrides:
  void OnKeyEvent(ui::KeyEvent* event) override {
    key_event = std::make_unique<ui::KeyEvent>(event->type(), event->key_code(),
                                               event->flags());
  }

  const ui::KeyEvent* event() const { return key_event.get(); }
  void reset() { key_event.reset(); }

 private:
  std::unique_ptr<ui::KeyEvent> key_event;
};

}  // namespace

using MagnifierKeyScrollerTest = AshTestBase;

TEST_F(MagnifierKeyScrollerTest, Basic) {
  KeyEventDelegate delegate;
  std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate(
      &delegate, 0, gfx::Rect(10, 10, 100, 100)));
  wm::ActivateWindow(window.get());

  MagnifierKeyScroller::ScopedEnablerForTest scoped;
  FullscreenMagnifierController* controller =
      Shell::Get()->fullscreen_magnifier_controller();
  controller->SetEnabled(true);

  EXPECT_EQ("200,150", controller->GetWindowPosition().ToString());
  ui::test::EventGenerator* generator = GetEventGenerator();

  // Click and Release generates the press event upon release.
  generator->PressKey(ui::VKEY_DOWN, ui::EF_SHIFT_DOWN);
  EXPECT_EQ("200,150", controller->GetWindowPosition().ToString());
  EXPECT_FALSE(delegate.event());

  generator->ReleaseKey(ui::VKEY_DOWN, 0);
  EXPECT_EQ("200,150", controller->GetWindowPosition().ToString());
  base::RunLoop().RunUntilIdle();
  ASSERT_TRUE(delegate.event());
  EXPECT_EQ(ui::EventType::kKeyPressed, delegate.event()->type());
  delegate.reset();

  // Click and hold scrolls the magnifier screen.
  generator->PressKey(ui::VKEY_DOWN, ui::EF_SHIFT_DOWN);
  EXPECT_EQ("200,150", controller->GetWindowPosition().ToString());
  EXPECT_FALSE(delegate.event());

  generator->PressKey(ui::VKEY_DOWN, ui::EF_SHIFT_DOWN);
  EXPECT_EQ("200,300", controller->GetWindowPosition().ToString());
  EXPECT_FALSE(delegate.event());

  generator->ReleaseKey(ui::VKEY_DOWN, 0);
  EXPECT_EQ("200,300", controller->GetWindowPosition().ToString());
  EXPECT_FALSE(delegate.event());

  // Events are passed normally when the magnifier is off.
  controller->SetEnabled(false);

  generator->PressKey(ui::VKEY_DOWN, ui::EF_SHIFT_DOWN);
  ASSERT_TRUE(delegate.event());
  EXPECT_EQ(ui::EventType::kKeyPressed, delegate.event()->type());
  delegate.reset();

  generator->ReleaseKey(ui::VKEY_DOWN, 0);
  ASSERT_TRUE(delegate.event());
  EXPECT_EQ(ui::EventType::kKeyReleased, delegate.event()->type());
  delegate.reset();

  generator->PressKey(ui::VKEY_DOWN, ui::EF_SHIFT_DOWN);
  ASSERT_TRUE(delegate.event());
  EXPECT_EQ(ui::EventType::kKeyPressed, delegate.event()->type());
  delegate.reset();

  generator->PressKey(ui::VKEY_DOWN, ui::EF_SHIFT_DOWN);
  ASSERT_TRUE(delegate.event());
  EXPECT_EQ(ui::EventType::kKeyPressed, delegate.event()->type());
  delegate.reset();

  generator->ReleaseKey(ui::VKEY_DOWN, 0);
  ASSERT_TRUE(delegate.event());
  EXPECT_EQ(ui::EventType::kKeyReleased, delegate.event()->type());
  delegate.reset();
}

}  // namespace ash