test_favicon.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from pathlib import Path
  2. from typing import Union
  3. import requests
  4. from bs4 import BeautifulSoup
  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 assert_favicon_url_starts_with(screen: Screen, content: str):
  10. soup = BeautifulSoup(screen.selenium.page_source, 'html.parser')
  11. icon_link = soup.find("link", rel="icon")
  12. assert icon_link['href'].startswith(content)
  13. def assert_favicon(content: Union[Path, str, bytes], url_path: str = '/favicon.ico'):
  14. response = requests.get(f'http://localhost:{Screen.PORT}{url_path}', timeout=5)
  15. assert response.status_code == 200
  16. if isinstance(content, Path):
  17. assert content.read_bytes() == response.content
  18. elif isinstance(content, str):
  19. assert content == response.text
  20. elif isinstance(content, bytes):
  21. assert content == response.content
  22. else:
  23. raise TypeError(f'Unexpected type: {type(content)}')
  24. def test_default(screen: Screen):
  25. ui.label('Hello, world')
  26. screen.open('/')
  27. assert_favicon(DEFAULT_FAVICON_PATH)
  28. def test_emoji(screen: Screen):
  29. ui.label('Hello, world')
  30. screen.ui_run_kwargs['favicon'] = '👋'
  31. screen.open('/')
  32. assert_favicon_url_starts_with(screen, 'data:image/svg+xml')
  33. assert_favicon(favicon._char_to_svg('👋'))
  34. def test_data_url(screen: Screen):
  35. ui.label('Hello, world')
  36. icon = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
  37. screen.ui_run_kwargs['favicon'] = icon
  38. screen.open('/')
  39. assert_favicon_url_starts_with(screen, 'data:image/png;base64')
  40. _, bytes_ = favicon._data_url_to_bytes(icon)
  41. assert_favicon(bytes_)
  42. def test_custom_file(screen: Screen):
  43. ui.label('Hello, world')
  44. screen.ui_run_kwargs['favicon'] = LOGO_FAVICON_PATH
  45. screen.open('/')
  46. assert_favicon_url_starts_with(screen, '/favicon.ico')
  47. assert_favicon(screen.ui_run_kwargs['favicon'])
  48. def test_page_specific_icon(screen: Screen):
  49. @ui.page('/subpage', favicon=LOGO_FAVICON_PATH)
  50. def sub():
  51. ui.label('Subpage')
  52. ui.label('Main')
  53. screen.open('/subpage')
  54. assert_favicon(LOGO_FAVICON_PATH, url_path='/subpage/favicon.ico')
  55. screen.open('/')
  56. def test_page_specific_emoji(screen: Screen):
  57. @ui.page('/subpage', favicon='👋')
  58. def sub():
  59. ui.label('Subpage')
  60. ui.label('Main')
  61. screen.open('/subpage')
  62. assert_favicon_url_starts_with(screen, 'data:image/svg+xml')
  63. screen.open('/')
  64. assert_favicon(DEFAULT_FAVICON_PATH)