Home | History | Annotate | Download | only in corewm
      1 // Copyright (c) 2012 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 "ui/views/corewm/input_method_event_filter.h"
      6 
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 #include "ui/aura/client/activation_client.h"
      9 #include "ui/aura/client/aura_constants.h"
     10 #include "ui/aura/root_window.h"
     11 #include "ui/views/corewm/compound_event_filter.h"
     12 #include "ui/aura/test/aura_test_base.h"
     13 #include "ui/aura/test/event_generator.h"
     14 #include "ui/aura/test/test_event_handler.h"
     15 #include "ui/aura/test/test_activation_client.h"
     16 #include "ui/aura/test/test_windows.h"
     17 
     18 #if !defined(OS_WIN) && !defined(USE_X11)
     19 // On platforms except Windows and X11, aura::test::EventGenerator::PressKey
     20 // generates a key event without native_event(), which is not supported by
     21 // ui::MockInputMethod.
     22 #define TestInputMethodKeyEventPropagation \
     23 DISABLED_TestInputMethodKeyEventPropagation
     24 #endif
     25 
     26 namespace views {
     27 namespace corewm {
     28 
     29 typedef aura::test::AuraTestBase InputMethodEventFilterTest;
     30 
     31 TEST_F(InputMethodEventFilterTest, TestInputMethodProperty) {
     32   CompoundEventFilter* root_filter = new CompoundEventFilter;
     33   root_window()->SetEventFilter(root_filter);
     34 
     35   InputMethodEventFilter input_method_event_filter(
     36       root_window()->GetAcceleratedWidget());
     37   root_filter->AddHandler(&input_method_event_filter);
     38 
     39   // Tests if InputMethodEventFilter adds a window property on its
     40   // construction.
     41   EXPECT_TRUE(root_window()->GetProperty(
     42       aura::client::kRootWindowInputMethodKey));
     43 
     44   root_filter->RemoveHandler(&input_method_event_filter);
     45 }
     46 
     47 // Tests if InputMethodEventFilter dispatches a ui::ET_TRANSLATED_KEY_* event to
     48 // the root window.
     49 TEST_F(InputMethodEventFilterTest, TestInputMethodKeyEventPropagation) {
     50   CompoundEventFilter* root_filter = new CompoundEventFilter;
     51   root_window()->SetEventFilter(root_filter);
     52 
     53   // Add the InputMethodEventFilter before the TestEventFilter.
     54   InputMethodEventFilter input_method_event_filter(
     55       root_window()->GetAcceleratedWidget());
     56   root_filter->AddHandler(&input_method_event_filter);
     57 
     58   // Add TestEventFilter to the RootWindow.
     59   aura::test::TestEventHandler test_filter;
     60   root_filter->AddHandler(&test_filter);
     61 
     62   // We need an active window. Otherwise, the root window will not forward a key
     63   // event to event filters.
     64   aura::test::TestWindowDelegate test_delegate;
     65   scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate(
     66       &test_delegate,
     67       -1,
     68       gfx::Rect(),
     69       root_window()));
     70   aura::client::GetActivationClient(root_window())->ActivateWindow(
     71       window.get());
     72 
     73   // Send a fake key event to the root window. InputMethodEventFilter, which is
     74   // automatically set up by AshTestBase, consumes it and sends a new
     75   // ui::ET_TRANSLATED_KEY_* event to the root window, which will be consumed by
     76   // the test event filter.
     77   aura::test::EventGenerator generator(root_window());
     78   EXPECT_EQ(0, test_filter.num_key_events());
     79   generator.PressKey(ui::VKEY_SPACE, 0);
     80   EXPECT_EQ(1, test_filter.num_key_events());
     81   generator.ReleaseKey(ui::VKEY_SPACE, 0);
     82   EXPECT_EQ(2, test_filter.num_key_events());
     83 
     84   root_filter->RemoveHandler(&input_method_event_filter);
     85   root_filter->RemoveHandler(&test_filter);
     86 
     87   // Reset window before |test_delegate| gets deleted.
     88   window.reset();
     89 }
     90 
     91 }  // namespace corewm
     92 }  // namespace views
     93