File: cocoa_helper.mm

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (337 lines) | stat: -rw-r--r-- 11,450 bytes parent folder | download | duplicates (5)
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
335
336
337
// 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 "ui/base/test/cocoa_helper.h"

#include <objc/message.h>
#include <objc/runtime.h>

#include <set>
#include <vector>

#include "base/debug/debugger.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/test/mock_chrome_application_mac.h"
#include "base/test/test_timeouts.h"

@implementation CocoaTestHelperWindow

@synthesize pretendIsKeyWindow = _pretendIsKeyWindow;
@synthesize pretendIsOnActiveSpace = _pretendIsOnActiveSpace;
@synthesize pretendFullKeyboardAccessIsEnabled =
    _pretendFullKeyboardAccessIsEnabled;
@synthesize useDefaultConstraints = _useDefaultConstraints;

- (instancetype)initWithContentRect:(NSRect)contentRect {
  self = [super initWithContentRect:contentRect
                          styleMask:NSWindowStyleMaskBorderless
                            backing:NSBackingStoreBuffered
                              defer:NO];
  if (self) {
    _useDefaultConstraints = YES;
    _pretendIsOnActiveSpace = YES;
    self.releasedWhenClosed = NO;
  }
  return self;
}

- (instancetype)init {
  return [self initWithContentRect:NSMakeRect(0, 0, 800, 600)];
}

// Enables users to write debugging code to help diagnose failures to close
// CocoaTestHelperWindow. Debugging code is not usually committed in Chromium,
// but because it's difficult to correctly override retain/release in ARC, this
// is left in.
#if 0

+ (void)initialize {
  if (self == [CocoaTestHelperWindow self]) {
    Class test_class = [CocoaTestHelperWindow class];

    Method method = class_getInstanceMethod(test_class, @selector(debugRetain));
    ASSERT_TRUE(method);
    ASSERT_TRUE(class_addMethod(test_class, sel_registerName("retain"),
                                method_getImplementation(method),
                                method_getTypeEncoding(method)));

    method = class_getInstanceMethod(test_class, @selector(debugRelease));
    ASSERT_TRUE(method);
    ASSERT_TRUE(class_addMethod(test_class, sel_registerName("release"),
                                method_getImplementation(method),
                                method_getTypeEncoding(method)));
  }
}

- (void)dealloc {
  // Insert debugging code here.
}

- (instancetype)debugRetain {
  // Insert debugging code here.

  struct objc_super mySuper = {.receiver = self,
                               .super_class = [self superclass]};
  using retainSendSuper = id (*)(struct objc_super*, SEL);
  retainSendSuper sendSuper =
      reinterpret_cast<retainSendSuper>(objc_msgSendSuper);
  return sendSuper(&mySuper, _cmd);
}

- (oneway void)debugRelease {
  // Insert debugging code here.

  struct objc_super mySuper = {.receiver = self,
                               .super_class = [self superclass]};
  using releaseSendSuper = void (*)(struct objc_super*, SEL);
  releaseSendSuper sendSuper =
      reinterpret_cast<releaseSendSuper>(objc_msgSendSuper);
  return sendSuper(&mySuper, _cmd);
}

#endif

- (BOOL)isKeyWindow {
  return _pretendIsKeyWindow;
}

- (BOOL)isOnActiveSpace {
  return _pretendIsOnActiveSpace;
}

- (void)makePretendKeyWindowAndSetFirstResponder:(NSResponder*)responder {
  EXPECT_TRUE([self makeFirstResponder:responder]);
  self.pretendIsKeyWindow = YES;
}

- (void)clearPretendKeyWindowAndFirstResponder {
  self.pretendIsKeyWindow = NO;
  EXPECT_TRUE([self makeFirstResponder:NSApp]);
}

- (void)setPretendIsOnActiveSpace:(BOOL)pretendIsOnActiveSpace {
  _pretendIsOnActiveSpace = pretendIsOnActiveSpace;
  [NSWorkspace.sharedWorkspace.notificationCenter
      postNotificationName:NSWorkspaceActiveSpaceDidChangeNotification
                    object:NSWorkspace.sharedWorkspace];
}

