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/managed_mode/managed_user_service.h"
12 #include "chrome/browser/managed_mode/managed_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/common/pref_names.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "components/bookmarks/test/bookmark_test_helpers.h"
27 class AvatarIconControllerTest : public CocoaProfileTest {
29 virtual void SetUp() OVERRIDE {
30 CocoaProfileTest::SetUp();
31 ASSERT_TRUE(browser());
34 [[AvatarIconController alloc] initWithBrowser:browser()]);
35 [[controller_ view] setHidden:YES];
38 virtual void TearDown() OVERRIDE {
39 browser()->window()->Close();
40 CocoaProfileTest::TearDown();
43 NSButton* button() { return [controller_ buttonView]; }
45 NSView* view() { return [controller_ view]; }
47 AvatarIconController* controller() { return controller_.get(); }
50 base::scoped_nsobject<AvatarIconController> controller_;
53 TEST_F(AvatarIconControllerTest, AddRemoveProfiles) {
54 EXPECT_TRUE([view() isHidden]);
56 testing_profile_manager()->CreateTestingProfile("one");
58 EXPECT_FALSE([view() isHidden]);
60 testing_profile_manager()->CreateTestingProfile("two");
61 EXPECT_FALSE([view() isHidden]);
63 testing_profile_manager()->DeleteTestingProfile("one");
64 EXPECT_FALSE([view() isHidden]);
66 testing_profile_manager()->DeleteTestingProfile("two");
67 EXPECT_TRUE([view() isHidden]);
70 TEST_F(AvatarIconControllerTest, DoubleOpen) {
71 // Create a second profile to enable the avatar menu.
72 testing_profile_manager()->CreateTestingProfile("p2");
74 EXPECT_FALSE([controller() menuController]);
76 [button() performClick:button()];
78 BaseBubbleController* menu = [controller() menuController];
79 EXPECT_TRUE([menu isKindOfClass:[AvatarMenuBubbleController class]]);
83 [button() performClick:button()];
84 EXPECT_EQ(menu, [controller() menuController]);
86 // Do not animate out because that is hard to test around.
87 static_cast<InfoBubbleWindow*>(menu.window).allowedAnimations =
88 info_bubble::kAnimateNone;
90 EXPECT_FALSE([controller() menuController]);
92 testing_profile_manager()->DeleteTestingProfile("p2");
95 TEST_F(AvatarIconControllerTest, ManagedUserLabel) {
96 DCHECK(!profile()->IsManaged());
97 EXPECT_FALSE([controller() labelButtonView]);
99 // Create a second, managed profile to enable the avatar menu.
100 std::string name = "p2";
101 TestingProfile* profile = testing_profile_manager()->CreateTestingProfile(
102 name, scoped_ptr<PrefServiceSyncable>(), base::ASCIIToUTF16(name), 0,
103 "asdf", TestingProfile::TestingFactories());
104 EXPECT_TRUE(profile->IsManaged());
106 // http://crbug.com/39725
107 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
108 profile, &TemplateURLServiceFactory::BuildInstanceFor);
109 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse(
110 profile, &AutocompleteClassifierFactory::BuildInstanceFor);
111 profile->CreateBookmarkModel(true);
112 test::WaitForBookmarkModelToLoad(
113 BookmarkModelFactory::GetForProfile(profile));
116 new Browser(Browser::CreateParams(profile, chrome::GetActiveDesktop()));
117 // Build a new controller to check if it is initialized correctly for a
118 // managed user profile.
119 base::scoped_nsobject<AvatarIconController> controller(
120 [[AvatarIconController alloc] initWithBrowser:browser]);
122 EXPECT_TRUE([controller labelButtonView]);
124 browser->window()->Close();