- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / corewm / capture_controller_unittest.cc
1 // Copyright (c) 2013 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/capture_controller.h"
6
7 #include "base/logging.h"
8 #include "ui/aura/env.h"
9 #include "ui/aura/root_window.h"
10 #include "ui/aura/test/aura_test_base.h"
11 #include "ui/aura/test/event_generator.h"
12 #include "ui/aura/test/test_screen.h"
13 #include "ui/aura/test/test_window_delegate.h"
14 #include "ui/events/event.h"
15 #include "ui/events/event_utils.h"
16 #include "ui/views/test/views_test_base.h"
17 #include "ui/views/view.h"
18 #include "ui/views/widget/root_view.h"
19 #include "ui/views/widget/widget.h"
20
21 #if !defined(OS_CHROMEOS)
22 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
23 #include "ui/views/widget/desktop_aura/desktop_screen_position_client.h"
24 #endif
25
26 namespace views {
27
28 class CaptureControllerTest : public aura::test::AuraTestBase {
29  public:
30   CaptureControllerTest() {}
31
32   virtual void SetUp() OVERRIDE {
33     AuraTestBase::SetUp();
34     capture_controller_.reset(new corewm::ScopedCaptureClient(root_window()));
35
36     second_root_.reset(new aura::RootWindow(
37         aura::RootWindow::CreateParams(gfx::Rect(0, 0, 800, 600))));
38     second_root_->Init();
39     second_root_->Show();
40     second_root_->SetHostSize(gfx::Size(800, 600));
41     second_capture_controller_.reset(
42         new corewm::ScopedCaptureClient(second_root_.get()));
43
44 #if !defined(OS_CHROMEOS)
45     desktop_position_client_.reset(new DesktopScreenPositionClient());
46     aura::client::SetScreenPositionClient(root_window(),
47                                           desktop_position_client_.get());
48
49     second_desktop_position_client_.reset(new DesktopScreenPositionClient());
50     aura::client::SetScreenPositionClient(
51         second_root_.get(),
52         second_desktop_position_client_.get());
53 #endif
54   }
55
56   virtual void TearDown() OVERRIDE {
57     RunAllPendingInMessageLoop();
58
59 #if !defined(OS_CHROMEOS)
60     second_desktop_position_client_.reset();
61 #endif
62     second_capture_controller_.reset();
63
64     // Kill any active compositors before we hit the compositor shutdown paths.
65     second_root_.reset();
66
67 #if !defined(OS_CHROMEOS)
68     desktop_position_client_.reset();
69 #endif
70     capture_controller_.reset();
71
72     AuraTestBase::TearDown();
73   }
74
75   aura::Window* GetCaptureWindow() {
76     return capture_controller_->capture_client()->GetCaptureWindow();
77   }
78
79   aura::Window* GetSecondCaptureWindow() {
80     return second_capture_controller_->capture_client()->GetCaptureWindow();
81   }
82
83   scoped_ptr<corewm::ScopedCaptureClient> capture_controller_;
84   scoped_ptr<aura::RootWindow> second_root_;
85   scoped_ptr<corewm::ScopedCaptureClient> second_capture_controller_;
86 #if !defined(OS_CHROMEOS)
87   scoped_ptr<aura::client::ScreenPositionClient> desktop_position_client_;
88   scoped_ptr<aura::client::ScreenPositionClient>
89       second_desktop_position_client_;
90 #endif
91
92   DISALLOW_COPY_AND_ASSIGN(CaptureControllerTest);
93 };
94
95 // Makes sure that internal details that are set on mouse down (such as
96 // mouse_pressed_handler()) are cleared when another root window takes capture.
97 TEST_F(CaptureControllerTest, ResetMouseEventHandlerOnCapture) {
98   // Create a window inside the RootWindow.
99   scoped_ptr<aura::Window> w1(CreateNormalWindow(1, root_window(), NULL));
100
101   // Make a synthesized mouse down event. Ensure that the RootWindow will
102   // dispatch further mouse events to |w1|.
103   ui::MouseEvent mouse_pressed_event(ui::ET_MOUSE_PRESSED, gfx::Point(5, 5),
104                                      gfx::Point(5, 5), 0);
105   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
106       &mouse_pressed_event);
107   EXPECT_EQ(w1.get(), root_window()->mouse_pressed_handler());
108
109   // Build a window in the second RootWindow.
110   scoped_ptr<aura::Window> w2(CreateNormalWindow(2, second_root_.get(), NULL));
111
112   // The act of having the second window take capture should clear out mouse
113   // pressed handler in the first RootWindow.
114   w2->SetCapture();
115   EXPECT_EQ(NULL, root_window()->mouse_pressed_handler());
116 }
117
118 // Makes sure that when one window gets capture, it forces the release on the
119 // other. This is needed has to be handled explicitly on Linux, and is a sanity
120 // check on Windows.
121 TEST_F(CaptureControllerTest, ResetOtherWindowCaptureOnCapture) {
122   // Create a window inside the RootWindow.
123   scoped_ptr<aura::Window> w1(CreateNormalWindow(1, root_window(), NULL));
124   w1->SetCapture();
125   // Both capture clients should return the same capture window.
126   EXPECT_EQ(w1.get(), GetCaptureWindow());
127   EXPECT_EQ(w1.get(), GetSecondCaptureWindow());
128
129   // Build a window in the second RootWindow and give it capture. Both capture
130   // clients should return the same capture window.
131   scoped_ptr<aura::Window> w2(CreateNormalWindow(2, second_root_.get(), NULL));
132   w2->SetCapture();
133   EXPECT_EQ(w2.get(), GetCaptureWindow());
134   EXPECT_EQ(w2.get(), GetSecondCaptureWindow());
135 }
136
137 // Verifies the touch target for the RootWindow gets reset on releasing capture.
138 TEST_F(CaptureControllerTest, TouchTargetResetOnCaptureChange) {
139   // Create a window inside the RootWindow.
140   scoped_ptr<aura::Window> w1(CreateNormalWindow(1, root_window(), NULL));
141   aura::test::EventGenerator event_generator1(root_window());
142   event_generator1.PressTouch();
143   w1->SetCapture();
144   // Both capture clients should return the same capture window.
145   EXPECT_EQ(w1.get(), GetCaptureWindow());
146   EXPECT_EQ(w1.get(), GetSecondCaptureWindow());
147
148   // Build a window in the second RootWindow and give it capture. Both capture
149   // clients should return the same capture window.
150   scoped_ptr<aura::Window> w2(CreateNormalWindow(2, second_root_.get(), NULL));
151   w2->SetCapture();
152   EXPECT_EQ(w2.get(), GetCaptureWindow());
153   EXPECT_EQ(w2.get(), GetSecondCaptureWindow());
154
155   // Release capture on the window. Releasing capture should reset the touch
156   // target of the first RootWindow (as it no longer contains the capture
157   // target).
158   w2->ReleaseCapture();
159   EXPECT_EQ(static_cast<aura::Window*>(NULL), GetCaptureWindow());
160   EXPECT_EQ(static_cast<aura::Window*>(NULL), GetSecondCaptureWindow());
161   ui::TouchEvent touch_event(
162       ui::ET_TOUCH_PRESSED, gfx::Point(), 0, 0, ui::EventTimeForNow(), 1.0f,
163       1.0f, 1.0f, 1.0f);
164   EXPECT_EQ(static_cast<ui::GestureConsumer*>(w2.get()),
165             ui::GestureRecognizer::Get()->GetTouchLockedTarget(
166                 &touch_event));
167 }
168
169 }  // namespace views