1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/profiles/profile_chooser_controller.h"
7 #include "base/command_line.h"
8 #import "base/mac/foundation_util.h"
9 #include "base/mac/scoped_nsobject.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/prefs/pref_service_syncable.h"
14 #include "chrome/browser/profiles/avatar_menu.h"
15 #include "chrome/browser/profiles/profile_info_cache.h"
16 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
17 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
18 #include "chrome/browser/signin/account_fetcher_service_factory.h"
19 #include "chrome/browser/signin/fake_account_fetcher_service.h"
20 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
21 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
23 #include "chrome/browser/signin/signin_header_helper.h"
24 #include "chrome/browser/signin/signin_manager_factory.h"
25 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/pref_names.h"
29 #include "components/signin/core/browser/profile_oauth2_token_service.h"
30 #include "components/signin/core/browser/signin_manager.h"
31 #include "components/signin/core/common/profile_management_switches.h"
33 const std::string kGaiaId = "gaiaid-user@gmail.com";
34 const std::string kEmail = "user@gmail.com";
35 const std::string kSecondaryEmail = "user2@gmail.com";
36 const std::string kLoginToken = "oauth2_login_token";
38 class ProfileChooserControllerTest : public CocoaProfileTest {
40 ProfileChooserControllerTest() {
41 TestingProfile::TestingFactories factories;
43 std::make_pair(ProfileOAuth2TokenServiceFactory::GetInstance(),
44 BuildFakeProfileOAuth2TokenService));
46 std::make_pair(AccountFetcherServiceFactory::GetInstance(),
47 FakeAccountFetcherService::BuildForTests));
48 AddTestingFactories(factories);
51 void SetUp() override {
52 CocoaProfileTest::SetUp();
54 ASSERT_TRUE(browser()->profile());
56 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory(
57 browser()->profile(), gcm::FakeGCMProfileService::Build);
59 testing_profile_manager()->
60 CreateTestingProfile("test1", scoped_ptr<PrefServiceSyncable>(),
61 base::ASCIIToUTF16("Test 1"), 0, std::string(),
63 testing_profile_manager()->
64 CreateTestingProfile("test2", scoped_ptr<PrefServiceSyncable>(),
65 base::ASCIIToUTF16("Test 2"), 1, std::string(),
66 TestingProfile::TestingFactories());
68 menu_ = new AvatarMenu(testing_profile_manager()->profile_info_cache(),
72 // There should be the default profile + two profiles we created.
73 EXPECT_EQ(3U, menu_->GetNumberOfItems());
76 void TearDown() override {
79 CocoaProfileTest::TearDown();
82 void StartProfileChooserController() {
83 StartProfileChooserControllerWithTutorialMode(profiles::TUTORIAL_MODE_NONE);
86 void StartProfileChooserControllerWithTutorialMode(
87 profiles::TutorialMode mode) {
88 NSRect frame = [test_window() frame];
89 NSPoint point = NSMakePoint(NSMidX(frame), NSMidY(frame));
90 controller_.reset([[ProfileChooserController alloc]
91 initWithBrowser:browser()
93 viewMode:profiles::BUBBLE_VIEW_MODE_PROFILE_CHOOSER
95 serviceType:signin::GAIA_SERVICE_TYPE_NONE]);
96 [controller_ showWindow:nil];
99 void AssertRightClickTutorialShown() {
100 NSArray* subviews = [[[controller() window] contentView] subviews];
101 ASSERT_EQ(2U, [subviews count]);
102 subviews = [[subviews objectAtIndex:0] subviews];
104 // There should be 4 views: the tutorial, the active profile card, a
105 // separator and the options view.
106 ASSERT_EQ(4U, [subviews count]);
108 // The tutorial is the topmost view, so the last in the array. It should
109 // contain 3 views: the title, the content text and the OK button.
110 NSArray* tutorialSubviews = [[subviews objectAtIndex:3] subviews];
111 ASSERT_EQ(3U, [tutorialSubviews count]);
113 NSTextField* tutorialTitle = base::mac::ObjCCastStrict<NSTextField>(
114 [tutorialSubviews objectAtIndex:2]);
115 EXPECT_GT([[tutorialTitle stringValue] length], 0U);
117 NSTextField* tutorialContent = base::mac::ObjCCastStrict<NSTextField>(
118 [tutorialSubviews objectAtIndex:1]);
119 EXPECT_GT([[tutorialContent stringValue] length], 0U);
121 NSButton* tutorialOKButton = base::mac::ObjCCastStrict<NSButton>(
122 [tutorialSubviews objectAtIndex:0]);
123 EXPECT_GT([[tutorialOKButton title] length], 0U);
126 void StartFastUserSwitcher() {
127 NSRect frame = [test_window() frame];
128 NSPoint point = NSMakePoint(NSMidX(frame), NSMidY(frame));
129 controller_.reset([[ProfileChooserController alloc]
130 initWithBrowser:browser()
132 viewMode:profiles::BUBBLE_VIEW_MODE_FAST_PROFILE_CHOOSER
133 tutorialMode:profiles::TUTORIAL_MODE_NONE
134 serviceType:signin::GAIA_SERVICE_TYPE_NONE]);
135 [controller_ showWindow:nil];
138 ProfileChooserController* controller() { return controller_; }
139 AvatarMenu* menu() { return menu_; }
142 base::scoped_nsobject<ProfileChooserController> controller_;
144 // Weak; owned by |controller_|.
147 DISALLOW_COPY_AND_ASSIGN(ProfileChooserControllerTest);
150 TEST_F(ProfileChooserControllerTest, InitialLayoutWithNewMenu) {
151 switches::EnableNewAvatarMenuForTesting(
152 base::CommandLine::ForCurrentProcess());
153 StartProfileChooserController();
155 NSArray* subviews = [[[controller() window] contentView] subviews];
156 ASSERT_EQ(2U, [subviews count]);
157 subviews = [[subviews objectAtIndex:0] subviews];
159 // Three profiles means we should have one active card, one separator and
160 // one option buttons view. We also have an update promo for the new avatar
162 // TODO(noms): Enforcing 4U fails on the waterfall debug bots, but it's not
163 // reproducible anywhere else.
164 ASSERT_GE([subviews count], 3U);
166 // There should be two buttons and a separator in the option buttons view.
167 NSArray* buttonSubviews = [[subviews objectAtIndex:0] subviews];
168 ASSERT_EQ(3U, [buttonSubviews count]);
170 // There should be an incognito button.
171 NSButton* incognitoButton =
172 base::mac::ObjCCast<NSButton>([buttonSubviews objectAtIndex:0]);
173 EXPECT_EQ(@selector(goIncognito:), [incognitoButton action]);
174 EXPECT_EQ(controller(), [incognitoButton target]);
176 // There should be a separator.
177 EXPECT_TRUE([[subviews objectAtIndex:1] isKindOfClass:[NSBox class]]);
179 // There should be a user switcher button.
180 NSButton* userSwitcherButton =
181 base::mac::ObjCCast<NSButton>([buttonSubviews objectAtIndex:2]);
182 EXPECT_EQ(@selector(showUserManager:), [userSwitcherButton action]);
183 EXPECT_EQ(controller(), [userSwitcherButton target]);
185 // There should be a separator.
186 EXPECT_TRUE([[subviews objectAtIndex:1] isKindOfClass:[NSBox class]]);
188 // There should be the profile avatar, name and links container in the active
189 // card view. The links displayed in the container are checked separately.
190 NSArray* activeCardSubviews = [[subviews objectAtIndex:2] subviews];
191 ASSERT_EQ(3U, [activeCardSubviews count]);
194 NSView* activeProfileImage = [activeCardSubviews objectAtIndex:2];
195 EXPECT_TRUE([activeProfileImage isKindOfClass:[NSButton class]]);
198 NSView* activeProfileName = [activeCardSubviews objectAtIndex:1];
199 EXPECT_TRUE([activeProfileName isKindOfClass:[NSButton class]]);
200 EXPECT_EQ(menu()->GetItemAt(0).name, base::SysNSStringToUTF16(
201 [base::mac::ObjCCast<NSButton>(activeProfileName) title]));
203 // Profile links. This is a local profile, so there should be a signin button
204 // and a signin promo.
205 NSArray* linksSubviews = [[activeCardSubviews objectAtIndex:0] subviews];
206 ASSERT_EQ(2U, [linksSubviews count]);
207 NSButton* link = base::mac::ObjCCast<NSButton>(
208 [linksSubviews objectAtIndex:0]);
209 EXPECT_EQ(@selector(showInlineSigninPage:), [link action]);
210 EXPECT_EQ(controller(), [link target]);
212 NSTextField* promo = base::mac::ObjCCast<NSTextField>(
213 [linksSubviews objectAtIndex:1]);
214 EXPECT_GT([[promo stringValue] length], 0U);
217 TEST_F(ProfileChooserControllerTest, RightClickTutorialShownAfterWelcome) {
218 switches::EnableNewAvatarMenuForTesting(
219 base::CommandLine::ForCurrentProcess());
220 // The welcome upgrade tutorial takes precedence so show it then dismiss it.
221 // The right click tutorial should be shown right away.
222 StartProfileChooserControllerWithTutorialMode(
223 profiles::TUTORIAL_MODE_WELCOME_UPGRADE);
225 [controller() dismissTutorial:nil];
226 AssertRightClickTutorialShown();
229 TEST_F(ProfileChooserControllerTest, RightClickTutorialShownAfterReopen) {
230 switches::EnableNewAvatarMenuForTesting(
231 base::CommandLine::ForCurrentProcess());
232 // The welcome upgrade tutorial takes precedence so show it then close the
233 // menu. Reopening the menu should show the tutorial.
234 StartProfileChooserController();
236 [controller() close];
237 StartProfileChooserController();
238 AssertRightClickTutorialShown();
240 // The tutorial must be manually dismissed so it should still be shown after
241 // closing and reopening the menu,
242 [controller() close];
243 StartProfileChooserController();
244 AssertRightClickTutorialShown();
247 TEST_F(ProfileChooserControllerTest, RightClickTutorialNotShownAfterDismiss) {
248 switches::EnableNewAvatarMenuForTesting(
249 base::CommandLine::ForCurrentProcess());
250 // The welcome upgrade tutorial takes precedence so show it then close the
251 // menu. Reopening the menu should show the tutorial.
252 StartProfileChooserController();
254 [controller() close];
255 StartProfileChooserControllerWithTutorialMode(
256 profiles::TUTORIAL_MODE_RIGHT_CLICK_SWITCHING);
257 AssertRightClickTutorialShown();
259 // Dismissing the tutorial should prevent it from being shown forever.
260 [controller() dismissTutorial:nil];
261 NSArray* subviews = [[[controller() window] contentView] subviews];
262 ASSERT_EQ(2U, [subviews count]);
263 subviews = [[subviews objectAtIndex:0] subviews];
265 // There should be 3 views since there's no tutorial
266 ASSERT_EQ(3U, [subviews count]);
268 // Closing and reopening the menu shouldn't show the tutorial.
269 [controller() close];
270 StartProfileChooserControllerWithTutorialMode(
271 profiles::TUTORIAL_MODE_RIGHT_CLICK_SWITCHING);
272 subviews = [[[controller() window] contentView] subviews];
273 ASSERT_EQ(2U, [subviews count]);
274 subviews = [[subviews objectAtIndex:0] subviews];
276 // There should be 3 views since there's no tutorial
277 ASSERT_EQ(3U, [subviews count]);
280 TEST_F(ProfileChooserControllerTest, OtherProfilesSortedAlphabetically) {
281 switches::EnableNewAvatarMenuForTesting(
282 base::CommandLine::ForCurrentProcess());
284 // Add two extra profiles, to make sure sorting is alphabetical and not
285 // by order of creation.
286 testing_profile_manager()->
287 CreateTestingProfile("test3", scoped_ptr<PrefServiceSyncable>(),
288 base::ASCIIToUTF16("New Profile"), 1, std::string(),
289 TestingProfile::TestingFactories());
290 testing_profile_manager()->
291 CreateTestingProfile("test4", scoped_ptr<PrefServiceSyncable>(),
292 base::ASCIIToUTF16("Another Test"), 1, std::string(),
293 TestingProfile::TestingFactories());
294 StartFastUserSwitcher();
296 NSArray* subviews = [[[controller() window] contentView] subviews];
297 ASSERT_EQ(2U, [subviews count]);
298 subviews = [[subviews objectAtIndex:0] subviews];
299 NSString* sortedNames[] = { @"Another Test",
303 // There are four "other" profiles, each with a button and a separator.
304 ASSERT_EQ([subviews count], 8U);
305 // There should be four "other profiles" items, sorted alphabetically. The
306 // "other profiles" start at index 2 (after the option buttons view and its
307 // separator), and each have a separator. We need to iterate through the
308 // profiles in the order displayed in the bubble, which is opposite from the
310 int sortedNameIndex = 0;
311 for (int i = 7; i > 0; i -= 2) {
312 // The item at index i is the separator.
313 NSButton* button = base::mac::ObjCCast<NSButton>(
314 [subviews objectAtIndex:i-1]);
316 [[button title] isEqualToString:sortedNames[sortedNameIndex++]]);
320 TEST_F(ProfileChooserControllerTest,
321 LocalProfileActiveCardLinksWithNewMenu) {
322 switches::EnableNewAvatarMenuForTesting(
323 base::CommandLine::ForCurrentProcess());
324 StartProfileChooserController();
325 NSArray* subviews = [[[controller() window] contentView] subviews];
326 ASSERT_EQ(2U, [subviews count]);
327 subviews = [[subviews objectAtIndex:0] subviews];
328 NSArray* activeCardSubviews = [[subviews objectAtIndex:2] subviews];
329 NSArray* activeCardLinks = [[activeCardSubviews objectAtIndex:0] subviews];
331 ASSERT_EQ(2U, [activeCardLinks count]);
333 // There should be a sign in button.
334 NSButton* link = base::mac::ObjCCast<NSButton>(
335 [activeCardLinks objectAtIndex:0]);
336 EXPECT_EQ(@selector(showInlineSigninPage:), [link action]);
337 EXPECT_EQ(controller(), [link target]);
339 // Local profiles have a signin promo.
340 NSTextField* promo = base::mac::ObjCCast<NSTextField>(
341 [activeCardLinks objectAtIndex:1]);
342 EXPECT_GT([[promo stringValue] length], 0U);
345 TEST_F(ProfileChooserControllerTest,
346 SignedInProfileActiveCardLinksWithAccountConsistency) {
347 switches::EnableAccountConsistencyForTesting(
348 base::CommandLine::ForCurrentProcess());
349 // Sign in the first profile.
350 ProfileInfoCache* cache = testing_profile_manager()->profile_info_cache();
351 cache->SetAuthInfoOfProfileAtIndex(0, kGaiaId, base::ASCIIToUTF16(kEmail));
353 StartProfileChooserController();
354 NSArray* subviews = [[[controller() window] contentView] subviews];
355 ASSERT_EQ(2U, [subviews count]);
356 subviews = [[subviews objectAtIndex:0] subviews];
357 NSArray* activeCardSubviews = [[subviews objectAtIndex:2] subviews];
358 NSArray* activeCardLinks = [[activeCardSubviews objectAtIndex:0] subviews];
360 // There is one link: manage accounts.
361 ASSERT_EQ(1U, [activeCardLinks count]);
362 NSButton* manageAccountsLink =
363 base::mac::ObjCCast<NSButton>([activeCardLinks objectAtIndex:0]);
364 EXPECT_EQ(@selector(showAccountManagement:), [manageAccountsLink action]);
365 EXPECT_EQ(controller(), [manageAccountsLink target]);
368 TEST_F(ProfileChooserControllerTest,
369 SignedInProfileActiveCardLinksWithNewMenu) {
370 switches::EnableNewAvatarMenuForTesting(
371 base::CommandLine::ForCurrentProcess());
372 // Sign in the first profile.
373 ProfileInfoCache* cache = testing_profile_manager()->profile_info_cache();
374 cache->SetAuthInfoOfProfileAtIndex(0, kGaiaId, base::ASCIIToUTF16(kEmail));
376 StartProfileChooserController();
377 NSArray* subviews = [[[controller() window] contentView] subviews];
378 ASSERT_EQ(2U, [subviews count]);
379 subviews = [[subviews objectAtIndex:0] subviews];
380 NSArray* activeCardSubviews = [[subviews objectAtIndex:2] subviews];
381 NSArray* activeCardLinks = [[activeCardSubviews objectAtIndex:0] subviews];
383 // There is one disabled button with the user's email.
384 ASSERT_EQ(1U, [activeCardLinks count]);
385 NSButton* emailButton =
386 base::mac::ObjCCast<NSButton>([activeCardLinks objectAtIndex:0]);
387 EXPECT_EQ(kEmail, base::SysNSStringToUTF8([emailButton title]));
388 EXPECT_EQ(nil, [emailButton action]);
389 EXPECT_FALSE([emailButton isEnabled]);
392 TEST_F(ProfileChooserControllerTest, AccountManagementLayout) {
393 switches::EnableAccountConsistencyForTesting(
394 base::CommandLine::ForCurrentProcess());
395 // Sign in the first profile.
396 ProfileInfoCache* cache = testing_profile_manager()->profile_info_cache();
397 cache->SetAuthInfoOfProfileAtIndex(0, kGaiaId, base::ASCIIToUTF16(kEmail));
399 // Mark that we are using the profile name on purpose, so that we don't
400 // fallback to testing the algorithm that chooses which default name
402 cache->SetProfileIsUsingDefaultNameAtIndex(0, false);
404 // Set up the signin manager and the OAuth2Tokens.
405 Profile* profile = browser()->profile();
406 SigninManagerFactory::GetForProfile(profile)->
407 SetAuthenticatedAccountInfo(kEmail, kEmail);
408 ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->
409 UpdateCredentials(kEmail, kLoginToken);
410 ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->
411 UpdateCredentials(kSecondaryEmail, kLoginToken);
413 StartProfileChooserController();
414 [controller() initMenuContentsWithView:
415 profiles::BUBBLE_VIEW_MODE_ACCOUNT_MANAGEMENT];
417 NSArray* subviews = [[[controller() window] contentView] subviews];
418 ASSERT_EQ(2U, [subviews count]);
419 subviews = [[subviews objectAtIndex:0] subviews];
421 // There should be one active card, one accounts container, two separators
422 // and one option buttons view.
423 ASSERT_EQ(5U, [subviews count]);
425 // There should be three buttons and two separators in the option
427 NSArray* buttonSubviews = [[subviews objectAtIndex:0] subviews];
428 ASSERT_EQ(3U, [buttonSubviews count]);
430 // There should be a separator.
431 EXPECT_TRUE([[buttonSubviews objectAtIndex:1] isKindOfClass:[NSBox class]]);
433 // There should be an incognito button.
434 NSButton* incognitoButton =
435 base::mac::ObjCCast<NSButton>([buttonSubviews objectAtIndex:0]);
436 EXPECT_EQ(@selector(goIncognito:), [incognitoButton action]);
437 EXPECT_EQ(controller(), [incognitoButton target]);
439 // There should be a separator.
440 EXPECT_TRUE([[subviews objectAtIndex:3] isKindOfClass:[NSBox class]]);
442 // There should be a user switcher button.
443 NSButton* userSwitcherButton =
444 base::mac::ObjCCast<NSButton>([buttonSubviews objectAtIndex:2]);
445 EXPECT_EQ(@selector(showUserManager:), [userSwitcherButton action]);
446 EXPECT_EQ(controller(), [userSwitcherButton target]);
448 // In the accounts view, there should be the account list container
449 // accounts and one "add accounts" button.
450 NSArray* accountsSubviews = [[subviews objectAtIndex:2] subviews];
451 ASSERT_EQ(2U, [accountsSubviews count]);
453 NSButton* addAccountsButton =
454 base::mac::ObjCCast<NSButton>([accountsSubviews objectAtIndex:0]);
455 EXPECT_EQ(@selector(addAccount:), [addAccountsButton action]);
456 EXPECT_EQ(controller(), [addAccountsButton target]);
458 // There should be two accounts in the account list container.
459 NSArray* accountsListSubviews = [[accountsSubviews objectAtIndex:1] subviews];
460 ASSERT_EQ(2U, [accountsListSubviews count]);
462 NSButton* genericAccount =
463 base::mac::ObjCCast<NSButton>([accountsListSubviews objectAtIndex:0]);
464 NSButton* genericAccountDelete = base::mac::ObjCCast<NSButton>(
465 [[genericAccount subviews] objectAtIndex:0]);
466 EXPECT_EQ(@selector(showAccountRemovalView:), [genericAccountDelete action]);
467 EXPECT_EQ(controller(), [genericAccountDelete target]);
468 EXPECT_NE(-1, [genericAccountDelete tag]);
470 // Primary accounts are always last.
471 NSButton* primaryAccount =
472 base::mac::ObjCCast<NSButton>([accountsListSubviews objectAtIndex:1]);
473 NSButton* primaryAccountDelete = base::mac::ObjCCast<NSButton>(
474 [[primaryAccount subviews] objectAtIndex:0]);
475 EXPECT_EQ(@selector(showAccountRemovalView:), [primaryAccountDelete action]);
476 EXPECT_EQ(controller(), [primaryAccountDelete target]);
477 EXPECT_EQ(-1, [primaryAccountDelete tag]);
479 // There should be another separator.
480 EXPECT_TRUE([[subviews objectAtIndex:3] isKindOfClass:[NSBox class]]);
482 // There should be the profile avatar, name and a "hide accounts" link
483 // container in the active card view.
484 NSArray* activeCardSubviews = [[subviews objectAtIndex:4] subviews];
485 ASSERT_EQ(3U, [activeCardSubviews count]);
488 NSView* activeProfileImage = [activeCardSubviews objectAtIndex:2];
489 EXPECT_TRUE([activeProfileImage isKindOfClass:[NSButton class]]);
492 NSView* activeProfileName = [activeCardSubviews objectAtIndex:1];
493 EXPECT_TRUE([activeProfileName isKindOfClass:[NSButton class]]);
494 EXPECT_EQ(menu()->GetItemAt(0).name, base::SysNSStringToUTF16(
495 [base::mac::ObjCCast<NSButton>(activeProfileName) title]));
497 // Profile links. This is a local profile, so there should be a signin button.
498 NSArray* linksSubviews = [[activeCardSubviews objectAtIndex:0] subviews];
499 ASSERT_EQ(1U, [linksSubviews count]);
500 NSButton* link = base::mac::ObjCCast<NSButton>(
501 [linksSubviews objectAtIndex:0]);
502 EXPECT_EQ(@selector(hideAccountManagement:), [link action]);
503 EXPECT_EQ(controller(), [link target]);
506 TEST_F(ProfileChooserControllerTest, SignedInProfileLockDisabled) {
507 switches::EnableNewProfileManagementForTesting(
508 base::CommandLine::ForCurrentProcess());
509 // Sign in the first profile.
510 ProfileInfoCache* cache = testing_profile_manager()->profile_info_cache();
511 cache->SetAuthInfoOfProfileAtIndex(0, kGaiaId, base::ASCIIToUTF16(kEmail));
512 // The preference, not the email, determines whether the profile can lock.
513 browser()->profile()->GetPrefs()->SetString(
514 prefs::kGoogleServicesHostedDomain, "chromium.org");
516 StartProfileChooserController();
517 NSArray* subviews = [[[controller() window] contentView] subviews];
518 ASSERT_EQ(2U, [subviews count]);
519 subviews = [[subviews objectAtIndex:0] subviews];
521 // There will be two buttons and one separators in the option buttons view.
522 NSArray* buttonSubviews = [[subviews objectAtIndex:0] subviews];
523 ASSERT_EQ(3U, [buttonSubviews count]);
525 // The last button should not be the lock button.
526 NSButton* lastButton =
527 base::mac::ObjCCast<NSButton>([buttonSubviews objectAtIndex:0]);
528 ASSERT_TRUE(lastButton);
529 EXPECT_NE(@selector(lockProfile:), [lastButton action]);
532 TEST_F(ProfileChooserControllerTest, SignedInProfileLockEnabled) {
533 switches::EnableNewProfileManagementForTesting(
534 base::CommandLine::ForCurrentProcess());
535 // Sign in the first profile.
536 ProfileInfoCache* cache = testing_profile_manager()->profile_info_cache();
537 cache->SetAuthInfoOfProfileAtIndex(0, kGaiaId, base::ASCIIToUTF16(kEmail));
538 // The preference, not the email, determines whether the profile can lock.
539 browser()->profile()->GetPrefs()->SetString(
540 prefs::kGoogleServicesHostedDomain, "google.com");
541 // Lock is only available where a supervised user is present.
542 cache->SetSupervisedUserIdOfProfileAtIndex(1, kEmail);
544 StartProfileChooserController();
545 NSArray* subviews = [[[controller() window] contentView] subviews];
546 ASSERT_EQ(2U, [subviews count]);
547 subviews = [[subviews objectAtIndex:0] subviews];
549 // There will be three buttons and two separators in the option buttons view.
550 NSArray* buttonSubviews = [[subviews objectAtIndex:0] subviews];
551 ASSERT_EQ(5U, [buttonSubviews count]);
553 // There should be a lock button.
554 NSButton* lockButton =
555 base::mac::ObjCCast<NSButton>([buttonSubviews objectAtIndex:0]);
556 ASSERT_TRUE(lockButton);
557 EXPECT_EQ(@selector(lockProfile:), [lockButton action]);
558 EXPECT_EQ(controller(), [lockButton target]);
559 EXPECT_TRUE([lockButton isEnabled]);