Files
yiyezhqiu-refactored/tools/acceptance/toolbar_writes.py
T

138 lines
6.3 KiB
Python

"""Real GUI writes and history restores, restricted to an acceptance copy."""
import argparse
import json
from pathlib import Path
import shlex
import ctypes
import win32gui
import win32process
from pywinauto.keyboard import send_keys
from pywinauto import mouse
from packaged_menu import tools_running, wait_for
from toolbar_ui import ToolbarUI
def main():
parser = argparse.ArgumentParser()
parser.add_argument('executable', type=Path)
parser.add_argument('--project', required=True, type=Path)
parser.add_argument('--evidence', required=True, type=Path)
options = parser.parse_args()
exe, project, base = options.executable.resolve(), options.project.resolve(), options.evidence.resolve()
assert 'acceptance' in project.parts
base.mkdir(parents=True, exist_ok=True)
tool = wait_for(lambda: next((p for p in tools_running() if Path(p.executable).resolve() == exe
and '--request' in p.command_line), None))
args = shlex.split(tool.command_line, posix=False)
request = Path(args[args.index('--request') + 1].strip('"'))
status_path = request.with_name('apply-status.json')
ui = ToolbarUI(tool.pid)
def snapshot():
data = json.loads(request.read_text(encoding='utf-8-sig'))
assert Path(data['design_path']).resolve() == project
return data
initial = snapshot()
selected = [c for c in initial['components'] if c['selected']]
assert len(selected) == 1 and selected[0]['refdes'] in ('CE17', 'C9000'), selected
uid = selected[0]['stable_uid']
def component():
return next(c for c in snapshot()['components'] if c['stable_uid'] == uid)
def attr(name):
return next(a for a in component()['attributes'] if a['name'] == name)
def show(page):
if ui.panel.exists(timeout=.2) and ui.panel.is_visible():
ui.collapse()
ui.feature(page)
results = []
def transaction(name, action, expected):
before = status_path.stat().st_mtime_ns if status_path.exists() else 0
action()
def completed():
if not status_path.exists() or status_path.stat().st_mtime_ns == before:
return None
data = json.loads(status_path.read_text(encoding='utf-8-sig'))
assert data['state'] == 'applied', data
return data
status = wait_for(completed, 30)
wait_for(expected, 30)
(base / f'{name}-snapshot.json').write_text(json.dumps(snapshot(), ensure_ascii=False, indent=2), encoding='utf-8')
results.append({'case': name, 'native_status': status, 'readback_verified': True})
print('PASS TOOLBAR WRITE', name, flush=True)
def restore(name, expected):
show('操作历史')
button = ui.panel.child_window(title='恢复所选操作', control_type='Button')
wait_for(button.is_enabled, 10)
def action():
button.click_input()
dialog = ui.panel.child_window(title='恢复所选操作', control_type='Window')
dialog.wait('visible', timeout=10)
yes = next(b for b in dialog.descendants(control_type='Button')
if b.window_text() in ('Yes', '&Yes', '是', '是(&Y)'))
yes.click_input()
transaction(name, action, expected)
if selected[0]['refdes'] == 'C9000':
restore('previous-acceptance-restore', lambda: component()['refdes'] == 'CE17')
initial = snapshot()
show('自动位号')
ui.panel.child_window(title='全部重编').click_input()
spinner = next(s for s in ui.panel.descendants(control_type='Spinner') if s.is_visible())
spinner.iface_value.SetValue('9000')
apply = ui.panel.child_window(title='应用位号', control_type='Button')
wait_for(apply.is_enabled, 10)
ui.capture(ui.panel).save(str(base / 'refdes-preview.png'))
transaction('refdes-apply', apply.click_input, lambda: component()['refdes'] == 'C9000')
restore('refdes-restore', lambda: component()['refdes'] == 'CE17')
show('属性批改')
ui.select_combo(ui.panel.child_window(control_type='ComboBox'), 0, 'Value')
old_value = attr('Value')['value']
# Qt's table editor does not publish its temporary line edit through UIA.
# This point is inside the visible first row's final (new value) column.
table = ui.panel.child_window(control_type='Table').rectangle()
scale = ctypes.windll.user32.GetDpiForWindow(ui.panel.handle) / 96
point = (table.right - round(80 * scale), table.top + round(52 * scale))
assert win32process.GetWindowThreadProcessId(win32gui.WindowFromPoint(point))[1] == ui.pid
mouse.double_click(coords=point)
assert win32process.GetWindowThreadProcessId(win32gui.GetForegroundWindow())[1] == ui.pid
send_keys('^aTOOLBAR_NATIVE_VALUE{ENTER}')
apply = ui.panel.child_window(title='应用属性修改', control_type='Button')
wait_for(apply.is_enabled, 10)
transaction('value-apply', apply.click_input, lambda: attr('Value')['value'] == 'TOOLBAR_NATIVE_VALUE')
restore('value-restore', lambda: attr('Value')['value'] == old_value)
show('属性显示')
ui.select_combo(ui.panel.child_window(control_type='ComboBox'), 0, 'Part Name')
was_visible = attr('Part Name')['visible']
ui.panel.child_window(title='隐藏' if was_visible else '显示', control_type='RadioButton').click_input()
apply = ui.panel.child_window(title='应用显示状态', control_type='Button')
wait_for(apply.is_enabled, 10)
transaction('visibility-apply', apply.click_input, lambda: attr('Part Name')['visible'] != was_visible)
restore('visibility-restore', lambda: attr('Part Name')['visible'] == was_visible)
def design_values(data):
return {c['stable_uid']: (c['refdes'], c['x'], c['y'],
sorted((a['name'], a['value'], a['visible']) for a in c['attributes']))
for c in data['components']}
assert design_values(initial) == design_values(snapshot()), 'Design values differ from initial baseline'
(base / 'toolbar-writes.json').write_text(json.dumps({'pid': tool.pid, 'executable': str(exe),
'project': str(project), 'cases': results, 'all_components_restored': len(initial['components'])},
ensure_ascii=False, indent=2), encoding='utf-8')
ui.collapse()
print('PASS ALL TOOLBAR WRITES AND RESTORES', flush=True)
if __name__ == '__main__':
main()