File: browser_window_touch_bar_controller_browsertest.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 (334 lines) | stat: -rw-r--r-- 12,882 bytes parent folder | download | duplicates (4)
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// 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_touch_bar_controller.h"

#import "base/apple/foundation_util.h"
#include "base/apple/scoped_objc_class_swizzler.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
#include "chrome/browser/ui/bookmarks/bookmark_tab_helper_observer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#import "chrome/browser/ui/cocoa/touchbar/browser_window_default_touch_bar.h"
#include "chrome/browser/ui/views/frame/browser_frame_mac.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/prefs/pref_service.h"
#include "components/remote_cocoa/app_shim/window_touch_bar_delegate.h"
#include "components/search_engines/default_search_manager.h"
#include "components/search_engines/search_engines_test_util.h"
#include "components/search_engines/template_url_data.h"
#include "components/search_engines/template_url_data_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "testing/gtest_mac.h"
#include "ui/gfx/native_widget_types.h"

// A class that watches for invalidations of a window's touch bar by calls
// to -setTouchBar:.
//
// Why is this class structured the way it is, with statics for the invalidation
// flag and the swizzler? We want to catch calls to -setTouchBar:. We can use
// swizzling for that. We implement -setTouchBar: in this class and swap it for
// the NSWindow implementation. Once inside TouchBarInvalidationWatcher's
// -setTouchBar: we need to set a flag somewhere to the value of (aTouchBar ==
// nil). In theory that flag could just be an instance variable of this class.
// The problem is when we reach -setTouchBar:, although the code comes from
// TouchBarInvalidationWatcher, the instance "executing" the code is an
// NSWindow. If we create an instance of TouchBarInvalidationWatcher that has a
// bool instance variable, say, the NSWindow can't access it (it's not the
// original TouchBarInvalidationWatcher instance). Similarly, at the end of
// -setTouchBar: we need to call the original (NSWindow) implementation of
// -setTouchBar:. To do that we need access to the ScopedObjCClassSwizzler that
// did the swap. If we store the ScopedObjCClassSwizzler in an instance variable
// of TouchBarInvalidationWatcher, our NSWindow instance again cannot access it.
//
// To get around these problems we store the flag and the swizzler in what are
// essentially class variables so they are accessible from anywhere.
@interface TouchBarInvalidationWatcher : NSObject
// Returns a new (non-autoreleased) TouchBarInvalidationWatcher.
+ (instancetype)newWatcher;

// Returns the touch bar invalidation flag. This flag is set to YES
// whenever -[NSWindow setTouchBar:] is called with nil.
+ (BOOL&)touchBarInvalidFlag;
@end

@implementation TouchBarInvalidationWatcher

+ (BOOL&)touchBarInvalidFlag {
  static BOOL touchBarInvalidFlag = NO;
  return touchBarInvalidFlag;
}

+ (std::unique_ptr<base::apple::ScopedObjCClassSwizzler>&)setTouchBarSwizzler {
  static base::NoDestructor<
      std::unique_ptr<base::apple::ScopedObjCClassSwizzler>>
      setTouchBarSwizzler(new base::apple::ScopedObjCClassSwizzler(
          [NSWindow class], [TouchBarInvalidationWatcher class],
          @selector(setTouchBar:)));

  return *setTouchBarSwizzler;
}

+ (instancetype)newWatcher {
  // Set up the swizzling.
  [self setTouchBarSwizzler];

  return [[TouchBarInvalidationWatcher alloc] init];
}

- (void)dealloc {
  [TouchBarInvalidationWatcher setTouchBarSwizzler].reset();
}

- (void)setTouchBar:(NSTouchBar*)aTouchBar {
  [TouchBarInvalidationWatcher touchBarInvalidFlag] = (aTouchBar == nil);

  // Proceed with setting the touch bar.
  [TouchBarInvalidationWatcher setTouchBarSwizzler]
      ->InvokeOriginal<void, NSTouchBar*>(self, @selector(setTouchBar:),
                                          aTouchBar);
}

@end

// A class that watches for page reload notifications in a window's touch bar.
//
// See the explanation at the top of TouchBarInvalidationWatcher for info on
// why this class is structured the way it is.
@interface PageReloadWatcher : NSObject
// Returns a new (non-autoreleased) PageReloadWatcher.
+ (instancetype)newWatcher;

// Returns the page loading flag. This flag is set to YES whenever
// -[BrowserWindowDefaultTouchBar setIsPageLoading:] is called with YES.
+ (BOOL&)pageIsLoadingFlag;
@end

@implementation PageReloadWatcher

