File: profile_menu_controller_unittest.mm

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (305 lines) | stat: -rw-r--r-- 10,874 bytes parent folder | download | duplicates (6)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/ui/cocoa/profiles/profile_menu_controller.h"

#include "base/strings/sys_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/cocoa/test/cocoa_test_helper.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/test_browser_window.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "testing/gtest_mac.h"
#include "ui/base/l10n/l10n_util_mac.h"

class ProfileMenuControllerTest : public BrowserWithTestWindowTest {
 public:
  void SetUp() override {
    CocoaTest::BootstrapCocoa();
    BrowserWithTestWindowTest::SetUp();

    RebuildController();
  }

  void TearDown() override {
    [controller_ deinitialize];
    controller_ = nil;
    item_ = nil;

    BrowserWithTestWindowTest::TearDown();
  }

  void TestBottomItems() {
    NSMenu* menu = controller().menu;
    NSInteger count = menu.numberOfItems;

    ASSERT_GE(count, 4);

    NSMenuItem* item = [menu itemAtIndex:count - 4];
    EXPECT_TRUE(item.isSeparatorItem);

    item = [menu itemAtIndex:count - 3];
    EXPECT_EQ(@selector(editProfile:), item.action);

    item = [menu itemAtIndex:count - 2];
    EXPECT_TRUE(item.isSeparatorItem);

    item = [menu itemAtIndex:count - 1];
    EXPECT_EQ(@selector(newProfile:), item.action);
  }

  void VerifyProfileNamedIsActive(NSString* title, int line) {
    for (NSMenuItem* item in controller().menu.itemArray) {
      if ([item.title isEqualToString:title]) {
        EXPECT_EQ(NSControlStateValueOn, item.state)
            << base::SysNSStringToUTF8(item.title) << " (from line " << line
            << ")";
      } else {
        EXPECT_EQ(NSControlStateValueOff, item.state)
            << base::SysNSStringToUTF8(item.title) << " (from line " << line
            << ")";
      }
    }
  }

  ProfileMenuController* controller() { return controller_; }

  NSMenuItem* menu_item() { return item_; }

 protected:
  void RebuildController() {
    item_ = [[NSMenuItem alloc] initWithTitle:@"Users"
                                       action:nil
                                keyEquivalent:@""];
    controller_ = [[ProfileMenuController alloc]
        initSynchronouslyForTestingWithMainMenuItem:item_
                           profileAttributesStorage:
                               profile_manager()->profile_attributes_storage()];
  }

 private:
  NSMenuItem* __strong item_;
  ProfileMenuController* __strong controller_;
};

TEST_F(ProfileMenuControllerTest, InitializeMenu) {
  NSMenu* menu = controller().menu;
  // Profile, <sep>, Edit, <sep>, New.
  ASSERT_EQ(5, menu.numberOfItems);

  TestBottomItems();

  EXPECT_FALSE(menu_item().hidden);
}

TEST_F(ProfileMenuControllerTest, CreateItemWithTitle) {
  NSMenuItem* item =
      [controller() createItemWithTitle:@"Title"
                                 action:@selector(someSelector:)];
  EXPECT_NSEQ(@"Title", item.title);
  EXPECT_EQ(controller(), item.target);
  EXPECT_EQ(@selector(someSelector:), item.action);
  EXPECT_NSEQ(@"", item.keyEquivalent);
}

