Files
signage-player/test_centering.py
2025-08-06 02:27:50 +03:00

144 lines
4.6 KiB
Python

#!/usr/bin/env python3
"""
Test script to verify window centering functionality
"""
import tkinter as tk
import sys
import os
sys.path.append('tkinter_app/src')
from tkinter_simple_player import SettingsWindow, SimpleMediaPlayerApp
def test_settings_centering():
"""Test settings window centering"""
root = tk.Tk()
root.withdraw() # Hide main window
# Create a mock app object
class MockApp:
def __init__(self):
self.playlist = []
self.current_index = 0
def play_current_media(self):
print('play_current_media called')
app = MockApp()
# Test settings window centering
try:
print("Testing settings window centering...")
settings = SettingsWindow(root, app)
# Get screen dimensions
screen_width = settings.window.winfo_screenwidth()
screen_height = settings.window.winfo_screenheight()
# Get window position
settings.window.update_idletasks()
window_x = settings.window.winfo_x()
window_y = settings.window.winfo_y()
window_width = 900
window_height = 700
# Calculate expected center position
expected_x = (screen_width - window_width) // 2
expected_y = (screen_height - window_height) // 2
print(f"Screen size: {screen_width}x{screen_height}")
print(f"Window position: {window_x}, {window_y}")
print(f"Expected center: {expected_x}, {expected_y}")
print(f"Window size: {window_width}x{window_height}")
# Check if window is roughly centered (allow some margin for window decorations)
margin = 50
is_centered_x = abs(window_x - expected_x) <= margin
is_centered_y = abs(window_y - expected_y) <= margin
if is_centered_x and is_centered_y:
print("✅ Settings window is properly centered!")
else:
print("❌ Settings window centering needs adjustment")
# Keep window open for 3 seconds to visually verify
root.after(3000, root.quit)
root.mainloop()
except Exception as e:
print(f"❌ Error testing settings window: {e}")
def test_exit_dialog_centering():
"""Test exit dialog centering"""
print("\nTesting exit dialog centering...")
# Create a simple test for the centering function
root = tk.Tk()
root.withdraw()
# Create a test dialog
dialog = tk.Toplevel(root)
dialog.title("Test Exit Dialog")
dialog.geometry("400x200")
dialog.configure(bg='#2d2d2d')
dialog.resizable(False, False)
# Test the centering logic
dialog.update_idletasks()
screen_width = dialog.winfo_screenwidth()
screen_height = dialog.winfo_screenheight()
dialog_width = 400
dialog_height = 200
# Calculate center position
center_x = int((screen_width - dialog_width) / 2)
center_y = int((screen_height - dialog_height) / 2)
# Ensure the dialog doesn't go off-screen
center_x = max(0, min(center_x, screen_width - dialog_width))
center_y = max(0, min(center_y, screen_height - dialog_height))
dialog.geometry(f"{dialog_width}x{dialog_height}+{center_x}+{center_y}")
dialog.lift()
# Add test content
tk.Label(dialog, text="🎬 Test Exit Dialog",
font=('Arial', 16, 'bold'),
fg='white', bg='#2d2d2d').pack(pady=20)
tk.Label(dialog, text="This dialog should be centered on screen",
font=('Arial', 12),
fg='white', bg='#2d2d2d').pack(pady=10)
# Get actual position
dialog.update_idletasks()
actual_x = dialog.winfo_x()
actual_y = dialog.winfo_y()
print(f"Screen size: {screen_width}x{screen_height}")
print(f"Dialog position: {actual_x}, {actual_y}")
print(f"Expected center: {center_x}, {center_y}")
# Check centering
margin = 50
is_centered_x = abs(actual_x - center_x) <= margin
is_centered_y = abs(actual_y - center_y) <= margin
if is_centered_x and is_centered_y:
print("✅ Exit dialog is properly centered!")
else:
print("❌ Exit dialog centering needs adjustment")
# Close dialog after 3 seconds
root.after(3000, root.quit)
root.mainloop()
if __name__ == "__main__":
print("🧪 Testing Window Centering Functionality")
print("=" * 50)
test_settings_centering()
test_exit_dialog_centering()
print("\n✅ Centering tests completed!")
print("\nThe windows should appear centered on your screen regardless of resolution.")
print("This works for any screen size: 1024x768, 1920x1080, 4K, etc.")