+ (BOOL&)pageIsLoadingFlag {
  static BOOL pageIsLoadingFlag = NO;
  return pageIsLoadingFlag;
}

+ (std::unique_ptr<base::apple::ScopedObjCClassSwizzler>&)
    setPageIsLoadingSwizzler {
  static base::NoDestructor<
      std::unique_ptr<base::apple::ScopedObjCClassSwizzler>>
      setPageIsLoadingSwizzler(new base::apple::ScopedObjCClassSwizzler(
          [BrowserWindowDefaultTouchBar class], [PageReloadWatcher class],
          @selector(setIsPageLoading:)));

  return *setPageIsLoadingSwizzler;
}

+ (instancetype)newWatcher {
  // Set up the swizzling.
  [self setPageIsLoadingSwizzler];

  return [[PageReloadWatcher alloc] init];
}

- (void)dealloc {
  [PageReloadWatcher setPageIsLoadingSwizzler].reset();
}

- (void)setIsPageLoading:(BOOL)flag {
  if (flag) {
    [PageReloadWatcher pageIsLoadingFlag] = YES;
  }

  [PageReloadWatcher setPageIsLoadingSwizzler]->InvokeOriginal<void, BOOL>(
      self, @selector(setIsPageLoading:), flag);
}

@end

class BrowserWindowTouchBarControllerTest : public InProcessBrowserTest {
 public:
  BrowserWindowTouchBarControllerTest() = default;

  BrowserWindowTouchBarControllerTest(
      const BrowserWindowTouchBarControllerTest&) = delete;
  BrowserWindowTouchBarControllerTest& operator=(
      const BrowserWindowTouchBarControllerTest&) = delete;

  NSTouchBar* MakeTouchBar() {
    auto* delegate =
        static_cast<NSObject<WindowTouchBarDelegate>*>(ns_window());
    return [delegate makeTouchBar];
  }

  gfx::NativeWindow native_window() const {
    return browser()->window()->GetNativeWindow();
  }

  NSWindow* ns_window() const { return native_window().GetNativeNSWindow(); }

  BrowserWindowTouchBarController* browser_touch_bar_controller() const {
    BrowserView* browser_view =
        BrowserView::GetBrowserViewForNativeWindow(native_window());
    EXPECT_TRUE(browser_view);
    if (!browser_view) {
      return nil;
    }

    BrowserFrameMac* browser_frame = static_cast<BrowserFrameMac*>(
        browser_view->frame()->native_browser_frame());
    return browser_frame->GetTouchBarController();
  }
};

// Test if the touch bar gets invalidated when the active tab is changed.
IN_PROC_BROWSER_TEST_F(BrowserWindowTouchBarControllerTest, TabChanges) {
  [[maybe_unused]] TouchBarInvalidationWatcher* invalidationWatcher =
      [TouchBarInvalidationWatcher newWatcher];

  EXPECT_FALSE(browser_touch_bar_controller());
  MakeTouchBar();
  EXPECT_TRUE(browser_touch_bar_controller());

  auto* current_touch_bar = [ns_window() touchBar];
  EXPECT_TRUE(current_touch_bar);

  // Insert a new tab in the foreground. The window should invalidate its
  // touch bar as a result.
  [TouchBarInvalidationWatcher touchBarInvalidFlag] = NO;
  ASSERT_FALSE([TouchBarInvalidationWatcher touchBarInvalidFlag]);
  std::unique_ptr<content::WebContents> contents = content::WebContents::Create(
      content::WebContents::CreateParams(browser()->profile()));
  browser()->tab_strip_model()->AppendWebContents(std::move(contents), true);

  EXPECT_TRUE([TouchBarInvalidationWatcher touchBarInvalidFlag]);

  // Update the touch bar.
  [ns_window() touchBar];

  // Activating the original tab should invalidate the touch bar.
  [TouchBarInvalidationWatcher touchBarInvalidFlag] = NO;
  ASSERT_FALSE([TouchBarInvalidationWatcher touchBarInvalidFlag]);
  browser()->tab_strip_model()->ActivateTabAt(0);

  EXPECT_TRUE([TouchBarInvalidationWatcher touchBarInvalidFlag]);
}

