File: interactive_test_utils_mac.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 (95 lines) | stat: -rw-r--r-- 3,622 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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/base/interactive_test_utils.h"

#include <Carbon/Carbon.h>
#import <Cocoa/Cocoa.h>

#include "base/apple/scoped_cftyperef.h"
#include "base/apple/scoped_objc_class_swizzler.h"
#include "base/logging.h"
#include "base/mac/mac_util.h"
#include "base/run_loop.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/chrome_browser_application_mac.h"
#import "ui/base/test/windowed_nsnotification_observer.h"
#include "ui/events/cocoa/cocoa_event_utils.h"
#include "ui/events/event_constants.h"

@interface NSApplication (Private)
// (Apparently) forces the application to activate itself.
- (void)_handleActivatedEvent:(id)arg1;
@end

namespace ui_test_utils {

void HideNativeWindow(gfx::NativeWindow native_window) {
  NSWindow* window = native_window.GetNativeNSWindow();
  [window orderOut:nil];
}

bool ShowAndFocusNativeWindow(gfx::NativeWindow native_window) {
  NSWindow* window = native_window.GetNativeNSWindow();
  // Make sure an unbundled program can get the input focus.
  ProcessSerialNumber psn = { 0, kCurrentProcess };
  TransformProcessType(&psn,kProcessTransformToForegroundApplication);
  // We used to call [NSApp activateIgnoringOtherApps:YES] but this
  // would not reliably activate the app, causing the window to never
  // become key. This bit of private API appears to be the secret
  // incantation that gets us what we want. See https://crbug.com/1215570.
  [NSApplication.sharedApplication _handleActivatedEvent:nil];

  WindowedNSNotificationObserver* async_waiter;
  if (!window.keyWindow) {
    // Only wait when expecting a change to actually occur.
    async_waiter = [[WindowedNSNotificationObserver alloc]
        initForNotification:NSWindowDidBecomeKeyNotification
                     object:window];
  }
  [window makeKeyAndOrderFront:nil];

  // Wait until |window| becomes key window, then make sure the shortcuts for
  // "Close Window" and "Close Tab" are updated.
  // This is because normal AppKit menu updating does not get invoked when
  // events are sent via ui_test_utils::SendKeyPressSync.
  BOOL notification_observed = [async_waiter wait];
  base::RunLoop().RunUntilIdle();  // There may be other events queued. Flush.
  NSMenu* file_menu = [[NSApp.mainMenu itemWithTag:IDC_FILE_MENU] submenu];
  [file_menu.delegate menuNeedsUpdate:file_menu];

  return !async_waiter || notification_observed;
}

bool ClearKeyEventModifiers() {
  static constexpr struct {
    CGEventFlags flag_mask;
    int key_code;
    const char* name;
  } kKnownModifiers[] = {
      {kCGEventFlagMaskCommand, kVK_Command, "Cmd"},
      {kCGEventFlagMaskShift, kVK_Shift, "Shift"},
      {kCGEventFlagMaskAlternate, kVK_Option, "Option"},
      // Expand as needed.
  };
  CGEventFlags event_flags =
      CGEventSourceFlagsState(kCGEventSourceStateCombinedSessionState);
  bool had_modifier = false;
  for (const auto& known_modifier : kKnownModifiers) {
    if (known_modifier.flag_mask & event_flags) {
      had_modifier = true;
      CGEventPost(kCGSessionEventTap,
                  base::apple::ScopedCFTypeRef<CGEventRef>(
                      CGEventCreateKeyboardEvent(
                          nullptr, known_modifier.key_code, false))
                      .get());
      LOG(ERROR) << "Modifier " << known_modifier.name
                 << " is hanging down, and may cause problems for any "
                    "subsequent test.";
    }
  }
  return had_modifier;
}

}  // namespace ui_test_utils