askrobots / dbbasic-web-native
Public
100+ copy-paste UI components in plain HTML and CSS: no dependencies, no build step.
danq9
Add canonical.html v1.0 - The single source of truth for usability standards
fb6e650
1 year ago
History
test_gestures.py · 11.8 KB · 319 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Gesture and touch interaction tests. |
| 4 | Tests touch gestures, swipes, long-press, and multi-touch interactions. |
| 5 | Cost: FREE (Playwright touch simulation) |
| 6 | Run: On PR |
| 7 | """ |
| 8 | |
| 9 | from playwright.sync_api import sync_playwright |
| 10 | from pathlib import Path |
| 11 | import json |
| 12 | import time |
| 13 | |
| 14 | class GestureTester: |
| 15 | def __init__(self): |
| 16 | self.results = [] |
| 17 | |
| 18 | def test_swipe_list(self, page): |
| 19 | """Test swipe gestures on list items""" |
| 20 | issues = [] |
| 21 | |
| 22 | page.set_content(""" |
| 23 | <!DOCTYPE html> |
| 24 | <html> |
| 25 | <head><script src="file:///Users/danq/dbbasic-web-native/semantic-components.js"></script></head> |
| 26 | <body> |
| 27 | <semantic-list id="swipeable-list"> |
| 28 | <div data-item-id="1">Swipe me</div> |
| 29 | <div data-item-id="2">Swipe me too</div> |
| 30 | <div data-item-id="3">Another item</div> |
| 31 | </semantic-list> |
| 32 | </body> |
| 33 | </html> |
| 34 | """) |
| 35 | page.wait_for_load_state('networkidle') |
| 36 | |
| 37 | # Simulate swipe gesture |
| 38 | first_item = page.locator('[data-item-id="1"]') |
| 39 | if first_item.count() > 0: |
| 40 | box = first_item.bounding_box() |
| 41 | if box: |
| 42 | # Touch start, move, end |
| 43 | page.mouse.move(box['x'] + 10, box['y'] + box['height'] / 2) |
| 44 | page.mouse.down() |
| 45 | page.mouse.move(box['x'] + 100, box['y'] + box['height'] / 2, steps=10) |
| 46 | page.mouse.up() |
| 47 | |
| 48 | # Check if swipe triggered any visual feedback |
| 49 | # Note: This is a basic test - real implementation would check for transform/animation |
| 50 | transform = first_item.evaluate('el => window.getComputedStyle(el).transform') |
| 51 | if transform == 'none': |
| 52 | issues.append({ |
| 53 | 'type': 'no_swipe_feedback', |
| 54 | 'severity': 'info', |
| 55 | 'message': 'No visual feedback detected for swipe gesture' |
| 56 | }) |
| 57 | |
| 58 | return {'test': 'swipe_list', 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, 'issues': issues} |
| 59 | |
| 60 | def test_long_press(self, page): |
| 61 | """Test long-press gesture on action buttons""" |
| 62 | issues = [] |
| 63 | |
| 64 | page.set_content(""" |
| 65 | <!DOCTYPE html> |
| 66 | <html> |
| 67 | <head><script src="file:///Users/danq/dbbasic-web-native/semantic-components.js"></script></head> |
| 68 | <body> |
| 69 | <semantic-action id="long-press-btn" intent="primary">Long Press Me</semantic-action> |
| 70 | </body> |
| 71 | </html> |
| 72 | """) |
| 73 | page.wait_for_load_state('networkidle') |
| 74 | |
| 75 | button = page.locator('semantic-action') |
| 76 | if button.count() > 0: |
| 77 | box = button.bounding_box() |
| 78 | if box: |
| 79 | # Simulate long press (touch down, wait, touch up) |
| 80 | page.mouse.move(box['x'] + box['width'] / 2, box['y'] + box['height'] / 2) |
| 81 | page.mouse.down() |
| 82 | time.sleep(0.6) # 600ms long press |
| 83 | page.mouse.up() |
| 84 | |
| 85 | # Check for long-press handling |
| 86 | # In production, would check for context menu or special state |
| 87 | issues.append({ |
| 88 | 'type': 'long_press_tested', |
| 89 | 'severity': 'info', |
| 90 | 'message': 'Long press gesture tested' |
| 91 | }) |
| 92 | |
| 93 | return {'test': 'long_press', 'passed': True, 'issues': issues} |
| 94 | |
| 95 | def test_touch_target_spacing(self, page): |
| 96 | """Test spacing between touch targets (minimum 8px)""" |
| 97 | issues = [] |
| 98 | |
| 99 | page.set_content(""" |
| 100 | <!DOCTYPE html> |
| 101 | <html> |
| 102 | <head><script src="file:///Users/danq/dbbasic-web-native/semantic-components.js"></script></head> |
| 103 | <body> |
| 104 | <div style="display: flex; gap: 4px;"> |
| 105 | <semantic-action intent="primary">Button 1</semantic-action> |
| 106 | <semantic-action intent="secondary">Button 2</semantic-action> |
| 107 | <semantic-action intent="destructive">Button 3</semantic-action> |
| 108 | </div> |
| 109 | </body> |
| 110 | </html> |
| 111 | """) |
| 112 | page.wait_for_load_state('networkidle') |
| 113 | |
| 114 | buttons = page.locator('semantic-action').all() |
| 115 | for i in range(len(buttons) - 1): |
| 116 | box1 = buttons[i].bounding_box() |
| 117 | box2 = buttons[i + 1].bounding_box() |
| 118 | |
| 119 | if box1 and box2: |
| 120 | gap = box2['x'] - (box1['x'] + box1['width']) |
| 121 | if gap < 8: |
| 122 | issues.append({ |
| 123 | 'type': 'insufficient_touch_spacing', |
| 124 | 'gap': f"{gap}px", |
| 125 | 'minimum': '8px', |
| 126 | 'severity': 'warning', |
| 127 | 'message': f'Touch targets only {gap}px apart, recommend 8px minimum' |
| 128 | }) |
| 129 | |
| 130 | return {'test': 'touch_target_spacing', 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, 'issues': issues} |
| 131 | |
| 132 | def test_tap_responsiveness(self, page): |
| 133 | """Test tap response time and visual feedback""" |
| 134 | issues = [] |
| 135 | |
| 136 | page.set_content(""" |
| 137 | <!DOCTYPE html> |
| 138 | <html> |
| 139 | <head><script src="file:///Users/danq/dbbasic-web-native/semantic-components.js"></script></head> |
| 140 | <body> |
| 141 | <semantic-action id="tap-test" intent="primary">Tap Me</semantic-action> |
| 142 | <script> |
| 143 | const btn = document.getElementById('tap-test'); |
| 144 | btn.addEventListener('click', () => { |
| 145 | btn.setAttribute('data-clicked', 'true'); |
| 146 | }); |
| 147 | </script> |
| 148 | </body> |
| 149 | </html> |
| 150 | """) |
| 151 | page.wait_for_load_state('networkidle') |
| 152 | |
| 153 | button = page.locator('semantic-action') |
| 154 | |
| 155 | # Record start time |
| 156 | start_time = time.time() |
| 157 | button.click() |
| 158 | |
| 159 | # Check if click was registered |
| 160 | clicked = button.get_attribute('data-clicked') |
| 161 | response_time = (time.time() - start_time) * 1000 # Convert to ms |
| 162 | |
| 163 | if not clicked: |
| 164 | issues.append({ |
| 165 | 'type': 'tap_not_registered', |
| 166 | 'severity': 'error', |
| 167 | 'message': 'Tap event not registered' |
| 168 | }) |
| 169 | |
| 170 | if response_time > 100: # 100ms is acceptable limit |
| 171 | issues.append({ |
| 172 | 'type': 'slow_tap_response', |
| 173 | 'response_time': f"{response_time:.0f}ms", |
| 174 | 'severity': 'warning', |
| 175 | 'message': f'Tap response took {response_time:.0f}ms (>100ms)' |
| 176 | }) |
| 177 | |
| 178 | return {'test': 'tap_responsiveness', 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, 'issues': issues} |
| 179 | |
| 180 | def test_gesture_conflicts(self, page): |
| 181 | """Test for gesture conflicts in nested components""" |
| 182 | issues = [] |
| 183 | |
| 184 | page.set_content(""" |
| 185 | <!DOCTYPE html> |
| 186 | <html> |
| 187 | <head><script src="file:///Users/danq/dbbasic-web-native/semantic-components.js"></script></head> |
| 188 | <body> |
| 189 | <semantic-modal id="modal-with-list" open> |
| 190 | <div slot="header">Modal with scrollable list</div> |
| 191 | <semantic-list style="height: 200px; overflow-y: auto;"> |
| 192 | <div data-item-id="1">Item 1</div> |
| 193 | <div data-item-id="2">Item 2</div> |
| 194 | <div data-item-id="3">Item 3</div> |
| 195 | <div data-item-id="4">Item 4</div> |
| 196 | <div data-item-id="5">Item 5</div> |
| 197 | </semantic-list> |
| 198 | </semantic-modal> |
| 199 | </body> |
| 200 | </html> |
| 201 | """) |
| 202 | page.wait_for_load_state('networkidle') |
| 203 | |
| 204 | # Test that scroll gestures work inside modal without closing it |
| 205 | list_element = page.locator('semantic-list') |
| 206 | if list_element.count() > 0: |
| 207 | box = list_element.bounding_box() |
| 208 | if box: |
| 209 | # Simulate scroll gesture |
| 210 | page.mouse.move(box['x'] + box['width'] / 2, box['y'] + 10) |
| 211 | page.mouse.down() |
| 212 | page.mouse.move(box['x'] + box['width'] / 2, box['y'] + 100, steps=10) |
| 213 | page.mouse.up() |
| 214 | |
| 215 | # Check modal is still open |
| 216 | modal = page.locator('semantic-modal') |
| 217 | is_open = modal.get_attribute('open') |
| 218 | if not is_open: |
| 219 | issues.append({ |
| 220 | 'type': 'gesture_conflict', |
| 221 | 'severity': 'error', |
| 222 | 'message': 'Scroll gesture closed parent modal (gesture conflict)' |
| 223 | }) |
| 224 | |
| 225 | return {'test': 'gesture_conflicts', 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, 'issues': issues} |
| 226 | |
| 227 | def test_pinch_zoom_disabled(self, page): |
| 228 | """Test that pinch zoom is appropriately handled""" |
| 229 | issues = [] |
| 230 | |
| 231 | page.set_content(""" |
| 232 | <!DOCTYPE html> |
| 233 | <html> |
| 234 | <head> |
| 235 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 236 | <script src="file:///Users/danq/dbbasic-web-native/semantic-components.js"></script> |
| 237 | </head> |
| 238 | <body> |
| 239 | <semantic-card> |
| 240 | <h3 slot="header">Pinch Test Card</h3> |
| 241 | <p>This content should allow zoom unless explicitly disabled</p> |
| 242 | </semantic-card> |
| 243 | </body> |
| 244 | </html> |
| 245 | """) |
| 246 | page.wait_for_load_state('networkidle') |
| 247 | |
| 248 | # Check viewport meta tag |
| 249 | viewport = page.locator('meta[name="viewport"]').get_attribute('content') |
| 250 | if viewport and 'user-scalable=no' in viewport: |
| 251 | issues.append({ |
| 252 | 'type': 'zoom_disabled', |
| 253 | 'severity': 'warning', |
| 254 | 'message': 'Pinch zoom is disabled (user-scalable=no). Consider allowing zoom for accessibility.' |
| 255 | }) |
| 256 | |
| 257 | return {'test': 'pinch_zoom_disabled', 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, 'issues': issues} |
| 258 | |
| 259 | def run_all_tests(self): |
| 260 | """Run all gesture tests""" |
| 261 | print("\n👆 Running gesture tests...\n") |
| 262 | |
| 263 | with sync_playwright() as p: |
| 264 | browser = p.chromium.launch(headless=True) |
| 265 | # Use mobile viewport for gesture testing |
| 266 | page = browser.new_page(viewport={'width': 375, 'height': 667}, has_touch=True) |
| 267 | |
| 268 | tests = [ |
| 269 | self.test_swipe_list, |
| 270 | self.test_long_press, |
| 271 | self.test_touch_target_spacing, |
| 272 | self.test_tap_responsiveness, |
| 273 | self.test_gesture_conflicts, |
| 274 | self.test_pinch_zoom_disabled |
| 275 | ] |
| 276 | |
| 277 | for test in tests: |
| 278 | test_name = test.__doc__.strip() |
| 279 | print(f"🔍 {test_name}...", end='') |
| 280 | result = test(page) |
| 281 | self.results.append(result) |
| 282 | |
| 283 | error_count = len([i for i in result['issues'] if i['severity'] == 'error']) |
| 284 | warning_count = len([i for i in result['issues'] if i['severity'] == 'warning']) |
| 285 | |
| 286 | if result['passed']: |
| 287 | if warning_count > 0: |
| 288 | print(f" ⚠️ ({warning_count} warnings)") |
| 289 | else: |
| 290 | print(" ✅") |
| 291 | else: |
| 292 | print(f" ❌ ({error_count} errors)") |
| 293 | |
| 294 | browser.close() |
| 295 | |
| 296 | self.generate_report() |
| 297 | |
| 298 | def generate_report(self): |
| 299 | """Generate test report""" |
| 300 | passed = sum(1 for r in self.results if r['passed']) |
| 301 | total = len(self.results) |
| 302 | |
| 303 | # Save JSON |
| 304 | results_path = Path(__file__).parent / 'test-results' / 'results-gestures.json' |
| 305 | results_path.parent.mkdir(exist_ok=True) |
| 306 | with open(results_path, 'w') as f: |
| 307 | json.dump(self.results, f, indent=2) |
| 308 | |
| 309 | print(f"\n📊 Gesture tests complete!") |
| 310 | print(f" Passed: {passed}/{total}") |
| 311 | print(f" Results: {results_path}") |
| 312 | |
| 313 | # Exit with error if any tests failed |
| 314 | if passed < total: |
| 315 | exit(1) |
| 316 | |
| 317 | if __name__ == '__main__': |
| 318 | tester = GestureTester() |
| 319 | tester.run_all_tests() |