1 // Copyright (c) 2011 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 <Cocoa/Cocoa.h>
7 #include "base/files/file_path.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/run_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #import "chrome/browser/app_controller_mac.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/signin/fake_signin_manager.h"
16 #include "chrome/browser/signin/signin_error_controller_factory.h"
17 #include "chrome/browser/signin/signin_manager_factory.h"
18 #include "chrome/browser/sync/profile_sync_service.h"
19 #include "chrome/browser/sync/profile_sync_service_factory.h"
20 #include "chrome/browser/sync/profile_sync_service_mock.h"
21 #include "chrome/common/chrome_constants.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/grit/chromium_strings.h"
24 #include "chrome/grit/generated_resources.h"
25 #include "chrome/test/base/testing_browser_process.h"
26 #include "chrome/test/base/testing_profile.h"
27 #include "chrome/test/base/testing_profile_manager.h"
28 #include "components/signin/core/browser/fake_auth_status_provider.h"
29 #include "components/signin/core/browser/signin_error_controller.h"
30 #include "components/signin/core/browser/signin_manager.h"
31 #include "content/public/test/test_browser_thread_bundle.h"
32 #include "testing/platform_test.h"
33 #include "ui/base/l10n/l10n_util.h"
34 #include "ui/base/l10n/l10n_util_mac.h"
36 class AppControllerTest : public PlatformTest {
39 : profile_manager_(TestingBrowserProcess::GetGlobal()),
42 void SetUp() override {
43 PlatformTest::SetUp();
44 ASSERT_TRUE(profile_manager_.SetUp());
45 profile_ = profile_manager_.CreateTestingProfile("New Profile 1");
48 void TearDown() override {
49 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
50 base::RunLoop().RunUntilIdle();
51 PlatformTest::TearDown();
54 content::TestBrowserThreadBundle thread_bundle_;
55 TestingProfileManager profile_manager_;
56 TestingProfile* profile_;
59 TEST_F(AppControllerTest, DockMenu) {
60 base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
61 NSMenu* menu = [ac applicationDockMenu:NSApp];
65 EXPECT_NE(-1, [menu indexOfItemWithTag:IDC_NEW_WINDOW]);
66 EXPECT_NE(-1, [menu indexOfItemWithTag:IDC_NEW_INCOGNITO_WINDOW]);
67 for (item in [menu itemArray]) {
68 EXPECT_EQ(ac.get(), [item target]);
69 EXPECT_EQ(@selector(commandFromDock:), [item action]);
73 TEST_F(AppControllerTest, LastProfile) {
74 // Create a second profile.
75 base::FilePath dest_path1 = profile_->GetPath();
76 base::FilePath dest_path2 =
77 profile_manager_.CreateTestingProfile("New Profile 2")->GetPath();
78 ASSERT_EQ(2U, profile_manager_.profile_manager()->GetNumberOfProfiles());
79 ASSERT_EQ(2U, profile_manager_.profile_manager()->GetLoadedProfiles().size());
81 PrefService* local_state = g_browser_process->local_state();
82 local_state->SetString(prefs::kProfileLastUsed,
83 dest_path1.BaseName().MaybeAsASCII());
85 base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
87 // Delete the active profile.
88 profile_manager_.profile_manager()->ScheduleProfileForDeletion(
89 dest_path1, ProfileManager::CreateCallback());
91 base::RunLoop().RunUntilIdle();
93 EXPECT_EQ(dest_path2, [ac lastProfile]->GetPath());
96 TEST_F(AppControllerTest, TestSigninMenuItemNoErrors) {
97 base::scoped_nsobject<NSMenuItem> syncMenuItem(
98 [[NSMenuItem alloc] initWithTitle:@""
99 action:@selector(commandDispatch)
101 [syncMenuItem setTag:IDC_SHOW_SYNC_SETUP];
103 NSString* startSignin = l10n_util::GetNSStringFWithFixup(
104 IDS_SYNC_MENU_PRE_SYNCED_LABEL,
105 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
107 // Make sure shouldShow parameter is obeyed, and we get the default
108 // label if not signed in.
109 [AppController updateSigninItem:syncMenuItem
111 currentProfile:profile_];
113 EXPECT_TRUE([[syncMenuItem title] isEqualTo:startSignin]);
114 EXPECT_FALSE([syncMenuItem isHidden]);
116 [AppController updateSigninItem:syncMenuItem
118 currentProfile:profile_];
119 EXPECT_TRUE([[syncMenuItem title] isEqualTo:startSignin]);
120 EXPECT_TRUE([syncMenuItem isHidden]);
123 std::string username = "foo@example.com";
124 NSString* alreadySignedIn = l10n_util::GetNSStringFWithFixup(
125 IDS_SYNC_MENU_SYNCED_LABEL, base::UTF8ToUTF16(username));
126 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_);
127 signin->SetAuthenticatedUsername(username);
128 ProfileSyncService* sync = ProfileSyncServiceFactory::GetForProfile(profile_);
129 sync->SetSyncSetupCompleted();
130 [AppController updateSigninItem:syncMenuItem
132 currentProfile:profile_];
133 EXPECT_TRUE([[syncMenuItem title] isEqualTo:alreadySignedIn]);
134 EXPECT_FALSE([syncMenuItem isHidden]);
137 TEST_F(AppControllerTest, TestSigninMenuItemAuthError) {
138 base::scoped_nsobject<NSMenuItem> syncMenuItem(
139 [[NSMenuItem alloc] initWithTitle:@""
140 action:@selector(commandDispatch)
142 [syncMenuItem setTag:IDC_SHOW_SYNC_SETUP];
145 std::string username = "foo@example.com";
146 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_);
147 signin->SetAuthenticatedUsername(username);
148 ProfileSyncService* sync = ProfileSyncServiceFactory::GetForProfile(profile_);
149 sync->SetSyncSetupCompleted();
150 // Force an auth error.
151 FakeAuthStatusProvider provider(
152 SigninErrorControllerFactory::GetForProfile(profile_));
153 GoogleServiceAuthError error(
154 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
155 provider.SetAuthError("user@gmail.com", "user@gmail.com", error);
156 [AppController updateSigninItem:syncMenuItem
158 currentProfile:profile_];
159 NSString* authError =
160 l10n_util::GetNSStringWithFixup(IDS_SYNC_SIGN_IN_ERROR_WRENCH_MENU_ITEM);
161 EXPECT_TRUE([[syncMenuItem title] isEqualTo:authError]);
162 EXPECT_FALSE([syncMenuItem isHidden]);
165 // If there's a separator after the signin menu item, make sure it is hidden/
166 // shown when the signin menu item is.
167 TEST_F(AppControllerTest, TestSigninMenuItemWithSeparator) {
168 base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]);
169 NSMenuItem* signinMenuItem = [menu addItemWithTitle:@""
170 action:@selector(commandDispatch)
172 [signinMenuItem setTag:IDC_SHOW_SYNC_SETUP];
173 NSMenuItem* followingSeparator = [NSMenuItem separatorItem];
174 [menu addItem:followingSeparator];
175 [signinMenuItem setHidden:NO];
176 [followingSeparator setHidden:NO];
178 [AppController updateSigninItem:signinMenuItem
180 currentProfile:profile_];
182 EXPECT_FALSE([followingSeparator isEnabled]);
183 EXPECT_TRUE([signinMenuItem isHidden]);
184 EXPECT_TRUE([followingSeparator isHidden]);
186 [AppController updateSigninItem:signinMenuItem
188 currentProfile:profile_];
190 EXPECT_FALSE([followingSeparator isEnabled]);
191 EXPECT_FALSE([signinMenuItem isHidden]);
192 EXPECT_FALSE([followingSeparator isHidden]);
195 // If there's a non-separator item after the signin menu item, it should not
196 // change state when the signin menu item is hidden/shown.
197 TEST_F(AppControllerTest, TestSigninMenuItemWithNonSeparator) {
198 base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]);
199 NSMenuItem* signinMenuItem = [menu addItemWithTitle:@""
200 action:@selector(commandDispatch)
202 [signinMenuItem setTag:IDC_SHOW_SYNC_SETUP];
203 NSMenuItem* followingNonSeparator =
204 [menu addItemWithTitle:@""
205 action:@selector(commandDispatch)
207 [signinMenuItem setHidden:NO];
208 [followingNonSeparator setHidden:NO];
210 [AppController updateSigninItem:signinMenuItem
212 currentProfile:profile_];
214 EXPECT_TRUE([followingNonSeparator isEnabled]);
215 EXPECT_TRUE([signinMenuItem isHidden]);
216 EXPECT_FALSE([followingNonSeparator isHidden]);
218 [followingNonSeparator setHidden:YES];
219 [AppController updateSigninItem:signinMenuItem
221 currentProfile:profile_];
223 EXPECT_TRUE([followingNonSeparator isEnabled]);
224 EXPECT_FALSE([signinMenuItem isHidden]);
225 EXPECT_TRUE([followingNonSeparator isHidden]);