test_favicon.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from pathlib import Path
  2. from typing import Union
  3. import pytest
  4. import requests
  5. from nicegui import favicon, ui
  6. from nicegui.testing import Screen
  7. DEFAULT_FAVICON_PATH = Path(__file__).parent.parent / 'nicegui' / 'static' / 'favicon.ico'
  8. LOGO_FAVICON_PATH = Path(__file__).parent.parent / 'website' / 'static' / 'logo_square.png'
  9. def get_favicon_url(screen: Screen) -> str:
  10. return screen.find_by_css('link[rel="shortcut icon"]').get_attribute('href')
  11. def assert_favicon(content: Union[Path, str, bytes], url_path: str = '/favicon.ico'):
  12. response = requests.get(f'http://localhost:{Screen.PORT}{url_path}', timeout=5)
  13. assert response.status_code == 200
  14. if isinstance(content, Path):
  15. assert content.read_bytes() == response.content
  16. elif isinstance(content, str):
  17. assert content == response.text
  18. elif isinstance(content, bytes):
  19. assert content == response.content
  20. else:
  21. raise TypeError(f'Unexpected type: {type(content)}')
  22. def test_default(screen: Screen):
  23. ui.label('Hello, world')
  24. screen.open('/')
  25. assert_favicon(DEFAULT_FAVICON_PATH)
  26. @pytest.mark.parametrize('emoji', ['👋', '⚔️'])
  27. def test_emoji(emoji: str, screen: Screen):
  28. ui.label('Hello, world')
  29. screen.ui_run_kwargs['favicon'] = emoji
  30. screen.open('/')
  31. assert get_favicon_url(screen).startswith('data:image/svg+xml')
  32. assert_favicon(favicon._char_to_svg(emoji))
  33. def test_data_url(screen: Screen):
  34. ui.label('Hello, world')
  35. icon = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
  36. screen.ui_run_kwargs['favicon'] = icon
  37. screen.open('/')
  38. assert get_favicon_url(screen).startswith('data:image/png;base64')
  39. _, bytes_ = favicon._data_url_to_bytes(icon)
  40. assert_favicon(bytes_)
  41. def test_custom_file(screen: Screen):
  42. ui.label('Hello, world')
  43. screen.ui_run_kwargs['favicon'] = LOGO_FAVICON_PATH
  44. screen.open('/')
  45. assert get_favicon_url(screen).endswith('/favicon.ico')
  46. assert_favicon(screen.ui_run_kwargs['favicon'])
  47. def test_page_specific_icon(screen: Screen):
  48. @ui.page('/subpage', favicon=LOGO_FAVICON_PATH)
  49. def sub():
  50. ui.label('Subpage')
  51. ui.label('Main')
  52. screen.open('/subpage')
  53. assert_favicon(LOGO_FAVICON_PATH, url_path='/subpage/favicon.ico')
  54. screen.open('/')
  55. def test_page_specific_emoji(screen: Screen):
  56. @ui.page('/subpage', favicon='👋')
  57. def sub():
  58. ui.label('Subpage')
  59. ui.label('Main')
  60. screen.open('/subpage')
  61. assert get_favicon_url(screen).startswith('data:image/svg+xml')
  62. screen.open('/')
  63. assert_favicon(DEFAULT_FAVICON_PATH)