File: browser_window_default_touch_bar_unittest.mm

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (265 lines) | stat: -rw-r--r-- 10,714 bytes parent folder | download | duplicates (3)
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
// Copyright 2017 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/touchbar/browser_window_default_touch_bar.h"

#import <Cocoa/Cocoa.h>

#include "base/apple/foundation_util.h"
#include "base/mac/mac_util.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/command_updater.h"
#include "chrome/browser/search_engines/template_url_service_factory_test_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/cocoa/test/cocoa_test_helper.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
#include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "components/prefs/pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#import "ui/base/cocoa/touch_bar_util.h"
#include "ui/base/l10n/l10n_util_mac.h"

class BrowserWindowDefaultTouchBarUnitTest : public BrowserWithTestWindowTest {
 public:
  void SetUp() override {
    BrowserWithTestWindowTest::SetUp();

    // The touch bar can show a search engine prompt, which requires this
    // service.
    template_service_util_ =
        std::make_unique<TemplateURLServiceFactoryTestUtil>(profile());
    template_service_util_->VerifyLoad();

    command_updater_ = browser()->command_controller();

    browser()->tab_strip_model()->AppendWebContents(
        content::WebContentsTester::CreateTestWebContents(profile(), nullptr),
        true);

    touch_bar_ = [[BrowserWindowDefaultTouchBar alloc] init];
    touch_bar_.browser = browser();
  }

  void UpdateCommandEnabled(int id, bool enabled) {
    command_updater_->UpdateCommandEnabled(id, enabled);
  }

  bool ShowsHomeButton() {
    return browser()->profile()->GetPrefs()->GetBoolean(prefs::kShowHomeButton);
  }

  void SetShowHomeButton(bool flag) {
    browser()->profile()->GetPrefs()->SetBoolean(prefs::kShowHomeButton, flag);
  }

  void TearDown() override {
    touch_bar_.browser = nullptr;
    touch_bar_ = nil;

    BrowserWithTestWindowTest::TearDown();
  }

  CocoaTestHelper cocoa_test_helper_;
  raw_ptr<CommandUpdater, DanglingUntriaged>
      command_updater_;  // Weak, owned by Browser.

  std::unique_ptr<TemplateURLServiceFactoryTestUtil> template_service_util_;

  BrowserWindowDefaultTouchBar* __strong touch_bar_;
};

// Test if any known identifiers no longer work. See the message in the test;
// these identifiers may be written out to disk on users' computers if they
// customize the Touch Bar, and the corresponding items will disappear if they
// can no longer be created.
TEST_F(BrowserWindowDefaultTouchBarUnitTest, HistoricTouchBarItems) {
  NSTouchBar* touch_bar = [touch_bar_ makeTouchBar];
  for (NSString* item_identifier : {
           @"BACK-FWD",
           @"BACK",
           @"FORWARD",
           @"RELOAD-STOP",
           @"HOME",
           @"SEARCH",
           @"BOOKMARK",
           @"NEW-TAB",
       }) {
    auto identifier = ui::GetTouchBarItemId(@"browser-window", item_identifier);
    EXPECT_NE(nil, [touch_bar itemForIdentifier:identifier])
        << "BrowserWindowDefaultTouchBar didn't return a Touch Bar item for "
           "an identifier that was once available ("
        << identifier.UTF8String
        << "). If a user's customized Touch Bar includes this item, it will "
           "disappear! Do not update or remove entries in this list just to "
           "make the test pass; keep supporting old identifiers when "
           "possible, even if they're no longer part of the set of "
           "default/customizable items.";
  }
}

