summaryrefslogtreecommitdiffstats
path: root/chrome/browser/app_controller_mac_unittest.mm
blob: 5cf952c86f3acdfe2d1e2b5376a3a93f5b4385f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Cocoa/Cocoa.h>

#include "base/files/file_path.h"
#include "base/mac/scoped_nsobject.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#import "chrome/browser/app_controller_mac.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/signin_error_controller_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/sync/profile_sync_service_mock.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/signin/core/browser/fake_auth_status_provider.h"
#include "components/signin/core/browser/signin_error_controller.h"
#include "components/signin/core/browser/signin_manager.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/platform_test.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/l10n_util_mac.h"

class AppControllerTest : public PlatformTest {
 protected:
  AppControllerTest()
      : profile_manager_(TestingBrowserProcess::GetGlobal()),
        profile_(nullptr) {}

  void SetUp() override {
    PlatformTest::SetUp();
    ASSERT_TRUE(profile_manager_.SetUp());
    profile_ = profile_manager_.CreateTestingProfile("New Profile 1");
  }

  void TearDown() override {
    TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
    base::RunLoop().RunUntilIdle();
    PlatformTest::TearDown();
  }

  content::TestBrowserThreadBundle thread_bundle_;
  TestingProfileManager profile_manager_;
  TestingProfile* profile_;
};

TEST_F(AppControllerTest, DockMenu) {
  base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
  NSMenu* menu = [ac applicationDockMenu:NSApp];
  NSMenuItem* item;

  EXPECT_TRUE(menu);
  EXPECT_NE(-1, [menu indexOfItemWithTag:IDC_NEW_WINDOW]);
  EXPECT_NE(-1, [menu indexOfItemWithTag:IDC_NEW_INCOGNITO_WINDOW]);
  for (item in [menu itemArray]) {
    EXPECT_EQ(ac.get(), [item target]);
    EXPECT_EQ(@selector(commandFromDock:), [item action]);
  }
}

TEST_F(AppControllerTest, LastProfile) {
  // Create a second profile.
  base::FilePath dest_path1 = profile_->GetPath();
  base::FilePath dest_path2 =
      profile_manager_.CreateTestingProfile("New Profile 2")->GetPath();
  ASSERT_EQ(2U, profile_manager_.profile_manager()->GetNumberOfProfiles());
  ASSERT_EQ(2U, profile_manager_.profile_manager()->GetLoadedProfiles().size());

  PrefService* local_state = g_browser_process->local_state();
  local_state->SetString(prefs::kProfileLastUsed,
                         dest_path1.BaseName().MaybeAsASCII());

  base::scoped_nsobject<AppController> ac([[AppController alloc] init]);

  // Delete the active profile.
  profile_manager_.profile_manager()->ScheduleProfileForDeletion(
      dest_path1, ProfileManager::CreateCallback());

  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(dest_path2, [ac lastProfile]->GetPath());
}

TEST_F(AppControllerTest, TestSigninMenuItemNoErrors) {
  base::scoped_nsobject<NSMenuItem> syncMenuItem(
      [[NSMenuItem alloc] initWithTitle:@""
                                 action:@selector(commandDispatch)
                          keyEquivalent:@""]);
  [syncMenuItem setTag:IDC_SHOW_SYNC_SETUP];

  NSString* startSignin = l10n_util::GetNSStringFWithFixup(
      IDS_SYNC_MENU_PRE_SYNCED_LABEL,
      l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));

  // Make sure shouldShow parameter is obeyed, and we get the default
  // label if not signed in.
  [AppController updateSigninItem:syncMenuItem
                       shouldShow:YES
                   currentProfile:profile_];

  EXPECT_TRUE([[syncMenuItem title] isEqualTo:startSignin]);
  EXPECT_FALSE([syncMenuItem isHidden]);

  [AppController updateSigninItem:syncMenuItem
                       shouldShow:NO
                   currentProfile:profile_];
  EXPECT_TRUE([[syncMenuItem title] isEqualTo:startSignin]);
  EXPECT_TRUE([syncMenuItem isHidden]);

  // Now sign in.
  std::string username = "foo@example.com";
  NSString* alreadySignedIn = l10n_util::GetNSStringFWithFixup(
      IDS_SYNC_MENU_SYNCED_LABEL, base::UTF8ToUTF16(username));
  SigninManager* signin = SigninManagerFactory::GetForProfile(profile_);
  signin->SetAuthenticatedAccountInfo(username, username);
  ProfileSyncService* sync = ProfileSyncServiceFactory::GetForProfile(profile_);
  sync->SetSyncSetupCompleted();
  [AppController updateSigninItem:syncMenuItem
                       shouldShow:YES
                   currentProfile:profile_];
  EXPECT_TRUE([[syncMenuItem title] isEqualTo:alreadySignedIn]);
  EXPECT_FALSE([syncMenuItem isHidden]);
}

