Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/memory/scoped_nsobject.h" | 5 #include "base/memory/scoped_nsobject.h" |
| 6 #import "testing/gtest_mac.h" | 6 #import "testing/gtest_mac.h" |
| 7 #include "ui/app_list/app_list_item_model.h" | 7 #include "ui/app_list/app_list_item_model.h" |
| 8 #import "ui/app_list/cocoa/apps_collection_view_drag_manager.h" | 8 #import "ui/app_list/cocoa/apps_collection_view_drag_manager.h" |
| 9 #import "ui/app_list/cocoa/apps_grid_controller.h" | 9 #import "ui/app_list/cocoa/apps_grid_controller.h" |
| 10 #import "ui/app_list/cocoa/apps_grid_view_item.h" | 10 #import "ui/app_list/cocoa/apps_grid_view_item.h" |
| 11 #import "ui/app_list/cocoa/apps_pagination_model_observer.h" | 11 #import "ui/app_list/cocoa/apps_pagination_model_observer.h" |
| 12 #import "ui/app_list/cocoa/test/apps_grid_controller_test_helper.h" | 12 #import "ui/app_list/cocoa/test/apps_grid_controller_test_helper.h" |
| 13 #include "ui/app_list/test/app_list_test_model.h" | 13 #include "ui/app_list/test/app_list_test_model.h" |
| 14 #include "ui/app_list/test/app_list_test_view_delegate.h" | 14 #include "ui/app_list/test/app_list_test_view_delegate.h" |
| 15 #import "ui/base/test/cocoa_test_event_utils.h" | 15 #import "ui/base/test/cocoa_test_event_utils.h" |
| 16 | 16 |
| 17 @interface TestPaginationObserver : NSObject<AppsPaginationModelObserver> { | 17 @interface TestPaginationObserver : NSObject<AppsPaginationModelObserver> { |
| 18 @private | 18 @private |
| 19 NSInteger hoveredSegmentForTest_; | |
| 19 int totalPagesChangedCount_; | 20 int totalPagesChangedCount_; |
| 20 int selectedPageChangedCount_; | 21 int selectedPageChangedCount_; |
| 21 int lastNewSelectedPage_; | 22 int lastNewSelectedPage_; |
| 22 bool visibilityDidChange_; | 23 bool visibilityDidChange_; |
| 23 } | 24 } |
| 24 | 25 |
| 26 @property(assign, nonatomic) NSInteger hoveredSegmentForTest; | |
| 25 @property(assign, nonatomic) int totalPagesChangedCount; | 27 @property(assign, nonatomic) int totalPagesChangedCount; |
| 26 @property(assign, nonatomic) int selectedPageChangedCount; | 28 @property(assign, nonatomic) int selectedPageChangedCount; |
| 27 @property(assign, nonatomic) int lastNewSelectedPage; | 29 @property(assign, nonatomic) int lastNewSelectedPage; |
| 28 | 30 |
| 29 - (bool)readVisibilityDidChange; | 31 - (bool)readVisibilityDidChange; |
| 30 | 32 |
| 31 @end | 33 @end |
| 32 | 34 |
| 33 @implementation TestPaginationObserver | 35 @implementation TestPaginationObserver |
| 34 | 36 |
| 37 @synthesize hoveredSegmentForTest = hoveredSegmentForTest_; | |
| 35 @synthesize totalPagesChangedCount = totalPagesChangedCount_; | 38 @synthesize totalPagesChangedCount = totalPagesChangedCount_; |
| 36 @synthesize selectedPageChangedCount = selectedPageChangedCount_; | 39 @synthesize selectedPageChangedCount = selectedPageChangedCount_; |
| 37 @synthesize lastNewSelectedPage = lastNewSelectedPage_; | 40 @synthesize lastNewSelectedPage = lastNewSelectedPage_; |
| 38 | 41 |
| 42 - (id)init { | |
| 43 if ((self = [super init])) | |
| 44 hoveredSegmentForTest_ = -1; | |
| 45 | |
| 46 return self; | |
| 47 } | |
| 48 | |
| 39 - (bool)readVisibilityDidChange { | 49 - (bool)readVisibilityDidChange { |
| 40 bool truth = visibilityDidChange_; | 50 bool truth = visibilityDidChange_; |
| 41 visibilityDidChange_ = false; | 51 visibilityDidChange_ = false; |
| 42 return truth; | 52 return truth; |
| 43 } | 53 } |
| 44 | 54 |
| 45 - (void)totalPagesChanged { | 55 - (void)totalPagesChanged { |
| 46 ++totalPagesChangedCount_; | 56 ++totalPagesChangedCount_; |
| 47 } | 57 } |
| 48 | 58 |
| 49 - (void)selectedPageChanged:(int)newSelected { | 59 - (void)selectedPageChanged:(int)newSelected { |
| 50 ++selectedPageChangedCount_; | 60 ++selectedPageChangedCount_; |
| 51 lastNewSelectedPage_ = newSelected; | 61 lastNewSelectedPage_ = newSelected; |
| 52 } | 62 } |
| 53 | 63 |
| 54 - (void)pageVisibilityChanged { | 64 - (void)pageVisibilityChanged { |
| 55 visibilityDidChange_ = true; | 65 visibilityDidChange_ = true; |
| 56 } | 66 } |
| 57 | 67 |
| 68 - (NSInteger)pagerSegmentAtLocation:(NSPoint)locationInWindow { | |
| 69 return hoveredSegmentForTest_; | |
| 70 } | |
| 71 | |
| 58 @end | 72 @end |
| 59 | 73 |
| 60 namespace app_list { | 74 namespace app_list { |
| 61 namespace test { | 75 namespace test { |
| 62 | 76 |
| 63 namespace { | 77 namespace { |
| 64 | 78 |
| 65 class AppsGridControllerTest : public AppsGridControllerTestHelper { | 79 class AppsGridControllerTest : public AppsGridControllerTestHelper { |
| 66 public: | 80 public: |
| 67 AppsGridControllerTest() {} | 81 AppsGridControllerTest() {} |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 SimulateKeyAction(@selector(moveRight:)); | 233 SimulateKeyAction(@selector(moveRight:)); |
| 220 SimulateKeyAction(@selector(moveRight:)); | 234 SimulateKeyAction(@selector(moveRight:)); |
| 221 EXPECT_EQ(2u, [apps_grid_controller_ selectedItemIndex]); | 235 EXPECT_EQ(2u, [apps_grid_controller_ selectedItemIndex]); |
| 222 SimulateKeyAction(@selector(insertNewline:)); | 236 SimulateKeyAction(@selector(insertNewline:)); |
| 223 EXPECT_EQ(1, delegate()->activate_count()); | 237 EXPECT_EQ(1, delegate()->activate_count()); |
| 224 EXPECT_EQ(std::string("Item 2"), delegate()->last_activated()->title()); | 238 EXPECT_EQ(std::string("Item 2"), delegate()->last_activated()->title()); |
| 225 } | 239 } |
| 226 | 240 |
| 227 // Tests keyboard navigation across pages. | 241 // Tests keyboard navigation across pages. |
| 228 TEST_F(AppsGridControllerTest, CrossPageKeyboardNavigation) { | 242 TEST_F(AppsGridControllerTest, CrossPageKeyboardNavigation) { |
| 229 [AppsGridController setScrollAnimationDuration:0.0]; | |
| 230 | |
| 231 model()->PopulateApps(2 * kItemsPerPage); | 243 model()->PopulateApps(2 * kItemsPerPage); |
| 232 EXPECT_EQ(kItemsPerPage, [[GetPageAt(0) content] count]); | 244 EXPECT_EQ(kItemsPerPage, [[GetPageAt(0) content] count]); |
| 233 EXPECT_EQ(kItemsPerPage, [[GetPageAt(1) content] count]); | 245 EXPECT_EQ(kItemsPerPage, [[GetPageAt(1) content] count]); |
| 234 | 246 |
| 235 // Moving Left, Up, or PageUp from the top-left corner of the first page does | 247 // Moving Left, Up, or PageUp from the top-left corner of the first page does |
| 236 // nothing. | 248 // nothing. |
| 237 [apps_grid_controller_ selectItemAtIndex:0]; | 249 [apps_grid_controller_ selectItemAtIndex:0]; |
| 238 SimulateKeyAction(@selector(moveLeft:)); | 250 SimulateKeyAction(@selector(moveLeft:)); |
| 239 EXPECT_EQ(0u, [apps_grid_controller_ selectedItemIndex]); | 251 EXPECT_EQ(0u, [apps_grid_controller_ selectedItemIndex]); |
| 240 SimulateKeyAction(@selector(moveUp:)); | 252 SimulateKeyAction(@selector(moveUp:)); |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 453 EXPECT_EQ(4u, [apps_grid_controller_ pageCount]); | 465 EXPECT_EQ(4u, [apps_grid_controller_ pageCount]); |
| 454 | 466 |
| 455 EXPECT_FALSE([observer readVisibilityDidChange]); | 467 EXPECT_FALSE([observer readVisibilityDidChange]); |
| 456 EXPECT_EQ(0, [observer selectedPageChangedCount]); | 468 EXPECT_EQ(0, [observer selectedPageChangedCount]); |
| 457 | 469 |
| 458 [apps_grid_controller_ setPaginationObserver:nil]; | 470 [apps_grid_controller_ setPaginationObserver:nil]; |
| 459 } | 471 } |
| 460 | 472 |
| 461 // Test AppsGridPaginationObserver selectedPageChanged(). | 473 // Test AppsGridPaginationObserver selectedPageChanged(). |
| 462 TEST_F(AppsGridControllerTest, PaginationObserverSelectedPageChanged) { | 474 TEST_F(AppsGridControllerTest, PaginationObserverSelectedPageChanged) { |
| 463 [AppsGridController setScrollAnimationDuration:0.0]; | |
| 464 scoped_nsobject<TestPaginationObserver> observer( | 475 scoped_nsobject<TestPaginationObserver> observer( |
| 465 [[TestPaginationObserver alloc] init]); | 476 [[TestPaginationObserver alloc] init]); |
| 466 [apps_grid_controller_ setPaginationObserver:observer]; | 477 [apps_grid_controller_ setPaginationObserver:observer]; |
| 467 EXPECT_EQ(0, [[NSAnimationContext currentContext] duration]); | 478 EXPECT_EQ(0, [[NSAnimationContext currentContext] duration]); |
| 468 | 479 |
| 469 ReplaceTestModel(kItemsPerPage * 3 + 1); | 480 ReplaceTestModel(kItemsPerPage * 3 + 1); |
| 470 EXPECT_EQ(1, [observer totalPagesChangedCount]); | 481 EXPECT_EQ(1, [observer totalPagesChangedCount]); |
| 471 EXPECT_EQ(4u, [apps_grid_controller_ pageCount]); | 482 EXPECT_EQ(4u, [apps_grid_controller_ pageCount]); |
| 472 | 483 |
| 473 EXPECT_FALSE([observer readVisibilityDidChange]); | 484 EXPECT_FALSE([observer readVisibilityDidChange]); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 618 [drag_manager onMouseDownInPage:page | 629 [drag_manager onMouseDownInPage:page |
| 619 withEvent:mouse_at_cell_0]; | 630 withEvent:mouse_at_cell_0]; |
| 620 [drag_manager onMouseDragged:mouse_at_cell_1]; | 631 [drag_manager onMouseDragged:mouse_at_cell_1]; |
| 621 [drag_manager onMouseUp:mouse_at_cell_1]; | 632 [drag_manager onMouseUp:mouse_at_cell_1]; |
| 622 EXPECT_EQ(std::string(), model()->GetModelContent()); | 633 EXPECT_EQ(std::string(), model()->GetModelContent()); |
| 623 EXPECT_EQ(std::string("||"), GetViewContent()); | 634 EXPECT_EQ(std::string("||"), GetViewContent()); |
| 624 } | 635 } |
| 625 | 636 |
| 626 // Test item moves between pages. | 637 // Test item moves between pages. |
| 627 TEST_F(AppsGridControllerTest, DragAndDropMultiPage) { | 638 TEST_F(AppsGridControllerTest, DragAndDropMultiPage) { |
| 628 [AppsGridController setScrollAnimationDuration:0.0]; | |
| 629 const size_t kPagesToTest = 3; | 639 const size_t kPagesToTest = 3; |
| 630 // Put one item on the last page to hit more edge cases. | 640 // Put one item on the last page to hit more edge cases. |
| 631 ReplaceTestModel(kItemsPerPage * (kPagesToTest - 1) + 1); | 641 ReplaceTestModel(kItemsPerPage * (kPagesToTest - 1) + 1); |
| 632 NSCollectionView* page[kPagesToTest]; | 642 NSCollectionView* page[kPagesToTest]; |
| 633 for (size_t i = 0; i < kPagesToTest; ++i) | 643 for (size_t i = 0; i < kPagesToTest; ++i) |
| 634 page[i] = [apps_grid_controller_ collectionViewAtPageIndex:i]; | 644 page[i] = [apps_grid_controller_ collectionViewAtPageIndex:i]; |
| 635 | 645 |
| 636 const std::string kSecondItemMovedToSecondPage = | 646 const std::string kSecondItemMovedToSecondPage = |
| 637 "|Item 0,Item 2,Item 3,Item 4,Item 5,Item 6,Item 7,Item 8," | 647 "|Item 0,Item 2,Item 3,Item 4,Item 5,Item 6,Item 7,Item 8," |
| 638 "Item 9,Item 10,Item 11,Item 12,Item 13,Item 14,Item 15,Item 16|" | 648 "Item 9,Item 10,Item 11,Item 12,Item 13,Item 14,Item 15,Item 16|" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 689 EXPECT_EQ(2u, GetPageIndexForItem(33)); | 699 EXPECT_EQ(2u, GetPageIndexForItem(33)); |
| 690 | 700 |
| 691 // Scroll to end again, and keep dragging (should be ignored). | 701 // Scroll to end again, and keep dragging (should be ignored). |
| 692 [apps_grid_controller_ scrollToPage:2]; | 702 [apps_grid_controller_ scrollToPage:2]; |
| 693 [drag_manager onMouseDragged:mouse_at_cell_0]; | 703 [drag_manager onMouseDragged:mouse_at_cell_0]; |
| 694 EXPECT_EQ(0u, GetPageIndexForItem(1)); | 704 EXPECT_EQ(0u, GetPageIndexForItem(1)); |
| 695 [drag_manager onMouseUp:mouse_at_cell_0]; | 705 [drag_manager onMouseUp:mouse_at_cell_0]; |
| 696 EXPECT_EQ(0u, GetPageIndexForItem(1)); | 706 EXPECT_EQ(0u, GetPageIndexForItem(1)); |
| 697 } | 707 } |
| 698 | 708 |
| 709 namespace { | |
| 710 | |
| 711 NSEvent* MouseEventForScroll(NSView* view, CGFloat relative_x) { | |
|
sail
2013/05/17 19:16:13
move to top of file?
tapted
2013/05/17 23:25:29
Done.
| |
| 712 NSRect view_rect = [view frame]; | |
| 713 NSPoint point_in_view = NSMakePoint(NSMidX(view_rect), NSMidY(view_rect)); | |
| 714 point_in_view.x += point_in_view.x * relative_x; | |
| 715 NSPoint point_in_window = [view convertPoint:point_in_view | |
| 716 toView:nil]; | |
| 717 return cocoa_test_event_utils::LeftMouseDownAtPoint(point_in_window); | |
| 718 } | |
| 719 | |
| 720 } // namespace | |
| 721 | |
| 722 // Test scrolling when dragging past edge or over the pager. | |
| 723 TEST_F(AppsGridControllerTest, ScrollingWhileDragging) { | |
| 724 scoped_nsobject<TestPaginationObserver> observer( | |
| 725 [[TestPaginationObserver alloc] init]); | |
| 726 [apps_grid_controller_ setPaginationObserver:observer]; | |
| 727 | |
| 728 ReplaceTestModel(kItemsPerPage * 3); | |
| 729 // Start on the middle page. | |
| 730 [apps_grid_controller_ scrollToPage:1]; | |
| 731 NSCollectionView* page = [apps_grid_controller_ collectionViewAtPageIndex:1]; | |
| 732 NSEvent* mouse_at_cell_0 = MouseEventInCell(page, 0); | |
| 733 | |
| 734 NSEvent* at_center = MouseEventForScroll([apps_grid_controller_ view], 0.0); | |
| 735 NSEvent* at_left = MouseEventForScroll([apps_grid_controller_ view], -1.1); | |
| 736 NSEvent* at_right = MouseEventForScroll([apps_grid_controller_ view], 1.1); | |
| 737 | |
| 738 AppsCollectionViewDragManager* drag_manager = | |
| 739 [apps_grid_controller_ dragManager]; | |
| 740 [drag_manager onMouseDownInPage:page | |
| 741 withEvent:mouse_at_cell_0]; | |
| 742 [drag_manager onMouseDragged:at_center]; | |
| 743 | |
| 744 // Nothing should be scheduled: target page is visible page. | |
| 745 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]); | |
| 746 EXPECT_EQ(1u, [apps_grid_controller_ scheduledScrollPage]); | |
| 747 | |
| 748 // Drag to the left, should go to first page and no further. | |
| 749 [drag_manager onMouseDragged:at_left]; | |
| 750 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]); | |
| 751 EXPECT_EQ(0u, [apps_grid_controller_ scheduledScrollPage]); | |
| 752 [apps_grid_controller_ scrollToPage:0]; // Commit without timer for testing. | |
| 753 [drag_manager onMouseDragged:at_left]; | |
| 754 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]); | |
| 755 EXPECT_EQ(0u, [apps_grid_controller_ scheduledScrollPage]); | |
| 756 | |
| 757 // Drag to the right, should go to last page and no futher. | |
| 758 [drag_manager onMouseDragged:at_right]; | |
| 759 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]); | |
| 760 EXPECT_EQ(1u, [apps_grid_controller_ scheduledScrollPage]); | |
| 761 [apps_grid_controller_ scrollToPage:1]; | |
| 762 [drag_manager onMouseDragged:at_right]; | |
| 763 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]); | |
| 764 EXPECT_EQ(2u, [apps_grid_controller_ scheduledScrollPage]); | |
| 765 [apps_grid_controller_ scrollToPage:2]; | |
| 766 [drag_manager onMouseDragged:at_right]; | |
| 767 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]); | |
| 768 EXPECT_EQ(2u, [apps_grid_controller_ scheduledScrollPage]); | |
| 769 | |
| 770 // Simulate a hover over the first pager segment. | |
| 771 [observer setHoveredSegmentForTest:0]; | |
| 772 [drag_manager onMouseDragged:at_center]; | |
| 773 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]); | |
| 774 EXPECT_EQ(0u, [apps_grid_controller_ scheduledScrollPage]); | |
| 775 | |
| 776 // Drag it back, should cancel schedule. | |
| 777 [observer setHoveredSegmentForTest:-1]; | |
| 778 [drag_manager onMouseDragged:at_center]; | |
| 779 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]); | |
| 780 EXPECT_EQ(2u, [apps_grid_controller_ scheduledScrollPage]); | |
| 781 | |
| 782 // Hover again, now over middle segment, and ensure a release also cancels. | |
| 783 [observer setHoveredSegmentForTest:1]; | |
| 784 [drag_manager onMouseDragged:at_center]; | |
| 785 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]); | |
| 786 EXPECT_EQ(1u, [apps_grid_controller_ scheduledScrollPage]); | |
| 787 [drag_manager onMouseUp:at_center]; | |
| 788 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]); | |
| 789 EXPECT_EQ(2u, [apps_grid_controller_ scheduledScrollPage]); | |
| 790 | |
| 791 [apps_grid_controller_ setPaginationObserver:nil]; | |
| 792 } | |
| 793 | |
| 699 } // namespace test | 794 } // namespace test |
| 700 } // namespace app_list | 795 } // namespace app_list |
| OLD | NEW |