Files
MicroPythonOS/tests/test_graphical_keyboard_q_button_bug.py
T

122 lines
4.5 KiB
Python
Raw Normal View History

2025-11-18 15:40:01 +01:00
"""
2025-11-18 15:46:38 +01:00
Test for keyboard button functionality (originally created to fix "q" button bug).
2025-11-18 15:40:01 +01:00
2025-11-18 15:46:38 +01:00
This test verifies that all keyboard buttons work correctly, including the
'q' button which was previously broken due to button index 0 being treated
as False in Python's truthiness check.
2025-11-18 15:40:01 +01:00
2025-11-18 15:46:38 +01:00
The bug was: `if not button:` would return True when button index was 0,
causing the 'q' key to be ignored. Fixed by changing to `if button is None:`.
2025-11-18 15:40:01 +01:00
Usage:
Desktop: ./tests/unittest.sh tests/test_graphical_keyboard_q_button_bug.py
Device: ./tests/unittest.sh tests/test_graphical_keyboard_q_button_bug.py --ondevice
"""
import unittest
2025-12-19 11:01:09 +01:00
from base import KeyboardTestBase
2025-11-18 15:40:01 +01:00
2025-12-19 11:01:09 +01:00
class TestKeyboardQButton(KeyboardTestBase):
2025-11-18 15:46:38 +01:00
"""Test keyboard button functionality (especially 'q' which was at index 0)."""
2025-11-18 15:40:01 +01:00
2025-11-18 15:46:38 +01:00
def test_q_button_works(self):
2025-11-18 15:40:01 +01:00
"""
Test that clicking the 'q' button adds 'q' to textarea.
2025-11-18 15:46:38 +01:00
This test verifies the fix for the bug where:
- Bug: Button index 0 ('q') was treated as False in `if not button:`
- Fix: Changed to `if button is None:` to properly handle index 0
2025-11-18 15:40:01 +01:00
Steps:
1. Create textarea and keyboard
2025-12-19 11:01:09 +01:00
2. Click 'q' button using click_keyboard_button helper
3. Verify 'q' appears in textarea (should PASS after fix)
4. Repeat with 'a' button
5. Verify 'a' appears correctly (should PASS)
2025-11-18 15:40:01 +01:00
"""
print("\n=== Testing keyboard 'q' and 'a' button behavior ===")
2025-12-19 11:01:09 +01:00
# Create keyboard scene (textarea + keyboard)
self.create_keyboard_scene()
2025-11-18 15:40:01 +01:00
2025-12-19 11:01:09 +01:00
print(f"Initial textarea: '{self.get_textarea_text()}'")
self.assertTextareaEmpty("Textarea should start empty")
2025-11-18 15:40:01 +01:00
# --- Test 'q' button ---
print("\n--- Testing 'q' button ---")
2025-12-19 11:01:09 +01:00
# Click the 'q' button using the reliable click_keyboard_button helper
success = self.click_keyboard_button("q")
self.assertTrue(success, "Should find and click 'q' button on keyboard")
2025-11-18 15:40:01 +01:00
# Check textarea content
2025-12-19 11:01:09 +01:00
text_after_q = self.get_textarea_text()
2025-11-18 15:40:01 +01:00
print(f"Textarea after clicking 'q': '{text_after_q}'")
2025-11-18 15:46:38 +01:00
# Verify 'q' was added (should work after fix)
2025-12-19 11:01:09 +01:00
self.assertTextareaText("q", "Clicking 'q' button should add 'q' to textarea")
2025-11-18 15:40:01 +01:00
# --- Test 'a' button for comparison ---
print("\n--- Testing 'a' button (for comparison) ---")
# Clear textarea
2025-12-19 11:01:09 +01:00
self.clear_textarea()
2025-11-18 15:40:01 +01:00
print("Cleared textarea")
2025-12-19 11:01:09 +01:00
# Click the 'a' button using the reliable click_keyboard_button helper
success = self.click_keyboard_button("a")
self.assertTrue(success, "Should find and click 'a' button on keyboard")
2025-11-18 15:40:01 +01:00
# Check textarea content
2025-12-19 11:01:09 +01:00
text_after_a = self.get_textarea_text()
2025-11-18 15:40:01 +01:00
print(f"Textarea after clicking 'a': '{text_after_a}'")
# The 'a' button should work correctly
2025-12-19 11:01:09 +01:00
self.assertTextareaText("a", "Clicking 'a' button should add 'a' to textarea")
2025-11-18 15:40:01 +01:00
print("\nSummary:")
2025-11-18 15:46:38 +01:00
print(f" 'q' button result: '{text_after_q}' (expected 'q') ✓")
print(f" 'a' button result: '{text_after_a}' (expected 'a') ✓")
print(" Both buttons work correctly!")
2025-11-18 15:40:01 +01:00
def test_keyboard_button_discovery(self):
"""
Debug test: Discover all buttons on the keyboard.
This test helps understand the keyboard layout and button structure.
It prints all found buttons and their text.
"""
print("\n=== Discovering keyboard buttons ===")
2025-12-19 11:01:09 +01:00
# Create keyboard scene
self.create_keyboard_scene()
2025-11-18 15:40:01 +01:00
2025-12-19 11:01:09 +01:00
# Get all buttons using the base class helper
found_buttons = self.get_all_keyboard_buttons()
# Print first 20 buttons
2025-11-18 15:40:01 +01:00
print("\nEnumerating keyboard buttons by index:")
2025-12-19 11:01:09 +01:00
for idx, text in found_buttons[:20]:
print(f" Button {idx}: '{text}'")
2025-11-18 15:40:01 +01:00
if len(found_buttons) > 20:
print(f" ... (showing first 20 of {len(found_buttons)} buttons)")
print(f"\nTotal buttons found: {len(found_buttons)}")
# Try to find specific letters
letters_to_test = ['q', 'w', 'e', 'r', 'a', 's', 'd', 'f']
print("\nLooking for specific letters:")
for letter in letters_to_test:
2025-12-19 11:01:09 +01:00
idx = self.find_keyboard_button_index(letter)
if idx is not None:
print(f" '{letter}' at index {idx}")
else:
2025-11-18 15:40:01 +01:00
print(f" '{letter}' NOT FOUND")
# Verify we can find at least some buttons
self.assertTrue(len(found_buttons) > 0,
"Should find at least some buttons on keyboard")