示例#1
0
def test_register_theme():
    # Check that blue theme is not listed in available themes
    themes = available_themes()
    assert 'test_blue' not in themes

    # Create new blue theme based on dark theme
    blue_theme = get_theme('dark')
    blue_theme.update(
        background='rgb(28, 31, 48)',
        foreground='rgb(45, 52, 71)',
        primary='rgb(80, 88, 108)',
        current='rgb(184, 112, 0)',
    )

    # Register blue theme
    register_theme('test_blue', blue_theme)

    # Check that blue theme is listed in available themes
    themes = available_themes()
    assert 'test_blue' in themes

    # Check that the dark theme has not been overwritten
    dark_theme = get_theme('dark')
    assert not dark_theme['background'] == blue_theme['background']

    # Check that blue theme can be gotten from available themes
    theme = get_theme('test_blue')
    assert theme['background'] == blue_theme['background']
示例#2
0
def test_provide_theme_hook_unregister(
    napari_plugin_manager: "NapariPluginManager", ):
    with pytest.warns(FutureWarning):
        dark = get_theme("dark")
        dark["name"] = "dark-test"

    class TestPlugin:
        @napari_hook_implementation
        def napari_experimental_provide_theme():
            return {"dark-test": dark}

    napari_plugin_manager.discover_themes()
    napari_plugin_manager.register(TestPlugin)

    # make sure theme was registered
    assert "TestPlugin" in napari_plugin_manager._theme_data
    reg = napari_plugin_manager._theme_data["TestPlugin"]
    assert isinstance(reg, dict)
    assert len(reg) == 1
    assert "dark-test" in available_themes()
    get_settings().appearance.theme = "dark-test"

    with pytest.warns(UserWarning, match="The current theme "):
        napari_plugin_manager.unregister("TestPlugin")

    # make sure that plugin-specific data was removed
    assert "TestPlugin" not in napari_plugin_manager._theme_data
    # since the plugin was unregistered, the current theme cannot
    # be the theme registered by the plugin
    assert get_settings().appearance.theme != "dark-test"
    assert "dark-test" not in available_themes()
示例#3
0
def test_rebuild_theme_settings():
    settings = get_settings()
    assert "another-theme" not in available_themes()
    # theme is not updated
    with pytest.raises(ValidationError):
        settings.appearance.theme = "another-theme"
    blue_theme = get_theme("dark")
    register_theme("another-theme", blue_theme)
    settings.appearance.theme = "another-theme"
示例#4
0
def test_unregister_theme():
    # Create new blue theme based on dark theme
    blue_theme = get_theme('dark')
    blue_theme.update(
        background='rgb(28, 31, 48)',
        foreground='rgb(45, 52, 71)',
        primary='rgb(80, 88, 108)',
        current='rgb(184, 112, 0)',
    )

    # Register blue theme
    register_theme('test_blue', blue_theme)

    # Check that blue theme is listed in available themes
    themes = available_themes()
    assert 'test_blue' in themes

    # Remove theme from available themes
    unregister_theme("test_blue")
    themes = available_themes()
    assert 'test_blue' not in themes
示例#5
0
def test_provide_theme_hook(napari_plugin_manager: "NapariPluginManager"):
    with pytest.warns(FutureWarning):
        dark = get_theme("dark")
        dark["name"] = "dark-test"

    class TestPlugin:
        @napari_hook_implementation
        def napari_experimental_provide_theme():
            return {"dark-test": dark}

    viewer = ViewerModel()
    napari_plugin_manager.discover_themes()
    napari_plugin_manager.register(TestPlugin)

    # make sure theme data is present in the plugin
    reg = napari_plugin_manager._theme_data["TestPlugin"]
    assert isinstance(reg, dict)
    assert len(reg) == 1
    assert isinstance(reg["dark-test"], Theme)

    # make sure theme was registered
    assert "dark-test" in available_themes()
    viewer.theme = "dark-test"
示例#6
0
def test_provide_theme_hook_bad(napari_plugin_manager: "NapariPluginManager"):
    napari_plugin_manager.discover_themes()

    with pytest.warns(FutureWarning):
        dark = get_theme("dark")
        dark.pop("foreground")
        dark["name"] = "dark-bad"

    class TestPluginBad:
        @napari_hook_implementation
        def napari_experimental_provide_theme():
            return {"dark-bad": dark}

    with pytest.warns(
            UserWarning,
            match=", plugin 'TestPluginBad' provided an invalid dict object",
    ):
        napari_plugin_manager.register(TestPluginBad)

    # make sure theme data is present in the plugin but the theme is not there
    reg = napari_plugin_manager._theme_data["TestPluginBad"]
    assert isinstance(reg, dict)
    assert len(reg) == 0
    assert "dark-bad" not in available_themes()
示例#7
0
@skip_local_popups
def test_leaks_labels(qtbot, make_napari_viewer):
    viewer = make_napari_viewer(show=True)
    lr = weakref.ref(
        viewer.add_labels((np.random.rand(10, 10) * 10).astype(np.uint8)))
    dr = weakref.ref(lr().data)
    viewer.layers.clear()
    qtbot.wait(100)
    gc.collect()
    assert not gc.collect()
    assert not lr()
    assert not dr()


@pytest.mark.parametrize("theme", available_themes())
def test_canvas_color(make_napari_viewer, theme):
    """Test instantiating viewer with different themes.

    See: https://github.com/napari/napari/issues/3278
    """
    # This test is to make sure the application starts with
    # with different themes
    get_settings().appearance.theme = theme
    viewer = make_napari_viewer()
    assert viewer.theme == theme


def test_remove_points(make_napari_viewer):
    viewer = make_napari_viewer()
    viewer.add_points([(1, 2), (2, 3)])
示例#8
0
"""
Displays an image and sets the theme to new custom theme.
"""

from skimage import data
import napari
from napari.utils.theme import available_themes, get_theme, register_theme

with napari.gui_qt():
    # create the viewer with an image
    viewer = napari.view_image(data.astronaut(), rgb=True, name='astronaut')

    # List themes
    print('Originally themes', available_themes())

    blue_theme = get_theme('dark')
    blue_theme.update(
        background='rgb(28, 31, 48)',
        foreground='rgb(45, 52, 71)',
        primary='rgb(80, 88, 108)',
        current='rgb(184, 112, 0)',
    )

    register_theme('blue', blue_theme)

    # List themes
    print('New themes', available_themes())

    # Set theme
    viewer.theme = 'blue'
示例#9
0
def test_default_themes():
    themes = available_themes()
    assert 'dark' in themes
    assert 'light' in themes