verify_library.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """Temporary-map gesture regression checks."""
  2. import json
  3. import tempfile
  4. from pathlib import Path
  5. from verify_recovery import editor, pygame
  6. from game.frame import obstacle
  7. from game.frame.frame import Frame
  8. from game.frame.obstacle_instance import ObstacleInstance
  9. with tempfile.TemporaryDirectory() as tmp:
  10. target = Path(tmp) / 'map.json'
  11. target.write_text(json.dumps(dict(basic=dict(name='test', width=40, height=40,
  12. scroll=[0, 0], begin=[0, 0], finish=[39, 39]), floor=[], obstacle=[], districts=[])))
  13. editor.MAP_JSON_FILE_NAME = str(target)
  14. editor.MapBuilder.update = lambda self: None
  15. app = editor.MapBuilder()
  16. import time
  17. from asset_library import scan, decode
  18. lib = app.library
  19. def wait_until(predicate):
  20. deadline = time.monotonic()+30
  21. while not predicate():
  22. lib.update()
  23. if time.monotonic() > deadline:
  24. raise AssertionError('background load timeout')
  25. time.sleep(0.005)
  26. try:
  27. lib.toggle()
  28. lib.choose_library('common')
  29. lib.choose_library('crimson_manor')
  30. wait_until(lambda: lib.scan_job is None and not lib.pending and not lib.jobs and bool(lib.entries))
  31. assert all(e['type'] == 'crimson_manor' for e in lib.entries)
  32. assert any(e['elem_type'] == 'floor' for e in lib.entries)
  33. assert any(e['elem_type'] == 'obstacle' for e in lib.entries)
  34. assert all(e['thumbnail'].get_size() == (112, 88) for e in lib.entries)
  35. for kind in ('floor', 'obstacle'):
  36. entry = next(e for e in lib.entries if e['elem_type'] == kind)
  37. lib.selected_job = lib.pool.submit(decode, lib.root, entry, True)
  38. wait_until(lambda: lib.selected_job is None)
  39. assert app.current_elem['name'] == entry['name'] and len(app.selection.group) == 1
  40. app.menu.block_pointer = False
  41. pos = (editor.MAP_AT[0]+405, editor.MAP_AT[1]+405)
  42. assert app.placement.handle_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN, button=1, pos=pos))
  43. app.placement.handle_event(pygame.event.Event(pygame.MOUSEBUTTONUP, button=1, pos=pos))
  44. assert (10, 10) in app.map_obstacles_dict
  45. app.menu.undo()
  46. assert (10, 10) not in app.map_obstacles_dict
  47. lib.draw()
  48. app.menu.draw()
  49. app.settings.draw_speaker()
  50. lib.draw_button()
  51. pygame.image.save(app.screen, 'recovered_map_editor/library_preview.png')
  52. lib.dropdown = True
  53. lib.scroll(10000)
  54. lib.draw()
  55. lib.choose_library('missing_test_directory')
  56. wait_until(lambda: lib.scan_job is None)
  57. assert not lib.entries
  58. print('PASS: concurrent loading, stale job isolation, thumbnails, floor/obstacle selection and placement, undo, dropdown scrolling, invalid directory')
  59. finally:
  60. lib.close()
  61. pygame.quit()