TEST_F(AppControllerTest, TestSigninMenuItemAuthError) {
  base::scoped_nsobject<NSMenuItem> syncMenuItem(
      [[NSMenuItem alloc] initWithTitle:@""
                                 action:@selector(commandDispatch)
                          keyEquivalent:@""]);
  [syncMenuItem setTag:IDC_SHOW_SYNC_SETUP];

  // Now sign in.
  std::string username = "foo@example.com";
  SigninManager* signin = SigninManagerFactory::GetForProfile(profile_);
  signin->SetAuthenticatedAccountInfo(username, username);
  ProfileSyncService* sync = ProfileSyncServiceFactory::GetForProfile(profile_);
  sync->SetSyncSetupCompleted();
  // Force an auth error.
  FakeAuthStatusProvider provider(
      SigninErrorControllerFactory::GetForProfile(profile_));
  GoogleServiceAuthError error(
      GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
  provider.SetAuthError("user@gmail.com", error);
  [AppController updateSigninItem:syncMenuItem
                       shouldShow:YES
                   currentProfile:profile_];
  NSString* authError =
      l10n_util::GetNSStringWithFixup(IDS_SYNC_SIGN_IN_ERROR_WRENCH_MENU_ITEM);
  EXPECT_TRUE([[syncMenuItem title] isEqualTo:authError]);
  EXPECT_FALSE([syncMenuItem isHidden]);
}

// If there's a separator after the signin menu item, make sure it is hidden/
// shown when the signin menu item is.
TEST_F(AppControllerTest, TestSigninMenuItemWithSeparator) {
  base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]);
  NSMenuItem* signinMenuItem = [menu addItemWithTitle:@""
                                               action:@selector(commandDispatch)
                                        keyEquivalent:@""];
  [signinMenuItem setTag:IDC_SHOW_SYNC_SETUP];
  NSMenuItem* followingSeparator = [NSMenuItem separatorItem];
  [menu addItem:followingSeparator];
  [signinMenuItem setHidden:NO];
  [followingSeparator setHidden:NO];

  [AppController updateSigninItem:signinMenuItem
                       shouldShow:NO
                   currentProfile:profile_];

  EXPECT_FALSE([followingSeparator isEnabled]);
  EXPECT_TRUE([signinMenuItem isHidden]);
  EXPECT_TRUE([followingSeparator isHidden]);

  [AppController updateSigninItem:signinMenuItem
                       shouldShow:YES
                   currentProfile:profile_];

  EXPECT_FALSE([followingSeparator isEnabled]);
  EXPECT_FALSE([signinMenuItem isHidden]);
  EXPECT_FALSE([followingSeparator isHidden]);
}

// If there's a non-separator item after the signin menu item, it should not
// change state when the signin menu item is hidden/shown.
TEST_F(AppControllerTest, TestSigninMenuItemWithNonSeparator) {
  base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]);
  NSMenuItem* signinMenuItem = [menu addItemWithTitle:@""
                                               action:@selector(commandDispatch)
                                        keyEquivalent:@""];
  [signinMenuItem setTag:IDC_SHOW_SYNC_SETUP];
  NSMenuItem* followingNonSeparator =
      [menu addItemWithTitle:@""
                      action:@selector(commandDispatch)
               keyEquivalent:@""];
  [signinMenuItem setHidden:NO];
  [followingNonSeparator setHidden:NO];

  [AppController updateSigninItem:signinMenuItem
                       shouldShow:NO
                   currentProfile:profile_];

  EXPECT_TRUE([followingNonSeparator isEnabled]);
  EXPECT_TRUE([signinMenuItem isHidden]);
  EXPECT_FALSE([followingNonSeparator isHidden]);

  [followingNonSeparator setHidden:YES];
  [AppController updateSigninItem:signinMenuItem
                       shouldShow:YES
                   currentProfile:profile_];

  EXPECT_TRUE([followingNonSeparator isEnabled]);
  EXPECT_FALSE([signinMenuItem isHidden]);
  EXPECT_TRUE([followingNonSeparator isHidden]);
}