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 #include "ui/app_list/app_list_model_observer.h" |
12 #include "ui/app_list/search_box_model.h" | 13 #include "ui/app_list/search_box_model.h" |
13 #include "ui/app_list/test/app_list_test_model.h" | 14 #include "ui/app_list/test/app_list_test_model.h" |
14 #include "ui/app_list/test/app_list_test_view_delegate.h" | 15 #include "ui/app_list/test/app_list_test_view_delegate.h" |
15 #import "ui/base/cocoa/menu_controller.h" | 16 #import "ui/base/cocoa/menu_controller.h" |
16 #import "ui/base/test/ui_cocoa_test_helper.h" | 17 #import "ui/base/test/ui_cocoa_test_helper.h" |
17 | 18 |
18 @interface TestAppsSearchBoxDelegate : NSObject<AppsSearchBoxDelegate> { | 19 @interface TestAppsSearchBoxDelegate : NSObject<AppsSearchBoxDelegate> { |
19 @private | 20 @private |
20 app_list::SearchBoxModel searchBoxModel_; | 21 app_list::SearchBoxModel searchBoxModel_; |
21 app_list::test::AppListTestViewDelegate appListDelegate_; | 22 app_list::test::AppListTestViewDelegate appListDelegate_; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 68 |
68 - (app_list::AppListModel*)appListModel { | 69 - (app_list::AppListModel*)appListModel { |
69 return &appListModel_; | 70 return &appListModel_; |
70 } | 71 } |
71 | 72 |
72 @end | 73 @end |
73 | 74 |
74 namespace app_list { | 75 namespace app_list { |
75 namespace test { | 76 namespace test { |
76 | 77 |
77 class AppsSearchBoxControllerTest : public ui::CocoaTest { | 78 class AppsSearchBoxControllerTest : public ui::CocoaTest, |
| 79 public AppListModelObserver { |
78 public: | 80 public: |
79 AppsSearchBoxControllerTest() { | 81 AppsSearchBoxControllerTest() { |
80 Init(); | 82 Init(); |
81 } | 83 } |
82 | 84 |
83 virtual void SetUp() OVERRIDE { | 85 virtual void SetUp() OVERRIDE { |
84 apps_search_box_controller_.reset( | 86 apps_search_box_controller_.reset( |
85 [[AppsSearchBoxController alloc] initWithFrame: | 87 [[AppsSearchBoxController alloc] initWithFrame: |
86 NSMakeRect(0, 0, 400, 100)]); | 88 NSMakeRect(0, 0, 400, 100)]); |
87 delegate_.reset([[TestAppsSearchBoxDelegate alloc] init]); | 89 delegate_.reset([[TestAppsSearchBoxDelegate alloc] init]); |
88 [apps_search_box_controller_ setDelegate:delegate_]; | 90 [apps_search_box_controller_ setDelegate:delegate_]; |
| 91 [delegate_ appListModel]->AddObserver(this); |
89 | 92 |
90 ui::CocoaTest::SetUp(); | 93 ui::CocoaTest::SetUp(); |
91 [[test_window() contentView] addSubview:[apps_search_box_controller_ view]]; | 94 [[test_window() contentView] addSubview:[apps_search_box_controller_ view]]; |
92 } | 95 } |
93 | 96 |
94 virtual void TearDown() OVERRIDE { | 97 virtual void TearDown() OVERRIDE { |
| 98 [delegate_ appListModel]->RemoveObserver(this); |
95 [apps_search_box_controller_ setDelegate:nil]; | 99 [apps_search_box_controller_ setDelegate:nil]; |
96 ui::CocoaTest::TearDown(); | 100 ui::CocoaTest::TearDown(); |
97 } | 101 } |
98 | 102 |
99 void SimulateKeyAction(SEL c) { | 103 void SimulateKeyAction(SEL c) { |
100 NSControl* control = [apps_search_box_controller_ searchTextField]; | 104 NSControl* control = [apps_search_box_controller_ searchTextField]; |
101 [apps_search_box_controller_ control:control | 105 [apps_search_box_controller_ control:control |
102 textView:nil | 106 textView:nil |
103 doCommandBySelector:c]; | 107 doCommandBySelector:c]; |
104 } | 108 } |
105 | 109 |
106 protected: | 110 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 |
107 base::scoped_nsobject<TestAppsSearchBoxDelegate> delegate_; | 118 base::scoped_nsobject<TestAppsSearchBoxDelegate> delegate_; |
108 base::scoped_nsobject<AppsSearchBoxController> apps_search_box_controller_; | 119 base::scoped_nsobject<AppsSearchBoxController> apps_search_box_controller_; |
109 | 120 |
110 private: | 121 private: |
111 DISALLOW_COPY_AND_ASSIGN(AppsSearchBoxControllerTest); | 122 DISALLOW_COPY_AND_ASSIGN(AppsSearchBoxControllerTest); |
112 }; | 123 }; |
113 | 124 |
114 TEST_VIEW(AppsSearchBoxControllerTest, [apps_search_box_controller_ view]); | 125 TEST_VIEW(AppsSearchBoxControllerTest, [apps_search_box_controller_ view]); |
115 | 126 |
116 // Test the search box initialization, and search input and clearing. | 127 // Test the search box initialization, and search input and clearing. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 // A regular item should have just the label. | 226 // A regular item should have just the label. |
216 EXPECT_TRUE(ui::MenuModel::GetModelAndIndexForCommandId( | 227 EXPECT_TRUE(ui::MenuModel::GetModelAndIndexForCommandId( |
217 AppListMenu::SHOW_SETTINGS, &menu_model, &index)); | 228 AppListMenu::SHOW_SETTINGS, &menu_model, &index)); |
218 EXPECT_EQ(found_menu_model, menu_model); | 229 EXPECT_EQ(found_menu_model, menu_model); |
219 NSMenuItem* settings_item = [[menu_control menu] itemAtIndex:index + 1]; | 230 NSMenuItem* settings_item = [[menu_control menu] itemAtIndex:index + 1]; |
220 EXPECT_FALSE([settings_item view]); | 231 EXPECT_FALSE([settings_item view]); |
221 EXPECT_NSEQ(base::SysUTF16ToNSString(menu_model->GetLabelAt(index)), | 232 EXPECT_NSEQ(base::SysUTF16ToNSString(menu_model->GetLabelAt(index)), |
222 [settings_item title]); | 233 [settings_item title]); |
223 } | 234 } |
224 | 235 |
| 236 // Test adding another user, and changing an existing one. |
| 237 TEST_F(AppsSearchBoxControllerTest, SearchBoxMenuChangingUsers) { |
| 238 app_list::AppListModel::Users users = [delegate_ appListModel]->users(); |
| 239 EXPECT_EQ(2u, users.size()); |
| 240 ui::MenuModel* menu_model |
| 241 = [apps_search_box_controller_ appListMenu]->menu_model(); |
| 242 // 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; |
| 244 |
| 245 NSPopUpButton* menu_control = [apps_search_box_controller_ menuControl]; |
| 246 EXPECT_EQ(2, [[menu_control menu] numberOfItems] - non_user_items); |
| 247 EXPECT_NSEQ(base::SysUTF16ToNSString(users[0].name), |
| 248 [[[menu_control menu] itemAtIndex:1] title]); |
| 249 |
| 250 users[0].name = ASCIIToUTF16("renamed user"); |
| 251 app_list::AppListModel::User new_user; |
| 252 new_user.name = ASCIIToUTF16("user3"); |
| 253 users.push_back(new_user); |
| 254 [delegate_ appListModel]->SetUsers(users); |
| 255 |
| 256 // Should now be an extra item, and it should have correct titles. |
| 257 EXPECT_EQ(3, [[menu_control menu] numberOfItems] - non_user_items); |
| 258 EXPECT_NSEQ(base::SysUTF16ToNSString(users[0].name), |
| 259 [[[menu_control menu] itemAtIndex:1] title]); |
| 260 EXPECT_NSEQ(base::SysUTF16ToNSString(new_user.name), |
| 261 [[[menu_control menu] itemAtIndex:3] title]); |
| 262 } |
| 263 |
225 } // namespace test | 264 } // namespace test |
226 } // namespace app_list | 265 } // namespace app_list |
OLD | NEW |