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
3 // found in the LICENSE file.
5 #include "base/mac/foundation_util.h"
6 #include "base/mac/scoped_nsobject.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "skia/ext/skia_utils_mac.h"
9 #import "testing/gtest_mac.h"
10 #include "ui/app_list/app_list_constants.h"
11 #include "ui/app_list/app_list_item.h"
12 #import "ui/app_list/cocoa/apps_collection_view_drag_manager.h"
13 #import "ui/app_list/cocoa/apps_grid_controller.h"
14 #import "ui/app_list/cocoa/apps_grid_view_item.h"
15 #import "ui/app_list/cocoa/apps_pagination_model_observer.h"
16 #import "ui/app_list/cocoa/test/apps_grid_controller_test_helper.h"
17 #include "ui/app_list/test/app_list_test_model.h"
18 #include "ui/app_list/test/app_list_test_view_delegate.h"
19 #include "ui/base/models/simple_menu_model.h"
20 #import "ui/events/test/cocoa_test_event_utils.h"
22 @interface TestPaginationObserver : NSObject<AppsPaginationModelObserver> {
24 NSInteger hoveredSegmentForTest_;
25 int totalPagesChangedCount_;
26 int selectedPageChangedCount_;
27 int lastNewSelectedPage_;
28 bool visibilityDidChange_;
31 @property(assign, nonatomic) NSInteger hoveredSegmentForTest;
32 @property(assign, nonatomic) int totalPagesChangedCount;
33 @property(assign, nonatomic) int selectedPageChangedCount;
34 @property(assign, nonatomic) int lastNewSelectedPage;
36 - (bool)readVisibilityDidChange;
40 @implementation TestPaginationObserver
42 @synthesize hoveredSegmentForTest = hoveredSegmentForTest_;
43 @synthesize totalPagesChangedCount = totalPagesChangedCount_;
44 @synthesize selectedPageChangedCount = selectedPageChangedCount_;
45 @synthesize lastNewSelectedPage = lastNewSelectedPage_;
48 if ((self = [super init]))
49 hoveredSegmentForTest_ = -1;
54 - (bool)readVisibilityDidChange {
55 bool truth = visibilityDidChange_;
56 visibilityDidChange_ = false;
60 - (void)totalPagesChanged {
61 ++totalPagesChangedCount_;
64 - (void)selectedPageChanged:(int)newSelected {
65 ++selectedPageChangedCount_;
66 lastNewSelectedPage_ = newSelected;
69 - (void)pageVisibilityChanged {
70 visibilityDidChange_ = true;
73 - (NSInteger)pagerSegmentAtLocation:(NSPoint)locationInWindow {
74 return hoveredSegmentForTest_;
84 class AppsGridControllerTest : public AppsGridControllerTestHelper {
86 AppsGridControllerTest() {}
88 AppListTestViewDelegate* delegate() {
89 return owned_delegate_.get();
92 NSColor* ButtonTitleColorAt(size_t index) {
93 NSDictionary* attributes =
94 [[[GetItemViewAt(index) cell] attributedTitle] attributesAtIndex:0
96 return [attributes objectForKey:NSForegroundColorAttributeName];
99 void SetUp() override {
100 owned_apps_grid_controller_.reset([[AppsGridController alloc] init]);
101 owned_delegate_.reset(new AppListTestViewDelegate);
102 [owned_apps_grid_controller_ setDelegate:owned_delegate_.get()];
103 AppsGridControllerTestHelper::SetUpWithGridController(
104 owned_apps_grid_controller_.get());
106 [[test_window() contentView] addSubview:[apps_grid_controller_ view]];
107 [test_window() makePretendKeyWindowAndSetFirstResponder:
108 [apps_grid_controller_ collectionViewAtPageIndex:0]];
111 void TearDown() override {
112 [owned_apps_grid_controller_ setDelegate:NULL];
113 owned_apps_grid_controller_.reset();
114 AppsGridControllerTestHelper::TearDown();
117 void ReplaceTestModel(int item_count) {
118 // Clear the delegate before reseting and destroying the model.
119 [owned_apps_grid_controller_ setDelegate:NULL];
121 owned_delegate_->ReplaceTestModel(item_count);
122 [owned_apps_grid_controller_ setDelegate:owned_delegate_.get()];
125 AppListTestModel* model() { return owned_delegate_->GetTestModel(); }
128 base::scoped_nsobject<AppsGridController> owned_apps_grid_controller_;
129 scoped_ptr<AppListTestViewDelegate> owned_delegate_;
131 DISALLOW_COPY_AND_ASSIGN(AppsGridControllerTest);
134 class AppListItemWithMenu : public AppListItem {
136 explicit AppListItemWithMenu(const std::string& id)
141 menu_model_.AddItem(0, base::UTF8ToUTF16("Menu For: " + id));
144 void SetMenuReadyForTesting(bool ready) {
148 ui::MenuModel* GetContextMenuModel() override {
156 ui::SimpleMenuModel menu_model_;
159 DISALLOW_COPY_AND_ASSIGN(AppListItemWithMenu);
162 // Generate a mouse event at the centre of the view in |page| with the given
163 // |index_in_page| that can be used to initiate, update and complete drag
165 NSEvent* MouseEventInCell(NSCollectionView* page, size_t index_in_page) {
166 NSRect cell_rect = [page frameForItemAtIndex:index_in_page];
167 NSPoint point_in_view = NSMakePoint(NSMidX(cell_rect), NSMidY(cell_rect));
168 NSPoint point_in_window = [page convertPoint:point_in_view
170 return cocoa_test_event_utils::LeftMouseDownAtPoint(point_in_window);
173 NSEvent* MouseEventForScroll(NSView* view, CGFloat relative_x) {
174 NSRect view_rect = [view frame];
175 NSPoint point_in_view = NSMakePoint(NSMidX(view_rect), NSMidY(view_rect));
176 point_in_view.x += point_in_view.x * relative_x;
177 NSPoint point_in_window = [view convertPoint:point_in_view
179 return cocoa_test_event_utils::LeftMouseDownAtPoint(point_in_window);
184 TEST_VIEW(AppsGridControllerTest, [apps_grid_controller_ view]);
186 // Test showing with an empty model.
187 TEST_F(AppsGridControllerTest, EmptyModelAndShow) {
188 EXPECT_TRUE([[apps_grid_controller_ view] superview]);
190 // First page should always exist, even if empty.
191 EXPECT_EQ(1u, [apps_grid_controller_ pageCount]);
192 EXPECT_EQ(0u, [[GetPageAt(0) content] count]);
193 EXPECT_TRUE([GetPageAt(0) superview]); // The pages container.
194 EXPECT_TRUE([[GetPageAt(0) superview] superview]);
197 // Test with a single item.
198 // This test is disabled in builders until the delay to wait for the collection
199 // view to load subviews can be removed, or some other solution is found.
200 TEST_F(AppsGridControllerTest, DISABLED_SingleEntryModel) {
201 // We need to "wake up" the NSCollectionView, otherwise it does not
202 // immediately update its subviews later in this function.
203 // When this test is run by itself, it's enough just to send a keypress (and
204 // this delay is not needed).
205 DelayForCollectionView();
206 EXPECT_EQ(1u, [apps_grid_controller_ pageCount]);
207 EXPECT_EQ(0u, [[GetPageAt(0) content] count]);
209 model()->PopulateApps(1);
211 EXPECT_FALSE([GetPageAt(0) animations]);
213 EXPECT_EQ(1u, [[GetPageAt(0) content] count]);
214 NSArray* subviews = [GetPageAt(0) subviews];
215 EXPECT_EQ(1u, [subviews count]);
217 // Note that using GetItemViewAt(0) here also works, and returns non-nil even
218 // without the delay, but a "click" on it does not register without the delay.
219 NSView* subview = [subviews objectAtIndex:0];
222 SimulateClick(subview);
224 EXPECT_EQ(1, model()->activate_count());
225 ASSERT_TRUE(model()->last_activated());
226 EXPECT_EQ(std::string("Item 0"), model()->last_activated()->name());
229 // Test activating an item on the second page (the 17th item).
230 TEST_F(AppsGridControllerTest, DISABLED_TwoPageModel) {
231 DelayForCollectionView();
232 ReplaceTestModel(kItemsPerPage * 2);
233 [apps_grid_controller_ scrollToPage:1];
235 // The NSScrollView animator ignores the duration configured on the
236 // NSAnimationContext (set by CocoaTest::Init), so we need to delay here.
237 DelayForCollectionView();
238 NSArray* subviews = [GetPageAt(1) subviews];
239 NSView* subview = [subviews objectAtIndex:0];
241 SimulateClick(subview);
243 EXPECT_EQ(1, model()->activate_count());
244 ASSERT_TRUE(model()->last_activated());
245 EXPECT_EQ(std::string("Item 16"), model()->last_activated()->name());
249 TEST_F(AppsGridControllerTest, ReplaceModel) {
250 const size_t kOrigItems = 1;
251 const size_t kNewItems = 2;
253 model()->PopulateApps(kOrigItems);
254 EXPECT_EQ(kOrigItems, [[GetPageAt(0) content] count]);
256 ReplaceTestModel(kNewItems);
257 EXPECT_EQ(kNewItems, [[GetPageAt(0) content] count]);
261 TEST_F(AppsGridControllerTest, Pagination) {
262 model()->PopulateApps(1);
263 EXPECT_EQ(1u, [apps_grid_controller_ pageCount]);
264 EXPECT_EQ(1u, [[GetPageAt(0) content] count]);
266 ReplaceTestModel(kItemsPerPage);
267 EXPECT_EQ(1u, [apps_grid_controller_ pageCount]);
268 EXPECT_EQ(kItemsPerPage, [[GetPageAt(0) content] count]);
270 // Test adding an item onto the next page.
271 model()->PopulateApps(1); // Now 17 items.
272 EXPECT_EQ(2u, [apps_grid_controller_ pageCount]);
273 EXPECT_EQ(kItemsPerPage, [[GetPageAt(0) content] count]);
274 EXPECT_EQ(1u, [[GetPageAt(1) content] count]);
276 // Test N pages with the last page having one empty spot.
277 const size_t kPagesToTest = 3;
278 ReplaceTestModel(kPagesToTest * kItemsPerPage - 1);
279 EXPECT_EQ(kPagesToTest, [apps_grid_controller_ pageCount]);
280 for (size_t page_index = 0; page_index < kPagesToTest - 1; ++page_index) {
281 EXPECT_EQ(kItemsPerPage, [[GetPageAt(page_index) content] count]);
283 EXPECT_EQ(kItemsPerPage - 1, [[GetPageAt(kPagesToTest - 1) content] count]);
285 // Test removing pages.
287 EXPECT_EQ(1u, [apps_grid_controller_ pageCount]);
288 EXPECT_EQ(1u, [[GetPageAt(0) content] count]);
291 // Tests basic keyboard navigation on the first page.
292 TEST_F(AppsGridControllerTest, FirstPageKeyboardNavigation) {
293 model()->PopulateApps(kItemsPerPage - 2);
294 EXPECT_EQ(kItemsPerPage - 2, [[GetPageAt(0) content] count]);
296 SimulateKeyAction(@selector(moveRight:));
297 EXPECT_EQ(0u, [apps_grid_controller_ selectedItemIndex]);
299 SimulateKeyAction(@selector(moveRight:));
300 EXPECT_EQ(1u, [apps_grid_controller_ selectedItemIndex]);
302 SimulateKeyAction(@selector(moveDown:));
303 EXPECT_EQ(5u, [apps_grid_controller_ selectedItemIndex]);
305 SimulateKeyAction(@selector(moveLeft:));
306 EXPECT_EQ(4u, [apps_grid_controller_ selectedItemIndex]);
308 SimulateKeyAction(@selector(moveUp:));
309 EXPECT_EQ(0u, [apps_grid_controller_ selectedItemIndex]);
311 // Go to the third item, and launch it.
312 SimulateKeyAction(@selector(moveRight:));
313 SimulateKeyAction(@selector(moveRight:));
314 EXPECT_EQ(2u, [apps_grid_controller_ selectedItemIndex]);
315 SimulateKeyAction(@selector(insertNewline:));
316 EXPECT_EQ(1, model()->activate_count());
317 ASSERT_TRUE(model()->last_activated());
318 EXPECT_EQ(std::string("Item 2"), model()->last_activated()->name());
321 // Tests keyboard navigation across pages.
322 TEST_F(AppsGridControllerTest, CrossPageKeyboardNavigation) {
323 model()->PopulateApps(kItemsPerPage + 10);
324 EXPECT_EQ(kItemsPerPage, [[GetPageAt(0) content] count]);
325 EXPECT_EQ(10u, [[GetPageAt(1) content] count]);
327 // Moving Left, Up, or PageUp from the top-left corner of the first page does
329 [apps_grid_controller_ selectItemAtIndex:0];
330 SimulateKeyAction(@selector(moveLeft:));
331 EXPECT_EQ(0u, [apps_grid_controller_ selectedItemIndex]);
332 SimulateKeyAction(@selector(moveUp:));
333 EXPECT_EQ(0u, [apps_grid_controller_ selectedItemIndex]);
334 SimulateKeyAction(@selector(scrollPageUp:));
335 EXPECT_EQ(0u, [apps_grid_controller_ selectedItemIndex]);
337 // Moving Right from the right side goes to the next page. Moving Left goes
338 // back to the first page.
339 [apps_grid_controller_ selectItemAtIndex:3];
340 SimulateKeyAction(@selector(moveRight:));
341 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
342 EXPECT_EQ(kItemsPerPage, [apps_grid_controller_ selectedItemIndex]);
343 SimulateKeyAction(@selector(moveLeft:));
344 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
345 EXPECT_EQ(3u, [apps_grid_controller_ selectedItemIndex]);
347 // Moving Down from the bottom does nothing.
348 [apps_grid_controller_ selectItemAtIndex:13];
349 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
350 SimulateKeyAction(@selector(moveDown:));
351 EXPECT_EQ(13u, [apps_grid_controller_ selectedItemIndex]);
353 // Moving Right into a non-existent square on the next page will select the
355 [apps_grid_controller_ selectItemAtIndex:15];
356 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
357 SimulateKeyAction(@selector(moveRight:));
358 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
359 EXPECT_EQ(kItemsPerPage + 9, [apps_grid_controller_ selectedItemIndex]);
361 // PageDown and PageUp switches pages while maintaining the same selection
363 [apps_grid_controller_ selectItemAtIndex:6];
364 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
365 SimulateKeyAction(@selector(scrollPageDown:));
366 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
367 EXPECT_EQ(kItemsPerPage + 6, [apps_grid_controller_ selectedItemIndex]);
368 SimulateKeyAction(@selector(scrollPageUp:));
369 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
370 EXPECT_EQ(6u, [apps_grid_controller_ selectedItemIndex]);
372 // PageDown into a non-existent square on the next page will select the last
374 [apps_grid_controller_ selectItemAtIndex:11];
375 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
376 SimulateKeyAction(@selector(scrollPageDown:));
377 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
378 EXPECT_EQ(kItemsPerPage + 9, [apps_grid_controller_ selectedItemIndex]);
380 // Moving Right, Down, or PageDown from the bottom-right corner of the last
381 // page (not the last item) does nothing.
382 [apps_grid_controller_ selectItemAtIndex:kItemsPerPage + 9];
383 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
384 SimulateKeyAction(@selector(moveRight:));
385 EXPECT_EQ(kItemsPerPage + 9, [apps_grid_controller_ selectedItemIndex]);
386 SimulateKeyAction(@selector(moveDown:));
387 EXPECT_EQ(kItemsPerPage + 9, [apps_grid_controller_ selectedItemIndex]);
388 SimulateKeyAction(@selector(scrollPageDown:));
389 EXPECT_EQ(kItemsPerPage + 9, [apps_grid_controller_ selectedItemIndex]);
391 // After page switch, arrow keys select first item on current page.
392 [apps_grid_controller_ scrollToPage:0];
393 [apps_grid_controller_ scrollToPage:1];
394 EXPECT_EQ(NSNotFound, [apps_grid_controller_ selectedItemIndex]);
395 SimulateKeyAction(@selector(moveUp:));
396 EXPECT_EQ(kItemsPerPage, [apps_grid_controller_ selectedItemIndex]);
399 // Highlighting an item should cause the page it's on to be visible.
400 TEST_F(AppsGridControllerTest, EnsureHighlightedVisible) {
401 model()->PopulateApps(3 * kItemsPerPage);
402 EXPECT_EQ(kItemsPerPage, [[GetPageAt(2) content] count]);
404 // First and last items of first page.
405 [apps_grid_controller_ selectItemAtIndex:0];
406 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
407 [apps_grid_controller_ selectItemAtIndex:kItemsPerPage - 1];
408 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
410 // First item of second page.
411 [apps_grid_controller_ selectItemAtIndex:kItemsPerPage + 1];
412 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
414 // Last item in model.
415 [apps_grid_controller_ selectItemAtIndex:3 * kItemsPerPage - 1];
416 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]);
419 // Test runtime updates: adding items, removing items, and moving items (e.g. in
420 // response to app install, uninstall, and chrome sync changes. Also test
421 // changing titles and icons.
422 TEST_F(AppsGridControllerTest, ModelUpdate) {
423 model()->PopulateApps(2);
424 EXPECT_EQ(2u, [[GetPageAt(0) content] count]);
425 EXPECT_EQ(std::string("|Item 0,Item 1|"), GetViewContent());
427 // Add an item (PopulateApps will create a new "Item 2").
428 model()->PopulateApps(1);
429 EXPECT_EQ(3u, [[GetPageAt(0) content] count]);
430 NSButton* button = GetItemViewAt(2);
431 EXPECT_NSEQ(@"Item 2", [button title]);
432 EXPECT_EQ(std::string("|Item 0,Item 1,Item 2|"), GetViewContent());
434 // Update the title via the ItemModelObserver.
435 app_list::AppListItem* item_model =
436 model()->top_level_item_list()->item_at(2);
437 model()->SetItemName(item_model, "UpdatedItem");
438 EXPECT_NSEQ(@"UpdatedItem", [button title]);
440 // Test icon updates through the model observer by ensuring the icon changes.
441 item_model->SetIcon(gfx::ImageSkia());
442 NSSize icon_size = [[button image] size];
443 EXPECT_EQ(0, icon_size.width);
444 EXPECT_EQ(0, icon_size.height);
447 const int kTestImageSize = 10;
448 const int kTargetImageSize = 48;
449 bitmap.setInfo(SkImageInfo::MakeN32Premul(kTestImageSize, kTestImageSize));
450 item_model->SetIcon(gfx::ImageSkia::CreateFrom1xBitmap(bitmap));
451 icon_size = [[button image] size];
452 // Icon should always be resized to 48x48.
453 EXPECT_EQ(kTargetImageSize, icon_size.width);
454 EXPECT_EQ(kTargetImageSize, icon_size.height);
457 TEST_F(AppsGridControllerTest, ModelAdd) {
458 model()->PopulateApps(2);
459 EXPECT_EQ(2u, [[GetPageAt(0) content] count]);
460 EXPECT_EQ(std::string("|Item 0,Item 1|"), GetViewContent());
462 app_list::AppListItemList* item_list = model()->top_level_item_list();
464 model()->CreateAndAddItem("Item 2");
465 ASSERT_EQ(3u, item_list->item_count());
466 EXPECT_EQ(3u, [apps_grid_controller_ itemCount]);
467 EXPECT_EQ(std::string("|Item 0,Item 1,Item 2|"), GetViewContent());
469 // Test adding an item whose position is in the middle.
470 app_list::AppListItem* item0 = item_list->item_at(0);
471 app_list::AppListItem* item1 = item_list->item_at(1);
472 app_list::AppListItem* item3 = model()->CreateItem("Item Three");
473 model()->AddItem(item3);
474 item_list->SetItemPosition(
475 item3, item0->position().CreateBetween(item1->position()));
476 EXPECT_EQ(4u, [apps_grid_controller_ itemCount]);
477 EXPECT_EQ(std::string("|Item 0,Item Three,Item 1,Item 2|"), GetViewContent());
480 TEST_F(AppsGridControllerTest, ModelMove) {
481 model()->PopulateApps(3);
482 EXPECT_EQ(3u, [[GetPageAt(0) content] count]);
483 EXPECT_EQ(std::string("|Item 0,Item 1,Item 2|"), GetViewContent());
485 // Test swapping items (e.g. rearranging via sync).
486 model()->top_level_item_list()->MoveItem(1, 2);
487 EXPECT_EQ(std::string("|Item 0,Item 2,Item 1|"), GetViewContent());
490 TEST_F(AppsGridControllerTest, ModelRemove) {
491 model()->PopulateApps(3);
492 EXPECT_EQ(3u, [[GetPageAt(0) content] count]);
493 EXPECT_EQ(std::string("|Item 0,Item 1,Item 2|"), GetViewContent());
495 // Test removing an item at the end.
496 model()->DeleteItem("Item 2");
497 EXPECT_EQ(2u, [apps_grid_controller_ itemCount]);
498 EXPECT_EQ(std::string("|Item 0,Item 1|"), GetViewContent());
500 // Test removing in the middle.
501 model()->CreateAndAddItem("Item 2");
502 EXPECT_EQ(3u, [apps_grid_controller_ itemCount]);
503 EXPECT_EQ(std::string("|Item 0,Item 1,Item 2|"), GetViewContent());
504 model()->DeleteItem("Item 1");
505 EXPECT_EQ(2u, [apps_grid_controller_ itemCount]);
506 EXPECT_EQ(std::string("|Item 0,Item 2|"), GetViewContent());
509 TEST_F(AppsGridControllerTest, ModelRemoveSeveral) {
510 model()->PopulateApps(3);
511 EXPECT_EQ(3u, [[GetPageAt(0) content] count]);
512 EXPECT_EQ(std::string("|Item 0,Item 1,Item 2|"), GetViewContent());
514 // Test removing multiple items via the model.
515 model()->DeleteItem("Item 0");
516 model()->DeleteItem("Item 1");
517 model()->DeleteItem("Item 2");
518 EXPECT_EQ(0u, [apps_grid_controller_ itemCount]);
519 EXPECT_EQ(std::string("||"), GetViewContent());
522 TEST_F(AppsGridControllerTest, ModelRemovePage) {
523 app_list::AppListItemList* item_list = model()->top_level_item_list();
525 model()->PopulateApps(kItemsPerPage + 1);
526 ASSERT_EQ(kItemsPerPage + 1, item_list->item_count());
527 EXPECT_EQ(kItemsPerPage + 1, [apps_grid_controller_ itemCount]);
528 EXPECT_EQ(2u, [apps_grid_controller_ pageCount]);
530 // Test removing the last item when there is one item on the second page.
531 app_list::AppListItem* last_item = item_list->item_at(kItemsPerPage);
532 model()->DeleteItem(last_item->id());
533 EXPECT_EQ(kItemsPerPage, item_list->item_count());
534 EXPECT_EQ(kItemsPerPage, [apps_grid_controller_ itemCount]);
535 EXPECT_EQ(1u, [apps_grid_controller_ pageCount]);
538 // Test install progress bars, and install flow with item highlighting.
539 TEST_F(AppsGridControllerTest, ItemInstallProgress) {
540 ReplaceTestModel(kItemsPerPage + 1);
541 EXPECT_EQ(2u, [apps_grid_controller_ pageCount]);
542 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
543 // The single item on the second page.
544 int kTestItemIndex = kItemsPerPage;
545 app_list::AppListItem* item_model =
546 model()->top_level_item_list()->item_at(kTestItemIndex);
548 // Highlighting an item should activate the page it is on.
549 model()->HighlightItemAt(kTestItemIndex);
550 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
552 // Starting install should add a progress bar, and temporarily clear the
554 NSButton* button = GetItemViewAt(kTestItemIndex);
555 NSView* containerView = [button superview];
556 EXPECT_EQ(1u, [[containerView subviews] count]);
557 EXPECT_NSEQ(@"Item 16", [button title]);
558 model()->HighlightItemAt(kTestItemIndex);
559 item_model->SetIsInstalling(true);
560 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
562 EXPECT_EQ(2u, [[containerView subviews] count]);
563 EXPECT_NSEQ(@"", [button title]);
564 NSProgressIndicator* progressIndicator =
565 [[containerView subviews] objectAtIndex:1];
566 EXPECT_FALSE([progressIndicator isIndeterminate]);
567 EXPECT_EQ(0.0, [progressIndicator doubleValue]);
569 // Updating the progress in the model should update the progress bar.
570 item_model->SetPercentDownloaded(50);
571 EXPECT_EQ(50.0, [progressIndicator doubleValue]);
573 // Two things can be installing simultaneously. When one starts or completes
574 // the model builder will ask for the item to be highlighted.
575 const int kAlternateTestItemIndex = 0;
576 model()->HighlightItemAt(kAlternateTestItemIndex);
577 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
579 // Update the first item (page doesn't change on updates).
580 item_model->SetPercentDownloaded(100);
581 EXPECT_EQ(100.0, [progressIndicator doubleValue]);
582 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
584 // A percent of -1 indicates the download is complete and the unpack/install
585 // process has started.
586 item_model->SetPercentDownloaded(-1);
587 EXPECT_TRUE([progressIndicator isIndeterminate]);
589 // Completing install removes the progress bar, and restores the title.
590 // ExtensionAppModelBuilder will reload the ExtensionAppItem, which also
591 // highlights. Do the same here.
592 model()->HighlightItemAt(kTestItemIndex);
593 item_model->SetIsInstalling(false);
594 EXPECT_EQ(1u, [[containerView subviews] count]);
595 EXPECT_NSEQ(@"Item 16", [button title]);
596 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
598 // Things should cleanup OK with |alternate_item_model| left installing.
601 // Test mouseover selection.
602 TEST_F(AppsGridControllerTest, MouseoverSelects) {
603 model()->PopulateApps(2);
604 EXPECT_EQ(nil, GetSelectedView());
606 // Test entering and exiting the first item.
607 SimulateMouseEnterItemAt(0);
608 EXPECT_EQ(GetItemViewAt(0), GetSelectedView());
609 SimulateMouseExitItemAt(0);
610 EXPECT_EQ(nil, GetSelectedView());
612 // AppKit doesn't guarantee the order, so test moving between items.
613 SimulateMouseEnterItemAt(0);
614 EXPECT_EQ(GetItemViewAt(0), GetSelectedView());
615 SimulateMouseEnterItemAt(1);
616 EXPECT_EQ(GetItemViewAt(1), GetSelectedView());
617 SimulateMouseExitItemAt(0);
618 EXPECT_EQ(GetItemViewAt(1), GetSelectedView());
619 SimulateMouseExitItemAt(1);
620 EXPECT_EQ(nil, GetSelectedView());
623 // Test AppsGridPaginationObserver totalPagesChanged().
624 TEST_F(AppsGridControllerTest, PaginationObserverPagesChanged) {
625 base::scoped_nsobject<TestPaginationObserver> observer(
626 [[TestPaginationObserver alloc] init]);
627 [apps_grid_controller_ setPaginationObserver:observer];
629 // Test totalPagesChanged.
630 model()->PopulateApps(kItemsPerPage);
631 EXPECT_EQ(0, [observer totalPagesChangedCount]);
632 EXPECT_EQ(1u, [apps_grid_controller_ pageCount]);
633 model()->PopulateApps(1);
634 EXPECT_EQ(1, [observer totalPagesChangedCount]);
635 EXPECT_EQ(2u, [apps_grid_controller_ pageCount]);
637 EXPECT_EQ(2, [observer totalPagesChangedCount]);
638 EXPECT_EQ(1u, [apps_grid_controller_ pageCount]);
639 ReplaceTestModel(kItemsPerPage * 3 + 1);
640 EXPECT_EQ(3, [observer totalPagesChangedCount]);
641 EXPECT_EQ(4u, [apps_grid_controller_ pageCount]);
643 EXPECT_FALSE([observer readVisibilityDidChange]);
644 EXPECT_EQ(0, [observer selectedPageChangedCount]);
646 [apps_grid_controller_ setPaginationObserver:nil];
649 // Test AppsGridPaginationObserver selectedPageChanged().
650 TEST_F(AppsGridControllerTest, PaginationObserverSelectedPageChanged) {
651 base::scoped_nsobject<TestPaginationObserver> observer(
652 [[TestPaginationObserver alloc] init]);
653 [apps_grid_controller_ setPaginationObserver:observer];
654 EXPECT_EQ(0, [[NSAnimationContext currentContext] duration]);
656 ReplaceTestModel(kItemsPerPage * 3 + 1);
657 EXPECT_EQ(1, [observer totalPagesChangedCount]);
658 EXPECT_EQ(4u, [apps_grid_controller_ pageCount]);
660 EXPECT_FALSE([observer readVisibilityDidChange]);
661 EXPECT_EQ(0, [observer selectedPageChangedCount]);
663 [apps_grid_controller_ scrollToPage:1];
664 EXPECT_EQ(1, [observer selectedPageChangedCount]);
665 EXPECT_EQ(1, [observer lastNewSelectedPage]);
666 EXPECT_TRUE([observer readVisibilityDidChange]);
667 EXPECT_FALSE([observer readVisibilityDidChange]); // Testing test behaviour.
668 EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:0]);
669 EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:1]);
670 EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:2]);
671 EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:3]);
673 [apps_grid_controller_ scrollToPage:0];
674 EXPECT_EQ(2, [observer selectedPageChangedCount]);
675 EXPECT_EQ(0, [observer lastNewSelectedPage]);
676 EXPECT_TRUE([observer readVisibilityDidChange]);
677 EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:0]);
678 EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:1]);
680 [apps_grid_controller_ scrollToPage:3];
681 // Note: with no animations, there is only a single page change. However, with
682 // animations we expect multiple updates depending on the rate that the scroll
683 // view updates and sends out NSViewBoundsDidChangeNotification.
684 EXPECT_EQ(3, [observer selectedPageChangedCount]);
685 EXPECT_EQ(3, [observer lastNewSelectedPage]);
686 EXPECT_TRUE([observer readVisibilityDidChange]);
687 EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:0]);
688 EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:3]);
690 [apps_grid_controller_ setPaginationObserver:nil];
693 // Test basic item moves with two items; swapping them around, dragging outside
694 // of the view bounds, and dragging on the background.
695 TEST_F(AppsGridControllerTest, DragAndDropSimple) {
696 model()->PopulateApps(2);
697 NSCollectionView* page = [apps_grid_controller_ collectionViewAtPageIndex:0];
698 NSEvent* mouse_at_cell_0 = MouseEventInCell(page, 0);
699 NSEvent* mouse_at_cell_1 = MouseEventInCell(page, 1);
700 NSEvent* mouse_at_page_centre = MouseEventInCell(page, 6);
701 NSEvent* mouse_off_page = MouseEventInCell(page, kItemsPerPage * 2);
703 const std::string kOrdered = "Item 0,Item 1";
704 const std::string kSwapped = "Item 1,Item 0";
705 const std::string kOrderedView = "|Item 0,Item 1|";
706 const std::string kSwappedView = "|Item 1,Item 0|";
708 EXPECT_EQ(kOrdered, model()->GetModelContent());
709 EXPECT_EQ(kOrderedView, GetViewContent());
710 AppsCollectionViewDragManager* drag_manager =
711 [apps_grid_controller_ dragManager];
713 // Drag first item over the second item and release.
714 [drag_manager onMouseDownInPage:page
715 withEvent:mouse_at_cell_0];
716 [drag_manager onMouseDragged:mouse_at_cell_1];
717 EXPECT_EQ(kOrdered, model()->GetModelContent());
718 EXPECT_EQ(kSwappedView, GetViewContent()); // View swaps first.
719 [drag_manager onMouseUp:mouse_at_cell_1];
720 EXPECT_EQ(kSwapped, model()->GetModelContent());
721 EXPECT_EQ(kSwappedView, GetViewContent());
724 [drag_manager onMouseDownInPage:page
725 withEvent:mouse_at_cell_1];
726 [drag_manager onMouseDragged:mouse_at_cell_0];
727 EXPECT_EQ(kSwapped, model()->GetModelContent());
728 EXPECT_EQ(kOrderedView, GetViewContent());
729 [drag_manager onMouseUp:mouse_at_cell_0];
730 EXPECT_EQ(kOrdered, model()->GetModelContent());
731 EXPECT_EQ(kOrderedView, GetViewContent());
733 // Drag first item to centre of view (should put in last place).
734 [drag_manager onMouseDownInPage:page
735 withEvent:mouse_at_cell_0];
736 [drag_manager onMouseDragged:mouse_at_page_centre];
737 EXPECT_EQ(kOrdered, model()->GetModelContent());
738 EXPECT_EQ(kSwappedView, GetViewContent());
739 [drag_manager onMouseUp:mouse_at_page_centre];
740 EXPECT_EQ(kSwapped, model()->GetModelContent());
741 EXPECT_EQ(kSwappedView, GetViewContent());
743 // Drag item to centre again (should leave it in the last place).
744 [drag_manager onMouseDownInPage:page
745 withEvent:mouse_at_cell_1];
746 [drag_manager onMouseDragged:mouse_at_page_centre];
747 EXPECT_EQ(kSwapped, model()->GetModelContent());
748 EXPECT_EQ(kSwappedView, GetViewContent());
749 [drag_manager onMouseUp:mouse_at_page_centre];
750 EXPECT_EQ(kSwapped, model()->GetModelContent());
751 EXPECT_EQ(kSwappedView, GetViewContent());
753 // Drag starting in the centre of the view, should do nothing.
754 [drag_manager onMouseDownInPage:page
755 withEvent:mouse_at_page_centre];
756 [drag_manager onMouseDragged:mouse_at_cell_0];
757 EXPECT_EQ(kSwapped, model()->GetModelContent());
758 EXPECT_EQ(kSwappedView, GetViewContent());
759 [drag_manager onMouseUp:mouse_at_cell_0];
760 EXPECT_EQ(kSwapped, model()->GetModelContent());
761 EXPECT_EQ(kSwappedView, GetViewContent());
764 [drag_manager onMouseDownInPage:page
765 withEvent:mouse_off_page];
766 [drag_manager onMouseDragged:mouse_at_cell_0];
767 EXPECT_EQ(kSwapped, model()->GetModelContent());
768 EXPECT_EQ(kSwappedView, GetViewContent());
769 [drag_manager onMouseUp:mouse_at_cell_0];
770 EXPECT_EQ(kSwapped, model()->GetModelContent());
771 EXPECT_EQ(kSwappedView, GetViewContent());
773 // Drag to first over second item, then off page.
774 [drag_manager onMouseDownInPage:page
775 withEvent:mouse_at_cell_0];
776 [drag_manager onMouseDragged:mouse_at_cell_1];
777 EXPECT_EQ(kSwapped, model()->GetModelContent());
778 EXPECT_EQ(kOrderedView, GetViewContent());
779 [drag_manager onMouseDragged:mouse_off_page];
780 EXPECT_EQ(kSwapped, model()->GetModelContent());
781 EXPECT_EQ(kOrderedView, GetViewContent());
782 [drag_manager onMouseUp:mouse_off_page];
783 EXPECT_EQ(kOrdered, model()->GetModelContent());
784 EXPECT_EQ(kOrderedView, GetViewContent());
786 // Replace with an empty model, and ensure we do not break.
788 EXPECT_EQ(std::string(), model()->GetModelContent());
789 EXPECT_EQ(std::string("||"), GetViewContent());
790 [drag_manager onMouseDownInPage:page
791 withEvent:mouse_at_cell_0];
792 [drag_manager onMouseDragged:mouse_at_cell_1];
793 [drag_manager onMouseUp:mouse_at_cell_1];
794 EXPECT_EQ(std::string(), model()->GetModelContent());
795 EXPECT_EQ(std::string("||"), GetViewContent());
798 // Test item moves between pages.
799 TEST_F(AppsGridControllerTest, DragAndDropMultiPage) {
800 const size_t kPagesToTest = 3;
801 // Put one item on the last page to hit more edge cases.
802 ReplaceTestModel(kItemsPerPage * (kPagesToTest - 1) + 1);
803 NSCollectionView* page[kPagesToTest];
804 for (size_t i = 0; i < kPagesToTest; ++i)
805 page[i] = [apps_grid_controller_ collectionViewAtPageIndex:i];
807 const std::string kSecondItemMovedToSecondPage =
808 "|Item 0,Item 2,Item 3,Item 4,Item 5,Item 6,Item 7,Item 8,"
809 "Item 9,Item 10,Item 11,Item 12,Item 13,Item 14,Item 15,Item 16|"
810 "|Item 17,Item 1,Item 18,Item 19,Item 20,Item 21,Item 22,Item 23,"
811 "Item 24,Item 25,Item 26,Item 27,Item 28,Item 29,Item 30,Item 31|"
814 NSEvent* mouse_at_cell_0 = MouseEventInCell(page[0], 0);
815 NSEvent* mouse_at_cell_1 = MouseEventInCell(page[0], 1);
816 AppsCollectionViewDragManager* drag_manager =
817 [apps_grid_controller_ dragManager];
818 [drag_manager onMouseDownInPage:page[0]
819 withEvent:mouse_at_cell_1];
821 // Initiate dragging before changing pages.
822 [drag_manager onMouseDragged:mouse_at_cell_0];
824 // Scroll to the second page.
825 [apps_grid_controller_ scrollToPage:1];
826 [drag_manager onMouseDragged:mouse_at_cell_1];
828 // Do one exhaustive check, and then spot-check corner cases.
829 EXPECT_EQ(kSecondItemMovedToSecondPage, GetViewContent());
830 EXPECT_EQ(0u, GetPageIndexForItem(0));
831 EXPECT_EQ(1u, GetPageIndexForItem(1));
832 EXPECT_EQ(0u, GetPageIndexForItem(2));
833 EXPECT_EQ(0u, GetPageIndexForItem(16));
834 EXPECT_EQ(1u, GetPageIndexForItem(17));
835 EXPECT_EQ(1u, GetPageIndexForItem(31));
836 EXPECT_EQ(2u, GetPageIndexForItem(32));
838 // Scroll to the third page and drag some more.
839 [apps_grid_controller_ scrollToPage:2];
840 [drag_manager onMouseDragged:mouse_at_cell_1];
841 EXPECT_EQ(2u, GetPageIndexForItem(1));
842 EXPECT_EQ(1u, GetPageIndexForItem(31));
843 EXPECT_EQ(1u, GetPageIndexForItem(32));
846 [apps_grid_controller_ scrollToPage:1];
847 [drag_manager onMouseDragged:mouse_at_cell_1];
848 EXPECT_EQ(kSecondItemMovedToSecondPage, GetViewContent());
849 EXPECT_EQ(1u, GetPageIndexForItem(1));
850 EXPECT_EQ(1u, GetPageIndexForItem(31));
851 EXPECT_EQ(2u, GetPageIndexForItem(32));
853 // Simulate installing an item while dragging (or have it appear during sync).
854 model()->PopulateAppWithId(33);
855 // Item should go back to its position before the drag.
856 EXPECT_EQ(0u, GetPageIndexForItem(1));
857 EXPECT_EQ(1u, GetPageIndexForItem(31));
858 EXPECT_EQ(2u, GetPageIndexForItem(32));
859 // New item should appear at end.
860 EXPECT_EQ(2u, GetPageIndexForItem(33));
862 // Scroll to end again, and keep dragging (should be ignored).
863 [apps_grid_controller_ scrollToPage:2];
864 [drag_manager onMouseDragged:mouse_at_cell_0];
865 EXPECT_EQ(0u, GetPageIndexForItem(1));
866 [drag_manager onMouseUp:mouse_at_cell_0];
867 EXPECT_EQ(0u, GetPageIndexForItem(1));
870 // Test scrolling when dragging past edge or over the pager.
871 TEST_F(AppsGridControllerTest, ScrollingWhileDragging) {
872 base::scoped_nsobject<TestPaginationObserver> observer(
873 [[TestPaginationObserver alloc] init]);
874 [apps_grid_controller_ setPaginationObserver:observer];
876 ReplaceTestModel(kItemsPerPage * 3);
877 // Start on the middle page.
878 [apps_grid_controller_ scrollToPage:1];
879 NSCollectionView* page = [apps_grid_controller_ collectionViewAtPageIndex:1];
880 NSEvent* mouse_at_cell_0 = MouseEventInCell(page, 0);
882 NSEvent* at_center = MouseEventForScroll([apps_grid_controller_ view], 0.0);
883 NSEvent* at_left = MouseEventForScroll([apps_grid_controller_ view], -1.1);
884 NSEvent* at_right = MouseEventForScroll([apps_grid_controller_ view], 1.1);
886 AppsCollectionViewDragManager* drag_manager =
887 [apps_grid_controller_ dragManager];
888 [drag_manager onMouseDownInPage:page
889 withEvent:mouse_at_cell_0];
890 [drag_manager onMouseDragged:at_center];
892 // Nothing should be scheduled: target page is visible page.
893 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
894 EXPECT_EQ(1u, [apps_grid_controller_ scheduledScrollPage]);
896 // Drag to the left, should go to first page and no further.
897 [drag_manager onMouseDragged:at_left];
898 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
899 EXPECT_EQ(0u, [apps_grid_controller_ scheduledScrollPage]);
900 [apps_grid_controller_ scrollToPage:0]; // Commit without timer for testing.
901 [drag_manager onMouseDragged:at_left];
902 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
903 EXPECT_EQ(0u, [apps_grid_controller_ scheduledScrollPage]);
905 // Drag to the right, should go to last page and no futher.
906 [drag_manager onMouseDragged:at_right];
907 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
908 EXPECT_EQ(1u, [apps_grid_controller_ scheduledScrollPage]);
909 [apps_grid_controller_ scrollToPage:1];
910 [drag_manager onMouseDragged:at_right];
911 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
912 EXPECT_EQ(2u, [apps_grid_controller_ scheduledScrollPage]);
913 [apps_grid_controller_ scrollToPage:2];
914 [drag_manager onMouseDragged:at_right];
915 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]);
916 EXPECT_EQ(2u, [apps_grid_controller_ scheduledScrollPage]);
918 // Simulate a hover over the first pager segment.
919 [observer setHoveredSegmentForTest:0];
920 [drag_manager onMouseDragged:at_center];
921 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]);
922 EXPECT_EQ(0u, [apps_grid_controller_ scheduledScrollPage]);
924 // Drag it back, should cancel schedule.
925 [observer setHoveredSegmentForTest:-1];
926 [drag_manager onMouseDragged:at_center];
927 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]);
928 EXPECT_EQ(2u, [apps_grid_controller_ scheduledScrollPage]);
930 // Hover again, now over middle segment, and ensure a release also cancels.
931 [observer setHoveredSegmentForTest:1];
932 [drag_manager onMouseDragged:at_center];
933 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]);
934 EXPECT_EQ(1u, [apps_grid_controller_ scheduledScrollPage]);
935 [drag_manager onMouseUp:at_center];
936 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]);
937 EXPECT_EQ(2u, [apps_grid_controller_ scheduledScrollPage]);
939 [apps_grid_controller_ setPaginationObserver:nil];
942 TEST_F(AppsGridControllerTest, ContextMenus) {
943 AppListItemWithMenu* item_two_model = new AppListItemWithMenu("Item Two");
944 model()->AddItem(new AppListItemWithMenu("Item One"));
945 model()->AddItem(item_two_model);
946 EXPECT_EQ(2u, [apps_grid_controller_ itemCount]);
948 NSCollectionView* page = [apps_grid_controller_ collectionViewAtPageIndex:0];
949 NSEvent* mouse_at_cell_0 = MouseEventInCell(page, 0);
950 NSEvent* mouse_at_cell_1 = MouseEventInCell(page, 1);
952 NSMenu* menu = [page menuForEvent:mouse_at_cell_0];
953 EXPECT_EQ(1, [menu numberOfItems]);
954 EXPECT_NSEQ(@"Menu For: Item One", [[menu itemAtIndex:0] title]);
956 // Test a context menu request while the item is still installing.
957 item_two_model->SetMenuReadyForTesting(false);
958 menu = [page menuForEvent:mouse_at_cell_1];
959 EXPECT_EQ(nil, menu);
961 item_two_model->SetMenuReadyForTesting(true);
962 menu = [page menuForEvent:mouse_at_cell_1];
963 EXPECT_EQ(1, [menu numberOfItems]);
964 EXPECT_NSEQ(@"Menu For: Item Two", [[menu itemAtIndex:0] title]);
966 // Test that a button being held down with the left button does not also show
968 [GetItemViewAt(0) highlight:YES];
969 EXPECT_FALSE([page menuForEvent:mouse_at_cell_0]);
970 [GetItemViewAt(0) highlight:NO];
971 EXPECT_TRUE([page menuForEvent:mouse_at_cell_0]);
975 } // namespace app_list