OLD | NEW |
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 Loading... |
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_; |
53 } | 53 } |
54 | 54 |
| 55 - (app_list::test::AppListTestViewDelegate*)appListTestViewDelegate { |
| 56 return &appListDelegate_; |
| 57 } |
| 58 |
| 59 |
55 - (BOOL)control:(NSControl*)control | 60 - (BOOL)control:(NSControl*)control |
56 textView:(NSTextView*)textView | 61 textView:(NSTextView*)textView |
57 doCommandBySelector:(SEL)command { | 62 doCommandBySelector:(SEL)command { |
58 return NO; | 63 return NO; |
59 } | 64 } |
60 | 65 |
61 - (void)modelTextDidChange { | 66 - (void)modelTextDidChange { |
62 ++textChangeCount_; | 67 ++textChangeCount_; |
63 } | 68 } |
64 | 69 |
65 - (CGFloat)bubbleCornerRadius { | 70 - (CGFloat)bubbleCornerRadius { |
66 return 3; | 71 return 3; |
67 } | 72 } |
68 | 73 |
69 - (app_list::AppListModel*)appListModel { | 74 - (app_list::AppListModel*)appListModel { |
70 return &appListModel_; | 75 return &appListModel_; |
71 } | 76 } |
72 | 77 |
73 @end | 78 @end |
74 | 79 |
75 namespace app_list { | 80 namespace app_list { |
76 namespace test { | 81 namespace test { |
77 | 82 |
78 class AppsSearchBoxControllerTest : public ui::CocoaTest, | 83 class AppsSearchBoxControllerTest : public ui::CocoaTest { |
79 public AppListModelObserver { | |
80 public: | 84 public: |
81 AppsSearchBoxControllerTest() { | 85 AppsSearchBoxControllerTest() { |
82 Init(); | 86 Init(); |
83 } | 87 } |
84 | 88 |
85 virtual void SetUp() OVERRIDE { | 89 virtual void SetUp() OVERRIDE { |
86 apps_search_box_controller_.reset( | 90 apps_search_box_controller_.reset( |
87 [[AppsSearchBoxController alloc] initWithFrame: | 91 [[AppsSearchBoxController alloc] initWithFrame: |
88 NSMakeRect(0, 0, 400, 100)]); | 92 NSMakeRect(0, 0, 400, 100)]); |
89 delegate_.reset([[TestAppsSearchBoxDelegate alloc] init]); | 93 delegate_.reset([[TestAppsSearchBoxDelegate alloc] init]); |
90 [apps_search_box_controller_ setDelegate:delegate_]; | 94 [apps_search_box_controller_ setDelegate:delegate_]; |
91 [delegate_ appListModel]->AddObserver(this); | |
92 | 95 |
93 ui::CocoaTest::SetUp(); | 96 ui::CocoaTest::SetUp(); |
94 [[test_window() contentView] addSubview:[apps_search_box_controller_ view]]; | 97 [[test_window() contentView] addSubview:[apps_search_box_controller_ view]]; |
95 } | 98 } |
96 | 99 |
97 virtual void TearDown() OVERRIDE { | 100 virtual void TearDown() OVERRIDE { |
98 [delegate_ appListModel]->RemoveObserver(this); | |
99 [apps_search_box_controller_ setDelegate:nil]; | 101 [apps_search_box_controller_ setDelegate:nil]; |
100 ui::CocoaTest::TearDown(); | 102 ui::CocoaTest::TearDown(); |
101 } | 103 } |
102 | 104 |
103 void SimulateKeyAction(SEL c) { | 105 void SimulateKeyAction(SEL c) { |
104 NSControl* control = [apps_search_box_controller_ searchTextField]; | 106 NSControl* control = [apps_search_box_controller_ searchTextField]; |
105 [apps_search_box_controller_ control:control | 107 [apps_search_box_controller_ control:control |
106 textView:nil | 108 textView:nil |
107 doCommandBySelector:c]; | 109 doCommandBySelector:c]; |
108 } | 110 } |
109 | 111 |
110 protected: | 112 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_; | 113 base::scoped_nsobject<TestAppsSearchBoxDelegate> delegate_; |
119 base::scoped_nsobject<AppsSearchBoxController> apps_search_box_controller_; | 114 base::scoped_nsobject<AppsSearchBoxController> apps_search_box_controller_; |
120 | 115 |
121 private: | 116 private: |
122 DISALLOW_COPY_AND_ASSIGN(AppsSearchBoxControllerTest); | 117 DISALLOW_COPY_AND_ASSIGN(AppsSearchBoxControllerTest); |
123 }; | 118 }; |
124 | 119 |
125 TEST_VIEW(AppsSearchBoxControllerTest, [apps_search_box_controller_ view]); | 120 TEST_VIEW(AppsSearchBoxControllerTest, [apps_search_box_controller_ view]); |
126 | 121 |
127 // Test the search box initialization, and search input and clearing. | 122 // Test the search box initialization, and search input and clearing. |
(...skipping 28 matching lines...) Expand all Loading... |
156 SimulateKeyAction(@selector(complete:)); | 151 SimulateKeyAction(@selector(complete:)); |
157 EXPECT_NSEQ([NSString string], | 152 EXPECT_NSEQ([NSString string], |
158 [[apps_search_box_controller_ searchTextField] stringValue]); | 153 [[apps_search_box_controller_ searchTextField] stringValue]); |
159 EXPECT_EQ(4, [delegate_ textChangeCount]); | 154 EXPECT_EQ(4, [delegate_ textChangeCount]); |
160 } | 155 } |
161 | 156 |
162 // Test the popup menu items when there is only one user.. | 157 // Test the popup menu items when there is only one user.. |
163 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenuSingleUser) { | 158 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenuSingleUser) { |
164 // Set a single user. We need to set the delegate again because the | 159 // Set a single user. We need to set the delegate again because the |
165 // AppListModel observer isn't hooked up in these tests. | 160 // AppListModel observer isn't hooked up in these tests. |
166 [delegate_ appListModel]->SetUsers(app_list::AppListModel::Users(1)); | 161 [delegate_ appListTestViewDelegate]->SetUsers( |
| 162 app_list::AppListViewDelegate::Users(1)); |
167 [apps_search_box_controller_ setDelegate:delegate_]; | 163 [apps_search_box_controller_ setDelegate:delegate_]; |
168 | 164 |
169 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; | 165 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; |
170 EXPECT_TRUE([apps_search_box_controller_ appListMenu]); | 166 EXPECT_TRUE([apps_search_box_controller_ appListMenu]); |
171 ui::MenuModel* menu_model | 167 ui::MenuModel* menu_model |
172 = [apps_search_box_controller_ appListMenu]->menu_model(); | 168 = [apps_search_box_controller_ appListMenu]->menu_model(); |
173 // Add one to the item count to account for the blank, first item that Cocoa | 169 // Add one to the item count to account for the blank, first item that Cocoa |
174 // has in its popup menus. | 170 // has in its popup menus. |
175 EXPECT_EQ(menu_model->GetItemCount() + 1, | 171 EXPECT_EQ(menu_model->GetItemCount() + 1, |
176 [[menu_control menu] numberOfItems]); | 172 [[menu_control menu] numberOfItems]); |
177 | 173 |
178 // All command ids should be less than |SELECT_PROFILE| as no user menu items | 174 // All command ids should be less than |SELECT_PROFILE| as no user menu items |
179 // are being shown. | 175 // are being shown. |
180 for (int i = 0; i < menu_model->GetItemCount(); ++i) | 176 for (int i = 0; i < menu_model->GetItemCount(); ++i) |
181 EXPECT_LT(menu_model->GetCommandIdAt(i), AppListMenu::SELECT_PROFILE); | 177 EXPECT_LT(menu_model->GetCommandIdAt(i), AppListMenu::SELECT_PROFILE); |
182 | 178 |
183 // The number of items should match the index that starts profile items. | 179 // The number of items should match the index that starts profile items. |
184 EXPECT_EQ(AppListMenu::SELECT_PROFILE, menu_model->GetItemCount()); | 180 EXPECT_EQ(AppListMenu::SELECT_PROFILE, menu_model->GetItemCount()); |
185 } | 181 } |
186 | 182 |
187 // Test the popup menu items for the multi-profile case. | 183 // Test the popup menu items for the multi-profile case. |
188 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenu) { | 184 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenu) { |
189 const app_list::AppListModel::Users& users = | 185 const app_list::AppListViewDelegate::Users& users = |
190 [delegate_ appListModel]->users(); | 186 [delegate_ appListDelegate]->GetUsers(); |
191 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; | 187 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; |
192 EXPECT_TRUE([apps_search_box_controller_ appListMenu]); | 188 EXPECT_TRUE([apps_search_box_controller_ appListMenu]); |
193 ui::MenuModel* menu_model | 189 ui::MenuModel* menu_model |
194 = [apps_search_box_controller_ appListMenu]->menu_model(); | 190 = [apps_search_box_controller_ appListMenu]->menu_model(); |
195 // Add one to the item count to account for the blank, first item that Cocoa | 191 // Add one to the item count to account for the blank, first item that Cocoa |
196 // has in its popup menus. | 192 // has in its popup menus. |
197 EXPECT_EQ(menu_model->GetItemCount() + 1, | 193 EXPECT_EQ(menu_model->GetItemCount() + 1, |
198 [[menu_control menu] numberOfItems]); | 194 [[menu_control menu] numberOfItems]); |
199 | 195 |
200 ui::MenuModel* found_menu_model = menu_model; | 196 ui::MenuModel* found_menu_model = menu_model; |
(...skipping 27 matching lines...) Expand all Loading... |
228 AppListMenu::SHOW_SETTINGS, &menu_model, &index)); | 224 AppListMenu::SHOW_SETTINGS, &menu_model, &index)); |
229 EXPECT_EQ(found_menu_model, menu_model); | 225 EXPECT_EQ(found_menu_model, menu_model); |
230 NSMenuItem* settings_item = [[menu_control menu] itemAtIndex:index + 1]; | 226 NSMenuItem* settings_item = [[menu_control menu] itemAtIndex:index + 1]; |
231 EXPECT_FALSE([settings_item view]); | 227 EXPECT_FALSE([settings_item view]); |
232 EXPECT_NSEQ(base::SysUTF16ToNSString(menu_model->GetLabelAt(index)), | 228 EXPECT_NSEQ(base::SysUTF16ToNSString(menu_model->GetLabelAt(index)), |
233 [settings_item title]); | 229 [settings_item title]); |
234 } | 230 } |
235 | 231 |
236 // Test adding another user, and changing an existing one. | 232 // Test adding another user, and changing an existing one. |
237 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenuChangingUsers) { | 233 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenuChangingUsers) { |
238 app_list::AppListModel::Users users = [delegate_ appListModel]->users(); | 234 app_list::AppListViewDelegate::Users users = |
| 235 [delegate_ appListDelegate]->GetUsers(); |
239 EXPECT_EQ(2u, users.size()); | 236 EXPECT_EQ(2u, users.size()); |
240 ui::MenuModel* menu_model | 237 ui::MenuModel* menu_model |
241 = [apps_search_box_controller_ appListMenu]->menu_model(); | 238 = [apps_search_box_controller_ appListMenu]->menu_model(); |
242 // Adding one to account for the empty item at index 0 in Cocoa popup menus. | 239 // 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; | 240 int non_user_items = menu_model->GetItemCount() - users.size() + 1; |
244 | 241 |
245 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; | 242 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; |
246 EXPECT_EQ(2, [[menu_control menu] numberOfItems] - non_user_items); | 243 EXPECT_EQ(2, [[menu_control menu] numberOfItems] - non_user_items); |
247 EXPECT_NSEQ(base::SysUTF16ToNSString(users[0].name), | 244 EXPECT_NSEQ(base::SysUTF16ToNSString(users[0].name), |
248 [[[menu_control menu] itemAtIndex:1] title]); | 245 [[[menu_control menu] itemAtIndex:1] title]); |
249 | 246 |
250 users[0].name = ASCIIToUTF16("renamed user"); | 247 users[0].name = ASCIIToUTF16("renamed user"); |
251 app_list::AppListModel::User new_user; | 248 app_list::AppListViewDelegate::User new_user; |
252 new_user.name = ASCIIToUTF16("user3"); | 249 new_user.name = ASCIIToUTF16("user3"); |
253 users.push_back(new_user); | 250 users.push_back(new_user); |
254 [delegate_ appListModel]->SetUsers(users); | 251 [delegate_ appListTestViewDelegate]->SetUsers(users); |
| 252 // Note: menu does not automatically get rebuilt. Force a rebuild (which |
| 253 // would normally occur when the UI is closed / re-opend). |
| 254 [apps_search_box_controller_ rebuildMenu]; |
255 | 255 |
256 // Should now be an extra item, and it should have correct titles. | 256 // Should now be an extra item, and it should have correct titles. |
257 EXPECT_EQ(3, [[menu_control menu] numberOfItems] - non_user_items); | 257 EXPECT_EQ(3, [[menu_control menu] numberOfItems] - non_user_items); |
258 EXPECT_NSEQ(base::SysUTF16ToNSString(users[0].name), | 258 EXPECT_NSEQ(base::SysUTF16ToNSString(users[0].name), |
259 [[[menu_control menu] itemAtIndex:1] title]); | 259 [[[menu_control menu] itemAtIndex:1] title]); |
260 EXPECT_NSEQ(base::SysUTF16ToNSString(new_user.name), | 260 EXPECT_NSEQ(base::SysUTF16ToNSString(new_user.name), |
261 [[[menu_control menu] itemAtIndex:3] title]); | 261 [[[menu_control menu] itemAtIndex:3] title]); |
262 } | 262 } |
263 | 263 |
264 } // namespace test | 264 } // namespace test |
265 } // namespace app_list | 265 } // namespace app_list |
OLD | NEW |