- (void)setPretendFullKeyboardAccessIsEnabled:(BOOL)enabled {
  EXPECT_TRUE([NSWindow
      instancesRespondToSelector:@selector(_allowsAnyValidResponder)]);
  _pretendFullKeyboardAccessIsEnabled = enabled;
  [self recalculateKeyViewLoop];
}

// Override of an undocumented AppKit method which controls call to check if
// full keyboard access is enabled. Its presence is verified in
// -setPretendFullKeyboardAccessIsEnabled:.
- (BOOL)_allowsAnyValidResponder {
  return _pretendFullKeyboardAccessIsEnabled;
}

- (NSArray<NSView*>*)validKeyViews {
  NSMutableArray<NSView*>* validKeyViews = [NSMutableArray array];
  NSView* contentView = self.contentView;
  if (contentView.canBecomeKeyView) {
    [validKeyViews addObject:contentView];
  }
  for (NSView* keyView = contentView.nextValidKeyView;
       keyView != nil && ![validKeyViews containsObject:keyView];
       keyView = keyView.nextValidKeyView) {
    [validKeyViews addObject:keyView];
  }
  return validKeyViews;
}

- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen*)screen {
  if (!_useDefaultConstraints) {
    return frameRect;
  }

  return [super constrainFrameRect:frameRect toScreen:screen];
}

@end

