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