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 that selecting an item changes the text color correctly.
292 TEST_F(AppsGridControllerTest, SelectionChangesTextColor) {
293 model()->PopulateApps(2);
294 [apps_grid_controller_ selectItemAtIndex:0];
295 EXPECT_NSEQ(ButtonTitleColorAt(0),
296 gfx::SkColorToSRGBNSColor(app_list::kGridTitleHoverColor));
297 EXPECT_NSEQ(ButtonTitleColorAt(1),
298 gfx::SkColorToSRGBNSColor(app_list::kGridTitleColor));
300 [apps_grid_controller_ selectItemAtIndex:1];
301 EXPECT_NSEQ(ButtonTitleColorAt(0),
302 gfx::SkColorToSRGBNSColor(app_list::kGridTitleColor));
303 EXPECT_NSEQ(ButtonTitleColorAt(1),
304 gfx::SkColorToSRGBNSColor(app_list::kGridTitleHoverColor));
307 // Tests basic keyboard navigation on the first page.
308 TEST_F(AppsGridControllerTest, FirstPageKeyboardNavigation) {
309 model()->PopulateApps(kItemsPerPage - 2);
310 EXPECT_EQ(kItemsPerPage - 2, [[GetPageAt(0) content] count]);
312 SimulateKeyAction(@selector(moveRight:));
313 EXPECT_EQ(0u, [apps_grid_controller_ selectedItemIndex]);
315 SimulateKeyAction(@selector(moveRight:));
316 EXPECT_EQ(1u, [apps_grid_controller_ selectedItemIndex]);
318 SimulateKeyAction(@selector(moveDown:));
319 EXPECT_EQ(5u, [apps_grid_controller_ selectedItemIndex]);
321 SimulateKeyAction(@selector(moveLeft:));
322 EXPECT_EQ(4u, [apps_grid_controller_ selectedItemIndex]);
324 SimulateKeyAction(@selector(moveUp:));
325 EXPECT_EQ(0u, [apps_grid_controller_ selectedItemIndex]);
327 // Go to the third item, and launch it.
328 SimulateKeyAction(@selector(moveRight:));
329 SimulateKeyAction(@selector(moveRight:));
330 EXPECT_EQ(2u, [apps_grid_controller_ selectedItemIndex]);
331 SimulateKeyAction(@selector(insertNewline:));
332 EXPECT_EQ(1, model()->activate_count());
333 ASSERT_TRUE(model()->last_activated());
334 EXPECT_EQ(std::string("Item 2"), model()->last_activated()->name());
337 // Tests keyboard navigation across pages.
338 TEST_F(AppsGridControllerTest, CrossPageKeyboardNavigation) {
339 model()->PopulateApps(kItemsPerPage + 10);
340 EXPECT_EQ(kItemsPerPage, [[GetPageAt(0) content] count]);
341 EXPECT_EQ(10u, [[GetPageAt(1) content] count]);
343 // Moving Left, Up, or PageUp from the top-left corner of the first page does
345 [apps_grid_controller_ selectItemAtIndex:0];
346 SimulateKeyAction(@selector(moveLeft:));
347 EXPECT_EQ(0u, [apps_grid_controller_ selectedItemIndex]);
348 SimulateKeyAction(@selector(moveUp:));
349 EXPECT_EQ(0u, [apps_grid_controller_ selectedItemIndex]);
350 SimulateKeyAction(@selector(scrollPageUp:));
351 EXPECT_EQ(0u, [apps_grid_controller_ selectedItemIndex]);
353 // Moving Right from the right side goes to the next page. Moving Left goes
354 // back to the first page.
355 [apps_grid_controller_ selectItemAtIndex:3];
356 SimulateKeyAction(@selector(moveRight:));
357 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
358 EXPECT_EQ(kItemsPerPage, [apps_grid_controller_ selectedItemIndex]);
359 SimulateKeyAction(@selector(moveLeft:));
360 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
361 EXPECT_EQ(3u, [apps_grid_controller_ selectedItemIndex]);
363 // Moving Down from the bottom does nothing.
364 [apps_grid_controller_ selectItemAtIndex:13];
365 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
366 SimulateKeyAction(@selector(moveDown:));
367 EXPECT_EQ(13u, [apps_grid_controller_ selectedItemIndex]);
369 // Moving Right into a non-existent square on the next page will select the
371 [apps_grid_controller_ selectItemAtIndex:15];
372 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
373 SimulateKeyAction(@selector(moveRight:));
374 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
375 EXPECT_EQ(kItemsPerPage + 9, [apps_grid_controller_ selectedItemIndex]);
377 // PageDown and PageUp switches pages while maintaining the same selection
379 [apps_grid_controller_ selectItemAtIndex:6];
380 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
381 SimulateKeyAction(@selector(scrollPageDown:));
382 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
383 EXPECT_EQ(kItemsPerPage + 6, [apps_grid_controller_ selectedItemIndex]);
384 SimulateKeyAction(@selector(scrollPageUp:));
385 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
386 EXPECT_EQ(6u, [apps_grid_controller_ selectedItemIndex]);
388 // PageDown into a non-existent square on the next page will select the last
390 [apps_grid_controller_ selectItemAtIndex:11];
391 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
392 SimulateKeyAction(@selector(scrollPageDown:));
393 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
394 EXPECT_EQ(kItemsPerPage + 9, [apps_grid_controller_ selectedItemIndex]);
396 // Moving Right, Down, or PageDown from the bottom-right corner of the last
397 // page (not the last item) does nothing.
398 [apps_grid_controller_ selectItemAtIndex:kItemsPerPage + 9];
399 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
400 SimulateKeyAction(@selector(moveRight:));
401 EXPECT_EQ(kItemsPerPage + 9, [apps_grid_controller_ selectedItemIndex]);
402 SimulateKeyAction(@selector(moveDown:));
403 EXPECT_EQ(kItemsPerPage + 9, [apps_grid_controller_ selectedItemIndex]);
404 SimulateKeyAction(@selector(scrollPageDown:));
405 EXPECT_EQ(kItemsPerPage + 9, [apps_grid_controller_ selectedItemIndex]);
407 // After page switch, arrow keys select first item on current page.
408 [apps_grid_controller_ scrollToPage:0];
409 [apps_grid_controller_ scrollToPage:1];
410 EXPECT_EQ(NSNotFound, [apps_grid_controller_ selectedItemIndex]);
411 SimulateKeyAction(@selector(moveUp:));
412 EXPECT_EQ(kItemsPerPage, [apps_grid_controller_ selectedItemIndex]);
415 // Highlighting an item should cause the page it's on to be visible.
416 TEST_F(AppsGridControllerTest, EnsureHighlightedVisible) {
417 model()->PopulateApps(3 * kItemsPerPage);
418 EXPECT_EQ(kItemsPerPage, [[GetPageAt(2) content] count]);
420 // First and last items of first page.
421 [apps_grid_controller_ selectItemAtIndex:0];
422 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
423 [apps_grid_controller_ selectItemAtIndex:kItemsPerPage - 1];
424 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
426 // First item of second page.
427 [apps_grid_controller_ selectItemAtIndex:kItemsPerPage + 1];
428 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
430 // Last item in model.
431 [apps_grid_controller_ selectItemAtIndex:3 * kItemsPerPage - 1];
432 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]);
435 // Test runtime updates: adding items, removing items, and moving items (e.g. in
436 // response to app install, uninstall, and chrome sync changes. Also test
437 // changing titles and icons.
438 TEST_F(AppsGridControllerTest, ModelUpdate) {
439 model()->PopulateApps(2);
440 EXPECT_EQ(2u, [[GetPageAt(0) content] count]);
441 EXPECT_EQ(std::string("|Item 0,Item 1|"), GetViewContent());
443 // Add an item (PopulateApps will create a new "Item 2").
444 model()->PopulateApps(1);
445 EXPECT_EQ(3u, [[GetPageAt(0) content] count]);
446 NSButton* button = GetItemViewAt(2);
447 EXPECT_NSEQ(@"Item 2", [button title]);
448 EXPECT_EQ(std::string("|Item 0,Item 1,Item 2|"), GetViewContent());
450 // Update the title via the ItemModelObserver.
451 app_list::AppListItem* item_model =
452 model()->top_level_item_list()->item_at(2);
453 model()->SetItemName(item_model, "UpdatedItem");
454 EXPECT_NSEQ(@"UpdatedItem", [button title]);
456 // Test icon updates through the model observer by ensuring the icon changes.
457 item_model->SetIcon(gfx::ImageSkia(), false);
458 NSSize icon_size = [[button image] size];
459 EXPECT_EQ(0, icon_size.width);
460 EXPECT_EQ(0, icon_size.height);
463 const int kTestImageSize = 10;
464 const int kTargetImageSize = 48;
465 bitmap.setInfo(SkImageInfo::MakeN32Premul(kTestImageSize, kTestImageSize));
466 item_model->SetIcon(gfx::ImageSkia::CreateFrom1xBitmap(bitmap), false);
467 icon_size = [[button image] size];
468 // Icon should always be resized to 48x48.
469 EXPECT_EQ(kTargetImageSize, icon_size.width);
470 EXPECT_EQ(kTargetImageSize, icon_size.height);
473 TEST_F(AppsGridControllerTest, ModelAdd) {
474 model()->PopulateApps(2);
475 EXPECT_EQ(2u, [[GetPageAt(0) content] count]);
476 EXPECT_EQ(std::string("|Item 0,Item 1|"), GetViewContent());
478 app_list::AppListItemList* item_list = model()->top_level_item_list();
480 model()->CreateAndAddItem("Item 2");
481 ASSERT_EQ(3u, item_list->item_count());
482 EXPECT_EQ(3u, [apps_grid_controller_ itemCount]);
483 EXPECT_EQ(std::string("|Item 0,Item 1,Item 2|"), GetViewContent());
485 // Test adding an item whose position is in the middle.
486 app_list::AppListItem* item0 = item_list->item_at(0);
487 app_list::AppListItem* item1 = item_list->item_at(1);
488 app_list::AppListItem* item3 = model()->CreateItem("Item Three");
489 model()->AddItem(item3);
490 item_list->SetItemPosition(
491 item3, item0->position().CreateBetween(item1->position()));
492 EXPECT_EQ(4u, [apps_grid_controller_ itemCount]);
493 EXPECT_EQ(std::string("|Item 0,Item Three,Item 1,Item 2|"), GetViewContent());
496 TEST_F(AppsGridControllerTest, ModelMove) {
497 model()->PopulateApps(3);
498 EXPECT_EQ(3u, [[GetPageAt(0) content] count]);
499 EXPECT_EQ(std::string("|Item 0,Item 1,Item 2|"), GetViewContent());
501 // Test swapping items (e.g. rearranging via sync).
502 model()->top_level_item_list()->MoveItem(1, 2);
503 EXPECT_EQ(std::string("|Item 0,Item 2,Item 1|"), GetViewContent());
506 TEST_F(AppsGridControllerTest, ModelRemove) {
507 model()->PopulateApps(3);
508 EXPECT_EQ(3u, [[GetPageAt(0) content] count]);
509 EXPECT_EQ(std::string("|Item 0,Item 1,Item 2|"), GetViewContent());
511 // Test removing an item at the end.
512 model()->DeleteItem("Item 2");
513 EXPECT_EQ(2u, [apps_grid_controller_ itemCount]);
514 EXPECT_EQ(std::string("|Item 0,Item 1|"), GetViewContent());
516 // Test removing in the middle.
517 model()->CreateAndAddItem("Item 2");
518 EXPECT_EQ(3u, [apps_grid_controller_ itemCount]);
519 EXPECT_EQ(std::string("|Item 0,Item 1,Item 2|"), GetViewContent());
520 model()->DeleteItem("Item 1");
521 EXPECT_EQ(2u, [apps_grid_controller_ itemCount]);
522 EXPECT_EQ(std::string("|Item 0,Item 2|"), GetViewContent());
525 TEST_F(AppsGridControllerTest, ModelRemoveSeveral) {
526 model()->PopulateApps(3);
527 EXPECT_EQ(3u, [[GetPageAt(0) content] count]);
528 EXPECT_EQ(std::string("|Item 0,Item 1,Item 2|"), GetViewContent());
530 // Test removing multiple items via the model.
531 model()->DeleteItem("Item 0");
532 model()->DeleteItem("Item 1");
533 model()->DeleteItem("Item 2");
534 EXPECT_EQ(0u, [apps_grid_controller_ itemCount]);
535 EXPECT_EQ(std::string("||"), GetViewContent());
538 TEST_F(AppsGridControllerTest, ModelRemovePage) {
539 app_list::AppListItemList* item_list = model()->top_level_item_list();
541 model()->PopulateApps(kItemsPerPage + 1);
542 ASSERT_EQ(kItemsPerPage + 1, item_list->item_count());
543 EXPECT_EQ(kItemsPerPage + 1, [apps_grid_controller_ itemCount]);
544 EXPECT_EQ(2u, [apps_grid_controller_ pageCount]);
546 // Test removing the last item when there is one item on the second page.
547 app_list::AppListItem* last_item = item_list->item_at(kItemsPerPage);
548 model()->DeleteItem(last_item->id());
549 EXPECT_EQ(kItemsPerPage, item_list->item_count());
550 EXPECT_EQ(kItemsPerPage, [apps_grid_controller_ itemCount]);
551 EXPECT_EQ(1u, [apps_grid_controller_ pageCount]);
554 // Test install progress bars, and install flow with item highlighting.
555 TEST_F(AppsGridControllerTest, ItemInstallProgress) {
556 ReplaceTestModel(kItemsPerPage + 1);
557 EXPECT_EQ(2u, [apps_grid_controller_ pageCount]);
558 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
559 // The single item on the second page.
560 int kTestItemIndex = kItemsPerPage;
561 app_list::AppListItem* item_model =
562 model()->top_level_item_list()->item_at(kTestItemIndex);
564 // Highlighting an item should activate the page it is on.
565 model()->HighlightItemAt(kTestItemIndex);
566 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
568 // Starting install should add a progress bar, and temporarily clear the
570 NSButton* button = GetItemViewAt(kTestItemIndex);
571 NSView* containerView = [button superview];
572 EXPECT_EQ(1u, [[containerView subviews] count]);
573 EXPECT_NSEQ(@"Item 16", [button title]);
574 model()->HighlightItemAt(kTestItemIndex);
575 item_model->SetIsInstalling(true);
576 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
578 EXPECT_EQ(2u, [[containerView subviews] count]);
579 EXPECT_NSEQ(@"", [button title]);
580 NSProgressIndicator* progressIndicator =
581 [[containerView subviews] objectAtIndex:1];
582 EXPECT_FALSE([progressIndicator isIndeterminate]);
583 EXPECT_EQ(0.0, [progressIndicator doubleValue]);
585 // Updating the progress in the model should update the progress bar.
586 item_model->SetPercentDownloaded(50);
587 EXPECT_EQ(50.0, [progressIndicator doubleValue]);
589 // Two things can be installing simultaneously. When one starts or completes
590 // the model builder will ask for the item to be highlighted.
591 const int kAlternateTestItemIndex = 0;
592 model()->HighlightItemAt(kAlternateTestItemIndex);
593 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
595 // Update the first item (page doesn't change on updates).
596 item_model->SetPercentDownloaded(100);
597 EXPECT_EQ(100.0, [progressIndicator doubleValue]);
598 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
600 // A percent of -1 indicates the download is complete and the unpack/install
601 // process has started.
602 item_model->SetPercentDownloaded(-1);
603 EXPECT_TRUE([progressIndicator isIndeterminate]);
605 // Completing install removes the progress bar, and restores the title.
606 // ExtensionAppModelBuilder will reload the ExtensionAppItem, which also
607 // highlights. Do the same here.
608 model()->HighlightItemAt(kTestItemIndex);
609 item_model->SetIsInstalling(false);
610 EXPECT_EQ(1u, [[containerView subviews] count]);
611 EXPECT_NSEQ(@"Item 16", [button title]);
612 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
614 // Things should cleanup OK with |alternate_item_model| left installing.
617 // Test mouseover selection.
618 TEST_F(AppsGridControllerTest, MouseoverSelects) {
619 model()->PopulateApps(2);
620 EXPECT_EQ(nil, GetSelectedView());
622 // Test entering and exiting the first item.
623 SimulateMouseEnterItemAt(0);
624 EXPECT_EQ(GetItemViewAt(0), GetSelectedView());
625 SimulateMouseExitItemAt(0);
626 EXPECT_EQ(nil, GetSelectedView());
628 // AppKit doesn't guarantee the order, so test moving between items.
629 SimulateMouseEnterItemAt(0);
630 EXPECT_EQ(GetItemViewAt(0), GetSelectedView());
631 SimulateMouseEnterItemAt(1);
632 EXPECT_EQ(GetItemViewAt(1), GetSelectedView());
633 SimulateMouseExitItemAt(0);
634 EXPECT_EQ(GetItemViewAt(1), GetSelectedView());
635 SimulateMouseExitItemAt(1);
636 EXPECT_EQ(nil, GetSelectedView());
639 // Test AppsGridPaginationObserver totalPagesChanged().
640 TEST_F(AppsGridControllerTest, PaginationObserverPagesChanged) {
641 base::scoped_nsobject<TestPaginationObserver> observer(
642 [[TestPaginationObserver alloc] init]);
643 [apps_grid_controller_ setPaginationObserver:observer];
645 // Test totalPagesChanged.
646 model()->PopulateApps(kItemsPerPage);
647 EXPECT_EQ(0, [observer totalPagesChangedCount]);
648 EXPECT_EQ(1u, [apps_grid_controller_ pageCount]);
649 model()->PopulateApps(1);
650 EXPECT_EQ(1, [observer totalPagesChangedCount]);
651 EXPECT_EQ(2u, [apps_grid_controller_ pageCount]);
653 EXPECT_EQ(2, [observer totalPagesChangedCount]);
654 EXPECT_EQ(1u, [apps_grid_controller_ pageCount]);
655 ReplaceTestModel(kItemsPerPage * 3 + 1);
656 EXPECT_EQ(3, [observer totalPagesChangedCount]);
657 EXPECT_EQ(4u, [apps_grid_controller_ pageCount]);
659 EXPECT_FALSE([observer readVisibilityDidChange]);
660 EXPECT_EQ(0, [observer selectedPageChangedCount]);
662 [apps_grid_controller_ setPaginationObserver:nil];
665 // Test AppsGridPaginationObserver selectedPageChanged().
666 TEST_F(AppsGridControllerTest, PaginationObserverSelectedPageChanged) {
667 base::scoped_nsobject<TestPaginationObserver> observer(
668 [[TestPaginationObserver alloc] init]);
669 [apps_grid_controller_ setPaginationObserver:observer];
670 EXPECT_EQ(0, [[NSAnimationContext currentContext] duration]);
672 ReplaceTestModel(kItemsPerPage * 3 + 1);
673 EXPECT_EQ(1, [observer totalPagesChangedCount]);
674 EXPECT_EQ(4u, [apps_grid_controller_ pageCount]);
676 EXPECT_FALSE([observer readVisibilityDidChange]);
677 EXPECT_EQ(0, [observer selectedPageChangedCount]);
679 [apps_grid_controller_ scrollToPage:1];
680 EXPECT_EQ(1, [observer selectedPageChangedCount]);
681 EXPECT_EQ(1, [observer lastNewSelectedPage]);
682 EXPECT_TRUE([observer readVisibilityDidChange]);
683 EXPECT_FALSE([observer readVisibilityDidChange]); // Testing test behaviour.
684 EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:0]);
685 EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:1]);
686 EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:2]);
687 EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:3]);
689 [apps_grid_controller_ scrollToPage:0];
690 EXPECT_EQ(2, [observer selectedPageChangedCount]);
691 EXPECT_EQ(0, [observer lastNewSelectedPage]);
692 EXPECT_TRUE([observer readVisibilityDidChange]);
693 EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:0]);
694 EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:1]);
696 [apps_grid_controller_ scrollToPage:3];
697 // Note: with no animations, there is only a single page change. However, with
698 // animations we expect multiple updates depending on the rate that the scroll
699 // view updates and sends out NSViewBoundsDidChangeNotification.
700 EXPECT_EQ(3, [observer selectedPageChangedCount]);
701 EXPECT_EQ(3, [observer lastNewSelectedPage]);
702 EXPECT_TRUE([observer readVisibilityDidChange]);
703 EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:0]);
704 EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:3]);
706 [apps_grid_controller_ setPaginationObserver:nil];
709 // Test basic item moves with two items; swapping them around, dragging outside
710 // of the view bounds, and dragging on the background.
711 TEST_F(AppsGridControllerTest, DragAndDropSimple) {
712 model()->PopulateApps(2);
713 NSCollectionView* page = [apps_grid_controller_ collectionViewAtPageIndex:0];
714 NSEvent* mouse_at_cell_0 = MouseEventInCell(page, 0);
715 NSEvent* mouse_at_cell_1 = MouseEventInCell(page, 1);
716 NSEvent* mouse_at_page_centre = MouseEventInCell(page, 6);
717 NSEvent* mouse_off_page = MouseEventInCell(page, kItemsPerPage * 2);
719 const std::string kOrdered = "Item 0,Item 1";
720 const std::string kSwapped = "Item 1,Item 0";
721 const std::string kOrderedView = "|Item 0,Item 1|";
722 const std::string kSwappedView = "|Item 1,Item 0|";
724 EXPECT_EQ(kOrdered, model()->GetModelContent());
725 EXPECT_EQ(kOrderedView, GetViewContent());
726 AppsCollectionViewDragManager* drag_manager =
727 [apps_grid_controller_ dragManager];
729 // Drag first item over the second item and release.
730 [drag_manager onMouseDownInPage:page
731 withEvent:mouse_at_cell_0];
732 [drag_manager onMouseDragged:mouse_at_cell_1];
733 EXPECT_EQ(kOrdered, model()->GetModelContent());
734 EXPECT_EQ(kSwappedView, GetViewContent()); // View swaps first.
735 [drag_manager onMouseUp:mouse_at_cell_1];
736 EXPECT_EQ(kSwapped, model()->GetModelContent());
737 EXPECT_EQ(kSwappedView, GetViewContent());
740 [drag_manager onMouseDownInPage:page
741 withEvent:mouse_at_cell_1];
742 [drag_manager onMouseDragged:mouse_at_cell_0];
743 EXPECT_EQ(kSwapped, model()->GetModelContent());
744 EXPECT_EQ(kOrderedView, GetViewContent());
745 [drag_manager onMouseUp:mouse_at_cell_0];
746 EXPECT_EQ(kOrdered, model()->GetModelContent());
747 EXPECT_EQ(kOrderedView, GetViewContent());
749 // Drag first item to centre of view (should put in last place).
750 [drag_manager onMouseDownInPage:page
751 withEvent:mouse_at_cell_0];
752 [drag_manager onMouseDragged:mouse_at_page_centre];
753 EXPECT_EQ(kOrdered, model()->GetModelContent());
754 EXPECT_EQ(kSwappedView, GetViewContent());
755 [drag_manager onMouseUp:mouse_at_page_centre];
756 EXPECT_EQ(kSwapped, model()->GetModelContent());
757 EXPECT_EQ(kSwappedView, GetViewContent());
759 // Drag item to centre again (should leave it in the last place).
760 [drag_manager onMouseDownInPage:page
761 withEvent:mouse_at_cell_1];
762 [drag_manager onMouseDragged:mouse_at_page_centre];
763 EXPECT_EQ(kSwapped, model()->GetModelContent());
764 EXPECT_EQ(kSwappedView, GetViewContent());
765 [drag_manager onMouseUp:mouse_at_page_centre];
766 EXPECT_EQ(kSwapped, model()->GetModelContent());
767 EXPECT_EQ(kSwappedView, GetViewContent());
769 // Drag starting in the centre of the view, should do nothing.
770 [drag_manager onMouseDownInPage:page
771 withEvent:mouse_at_page_centre];
772 [drag_manager onMouseDragged:mouse_at_cell_0];
773 EXPECT_EQ(kSwapped, model()->GetModelContent());
774 EXPECT_EQ(kSwappedView, GetViewContent());
775 [drag_manager onMouseUp:mouse_at_cell_0];
776 EXPECT_EQ(kSwapped, model()->GetModelContent());
777 EXPECT_EQ(kSwappedView, GetViewContent());
780 [drag_manager onMouseDownInPage:page
781 withEvent:mouse_off_page];
782 [drag_manager onMouseDragged:mouse_at_cell_0];
783 EXPECT_EQ(kSwapped, model()->GetModelContent());
784 EXPECT_EQ(kSwappedView, GetViewContent());
785 [drag_manager onMouseUp:mouse_at_cell_0];
786 EXPECT_EQ(kSwapped, model()->GetModelContent());
787 EXPECT_EQ(kSwappedView, GetViewContent());
789 // Drag to first over second item, then off page.
790 [drag_manager onMouseDownInPage:page
791 withEvent:mouse_at_cell_0];
792 [drag_manager onMouseDragged:mouse_at_cell_1];
793 EXPECT_EQ(kSwapped, model()->GetModelContent());
794 EXPECT_EQ(kOrderedView, GetViewContent());
795 [drag_manager onMouseDragged:mouse_off_page];
796 EXPECT_EQ(kSwapped, model()->GetModelContent());
797 EXPECT_EQ(kOrderedView, GetViewContent());
798 [drag_manager onMouseUp:mouse_off_page];
799 EXPECT_EQ(kOrdered, model()->GetModelContent());
800 EXPECT_EQ(kOrderedView, GetViewContent());
802 // Replace with an empty model, and ensure we do not break.
804 EXPECT_EQ(std::string(), model()->GetModelContent());
805 EXPECT_EQ(std::string("||"), GetViewContent());
806 [drag_manager onMouseDownInPage:page
807 withEvent:mouse_at_cell_0];
808 [drag_manager onMouseDragged:mouse_at_cell_1];
809 [drag_manager onMouseUp:mouse_at_cell_1];
810 EXPECT_EQ(std::string(), model()->GetModelContent());
811 EXPECT_EQ(std::string("||"), GetViewContent());
814 // Test item moves between pages.
815 TEST_F(AppsGridControllerTest, DragAndDropMultiPage) {
816 const size_t kPagesToTest = 3;
817 // Put one item on the last page to hit more edge cases.
818 ReplaceTestModel(kItemsPerPage * (kPagesToTest - 1) + 1);
819 NSCollectionView* page[kPagesToTest];
820 for (size_t i = 0; i < kPagesToTest; ++i)
821 page[i] = [apps_grid_controller_ collectionViewAtPageIndex:i];
823 const std::string kSecondItemMovedToSecondPage =
824 "|Item 0,Item 2,Item 3,Item 4,Item 5,Item 6,Item 7,Item 8,"
825 "Item 9,Item 10,Item 11,Item 12,Item 13,Item 14,Item 15,Item 16|"
826 "|Item 17,Item 1,Item 18,Item 19,Item 20,Item 21,Item 22,Item 23,"
827 "Item 24,Item 25,Item 26,Item 27,Item 28,Item 29,Item 30,Item 31|"
830 NSEvent* mouse_at_cell_0 = MouseEventInCell(page[0], 0);
831 NSEvent* mouse_at_cell_1 = MouseEventInCell(page[0], 1);
832 AppsCollectionViewDragManager* drag_manager =
833 [apps_grid_controller_ dragManager];
834 [drag_manager onMouseDownInPage:page[0]
835 withEvent:mouse_at_cell_1];
837 // Initiate dragging before changing pages.
838 [drag_manager onMouseDragged:mouse_at_cell_0];
840 // Scroll to the second page.
841 [apps_grid_controller_ scrollToPage:1];
842 [drag_manager onMouseDragged:mouse_at_cell_1];
844 // Do one exhaustive check, and then spot-check corner cases.
845 EXPECT_EQ(kSecondItemMovedToSecondPage, GetViewContent());
846 EXPECT_EQ(0u, GetPageIndexForItem(0));
847 EXPECT_EQ(1u, GetPageIndexForItem(1));
848 EXPECT_EQ(0u, GetPageIndexForItem(2));
849 EXPECT_EQ(0u, GetPageIndexForItem(16));
850 EXPECT_EQ(1u, GetPageIndexForItem(17));
851 EXPECT_EQ(1u, GetPageIndexForItem(31));
852 EXPECT_EQ(2u, GetPageIndexForItem(32));
854 // Scroll to the third page and drag some more.
855 [apps_grid_controller_ scrollToPage:2];
856 [drag_manager onMouseDragged:mouse_at_cell_1];
857 EXPECT_EQ(2u, GetPageIndexForItem(1));
858 EXPECT_EQ(1u, GetPageIndexForItem(31));
859 EXPECT_EQ(1u, GetPageIndexForItem(32));
862 [apps_grid_controller_ scrollToPage:1];
863 [drag_manager onMouseDragged:mouse_at_cell_1];
864 EXPECT_EQ(kSecondItemMovedToSecondPage, GetViewContent());
865 EXPECT_EQ(1u, GetPageIndexForItem(1));
866 EXPECT_EQ(1u, GetPageIndexForItem(31));
867 EXPECT_EQ(2u, GetPageIndexForItem(32));
869 // Simulate installing an item while dragging (or have it appear during sync).
870 model()->PopulateAppWithId(33);
871 // Item should go back to its position before the drag.
872 EXPECT_EQ(0u, GetPageIndexForItem(1));
873 EXPECT_EQ(1u, GetPageIndexForItem(31));
874 EXPECT_EQ(2u, GetPageIndexForItem(32));
875 // New item should appear at end.
876 EXPECT_EQ(2u, GetPageIndexForItem(33));
878 // Scroll to end again, and keep dragging (should be ignored).
879 [apps_grid_controller_ scrollToPage:2];
880 [drag_manager onMouseDragged:mouse_at_cell_0];
881 EXPECT_EQ(0u, GetPageIndexForItem(1));
882 [drag_manager onMouseUp:mouse_at_cell_0];
883 EXPECT_EQ(0u, GetPageIndexForItem(1));
886 // Test scrolling when dragging past edge or over the pager.
887 TEST_F(AppsGridControllerTest, ScrollingWhileDragging) {
888 base::scoped_nsobject<TestPaginationObserver> observer(
889 [[TestPaginationObserver alloc] init]);
890 [apps_grid_controller_ setPaginationObserver:observer];
892 ReplaceTestModel(kItemsPerPage * 3);
893 // Start on the middle page.
894 [apps_grid_controller_ scrollToPage:1];
895 NSCollectionView* page = [apps_grid_controller_ collectionViewAtPageIndex:1];
896 NSEvent* mouse_at_cell_0 = MouseEventInCell(page, 0);
898 NSEvent* at_center = MouseEventForScroll([apps_grid_controller_ view], 0.0);
899 NSEvent* at_left = MouseEventForScroll([apps_grid_controller_ view], -1.1);
900 NSEvent* at_right = MouseEventForScroll([apps_grid_controller_ view], 1.1);
902 AppsCollectionViewDragManager* drag_manager =
903 [apps_grid_controller_ dragManager];
904 [drag_manager onMouseDownInPage:page
905 withEvent:mouse_at_cell_0];
906 [drag_manager onMouseDragged:at_center];
908 // Nothing should be scheduled: target page is visible page.
909 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
910 EXPECT_EQ(1u, [apps_grid_controller_ scheduledScrollPage]);
912 // Drag to the left, should go to first page and no further.
913 [drag_manager onMouseDragged:at_left];
914 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
915 EXPECT_EQ(0u, [apps_grid_controller_ scheduledScrollPage]);
916 [apps_grid_controller_ scrollToPage:0]; // Commit without timer for testing.
917 [drag_manager onMouseDragged:at_left];
918 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
919 EXPECT_EQ(0u, [apps_grid_controller_ scheduledScrollPage]);
921 // Drag to the right, should go to last page and no futher.
922 [drag_manager onMouseDragged:at_right];
923 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
924 EXPECT_EQ(1u, [apps_grid_controller_ scheduledScrollPage]);
925 [apps_grid_controller_ scrollToPage:1];
926 [drag_manager onMouseDragged:at_right];
927 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
928 EXPECT_EQ(2u, [apps_grid_controller_ scheduledScrollPage]);
929 [apps_grid_controller_ scrollToPage:2];
930 [drag_manager onMouseDragged:at_right];
931 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]);
932 EXPECT_EQ(2u, [apps_grid_controller_ scheduledScrollPage]);
934 // Simulate a hover over the first pager segment.
935 [observer setHoveredSegmentForTest:0];
936 [drag_manager onMouseDragged:at_center];
937 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]);
938 EXPECT_EQ(0u, [apps_grid_controller_ scheduledScrollPage]);
940 // Drag it back, should cancel schedule.
941 [observer setHoveredSegmentForTest:-1];
942 [drag_manager onMouseDragged:at_center];
943 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]);
944 EXPECT_EQ(2u, [apps_grid_controller_ scheduledScrollPage]);
946 // Hover again, now over middle segment, and ensure a release also cancels.
947 [observer setHoveredSegmentForTest:1];
948 [drag_manager onMouseDragged:at_center];
949 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]);
950 EXPECT_EQ(1u, [apps_grid_controller_ scheduledScrollPage]);
951 [drag_manager onMouseUp:at_center];
952 EXPECT_EQ(2u, [apps_grid_controller_ visiblePage]);
953 EXPECT_EQ(2u, [apps_grid_controller_ scheduledScrollPage]);
955 [apps_grid_controller_ setPaginationObserver:nil];
958 TEST_F(AppsGridControllerTest, ContextMenus) {
959 AppListItemWithMenu* item_two_model = new AppListItemWithMenu("Item Two");
960 model()->AddItem(new AppListItemWithMenu("Item One"));
961 model()->AddItem(item_two_model);
962 EXPECT_EQ(2u, [apps_grid_controller_ itemCount]);
964 NSCollectionView* page = [apps_grid_controller_ collectionViewAtPageIndex:0];
965 NSEvent* mouse_at_cell_0 = MouseEventInCell(page, 0);
966 NSEvent* mouse_at_cell_1 = MouseEventInCell(page, 1);
968 NSMenu* menu = [page menuForEvent:mouse_at_cell_0];
969 EXPECT_EQ(1, [menu numberOfItems]);
970 EXPECT_NSEQ(@"Menu For: Item One", [[menu itemAtIndex:0] title]);
972 // Test a context menu request while the item is still installing.
973 item_two_model->SetMenuReadyForTesting(false);
974 menu = [page menuForEvent:mouse_at_cell_1];
975 EXPECT_EQ(nil, menu);
977 item_two_model->SetMenuReadyForTesting(true);
978 menu = [page menuForEvent:mouse_at_cell_1];
979 EXPECT_EQ(1, [menu numberOfItems]);
980 EXPECT_NSEQ(@"Menu For: Item Two", [[menu itemAtIndex:0] title]);
982 // Test that a button being held down with the left button does not also show
984 [GetItemViewAt(0) highlight:YES];
985 EXPECT_FALSE([page menuForEvent:mouse_at_cell_0]);
986 [GetItemViewAt(0) highlight:NO];
987 EXPECT_TRUE([page menuForEvent:mouse_at_cell_0]);
991 } // namespace app_list