| 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" |
| 11 #include "ui/app_list/app_list_menu.h" | 11 #include "ui/app_list/app_list_menu.h" |
| 12 #import "ui/app_list/cocoa/current_user_menu_item_view.h" | |
| 13 #include "ui/app_list/search_box_model.h" | 12 #include "ui/app_list/search_box_model.h" |
| 14 #include "ui/app_list/test/app_list_test_model.h" | 13 #include "ui/app_list/test/app_list_test_model.h" |
| 15 #include "ui/app_list/test/app_list_test_view_delegate.h" | 14 #include "ui/app_list/test/app_list_test_view_delegate.h" |
| 15 #import "ui/base/cocoa/menu_controller.h" |
| 16 #import "ui/base/test/ui_cocoa_test_helper.h" | 16 #import "ui/base/test/ui_cocoa_test_helper.h" |
| 17 | 17 |
| 18 @interface TestAppsSearchBoxDelegate : NSObject<AppsSearchBoxDelegate> { | 18 @interface TestAppsSearchBoxDelegate : NSObject<AppsSearchBoxDelegate> { |
| 19 @private | 19 @private |
| 20 app_list::SearchBoxModel searchBoxModel_; | 20 app_list::SearchBoxModel searchBoxModel_; |
| 21 app_list::test::AppListTestViewDelegate appListDelegate_; | 21 app_list::test::AppListTestViewDelegate appListDelegate_; |
| 22 app_list::test::AppListTestModel appListModel_; |
| 22 int textChangeCount_; | 23 int textChangeCount_; |
| 23 } | 24 } |
| 24 | 25 |
| 25 @property(assign, nonatomic) int textChangeCount; | 26 @property(assign, nonatomic) int textChangeCount; |
| 26 | 27 |
| 27 @end | 28 @end |
| 28 | 29 |
| 29 @implementation TestAppsSearchBoxDelegate | 30 @implementation TestAppsSearchBoxDelegate |
| 30 | 31 |
| 31 @synthesize textChangeCount = textChangeCount_; | 32 @synthesize textChangeCount = textChangeCount_; |
| 32 | 33 |
| 34 - (id)init { |
| 35 if ((self = [super init])) { |
| 36 app_list::AppListModel::Users users(2); |
| 37 users[0].name = ASCIIToUTF16("user1"); |
| 38 users[1].name = ASCIIToUTF16("user2"); |
| 39 users[1].email = ASCIIToUTF16("user2@chromium.org"); |
| 40 users[1].active = true; |
| 41 appListModel_.SetUsers(users); |
| 42 } |
| 43 return self; |
| 44 } |
| 45 |
| 33 - (app_list::SearchBoxModel*)searchBoxModel { | 46 - (app_list::SearchBoxModel*)searchBoxModel { |
| 34 return &searchBoxModel_; | 47 return &searchBoxModel_; |
| 35 } | 48 } |
| 36 | 49 |
| 37 - (app_list::AppListViewDelegate*)appListDelegate { | 50 - (app_list::AppListViewDelegate*)appListDelegate { |
| 38 return &appListDelegate_; | 51 return &appListDelegate_; |
| 39 } | 52 } |
| 40 | 53 |
| 41 - (BOOL)control:(NSControl*)control | 54 - (BOOL)control:(NSControl*)control |
| 42 textView:(NSTextView*)textView | 55 textView:(NSTextView*)textView |
| 43 doCommandBySelector:(SEL)command { | 56 doCommandBySelector:(SEL)command { |
| 44 return NO; | 57 return NO; |
| 45 } | 58 } |
| 46 | 59 |
| 47 - (void)modelTextDidChange { | 60 - (void)modelTextDidChange { |
| 48 ++textChangeCount_; | 61 ++textChangeCount_; |
| 49 } | 62 } |
| 50 | 63 |
| 51 - (CGFloat)bubbleCornerRadius { | 64 - (CGFloat)bubbleCornerRadius { |
| 52 return 3; | 65 return 3; |
| 53 } | 66 } |
| 54 | 67 |
| 55 - (NSString*)currentUserName { | 68 - (app_list::AppListModel*)appListModel { |
| 56 return @""; | 69 return &appListModel_; |
| 57 } | |
| 58 | |
| 59 - (NSString*)currentUserEmail { | |
| 60 return @""; | |
| 61 } | 70 } |
| 62 | 71 |
| 63 @end | 72 @end |
| 64 | 73 |
| 65 namespace app_list { | 74 namespace app_list { |
| 66 namespace test { | 75 namespace test { |
| 67 | 76 |
| 68 class AppsSearchBoxControllerTest : public ui::CocoaTest { | 77 class AppsSearchBoxControllerTest : public ui::CocoaTest { |
| 69 public: | 78 public: |
| 70 AppsSearchBoxControllerTest() { | 79 AppsSearchBoxControllerTest() { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 EXPECT_EQ(3, [delegate_ textChangeCount]); | 141 EXPECT_EQ(3, [delegate_ textChangeCount]); |
| 133 | 142 |
| 134 EXPECT_NSEQ(base::SysUTF16ToNSString(search_text), | 143 EXPECT_NSEQ(base::SysUTF16ToNSString(search_text), |
| 135 [[apps_search_box_controller_ searchTextField] stringValue]); | 144 [[apps_search_box_controller_ searchTextField] stringValue]); |
| 136 SimulateKeyAction(@selector(complete:)); | 145 SimulateKeyAction(@selector(complete:)); |
| 137 EXPECT_NSEQ([NSString string], | 146 EXPECT_NSEQ([NSString string], |
| 138 [[apps_search_box_controller_ searchTextField] stringValue]); | 147 [[apps_search_box_controller_ searchTextField] stringValue]); |
| 139 EXPECT_EQ(4, [delegate_ textChangeCount]); | 148 EXPECT_EQ(4, [delegate_ textChangeCount]); |
| 140 } | 149 } |
| 141 | 150 |
| 142 // Test the popup menu items. | 151 // Test the popup menu items when there is only one user.. |
| 143 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenu) { | 152 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenuSingleUser) { |
| 153 // Set a single user. We need to set the delegate again because the |
| 154 // AppListModel observer isn't hooked up in these tests. |
| 155 [delegate_ appListModel]->SetUsers(app_list::AppListModel::Users(1)); |
| 156 [apps_search_box_controller_ setDelegate:delegate_]; |
| 157 |
| 144 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; | 158 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; |
| 145 EXPECT_TRUE([apps_search_box_controller_ appListMenu]); | 159 EXPECT_TRUE([apps_search_box_controller_ appListMenu]); |
| 146 ui::MenuModel* menu_model | 160 ui::MenuModel* menu_model |
| 161 = [apps_search_box_controller_ appListMenu]->menu_model(); |
| 162 // Add one to the item count to account for the blank, first item that Cocoa |
| 163 // has in its popup menus. |
| 164 EXPECT_EQ(menu_model->GetItemCount() + 1, |
| 165 [[menu_control menu] numberOfItems]); |
| 166 |
| 167 // All command ids should be less than |SELECT_PROFILE| as no user menu items |
| 168 // are being shown. |
| 169 for (int i = 0; i < menu_model->GetItemCount(); ++i) |
| 170 EXPECT_LT(menu_model->GetCommandIdAt(i), AppListMenu::SELECT_PROFILE); |
| 171 |
| 172 // The number of items should match the index that starts profile items. |
| 173 EXPECT_EQ(AppListMenu::SELECT_PROFILE, menu_model->GetItemCount()); |
| 174 } |
| 175 |
| 176 // Test the popup menu items for the multi-profile case. |
| 177 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenu) { |
| 178 const app_list::AppListModel::Users& users = |
| 179 [delegate_ appListModel]->users(); |
| 180 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; |
| 181 EXPECT_TRUE([apps_search_box_controller_ appListMenu]); |
| 182 ui::MenuModel* menu_model |
| 147 = [apps_search_box_controller_ appListMenu]->menu_model(); | 183 = [apps_search_box_controller_ appListMenu]->menu_model(); |
| 148 // Add one to the item count to account for the blank, first item that Cocoa | 184 // Add one to the item count to account for the blank, first item that Cocoa |
| 149 // has in its popup menus. | 185 // has in its popup menus. |
| 150 EXPECT_EQ(menu_model->GetItemCount() + 1, | 186 EXPECT_EQ(menu_model->GetItemCount() + 1, |
| 151 [[menu_control menu] numberOfItems]); | 187 [[menu_control menu] numberOfItems]); |
| 152 | 188 |
| 153 // The CURRENT_USER item should contain our custom view. | |
| 154 ui::MenuModel* found_menu_model = menu_model; | 189 ui::MenuModel* found_menu_model = menu_model; |
| 155 int index; | 190 int index; |
| 191 MenuController* controller = [[menu_control menu] delegate]; |
| 192 |
| 193 // The first user item is an unchecked label. |
| 156 EXPECT_TRUE(ui::MenuModel::GetModelAndIndexForCommandId( | 194 EXPECT_TRUE(ui::MenuModel::GetModelAndIndexForCommandId( |
| 157 AppListMenu::CURRENT_USER, &menu_model, &index)); | 195 AppListMenu::SELECT_PROFILE, &menu_model, &index)); |
| 158 EXPECT_EQ(found_menu_model, menu_model); | 196 EXPECT_EQ(found_menu_model, menu_model); |
| 159 NSMenuItem* current_user_item = [[menu_control menu] itemAtIndex:index + 1]; | 197 NSMenuItem* unchecked_user_item = [[menu_control menu] itemAtIndex:index + 1]; |
| 160 EXPECT_TRUE([current_user_item view]); | 198 [controller validateUserInterfaceItem:unchecked_user_item]; |
| 199 // The profile name should be shown if there is no email available. |
| 200 EXPECT_NSEQ(base::SysUTF16ToNSString(users[0].name), |
| 201 [unchecked_user_item title]); |
| 202 EXPECT_EQ(NSOffState, [unchecked_user_item state]); |
| 203 |
| 204 // The second user item is a checked label because it is the active profile. |
| 205 EXPECT_TRUE(ui::MenuModel::GetModelAndIndexForCommandId( |
| 206 AppListMenu::SELECT_PROFILE + 1, &menu_model, &index)); |
| 207 EXPECT_EQ(found_menu_model, menu_model); |
| 208 NSMenuItem* checked_user_item = [[menu_control menu] itemAtIndex:index + 1]; |
| 209 [controller validateUserInterfaceItem:checked_user_item]; |
| 210 // The email is shown when available. |
| 211 EXPECT_NSEQ(base::SysUTF16ToNSString(users[1].email), |
| 212 [checked_user_item title]); |
| 213 EXPECT_EQ(NSOnState, [checked_user_item state]); |
| 161 | 214 |
| 162 // A regular item should have just the label. | 215 // A regular item should have just the label. |
| 163 EXPECT_TRUE(ui::MenuModel::GetModelAndIndexForCommandId( | 216 EXPECT_TRUE(ui::MenuModel::GetModelAndIndexForCommandId( |
| 164 AppListMenu::SHOW_SETTINGS, &menu_model, &index)); | 217 AppListMenu::SHOW_SETTINGS, &menu_model, &index)); |
| 165 EXPECT_EQ(found_menu_model, menu_model); | 218 EXPECT_EQ(found_menu_model, menu_model); |
| 166 NSMenuItem* settings_item = [[menu_control menu] itemAtIndex:index + 1]; | 219 NSMenuItem* settings_item = [[menu_control menu] itemAtIndex:index + 1]; |
| 167 EXPECT_FALSE([settings_item view]); | 220 EXPECT_FALSE([settings_item view]); |
| 168 EXPECT_NSEQ(base::SysUTF16ToNSString(menu_model->GetLabelAt(index)), | 221 EXPECT_NSEQ(base::SysUTF16ToNSString(menu_model->GetLabelAt(index)), |
| 169 [settings_item title]); | 222 [settings_item title]); |
| 170 } | 223 } |
| 171 | 224 |
| 172 // Test initialization and display of the custom menu item that shows the | |
| 173 // currently signed-in user. This is a non-interactive view. | |
| 174 class AppsSearchBoxCustomMenuItemTest : public ui::CocoaTest { | |
| 175 public: | |
| 176 AppsSearchBoxCustomMenuItemTest() { | |
| 177 Init(); | |
| 178 } | |
| 179 | |
| 180 virtual void SetUp() OVERRIDE { | |
| 181 current_user_menu_item_.reset([[[CurrentUserMenuItemView alloc] | |
| 182 initWithCurrentUser:@"testUser" | |
| 183 userEmail:@"testUser@chromium.org"] retain]); | |
| 184 ui::CocoaTest::SetUp(); | |
| 185 [[test_window() contentView] addSubview:current_user_menu_item_]; | |
| 186 } | |
| 187 | |
| 188 protected: | |
| 189 base::scoped_nsobject<NSView> current_user_menu_item_; | |
| 190 | |
| 191 private: | |
| 192 DISALLOW_COPY_AND_ASSIGN(AppsSearchBoxCustomMenuItemTest); | |
| 193 }; | |
| 194 | |
| 195 TEST_VIEW(AppsSearchBoxCustomMenuItemTest, current_user_menu_item_); | |
| 196 | |
| 197 } // namespace test | 225 } // namespace test |
| 198 } // namespace app_list | 226 } // namespace app_list |
| OLD | NEW |