2020-11-20 20:38:19 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper - GUI for device specific keyboard mappings
|
2022-01-01 12:52:33 +00:00
|
|
|
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
|
2020-11-20 20:38:19 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2020-11-20 20:38:19 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2020-11-20 20:38:19 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is distributed in the hope that it will be useful,
|
2020-11-20 20:38:19 +00:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2022-01-01 12:00:49 +00:00
|
|
|
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
|
2020-11-20 20:38:19 +00:00
|
|
|
|
|
|
|
|
2022-03-03 22:42:37 +00:00
|
|
|
from tests.test import quick_cleanup, tmp
|
|
|
|
|
2020-12-19 20:50:27 +00:00
|
|
|
import os
|
2020-11-20 20:38:19 +00:00
|
|
|
import unittest
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.global_config import global_config
|
|
|
|
from inputremapper.configs.paths import touch
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2020-11-20 20:38:19 +00:00
|
|
|
|
|
|
|
class TestConfig(unittest.TestCase):
|
|
|
|
def tearDown(self):
|
2021-02-12 20:43:40 +00:00
|
|
|
quick_cleanup()
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(len(global_config.iterate_autoload_presets()), 0)
|
2020-11-20 20:38:19 +00:00
|
|
|
|
2020-11-30 19:57:09 +00:00
|
|
|
def test_basic(self):
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(global_config.get("a"), None)
|
2020-12-01 23:02:41 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config.set("a", 1)
|
|
|
|
self.assertEqual(global_config.get("a"), 1)
|
2020-11-30 19:57:09 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config.remove("a")
|
|
|
|
global_config.set("a.b", 2)
|
|
|
|
self.assertEqual(global_config.get("a.b"), 2)
|
|
|
|
self.assertEqual(global_config._config["a"]["b"], 2)
|
2020-11-30 19:57:09 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config.remove("a.b")
|
|
|
|
global_config.set("a.b.c", 3)
|
|
|
|
self.assertEqual(global_config.get("a.b.c"), 3)
|
|
|
|
self.assertEqual(global_config._config["a"]["b"]["c"], 3)
|
2020-11-30 19:57:09 +00:00
|
|
|
|
2020-11-20 20:38:19 +00:00
|
|
|
def test_autoload(self):
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(len(global_config.iterate_autoload_presets()), 0)
|
|
|
|
self.assertFalse(global_config.is_autoloaded("d1", "a"))
|
|
|
|
self.assertFalse(global_config.is_autoloaded("d2.foo", "b"))
|
|
|
|
self.assertEqual(global_config.get(["autoload", "d1"]), None)
|
|
|
|
self.assertEqual(global_config.get(["autoload", "d2.foo"]), None)
|
|
|
|
|
|
|
|
global_config.set_autoload_preset("d1", "a")
|
|
|
|
self.assertEqual(len(global_config.iterate_autoload_presets()), 1)
|
|
|
|
self.assertTrue(global_config.is_autoloaded("d1", "a"))
|
|
|
|
self.assertFalse(global_config.is_autoloaded("d2.foo", "b"))
|
|
|
|
|
|
|
|
global_config.set_autoload_preset("d2.foo", "b")
|
|
|
|
self.assertEqual(len(global_config.iterate_autoload_presets()), 2)
|
|
|
|
self.assertTrue(global_config.is_autoloaded("d1", "a"))
|
|
|
|
self.assertTrue(global_config.is_autoloaded("d2.foo", "b"))
|
|
|
|
self.assertEqual(global_config.get(["autoload", "d1"]), "a")
|
|
|
|
self.assertEqual(global_config.get("autoload.d1"), "a")
|
|
|
|
self.assertEqual(global_config.get(["autoload", "d2.foo"]), "b")
|
|
|
|
|
|
|
|
global_config.set_autoload_preset("d2.foo", "c")
|
|
|
|
self.assertEqual(len(global_config.iterate_autoload_presets()), 2)
|
|
|
|
self.assertTrue(global_config.is_autoloaded("d1", "a"))
|
|
|
|
self.assertFalse(global_config.is_autoloaded("d2.foo", "b"))
|
|
|
|
self.assertTrue(global_config.is_autoloaded("d2.foo", "c"))
|
|
|
|
self.assertEqual(global_config._config["autoload"]["d2.foo"], "c")
|
2020-11-20 20:38:19 +00:00
|
|
|
self.assertListEqual(
|
2022-01-31 19:58:37 +00:00
|
|
|
list(global_config.iterate_autoload_presets()),
|
2022-01-10 19:37:22 +00:00
|
|
|
[("d1", "a"), ("d2.foo", "c")],
|
2020-11-20 20:38:19 +00:00
|
|
|
)
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config.set_autoload_preset("d2.foo", None)
|
|
|
|
self.assertTrue(global_config.is_autoloaded("d1", "a"))
|
|
|
|
self.assertFalse(global_config.is_autoloaded("d2.foo", "b"))
|
|
|
|
self.assertFalse(global_config.is_autoloaded("d2.foo", "c"))
|
|
|
|
self.assertListEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
list(global_config.iterate_autoload_presets()),
|
|
|
|
[("d1", "a")],
|
2022-01-31 19:58:37 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(global_config.get(["autoload", "d1"]), "a")
|
2020-11-26 23:11:23 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertRaises(ValueError, global_config.is_autoloaded, "d1", None)
|
|
|
|
self.assertRaises(ValueError, global_config.is_autoloaded, None, "a")
|
|
|
|
|
2020-12-19 20:50:27 +00:00
|
|
|
def test_initial(self):
|
|
|
|
# when loading for the first time, create a config file with
|
|
|
|
# the default values
|
2022-01-31 19:58:37 +00:00
|
|
|
os.remove(global_config.path)
|
|
|
|
self.assertFalse(os.path.exists(global_config.path))
|
|
|
|
global_config.load_config()
|
|
|
|
self.assertTrue(os.path.exists(global_config.path))
|
2020-12-19 20:50:27 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
with open(global_config.path, "r") as file:
|
2020-12-19 20:50:27 +00:00
|
|
|
contents = file.read()
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertIn('"autoload": {}', contents)
|
2020-12-19 20:50:27 +00:00
|
|
|
|
2020-11-20 20:38:19 +00:00
|
|
|
def test_save_load(self):
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(len(global_config.iterate_autoload_presets()), 0)
|
2020-11-25 18:20:44 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config.load_config()
|
|
|
|
self.assertEqual(len(global_config.iterate_autoload_presets()), 0)
|
2020-11-25 18:20:44 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config.set_autoload_preset("d1", "a")
|
|
|
|
global_config.set_autoload_preset("d2.foo", "b")
|
2020-11-20 20:38:19 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config.load_config()
|
2020-11-20 20:38:19 +00:00
|
|
|
self.assertListEqual(
|
2022-01-31 19:58:37 +00:00
|
|
|
list(global_config.iterate_autoload_presets()),
|
2022-01-10 19:37:22 +00:00
|
|
|
[("d1", "a"), ("d2.foo", "b")],
|
2020-11-20 20:38:19 +00:00
|
|
|
)
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
config_2 = os.path.join(tmp, "config_2.json")
|
2021-01-07 16:15:12 +00:00
|
|
|
touch(config_2)
|
2021-09-26 10:44:56 +00:00
|
|
|
with open(config_2, "w") as f:
|
2021-01-07 16:15:12 +00:00
|
|
|
f.write('{"a":"b"}')
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config.load_config(config_2)
|
|
|
|
self.assertEqual(global_config.get("a"), "b")
|
|
|
|
self.assertEqual(global_config.get(["a"]), "b")
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2020-11-20 20:38:19 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|