|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Real Flutter/AppKit quit (#65): app-menu AXPress and OS normal termination. |
| 3 | +
|
| 4 | +No mouse or keyboard input by default; policy is set through the debug VM service. |
| 5 | +With --pointer, all pointer presses stay on the fixture's own window. |
| 6 | +The menu and OS driver validates both PID and executable before targeting it. |
| 7 | +Build the title-bar example with --target tools/gui/application_quit_flutter.dart |
| 8 | +(an absolute path), then run this script. The runner is the ordinary Flutter one. |
| 9 | +""" |
| 10 | +from pathlib import Path |
| 11 | +import argparse |
| 12 | +import subprocess |
| 13 | +import tempfile |
| 14 | +import time |
| 15 | + |
| 16 | +from common import example |
| 17 | +from guiapp import Abort, Checks, assert_idle, pause |
| 18 | + |
| 19 | + |
| 20 | +def main(): |
| 21 | + parser = argparse.ArgumentParser(description=__doc__) |
| 22 | + parser.add_argument('--source', choices=('menu', 'system')) |
| 23 | + parser.add_argument('--pointer', action='store_true', help='click policy buttons instead of using the VM service') |
| 24 | + args = parser.parse_args() |
| 25 | + checks = Checks() |
| 26 | + with tempfile.TemporaryDirectory(prefix='nativeapi-quit-driver-') as temporary: |
| 27 | + driver = str(Path(temporary) / 'quit-driver') |
| 28 | + subprocess.run(['clang', '-fobjc-arc', '-framework', 'Cocoa', '-framework', |
| 29 | + 'ApplicationServices', str(Path(__file__).with_name( |
| 30 | + 'application_quit_macos_driver.m')), '-o', driver], check=True) |
| 31 | + for source in ((args.source,) if args.source else ('menu', 'system')): |
| 32 | + if args.pointer: |
| 33 | + assert_idle() |
| 34 | + app = example('window_title_bar_example', args=['-ApplePersistenceIgnoreState', 'YES']) |
| 35 | + try: |
| 36 | + app.launch() |
| 37 | + |
| 38 | + def counts(): |
| 39 | + view = next(v for v in app.views() if v.has('NativeAPI quit verification')) |
| 40 | + values = {} |
| 41 | + for label in ('Native requests', 'Host requests'): |
| 42 | + line = next(t for t, _ in view.texts if t.startswith(label + ': ')) |
| 43 | + values[label] = int(line.split(': ')[1]) |
| 44 | + return values['Native requests'], values['Host requests'] |
| 45 | + |
| 46 | + def select(title): |
| 47 | + if not args.pointer: |
| 48 | + vm = app.probe |
| 49 | + isolate = next(i['id'] for i in vm._call('getVM')['result']['isolates'] |
| 50 | + if i['name'] == 'main') |
| 51 | + mode = {'Native cancels': 'native', 'Flutter cancels': 'flutter', |
| 52 | + 'Allow quit': 'allow'}[title] |
| 53 | + result = vm._call('ext.nativeapi.quitMode', isolateId=isolate, mode=mode) |
| 54 | + if 'error' in result: |
| 55 | + raise Abort(f'cannot set fixture mode: {result}') |
| 56 | + pause(0.3) |
| 57 | + return |
| 58 | + _, frame = app.windows()[0] |
| 59 | + view = next(v for v in app.views() if v.has(title)) |
| 60 | + app.click(app.to_screen(frame, view, view.center(title))) |
| 61 | + pause(0.3) |
| 62 | + |
| 63 | + def request(): |
| 64 | + subprocess.run([driver, str(app.proc.pid), app.executable, source], |
| 65 | + check=True, timeout=10) |
| 66 | + |
| 67 | + def expect(native, host): |
| 68 | + deadline = time.monotonic() + 8 |
| 69 | + actual = None |
| 70 | + while time.monotonic() < deadline and app.proc.poll() is None: |
| 71 | + actual = counts() |
| 72 | + if actual == (native, host): |
| 73 | + break |
| 74 | + pause(0.2) |
| 75 | + # Give the asynchronous veto/host reply time to finish too. |
| 76 | + pause(0.5) |
| 77 | + alive = app.proc.poll() is None and len(app.windows()) == 1 |
| 78 | + checks.check(f'{source}: veto keeps the app and window alive', alive) |
| 79 | + if not alive: |
| 80 | + raise Abort('quit bypassed cancellation') |
| 81 | + checks.check(f'{source}: exactly {native} native and {host} host requests', |
| 82 | + counts() == (native, host), counts()) |
| 83 | + |
| 84 | + checks.check(f'{source}: counters start at zero', counts() == (0, 0)) |
| 85 | + request() |
| 86 | + expect(1, 0) |
| 87 | + request() |
| 88 | + expect(2, 0) |
| 89 | + select('Flutter cancels') |
| 90 | + request() |
| 91 | + expect(3, 1) |
| 92 | + request() |
| 93 | + expect(4, 2) |
| 94 | + select('Allow quit') |
| 95 | + request() |
| 96 | + try: |
| 97 | + app.proc.wait(timeout=10) |
| 98 | + except subprocess.TimeoutExpired: |
| 99 | + pass |
| 100 | + checks.check(f'{source}: approval exits normally', app.proc.poll() == 0, |
| 101 | + app.proc.poll()) |
| 102 | + output = app.output() |
| 103 | + checks.check(f'{source}: final native request is delivered once', |
| 104 | + output.count('NATIVE_QUIT 5 allow=true') == 1) |
| 105 | + checks.check(f'{source}: final Flutter approval is delivered once', |
| 106 | + output.count('HOST_QUIT 3 allow=true') == 1) |
| 107 | + except (Abort, subprocess.SubprocessError, StopIteration) as error: |
| 108 | + checks.check(f'{source}: scenario finishes', False, error) |
| 109 | + finally: |
| 110 | + app.quit() |
| 111 | + print(f'{source} log: {app.log_path}', flush=True) |
| 112 | + print(f'{checks.failures} failure(s)', flush=True) |
| 113 | + return int(bool(checks.failures)) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + raise SystemExit(main()) |
0 commit comments