-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathtest_appearance_manager.py
More file actions
92 lines (73 loc) · 3.6 KB
/
test_appearance_manager.py
File metadata and controls
92 lines (73 loc) · 3.6 KB
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
"""
Unit tests for AppearanceManager.set_light_mode / set_primary_color.
Regression tests: pre-fix these methods called prefs.set_string(...) — a method
that does not exist on SharedPreferences — so any third-party caller supplying a
prefs object hit AttributeError. These tests pin down the corrected
edit/put_string/commit write path and the expected on-disk values.
Usage:
Desktop: ./tests/unittest.sh tests/test_appearance_manager.py
Device: ./tests/unittest.sh tests/test_appearance_manager.py --ondevice
"""
import os
import unittest
from mpos import AppearanceManager, SharedPreferences
class TestAppearanceManagerPrefs(unittest.TestCase):
APP_ID = "com.test.appearance_manager"
FILEPATH = "data/com.test.appearance_manager/config.json"
def setUp(self):
# Scrub any leftover prefs from previous runs so get_string()
# observations reflect only what this test writes.
self._remove(self.FILEPATH)
# Snapshot AppearanceManager state so a test that flips light_mode
# doesn't leak into the next test.
self._saved_is_light = AppearanceManager.is_light_mode()
self._saved_primary = AppearanceManager.get_primary_color()
def tearDown(self):
self._remove(self.FILEPATH)
# Best-effort restore.
AppearanceManager._is_light_mode = self._saved_is_light
AppearanceManager._primary_color = self._saved_primary
def _remove(self, path):
try:
os.remove(path)
except OSError:
pass
# ---- set_light_mode --------------------------------------------------
def test_set_light_mode_dark_persists(self):
prefs = SharedPreferences(self.APP_ID)
AppearanceManager.set_light_mode(False, prefs)
self.assertEqual(prefs.get_string("theme_light_dark"), "dark")
self.assertFalse(AppearanceManager.is_light_mode())
def test_set_light_mode_light_persists(self):
prefs = SharedPreferences(self.APP_ID)
AppearanceManager.set_light_mode(True, prefs)
self.assertEqual(prefs.get_string("theme_light_dark"), "light")
self.assertTrue(AppearanceManager.is_light_mode())
def test_set_light_mode_without_prefs_updates_memory_only(self):
# Without a prefs arg the method must not raise and must still update
# the in-memory flag, matching the documented "prefs optional" signature.
AppearanceManager.set_light_mode(False)
self.assertFalse(AppearanceManager.is_light_mode())
AppearanceManager.set_light_mode(True)
self.assertTrue(AppearanceManager.is_light_mode())
# ---- set_primary_color -----------------------------------------------
def test_set_primary_color_persists_hex_string(self):
prefs = SharedPreferences(self.APP_ID)
AppearanceManager.set_primary_color(0xFF5722, prefs)
self.assertEqual(prefs.get_string("theme_primary_color"), "0xFF5722")
def test_set_primary_color_non_int_does_not_persist(self):
# The method only persists when the colour is an int; anything else
# (e.g. an lv.color_t) just updates the in-memory value. It must not
# raise either way.
prefs = SharedPreferences(self.APP_ID)
try:
AppearanceManager.set_primary_color("not-an-int", prefs)
except Exception as e:
self.fail("set_primary_color raised for non-int colour: %r" % e)
# Key wasn't written, so get_string returns the explicit default.
self.assertEqual(
prefs.get_string("theme_primary_color", "__unset__"),
"__unset__",
)
if __name__ == "__main__":
unittest.main()