| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <map> | 5 #include <map> |
| 6 | 6 |
| 7 #include "app/clipboard/clipboard.h" | 7 #include "app/clipboard/clipboard.h" |
| 8 #include "app/keyboard_codes.h" | 8 #include "app/keyboard_codes.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "gfx/canvas_skia.h" | 12 #include "gfx/canvas_skia.h" |
| 13 #include "gfx/path.h" | 13 #include "gfx/path.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "views/background.h" | 15 #include "views/background.h" |
| 16 #include "views/controls/button/checkbox.h" | 16 #include "views/controls/button/checkbox.h" |
| 17 #if defined(OS_WIN) | |
| 18 #include "views/controls/button/native_button_win.h" | |
| 19 #endif | |
| 20 #include "views/controls/native/native_view_host.h" | 17 #include "views/controls/native/native_view_host.h" |
| 21 #include "views/controls/scroll_view.h" | 18 #include "views/controls/scroll_view.h" |
| 22 #include "views/controls/textfield/textfield.h" | 19 #include "views/controls/textfield/textfield.h" |
| 23 #include "views/event.h" | 20 #include "views/event.h" |
| 24 #include "views/focus/accelerator_handler.h" | 21 #include "views/focus/accelerator_handler.h" |
| 25 #include "views/focus/view_storage.h" | 22 #include "views/focus/view_storage.h" |
| 26 #include "views/view.h" | 23 #include "views/view.h" |
| 27 #include "views/views_delegate.h" | 24 #include "views/views_delegate.h" |
| 28 #include "views/widget/root_view.h" | 25 #include "views/widget/root_view.h" |
| 26 #include "views/window/dialog_delegate.h" |
| 27 #include "views/window/window.h" |
| 28 |
| 29 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
| 30 #include "views/widget/widget_win.h" | 30 #include "views/widget/widget_win.h" |
| 31 #include "views/controls/button/native_button_win.h" |
| 31 #elif defined(OS_LINUX) | 32 #elif defined(OS_LINUX) |
| 32 #include "views/widget/widget_gtk.h" | 33 #include "views/widget/widget_gtk.h" |
| 33 #include "views/window/window_gtk.h" | 34 #include "views/window/window_gtk.h" |
| 34 #endif | 35 #endif |
| 35 #include "views/window/dialog_delegate.h" | 36 #if defined(TOUCH_UI) |
| 36 #include "views/window/window.h" | 37 #include "views/touchui/gesture_manager.h" |
| 38 #endif |
| 37 | 39 |
| 38 using namespace views; | 40 using namespace views; |
| 39 | 41 |
| 40 namespace { | 42 namespace { |
| 41 | 43 |
| 42 class ViewTest : public testing::Test { | 44 class ViewTest : public testing::Test { |
| 43 public: | 45 public: |
| 44 ViewTest() { | 46 ViewTest() { |
| 45 #if defined(OS_WIN) | 47 #if defined(OS_WIN) |
| 46 OleInitialize(NULL); | 48 OleInitialize(NULL); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 150 |
| 149 virtual ~TestView() {} | 151 virtual ~TestView() {} |
| 150 | 152 |
| 151 // Reset all test state | 153 // Reset all test state |
| 152 void Reset() { | 154 void Reset() { |
| 153 did_change_bounds_ = false; | 155 did_change_bounds_ = false; |
| 154 child_added_ = false; | 156 child_added_ = false; |
| 155 child_removed_ = false; | 157 child_removed_ = false; |
| 156 last_mouse_event_type_ = 0; | 158 last_mouse_event_type_ = 0; |
| 157 location_.SetPoint(0, 0); | 159 location_.SetPoint(0, 0); |
| 160 #if defined(TOUCH_UI) |
| 161 last_touch_event_type_ = 0; |
| 162 last_touch_event_was_handled_ = false; |
| 163 #endif |
| 158 last_clip_.setEmpty(); | 164 last_clip_.setEmpty(); |
| 159 accelerator_count_map_.clear(); | 165 accelerator_count_map_.clear(); |
| 160 } | 166 } |
| 161 | 167 |
| 162 virtual void DidChangeBounds(const gfx::Rect& previous, | 168 virtual void DidChangeBounds(const gfx::Rect& previous, |
| 163 const gfx::Rect& current); | 169 const gfx::Rect& current); |
| 164 virtual void ViewHierarchyChanged(bool is_add, View *parent, View *child); | 170 virtual void ViewHierarchyChanged(bool is_add, View *parent, View *child); |
| 165 virtual bool OnMousePressed(const MouseEvent& event); | 171 virtual bool OnMousePressed(const MouseEvent& event); |
| 166 virtual bool OnMouseDragged(const MouseEvent& event); | 172 virtual bool OnMouseDragged(const MouseEvent& event); |
| 167 virtual void OnMouseReleased(const MouseEvent& event, bool canceled); | 173 virtual void OnMouseReleased(const MouseEvent& event, bool canceled); |
| 174 #if defined(TOUCH_UI) |
| 175 virtual bool OnTouchEvent(const TouchEvent& event); |
| 176 #endif |
| 168 virtual void Paint(gfx::Canvas* canvas); | 177 virtual void Paint(gfx::Canvas* canvas); |
| 169 virtual bool AcceleratorPressed(const Accelerator& accelerator); | 178 virtual bool AcceleratorPressed(const Accelerator& accelerator); |
| 170 | 179 |
| 171 // DidChangeBounds test | 180 // DidChangeBounds test |
| 172 bool did_change_bounds_; | 181 bool did_change_bounds_; |
| 173 gfx::Rect previous_bounds_; | 182 gfx::Rect previous_bounds_; |
| 174 gfx::Rect new_bounds_; | 183 gfx::Rect new_bounds_; |
| 175 | 184 |
| 176 // AddRemoveNotifications test | 185 // AddRemoveNotifications test |
| 177 bool child_added_; | 186 bool child_added_; |
| 178 bool child_removed_; | 187 bool child_removed_; |
| 179 View* parent_; | 188 View* parent_; |
| 180 View* child_; | 189 View* child_; |
| 181 | 190 |
| 182 // MouseEvent | 191 // MouseEvent |
| 183 int last_mouse_event_type_; | 192 int last_mouse_event_type_; |
| 184 gfx::Point location_; | 193 gfx::Point location_; |
| 185 | 194 |
| 195 #if defined(TOUCH_UI) |
| 196 // TouchEvent |
| 197 int last_touch_event_type_; |
| 198 bool last_touch_event_was_handled_; |
| 199 #endif |
| 200 |
| 186 // Painting | 201 // Painting |
| 187 SkRect last_clip_; | 202 SkRect last_clip_; |
| 188 | 203 |
| 189 // Accelerators | 204 // Accelerators |
| 190 std::map<Accelerator, int> accelerator_count_map_; | 205 std::map<Accelerator, int> accelerator_count_map_; |
| 191 }; | 206 }; |
| 192 | 207 |
| 208 #if defined(TOUCH_UI) |
| 209 // Mock instance of the GestureManager for testing. |
| 210 class MockGestureManager : public GestureManager { |
| 211 public: |
| 212 // Reset all test state |
| 213 void Reset() { |
| 214 last_touch_event_ = 0; |
| 215 last_view_ = NULL; |
| 216 previously_handled_flag_ = false; |
| 217 } |
| 218 |
| 219 bool previously_handled_flag_; |
| 220 bool ProcessTouchEventForGesture(const TouchEvent& event, |
| 221 View* source, |
| 222 bool previouslyHandled); |
| 223 MockGestureManager(); |
| 224 |
| 225 int last_touch_event_; |
| 226 View *last_view_; |
| 227 |
| 228 DISALLOW_COPY_AND_ASSIGN(MockGestureManager); |
| 229 }; |
| 230 |
| 231 #endif |
| 232 |
| 193 //////////////////////////////////////////////////////////////////////////////// | 233 //////////////////////////////////////////////////////////////////////////////// |
| 194 // DidChangeBounds | 234 // DidChangeBounds |
| 195 //////////////////////////////////////////////////////////////////////////////// | 235 //////////////////////////////////////////////////////////////////////////////// |
| 196 | 236 |
| 197 void TestView::DidChangeBounds(const gfx::Rect& previous, | 237 void TestView::DidChangeBounds(const gfx::Rect& previous, |
| 198 const gfx::Rect& current) { | 238 const gfx::Rect& current) { |
| 199 did_change_bounds_ = true; | 239 did_change_bounds_ = true; |
| 200 previous_bounds_ = previous; | 240 previous_bounds_ = previous; |
| 201 new_bounds_ = current; | 241 new_bounds_ = current; |
| 202 } | 242 } |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 root->OnMouseDragged(released); | 416 root->OnMouseDragged(released); |
| 377 EXPECT_EQ(v2->last_mouse_event_type_, Event::ET_MOUSE_RELEASED); | 417 EXPECT_EQ(v2->last_mouse_event_type_, Event::ET_MOUSE_RELEASED); |
| 378 EXPECT_EQ(v2->location_.x(), -100); | 418 EXPECT_EQ(v2->location_.x(), -100); |
| 379 EXPECT_EQ(v2->location_.y(), -100); | 419 EXPECT_EQ(v2->location_.y(), -100); |
| 380 // Make sure v1 did not receive the event | 420 // Make sure v1 did not receive the event |
| 381 EXPECT_EQ(v1->last_mouse_event_type_, 0); | 421 EXPECT_EQ(v1->last_mouse_event_type_, 0); |
| 382 | 422 |
| 383 window->CloseNow(); | 423 window->CloseNow(); |
| 384 } | 424 } |
| 385 | 425 |
| 426 #if defined(TOUCH_UI) |
| 427 //////////////////////////////////////////////////////////////////////////////// |
| 428 // TouchEvent |
| 429 //////////////////////////////////////////////////////////////////////////////// |
| 430 bool MockGestureManager::ProcessTouchEventForGesture( |
| 431 const TouchEvent& event, |
| 432 View* source, |
| 433 bool previouslyHandled) { |
| 434 last_touch_event_ = event.GetType(); |
| 435 last_view_ = source; |
| 436 previously_handled_flag_ = previouslyHandled; |
| 437 return true; |
| 438 } |
| 439 |
| 440 MockGestureManager::MockGestureManager() { |
| 441 } |
| 442 |
| 443 bool TestView::OnTouchEvent(const TouchEvent& event) { |
| 444 last_touch_event_type_ = event.GetType(); |
| 445 location_.SetPoint(event.x(), event.y()); |
| 446 return last_touch_event_was_handled_; |
| 447 } |
| 448 |
| 449 TEST_F(ViewTest, TouchEvent) { |
| 450 MockGestureManager* gm = new MockGestureManager(); |
| 451 |
| 452 TestView* v1 = new TestView(); |
| 453 v1->SetBounds(0, 0, 300, 300); |
| 454 |
| 455 TestView* v2 = new TestView(); |
| 456 v2->SetBounds(100, 100, 100, 100); |
| 457 |
| 458 scoped_ptr<Widget> window(CreateWidget()); |
| 459 #if defined(OS_WIN) |
| 460 // This code would need to be here when we support |
| 461 // touch on windows? |
| 462 WidgetWin* window_win = static_cast<WidgetWin*>(window.get()); |
| 463 window_win->set_delete_on_destroy(false); |
| 464 window_win->set_window_style(WS_OVERLAPPEDWINDOW); |
| 465 window_win->Init(NULL, gfx::Rect(50, 50, 650, 650)); |
| 466 #endif |
| 467 RootView* root = window->GetRootView(); |
| 468 |
| 469 root->AddChildView(v1); |
| 470 root->SetGestureManager(gm); |
| 471 v1->AddChildView(v2); |
| 472 |
| 473 v1->Reset(); |
| 474 v2->Reset(); |
| 475 gm->Reset(); |
| 476 |
| 477 TouchEvent pressed(Event::ET_TOUCH_PRESSED, |
| 478 110, |
| 479 120, |
| 480 0, /* no flags */ |
| 481 0 /* first finger touch */); |
| 482 v2->last_touch_event_was_handled_ = true; |
| 483 root->OnTouchEvent(pressed); |
| 484 |
| 485 EXPECT_EQ(v2->last_touch_event_type_, Event::ET_TOUCH_PRESSED); |
| 486 EXPECT_EQ(v2->location_.x(), 10); |
| 487 EXPECT_EQ(v2->location_.y(), 20); |
| 488 // Make sure v1 did not receive the event |
| 489 EXPECT_EQ(v1->last_touch_event_type_, 0); |
| 490 |
| 491 EXPECT_EQ(gm->last_touch_event_, Event::ET_TOUCH_PRESSED); |
| 492 EXPECT_EQ(gm->last_view_, root); |
| 493 EXPECT_EQ(gm->previously_handled_flag_, true); |
| 494 |
| 495 // Drag event out of bounds. Should still go to v2 |
| 496 v1->Reset(); |
| 497 v2->Reset(); |
| 498 TouchEvent dragged(Event::ET_TOUCH_MOVED, |
| 499 50, |
| 500 40, |
| 501 0, /* no flags */ |
| 502 0 /* first finger touch */); |
| 503 root->OnTouchEvent(dragged); |
| 504 EXPECT_EQ(v2->last_touch_event_type_, Event::ET_TOUCH_MOVED); |
| 505 EXPECT_EQ(v2->location_.x(), -50); |
| 506 EXPECT_EQ(v2->location_.y(), -60); |
| 507 // Make sure v1 did not receive the event |
| 508 EXPECT_EQ(v1->last_touch_event_type_, 0); |
| 509 |
| 510 EXPECT_EQ(gm->last_touch_event_, Event::ET_TOUCH_MOVED); |
| 511 EXPECT_EQ(gm->last_view_, root); |
| 512 EXPECT_EQ(gm->previously_handled_flag_, true); |
| 513 |
| 514 // Releasted event out of bounds. Should still go to v2 |
| 515 v1->Reset(); |
| 516 v2->Reset(); |
| 517 TouchEvent released(Event::ET_TOUCH_RELEASED, 0, 0, 0, 0 /* first finger */); |
| 518 v2->last_touch_event_was_handled_ = true; |
| 519 root->OnTouchEvent(released); |
| 520 EXPECT_EQ(v2->last_touch_event_type_, Event::ET_TOUCH_RELEASED); |
| 521 EXPECT_EQ(v2->location_.x(), -100); |
| 522 EXPECT_EQ(v2->location_.y(), -100); |
| 523 // Make sure v1 did not receive the event |
| 524 EXPECT_EQ(v1->last_touch_event_type_, 0); |
| 525 |
| 526 EXPECT_EQ(gm->last_touch_event_, Event::ET_TOUCH_RELEASED); |
| 527 EXPECT_EQ(gm->last_view_, root); |
| 528 EXPECT_EQ(gm->previously_handled_flag_, true); |
| 529 |
| 530 window->CloseNow(); |
| 531 } |
| 532 #endif |
| 533 |
| 386 //////////////////////////////////////////////////////////////////////////////// | 534 //////////////////////////////////////////////////////////////////////////////// |
| 387 // Painting | 535 // Painting |
| 388 //////////////////////////////////////////////////////////////////////////////// | 536 //////////////////////////////////////////////////////////////////////////////// |
| 389 | 537 |
| 390 void TestView::Paint(gfx::Canvas* canvas) { | 538 void TestView::Paint(gfx::Canvas* canvas) { |
| 391 canvas->AsCanvasSkia()->getClipBounds(&last_clip_); | 539 canvas->AsCanvasSkia()->getClipBounds(&last_clip_); |
| 392 } | 540 } |
| 393 | 541 |
| 394 void CheckRect(const SkRect& check_rect, const SkRect& target_rect) { | 542 void CheckRect(const SkRect& check_rect, const SkRect& target_rect) { |
| 395 EXPECT_EQ(target_rect.fLeft, check_rect.fLeft); | 543 EXPECT_EQ(target_rect.fLeft, check_rect.fLeft); |
| (...skipping 949 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1345 #endif | 1493 #endif |
| 1346 } | 1494 } |
| 1347 | 1495 |
| 1348 TEST_F(ViewTest, ChangeNativeViewHierarchyChangeHierarchy) { | 1496 TEST_F(ViewTest, ChangeNativeViewHierarchyChangeHierarchy) { |
| 1349 // TODO(georgey): Fix the test for Linux | 1497 // TODO(georgey): Fix the test for Linux |
| 1350 #if defined(OS_WIN) | 1498 #if defined(OS_WIN) |
| 1351 TestChangeNativeViewHierarchy test(this); | 1499 TestChangeNativeViewHierarchy test(this); |
| 1352 test.CheckChangingHierarhy(); | 1500 test.CheckChangingHierarhy(); |
| 1353 #endif | 1501 #endif |
| 1354 } | 1502 } |
| OLD | NEW |