Add Clear cache to the settings
A new row, "Downloaded pictures and songs", shows how much space the cached character pictures, covers and downloaded songs take, with a Clear cache button. Clearing deletes them, but keeps the songs the current quiz uses, the settings, config.ini and the saved quiz. The characters are loaded again at once; covers come back when they are shown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
06421bb63e
commit
c03c0e4efe
10 files changed
+1216
-679
No files matched your search
+1003
-679
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,100 @@
|
||||
extends MainWindowTestSuite
|
||||
## Clear cache in the settings: deletes the downloaded character pictures, covers and
|
||||
## songs, but not the songs the quiz uses, the settings or the saved quiz.
|
||||
|
||||
var _popup: SettingsPopup
|
||||
|
||||
|
||||
func before_test() -> void:
|
||||
# The cache is kept next to the settings, which the tests keep in a folder of their own
|
||||
OS.set_environment(Settings.CONFIG_DIR_ENV, create_temp_dir("clear_cache_%d" % randi()))
|
||||
await super()
|
||||
_popup = _window.get_node("SettingsPopup")
|
||||
|
||||
|
||||
func _write(path: String, size: int) -> void:
|
||||
DirAccess.make_dir_recursive_absolute(path.get_base_dir())
|
||||
var file: FileAccess = FileAccess.open(path, FileAccess.WRITE)
|
||||
var bytes: PackedByteArray = PackedByteArray()
|
||||
bytes.resize(size)
|
||||
file.store_buffer(bytes)
|
||||
file.close()
|
||||
|
||||
|
||||
func _files_in(dir: String) -> PackedStringArray:
|
||||
return DirAccess.get_files_at(dir) if DirAccess.dir_exists_absolute(dir) \
|
||||
else PackedStringArray()
|
||||
|
||||
|
||||
func _fill_cache() -> String:
|
||||
_write(CharacterCache.folder().path_join("Crono.png"), 3000)
|
||||
_write(CoverCache.folder().path_join("st-1"), 2000)
|
||||
var old_song: String = Playlist.songs_folder().path_join("old.mp3")
|
||||
_write(old_song, 5000)
|
||||
return old_song
|
||||
|
||||
|
||||
func test_shows_how_much_the_cache_takes() -> void:
|
||||
_fill_cache()
|
||||
|
||||
_popup.open()
|
||||
|
||||
assert_str(_popup.cache_size_label.text).is_equal(
|
||||
SettingsPopup.size_text(SettingsPopup.cache_size()))
|
||||
assert_int(SettingsPopup.cache_size()).is_greater_equal(10000)
|
||||
assert_bool(_popup.clear_cache_button.disabled).is_false()
|
||||
|
||||
|
||||
func test_clear_cache_deletes_pictures_covers_and_old_songs() -> void:
|
||||
var old_song: String = _fill_cache()
|
||||
var playing: PackedStringArray = Playlist.song_files_in_use()
|
||||
assert_array(playing).is_not_empty()
|
||||
var lists_before: int = _server.count("/character/list")
|
||||
_popup.open()
|
||||
|
||||
_popup.clear_cache()
|
||||
|
||||
assert_array(_files_in(CharacterCache.folder())).is_empty()
|
||||
assert_array(_files_in(CoverCache.folder())).is_empty()
|
||||
assert_bool(FileAccess.file_exists(old_song)).is_false()
|
||||
# The quiz's songs stay, so they can still be played
|
||||
for path: String in playing:
|
||||
assert_bool(FileAccess.file_exists(path)).is_true()
|
||||
assert_str(_popup.cache_size_label.text).starts_with("Cleared ")
|
||||
assert_bool(_popup.clear_cache_button.disabled).is_true()
|
||||
# The characters are loaded again at once
|
||||
assert_int(_server.count("/character/list")).is_greater(lists_before)
|
||||
|
||||
|
||||
func test_clear_cache_keeps_the_settings_and_the_saved_quiz() -> void:
|
||||
Settings.save_settings()
|
||||
_quiz.save_soon()
|
||||
await _runner.simulate_frames(1)
|
||||
_popup.open()
|
||||
|
||||
_popup.clear_cache()
|
||||
|
||||
assert_bool(FileAccess.file_exists(Settings.saved_settings_path())).is_true()
|
||||
assert_bool(FileAccess.file_exists(QuizSave.path())).is_true()
|
||||
|
||||
|
||||
func test_nothing_to_clear() -> void:
|
||||
for dir: String in [CharacterCache.folder(), CoverCache.folder()]:
|
||||
for file_name: String in _files_in(dir):
|
||||
DirAccess.remove_absolute(dir.path_join(file_name))
|
||||
Playlist.clear_song_files(Playlist.song_files_in_use())
|
||||
|
||||
_popup.open()
|
||||
|
||||
assert_str(_popup.cache_size_label.text).is_equal("Nothing")
|
||||
assert_bool(_popup.clear_cache_button.disabled).is_true()
|
||||
|
||||
|
||||
@warning_ignore("unused_parameter")
|
||||
func test_size_text(bytes: int, text: String, test_parameters: Array = [
|
||||
[0, "Nothing"],
|
||||
[1, "1 KB"],
|
||||
[350 * 1024, "350 KB"],
|
||||
[int(14.2 * 1024 * 1024), "14.2 MB"],
|
||||
]) -> void:
|
||||
assert_str(SettingsPopup.size_text(bytes)).is_equal(text)
|
||||
@@ -0,0 +1 @@
|
||||
uid://da5wh1me3wncq
|
||||
Reference in new issue
Block a user