summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/profiles/avatar_button_controller_unittest.mm
blob: 1e3e7455d6ac286ec53afa43a249fe64d835d6a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/ui/cocoa/profiles/avatar_button_controller.h"

#include "base/command_line.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#import "chrome/browser/ui/cocoa/base_bubble_controller.h"
#include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
#include "chrome/browser/ui/cocoa/info_bubble_window.h"
#import "chrome/browser/ui/cocoa/profiles/profile_chooser_controller.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/grit/generated_resources.h"
#include "components/signin/core/common/profile_management_switches.h"
#include "grit/theme_resources.h"
#import "testing/gtest_mac.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"

// Defined in the AvatarButtonController implementation.
@interface AvatarButtonController (ExposedForTesting)
- (void)updateErrorStatus:(BOOL)hasError;
@end

// Subclassing AvatarButtonController to be able to control the state of
// keyboard modifierFlags.
@interface AvatarButtonControllerForTesting : AvatarButtonController {
 @private
  bool isCtrlPressed_;
}
@end

@interface AvatarButtonControllerForTesting (ExposedForTesting)
- (void)setIsCtrlPressed:(BOOL)isPressed;
- (BOOL)isCtrlPressed;
@end

@implementation AvatarButtonControllerForTesting
- (void)setIsCtrlPressed:(BOOL)isPressed {
  isCtrlPressed_ = isPressed;
}

- (BOOL)isCtrlPressed {
 // Always report that Cmd is not pressed since that's the case we're testing
 // and otherwise running the test while holding the Cmd key makes it fail.
 return isCtrlPressed_;
}
@end

class AvatarButtonControllerTest : public CocoaProfileTest {
 public:
  void SetUp() override {
    DCHECK(profiles::IsMultipleProfilesEnabled());

    CocoaProfileTest::SetUp();
    ASSERT_TRUE(browser());

    controller_.reset(
        [[AvatarButtonControllerForTesting alloc] initWithBrowser:browser()]);
    [controller_ setIsCtrlPressed:false];
  }

  void TearDown() override {
    browser()->window()->Close();
    CocoaProfileTest::TearDown();
  }

  NSButton* button() { return [controller_ buttonView]; }

  NSView* view() { return [controller_ view]; }

  AvatarButtonControllerForTesting* controller() { return controller_.get(); }

 private:
  base::scoped_nsobject<AvatarButtonControllerForTesting> controller_;
};

TEST_F(AvatarButtonControllerTest, GenericButtonShown) {
  ASSERT_FALSE([view() isHidden]);
  // There is only one local profile, which means displaying the generic
  // avatar button.
  EXPECT_NSEQ(@"", [button() title]);
}

TEST_F(AvatarButtonControllerTest, ProfileButtonShown) {
  // Create a second profile, to force the button to display the profile name.
  testing_profile_manager()->CreateTestingProfile("batman");

  ASSERT_FALSE([view() isHidden]);
  EXPECT_NSEQ(@"Person 1", [button() title]);
}

TEST_F(AvatarButtonControllerTest, ProfileButtonWithErrorShown) {
  // Create a second profile, to force the button to display the profile name.
  testing_profile_manager()->CreateTestingProfile("batman");

  EXPECT_EQ(0, [button() image].size.width);
  [controller() updateErrorStatus:true];

  ASSERT_FALSE([view() isHidden]);
  EXPECT_NSEQ(@"Person 1", [button() title]);

  // If the button has an authentication error, it should display an error icon.
  int errorWidth = ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(
      IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).Width();
  EXPECT_EQ(errorWidth, [button() image].size.width);
}

TEST_F(AvatarButtonControllerTest, DoubleOpen) {
  EXPECT_FALSE([controller() menuController]);

  [button() performClick:button()];

  BaseBubbleController* menu = [controller() menuController];
  EXPECT_TRUE(menu);
  EXPECT_TRUE([menu isKindOfClass:[ProfileChooserController class]]);

  [button() performClick:button()];
  EXPECT_EQ(menu, [controller() menuController]);

  // Do not animate out because that is hard to test around.
  static_cast<InfoBubbleWindow*>(menu.window).allowedAnimations =
      info_bubble::kAnimateNone;
  [menu close];
  EXPECT_FALSE([controller() menuController]);
}

TEST_F(AvatarButtonControllerTest, DontOpenFastSwitcherWithoutTarget) {
  EXPECT_FALSE([controller() menuController]);

  [controller() setIsCtrlPressed:YES];
  [button() performClick:button()];

  // If there's only one profile and the fast user switcher is requested,
  // nothing should happen.
  EXPECT_FALSE([controller() menuController]);
}

TEST_F(AvatarButtonControllerTest, OpenFastUserSwitcherWithTarget) {
  testing_profile_manager()->CreateTestingProfile("batman");
  EXPECT_FALSE([controller() menuController]);

  [controller() setIsCtrlPressed:YES];
  [button() performClick:button()];

  BaseBubbleController* menu = [controller() menuController];
  EXPECT_TRUE(menu);
  EXPECT_TRUE([menu isKindOfClass:[ProfileChooserController class]]);

  // Do not animate out because that is hard to test around.
  static_cast<InfoBubbleWindow*>(menu.window).allowedAnimations =
      info_bubble::kAnimateNone;
  [menu close];
  EXPECT_FALSE([controller() menuController]);
}