TEST_F(ProfileMenuControllerTest, RebuildMenu) {
  NSMenu* menu = controller().menu;
  EXPECT_EQ(5, menu.numberOfItems);

  EXPECT_FALSE(menu_item().hidden);

  // Create some more profiles on the manager.
  TestingProfileManager* manager = profile_manager();
  manager->CreateTestingProfile("Profile 2");
  manager->CreateTestingProfile("Profile 3");

  // Verify that the menu got rebuilt.
  ASSERT_EQ(7, menu.numberOfItems);

  NSMenuItem* item = [menu itemAtIndex:0];
  EXPECT_EQ(@selector(switchToProfileFromMenu:), item.action);
  EXPECT_TRUE([controller() validateMenuItem:item]);

  item = [menu itemAtIndex:1];
  EXPECT_EQ(@selector(switchToProfileFromMenu:), item.action);
  EXPECT_TRUE([controller() validateMenuItem:item]);

  item = [menu itemAtIndex:2];
  EXPECT_EQ(@selector(switchToProfileFromMenu:), item.action);
  EXPECT_TRUE([controller() validateMenuItem:item]);

  TestBottomItems();

  EXPECT_FALSE(menu_item().hidden);
}

TEST_F(ProfileMenuControllerTest, InsertItems) {
  NSMenu* menu = [[NSMenu alloc] initWithTitle:@""];
  ASSERT_EQ(0, menu.numberOfItems);

  // Even with one profile items can still be inserted.
  BOOL result = [controller() insertItemsIntoMenu:menu atOffset:0 fromDock:NO];
  EXPECT_TRUE(result);
  EXPECT_EQ(1, menu.numberOfItems);
  [menu removeAllItems];

  // Same for use in building the dock menu.
  result = [controller() insertItemsIntoMenu:menu atOffset:0 fromDock:YES];
  EXPECT_FALSE(result);
  EXPECT_EQ(0, menu.numberOfItems);
  [menu removeAllItems];

  // Create one more profile on the manager.
  TestingProfileManager* manager = profile_manager();
  manager->CreateTestingProfile("Profile 2");

  // With more than one profile, insertItems should return YES.
  result = [controller() insertItemsIntoMenu:menu atOffset:0 fromDock:NO];
  EXPECT_TRUE(result);
  ASSERT_EQ(2, menu.numberOfItems);

  NSMenuItem* item = [menu itemAtIndex:0];
  EXPECT_EQ(@selector(switchToProfileFromMenu:), item.action);

  item = [menu itemAtIndex:1];
  EXPECT_EQ(@selector(switchToProfileFromMenu:), item.action);
  [menu removeAllItems];

  // And for the dock, the selector should be different and there should be a
  // header item.
  result = [controller() insertItemsIntoMenu:menu atOffset:0 fromDock:YES];
  EXPECT_TRUE(result);
  ASSERT_EQ(3, menu.numberOfItems);

  // First item is a label item.
  item = [menu itemAtIndex:0];
  EXPECT_FALSE([item isEnabled]);

  item = [menu itemAtIndex:1];
  EXPECT_EQ(@selector(switchToProfileFromDock:), item.action);

  item = [menu itemAtIndex:2];
  EXPECT_EQ(@selector(switchToProfileFromDock:), item.action);
}

TEST_F(ProfileMenuControllerTest, InitialActiveBrowser) {
  [controller() activeBrowserChangedTo:nullptr];
  VerifyProfileNamedIsActive(l10n_util::GetNSString(IDS_DEFAULT_PROFILE_NAME),
                             __LINE__);
}