// Tests if BrowserWindowDefaultTouchBar can produce the items it says it can
// and, for each kind of bar, also verify that the advertised/customizable lists
// include some representative items (if not, the lists might be wrong.)
TEST_F(BrowserWindowDefaultTouchBarUnitTest, TouchBarItems) {
  auto test_default_identifiers = [&](NSSet* expected_identifiers) {
    NSTouchBar* touch_bar = [touch_bar_ makeTouchBar];
    NSMutableSet<NSString*>* advertised_identifiers = [NSMutableSet set];
    [advertised_identifiers
        addObjectsFromArray:touch_bar.defaultItemIdentifiers];
    [advertised_identifiers
        addObjectsFromArray:touch_bar.customizationAllowedItemIdentifiers];
    [advertised_identifiers
        addObjectsFromArray:touch_bar.customizationRequiredItemIdentifiers];
    EXPECT_TRUE([expected_identifiers isSubsetOfSet:advertised_identifiers])
        << "Didn't find the expected identifiers "
        << expected_identifiers.description.UTF8String
        << " in the set of advertised identifiers "
        << advertised_identifiers.description.UTF8String << ".";
    for (NSString* identifier in advertised_identifiers) {
      EXPECT_NE(nil, [touch_bar itemForIdentifier:identifier])
          << "Didn't get a touch bar item for " << identifier.UTF8String;
    }
  };

  // Set to tab fullscreen.
  FullscreenController* fullscreen_controller =
      browser()->exclusive_access_manager()->fullscreen_controller();
  fullscreen_controller->set_is_tab_fullscreen_for_testing(true);
  EXPECT_TRUE(fullscreen_controller->IsTabFullscreen());

  // The fullscreen Touch Bar should include *at least* these items.
  test_default_identifiers([NSSet setWithArray:@[
    BrowserWindowDefaultTouchBar.fullscreenOriginItemIdentifier,
  ]]);

  // Exit fullscreen.
  fullscreen_controller->set_is_tab_fullscreen_for_testing(false);
  EXPECT_FALSE(fullscreen_controller->IsTabFullscreen());

  // The default Touch Bar should include *at least* these items.
  test_default_identifiers([NSSet setWithArray:@[
    BrowserWindowDefaultTouchBar.backItemIdentifier,
    BrowserWindowDefaultTouchBar.forwardItemIdentifier,
    BrowserWindowDefaultTouchBar.reloadOrStopItemIdentifier,
  ]]);
}

// Tests the reload or stop touch bar item.
TEST_F(BrowserWindowDefaultTouchBarUnitTest, ReloadOrStopTouchBarItem) {
  NSTouchBar* touch_bar = [touch_bar_ makeTouchBar];
  [touch_bar_ setIsPageLoading:NO];

  NSTouchBarItem* item =
      [touch_bar itemForIdentifier:BrowserWindowDefaultTouchBar
                                       .reloadOrStopItemIdentifier];
  NSButton* button = base::apple::ObjCCast<NSButton>([item view]);
  EXPECT_EQ(IDC_RELOAD, [button tag]);
  EXPECT_EQ([BrowserWindowDefaultTouchBar reloadIcon], [button image]);

  [touch_bar_ setIsPageLoading:YES];
  item = [touch_bar itemForIdentifier:BrowserWindowDefaultTouchBar
                                          .reloadOrStopItemIdentifier];
  button = base::apple::ObjCCast<NSButton>([item view]);
  EXPECT_EQ(IDC_STOP, [button tag]);
  EXPECT_EQ([BrowserWindowDefaultTouchBar navigateStopIcon], [button image]);
}

// Tests the bookmark star touch bar item.
TEST_F(BrowserWindowDefaultTouchBarUnitTest, BookmkarStarTouchBarItem) {
  NSTouchBar* touch_bar = [touch_bar_ makeTouchBar];

  [touch_bar_ setIsStarred:NO];
  NSTouchBarItem* item =
      [touch_bar itemForIdentifier:BrowserWindowDefaultTouchBar
                                       .bookmarkStarItemIdentifier];
  NSButton* button = base::apple::ObjCCast<NSButton>([item view]);
  EXPECT_EQ([BrowserWindowDefaultTouchBar starDefaultIcon], [button image]);

  [touch_bar_ setIsStarred:YES];
  item = [touch_bar itemForIdentifier:BrowserWindowDefaultTouchBar
                                          .bookmarkStarItemIdentifier];
  button = base::apple::ObjCCast<NSButton>([item view]);
  EXPECT_EQ([BrowserWindowDefaultTouchBar starActiveIcon], [button image]);
}

