[Presentation API, Android] Implement basic messaging
[chromium-blink-merge.git] / chrome / browser / app_controller_mac_unittest.mm
blob5cf952c86f3acdfe2d1e2b5376a3a93f5b4385f0
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/signin_error_controller_factory.h"
16 #include "chrome/browser/signin/signin_manager_factory.h"
17 #include "chrome/browser/sync/profile_sync_service.h"
18 #include "chrome/browser/sync/profile_sync_service_factory.h"
19 #include "chrome/browser/sync/profile_sync_service_mock.h"
20 #include "chrome/common/chrome_constants.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/grit/chromium_strings.h"
23 #include "chrome/grit/generated_resources.h"
24 #include "chrome/test/base/testing_browser_process.h"
25 #include "chrome/test/base/testing_profile.h"
26 #include "chrome/test/base/testing_profile_manager.h"
27 #include "components/signin/core/browser/fake_auth_status_provider.h"
28 #include "components/signin/core/browser/signin_error_controller.h"
29 #include "components/signin/core/browser/signin_manager.h"
30 #include "content/public/test/test_browser_thread_bundle.h"
31 #include "testing/platform_test.h"
32 #include "ui/base/l10n/l10n_util.h"
33 #include "ui/base/l10n/l10n_util_mac.h"
35 class AppControllerTest : public PlatformTest {
36  protected:
37   AppControllerTest()
38       : profile_manager_(TestingBrowserProcess::GetGlobal()),
39         profile_(nullptr) {}
41   void SetUp() override {
42     PlatformTest::SetUp();
43     ASSERT_TRUE(profile_manager_.SetUp());
44     profile_ = profile_manager_.CreateTestingProfile("New Profile 1");
45   }
47   void TearDown() override {
48     TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
49     base::RunLoop().RunUntilIdle();
50     PlatformTest::TearDown();
51   }
53   content::TestBrowserThreadBundle thread_bundle_;
54   TestingProfileManager profile_manager_;
55   TestingProfile* profile_;
58 TEST_F(AppControllerTest, DockMenu) {
59   base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
60   NSMenu* menu = [ac applicationDockMenu:NSApp];
61   NSMenuItem* item;
63   EXPECT_TRUE(menu);
64   EXPECT_NE(-1, [menu indexOfItemWithTag:IDC_NEW_WINDOW]);
65   EXPECT_NE(-1, [menu indexOfItemWithTag:IDC_NEW_INCOGNITO_WINDOW]);
66   for (item in [menu itemArray]) {
67     EXPECT_EQ(ac.get(), [item target]);
68     EXPECT_EQ(@selector(commandFromDock:), [item action]);
69   }
72 TEST_F(AppControllerTest, LastProfile) {
73   // Create a second profile.
74   base::FilePath dest_path1 = profile_->GetPath();
75   base::FilePath dest_path2 =
76       profile_manager_.CreateTestingProfile("New Profile 2")->GetPath();
77   ASSERT_EQ(2U, profile_manager_.profile_manager()->GetNumberOfProfiles());
78   ASSERT_EQ(2U, profile_manager_.profile_manager()->GetLoadedProfiles().size());
80   PrefService* local_state = g_browser_process->local_state();
81   local_state->SetString(prefs::kProfileLastUsed,
82                          dest_path1.BaseName().MaybeAsASCII());
84   base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
86   // Delete the active profile.
87   profile_manager_.profile_manager()->ScheduleProfileForDeletion(
88       dest_path1, ProfileManager::CreateCallback());
90   base::RunLoop().RunUntilIdle();
92   EXPECT_EQ(dest_path2, [ac lastProfile]->GetPath());
95 TEST_F(AppControllerTest, TestSigninMenuItemNoErrors) {
96   base::scoped_nsobject<NSMenuItem> syncMenuItem(
97       [[NSMenuItem alloc] initWithTitle:@""
98                                  action:@selector(commandDispatch)
99                           keyEquivalent:@""]);
100   [syncMenuItem setTag:IDC_SHOW_SYNC_SETUP];
102   NSString* startSignin = l10n_util::GetNSStringFWithFixup(
103       IDS_SYNC_MENU_PRE_SYNCED_LABEL,
104       l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
106   // Make sure shouldShow parameter is obeyed, and we get the default
107   // label if not signed in.
108   [AppController updateSigninItem:syncMenuItem
109                        shouldShow:YES
110                    currentProfile:profile_];
112   EXPECT_TRUE([[syncMenuItem title] isEqualTo:startSignin]);
113   EXPECT_FALSE([syncMenuItem isHidden]);
115   [AppController updateSigninItem:syncMenuItem
116                        shouldShow:NO
117                    currentProfile:profile_];
118   EXPECT_TRUE([[syncMenuItem title] isEqualTo:startSignin]);
119   EXPECT_TRUE([syncMenuItem isHidden]);
121   // Now sign in.
122   std::string username = "foo@example.com";
123   NSString* alreadySignedIn = l10n_util::GetNSStringFWithFixup(
124       IDS_SYNC_MENU_SYNCED_LABEL, base::UTF8ToUTF16(username));
125   SigninManager* signin = SigninManagerFactory::GetForProfile(profile_);
126   signin->SetAuthenticatedAccountInfo(username, username);
127   ProfileSyncService* sync = ProfileSyncServiceFactory::GetForProfile(profile_);
128   sync->SetSyncSetupCompleted();
129   [AppController updateSigninItem:syncMenuItem
130                        shouldShow:YES
131                    currentProfile:profile_];
132   EXPECT_TRUE([[syncMenuItem title] isEqualTo:alreadySignedIn]);
133   EXPECT_FALSE([syncMenuItem isHidden]);
136 TEST_F(AppControllerTest, TestSigninMenuItemAuthError) {
137   base::scoped_nsobject<NSMenuItem> syncMenuItem(
138       [[NSMenuItem alloc] initWithTitle:@""
139                                  action:@selector(commandDispatch)
140                           keyEquivalent:@""]);
141   [syncMenuItem setTag:IDC_SHOW_SYNC_SETUP];
143   // Now sign in.
144   std::string username = "foo@example.com";
145   SigninManager* signin = SigninManagerFactory::GetForProfile(profile_);
146   signin->SetAuthenticatedAccountInfo(username, username);
147   ProfileSyncService* sync = ProfileSyncServiceFactory::GetForProfile(profile_);
148   sync->SetSyncSetupCompleted();
149   // Force an auth error.
150   FakeAuthStatusProvider provider(
151       SigninErrorControllerFactory::GetForProfile(profile_));
152   GoogleServiceAuthError error(
153       GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
154   provider.SetAuthError("user@gmail.com", error);
155   [AppController updateSigninItem:syncMenuItem
156                        shouldShow:YES
157                    currentProfile:profile_];
158   NSString* authError =
159       l10n_util::GetNSStringWithFixup(IDS_SYNC_SIGN_IN_ERROR_WRENCH_MENU_ITEM);
160   EXPECT_TRUE([[syncMenuItem title] isEqualTo:authError]);
161   EXPECT_FALSE([syncMenuItem isHidden]);
164 // If there's a separator after the signin menu item, make sure it is hidden/
165 // shown when the signin menu item is.
166 TEST_F(AppControllerTest, TestSigninMenuItemWithSeparator) {
167   base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]);
168   NSMenuItem* signinMenuItem = [menu addItemWithTitle:@""
169                                                action:@selector(commandDispatch)
170                                         keyEquivalent:@""];
171   [signinMenuItem setTag:IDC_SHOW_SYNC_SETUP];
172   NSMenuItem* followingSeparator = [NSMenuItem separatorItem];
173   [menu addItem:followingSeparator];
174   [signinMenuItem setHidden:NO];
175   [followingSeparator setHidden:NO];
177   [AppController updateSigninItem:signinMenuItem
178                        shouldShow:NO
179                    currentProfile:profile_];
181   EXPECT_FALSE([followingSeparator isEnabled]);
182   EXPECT_TRUE([signinMenuItem isHidden]);
183   EXPECT_TRUE([followingSeparator isHidden]);
185   [AppController updateSigninItem:signinMenuItem
186                        shouldShow:YES
187                    currentProfile:profile_];
189   EXPECT_FALSE([followingSeparator isEnabled]);
190   EXPECT_FALSE([signinMenuItem isHidden]);
191   EXPECT_FALSE([followingSeparator isHidden]);
194 // If there's a non-separator item after the signin menu item, it should not
195 // change state when the signin menu item is hidden/shown.
196 TEST_F(AppControllerTest, TestSigninMenuItemWithNonSeparator) {
197   base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]);
198   NSMenuItem* signinMenuItem = [menu addItemWithTitle:@""
199                                                action:@selector(commandDispatch)
200                                         keyEquivalent:@""];
201   [signinMenuItem setTag:IDC_SHOW_SYNC_SETUP];
202   NSMenuItem* followingNonSeparator =
203       [menu addItemWithTitle:@""
204                       action:@selector(commandDispatch)
205                keyEquivalent:@""];
206   [signinMenuItem setHidden:NO];
207   [followingNonSeparator setHidden:NO];
209   [AppController updateSigninItem:signinMenuItem
210                        shouldShow:NO
211                    currentProfile:profile_];
213   EXPECT_TRUE([followingNonSeparator isEnabled]);
214   EXPECT_TRUE([signinMenuItem isHidden]);
215   EXPECT_FALSE([followingNonSeparator isHidden]);
217   [followingNonSeparator setHidden:YES];
218   [AppController updateSigninItem:signinMenuItem
219                        shouldShow:YES
220                    currentProfile:profile_];
222   EXPECT_TRUE([followingNonSeparator isEnabled]);
223   EXPECT_FALSE([signinMenuItem isHidden]);
224   EXPECT_TRUE([followingNonSeparator isHidden]);