Views Omnibox: tolerate minor click-to-select-all dragging.
[chromium-blink-merge.git] / ui / app_list / cocoa / app_list_view_controller_unittest.mm
blob0a7d82b1faa1c86befec910d47a92e752bf9cdd2
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     delegate->SetSignedIn(true);
24     [app_list_view_controller_
25         setDelegate:delegate.PassAs<app_list::AppListViewDelegate>()];
26     SetUpWithGridController([app_list_view_controller_ appsGridController]);
27     [[test_window() contentView] addSubview:[app_list_view_controller_ view]];
28   }
30   virtual void TearDown() OVERRIDE {
31     [app_list_view_controller_
32         setDelegate:scoped_ptr<app_list::AppListViewDelegate>()];
33     app_list_view_controller_.reset();
34     AppsGridControllerTestHelper::TearDown();
35   }
37   void ReplaceTestModel(int item_count) {
38     [app_list_view_controller_
39         setDelegate:scoped_ptr<app_list::AppListViewDelegate>()];
40     scoped_ptr<AppListTestViewDelegate> delegate(new AppListTestViewDelegate);
41     delegate->ReplaceTestModel(item_count);
42     [app_list_view_controller_
43         setDelegate:delegate.PassAs<app_list::AppListViewDelegate>()];
44   }
46   AppListTestViewDelegate* delegate() {
47     return static_cast<AppListTestViewDelegate*>(
48         [app_list_view_controller_ delegate]);
49   }
51   AppListTestModel* model() {
52     return delegate()->GetTestModel();
53   }
55  protected:
56   base::scoped_nsobject<AppListViewController> app_list_view_controller_;
58  private:
59   DISALLOW_COPY_AND_ASSIGN(AppListViewControllerTest);
62 TEST_VIEW(AppListViewControllerTest, [app_list_view_controller_ view]);
64 // Test that adding and removing pages updates the pager.
65 TEST_F(AppListViewControllerTest, PagerSegmentCounts) {
66   NSSegmentedControl* pager = [app_list_view_controller_ pagerControl];
67   EXPECT_EQ(1, [pager segmentCount]);
69   ReplaceTestModel(kItemsPerPage * 2);
70   EXPECT_EQ(2, [pager segmentCount]);
71   model()->PopulateApps(1);
72   EXPECT_EQ(3, [pager segmentCount]);
74   ReplaceTestModel(1);
75   EXPECT_EQ(1, [pager segmentCount]);
78 // Test that clicking the pager changes pages.
79 TEST_F(AppListViewControllerTest, PagerChangingPage) {
80   NSSegmentedControl* pager = [app_list_view_controller_ pagerControl];
81   ReplaceTestModel(kItemsPerPage * 3);
82   EXPECT_EQ(3, [pager segmentCount]);
84   EXPECT_EQ(0, [pager selectedSegment]);
85   EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
86   EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:0]);
87   EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:1]);
89   // Emulate a click on the second segment to navigate to the second page.
90   [pager setSelectedSegment:1];
91   [[pager target] performSelector:[pager action]
92                        withObject:pager];
94   EXPECT_EQ(1u, [apps_grid_controller_ visiblePage]);
95   EXPECT_EQ(1, [pager selectedSegment]);
96   EXPECT_EQ(0.0, [apps_grid_controller_ visiblePortionOfPage:0]);
97   EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:1]);
99   // Replace with a single page model, and ensure we go back to the first page.
100   ReplaceTestModel(1);
101   EXPECT_EQ(0u, [apps_grid_controller_ visiblePage]);
102   EXPECT_EQ(0, [pager selectedSegment]);
103   EXPECT_EQ(1, [pager segmentCount]);
104   EXPECT_EQ(1.0, [apps_grid_controller_ visiblePortionOfPage:0]);
107 // Test the view when the user is already signed in.
108 TEST_F(AppListViewControllerTest, SignedIn) {
109   // There should be just 1, visible subview when signed in.
110   EXPECT_EQ(1u, [[[app_list_view_controller_ view] subviews] count]);
111   EXPECT_FALSE([[app_list_view_controller_ backgroundView] isHidden]);
114 // Test the view when signin is required.
115 TEST_F(AppListViewControllerTest, NeedsSignin) {
116   // Begin the test with a signed out app list.
117   delegate()->SetSignedIn(false);
118   EXPECT_EQ(2u, [[[app_list_view_controller_ view] subviews] count]);
119   EXPECT_TRUE([[app_list_view_controller_ backgroundView] isHidden]);
121   // Simulate signing in, should enter the SignedIn state.
122   delegate()->SetSignedIn(true);
123   EXPECT_EQ(1u, [[[app_list_view_controller_ view] subviews] count]);
124   EXPECT_FALSE([[app_list_view_controller_ backgroundView] isHidden]);
127 }  // namespace test
128 }  // namespace app_list