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