// Note: BrowserList::SetLastActive() is typically called as part of
// BrowserWindow::Show() and when a Browser becomes active. We don't need a full
// BrowserWindow, so it is called manually.
TEST_F(ProfileMenuControllerTest, SetActiveAndRemove) {
  // Set the name of the default profile, so that's it's not empty.
  const std::u16string kDefaultProfileName = u"DefaultProfile";
  profile_manager()
      ->profile_attributes_storage()
      ->GetProfileAttributesWithPath(browser()->profile()->GetPath())
      ->SetLocalProfileName(kDefaultProfileName, false);

  NSMenu* menu = controller().menu;
  TestingProfileManager* manager = profile_manager();
  TestingProfile* profile2 = manager->CreateTestingProfile("Profile 2");
  TestingProfile* profile3 = manager->CreateTestingProfile("Profile 3");
  ASSERT_EQ(7, menu.numberOfItems);

  // Create a browser and "show" it.
  Browser::CreateParams profile2_params(profile2, true);
  std::unique_ptr<Browser> p2_browser(
      CreateBrowserWithTestWindowForParams(profile2_params));
  [controller() activeBrowserChangedTo:p2_browser.get()];
  VerifyProfileNamedIsActive(@"Profile 2", __LINE__);

  // Close the browser and make sure the new active browser's profile is active.
  p2_browser.reset();
  [controller() activeBrowserChangedTo:browser()];
  VerifyProfileNamedIsActive(base::SysUTF16ToNSString(kDefaultProfileName),
                             __LINE__);

  // Open a new browser and make sure it takes effect.
  Browser::CreateParams profile3_params(profile3, true);
  std::unique_ptr<Browser> p3_browser(
      CreateBrowserWithTestWindowForParams(profile3_params));
  [controller() activeBrowserChangedTo:p3_browser.get()];
  VerifyProfileNamedIsActive(@"Profile 3", __LINE__);

  // Close the browser and make sure the new active browser's profile is active.
  p3_browser.reset();
  [controller() activeBrowserChangedTo:browser()];
  VerifyProfileNamedIsActive(base::SysUTF16ToNSString(kDefaultProfileName),
                             __LINE__);

  // Close the browser.
  std::unique_ptr<Browser> browser = release_browser();
  browser->tab_strip_model()->CloseAllTabs();
  browser.reset();
  std::unique_ptr<BrowserWindow> browser_window = release_browser_window();
  browser_window->Close();
  browser_window.reset();
  EXPECT_TRUE(BrowserList::GetInstance()->empty());

  [controller() activeBrowserChangedTo:nil];
  VerifyProfileNamedIsActive(base::SysUTF16ToNSString(kDefaultProfileName),
                             __LINE__);
}

TEST_F(ProfileMenuControllerTest, DeleteActiveProfile) {
  TestingProfileManager* manager = profile_manager();

  manager->CreateTestingProfile("Profile 2");
  TestingProfile* profile3 = manager->CreateTestingProfile("Profile 3");
  ASSERT_EQ(3U, manager->profile_manager()->GetNumberOfProfiles());

  const base::FilePath profile3_path = profile3->GetPath();
  manager->DeleteTestingProfile("Profile 3");

  // Simulate an unloaded profile by setting the "last used" local state pref
  // the profile that was just deleted.
  ScopedTestingLocalState* local_state = manager->local_state();
  local_state->Get()->SetUserPref(
      prefs::kProfileLastUsed,
      base::Value(profile3_path.BaseName().MaybeAsASCII()));
  EXPECT_FALSE(ProfileManager::GetLastUsedProfileIfLoaded());

  // Simulate the active browser changing to NULL and ensure a profile doesn't
  // get created by disallowing IO operations temporarily.
  base::ScopedDisallowBlocking scoped_disallow_blocking;
  [controller() activeBrowserChangedTo:nullptr];
  // Check that validateMenuItem does not load a profile, and edit is disabled.
  // Adding a new profile is still possible since this happens through the
  // profile picker.
  NSMenu* menu = controller().menu;
  for (NSMenuItem* item in [menu itemArray]) {
    bool is_edit = item.action == @selector(editProfile:);
    EXPECT_EQ([controller() validateMenuItem:item], !is_edit);
  }
}

TEST_F(ProfileMenuControllerTest, AddProfileDisabled) {
  ScopedTestingLocalState* local_state = profile_manager()->local_state();
  local_state->Get()->SetUserPref(prefs::kBrowserAddPersonEnabled,
                                  base::Value(false));

  RebuildController();

  NSMenu* menu = controller().menu;
  NSInteger count = menu.numberOfItems;

  ASSERT_GE(count, 2);

  NSMenuItem* item = [menu itemAtIndex:count - 2];
  EXPECT_TRUE(item.isSeparatorItem);

  item = [menu itemAtIndex:count - 1];
  EXPECT_EQ(@selector(editProfile:), item.action);
}