namespace ui {

CocoaTestHelper::CocoaTestHelper() {
  // If a test suite hasn't already initialized NSApp, register the mock one
  // now.
  if (!NSApp) {
    mock_cr_app::RegisterMockCrApp();
  }

  // Set the duration of AppKit-evaluated animations (such as frame changes)
  // to zero for testing purposes. That way they take effect immediately.
  NSAnimationContext.currentContext.duration = 0.0;

  // The above does not affect window-resize time, such as for an
  // attached sheet dropping in.  Set that duration for the current
  // process (this is not persisted).  Empirically, the value of 0.0
  // is ignored.
  NSDictionary* dict = @{@"NSWindowResizeTime" : @"0.01"};
  [NSUserDefaults.standardUserDefaults registerDefaults:dict];

  MarkCurrentWindowsAsInitial();
}

CocoaTestHelper::~CocoaTestHelper() {
  // Call close on the test_window to clean it up if one was opened.
  [test_window_ clearPretendKeyWindowAndFirstResponder];
  [test_window_ close];
  test_window_ = nil;

  // Recycle the pool to clean up any stuff that was put on the
  // autorelease pool due to window or window controller closures.
  pool_.Recycle();

  // Some controls (NSTextFields, NSComboboxes etc) use
  // performSelector:withDelay: to clean up drag handlers and other
  // things (Radar 5851458 "Closing a window with a NSTextView in it
  // should get rid of it immediately").  The event loop must be spun
  // to get everything cleaned up correctly.  It normally only takes
  // one to two spins through the event loop to see a change.

  // NOTE(shess): Under valgrind, -nextEventMatchingMask:* in one test
  // needed to run twice, once taking .2 seconds, the next time .6
  // seconds.  The loop exit condition attempts to be scalable.

  // Get the set of windows which weren't present when the test
  // started.
  WeakWindowVector windows_left = WindowsLeft();

  while (!windows_left.empty()) {
    // Cover delayed actions by spinning the loop at least once after
    // this timeout.
    const NSTimeInterval kCloseTimeoutSeconds =
        TestTimeouts::action_timeout().InSecondsF();

    // Cover chains of delayed actions by spinning the loop at least
    // this many times.
    const int kCloseSpins = 3;

    // Track the set of remaining windows so that everything can be
    // reset if progress is made.
    WeakWindowVector still_left = windows_left;

    NSDate* start_date = [NSDate date];
    bool one_more_time = true;
    int spins = 0;
    while (still_left.size() == windows_left.size() &&
           (spins < kCloseSpins || one_more_time)) {
      // Check the timeout before pumping events, so that we'll spin
      // the loop once after the timeout.
      one_more_time = start_date.timeIntervalSinceNow > -kCloseTimeoutSeconds;

      // Autorelease anything thrown up by the event loop.
      @autoreleasepool {
        ++spins;
        NSEvent* next_event = [NSApp nextEventMatchingMask:NSEventMaskAny
                                                 untilDate:nil
                                                    inMode:NSDefaultRunLoopMode
                                                   dequeue:YES];
        [NSApp sendEvent:next_event];
        [NSApp updateWindows];
      }

      // Refresh the outstanding windows.
      still_left = WindowsLeft();
    }

    // If no progress is being made, log a failure and continue.
    if (still_left.size() == windows_left.size()) {
      // NOTE(shess): Failing this expectation means that the test
      // opened windows which have not been fully released.  Either
      // there is a leak, or perhaps one of |kCloseTimeoutSeconds| or
      // |kCloseSpins| needs adjustment.
      EXPECT_EQ(0U, windows_left.size());
      for (NSWindow* __weak window : windows_left) {
        LOG(WARNING) << "Didn't close window "
                     << base::SysNSStringToUTF8(window.description);
      }
      break;
    }

    windows_left = still_left;
  }
}

void CocoaTestHelper::MarkCurrentWindowsAsInitial() {
  // Collect the list of windows that were open when the test started so
  // that we don't wait for them to close in TearDown. Has to be done
  // after BootstrapCocoa is called.
  initial_windows_ = ApplicationWindows();
}

CocoaTestHelperWindow* CocoaTestHelper::test_window() {
  if (!test_window_) {
    test_window_ = [[CocoaTestHelperWindow alloc] init];
    if (base::debug::BeingDebugged()) {
      [test_window_ orderFront:nil];
    } else {
      [test_window_ orderBack:nil];
    }
  }
  return test_window_;
}

// Returns a vector of currently open windows.
CocoaTestHelper::WeakWindowVector CocoaTestHelper::ApplicationWindows() {
  WeakWindowVector windows;

  // Must create a pool here because [NSApp windows] has created an array which
  // retains all the windows in it.
  @autoreleasepool {
    for (NSWindow* window in NSApp.windows) {
      windows.push_back(window);
    }
    return windows;
  }
}

CocoaTestHelper::WeakWindowVector CocoaTestHelper::WindowsLeft() {
  // Window pointers can go nil only when the run loop is going, so it's safe to
  // use sets within this function, just not outside it.
  using WeakWindowSet = std::set<NSWindow * __weak>;

  WeakWindowVector windows = ApplicationWindows();
  WeakWindowSet windows_set(windows.begin(), windows.end());

  // Ignore TextInputUIMacHelper.framework created TUINSWindow. We have no
  // control or documentation about these windows, ignoring them seems like the
  // best approach.
  std::erase_if(windows_set, [](NSWindow* __weak set_window) {
    return [set_window isKindOfClass:NSClassFromString(@"TUINSWindow")];
  });

  // Subtract away the initial windows. The current window set will not have any
  // nil values, as it was just obtained, so subtracting away the nil from any
  // initial windows that have been closed is safe.
  WeakWindowSet initial_windows_set(initial_windows_.begin(),
                                    initial_windows_.end());

  WeakWindowSet windows_left_set =
      base::STLSetDifference<WeakWindowSet>(windows_set, initial_windows_set);
  return std::vector(windows_left_set.begin(), windows_left_set.end());
}

CocoaTest::CocoaTest() : helper_(std::make_unique<CocoaTestHelper>()) {}
CocoaTest::~CocoaTest() {
  CHECK(!helper_);
}

void CocoaTest::TearDown() {
  helper_.reset();
  PlatformTest::TearDown();
}

void CocoaTest::MarkCurrentWindowsAsInitial() {
  helper_->MarkCurrentWindowsAsInitial();
}

}  // namespace ui