2020-11-02 00:25:43 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# key-mapper - GUI for device specific keyboard mappings
|
|
|
|
# Copyright (C) 2020 sezanzeb <proxima@hip70890b.de>
|
|
|
|
#
|
|
|
|
# This file is part of key-mapper.
|
|
|
|
#
|
|
|
|
# key-mapper is free software: you can redistribute it and/or modify
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# key-mapper is distributed in the hope that it will be useful,
|
|
|
|
# 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
|
|
|
|
# along with key-mapper. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
import shutil
|
|
|
|
import time
|
|
|
|
|
2020-11-02 22:52:58 +00:00
|
|
|
from keymapper.presets import find_newest_preset, rename_preset
|
2020-11-02 20:57:28 +00:00
|
|
|
from keymapper.X import create_preset
|
2020-11-10 22:14:28 +00:00
|
|
|
from keymapper.paths import USERS_SYMBOLS, HOME_PATH
|
2020-11-02 00:25:43 +00:00
|
|
|
|
2020-11-07 23:54:19 +00:00
|
|
|
from test import tmp
|
2020-11-02 01:17:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestCreatePreset(unittest.TestCase):
|
2020-11-02 18:17:28 +00:00
|
|
|
def setUp(self):
|
|
|
|
if os.path.exists(tmp):
|
|
|
|
shutil.rmtree(tmp)
|
|
|
|
|
2020-11-02 01:17:58 +00:00
|
|
|
def test_create_preset_1(self):
|
|
|
|
create_preset('device 1')
|
2020-11-10 22:14:28 +00:00
|
|
|
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/new_preset'))
|
|
|
|
self.assertTrue(os.path.exists(f'{HOME_PATH}/device_1/new_preset'))
|
2020-11-02 01:17:58 +00:00
|
|
|
|
|
|
|
def test_create_preset_2(self):
|
|
|
|
create_preset('device 1')
|
|
|
|
create_preset('device 1')
|
2020-11-10 22:14:28 +00:00
|
|
|
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/new_preset'))
|
|
|
|
self.assertTrue(os.path.exists(f'{HOME_PATH}/device_1/new_preset'))
|
|
|
|
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/new_preset_2'))
|
|
|
|
self.assertTrue(os.path.exists(f'{HOME_PATH}/device_1/new_preset_2'))
|
2020-11-02 01:17:58 +00:00
|
|
|
|
|
|
|
def test_create_preset_3(self):
|
|
|
|
create_preset('device 1', 'pre set')
|
|
|
|
create_preset('device 1', 'pre set')
|
2020-11-10 22:14:28 +00:00
|
|
|
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/pre_set'))
|
|
|
|
self.assertTrue(os.path.exists(f'{HOME_PATH}/device_1/pre_set'))
|
|
|
|
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/pre_set_2'))
|
|
|
|
self.assertTrue(os.path.exists(f'{HOME_PATH}/device_1/pre_set_2'))
|
2020-11-02 01:17:58 +00:00
|
|
|
|
|
|
|
|
2020-11-02 22:52:58 +00:00
|
|
|
class TestRenamePreset(unittest.TestCase):
|
|
|
|
def test_rename_preset(self):
|
|
|
|
create_preset('device 1', 'preset 1')
|
|
|
|
create_preset('device 1', 'foobar')
|
|
|
|
rename_preset('device 1', 'preset 1', 'foobar')
|
2020-11-10 22:14:28 +00:00
|
|
|
self.assertFalse(os.path.exists(f'{HOME_PATH}/device_1/preset_1'))
|
|
|
|
self.assertTrue(os.path.exists(f'{HOME_PATH}/device_1/foobar'))
|
|
|
|
self.assertTrue(os.path.exists(f'{HOME_PATH}/device_1/foobar_2'))
|
2020-11-02 22:52:58 +00:00
|
|
|
|
|
|
|
|
2020-11-02 01:17:58 +00:00
|
|
|
class TestFindPresets(unittest.TestCase):
|
2020-11-02 00:25:43 +00:00
|
|
|
def setUp(self):
|
2020-11-02 18:17:28 +00:00
|
|
|
if os.path.exists(tmp):
|
|
|
|
shutil.rmtree(tmp)
|
2020-11-02 00:25:43 +00:00
|
|
|
|
|
|
|
def test_find_newest_preset_1(self):
|
2020-11-02 01:17:58 +00:00
|
|
|
create_preset('device 1', 'preset 1')
|
2020-11-02 00:25:43 +00:00
|
|
|
time.sleep(0.01)
|
2020-11-02 01:17:58 +00:00
|
|
|
create_preset('device 2', 'preset 2')
|
|
|
|
self.assertEqual(find_newest_preset(), ('device 2', 'preset 2'))
|
2020-11-02 00:25:43 +00:00
|
|
|
|
|
|
|
def test_find_newest_preset_2(self):
|
2020-11-10 22:14:28 +00:00
|
|
|
os.makedirs(f'{USERS_SYMBOLS}/device_1')
|
|
|
|
os.makedirs(f'{HOME_PATH}/device_1')
|
2020-11-02 00:25:43 +00:00
|
|
|
time.sleep(0.01)
|
2020-11-10 22:14:28 +00:00
|
|
|
os.makedirs(f'{USERS_SYMBOLS}/device_2')
|
|
|
|
os.makedirs(f'{HOME_PATH}/device_2')
|
2020-11-02 00:25:43 +00:00
|
|
|
# takes the first one that the test-fake returns
|
|
|
|
self.assertEqual(find_newest_preset(), ('device 1', None))
|
|
|
|
|
|
|
|
def test_find_newest_preset_3(self):
|
2020-11-10 22:14:28 +00:00
|
|
|
os.makedirs(f'{USERS_SYMBOLS}/device_1')
|
|
|
|
os.makedirs(f'{HOME_PATH}/device_1')
|
2020-11-02 00:25:43 +00:00
|
|
|
self.assertEqual(find_newest_preset(), ('device 1', None))
|
|
|
|
|
|
|
|
def test_find_newest_preset_4(self):
|
2020-11-02 01:17:58 +00:00
|
|
|
create_preset('device 1', 'preset 1')
|
|
|
|
self.assertEqual(find_newest_preset(), ('device 1', 'preset 1'))
|
2020-11-02 00:25:43 +00:00
|
|
|
|
|
|
|
def test_find_newest_preset_5(self):
|
2020-11-02 01:17:58 +00:00
|
|
|
create_preset('device 1', 'preset 1')
|
2020-11-02 00:25:43 +00:00
|
|
|
time.sleep(0.01)
|
2020-11-02 01:17:58 +00:00
|
|
|
create_preset('unknown device 3', 'preset 3')
|
|
|
|
self.assertEqual(find_newest_preset(), ('device 1', 'preset 1'))
|
2020-11-02 00:25:43 +00:00
|
|
|
|
|
|
|
def test_find_newest_preset_6(self):
|
|
|
|
# takes the first one that the test-fake returns
|
|
|
|
self.assertEqual(find_newest_preset(), ('device 1', None))
|
|
|
|
|
2020-11-02 22:52:58 +00:00
|
|
|
def test_find_newest_preset_7(self):
|
|
|
|
self.assertEqual(find_newest_preset('device 1'), ('device 1', None))
|
|
|
|
|
|
|
|
def test_find_newest_preset_8(self):
|
|
|
|
create_preset('device 1', 'preset 1')
|
|
|
|
time.sleep(0.01)
|
|
|
|
create_preset('device 1', 'preset 3')
|
|
|
|
time.sleep(0.01)
|
|
|
|
create_preset('device 2', 'preset 2')
|
|
|
|
self.assertEqual(
|
|
|
|
find_newest_preset('device 1'),
|
|
|
|
('device 1', 'preset 3')
|
|
|
|
)
|
|
|
|
|
2020-11-02 00:25:43 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|