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/avatar_button_controller.h"
7 #include "base/command_line.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/sys_string_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/browser/profiles/profiles_state.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_window.h"
15 #import "chrome/browser/ui/cocoa/base_bubble_controller.h"
16 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
17 #include "chrome/browser/ui/cocoa/info_bubble_window.h"
18 #import "chrome/browser/ui/cocoa/profiles/profile_chooser_controller.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/grit/generated_resources.h"
21 #include "components/signin/core/common/profile_management_switches.h"
22 #include "grit/theme_resources.h"
23 #import "testing/gtest_mac.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/base/resource/resource_bundle.h"
27 // Defined in the AvatarButtonController implementation.
28 @interface AvatarButtonController (ExposedForTesting)
29 - (void)updateErrorStatus:(BOOL)hasError;
32 class AvatarButtonControllerTest : public CocoaProfileTest {
34 void SetUp() override {
35 switches::EnableNewAvatarMenuForTesting(
36 base::CommandLine::ForCurrentProcess());
37 DCHECK(profiles::IsMultipleProfilesEnabled());
39 CocoaProfileTest::SetUp();
40 ASSERT_TRUE(browser());
43 [[AvatarButtonController alloc] initWithBrowser:browser()]);
46 void TearDown() override {
47 browser()->window()->Close();
48 CocoaProfileTest::TearDown();
51 NSButton* button() { return [controller_ buttonView]; }
53 NSView* view() { return [controller_ view]; }
55 AvatarButtonController* controller() { return controller_.get(); }
58 base::scoped_nsobject<AvatarButtonController> controller_;
61 TEST_F(AvatarButtonControllerTest, GenericButtonShown) {
62 ASSERT_FALSE([view() isHidden]);
63 // There is only one local profile, which means displaying the generic
65 EXPECT_NSEQ(@"", [button() title]);
68 TEST_F(AvatarButtonControllerTest, ProfileButtonShown) {
69 // Create a second profile, to force the button to display the profile name.
70 testing_profile_manager()->CreateTestingProfile("batman");
72 ASSERT_FALSE([view() isHidden]);
73 EXPECT_NSEQ(@"Person 1", [button() title]);
76 TEST_F(AvatarButtonControllerTest, ProfileButtonWithErrorShown) {
77 // Create a second profile, to force the button to display the profile name.
78 testing_profile_manager()->CreateTestingProfile("batman");
80 EXPECT_EQ(0, [button() image].size.width);
81 [controller() updateErrorStatus:true];
83 ASSERT_FALSE([view() isHidden]);
84 EXPECT_NSEQ(@"Person 1", [button() title]);
86 // If the button has an authentication error, it should display an error icon.
87 int errorWidth = ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(
88 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).Width();
89 EXPECT_EQ(errorWidth, [button() image].size.width);
92 TEST_F(AvatarButtonControllerTest, DoubleOpen) {
93 EXPECT_FALSE([controller() menuController]);
95 [button() performClick:button()];
97 BaseBubbleController* menu = [controller() menuController];
99 EXPECT_TRUE([menu isKindOfClass:[ProfileChooserController class]]);
101 [button() performClick:button()];
102 EXPECT_EQ(menu, [controller() menuController]);
104 // Do not animate out because that is hard to test around.
105 static_cast<InfoBubbleWindow*>(menu.window).allowedAnimations =
106 info_bubble::kAnimateNone;
108 EXPECT_FALSE([controller() menuController]);