File size: 8,903 Bytes
a5784e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""Tests for gui/theme.py module."""

from unittest.mock import MagicMock, patch


class TestGetAppearanceMode:
    """Tests for get_appearance_mode function."""

    def test_returns_default_mode(self):
        """Should return the default mode on first call."""
        from gui import theme

        # Reset module state
        theme._current_mode = "dark"
        assert theme.get_appearance_mode() == "dark"

    def test_returns_current_mode_after_set(self):
        """Should return the mode that was set."""
        from gui import theme

        theme._current_mode = "light"
        assert theme.get_appearance_mode() == "light"

        theme._current_mode = "system"
        assert theme.get_appearance_mode() == "system"


class TestSetAppearanceMode:
    """Tests for set_appearance_mode function."""

    @patch("gui.theme.ctk")
    def test_sets_dark_mode(self, mock_ctk):
        """Should set dark mode correctly."""
        from gui import theme

        theme._on_change_callbacks = []
        theme.set_appearance_mode("dark")

        assert theme._current_mode == "dark"
        mock_ctk.set_appearance_mode.assert_called_with("dark")

    @patch("gui.theme.ctk")
    def test_sets_light_mode(self, mock_ctk):
        """Should set light mode correctly."""
        from gui import theme

        theme._on_change_callbacks = []
        theme.set_appearance_mode("light")

        assert theme._current_mode == "light"
        mock_ctk.set_appearance_mode.assert_called_with("light")

    @patch("gui.theme.ctk")
    def test_sets_system_mode(self, mock_ctk):
        """Should set system mode correctly."""
        from gui import theme

        theme._on_change_callbacks = []
        theme.set_appearance_mode("system")

        assert theme._current_mode == "system"
        mock_ctk.set_appearance_mode.assert_called_with("system")

    @patch("gui.theme.ctk")
    def test_invalid_mode_defaults_to_dark(self, mock_ctk):
        """Should default to dark for invalid mode."""
        from gui import theme

        theme._on_change_callbacks = []
        theme.set_appearance_mode("invalid")

        assert theme._current_mode == "dark"
        mock_ctk.set_appearance_mode.assert_called_with("dark")

    @patch("gui.theme.ctk")
    def test_notifies_callbacks(self, mock_ctk):
        """Should notify registered callbacks on mode change."""
        from gui import theme

        callback = MagicMock()
        theme._on_change_callbacks = [callback]

        theme.set_appearance_mode("light")

        callback.assert_called_once_with("light")

    @patch("gui.theme.ctk")
    def test_handles_callback_exception(self, mock_ctk):
        """Should continue even if callback raises exception."""
        from gui import theme

        bad_callback = MagicMock(side_effect=Exception("Test error"))
        good_callback = MagicMock()
        theme._on_change_callbacks = [bad_callback, good_callback]

        # Should not raise
        theme.set_appearance_mode("dark")

        bad_callback.assert_called_once()
        good_callback.assert_called_once_with("dark")


class TestToggleAppearanceMode:
    """Tests for toggle_appearance_mode function."""

    @patch("gui.theme.ctk")
    def test_toggle_from_dark_to_light(self, mock_ctk):
        """Should toggle from dark to light."""
        from gui import theme

        theme._current_mode = "dark"
        theme._on_change_callbacks = []

        result = theme.toggle_appearance_mode()

        assert result == "light"
        assert theme._current_mode == "light"

    @patch("gui.theme.ctk")
    def test_toggle_from_light_to_dark(self, mock_ctk):
        """Should toggle from light to dark."""
        from gui import theme

        theme._current_mode = "light"
        theme._on_change_callbacks = []

        result = theme.toggle_appearance_mode()

        assert result == "dark"
        assert theme._current_mode == "dark"

    @patch("gui.theme.ctk")
    def test_toggle_from_system_to_dark(self, mock_ctk):
        """Should toggle from system to dark (system is treated as not-dark)."""
        from gui import theme

        theme._current_mode = "system"
        theme._on_change_callbacks = []

        result = theme.toggle_appearance_mode()

        assert result == "dark"
        assert theme._current_mode == "dark"


class TestIsDarkMode:
    """Tests for is_dark_mode function."""

    def test_dark_mode_returns_true(self):
        """Should return True when mode is dark."""
        from gui import theme

        theme._current_mode = "dark"
        assert theme.is_dark_mode() is True

    def test_light_mode_returns_false(self):
        """Should return False when mode is light."""
        from gui import theme

        theme._current_mode = "light"
        assert theme.is_dark_mode() is False

    @patch("gui.theme.ctk")
    def test_system_mode_checks_actual(self, mock_ctk):
        """Should check actual system setting when mode is system."""
        from gui import theme

        theme._current_mode = "system"

        mock_ctk.get_appearance_mode.return_value = "Dark"
        assert theme.is_dark_mode() is True

        mock_ctk.get_appearance_mode.return_value = "Light"
        assert theme.is_dark_mode() is False


class TestThemeCallbacks:
    """Tests for callback registration functions."""

    def test_on_theme_change_registers_callback(self):
        """Should register a callback."""
        from gui import theme

        theme._on_change_callbacks = []
        callback = MagicMock()

        theme.on_theme_change(callback)

        assert callback in theme._on_change_callbacks

    def test_on_theme_change_prevents_duplicates(self):
        """Should not register the same callback twice."""
        from gui import theme

        theme._on_change_callbacks = []
        callback = MagicMock()

        theme.on_theme_change(callback)
        theme.on_theme_change(callback)

        assert len(theme._on_change_callbacks) == 1

    def test_remove_theme_callback(self):
        """Should remove a registered callback."""
        from gui import theme

        callback = MagicMock()
        theme._on_change_callbacks = [callback]

        theme.remove_theme_callback(callback)

        assert callback not in theme._on_change_callbacks

    def test_remove_nonexistent_callback_no_error(self):
        """Should not error when removing non-existent callback."""
        from gui import theme

        theme._on_change_callbacks = []
        callback = MagicMock()

        # Should not raise
        theme.remove_theme_callback(callback)


class TestGetModeDisplayName:
    """Tests for get_mode_display_name function."""

    def test_dark_mode_display_name(self):
        """Should return dark mode display name with emoji."""
        from gui.theme import get_mode_display_name

        assert get_mode_display_name("dark") == "🌙 Dark"

    def test_light_mode_display_name(self):
        """Should return light mode display name with emoji."""
        from gui.theme import get_mode_display_name

        assert get_mode_display_name("light") == "☀️ Light"

    def test_system_mode_display_name(self):
        """Should return system mode display name with emoji."""
        from gui.theme import get_mode_display_name

        assert get_mode_display_name("system") == "💻 System"

    def test_unknown_mode_returns_mode(self):
        """Should return the mode itself for unknown modes."""
        from gui.theme import get_mode_display_name

        assert get_mode_display_name("unknown") == "unknown"


class TestGetAvailableModes:
    """Tests for get_available_modes function."""

    def test_returns_all_modes(self):
        """Should return all available modes."""
        from gui.theme import get_available_modes

        modes = get_available_modes()

        assert "dark" in modes
        assert "light" in modes
        assert "system" in modes
        assert len(modes) == 3

    def test_returns_list(self):
        """Should return a list."""
        from gui.theme import get_available_modes

        assert isinstance(get_available_modes(), list)


class TestInitTheme:
    """Tests for init_theme function."""

    @patch("gui.theme.ctk")
    def test_init_with_default_dark(self, mock_ctk):
        """Should initialize with dark mode by default."""
        from gui import theme

        theme._on_change_callbacks = []
        theme.init_theme()

        assert theme._current_mode == "dark"
        mock_ctk.set_appearance_mode.assert_called_with("dark")

    @patch("gui.theme.ctk")
    def test_init_with_custom_mode(self, mock_ctk):
        """Should initialize with specified mode."""
        from gui import theme

        theme._on_change_callbacks = []
        theme.init_theme("light")

        assert theme._current_mode == "light"
        mock_ctk.set_appearance_mode.assert_called_with("light")