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 "ash/shelf/shelf_view.h" | 5 #include "ash/shelf/shelf_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "ash/ash_switches.h" | 10 #include "ash/ash_switches.h" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 return Shelf::ForWindow(Shell::GetAllRootWindows()[1]); | 113 return Shelf::ForWindow(Shell::GetAllRootWindows()[1]); |
114 } | 114 } |
115 | 115 |
116 private: | 116 private: |
117 scoped_ptr<TestShelfIconObserver> observer_; | 117 scoped_ptr<TestShelfIconObserver> observer_; |
118 scoped_ptr<ShelfViewTestAPI> shelf_view_test_; | 118 scoped_ptr<ShelfViewTestAPI> shelf_view_test_; |
119 | 119 |
120 DISALLOW_COPY_AND_ASSIGN(ShelfViewIconObserverTest); | 120 DISALLOW_COPY_AND_ASSIGN(ShelfViewIconObserverTest); |
121 }; | 121 }; |
122 | 122 |
| 123 // TestShelfItemDelegate which tracks whether it gets selected. |
| 124 class ShelfItemSelectionTracker : public TestShelfItemDelegate { |
| 125 public: |
| 126 ShelfItemSelectionTracker() : TestShelfItemDelegate(NULL), selected_(false) { |
| 127 } |
| 128 |
| 129 virtual ~ShelfItemSelectionTracker() { |
| 130 } |
| 131 |
| 132 // Returns true if the delegate was selected. |
| 133 bool WasSelected() { |
| 134 return selected_; |
| 135 } |
| 136 |
| 137 // TestShelfItemDelegate: |
| 138 virtual bool ItemSelected(const ui::Event& event) OVERRIDE { |
| 139 selected_ = true; |
| 140 return false; |
| 141 } |
| 142 |
| 143 private: |
| 144 bool selected_; |
| 145 |
| 146 DISALLOW_COPY_AND_ASSIGN(ShelfItemSelectionTracker); |
| 147 }; |
| 148 |
123 TEST_F(ShelfViewIconObserverTest, AddRemove) { | 149 TEST_F(ShelfViewIconObserverTest, AddRemove) { |
124 TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance(); | 150 TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance(); |
125 ASSERT_TRUE(shelf_delegate); | 151 ASSERT_TRUE(shelf_delegate); |
126 | 152 |
127 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); | 153 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); |
128 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 154 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
129 params.bounds = gfx::Rect(0, 0, 200, 200); | 155 params.bounds = gfx::Rect(0, 0, 200, 200); |
130 params.context = CurrentContext(); | 156 params.context = CurrentContext(); |
131 | 157 |
132 scoped_ptr<views::Widget> widget(new views::Widget()); | 158 scoped_ptr<views::Widget> widget(new views::Widget()); |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 EXPECT_LE(item_bounds.bottom(), shelf_view_bounds.height()); | 417 EXPECT_LE(item_bounds.bottom(), shelf_view_bounds.height()); |
392 } | 418 } |
393 } | 419 } |
394 } | 420 } |
395 | 421 |
396 views::View* SimulateButtonPressed(ShelfButtonHost::Pointer pointer, | 422 views::View* SimulateButtonPressed(ShelfButtonHost::Pointer pointer, |
397 int button_index) { | 423 int button_index) { |
398 ShelfButtonHost* button_host = shelf_view_; | 424 ShelfButtonHost* button_host = shelf_view_; |
399 views::View* button = test_api_->GetButton(button_index); | 425 views::View* button = test_api_->GetButton(button_index); |
400 ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED, | 426 ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED, |
401 button->bounds().origin(), | 427 gfx::Point(), |
402 button->GetBoundsInScreen().origin(), 0, 0); | 428 button->GetBoundsInScreen().origin(), 0, 0); |
403 button_host->PointerPressedOnButton(button, pointer, click_event); | 429 button_host->PointerPressedOnButton(button, pointer, click_event); |
404 return button; | 430 return button; |
405 } | 431 } |
406 | 432 |
407 views::View* SimulateClick(ShelfButtonHost::Pointer pointer, | 433 views::View* SimulateClick(ShelfButtonHost::Pointer pointer, |
408 int button_index) { | 434 int button_index) { |
409 ShelfButtonHost* button_host = shelf_view_; | 435 ShelfButtonHost* button_host = shelf_view_; |
410 views::View* button = SimulateButtonPressed(pointer, button_index); | 436 views::View* button = SimulateButtonPressed(pointer, button_index); |
411 button_host->PointerReleasedOnButton(button, ShelfButtonHost::MOUSE, false); | 437 button_host->PointerReleasedOnButton(button, ShelfButtonHost::MOUSE, false); |
412 return button; | 438 return button; |
413 } | 439 } |
414 | 440 |
415 views::View* SimulateDrag(ShelfButtonHost::Pointer pointer, | 441 views::View* SimulateDrag(ShelfButtonHost::Pointer pointer, |
416 int button_index, | 442 int button_index, |
417 int destination_index) { | 443 int destination_index) { |
418 ShelfButtonHost* button_host = shelf_view_; | 444 ShelfButtonHost* button_host = shelf_view_; |
419 views::View* button = SimulateButtonPressed(pointer, button_index); | 445 views::View* button = SimulateButtonPressed(pointer, button_index); |
420 | 446 |
421 // Drag. | 447 // Drag. |
422 views::View* destination = test_api_->GetButton(destination_index); | 448 views::View* destination = test_api_->GetButton(destination_index); |
423 ui::MouseEvent drag_event(ui::ET_MOUSE_DRAGGED, | 449 ui::MouseEvent drag_event(ui::ET_MOUSE_DRAGGED, |
424 destination->bounds().origin(), | 450 gfx::Point(destination->x() - button->x(), |
| 451 destination->y() - button->y()), |
425 destination->GetBoundsInScreen().origin(), 0, 0); | 452 destination->GetBoundsInScreen().origin(), 0, 0); |
426 button_host->PointerDraggedOnButton(button, pointer, drag_event); | 453 button_host->PointerDraggedOnButton(button, pointer, drag_event); |
427 return button; | 454 return button; |
428 } | 455 } |
429 | 456 |
430 void SetupForDragTest( | 457 void SetupForDragTest( |
431 std::vector<std::pair<ShelfID, views::View*> >* id_map) { | 458 std::vector<std::pair<ShelfID, views::View*> >* id_map) { |
432 // Initialize |id_map| with the automatically-created shelf buttons. | 459 // Initialize |id_map| with the automatically-created shelf buttons. |
433 for (size_t i = 0; i < model_->items().size(); ++i) { | 460 for (size_t i = 0; i < model_->items().size(); ++i) { |
434 ShelfButton* button = test_api_->GetButton(i); | 461 ShelfButton* button = test_api_->GetButton(i); |
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1023 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3); | 1050 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3); |
1024 std::rotate(id_map.begin() + 1, | 1051 std::rotate(id_map.begin() + 1, |
1025 id_map.begin() + 2, | 1052 id_map.begin() + 2, |
1026 id_map.begin() + 4); | 1053 id_map.begin() + 4); |
1027 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map)); | 1054 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map)); |
1028 button_host->PointerReleasedOnButton( | 1055 button_host->PointerReleasedOnButton( |
1029 dragged_button, ShelfButtonHost::MOUSE, false); | 1056 dragged_button, ShelfButtonHost::MOUSE, false); |
1030 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT); | 1057 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT); |
1031 } | 1058 } |
1032 | 1059 |
| 1060 // Check that clicking an item and jittering the mouse a bit still selects the |
| 1061 // item. |
| 1062 TEST_F(ShelfViewTest, ClickAndMoveSlightly) { |
| 1063 std::vector<std::pair<ShelfID, views::View*> > id_map; |
| 1064 SetupForDragTest(&id_map); |
| 1065 |
| 1066 ShelfID shelf_id = (id_map.begin() + 1)->first; |
| 1067 views::View* button = (id_map.begin() + 1)->second; |
| 1068 |
| 1069 // Replace the ShelfItemDelegate for |shelf_id| with one which tracks whether |
| 1070 // the shelf item gets selected. |
| 1071 ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker; |
| 1072 item_manager_->SetShelfItemDelegate( |
| 1073 shelf_id, |
| 1074 scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass()); |
| 1075 |
| 1076 gfx::Vector2d press_offset(5, 30); |
| 1077 gfx::Point press_location = gfx::Point() + press_offset; |
| 1078 gfx::Point press_location_in_screen = |
| 1079 button->GetBoundsInScreen().origin() + press_offset; |
| 1080 |
| 1081 ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED, |
| 1082 press_location, |
| 1083 press_location_in_screen, |
| 1084 ui::EF_LEFT_MOUSE_BUTTON, 0); |
| 1085 button->OnMousePressed(click_event); |
| 1086 |
| 1087 ui::MouseEvent drag_event1(ui::ET_MOUSE_DRAGGED, |
| 1088 press_location + gfx::Vector2d(0, 1), |
| 1089 press_location_in_screen + gfx::Vector2d(0, 1), |
| 1090 ui::EF_LEFT_MOUSE_BUTTON, 0); |
| 1091 button->OnMouseDragged(drag_event1); |
| 1092 |
| 1093 ui::MouseEvent drag_event2(ui::ET_MOUSE_DRAGGED, |
| 1094 press_location + gfx::Vector2d(-1, 0), |
| 1095 press_location_in_screen + gfx::Vector2d(-1, 0), |
| 1096 ui::EF_LEFT_MOUSE_BUTTON, 0); |
| 1097 button->OnMouseDragged(drag_event2); |
| 1098 |
| 1099 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED, |
| 1100 press_location + gfx::Vector2d(-1, 0), |
| 1101 press_location_in_screen + gfx::Vector2d(-1, 0), |
| 1102 ui::EF_LEFT_MOUSE_BUTTON, 0); |
| 1103 button->OnMouseReleased(release_event); |
| 1104 |
| 1105 EXPECT_TRUE(selection_tracker->WasSelected()); |
| 1106 } |
| 1107 |
1033 // Confirm that item status changes are reflected in the buttons. | 1108 // Confirm that item status changes are reflected in the buttons. |
1034 TEST_F(ShelfViewTest, ShelfItemStatus) { | 1109 TEST_F(ShelfViewTest, ShelfItemStatus) { |
1035 // All buttons should be visible. | 1110 // All buttons should be visible. |
1036 ASSERT_EQ(test_api_->GetButtonCount(), | 1111 ASSERT_EQ(test_api_->GetButtonCount(), |
1037 test_api_->GetLastVisibleIndex() + 1); | 1112 test_api_->GetLastVisibleIndex() + 1); |
1038 | 1113 |
1039 // Add platform app button. | 1114 // Add platform app button. |
1040 ShelfID last_added = AddPlatformApp(); | 1115 ShelfID last_added = AddPlatformApp(); |
1041 ShelfItem item = GetItemByID(last_added); | 1116 ShelfItem item = GetItemByID(last_added); |
1042 int index = model_->ItemIndexByID(last_added); | 1117 int index = model_->ItemIndexByID(last_added); |
(...skipping 551 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1594 test_api_->RunMessageLoopUntilAnimationsDone(); | 1669 test_api_->RunMessageLoopUntilAnimationsDone(); |
1595 CheckAllItemsAreInBounds(); | 1670 CheckAllItemsAreInBounds(); |
1596 } | 1671 } |
1597 | 1672 |
1598 INSTANTIATE_TEST_CASE_P(LtrRtl, ShelfViewTextDirectionTest, testing::Bool()); | 1673 INSTANTIATE_TEST_CASE_P(LtrRtl, ShelfViewTextDirectionTest, testing::Bool()); |
1599 INSTANTIATE_TEST_CASE_P(VisibleBounds, ShelfViewVisibleBoundsTest, | 1674 INSTANTIATE_TEST_CASE_P(VisibleBounds, ShelfViewVisibleBoundsTest, |
1600 testing::Bool()); | 1675 testing::Bool()); |
1601 | 1676 |
1602 } // namespace test | 1677 } // namespace test |
1603 } // namespace ash | 1678 } // namespace ash |
OLD | NEW |