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_icon_controller.h" | 5 #import "chrome/browser/ui/cocoa/profiles/avatar_icon_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/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" | 10 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
12 #include "chrome/browser/search_engines/template_url_service_factory.h" | 12 #include "chrome/browser/search_engines/template_url_service_factory.h" |
13 #include "chrome/browser/supervised_user/supervised_user_service.h" | 13 #include "chrome/browser/supervised_user/supervised_user_service.h" |
14 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" | 14 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" |
15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_commands.h" | 16 #include "chrome/browser/ui/browser_commands.h" |
17 #include "chrome/browser/ui/browser_window.h" | 17 #include "chrome/browser/ui/browser_window.h" |
18 #import "chrome/browser/ui/cocoa/base_bubble_controller.h" | 18 #import "chrome/browser/ui/cocoa/base_bubble_controller.h" |
| 19 #import "chrome/browser/ui/cocoa/browser_window_cocoa.h" |
| 20 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
19 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" | 21 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" |
20 #include "chrome/browser/ui/cocoa/info_bubble_window.h" | 22 #include "chrome/browser/ui/cocoa/info_bubble_window.h" |
| 23 #import "chrome/browser/ui/cocoa/profiles/avatar_button_controller.h" |
21 #import "chrome/browser/ui/cocoa/profiles/avatar_menu_bubble_controller.h" | 24 #import "chrome/browser/ui/cocoa/profiles/avatar_menu_bubble_controller.h" |
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 25 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
23 #include "chrome/test/base/testing_profile.h" | 26 #include "chrome/test/base/testing_profile.h" |
24 #include "components/bookmarks/test/bookmark_test_helpers.h" | 27 #include "components/bookmarks/test/bookmark_test_helpers.h" |
25 #include "components/signin/core/common/profile_management_switches.h" | 28 #include "components/signin/core/common/profile_management_switches.h" |
26 #include "components/syncable_prefs/pref_service_syncable.h" | 29 #include "components/syncable_prefs/pref_service_syncable.h" |
27 | 30 |
28 class AvatarIconControllerTest : public CocoaProfileTest { | 31 class AvatarIconControllerTest : public CocoaProfileTest { |
29 public: | 32 public: |
| 33 // Incognito, which is the only way to show the icon |
| 34 AvatarIconControllerTest(bool incognito) |
| 35 : CocoaProfileTest(incognito) {} |
| 36 |
30 void SetUp() override { | 37 void SetUp() override { |
31 switches::DisableNewAvatarMenuForTesting( | |
32 base::CommandLine::ForCurrentProcess()); | |
33 CocoaProfileTest::SetUp(); | 38 CocoaProfileTest::SetUp(); |
34 ASSERT_TRUE(browser()); | 39 ASSERT_TRUE(browser()); |
35 | |
36 controller_.reset( | |
37 [[AvatarIconController alloc] initWithBrowser:browser()]); | |
38 [[controller_ view] setHidden:YES]; | |
39 } | 40 } |
40 | 41 |
41 void TearDown() override { | 42 void TearDown() override { |
42 browser()->window()->Close(); | 43 browser()->window()->Close(); |
43 CocoaProfileTest::TearDown(); | 44 CocoaProfileTest::TearDown(); |
44 } | 45 } |
45 | 46 |
46 NSButton* button() { return [controller_ buttonView]; } | 47 AvatarBaseController* icon_controller() { |
47 | 48 BrowserWindowCocoa* window = (BrowserWindowCocoa*)browser()->window(); |
48 NSView* view() { return [controller_ view]; } | 49 return [window->cocoa_controller() avatarButtonController]; |
49 | 50 } |
50 AvatarIconController* controller() { return controller_.get(); } | |
51 | |
52 private: | |
53 base::scoped_nsobject<AvatarIconController> controller_; | |
54 }; | 51 }; |
55 | 52 |
56 TEST_F(AvatarIconControllerTest, AddRemoveProfiles) { | 53 class AvatarIconControllerIncognitoTest : public AvatarIconControllerTest { |
57 EXPECT_TRUE([view() isHidden]); | 54 public: |
| 55 AvatarIconControllerIncognitoTest() : AvatarIconControllerTest(true) {} |
| 56 }; |
58 | 57 |
59 testing_profile_manager()->CreateTestingProfile("one"); | 58 TEST_F(AvatarIconControllerIncognitoTest, ShowingAvatarIcon) { |
60 | 59 // In incognito, we should be using the AvatarIconController to show the |
61 EXPECT_FALSE([view() isHidden]); | 60 // incognito guy. |
62 | 61 EXPECT_TRUE([icon_controller() isKindOfClass:[AvatarIconController class]]); |
63 testing_profile_manager()->CreateTestingProfile("two"); | |
64 EXPECT_FALSE([view() isHidden]); | |
65 | |
66 testing_profile_manager()->DeleteTestingProfile("one"); | |
67 EXPECT_FALSE([view() isHidden]); | |
68 | |
69 testing_profile_manager()->DeleteTestingProfile("two"); | |
70 EXPECT_TRUE([view() isHidden]); | |
71 } | 62 } |
72 | 63 |
73 TEST_F(AvatarIconControllerTest, DoubleOpen) { | 64 class AvatarIconControllerRegularSessionTest : public AvatarIconControllerTest { |
74 // Create a second profile to enable the avatar menu. | 65 public: |
75 testing_profile_manager()->CreateTestingProfile("p2"); | 66 AvatarIconControllerRegularSessionTest() : AvatarIconControllerTest(false) {} |
| 67 }; |
76 | 68 |
77 EXPECT_FALSE([controller() menuController]); | 69 TEST_F(AvatarIconControllerRegularSessionTest, ShowingAvatarButton) { |
78 | 70 // In a regular session, we should be using the AvatarButtonController to show |
79 [button() performClick:button()]; | 71 // the profile name. |
80 | 72 EXPECT_TRUE([icon_controller() isKindOfClass:[AvatarButtonController class]]); |
81 BaseBubbleController* menu = [controller() menuController]; | |
82 EXPECT_TRUE([menu isKindOfClass:[AvatarMenuBubbleController class]]); | |
83 | |
84 EXPECT_TRUE(menu); | |
85 | |
86 [button() performClick:button()]; | |
87 EXPECT_EQ(menu, [controller() menuController]); | |
88 | |
89 // Do not animate out because that is hard to test around. | |
90 static_cast<InfoBubbleWindow*>(menu.window).allowedAnimations = | |
91 info_bubble::kAnimateNone; | |
92 [menu close]; | |
93 EXPECT_FALSE([controller() menuController]); | |
94 | |
95 testing_profile_manager()->DeleteTestingProfile("p2"); | |
96 } | 73 } |
97 | |
98 TEST_F(AvatarIconControllerTest, SupervisedUserLabel) { | |
99 DCHECK(!profile()->IsSupervised()); | |
100 EXPECT_FALSE([controller() labelButtonView]); | |
101 | |
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<syncable_prefs::PrefServiceSyncable>(), | |
106 base::ASCIIToUTF16(name), 0, "asdf", TestingProfile::TestingFactories()); | |
107 EXPECT_TRUE(profile->IsSupervised()); | |
108 | |
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)); | |
117 | |
118 Browser* browser = | |
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]); | |
124 | |
125 EXPECT_TRUE([controller labelButtonView]); | |
126 | |
127 browser->window()->Close(); | |
128 } | |
OLD | NEW |