241 lines
7.2 KiB
Python
241 lines
7.2 KiB
Python
"""
|
|
Test suite for PluginAPI and BasePlugin clipboard integration
|
|
|
|
Run with: python tests/test_plugin_api.py
|
|
"""
|
|
|
|
import sys
|
|
import tempfile
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# Add parent to path
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from core.plugin_api import PluginAPI
|
|
from core.base_plugin import BasePlugin
|
|
from core.clipboard import ClipboardManager, get_clipboard_manager
|
|
|
|
|
|
class MockPlugin(BasePlugin):
|
|
"""Mock plugin for testing."""
|
|
name = "mock_plugin"
|
|
description = "Mock plugin for testing"
|
|
version = "1.0.0"
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.started = False
|
|
self.stopped = False
|
|
|
|
def on_start(self):
|
|
self.started = True
|
|
|
|
def on_stop(self):
|
|
self.stopped = True
|
|
|
|
|
|
class TestPluginAPI:
|
|
"""Tests for PluginAPI functionality."""
|
|
|
|
def setup_method(self):
|
|
"""Setup for each test."""
|
|
ClipboardManager._instance = None
|
|
self.api = PluginAPI()
|
|
|
|
def teardown_method(self):
|
|
"""Cleanup after each test."""
|
|
ClipboardManager._instance = None
|
|
|
|
def test_register_clipboard_service(self):
|
|
"""Test registering clipboard service."""
|
|
manager = self.api.register_clipboard_service()
|
|
|
|
assert manager is not None
|
|
assert self.api.get_clipboard_manager() is manager
|
|
|
|
def test_load_plugin_injects_clipboard(self):
|
|
"""Test that loading a plugin injects clipboard manager."""
|
|
# Register clipboard first
|
|
self.api.register_clipboard_service()
|
|
|
|
# Load plugin
|
|
plugin = self.api.load_plugin(MockPlugin)
|
|
|
|
# Plugin should have clipboard manager
|
|
assert plugin._clipboard_manager is not None
|
|
assert plugin._clipboard_manager is self.api.get_clipboard_manager()
|
|
|
|
def test_copy_to_clipboard_via_plugin(self):
|
|
"""Test copying via plugin method."""
|
|
self.api.register_clipboard_service()
|
|
plugin = self.api.load_plugin(MockPlugin)
|
|
|
|
# Copy via plugin
|
|
result = plugin.copy_to_clipboard("Test via plugin")
|
|
assert result is True
|
|
|
|
# Verify via manager
|
|
assert self.api.get_clipboard_manager().paste() == "Test via plugin"
|
|
|
|
def test_paste_via_plugin(self):
|
|
"""Test pasting via plugin method."""
|
|
self.api.register_clipboard_service()
|
|
plugin = self.api.load_plugin(MockPlugin)
|
|
|
|
# Copy via manager
|
|
self.api.get_clipboard_manager().copy("Original text")
|
|
|
|
# Paste via plugin
|
|
result = plugin.paste_from_clipboard()
|
|
assert result == "Original text"
|
|
|
|
def test_get_history_via_plugin(self):
|
|
"""Test getting history via plugin method."""
|
|
self.api.register_clipboard_service()
|
|
plugin = self.api.load_plugin(MockPlugin)
|
|
|
|
# Add entries
|
|
plugin.copy_to_clipboard("Entry 1")
|
|
plugin.copy_to_clipboard("Entry 2")
|
|
|
|
# Get history via plugin
|
|
history = plugin.get_clipboard_history()
|
|
|
|
assert len(history) == 2
|
|
assert history[0]['content'] == "Entry 2"
|
|
assert history[1]['content'] == "Entry 1"
|
|
assert history[0]['source'] == "mock_plugin"
|
|
|
|
def test_start_stop_all(self):
|
|
"""Test starting and stopping all plugins."""
|
|
self.api.register_clipboard_service()
|
|
plugin = self.api.load_plugin(MockPlugin)
|
|
|
|
# Start all
|
|
self.api.start_all()
|
|
assert plugin.started is True
|
|
assert plugin.is_initialized() is True
|
|
|
|
# Stop all
|
|
self.api.stop_all()
|
|
assert plugin.stopped is True
|
|
assert plugin.is_initialized() is False
|
|
|
|
def test_get_plugin(self):
|
|
"""Test getting a plugin by name."""
|
|
self.api.register_clipboard_service()
|
|
self.api.load_plugin(MockPlugin)
|
|
|
|
found = self.api.get_plugin("mock_plugin")
|
|
assert found is not None
|
|
assert found.name == "mock_plugin"
|
|
|
|
not_found = self.api.get_plugin("nonexistent")
|
|
assert not_found is None
|
|
|
|
def test_get_plugin_info(self):
|
|
"""Test getting plugin information."""
|
|
self.api.register_clipboard_service()
|
|
self.api.load_plugin(MockPlugin)
|
|
|
|
info = self.api.get_plugin_info()
|
|
|
|
assert len(info) == 1
|
|
assert info[0]['name'] == "mock_plugin"
|
|
assert info[0]['version'] == "1.0.0"
|
|
|
|
|
|
def run_integration_test():
|
|
"""Run an integration test demonstrating all features."""
|
|
print("\n" + "="*60)
|
|
print("PLUGIN API CLIPBOARD INTEGRATION TEST")
|
|
print("="*60)
|
|
|
|
# Reset singleton
|
|
ClipboardManager._instance = None
|
|
|
|
# Create API
|
|
api = PluginAPI()
|
|
|
|
# Register clipboard service
|
|
print("\n1. Registering clipboard service...")
|
|
clipboard = api.register_clipboard_service()
|
|
print(f" ✓ Clipboard service registered")
|
|
print(f" - Available: {clipboard.is_available()}")
|
|
|
|
# Create a test plugin
|
|
print("\n2. Creating and loading test plugin...")
|
|
|
|
class TestPlugin(BasePlugin):
|
|
name = "integration_test_plugin"
|
|
description = "Integration test"
|
|
version = "1.0"
|
|
|
|
def on_start(self):
|
|
print(f" [{self.name}] Started")
|
|
|
|
def on_stop(self):
|
|
print(f" [{self.name}] Stopped")
|
|
|
|
def copy_coordinates(self, x, y):
|
|
"""Copy coordinates to clipboard."""
|
|
coords = f"{x}, {y}"
|
|
success = self.copy_to_clipboard(coords)
|
|
print(f" [{self.name}] Copied coordinates: {coords}")
|
|
return success
|
|
|
|
def read_user_paste(self):
|
|
"""Read pasted value from user."""
|
|
value = self.paste_from_clipboard()
|
|
print(f" [{self.name}] Read clipboard: '{value}'")
|
|
return value
|
|
|
|
def show_history(self):
|
|
"""Show clipboard history."""
|
|
history = self.get_clipboard_history(limit=5)
|
|
print(f" [{self.name}] Recent clipboard entries:")
|
|
for i, entry in enumerate(history, 1):
|
|
content = entry['content'][:30]
|
|
if len(entry['content']) > 30:
|
|
content += "..."
|
|
print(f" {i}. [{entry.get('source', '?')}] {content}")
|
|
|
|
plugin = api.load_plugin(TestPlugin)
|
|
print(f" ✓ Plugin loaded: {plugin.name}")
|
|
|
|
# Start plugins
|
|
print("\n3. Starting plugins...")
|
|
api.start_all()
|
|
|
|
# Test: Copy coordinates
|
|
print("\n4. Testing: Copy coordinates to clipboard...")
|
|
plugin.copy_coordinates(100, 200)
|
|
plugin.copy_coordinates(45.5231, -122.6765)
|
|
|
|
# Test: Read pasted values
|
|
print("\n5. Testing: Read pasted values...")
|
|
plugin.read_user_paste()
|
|
|
|
# Test: Access clipboard history
|
|
print("\n6. Testing: Access clipboard history...")
|
|
plugin.show_history()
|
|
|
|
# Check stats
|
|
print("\n7. Clipboard stats:")
|
|
stats = clipboard.get_stats()
|
|
for key, value in stats.items():
|
|
print(f" {key}: {value}")
|
|
|
|
# Stop plugins
|
|
print("\n8. Stopping plugins...")
|
|
api.stop_all()
|
|
|
|
print("\n" + "="*60)
|
|
print("INTEGRATION TEST COMPLETE")
|
|
print("="*60)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run_integration_test()
|