#!/usr/bin/env python # -*- coding: utf-8 -*- """ LLMClipboard 通知系统测试脚本 用于测试各种通知场景和错误处理机制 """ import sys import os import time import logging from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel from PyQt6.QtCore import Qt, QTimer # 添加项目根目录到路径 sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # 导入LLMClipboard模块 from llmclipboard.gui import MainWindow from llmclipboard.app import TextCaptureService # 配置日志 logging.basicConfig( level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handlers=[ logging.StreamHandler(), logging.FileHandler('notification_test.log', encoding='utf-8') ] ) logger = logging.getLogger('notification_test') class NotificationTestWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("LLMClipboard 通知测试") self.resize(500, 400) # 创建主窗口实例 self.main_window = MainWindow() # 创建服务实例 self.service = TextCaptureService() self.main_window.service = self.service self.service.gui = self.main_window # 创建测试界面 central_widget = QWidget() self.setCentralWidget(central_widget) layout = QVBoxLayout(central_widget) # 添加标题 title = QLabel("LLMClipboard 通知系统测试") title.setStyleSheet("font-size: 18px; font-weight: bold;") title.setAlignment(Qt.AlignmentFlag.AlignCenter) layout.addWidget(title) # 添加测试按钮 self.add_test_button(layout, "测试基本通知", self.test_basic_notification) self.add_test_button(layout, "测试文件保存通知", self.test_file_save_notification) self.add_test_button(layout, "测试图片保存通知", self.test_image_save_notification) self.add_test_button(layout, "测试多个通知队列", self.test_notification_queue) self.add_test_button(layout, "测试错误通知", self.test_error_notification) self.add_test_button(layout, "测试托盘图标通知", self.test_tray_notification) self.add_test_button(layout, "测试备用通知机制", self.test_fallback_notification) # 添加状态标签 self.status_label = QLabel("就绪") self.status_label.setStyleSheet("font-style: italic;") self.status_label.setAlignment(Qt.AlignmentFlag.AlignCenter) layout.addWidget(self.status_label) # 显示主窗口 self.main_window.show() def add_test_button(self, layout, text, callback): """添加测试按钮""" button = QPushButton(text) button.setMinimumHeight(40) button.clicked.connect(callback) layout.addWidget(button) def update_status(self, message): """更新状态标签""" self.status_label.setText(message) logger.info(message) # 5秒后重置状态 QTimer.singleShot(5000, lambda: self.status_label.setText("就绪")) def test_basic_notification(self): """测试基本通知""" self.update_status("正在测试基本通知...") self.main_window.show_notification( "测试通知", "这是一个基本的测试通知消息", "info" ) def test_file_save_notification(self): """测试文件保存通知""" self.update_status("正在测试文件保存通知...") # 创建临时内容 content = """# 测试文档 这是一个测试文档内容,用于测试文件保存通知功能。 ## 测试章节 * 项目1 * 项目2 * 项目3 ## 代码示例 ```python def hello_world(): print("Hello, World!") ``` """ # 保存内容 self.service.save_to_markdown(content) def test_image_save_notification(self): """测试图片保存通知""" self.update_status("正在测试图片保存通知...") try: # 创建测试图片 from PIL import Image, ImageDraw # 创建一个彩色测试图像 img = Image.new('RGB', (800, 600), color=(73, 109, 137)) d = ImageDraw.Draw(img) # 绘制一些图形 d.rectangle([(200, 100), (600, 500)], fill=(255, 255, 255), outline=(255, 0, 0)) d.ellipse([(250, 150), (550, 450)], fill=(0, 255, 0)) d.line([(200, 100), (600, 500)], fill=(0, 0, 255), width=5) # 保存图片 self.service.save_image(img) except Exception as e: logger.error(f"创建测试图片失败: {e}") self.update_status(f"创建测试图片失败: {e}") def test_notification_queue(self): """测试多个通知队列""" self.update_status("正在测试多个通知队列...") # 发送多个通知 for i in range(5): self.main_window.show_notification( f"测试通知 {i+1}", f"这是第 {i+1} 个测试通知消息,测试通知队列处理", "info" ) def test_error_notification(self): """测试错误通知""" self.update_status("正在测试错误通知...") self.main_window.show_notification( "错误通知测试", "这是一个错误通知测试消息", "error" ) def test_tray_notification(self): """测试托盘图标通知""" self.update_status("正在测试托盘图标通知...") # 确保托盘图标存在 if hasattr(self.main_window, 'tray_icon') and self.main_window.tray_icon is not None: self.main_window.show_tray_message( "托盘通知测试", "这是一个托盘图标通知测试消息", "info", 3000 ) else: self.update_status("托盘图标不可用,无法测试托盘通知") def test_fallback_notification(self): """测试备用通知机制""" self.update_status("正在测试备用通知机制...") # 临时禁用托盘图标,强制使用备用通知 if hasattr(self.main_window, 'tray_icon'): original_tray = self.main_window.tray_icon self.main_window.tray_icon = None # 显示通知 (将使用备用机制) self.main_window.show_notification( "备用通知测试", "这是一个备用通知机制测试消息", "info" ) # 恢复托盘图标 self.main_window.tray_icon = original_tray else: self.update_status("无法测试备用通知机制,托盘图标不存在") def main(): app = QApplication(sys.argv) window = NotificationTestWindow() window.show() sys.exit(app.exec()) if __name__ == "__main__": main()