| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/widget/native_widget_aura.h" | 5 #include "ui/views/widget/native_widget_aura.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/command_line.h" |
| 8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "ui/aura/aura_switches.h" |
| 11 #include "ui/aura/env.h" | 13 #include "ui/aura/env.h" |
| 14 #include "ui/aura/event.h" |
| 12 #include "ui/aura/layout_manager.h" | 15 #include "ui/aura/layout_manager.h" |
| 13 #include "ui/aura/monitor_manager.h" | 16 #include "ui/aura/monitor_manager.h" |
| 14 #include "ui/aura/root_window.h" | 17 #include "ui/aura/root_window.h" |
| 15 #include "ui/aura/single_monitor_manager.h" | 18 #include "ui/aura/single_monitor_manager.h" |
| 16 #include "ui/aura/test/aura_test_helper.h" | 19 #include "ui/aura/test/aura_test_helper.h" |
| 17 #include "ui/aura/window.h" | 20 #include "ui/aura/window.h" |
| 18 #include "ui/gfx/screen.h" | 21 #include "ui/gfx/screen.h" |
| 22 #include "ui/views/layout/fill_layout.h" |
| 19 #include "ui/views/widget/root_view.h" | 23 #include "ui/views/widget/root_view.h" |
| 20 #include "ui/views/widget/widget_delegate.h" | 24 #include "ui/views/widget/widget_delegate.h" |
| 21 | 25 |
| 22 namespace views { | 26 namespace views { |
| 23 namespace { | 27 namespace { |
| 24 | 28 |
| 25 NativeWidgetAura* Init(aura::Window* parent, Widget* widget) { | 29 NativeWidgetAura* Init(aura::Window* parent, Widget* widget) { |
| 26 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | 30 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); |
| 27 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 31 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 28 params.parent = parent; | 32 params.parent = parent; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 widget->Init(params); | 174 widget->Init(params); |
| 171 | 175 |
| 172 // For Aura, client area bounds match window bounds. | 176 // For Aura, client area bounds match window bounds. |
| 173 gfx::Rect client_bounds = widget->GetClientAreaScreenBounds(); | 177 gfx::Rect client_bounds = widget->GetClientAreaScreenBounds(); |
| 174 EXPECT_EQ(10, client_bounds.x()); | 178 EXPECT_EQ(10, client_bounds.x()); |
| 175 EXPECT_EQ(20, client_bounds.y()); | 179 EXPECT_EQ(20, client_bounds.y()); |
| 176 EXPECT_EQ(300, client_bounds.width()); | 180 EXPECT_EQ(300, client_bounds.width()); |
| 177 EXPECT_EQ(400, client_bounds.height()); | 181 EXPECT_EQ(400, client_bounds.height()); |
| 178 } | 182 } |
| 179 | 183 |
| 184 namespace { |
| 185 |
| 186 // View subclass that tracks whether it has gotten a gesture event. |
| 187 class GestureTrackingView : public views::View { |
| 188 public: |
| 189 GestureTrackingView() |
| 190 : got_gesture_event_(false), |
| 191 consume_gesture_event_(true) {} |
| 192 |
| 193 void set_consume_gesture_event(bool value) { |
| 194 consume_gesture_event_ = value; |
| 195 } |
| 196 |
| 197 void clear_got_gesture_event() { |
| 198 got_gesture_event_ = false; |
| 199 } |
| 200 bool got_gesture_event() const { |
| 201 return got_gesture_event_; |
| 202 } |
| 203 |
| 204 // View overrides: |
| 205 virtual ui::GestureStatus OnGestureEvent(const GestureEvent& event) OVERRIDE { |
| 206 got_gesture_event_ = true; |
| 207 return consume_gesture_event_ ? ui::GESTURE_STATUS_CONSUMED : |
| 208 ui::GESTURE_STATUS_UNKNOWN; |
| 209 } |
| 210 |
| 211 private: |
| 212 // Was OnGestureEvent() invoked? |
| 213 bool got_gesture_event_; |
| 214 |
| 215 // Dictates what OnGestureEvent() returns. |
| 216 bool consume_gesture_event_; |
| 217 |
| 218 DISALLOW_COPY_AND_ASSIGN(GestureTrackingView); |
| 219 }; |
| 220 |
| 221 } // namespace |
| 222 |
| 223 // Verifies a capture isn't set on touch press and that the view that gets |
| 224 // the press gets the release. |
| 225 TEST_F(NativeWidgetAuraTest, DontCaptureOnGesture) { |
| 226 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 227 switches::kAuraDisableMouseEventsFromTouch); |
| 228 // Create two views (both sized the same). |child| is configured not to |
| 229 // consume the gesture event. |
| 230 GestureTrackingView* view = new GestureTrackingView(); |
| 231 GestureTrackingView* child = new GestureTrackingView(); |
| 232 child->set_consume_gesture_event(false); |
| 233 view->SetLayoutManager(new FillLayout); |
| 234 view->AddChildView(child); |
| 235 scoped_ptr<TestWidget> widget(new TestWidget()); |
| 236 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); |
| 237 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 238 params.bounds = gfx::Rect(0, 0, 100, 200); |
| 239 widget->Init(params); |
| 240 widget->SetContentsView(view); |
| 241 widget->Show(); |
| 242 |
| 243 aura::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(41, 51), 1, |
| 244 base::TimeDelta()); |
| 245 root_window()->DispatchTouchEvent(&press); |
| 246 // Both views should get the press. |
| 247 EXPECT_TRUE(view->got_gesture_event()); |
| 248 EXPECT_TRUE(child->got_gesture_event()); |
| 249 view->clear_got_gesture_event(); |
| 250 child->clear_got_gesture_event(); |
| 251 // Touch events should not automatically grab capture. |
| 252 EXPECT_FALSE(widget->HasCapture()); |
| 253 |
| 254 // Release touch. Only |view| should get the release since that it consumed |
| 255 // the press. |
| 256 aura::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(250, 251), 1, |
| 257 base::TimeDelta()); |
| 258 root_window()->DispatchTouchEvent(&release); |
| 259 EXPECT_TRUE(view->got_gesture_event()); |
| 260 EXPECT_FALSE(child->got_gesture_event()); |
| 261 view->clear_got_gesture_event(); |
| 262 |
| 263 // Work around for bug in NativeWidgetAura. |
| 264 // TODO: fix bug and remove this. |
| 265 widget->Close(); |
| 266 } |
| 267 |
| 268 TEST_F(NativeWidgetAuraTest, ReleaseCaptureOnTouchRelease) { |
| 269 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 270 switches::kAuraDisableMouseEventsFromTouch); |
| 271 GestureTrackingView* view = new GestureTrackingView(); |
| 272 scoped_ptr<TestWidget> widget(new TestWidget()); |
| 273 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); |
| 274 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 275 params.bounds = gfx::Rect(0, 0, 100, 200); |
| 276 widget->Init(params); |
| 277 widget->SetContentsView(view); |
| 278 widget->Show(); |
| 279 |
| 280 aura::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(41, 51), 1, |
| 281 base::TimeDelta()); |
| 282 root_window()->DispatchTouchEvent(&press); |
| 283 EXPECT_TRUE(view->got_gesture_event()); |
| 284 view->clear_got_gesture_event(); |
| 285 // Set the capture. |
| 286 widget->SetCapture(view); |
| 287 EXPECT_TRUE(widget->HasCapture()); |
| 288 |
| 289 // Press outside the bounds of the widget, it should still go to |view| since |
| 290 // it set the capture. |
| 291 aura::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(241, 251), 2, |
| 292 base::TimeDelta()); |
| 293 root_window()->DispatchTouchEvent(&press2); |
| 294 EXPECT_TRUE(view->got_gesture_event()); |
| 295 view->clear_got_gesture_event(); |
| 296 EXPECT_TRUE(widget->HasCapture()); |
| 297 |
| 298 // Generate a release, this should trigger releasing capture. |
| 299 aura::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(241, 251), 2, |
| 300 base::TimeDelta()); |
| 301 root_window()->DispatchTouchEvent(&release); |
| 302 EXPECT_TRUE(view->got_gesture_event()); |
| 303 view->clear_got_gesture_event(); |
| 304 EXPECT_FALSE(widget->HasCapture()); |
| 305 |
| 306 // Work around for bug in NativeWidgetAura. |
| 307 // TODO: fix bug and remove this. |
| 308 widget->Close(); |
| 309 } |
| 310 |
| 180 } // namespace | 311 } // namespace |
| 181 } // namespace views | 312 } // namespace views |
| OLD | NEW |