pyWebLayout/tests/test_enhanced_page.py

156 lines
4.3 KiB
Python

#!/usr/bin/env python3
"""
Test the enhanced Page class with HTML loading capabilities
"""
from pyWebLayout.concrete.page import Page
from pyWebLayout.style.fonts import Font
from PIL import Image
import tempfile
import os
def test_page_html_loading():
"""Test loading HTML content into a Page"""
# Create a test HTML content
html_content = """
<html>
<head><title>Test Page</title></head>
<body>
<h1>Welcome to pyWebLayout</h1>
<p>This is a <strong>test paragraph</strong> with <em>some formatting</em>.</p>
<h2>Features</h2>
<ul>
<li>HTML parsing</li>
<li>Text rendering</li>
<li>Basic styling</li>
</ul>
<p>Another paragraph with different content.</p>
</body>
</html>
"""
# Create a page and load the HTML
page = Page(size=(800, 600))
page.load_html_string(html_content)
# Render the page
try:
image = page.render()
print(f"✓ Successfully rendered page: {image.size}")
# Save the rendered image for inspection
output_path = "test_page_output.png"
image.save(output_path)
print(f"✓ Saved rendered page to: {output_path}")
return True
except Exception as e:
print(f"✗ Error rendering page: {e}")
return False
def test_page_html_file_loading():
"""Test loading HTML from a file"""
# Create a temporary HTML file
html_content = """
<!DOCTYPE html>
<html>
<head><title>File Test</title></head>
<body>
<h1>Loading from File</h1>
<p>This content was loaded from a file.</p>
<h2>Styled Content</h2>
<p>Text with <strong>bold</strong> and <em>italic</em> formatting.</p>
</body>
</html>
"""
# Write to temporary file
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as f:
f.write(html_content)
temp_file = f.name
try:
# Create a page and load the file
page = Page(size=(800, 600))
page.load_html_file(temp_file)
# Render the page
image = page.render()
print(f"✓ Successfully loaded and rendered HTML file: {image.size}")
# Save the rendered image
output_path = "test_file_page_output.png"
image.save(output_path)
print(f"✓ Saved file-loaded page to: {output_path}")
return True
except Exception as e:
print(f"✗ Error loading HTML file: {e}")
return False
finally:
# Clean up temporary file
try:
os.unlink(temp_file)
except OSError:
pass
def test_epub_reader_imports():
"""Test that the EPUB reader can be imported without errors"""
try:
from epub_reader_tk import EPUBReaderApp
print("✓ Successfully imported EPUBReaderApp")
# Test creating the app (but don't show it)
app = EPUBReaderApp()
print("✓ Successfully created EPUBReaderApp instance")
return True
except Exception as e:
print(f"✗ Error importing/creating EPUB reader: {e}")
return False
def main():
"""Run all tests"""
print("Testing enhanced Page class and EPUB reader...")
print("=" * 50)
tests = [
("HTML String Loading", test_page_html_loading),
("HTML File Loading", test_page_html_file_loading),
("EPUB Reader Imports", test_epub_reader_imports),
]
results = []
for test_name, test_func in tests:
print(f"\nTesting: {test_name}")
print("-" * 30)
success = test_func()
results.append((test_name, success))
# Summary
print("\n" + "=" * 50)
print("Test Summary:")
for test_name, success in results:
status = "PASS" if success else "FAIL"
print(f" {test_name}: {status}")
total_tests = len(results)
passed_tests = sum(1 for _, success in results if success)
print(f"\nPassed: {passed_tests}/{total_tests}")
if passed_tests == total_tests:
print("🎉 All tests passed!")
else:
print(f"⚠️ {total_tests - passed_tests} test(s) failed")
if __name__ == "__main__":
main()