// Tests if the back button on the touch bar is in sync with the back command.
TEST_F(BrowserWindowDefaultTouchBarUnitTest, BackCommandUpdate) {
  NSTouchBar* touch_bar = [touch_bar_ makeTouchBar];
  NSTouchBarItem* item = [touch_bar
      itemForIdentifier:BrowserWindowDefaultTouchBar.backItemIdentifier];
  NSButton* button = base::apple::ObjCCast<NSButton>(item.view);

  UpdateCommandEnabled(IDC_BACK, true);
  EXPECT_TRUE(button.enabled);
  UpdateCommandEnabled(IDC_BACK, false);
  EXPECT_FALSE(button.enabled);
}

// Tests if the forward button on the touch bar is in sync with the forward
// command.
TEST_F(BrowserWindowDefaultTouchBarUnitTest, ForwardCommandUpdate) {
  NSTouchBar* touch_bar = [touch_bar_ makeTouchBar];
  NSTouchBarItem* item = [touch_bar
      itemForIdentifier:BrowserWindowDefaultTouchBar.forwardItemIdentifier];
  NSButton* button = base::apple::ObjCCast<NSButton>(item.view);

  UpdateCommandEnabled(IDC_FORWARD, true);
  EXPECT_TRUE(button.enabled);
  UpdateCommandEnabled(IDC_FORWARD, false);
  EXPECT_FALSE(button.enabled);
}

TEST_F(BrowserWindowDefaultTouchBarUnitTest, BackAccessibilityLabel) {
  NSTouchBar* touch_bar = [touch_bar_ makeTouchBar];
  NSTouchBarItem* item = [touch_bar
      itemForIdentifier:BrowserWindowDefaultTouchBar.backItemIdentifier];
  id<NSAccessibility> view = item.view;
  ASSERT_TRUE([view conformsToProtocol:@protocol(NSAccessibility)]);
  EXPECT_NSEQ(view.accessibilityTitle,
              l10n_util::GetNSString(IDS_ACCNAME_BACK));
}

TEST_F(BrowserWindowDefaultTouchBarUnitTest, ForwardAccessibilityLabel) {
  NSTouchBar* touch_bar = [touch_bar_ makeTouchBar];
  NSTouchBarItem* item = [touch_bar
      itemForIdentifier:BrowserWindowDefaultTouchBar.forwardItemIdentifier];
  id<NSAccessibility> view = item.view;
  ASSERT_TRUE([view conformsToProtocol:@protocol(NSAccessibility)]);
  EXPECT_NSEQ(view.accessibilityTitle,
              l10n_util::GetNSString(IDS_ACCNAME_FORWARD));
}

// Tests that the home button in the Touch Bar is in sync with the setting.
TEST_F(BrowserWindowDefaultTouchBarUnitTest, HomeUpdate) {
  NSTouchBar* touch_bar = [touch_bar_ makeTouchBar];

  // Save the current state before we start mucking with preferences.
  bool home_button_showing = ShowsHomeButton();

  SetShowHomeButton(false);
  touch_bar = [touch_bar_ makeTouchBar];

  NSString* home_identifier = [BrowserWindowDefaultTouchBar homeItemIdentifier];

  EXPECT_FALSE(
      [[touch_bar defaultItemIdentifiers] containsObject:home_identifier]);

  SetShowHomeButton(true);
  touch_bar = [touch_bar_ makeTouchBar];

  EXPECT_TRUE(
      [[touch_bar defaultItemIdentifiers] containsObject:home_identifier]);

  // Restore the original state.
  SetShowHomeButton(home_button_showing);
}