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_icon_controller.h"
7 #include "base/command_line.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
11 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
12 #include "chrome/browser/prefs/pref_service_syncable.h"
13 #include "chrome/browser/search_engines/template_url_service_factory.h"
14 #include "chrome/browser/supervised_user/supervised_user_service.h"
15 #include "chrome/browser/supervised_user/supervised_user_service_factory.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_commands.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #import "chrome/browser/ui/cocoa/base_bubble_controller.h"
20 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
21 #include "chrome/browser/ui/cocoa/info_bubble_window.h"
22 #import "chrome/browser/ui/cocoa/profiles/avatar_menu_bubble_controller.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "components/bookmarks/test/bookmark_test_helpers.h"
26 #include "components/signin/core/common/profile_management_switches.h"
28 class AvatarIconControllerTest : public CocoaProfileTest {
30 void SetUp() override {
31 switches::DisableNewAvatarMenuForTesting(
32 base::CommandLine::ForCurrentProcess());
33 CocoaProfileTest::SetUp();
34 ASSERT_TRUE(browser());
37 [[AvatarIconController alloc] initWithBrowser:browser()]);
38 [[controller_ view] setHidden:YES];
41 void TearDown() override {
42 browser()->window()->Close();
43 CocoaProfileTest::TearDown();
46 NSButton* button() { return [controller_ buttonView]; }
48 NSView* view() { return [controller_ view]; }
50 AvatarIconController* controller() { return controller_.get(); }
53 base::scoped_nsobject<AvatarIconController> controller_;
56 TEST_F(AvatarIconControllerTest, AddRemoveProfiles) {
57 EXPECT_TRUE([view() isHidden]);
59 testing_profile_manager()->CreateTestingProfile("one");
61 EXPECT_FALSE([view() isHidden]);
63 testing_profile_manager()->CreateTestingProfile("two");
64 EXPECT_FALSE([view() isHidden]);
66 testing_profile_manager()->DeleteTestingProfile("one");
67 EXPECT_FALSE([view() isHidden]);
69 testing_profile_manager()->DeleteTestingProfile("two");
70 EXPECT_TRUE([view() isHidden]);
73 TEST_F(AvatarIconControllerTest, DoubleOpen) {
74 // Create a second profile to enable the avatar menu.
75 testing_profile_manager()->CreateTestingProfile("p2");
77 EXPECT_FALSE([controller() menuController]);
79 [button() performClick:button()];
81 BaseBubbleController* menu = [controller() menuController];
82 EXPECT_TRUE([menu isKindOfClass:[AvatarMenuBubbleController class]]);
86 [button() performClick:button()];
87 EXPECT_EQ(menu, [controller() menuController]);
89 // Do not animate out because that is hard to test around.
90 static_cast<InfoBubbleWindow*>(menu.window).allowedAnimations =
91 info_bubble::kAnimateNone;
93 EXPECT_FALSE([controller() menuController]);
95 testing_profile_manager()->DeleteTestingProfile("p2");
98 TEST_F(AvatarIconControllerTest, SupervisedUserLabel) {
99 DCHECK(!profile()->IsSupervised());
100 EXPECT_FALSE([controller() labelButtonView]);
102 // Create a second, supervised profile to enable the avatar menu.
103 std::string name = "p2";
104 TestingProfile* profile = testing_profile_manager()->CreateTestingProfile(
105 name, scoped_ptr<PrefServiceSyncable>(), base::ASCIIToUTF16(name), 0,
106 "asdf", TestingProfile::TestingFactories());
107 EXPECT_TRUE(profile->IsSupervised());
109 // http://crbug.com/39725
110 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
111 profile, &TemplateURLServiceFactory::BuildInstanceFor);
112 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse(
113 profile, &AutocompleteClassifierFactory::BuildInstanceFor);
114 profile->CreateBookmarkModel(true);
115 bookmarks::test::WaitForBookmarkModelToLoad(
116 BookmarkModelFactory::GetForProfile(profile));
119 new Browser(Browser::CreateParams(profile, chrome::GetActiveDesktop()));
120 // Build a new controller to check if it is initialized correctly for a
121 // supervised user profile.
122 base::scoped_nsobject<AvatarIconController> controller(
123 [[AvatarIconController alloc] initWithBrowser:browser]);
125 EXPECT_TRUE([controller labelButtonView]);
127 browser->window()->Close();