Chromium Code Reviews| 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 |
| 151 // Test the popup menu items when there is only one user.. | |
| 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 | |
| 158 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; | |
| 159 EXPECT_TRUE([apps_search_box_controller_ appListMenu]); | |
| 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) { | |
|
tapted
2013/09/16 20:46:44
nit: no braces
calamity
2013/09/17 00:09:26
Done.
| |
| 170 EXPECT_LT(menu_model->GetCommandIdAt(i), AppListMenu::SELECT_PROFILE); | |
|
tapted
2013/09/16 20:46:44
this is cool - I'd keep it. But I'd also add (outs
tapted
2013/09/16 21:02:20
oops. and to nit my own comment: the "more constan
calamity
2013/09/17 00:09:26
Done.
| |
| 171 } | |
| 172 } | |
| 173 | |
| 142 // Test the popup menu items. | 174 // Test the popup menu items. |
|
tapted
2013/09/16 20:46:44
nit: ... for the multi-profile case.
calamity
2013/09/17 00:09:26
Done.
| |
| 143 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenu) { | 175 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenu) { |
| 176 const app_list::AppListModel::Users& users = | |
| 177 [delegate_ appListModel]->users(); | |
| 144 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; | 178 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; |
| 145 EXPECT_TRUE([apps_search_box_controller_ appListMenu]); | 179 EXPECT_TRUE([apps_search_box_controller_ appListMenu]); |
| 146 ui::MenuModel* menu_model | 180 ui::MenuModel* menu_model |
| 147 = [apps_search_box_controller_ appListMenu]->menu_model(); | 181 = [apps_search_box_controller_ appListMenu]->menu_model(); |
| 148 // Add one to the item count to account for the blank, first item that Cocoa | 182 // Add one to the item count to account for the blank, first item that Cocoa |
| 149 // has in its popup menus. | 183 // has in its popup menus. |
| 150 EXPECT_EQ(menu_model->GetItemCount() + 1, | 184 EXPECT_EQ(menu_model->GetItemCount() + 1, |
| 151 [[menu_control menu] numberOfItems]); | 185 [[menu_control menu] numberOfItems]); |
| 152 | 186 |
| 153 // The CURRENT_USER item should contain our custom view. | |
| 154 ui::MenuModel* found_menu_model = menu_model; | 187 ui::MenuModel* found_menu_model = menu_model; |
| 155 int index; | 188 int index; |
| 189 MenuController* controller = [[menu_control menu] delegate]; | |
| 190 | |
| 191 // The first user item is an unchecked label. | |
| 156 EXPECT_TRUE(ui::MenuModel::GetModelAndIndexForCommandId( | 192 EXPECT_TRUE(ui::MenuModel::GetModelAndIndexForCommandId( |
| 157 AppListMenu::CURRENT_USER, &menu_model, &index)); | 193 AppListMenu::SELECT_PROFILE, &menu_model, &index)); |
| 158 EXPECT_EQ(found_menu_model, menu_model); | 194 EXPECT_EQ(found_menu_model, menu_model); |
| 159 NSMenuItem* current_user_item = [[menu_control menu] itemAtIndex:index + 1]; | 195 NSMenuItem* unchecked_user_item = [[menu_control menu] itemAtIndex:index + 1]; |
| 160 EXPECT_TRUE([current_user_item view]); | 196 [controller validateUserInterfaceItem:unchecked_user_item]; |
| 197 // The profile name should be shown if there is no email available. | |
| 198 EXPECT_NSEQ(base::SysUTF16ToNSString(users[0].name), | |
| 199 [unchecked_user_item title]); | |
| 200 EXPECT_EQ(NSOffState, [unchecked_user_item state]); | |
| 201 | |
| 202 // The second user item is a checked label because it is the active profile. | |
| 203 EXPECT_TRUE(ui::MenuModel::GetModelAndIndexForCommandId( | |
| 204 AppListMenu::SELECT_PROFILE + 1, &menu_model, &index)); | |
| 205 EXPECT_EQ(found_menu_model, menu_model); | |
| 206 NSMenuItem* checked_user_item = [[menu_control menu] itemAtIndex:index + 1]; | |
| 207 [controller validateUserInterfaceItem:checked_user_item]; | |
| 208 // The email is shown when available. | |
| 209 EXPECT_NSEQ(base::SysUTF16ToNSString(users[1].email), | |
| 210 [checked_user_item title]); | |
| 211 EXPECT_EQ(NSOnState, [checked_user_item state]); | |
| 161 | 212 |
| 162 // A regular item should have just the label. | 213 // A regular item should have just the label. |
| 163 EXPECT_TRUE(ui::MenuModel::GetModelAndIndexForCommandId( | 214 EXPECT_TRUE(ui::MenuModel::GetModelAndIndexForCommandId( |
| 164 AppListMenu::SHOW_SETTINGS, &menu_model, &index)); | 215 AppListMenu::SHOW_SETTINGS, &menu_model, &index)); |
| 165 EXPECT_EQ(found_menu_model, menu_model); | 216 EXPECT_EQ(found_menu_model, menu_model); |
| 166 NSMenuItem* settings_item = [[menu_control menu] itemAtIndex:index + 1]; | 217 NSMenuItem* settings_item = [[menu_control menu] itemAtIndex:index + 1]; |
| 167 EXPECT_FALSE([settings_item view]); | 218 EXPECT_FALSE([settings_item view]); |
| 168 EXPECT_NSEQ(base::SysUTF16ToNSString(menu_model->GetLabelAt(index)), | 219 EXPECT_NSEQ(base::SysUTF16ToNSString(menu_model->GetLabelAt(index)), |
| 169 [settings_item title]); | 220 [settings_item title]); |
| 170 } | 221 } |
| 171 | 222 |
| 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 | 223 } // namespace test |
| 198 } // namespace app_list | 224 } // namespace app_list |
| OLD | NEW |