1 // Copyright 2014 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 "ash/accelerators/magnifier_key_scroller.h"
7 #include "ash/magnifier/magnification_controller.h"
9 #include "ash/test/ash_test_base.h"
10 #include "ash/wm/window_util.h"
11 #include "ui/aura/test/test_window_delegate.h"
12 #include "ui/events/test/event_generator.h"
17 class KeyEventDelegate
: public aura::test::TestWindowDelegate
{
20 ~KeyEventDelegate() override
{}
22 // ui::EventHandler overrides:
23 void OnKeyEvent(ui::KeyEvent
* event
) override
{
24 key_event
.reset(new ui::KeyEvent(
25 event
->type(), event
->key_code(), event
->flags()));
28 const ui::KeyEvent
* event() const { return key_event
.get(); }
29 void reset() { key_event
.reset(); }
32 scoped_ptr
<ui::KeyEvent
> key_event
;
34 DISALLOW_COPY_AND_ASSIGN(KeyEventDelegate
);
39 typedef ash::test::AshTestBase MagnifierKeyScrollerTest
;
41 TEST_F(MagnifierKeyScrollerTest
, Basic
) {
42 KeyEventDelegate delegate
;
43 scoped_ptr
<aura::Window
> window(CreateTestWindowInShellWithDelegate(
46 gfx::Rect(10, 10, 100, 100)));
47 wm::ActivateWindow(window
.get());
49 MagnifierKeyScroller::ScopedEnablerForTest scoped
;
50 Shell
* shell
= Shell::GetInstance();
51 MagnificationController
* controller
= shell
->magnification_controller();
52 controller
->SetEnabled(true);
54 EXPECT_EQ("200,150", controller
->GetWindowPosition().ToString());
55 ui::test::EventGenerator
& generator
= GetEventGenerator();
57 // Click and Release generates the press event upon release.
58 generator
.PressKey(ui::VKEY_DOWN
, ui::EF_SHIFT_DOWN
);
59 EXPECT_EQ("200,150", controller
->GetWindowPosition().ToString());
60 EXPECT_FALSE(delegate
.event());
62 generator
.ReleaseKey(ui::VKEY_DOWN
, 0);
63 EXPECT_EQ("200,150", controller
->GetWindowPosition().ToString());
64 RunAllPendingInMessageLoop();
65 EXPECT_TRUE(delegate
.event());
66 EXPECT_EQ(ui::ET_KEY_PRESSED
, delegate
.event()->type());
69 // Click and hold scrolls the magnifier screen.
70 generator
.PressKey(ui::VKEY_DOWN
, ui::EF_SHIFT_DOWN
);
71 EXPECT_EQ("200,150", controller
->GetWindowPosition().ToString());
72 EXPECT_FALSE(delegate
.event());
74 generator
.PressKey(ui::VKEY_DOWN
, ui::EF_SHIFT_DOWN
);
75 EXPECT_EQ("200,300", controller
->GetWindowPosition().ToString());
76 EXPECT_FALSE(delegate
.event());
78 generator
.ReleaseKey(ui::VKEY_DOWN
, 0);
79 EXPECT_EQ("200,300", controller
->GetWindowPosition().ToString());
80 EXPECT_FALSE(delegate
.event());
82 // Events are passed normally when the magnifier is off.
83 controller
->SetEnabled(false);
85 generator
.PressKey(ui::VKEY_DOWN
, ui::EF_SHIFT_DOWN
);
86 EXPECT_TRUE(delegate
.event());
87 EXPECT_EQ(ui::ET_KEY_PRESSED
, delegate
.event()->type());
90 generator
.ReleaseKey(ui::VKEY_DOWN
, 0);
91 EXPECT_TRUE(delegate
.event());
92 EXPECT_EQ(ui::ET_KEY_RELEASED
, delegate
.event()->type());
95 generator
.PressKey(ui::VKEY_DOWN
, ui::EF_SHIFT_DOWN
);
96 EXPECT_TRUE(delegate
.event());
97 EXPECT_EQ(ui::ET_KEY_PRESSED
, delegate
.event()->type());
100 generator
.PressKey(ui::VKEY_DOWN
, ui::EF_SHIFT_DOWN
);
101 EXPECT_TRUE(delegate
.event());
102 EXPECT_EQ(ui::ET_KEY_PRESSED
, delegate
.event()->type());
105 generator
.ReleaseKey(ui::VKEY_DOWN
, 0);
106 EXPECT_TRUE(delegate
.event());
107 EXPECT_EQ(ui::ET_KEY_RELEASED
, delegate
.event()->type());