Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: ui/app_list/cocoa/apps_search_box_controller_unittest.mm

Issue 63283003: Move AppListModel::Users to AppListViewDelegate (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Mac fixes Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "ui/app_list/cocoa/apps_search_box_controller.h" 5 #import "ui/app_list/cocoa/apps_search_box_controller.h"
6 6
7 #include "base/mac/scoped_nsobject.h" 7 #include "base/mac/scoped_nsobject.h"
8 #include "base/strings/sys_string_conversions.h" 8 #include "base/strings/sys_string_conversions.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #import "testing/gtest_mac.h" 10 #import "testing/gtest_mac.h"
(...skipping 16 matching lines...) Expand all
27 @property(assign, nonatomic) int textChangeCount; 27 @property(assign, nonatomic) int textChangeCount;
28 28
29 @end 29 @end
30 30
31 @implementation TestAppsSearchBoxDelegate 31 @implementation TestAppsSearchBoxDelegate
32 32
33 @synthesize textChangeCount = textChangeCount_; 33 @synthesize textChangeCount = textChangeCount_;
34 34
35 - (id)init { 35 - (id)init {
36 if ((self = [super init])) { 36 if ((self = [super init])) {
37 app_list::AppListModel::Users users(2); 37 app_list::AppListViewDelegate::Users users(2);
38 users[0].name = ASCIIToUTF16("user1"); 38 users[0].name = ASCIIToUTF16("user1");
39 users[1].name = ASCIIToUTF16("user2"); 39 users[1].name = ASCIIToUTF16("user2");
40 users[1].email = ASCIIToUTF16("user2@chromium.org"); 40 users[1].email = ASCIIToUTF16("user2@chromium.org");
41 users[1].active = true; 41 users[1].active = true;
42 appListModel_.SetUsers(users); 42 appListDelegate_.SetUsers(users);
43 } 43 }
44 return self; 44 return self;
45 } 45 }
46 46
47 - (app_list::SearchBoxModel*)searchBoxModel { 47 - (app_list::SearchBoxModel*)searchBoxModel {
48 return &searchBoxModel_; 48 return &searchBoxModel_;
49 } 49 }
50 50
51 - (app_list::AppListViewDelegate*)appListDelegate { 51 - (app_list::AppListViewDelegate*)appListDelegate {
52 return &appListDelegate_; 52 return &appListDelegate_;
(...skipping 10 matching lines...) Expand all
63 } 63 }
64 64
65 - (CGFloat)bubbleCornerRadius { 65 - (CGFloat)bubbleCornerRadius {
66 return 3; 66 return 3;
67 } 67 }
68 68
69 - (app_list::AppListModel*)appListModel { 69 - (app_list::AppListModel*)appListModel {
70 return &appListModel_; 70 return &appListModel_;
71 } 71 }
72 72
73 - (app_list::test::AppListTestViewDelegate*)appListViewDelegate {
74 return &appListViewDelegate_;
75 }
76
73 @end 77 @end
74 78
75 namespace app_list { 79 namespace app_list {
76 namespace test { 80 namespace test {
77 81
78 class AppsSearchBoxControllerTest : public ui::CocoaTest, 82 class AppsSearchBoxControllerTest : public ui::CocoaTest {
79 public AppListModelObserver {
80 public: 83 public:
81 AppsSearchBoxControllerTest() { 84 AppsSearchBoxControllerTest() {
82 Init(); 85 Init();
83 } 86 }
84 87
85 virtual void SetUp() OVERRIDE { 88 virtual void SetUp() OVERRIDE {
86 apps_search_box_controller_.reset( 89 apps_search_box_controller_.reset(
87 [[AppsSearchBoxController alloc] initWithFrame: 90 [[AppsSearchBoxController alloc] initWithFrame:
88 NSMakeRect(0, 0, 400, 100)]); 91 NSMakeRect(0, 0, 400, 100)]);
89 delegate_.reset([[TestAppsSearchBoxDelegate alloc] init]); 92 delegate_.reset([[TestAppsSearchBoxDelegate alloc] init]);
90 [apps_search_box_controller_ setDelegate:delegate_]; 93 [apps_search_box_controller_ setDelegate:delegate_];
91 [delegate_ appListModel]->AddObserver(this);
92 94
93 ui::CocoaTest::SetUp(); 95 ui::CocoaTest::SetUp();
94 [[test_window() contentView] addSubview:[apps_search_box_controller_ view]]; 96 [[test_window() contentView] addSubview:[apps_search_box_controller_ view]];
95 } 97 }
96 98
97 virtual void TearDown() OVERRIDE { 99 virtual void TearDown() OVERRIDE {
98 [delegate_ appListModel]->RemoveObserver(this);
99 [apps_search_box_controller_ setDelegate:nil]; 100 [apps_search_box_controller_ setDelegate:nil];
100 ui::CocoaTest::TearDown(); 101 ui::CocoaTest::TearDown();
101 } 102 }
102 103
103 void SimulateKeyAction(SEL c) { 104 void SimulateKeyAction(SEL c) {
104 NSControl* control = [apps_search_box_controller_ searchTextField]; 105 NSControl* control = [apps_search_box_controller_ searchTextField];
105 [apps_search_box_controller_ control:control 106 [apps_search_box_controller_ control:control
106 textView:nil 107 textView:nil
107 doCommandBySelector:c]; 108 doCommandBySelector:c];
108 } 109 }
109 110
110 protected: 111 protected:
111 // Overridden from app_list::AppListModelObserver:
112 virtual void OnAppListModelUsersChanged() OVERRIDE {
113 [apps_search_box_controller_ rebuildMenu];
114 }
115
116 virtual void OnAppListModelSigninStatusChanged() OVERRIDE {}
117
118 base::scoped_nsobject<TestAppsSearchBoxDelegate> delegate_; 112 base::scoped_nsobject<TestAppsSearchBoxDelegate> delegate_;
119 base::scoped_nsobject<AppsSearchBoxController> apps_search_box_controller_; 113 base::scoped_nsobject<AppsSearchBoxController> apps_search_box_controller_;
120 114
121 private: 115 private:
122 DISALLOW_COPY_AND_ASSIGN(AppsSearchBoxControllerTest); 116 DISALLOW_COPY_AND_ASSIGN(AppsSearchBoxControllerTest);
123 }; 117 };
124 118
125 TEST_VIEW(AppsSearchBoxControllerTest, [apps_search_box_controller_ view]); 119 TEST_VIEW(AppsSearchBoxControllerTest, [apps_search_box_controller_ view]);
126 120
127 // Test the search box initialization, and search input and clearing. 121 // Test the search box initialization, and search input and clearing.
(...skipping 28 matching lines...) Expand all
156 SimulateKeyAction(@selector(complete:)); 150 SimulateKeyAction(@selector(complete:));
157 EXPECT_NSEQ([NSString string], 151 EXPECT_NSEQ([NSString string],
158 [[apps_search_box_controller_ searchTextField] stringValue]); 152 [[apps_search_box_controller_ searchTextField] stringValue]);
159 EXPECT_EQ(4, [delegate_ textChangeCount]); 153 EXPECT_EQ(4, [delegate_ textChangeCount]);
160 } 154 }
161 155
162 // Test the popup menu items when there is only one user.. 156 // Test the popup menu items when there is only one user..
163 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenuSingleUser) { 157 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenuSingleUser) {
164 // Set a single user. We need to set the delegate again because the 158 // Set a single user. We need to set the delegate again because the
165 // AppListModel observer isn't hooked up in these tests. 159 // AppListModel observer isn't hooked up in these tests.
166 [delegate_ appListModel]->SetUsers(app_list::AppListModel::Users(1)); 160 [delegate_ appListViewDelegate]->SetUsers(
161 app_list::AppListViewDelegate::Users(1));
167 [apps_search_box_controller_ setDelegate:delegate_]; 162 [apps_search_box_controller_ setDelegate:delegate_];
168 163
169 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; 164 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl];
170 EXPECT_TRUE([apps_search_box_controller_ appListMenu]); 165 EXPECT_TRUE([apps_search_box_controller_ appListMenu]);
171 ui::MenuModel* menu_model 166 ui::MenuModel* menu_model
172 = [apps_search_box_controller_ appListMenu]->menu_model(); 167 = [apps_search_box_controller_ appListMenu]->menu_model();
173 // Add one to the item count to account for the blank, first item that Cocoa 168 // Add one to the item count to account for the blank, first item that Cocoa
174 // has in its popup menus. 169 // has in its popup menus.
175 EXPECT_EQ(menu_model->GetItemCount() + 1, 170 EXPECT_EQ(menu_model->GetItemCount() + 1,
176 [[menu_control menu] numberOfItems]); 171 [[menu_control menu] numberOfItems]);
177 172
178 // All command ids should be less than |SELECT_PROFILE| as no user menu items 173 // All command ids should be less than |SELECT_PROFILE| as no user menu items
179 // are being shown. 174 // are being shown.
180 for (int i = 0; i < menu_model->GetItemCount(); ++i) 175 for (int i = 0; i < menu_model->GetItemCount(); ++i)
181 EXPECT_LT(menu_model->GetCommandIdAt(i), AppListMenu::SELECT_PROFILE); 176 EXPECT_LT(menu_model->GetCommandIdAt(i), AppListMenu::SELECT_PROFILE);
182 177
183 // The number of items should match the index that starts profile items. 178 // The number of items should match the index that starts profile items.
184 EXPECT_EQ(AppListMenu::SELECT_PROFILE, menu_model->GetItemCount()); 179 EXPECT_EQ(AppListMenu::SELECT_PROFILE, menu_model->GetItemCount());
185 } 180 }
186 181
187 // Test the popup menu items for the multi-profile case. 182 // Test the popup menu items for the multi-profile case.
188 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenu) { 183 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenu) {
189 const app_list::AppListModel::Users& users = 184 const app_list::AppListViewDelegate::Users& users =
190 [delegate_ appListModel]->users(); 185 [delegate_ appListViewDelegate]->GetUsers();
191 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; 186 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl];
192 EXPECT_TRUE([apps_search_box_controller_ appListMenu]); 187 EXPECT_TRUE([apps_search_box_controller_ appListMenu]);
193 ui::MenuModel* menu_model 188 ui::MenuModel* menu_model
194 = [apps_search_box_controller_ appListMenu]->menu_model(); 189 = [apps_search_box_controller_ appListMenu]->menu_model();
195 // Add one to the item count to account for the blank, first item that Cocoa 190 // Add one to the item count to account for the blank, first item that Cocoa
196 // has in its popup menus. 191 // has in its popup menus.
197 EXPECT_EQ(menu_model->GetItemCount() + 1, 192 EXPECT_EQ(menu_model->GetItemCount() + 1,
198 [[menu_control menu] numberOfItems]); 193 [[menu_control menu] numberOfItems]);
199 194
200 ui::MenuModel* found_menu_model = menu_model; 195 ui::MenuModel* found_menu_model = menu_model;
(...skipping 27 matching lines...) Expand all
228 AppListMenu::SHOW_SETTINGS, &menu_model, &index)); 223 AppListMenu::SHOW_SETTINGS, &menu_model, &index));
229 EXPECT_EQ(found_menu_model, menu_model); 224 EXPECT_EQ(found_menu_model, menu_model);
230 NSMenuItem* settings_item = [[menu_control menu] itemAtIndex:index + 1]; 225 NSMenuItem* settings_item = [[menu_control menu] itemAtIndex:index + 1];
231 EXPECT_FALSE([settings_item view]); 226 EXPECT_FALSE([settings_item view]);
232 EXPECT_NSEQ(base::SysUTF16ToNSString(menu_model->GetLabelAt(index)), 227 EXPECT_NSEQ(base::SysUTF16ToNSString(menu_model->GetLabelAt(index)),
233 [settings_item title]); 228 [settings_item title]);
234 } 229 }
235 230
236 // Test adding another user, and changing an existing one. 231 // Test adding another user, and changing an existing one.
237 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenuChangingUsers) { 232 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenuChangingUsers) {
238 app_list::AppListModel::Users users = [delegate_ appListModel]->users(); 233 app_list::AppListViewDelegate::Users users =
234 [delegate_ appListViewDelegate]->GetUsers();
239 EXPECT_EQ(2u, users.size()); 235 EXPECT_EQ(2u, users.size());
240 ui::MenuModel* menu_model 236 ui::MenuModel* menu_model
241 = [apps_search_box_controller_ appListMenu]->menu_model(); 237 = [apps_search_box_controller_ appListMenu]->menu_model();
242 // Adding one to account for the empty item at index 0 in Cocoa popup menus. 238 // Adding one to account for the empty item at index 0 in Cocoa popup menus.
243 int non_user_items = menu_model->GetItemCount() - users.size() + 1; 239 int non_user_items = menu_model->GetItemCount() - users.size() + 1;
244 240
245 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; 241 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl];
246 EXPECT_EQ(2, [[menu_control menu] numberOfItems] - non_user_items); 242 EXPECT_EQ(2, [[menu_control menu] numberOfItems] - non_user_items);
247 EXPECT_NSEQ(base::SysUTF16ToNSString(users[0].name), 243 EXPECT_NSEQ(base::SysUTF16ToNSString(users[0].name),
248 [[[menu_control menu] itemAtIndex:1] title]); 244 [[[menu_control menu] itemAtIndex:1] title]);
249 245
250 users[0].name = ASCIIToUTF16("renamed user"); 246 users[0].name = ASCIIToUTF16("renamed user");
251 app_list::AppListModel::User new_user; 247 app_list::AppListViewDelegate::User new_user;
252 new_user.name = ASCIIToUTF16("user3"); 248 new_user.name = ASCIIToUTF16("user3");
253 users.push_back(new_user); 249 users.push_back(new_user);
254 [delegate_ appListModel]->SetUsers(users); 250 [delegate_ appListViewDelegate]->SetUsers(users);
255 251
256 // Should now be an extra item, and it should have correct titles. 252 // Should now be an extra item, and it should have correct titles.
257 EXPECT_EQ(3, [[menu_control menu] numberOfItems] - non_user_items); 253 EXPECT_EQ(3, [[menu_control menu] numberOfItems] - non_user_items);
258 EXPECT_NSEQ(base::SysUTF16ToNSString(users[0].name), 254 EXPECT_NSEQ(base::SysUTF16ToNSString(users[0].name),
259 [[[menu_control menu] itemAtIndex:1] title]); 255 [[[menu_control menu] itemAtIndex:1] title]);
260 EXPECT_NSEQ(base::SysUTF16ToNSString(new_user.name), 256 EXPECT_NSEQ(base::SysUTF16ToNSString(new_user.name),
261 [[[menu_control menu] itemAtIndex:3] title]); 257 [[[menu_control menu] itemAtIndex:3] title]);
262 } 258 }
263 259
264 } // namespace test 260 } // namespace test
265 } // namespace app_list 261 } // namespace app_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698