Debian Codegraph @

src/pyj/test_theme.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import hash_literals
4
5 from book_list.theme import css_for_variables, set_ui_colors, theme_variables
6 from session import all_settings
7 from test_book_list_helpers import with_empty_session
8 from testing import assert_equal, assert_true, test
9
10 DEFAULT_PRIMARY_HEX = all_settings.ui_highlight_colors.default.light
11
12
13 def assert_css_and_dom_use_variables(variables, css, style):
14 for name in Object.keys(variables):
15 assert_true(css.indexOf(name + ': ' + variables[name] + ';') > -1)
16 assert_equal(style.getPropertyValue(name), variables[name])
17
18
19 def assert_variables_have_values(variables, expected):
20 for name in Object.keys(expected):
21 assert_equal(variables[name], expected[name])
22
23
24 @test
25 def invalid_primary_color_uses_default_theme_contract():
26 def run(sd):
27 sd.set('book_list_color_scheme', 'light')
28 sd.set('ui_highlight_colors', {'light': 'not-a-color', 'dark': 'not-a-color'})
29 variables = theme_variables(False, 'not-a-color')
30 default_variables = theme_variables(False, DEFAULT_PRIMARY_HEX)
31 assert_equal(Object.keys(variables).length, Object.keys(default_variables).length)
32 for name in Object.keys(default_variables):
33 assert_equal(variables[name], default_variables[name])
34 css = css_for_variables()
35 set_ui_colors(False, 'not-a-color')
36 assert_css_and_dom_use_variables(default_variables, css, document.documentElement.style)
37 with_empty_session(run)
38
39
40 @test
41 def light_theme_css_uses_shared_variables():
42 def run(sd):
43 sd.set('book_list_color_scheme', 'light')
44 sd.set('ui_highlight_colors', {'light': '#437000', 'dark': '#437000'})
45 variables = theme_variables(False, '#437000')
46 css = css_for_variables()
47 set_ui_colors(False, '#437000')
48 style = document.documentElement.style
49 assert_variables_have_values(variables, {
50 '--calibre-color-primary': '#437000',
51 '--calibre-color-primary-light': 'rgba(67,112,0,0.18)',
52 '--calibre-color-primary-light-2': 'rgba(67,112,0,0.28)',
53 '--calibre-color-window-background': '#f5f7f1',
54 '--calibre-color-window-background2': '#e9efe2',
55 '--calibre-color-tree-highlight-item': '#edf1e7',
56 '--calibre-color-dialog-background': '#f5f7f1',
57 '--calibre-color-list-hover-background': '#dfe7d4',
58 '--calibre-color-list-hover-foreground': '#39322B',
59 '--calibre-color-input-background': '#f2f5ee',
60 '--calibre-color-bar-background': '#f5f8f2',
61 '--calibre-color-bar-foreground': '#39322B',
62 '--calibre-color-button-start': '#304418',
63 '--calibre-color-button-end': '#394b25',
64 })
65 assert_css_and_dom_use_variables(variables, css, style)
66 assert_equal(style.colorScheme, 'light')
67 with_empty_session(run)
68
69
70 @test
71 def dark_theme_css_uses_shared_variables():
72 def run(sd):
73 sd.set('book_list_color_scheme', 'dark')
74 sd.set('ui_highlight_colors', {'light': '#437000', 'dark': '#437000'})
75 variables = theme_variables(True, '#437000')
76 css = css_for_variables()
77 set_ui_colors(True, '#437000')
78 style = document.documentElement.style
79 assert_variables_have_values(variables, {
80 '--calibre-color-primary': '#437000',
81 '--calibre-color-primary-light': 'rgba(67,112,0,0.18)',
82 '--calibre-color-primary-light-2': 'rgba(67,112,0,0.28)',
83 '--calibre-color-window-background': '#2d2d2d',
84 '--calibre-color-window-background2': '#777',
85 '--calibre-color-tree-highlight-item': '#777',
86 '--calibre-color-dialog-background': '#2d2d2d',
87 '--calibre-color-list-hover-background': '#777',
88 '--calibre-color-list-hover-foreground': '#1d1d1d',
89 '--calibre-color-input-background': 'black',
90 '--calibre-color-bar-background': '#777',
91 '--calibre-color-bar-foreground': '#1d1d1d',
92 '--calibre-color-button-start': '#ddd',
93 '--calibre-color-button-end': '#666',
94 })
95 assert_css_and_dom_use_variables(variables, css, style)
96 assert_equal(style.colorScheme, 'dark')
97 with_empty_session(run)