// Test if the touch bar receives a notification that the current tab is
// loading.
IN_PROC_BROWSER_TEST_F(BrowserWindowTouchBarControllerTest, PageReload) {
  [[maybe_unused]] PageReloadWatcher* pageReloadWatcher =
      [PageReloadWatcher newWatcher];

  EXPECT_FALSE(browser_touch_bar_controller());
  MakeTouchBar();
  EXPECT_TRUE(browser_touch_bar_controller());

  // Make sure the touch bar exists for the window.
  auto* current_touch_bar = [ns_window() touchBar];
  EXPECT_TRUE(current_touch_bar);

  // We can't just ask the BrowserWindowDefaultTouchBar for the value of the
  // page loading flag like we can for the tab bookmark. The reload my happen
  // so fast that the flag may be reset to NO by the time we check it. We
  // have to use swizzling instead.
  [PageReloadWatcher pageIsLoadingFlag] = NO;
  ASSERT_FALSE([PageReloadWatcher pageIsLoadingFlag]);

  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), GURL("data:text/html, <html><body></body></html>")));

  EXPECT_TRUE([PageReloadWatcher pageIsLoadingFlag]);
}

// Test if the touch bar receives a notification that the current tab has been
// bookmarked.
IN_PROC_BROWSER_TEST_F(BrowserWindowTouchBarControllerTest,
                       BookmarkCurrentTab) {
  EXPECT_FALSE(browser_touch_bar_controller());
  MakeTouchBar();
  EXPECT_TRUE(browser_touch_bar_controller());

  // Make sure the touch bar exists for the window.
  auto* current_touch_bar = [ns_window() touchBar];
  EXPECT_TRUE(current_touch_bar);
  BrowserWindowDefaultTouchBar* touch_bar_delegate =
      base::apple::ObjCCastStrict<BrowserWindowDefaultTouchBar>(
          [current_touch_bar delegate]);
  EXPECT_FALSE([touch_bar_delegate isStarred]);

  chrome::BookmarkCurrentTab(browser());

  EXPECT_TRUE([touch_bar_delegate isStarred]);
}

// Tests if the touch bar's search button updates if the default search engine
// has changed.
IN_PROC_BROWSER_TEST_F(BrowserWindowTouchBarControllerTest,
                       SearchEngineChanges) {
  [[maybe_unused]] TouchBarInvalidationWatcher* invalidationWatcher =
      [TouchBarInvalidationWatcher newWatcher];

  PrefService* prefs = browser()->profile()->GetPrefs();
  DCHECK(prefs);

  EXPECT_FALSE(browser_touch_bar_controller());
  MakeTouchBar();

  // Force the window to create the touch bar.
  [ns_window() touchBar];
  NSString* orig_search_button_title =
      [[[browser_touch_bar_controller() defaultTouchBar] searchButton] title];
  EXPECT_TRUE(orig_search_button_title);

  // Change the default search engine.
  [TouchBarInvalidationWatcher touchBarInvalidFlag] = NO;
  ASSERT_FALSE([TouchBarInvalidationWatcher touchBarInvalidFlag]);
  std::unique_ptr<TemplateURLData> data =
      GenerateDummyTemplateURLData("poutine");
  prefs->SetDict(DefaultSearchManager::kDefaultSearchProviderDataPrefName,
                 TemplateURLDataToDictionary(*data));

  // Confirm the touch bar was invalidated.
  EXPECT_TRUE([TouchBarInvalidationWatcher touchBarInvalidFlag]);

  // Ask the window again for its touch bar. Previously, changes like updates
  // to the default search engine would completely regenerate the touch bar.
  // That's expensive (view creation, autolayout, etc.). Instead we now retain
  // the original touch bar and expect touch bar invalidation to force an
  // update of the search item.
  [ns_window() touchBar];
  EXPECT_FALSE([orig_search_button_title
      isEqualToString:[[[browser_touch_bar_controller() defaultTouchBar]
                          searchButton] title]]);
}

// Tests to see if the touch bar's bookmark tab helper observer gets removed
// when the touch bar is destroyed.
IN_PROC_BROWSER_TEST_F(BrowserWindowTouchBarControllerTest,
                       DestroyNotificationBridge) {
  MakeTouchBar();

  ASSERT_TRUE([browser_touch_bar_controller() defaultTouchBar]);

  BookmarkTabHelperObserver* observer =
      [[browser_touch_bar_controller() defaultTouchBar] bookmarkTabObserver];
  std::unique_ptr<content::WebContents> contents = content::WebContents::Create(
      content::WebContents::CreateParams(browser()->profile()));
  browser()->tab_strip_model()->AppendWebContents(std::move(contents), true);

  BookmarkTabHelper* tab_helper = BookmarkTabHelper::FromWebContents(
      browser()->tab_strip_model()->GetActiveWebContents());
  ASSERT_TRUE(tab_helper);
  EXPECT_TRUE(tab_helper->HasObserver(observer));

  [[browser_touch_bar_controller() defaultTouchBar] setBrowser:nullptr];
  EXPECT_FALSE(tab_helper->HasObserver(observer));
}