60 lines
2.7 KiB
Python
60 lines
2.7 KiB
Python
import importlib.util
|
|
from pathlib import Path
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
path = Path(__file__).resolve().parents[1] / 'backend' / 'display_preview.py'
|
|
spec = importlib.util.spec_from_file_location('display_preview', path)
|
|
mod = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(mod)
|
|
|
|
class DisplayPreviewTests(unittest.TestCase):
|
|
def test_valid_rule(self):
|
|
self.assertEqual(mod.monitor_rule('eDP-1', '1920x1080@60.00', '0x0', '1.25'), 'eDP-1,1920x1080@60.00,0x0,1.25')
|
|
|
|
def test_reject_injection(self):
|
|
for value in ('eDP-1;rm -rf /', 'eDP-1,disable', '../monitor'):
|
|
with self.assertRaises(ValueError):
|
|
mod.monitor_rule(value, 'preferred', 'auto', '1')
|
|
with self.assertRaises(ValueError):
|
|
mod.monitor_rule('eDP-1', '1920x1080;bad', 'auto', '1')
|
|
|
|
def test_original_rule(self):
|
|
data = {'name':'eDP-1','width':1920,'height':1080,'refreshRate':60,'x':-1920,'y':0,'scale':1.5}
|
|
self.assertEqual(mod.original_rule(data), 'eDP-1,1920x1080@60.00,-1920x0,1.5,transform,0')
|
|
|
|
def test_transform_and_position(self):
|
|
self.assertEqual(mod.monitor_rule('DP-1', 'preferred', '-1200x0', '1.5', 3),
|
|
'DP-1,preferred,-1200x0,1.5,transform,3')
|
|
with self.assertRaises(ValueError):
|
|
mod.monitor_rule('DP-1', 'preferred', 'auto', '1', 8)
|
|
with self.assertRaises(ValueError):
|
|
mod.monitor_rule('DP-1', 'preferred', 'auto', '1', True)
|
|
|
|
def test_preview_keeps_position_and_rotation(self):
|
|
monitor = {'name':'eDP-1', 'width':1080, 'height':2400, 'refreshRate':60,
|
|
'x':-1080, 'y':200, 'scale':1.5, 'transform':1}
|
|
with patch.object(mod, 'current', return_value=monitor), \
|
|
patch.object(mod, 'state_dir') as directory, \
|
|
patch.object(mod, 'run_hypr') as run, \
|
|
patch.object(mod.subprocess, 'Popen') as popen:
|
|
import tempfile
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
directory.return_value = Path(tmp)
|
|
popen.return_value.pid = 123
|
|
result = mod.preview('eDP-1', 'preferred', '2', '3')
|
|
self.assertTrue(result['ok'])
|
|
run.assert_called_once_with('eDP-1,preferred,-1080x200,2,transform,3')
|
|
|
|
def test_unknown_command(self):
|
|
with self.assertRaises(ValueError):
|
|
mod.main(['delete', 'eDP-1'])
|
|
|
|
def test_hyprctl_no_shell(self):
|
|
with patch.object(mod.subprocess, 'run') as run:
|
|
mod.run_hypr('eDP-1,preferred,auto,1')
|
|
self.assertEqual(run.call_args.args[0], ['hyprctl','keyword','monitor','eDP-1,preferred,auto,1'])
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|