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/signin_delegate.h"
11 #include "ui/app_list/test/app_list_test_model.h"
12 #include "ui/app_list/test/app_list_test_view_delegate.h"
17 class AppListViewControllerTest : public AppsGridControllerTestHelper,
18 public SigninDelegate {
20 AppListViewControllerTest() {}
22 virtual void SetUp() OVERRIDE {
23 app_list_view_controller_.reset([[AppListViewController alloc] init]);
24 SetUpWithGridController([app_list_view_controller_ appsGridController]);
25 [[test_window() contentView] addSubview:[app_list_view_controller_ view]];
28 virtual void TearDown() OVERRIDE {
29 [app_list_view_controller_
30 setDelegate:scoped_ptr<app_list::AppListViewDelegate>()];
31 app_list_view_controller_.reset();
32 AppsGridControllerTestHelper::TearDown();
35 virtual void ResetModel(scoped_ptr<AppListModel> new_model) OVERRIDE {
36 scoped_ptr<AppListTestViewDelegate> delegate(new AppListTestViewDelegate);
37 delegate->set_test_signin_delegate(this);
38 [app_list_view_controller_
39 setDelegate:delegate.PassAs<AppListViewDelegate>()
40 withTestModel:new_model.Pass()];
43 // SigninDelegate overrides:
44 virtual bool NeedSignin() OVERRIDE { return false; }
45 virtual void ShowSignin() OVERRIDE {}
46 virtual void OpenLearnMore() OVERRIDE {}
47 virtual void OpenSettings() OVERRIDE {}
49 virtual base::string16 GetSigninHeading() OVERRIDE {
50 return base::string16();
52 virtual base::string16 GetSigninText() OVERRIDE { return base::string16(); }
53 virtual base::string16 GetSigninButtonText() OVERRIDE {
54 return base::string16();
56 virtual base::string16 GetLearnMoreLinkText() OVERRIDE {
57 return base::string16();
59 virtual base::string16 GetSettingsLinkText() OVERRIDE {
60 return base::string16();
64 base::scoped_nsobject<AppListViewController> app_list_view_controller_;
67 DISALLOW_COPY_AND_ASSIGN(AppListViewControllerTest);
70 // Helper class allowing NeedSignin() to return true during SetUp(), to test an
71 // initial state where signin is required, and the regular view starts hidden.
72 class AppListViewControllerSigninTest : public AppListViewControllerTest {
74 AppListViewControllerSigninTest() : needs_signin_(true) {}
76 // SigninDelegate override:
77 virtual bool NeedSignin() OVERRIDE { return needs_signin_; }
83 DISALLOW_COPY_AND_ASSIGN(AppListViewControllerSigninTest);
86 TEST_VIEW(AppListViewControllerTest, [app_list_view_controller_ view]);
88 // Test that adding and removing pages updates the pager.
89 TEST_F(AppListViewControllerTest, PagerSegmentCounts) {
90 NSSegmentedControl* pager = [app_list_view_controller_ pagerControl];
91 EXPECT_EQ(1, [pager segmentCount]);
93 ReplaceTestModel(kItemsPerPage * 2);
94 EXPECT_EQ(2, [pager segmentCount]);
95 model()->PopulateApps(1);
96 EXPECT_EQ(3, [pager segmentCount]);
99 EXPECT_EQ(1, [pager segmentCount]);
102 // Test that clicking the pager changes pages.
103 TEST_F(AppListViewControllerTest, PagerChangingPage) {
104 NSSegmentedControl* pager = [app_list_view_controller_ pagerControl];
105 ReplaceTestModel(kItemsPerPage * 3);
106 EXPECT_EQ(3, [pager segmentCount]);
108 EXPECT_EQ(0, [pager selectedSegment]);
109 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
110 EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:0]);
111 EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:1]);
113 // Emulate a click on the second segment to navigate to the second page.
114 [pager setSelectedSegment:1];
115 [[pager target] performSelector:[pager action]
118 EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
119 EXPECT_EQ(1, [pager selectedSegment]);
120 EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:0]);
121 EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:1]);
123 // Replace with a single page model, and ensure we go back to the first page.
125 EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
126 EXPECT_EQ(0, [pager selectedSegment]);
127 EXPECT_EQ(1, [pager segmentCount]);
128 EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:0]);
131 // Test the view when the user is already signed in.
132 TEST_F(AppListViewControllerTest, SignedIn) {
133 // There should be just 1, visible subview when signed in.
134 EXPECT_EQ(1u, [[[app_list_view_controller_ view] subviews] count]);
135 EXPECT_FALSE([[app_list_view_controller_ backgroundView] isHidden]);
138 // Test the view when signin is required.
139 TEST_F(AppListViewControllerSigninTest, NeedsSignin) {
140 EXPECT_EQ(2u, [[[app_list_view_controller_ view] subviews] count]);
141 EXPECT_TRUE([[app_list_view_controller_ backgroundView] isHidden]);
143 // Simulate signing in, should enter the SignedIn state.
144 needs_signin_ = false;
145 NotifySigninSuccess();
146 EXPECT_EQ(1u, [[[app_list_view_controller_ view] subviews] count]);
147 EXPECT_FALSE([[app_list_view_controller_ backgroundView] isHidden]);
151 } // namespace app_list