Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / app_list / cocoa / app_list_view_controller_unittest.mm
blob60f83e7473123d71acffafe195914c1d9d50e839
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/scoped_nsobject.h"
6 #import "testing/gtest_mac.h"
7 #import "ui/app_list/cocoa/app_list_view_controller.h"
8 #import "ui/app_list/cocoa/apps_grid_controller.h"
9 #import "ui/app_list/cocoa/test/apps_grid_controller_test_helper.h"
10 #include "ui/app_list/test/app_list_test_model.h"
11 #include "ui/app_list/test/app_list_test_view_delegate.h"
13 namespace app_list {
14 namespace test {
16 class AppListViewControllerTest : public AppsGridControllerTestHelper {
17  public:
18   AppListViewControllerTest() {}
20   virtual void SetUp() OVERRIDE {
21     app_list_view_controller_.reset([[AppListViewController alloc] init]);
22     scoped_ptr<AppListTestViewDelegate> delegate(new AppListTestViewDelegate);
23     [app_list_view_controller_
24         setDelegate:delegate.PassAs<app_list::AppListViewDelegate>()];
25     SetUpWithGridController([app_list_view_controller_ appsGridController]);
26     [[test_window() contentView] addSubview:[app_list_view_controller_ view]];
27   }
29   virtual void TearDown() OVERRIDE {
30     [app_list_view_controller_
31         setDelegate:scoped_ptr<app_list::AppListViewDelegate>()];
32     app_list_view_controller_.reset();
33     AppsGridControllerTestHelper::TearDown();
34   }
36   void ReplaceTestModel(int item_count) {
37     [app_list_view_controller_
38         setDelegate:scoped_ptr<app_list::AppListViewDelegate>()];
39     scoped_ptr<AppListTestViewDelegate> delegate(new AppListTestViewDelegate);
40     delegate->ReplaceTestModel(item_count);
41     [app_list_view_controller_
42         setDelegate:delegate.PassAs<app_list::AppListViewDelegate>()];
43   }
45   AppListTestViewDelegate* delegate() {
46     return static_cast<AppListTestViewDelegate*>(
47         [app_list_view_controller_ delegate]);
48   }
50   AppListTestModel* model() {
51     return delegate()->GetTestModel();
52   }
54  protected:
55   base::scoped_nsobject<AppListViewController> app_list_view_controller_;
57  private:
58   DISALLOW_COPY_AND_ASSIGN(AppListViewControllerTest);
61 TEST_VIEW(AppListViewControllerTest, [app_list_view_controller_ view]);
63 // Test that adding and removing pages updates the pager.
64 TEST_F(AppListViewControllerTest, PagerSegmentCounts) {
65   NSSegmentedControl* pager = [app_list_view_controller_ pagerControl];
66   EXPECT_EQ(1, [pager segmentCount]);
68   ReplaceTestModel(kItemsPerPage * 2);
69   EXPECT_EQ(2, [pager segmentCount]);
70   model()->PopulateApps(1);
71   EXPECT_EQ(3, [pager segmentCount]);
73   ReplaceTestModel(1);
74   EXPECT_EQ(1, [pager segmentCount]);
77 // Test that clicking the pager changes pages.
78 TEST_F(AppListViewControllerTest, PagerChangingPage) {
79   NSSegmentedControl* pager = [app_list_view_controller_ pagerControl];
80   ReplaceTestModel(kItemsPerPage * 3);
81   EXPECT_EQ(3, [pager segmentCount]);
83   EXPECT_EQ(0, [pager selectedSegment]);
84   EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
85   EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:0]);
86   EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:1]);
88   // Emulate a click on the second segment to navigate to the second page.
89   [pager setSelectedSegment:1];
90   [[pager target] performSelector:[pager action]
91                        withObject:pager];
93   EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
94   EXPECT_EQ(1, [pager selectedSegment]);
95   EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:0]);
96   EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:1]);
98   // Replace with a single page model, and ensure we go back to the first page.
99   ReplaceTestModel(1);
100   EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
101   EXPECT_EQ(0, [pager selectedSegment]);
102   EXPECT_EQ(1, [pager segmentCount]);
103   EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:0]);
106 }  // namespace test
107 }  // namespace app_list