diff --git a/tests/ui/test_pdf_export_page.py b/tests/ui/test_pdf_export_page.py index fae5e2d..641c71a 100644 --- a/tests/ui/test_pdf_export_page.py +++ b/tests/ui/test_pdf_export_page.py @@ -186,15 +186,27 @@ def test_disconnected_project_scope_hides_block_fields(qtbot): assert not page.export_button.isEnabled() -def test_themed_pdf_scopes_fit_and_advanced_parameters_scroll(qtbot, qapp, tmp_path): - from dx_productivity.ui.theme import Theme, apply_theme +def test_pdf_scopes_fit_approved_window_minimum_and_advanced_parameters_scroll(qtbot, qapp, tmp_path): + from dx_productivity.ui.app import _load_windows_ui_fonts + from dx_productivity.ui.main_window import MainWindow + from dx_productivity.ui.theme import Theme previous_stylesheet = qapp.styleSheet() + previous_font = qapp.font() + window = None try: - apply_theme(qapp, Theme.LIGHT) + _load_windows_ui_fonts(qapp) + window = MainWindow(theme=Theme.LIGHT, layout_path=tmp_path / 'layout.json') + qtbot.addWidget(window) + qtbot.addWidget(window.toolbar) page = PdfExportPage() - qtbot.addWidget(page) - page.resize(640, 270) - page.show() + window.set_page(7, page, 'PDF') + window.set_active_page(7) + window.show() + qtbot.wait(10) + window.resize(window.minimumSize()) + qtbot.wait(10) + assert window.size().toTuple() == (660, 340) + assert page.size().toTuple() == (660, 280) for scope in ExportScope: page.bind(_snapshot('中文 Digital signal'), PdfExportSettings(Path('C:/Demo.prj'), scope, 'Main', tmp_path)) qtbot.wait(10) @@ -208,4 +220,7 @@ def test_themed_pdf_scopes_fit_and_advanced_parameters_scroll(qtbot, qapp, tmp_p assert page.project_input.text() == 'Demo.prj' assert page.project_input.toolTip() == 'C:/Demo.prj' finally: + if window is not None: + window.request_exit() + qapp.setFont(previous_font) qapp.setStyleSheet(previous_stylesheet) diff --git a/tools/acceptance/compact_window.py b/tools/acceptance/compact_window.py index 3d2a3bf..0c5bf7d 100644 --- a/tools/acceptance/compact_window.py +++ b/tools/acceptance/compact_window.py @@ -32,7 +32,8 @@ def main(): assert bounds[3] * 96 / dpi <= 46, (bounds, dpi) assert win32gui.GetWindowLong(handle, -20) & 0x8 buttons = [button.rectangle() for button in ui.toolbar.descendants(control_type="Button")] - assert len({(rect.top, rect.bottom) for rect in buttons}) == 1, buttons + centers = [(rect.top + rect.bottom) / 2 for rect in buttons] + assert max(centers) - min(centers) <= 2, buttons ui.toolbar.capture_as_image().save(str(evidence / "toolbar.png")) if ui.panel.exists(timeout=0.2) and ui.panel.is_visible(): ui.collapse() diff --git a/tools/acceptance/native_read_tools.py b/tools/acceptance/native_read_tools.py new file mode 100644 index 0000000..34cce37 --- /dev/null +++ b/tools/acceptance/native_read_tools.py @@ -0,0 +1,90 @@ +"""Exercise visible read controls and native Excel output on a dedicated copy.""" +import argparse +import json +from pathlib import Path +import shlex +import time + +from pywinauto import Application +import win32com.client + +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('"')) + snapshot = json.loads(request.read_text(encoding='utf-8-sig')) + assert Path(snapshot['design_path']).resolve() == project + ui = ToolbarUI(tool.pid) + if ui.panel.exists(timeout=0.2) and ui.panel.is_visible(): + ui.collapse() + results = [] + for page, checks in (('设计检查', ('孤立网络', '全部网络', 'Value 遗漏', 'Value1 遗漏')), + ('网络工具', ('全部网络', '跨页查询'))): + ui.feature(page) + combo = ui.panel.child_window(title='检查类型', control_type='ComboBox') + for check in checks: + combo.select(check) + if check == '跨页查询': + ui.panel.child_window(control_type='Edit').set_edit_text('GND') + ui.panel.child_window(title='运行检查', control_type='Button').invoke() + prefix = {'孤立网络': '孤立网络:', '全部网络': '网络总数:', + 'Value 遗漏': 'Value 遗漏:', 'Value1 遗漏': 'Value1 遗漏:', + '跨页查询': 'GND 出现在'}[check] + summary = wait_for(lambda: next((text for text in ui.texts(ui.panel) if text.startswith(prefix)), None), 10) + results.append({'page': page, 'operation': check, 'result': summary}) + print('PASS NATIVE READ TOOL', page, check, summary, flush=True) + ui.collapse() + ui.feature('BOM 工具') + wait_for(lambda: any(text.startswith('BOM 行数:') for text in ui.texts(ui.panel)), 10) + output = base / 'BOM-toolbar.xls' + if output.exists(): + raise AssertionError('Use a fresh evidence directory to retain the old workbook') + ui.panel.child_window(title='导出 Excel…', control_type='Button').click_input() + dialog = ui.app.window(title='导出 BOM') + dialog.wait('visible', timeout=10) + filename = dialog.child_window(auto_id='1001', control_type='Edit') + filename.set_edit_text(str(output)) + save = next(button for button in dialog.descendants(control_type='Button') + if '保存' in button.window_text() or 'Save' in button.window_text()) + save.click_input() + ui.collapse() + wait_for(lambda: output.is_file() and output.stat().st_size > 4096, 60) + status = request.with_name('bom-export-status.json') + wait_for(lambda: status.is_file(), 10) + excel = win32com.client.DispatchEx('Excel.Application') + excel.Visible = False + excel.DisplayAlerts = False + workbook = None + try: + workbook = excel.Workbooks.Open(str(output), ReadOnly=True) + values = workbook.Worksheets(1).UsedRange.Value + text = '\n'.join(str(value) for row in values for value in row if value is not None) + assert 'CE17' in text and 'LE03' in text, text + assert len(values) > 10 + results.append({'page': 'BOM 工具', 'output': str(output), 'used_rows': len(values), + 'real_component_refdes_verified': True, 'completed_while_collapsed': True, + 'native_status': json.loads(status.read_text(encoding='utf-8-sig'))}) + finally: + if workbook is not None: + workbook.Close(SaveChanges=False) + excel.Quit() + (base / 'native-read-tools.json').write_text(json.dumps({'pid': tool.pid, + 'project': str(project), 'cases': results, 'design_values_written': False}, ensure_ascii=False, indent=2), encoding='utf-8') + print('PASS NATIVE BOM WORKBOOK', str(output), flush=True) + + +if __name__ == '__main__': + main() diff --git a/tools/acceptance/pdf_toolbar.py b/tools/acceptance/pdf_toolbar.py new file mode 100644 index 0000000..f06d30f --- /dev/null +++ b/tools/acceptance/pdf_toolbar.py @@ -0,0 +1,79 @@ +"""Export all three PDF ranges through the real toolbar on an acceptance copy.""" +import argparse +import json +from pathlib import Path +import shlex +import time + +from pypdf import PdfReader +from packaged_menu import tools_running, wait_for +from pdf_palette import color_operations +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() + if 'acceptance' not in project.parts: + raise SystemExit('PDF acceptance requires an explicit acceptance project') + 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('"')) + snapshot = json.loads(request.read_text(encoding='utf-8-sig')) + assert Path(snapshot['design_path']).resolve() == project + ui = ToolbarUI(tool.pid) + if ui.panel.exists(timeout=0.2) and ui.panel.is_visible(): + ui.collapse() + ui.feature('PDF 导出') + scope = ui.panel.child_window(title='导出范围', control_type='ComboBox') + directory = ui.panel.child_window(title='PDF 输出目录', control_type='Edit') + results = [] + for index, name in enumerate(('project', 'page', 'block')): + output = base / name + if output.exists(): + raise AssertionError(f'Retain previous evidence and choose a fresh directory: {output}') + scope.select(index) + if name == 'block': + ui.panel.child_window(title='指定 Block', control_type='Edit').set_edit_text(snapshot['active_block_name']) + directory.set_edit_text(str(output)) + ui.panel.child_window(title='导出 PDF', control_type='Button').invoke() + began = time.monotonic() + ui.collapse() + def published(): + candidates = tuple(output.glob('*.pdf')) + if not candidates: + return None + assert len(candidates) == 1, candidates + try: + pdf = PdfReader(candidates[0]) + return candidates[0], pdf if len(pdf.pages) else None + except (OSError, ValueError): + return None + path, pdf = wait_for(published, 90) + assert len(pdf.pages) == 1 + text = '\n'.join(page.extract_text() for page in pdf.pages) + for expected in ('输入网络', '输出网络', '靠近CPU放置', '电源输入3V3'): + assert expected in text, (name, expected) + colors = color_operations(path) + assert colors[(1., 0., 0.)] > 0 and colors[(0., 0., 1.)] > 0, colors + ui.expand() + wait_for(lambda: ui.panel.child_window(title='导出 PDF', control_type='Button').is_enabled(), 10) + result = {'scope': name, 'pdf': str(path), 'pages': len(pdf.pages), 'chinese_text_verified': True, + 'reference_red_blue_verified': True, 'export_finished_while_collapsed': True, + 'elapsed_seconds': round(time.monotonic() - began, 2)} + results.append(result) + ui.panel.capture_as_image().save(str(base / f'{name}-page.png')) + print('PASS TOOLBAR PDF', json.dumps(result, ensure_ascii=False), flush=True) + ui.panel.child_window(title='打开输出目录', control_type='Button').invoke() + (base / 'pdf-toolbar-acceptance.json').write_text(json.dumps({'pid': tool.pid, + 'project': str(project), 'cases': results, 'output_directory_button_invoked': True}, ensure_ascii=False, indent=2), encoding='utf-8') + + +if __name__ == '__main__': + main()