diff --git a/openpilot/selfdrive/ui/tests/test_onboarding_textures.py b/openpilot/selfdrive/ui/tests/test_onboarding_textures.py index df5abf0530..5a8dff089d 100644 --- a/openpilot/selfdrive/ui/tests/test_onboarding_textures.py +++ b/openpilot/selfdrive/ui/tests/test_onboarding_textures.py @@ -1,66 +1,57 @@ import threading +from pathlib import Path + +from openpilot.selfdrive.ui.layouts import onboarding, training_guide -from openpilot.selfdrive.ui.layouts import onboarding +def make_guide(loader): + guide = object.__new__(training_guide.TrainingGuide) + guide._children = [] + guide._loader = loader + return guide def test_release_unloads_textures_and_images_once(monkeypatch): unloaded = [] - monkeypatch.setattr(onboarding.rl, "unload_texture", lambda texture: unloaded.append(("texture", texture))) - monkeypatch.setattr(onboarding.rl, "unload_image", lambda image: unloaded.append(("image", image))) + monkeypatch.setattr(training_guide.rl, "unload_texture", lambda texture: unloaded.append(("texture", texture))) + monkeypatch.setattr(training_guide.rl, "unload_image", lambda image: unloaded.append(("image", image))) - guide = object.__new__(onboarding.TrainingGuide) - guide._lock = threading.Lock() - guide._released = False - guide._textures = ["texture-0", "texture-1"] - guide._image_objs = ["image-0"] + loader = training_guide.TextureLoader() + loader._textures = {"a": "texture-0", "b": "texture-1"} + loader._images = {"c": "image-0"} + loader.release() - guide.release() - - assert guide._textures == [] - assert guide._image_objs == [] + assert loader._textures == {} + assert loader._images == {} assert unloaded == [("texture", "texture-0"), ("texture", "texture-1"), ("image", "image-0")] - # releasing again must not unload anything twice - guide.release() + loader.release() assert unloaded == [("texture", "texture-0"), ("texture", "texture-1"), ("image", "image-0")] def test_preload_thread_does_not_append_after_release(monkeypatch): - unloaded = [] - monkeypatch.setattr(onboarding.gui_app, "_load_image_from_path", lambda path, *args, **kwargs: f"image:{path}") - monkeypatch.setattr(onboarding.rl, "unload_image", lambda image: unloaded.append(image)) + loaded = [] + monkeypatch.setattr(training_guide.gui_app, "_load_image_from_path", loaded.append) + loader = training_guide.TextureLoader() + generation = loader._generation - guide = object.__new__(onboarding.TrainingGuide) - guide._lock = threading.Lock() - guide._released = False - guide._textures = [] - guide._image_objs = [] - guide._image_paths = ["a.png", "b.png", "c.png"] + loader.release() + loader._load(["a.png", "b.png", "c.png"], generation) - guide.release() - guide._preload_thread() - - assert guide._image_objs == [] - assert unloaded == ["image:b.png"] + assert loader._images == {} + assert loaded == [] def test_hide_event_releases_textures(monkeypatch): unloaded = [] - monkeypatch.setattr(onboarding.rl, "unload_texture", lambda texture: unloaded.append(texture)) - monkeypatch.setattr(onboarding.rl, "unload_image", lambda image: None) - - guide = object.__new__(onboarding.TrainingGuide) - guide._children = [] - guide._lock = threading.Lock() - guide._released = False - guide._textures = ["texture-0"] - guide._image_objs = [] + monkeypatch.setattr(training_guide.rl, "unload_texture", unloaded.append) + loader = training_guide.TextureLoader() + loader._textures = {"a": "texture-0"} + guide = make_guide(loader) guide.hide_event() - assert guide._released - assert guide._textures == [] + assert loader._textures == {} assert unloaded == ["texture-0"] @@ -92,69 +83,65 @@ def test_onboarding_window_registers_guide_as_child(monkeypatch): def test_onboarding_window_hide_event_releases_child_guide(monkeypatch): unloaded = [] - monkeypatch.setattr(onboarding.rl, "unload_texture", lambda texture: unloaded.append(texture)) - monkeypatch.setattr(onboarding.rl, "unload_image", lambda image: None) - - guide = object.__new__(onboarding.TrainingGuide) - guide._children = [] - guide._lock = threading.Lock() - guide._released = False - guide._textures = ["texture-0"] - guide._image_objs = [] + monkeypatch.setattr(training_guide.rl, "unload_texture", unloaded.append) + loader = training_guide.TextureLoader() + loader._textures = {"a": "texture-0"} window = object.__new__(onboarding.OnboardingWindow) window._children = [] - window._child(guide) - + window._child(make_guide(loader)) window.hide_event() - assert guide._released + assert loader._textures == {} assert unloaded == ["texture-0"] def test_release_while_preload_is_loading_discards_pending_image(monkeypatch): loading, resume = threading.Event(), threading.Event() unloaded = [] + def load(path): loading.set() assert resume.wait(2) - return path - monkeypatch.setattr(onboarding.gui_app, '_load_image_from_path', load) - monkeypatch.setattr(onboarding.rl, 'unload_image', unloaded.append) - guide = object.__new__(onboarding.TrainingGuide) - guide._lock, guide._released = threading.Lock(), False - guide._textures, guide._image_objs = [], [] - guide._image_paths = ['first', 'pending', 'unused'] - thread = threading.Thread(target=guide._preload_thread) + return Path(path).name + + monkeypatch.setattr(training_guide.gui_app, "_load_image_from_path", load) + monkeypatch.setattr(training_guide.rl, "unload_image", unloaded.append) + loader = training_guide.TextureLoader() + thread = threading.Thread(target=loader._load, args=(["pending", "unused"], loader._generation)) thread.start() try: assert loading.wait(2) - guide.release() + loader.release() finally: resume.set() thread.join(2) + assert not thread.is_alive() - assert guide._image_objs == [] - assert unloaded == ['pending'] + assert loader._images == {} + assert unloaded == ["pending"] def test_first_texture_is_private_and_settings_review_constructs_fresh(monkeypatch): from openpilot.selfdrive.ui.layouts.settings import device - monkeypatch.setattr(onboarding.TrainingGuide, '_load_image_paths', - lambda self: setattr(self, '_image_paths', ['first'])) - monkeypatch.setattr(onboarding.gui_app, '_load_image_from_path', lambda path: path) - monkeypatch.setattr(onboarding.gui_app, '_load_texture_from_image', lambda image: 'private:' + image) + + monkeypatch.setattr(training_guide.TextureLoader, "queue", lambda self, paths: None) + monkeypatch.setattr(training_guide.gui_app, "_load_image_from_path", lambda path: Path(path).name) + monkeypatch.setattr(training_guide.gui_app, "_load_texture_from_image", lambda image: "private:" + image) + def shared_texture(*args, **kwargs): - raise AssertionError('Shared texture cache used') - monkeypatch.setattr(onboarding.gui_app, 'texture', shared_texture) - monkeypatch.setattr(onboarding.rl, 'unload_texture', lambda texture: None) + raise AssertionError("Shared texture cache used for the first photo") + + monkeypatch.setattr(training_guide.gui_app, "texture", shared_texture) + monkeypatch.setattr(training_guide.rl, "unload_texture", lambda texture: None) guides = [] - monkeypatch.setattr(device.gui_app, 'push_widget', guides.append) + monkeypatch.setattr(device.gui_app, "push_widget", guides.append) owner = object.__new__(device.DeviceLayout) device.DeviceLayout._on_review_training_guide(owner) guides[0].hide_event() device.DeviceLayout._on_review_training_guide(owner) + assert guides[0] is not guides[1] - assert guides[0]._textures == [] - assert guides[1]._textures == ['private:first'] + assert guides[0]._loader._textures == {} + assert guides[1]._loader._textures == {"training/bg_welcome.png": "private:bg_welcome.png"} guides[1].hide_event()