accelerator_filter_unittest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/accelerators/accelerator_filter.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/accelerators/accelerator_controller.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/screenshot_delegate.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shell.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shell_window_ids.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/test/ash_test_base.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/window_util.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/test/aura_test_base.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/test/event_generator.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/test/test_windows.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/window.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/rect.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ash {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace test {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DummyScreenshotDelegate : public ScreenshotDelegate {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DummyScreenshotDelegate() : handle_take_screenshot_count_(0) {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~DummyScreenshotDelegate() {}
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from ScreenshotDelegate:
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void HandleTakeScreenshotForAllRootWindows() OVERRIDE {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ++handle_take_screenshot_count_;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void HandleTakePartialScreenshot(
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      aura::Window* window, const gfx::Rect& rect) OVERRIDE {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Do nothing because it's not tested yet.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool CanTakeScreenshot() OVERRIDE {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int handle_take_screenshot_count() const {
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return handle_take_screenshot_count_;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int handle_take_screenshot_count_;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(DummyScreenshotDelegate);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)AcceleratorController* GetController() {
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return Shell::GetInstance()->accelerator_controller();
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef AshTestBase AcceleratorFilterTest;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests if AcceleratorFilter works without a focused window.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(AcceleratorFilterTest, TestFilterWithoutFocus) {
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DummyScreenshotDelegate* delegate = new DummyScreenshotDelegate;
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetController()->SetScreenshotDelegate(
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scoped_ptr<ScreenshotDelegate>(delegate).Pass());
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, delegate->handle_take_screenshot_count());
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // AcceleratorController calls ScreenshotDelegate::HandleTakeScreenshot() when
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // VKEY_PRINT is pressed. See kAcceleratorData[] in accelerator_controller.cc.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  generator.PressKey(ui::VKEY_PRINT, 0);
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(1, delegate->handle_take_screenshot_count());
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  generator.ReleaseKey(ui::VKEY_PRINT, 0);
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(1, delegate->handle_take_screenshot_count());
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests if AcceleratorFilter works as expected with a focused window.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(AcceleratorFilterTest, TestFilterWithFocus) {
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  aura::test::TestWindowDelegate test_delegate;
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate(
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &test_delegate,
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      -1,
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      gfx::Rect(),
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NULL));
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  wm::ActivateWindow(window.get());
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DummyScreenshotDelegate* delegate = new DummyScreenshotDelegate;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetController()->SetScreenshotDelegate(
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scoped_ptr<ScreenshotDelegate>(delegate).Pass());
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, delegate->handle_take_screenshot_count());
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // AcceleratorFilter should ignore the key events since the root window is
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not focused.
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  generator.PressKey(ui::VKEY_PRINT, 0);
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, delegate->handle_take_screenshot_count());
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  generator.ReleaseKey(ui::VKEY_PRINT, 0);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, delegate->handle_take_screenshot_count());
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Reset window before |test_delegate| gets deleted.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  window.reset();
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests if AcceleratorFilter ignores the flag for Caps Lock.
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(AcceleratorFilterTest, TestCapsLockMask) {
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DummyScreenshotDelegate* delegate = new DummyScreenshotDelegate;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetController()->SetScreenshotDelegate(
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scoped_ptr<ScreenshotDelegate>(delegate).Pass());
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, delegate->handle_take_screenshot_count());
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  generator.PressKey(ui::VKEY_PRINT, 0);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(1, delegate->handle_take_screenshot_count());
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  generator.ReleaseKey(ui::VKEY_PRINT, 0);
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(1, delegate->handle_take_screenshot_count());
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check if AcceleratorFilter ignores the mask for Caps Lock. Note that there
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is no ui::EF_ mask for Num Lock.
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  generator.PressKey(ui::VKEY_PRINT, ui::EF_CAPS_LOCK_DOWN);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(2, delegate->handle_take_screenshot_count());
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  generator.ReleaseKey(ui::VKEY_PRINT, ui::EF_CAPS_LOCK_DOWN);
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(2, delegate->handle_take_screenshot_count());
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace test
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace ash
130