import sys; sys.path.insert(0, '.') #!/usr/bin/env python3 """Test for UICollection - Related to issue #69 (Sequence Protocol)""" import mcrfpy from mcrfpy import automation from datetime import datetime def test_UICollection(): """Test UICollection sequence protocol compliance""" # Create test scene mcrfpy.createScene("collection_test") mcrfpy.setScene("collection_test") ui = mcrfpy.sceneUI("collection_test") # Add various UI elements frame = mcrfpy.Frame(10, 10, 100, 100) caption = mcrfpy.Caption(mcrfpy.Vector(120, 10), text="Test") sprite = mcrfpy.Sprite(10, 120) ui.append(frame) ui.append(caption) ui.append(sprite) print("Testing UICollection sequence protocol (Issue #69)...") # Test len() try: length = len(ui) print(f"✓ len() works: {length} items") except Exception as e: print(f"✗ len() failed: {e}") # Test indexing try: item0 = ui[0] item1 = ui[1] item2 = ui[2] print(f"✓ Indexing works: [{type(item0).__name__}, {type(item1).__name__}, {type(item2).__name__}]") # Test negative indexing last_item = ui[-1] print(f"✓ Negative indexing works: ui[-1] = {type(last_item).__name__}") except Exception as e: print(f"✗ Indexing failed: {e}") # Test slicing (if implemented) try: slice_items = ui[0:2] print(f"✓ Slicing works: got {len(slice_items)} items") except Exception as e: print(f"✗ Slicing not implemented (Issue #69): {e}") # Test iteration try: types = [] for item in ui: types.append(type(item).__name__) print(f"✓ Iteration works: {types}") except Exception as e: print(f"✗ Iteration failed: {e}") # Test contains try: if frame in ui: print("✓ 'in' operator works") else: print("✗ 'in' operator returned False for existing item") except Exception as e: print(f"✗ 'in' operator not implemented (Issue #69): {e}") # Test remove try: ui.remove(1) # Remove caption print(f"✓ remove() works, now {len(ui)} items") except Exception as e: print(f"✗ remove() failed: {e}") # Test type preservation (Issue #76) try: # Add a frame with children to test nested collections parent_frame = mcrfpy.Frame(250, 10, 200, 200, fill_color=mcrfpy.Color(200, 200, 200)) child_caption = mcrfpy.Caption(mcrfpy.Vector(10, 10), text="Child") parent_frame.children.append(child_caption) ui.append(parent_frame) # Check if type is preserved when retrieving retrieved = ui[-1] if type(retrieved).__name__ == "Frame": print("✓ Type preservation works") else: print(f"✗ Type not preserved (Issue #76): got {type(retrieved).__name__}") except Exception as e: print(f"✗ Type preservation test failed: {e}") # Test find by name (Issue #41 - not yet implemented) try: found = ui.find("Test") print(f"✓ find() method works: {type(found).__name__}") except AttributeError: print("✗ find() method not implemented (Issue #41)") except Exception as e: print(f"✗ find() method error: {e}") # Take screenshot timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") filename = f"test_UICollection_issue69_{timestamp}.png" automation.screenshot(filename) print(f"Screenshot saved: {filename}") print("PASS") # Set up timer to run test mcrfpy.setTimer("test", test_UICollection, 1000) # Cancel timer after running once def cleanup(): mcrfpy.delTimer("test") mcrfpy.delTimer("cleanup") mcrfpy.setTimer("cleanup", cleanup, 1100)