2018-12-30 00:25:09 -04:00
|
|
|
"""Test config_key, coverage 98%.
|
|
|
|
|
|
|
|
Coverage is effectively 100%. Tkinter dialog is mocked, Mac-only line
|
|
|
|
may be skipped, and dummy function in bind test should not be called.
|
2021-09-21 20:09:00 -03:00
|
|
|
Not tested: exit with 'self.advanced or self.keys_ok(keys) ...' False.
|
2018-12-30 00:25:09 -04:00
|
|
|
"""
|
2016-06-10 19:19:21 -03:00
|
|
|
|
|
|
|
from idlelib import config_key
|
|
|
|
from test.support import requires
|
|
|
|
import unittest
|
2018-12-30 00:25:09 -04:00
|
|
|
from unittest import mock
|
|
|
|
from tkinter import Tk, TclError
|
2017-06-26 01:55:48 -03:00
|
|
|
from idlelib.idle_test.mock_idle import Func
|
2018-11-17 02:38:01 -04:00
|
|
|
from idlelib.idle_test.mock_tk import Mbox_func
|
2016-06-10 19:19:21 -03:00
|
|
|
|
|
|
|
|
2017-06-26 01:55:48 -03:00
|
|
|
class ValidationTest(unittest.TestCase):
|
2018-12-27 23:47:54 -04:00
|
|
|
"Test validation methods: ok, keys_ok, bind_ok."
|
2017-06-26 01:55:48 -03:00
|
|
|
|
2022-09-30 13:44:44 -03:00
|
|
|
class Validator(config_key.GetKeysFrame):
|
2017-06-26 01:55:48 -03:00
|
|
|
def __init__(self, *args, **kwargs):
|
2022-09-30 13:44:44 -03:00
|
|
|
super().__init__(*args, **kwargs)
|
2018-12-27 23:47:54 -04:00
|
|
|
class list_keys_final:
|
2017-06-26 01:55:48 -03:00
|
|
|
get = Func()
|
2018-12-27 23:47:54 -04:00
|
|
|
self.list_keys_final = list_keys_final
|
|
|
|
get_modifiers = Func()
|
2017-06-26 01:55:48 -03:00
|
|
|
showerror = Mbox_func()
|
2016-06-10 19:19:21 -03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2017-06-26 01:55:48 -03:00
|
|
|
requires('gui')
|
2016-06-10 19:19:21 -03:00
|
|
|
cls.root = Tk()
|
2016-08-31 22:09:02 -03:00
|
|
|
cls.root.withdraw()
|
2017-06-27 02:23:55 -03:00
|
|
|
keylist = [['<Key-F12>'], ['<Control-Key-x>', '<Control-Key-X>']]
|
2022-09-30 13:44:44 -03:00
|
|
|
cls.dialog = cls.Validator(cls.root, '<<Test>>', keylist)
|
2016-06-10 19:19:21 -03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2022-09-30 13:44:44 -03:00
|
|
|
del cls.dialog
|
2017-06-26 01:55:48 -03:00
|
|
|
cls.root.update_idletasks()
|
2016-06-10 19:19:21 -03:00
|
|
|
cls.root.destroy()
|
2022-09-30 13:44:44 -03:00
|
|
|
del cls.root
|
2017-06-26 01:55:48 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.dialog.showerror.message = ''
|
|
|
|
# A test that needs a particular final key value should set it.
|
|
|
|
# A test that sets a non-blank modifier list should reset it to [].
|
|
|
|
|
|
|
|
def test_ok_empty(self):
|
2018-12-27 23:47:54 -04:00
|
|
|
self.dialog.key_string.set(' ')
|
|
|
|
self.dialog.ok()
|
2017-06-26 01:55:48 -03:00
|
|
|
self.assertEqual(self.dialog.result, '')
|
|
|
|
self.assertEqual(self.dialog.showerror.message, 'No key specified.')
|
|
|
|
|
|
|
|
def test_ok_good(self):
|
2018-12-27 23:47:54 -04:00
|
|
|
self.dialog.key_string.set('<Key-F11>')
|
|
|
|
self.dialog.list_keys_final.get.result = 'F11'
|
|
|
|
self.dialog.ok()
|
2017-06-26 01:55:48 -03:00
|
|
|
self.assertEqual(self.dialog.result, '<Key-F11>')
|
|
|
|
self.assertEqual(self.dialog.showerror.message, '')
|
|
|
|
|
|
|
|
def test_keys_no_ending(self):
|
2018-12-27 23:47:54 -04:00
|
|
|
self.assertFalse(self.dialog.keys_ok('<Control-Shift'))
|
2017-06-26 01:55:48 -03:00
|
|
|
self.assertIn('Missing the final', self.dialog.showerror.message)
|
|
|
|
|
|
|
|
def test_keys_no_modifier_bad(self):
|
2018-12-27 23:47:54 -04:00
|
|
|
self.dialog.list_keys_final.get.result = 'A'
|
|
|
|
self.assertFalse(self.dialog.keys_ok('<Key-A>'))
|
2017-06-26 01:55:48 -03:00
|
|
|
self.assertIn('No modifier', self.dialog.showerror.message)
|
|
|
|
|
|
|
|
def test_keys_no_modifier_ok(self):
|
2018-12-27 23:47:54 -04:00
|
|
|
self.dialog.list_keys_final.get.result = 'F11'
|
|
|
|
self.assertTrue(self.dialog.keys_ok('<Key-F11>'))
|
2017-06-26 01:55:48 -03:00
|
|
|
self.assertEqual(self.dialog.showerror.message, '')
|
|
|
|
|
|
|
|
def test_keys_shift_bad(self):
|
2018-12-27 23:47:54 -04:00
|
|
|
self.dialog.list_keys_final.get.result = 'a'
|
|
|
|
self.dialog.get_modifiers.result = ['Shift']
|
|
|
|
self.assertFalse(self.dialog.keys_ok('<a>'))
|
2017-06-26 01:55:48 -03:00
|
|
|
self.assertIn('shift modifier', self.dialog.showerror.message)
|
2018-12-27 23:47:54 -04:00
|
|
|
self.dialog.get_modifiers.result = []
|
2017-06-26 01:55:48 -03:00
|
|
|
|
|
|
|
def test_keys_dup(self):
|
2017-06-27 02:23:55 -03:00
|
|
|
for mods, final, seq in (([], 'F12', '<Key-F12>'),
|
|
|
|
(['Control'], 'x', '<Control-Key-x>'),
|
|
|
|
(['Control'], 'X', '<Control-Key-X>')):
|
|
|
|
with self.subTest(m=mods, f=final, s=seq):
|
2018-12-27 23:47:54 -04:00
|
|
|
self.dialog.list_keys_final.get.result = final
|
|
|
|
self.dialog.get_modifiers.result = mods
|
|
|
|
self.assertFalse(self.dialog.keys_ok(seq))
|
2017-06-27 02:23:55 -03:00
|
|
|
self.assertIn('already in use', self.dialog.showerror.message)
|
2018-12-27 23:47:54 -04:00
|
|
|
self.dialog.get_modifiers.result = []
|
2016-06-10 19:19:21 -03:00
|
|
|
|
2017-06-26 01:55:48 -03:00
|
|
|
def test_bind_ok(self):
|
|
|
|
self.assertTrue(self.dialog.bind_ok('<Control-Shift-Key-a>'))
|
|
|
|
self.assertEqual(self.dialog.showerror.message, '')
|
2016-06-10 19:19:21 -03:00
|
|
|
|
2017-06-26 01:55:48 -03:00
|
|
|
def test_bind_not_ok(self):
|
|
|
|
self.assertFalse(self.dialog.bind_ok('<Control-Shift>'))
|
|
|
|
self.assertIn('not accepted', self.dialog.showerror.message)
|
2016-06-10 19:19:21 -03:00
|
|
|
|
|
|
|
|
2018-12-30 00:25:09 -04:00
|
|
|
class ToggleLevelTest(unittest.TestCase):
|
|
|
|
"Test toggle between Basic and Advanced frames."
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
requires('gui')
|
|
|
|
cls.root = Tk()
|
|
|
|
cls.root.withdraw()
|
2022-09-30 13:44:44 -03:00
|
|
|
cls.dialog = config_key.GetKeysFrame(cls.root, '<<Test>>', [])
|
2018-12-30 00:25:09 -04:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2022-09-30 13:44:44 -03:00
|
|
|
del cls.dialog
|
2018-12-30 00:25:09 -04:00
|
|
|
cls.root.update_idletasks()
|
|
|
|
cls.root.destroy()
|
2022-09-30 13:44:44 -03:00
|
|
|
del cls.root
|
2018-12-30 00:25:09 -04:00
|
|
|
|
|
|
|
def test_toggle_level(self):
|
|
|
|
dialog = self.dialog
|
|
|
|
|
|
|
|
def stackorder():
|
|
|
|
"""Get the stack order of the children of the frame.
|
|
|
|
|
|
|
|
winfo_children() stores the children in stack order, so
|
|
|
|
this can be used to check whether a frame is above or
|
|
|
|
below another one.
|
|
|
|
"""
|
2022-09-30 13:44:44 -03:00
|
|
|
for index, child in enumerate(dialog.winfo_children()):
|
2018-12-30 00:25:09 -04:00
|
|
|
if child._name == 'keyseq_basic':
|
|
|
|
basic = index
|
|
|
|
if child._name == 'keyseq_advanced':
|
|
|
|
advanced = index
|
|
|
|
return basic, advanced
|
|
|
|
|
|
|
|
# New window starts at basic level.
|
|
|
|
self.assertFalse(dialog.advanced)
|
|
|
|
self.assertIn('Advanced', dialog.button_level['text'])
|
|
|
|
basic, advanced = stackorder()
|
|
|
|
self.assertGreater(basic, advanced)
|
|
|
|
|
|
|
|
# Toggle to advanced.
|
|
|
|
dialog.toggle_level()
|
|
|
|
self.assertTrue(dialog.advanced)
|
|
|
|
self.assertIn('Basic', dialog.button_level['text'])
|
|
|
|
basic, advanced = stackorder()
|
|
|
|
self.assertGreater(advanced, basic)
|
|
|
|
|
|
|
|
# Toggle to basic.
|
|
|
|
dialog.button_level.invoke()
|
|
|
|
self.assertFalse(dialog.advanced)
|
|
|
|
self.assertIn('Advanced', dialog.button_level['text'])
|
|
|
|
basic, advanced = stackorder()
|
|
|
|
self.assertGreater(basic, advanced)
|
|
|
|
|
|
|
|
|
|
|
|
class KeySelectionTest(unittest.TestCase):
|
|
|
|
"Test selecting key on Basic frames."
|
|
|
|
|
2022-09-30 13:44:44 -03:00
|
|
|
class Basic(config_key.GetKeysFrame):
|
2018-12-30 00:25:09 -04:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
class list_keys_final:
|
|
|
|
get = Func()
|
|
|
|
select_clear = Func()
|
|
|
|
yview = Func()
|
|
|
|
self.list_keys_final = list_keys_final
|
|
|
|
def set_modifiers_for_platform(self):
|
|
|
|
self.modifiers = ['foo', 'bar', 'BAZ']
|
|
|
|
self.modifier_label = {'BAZ': 'ZZZ'}
|
|
|
|
showerror = Mbox_func()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
requires('gui')
|
|
|
|
cls.root = Tk()
|
|
|
|
cls.root.withdraw()
|
2022-09-30 13:44:44 -03:00
|
|
|
cls.dialog = cls.Basic(cls.root, '<<Test>>', [])
|
2018-12-30 00:25:09 -04:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2022-09-30 13:44:44 -03:00
|
|
|
del cls.dialog
|
2018-12-30 00:25:09 -04:00
|
|
|
cls.root.update_idletasks()
|
|
|
|
cls.root.destroy()
|
2022-09-30 13:44:44 -03:00
|
|
|
del cls.root
|
2018-12-30 00:25:09 -04:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.dialog.clear_key_seq()
|
|
|
|
|
|
|
|
def test_get_modifiers(self):
|
|
|
|
dialog = self.dialog
|
|
|
|
gm = dialog.get_modifiers
|
|
|
|
eq = self.assertEqual
|
|
|
|
|
2018-12-30 15:48:51 -04:00
|
|
|
# Modifiers are set on/off by invoking the checkbutton.
|
|
|
|
dialog.modifier_checkbuttons['foo'].invoke()
|
2018-12-30 00:25:09 -04:00
|
|
|
eq(gm(), ['foo'])
|
|
|
|
|
2018-12-30 15:48:51 -04:00
|
|
|
dialog.modifier_checkbuttons['BAZ'].invoke()
|
2018-12-30 00:25:09 -04:00
|
|
|
eq(gm(), ['foo', 'BAZ'])
|
|
|
|
|
2018-12-30 15:48:51 -04:00
|
|
|
dialog.modifier_checkbuttons['foo'].invoke()
|
2018-12-30 00:25:09 -04:00
|
|
|
eq(gm(), ['BAZ'])
|
|
|
|
|
2022-09-30 13:44:44 -03:00
|
|
|
@mock.patch.object(config_key.GetKeysFrame, 'get_modifiers')
|
2018-12-30 00:25:09 -04:00
|
|
|
def test_build_key_string(self, mock_modifiers):
|
|
|
|
dialog = self.dialog
|
|
|
|
key = dialog.list_keys_final
|
|
|
|
string = dialog.key_string.get
|
|
|
|
eq = self.assertEqual
|
|
|
|
|
|
|
|
key.get.result = 'a'
|
|
|
|
mock_modifiers.return_value = []
|
|
|
|
dialog.build_key_string()
|
|
|
|
eq(string(), '<Key-a>')
|
|
|
|
|
|
|
|
mock_modifiers.return_value = ['mymod']
|
|
|
|
dialog.build_key_string()
|
|
|
|
eq(string(), '<mymod-Key-a>')
|
|
|
|
|
|
|
|
key.get.result = ''
|
|
|
|
mock_modifiers.return_value = ['mymod', 'test']
|
|
|
|
dialog.build_key_string()
|
|
|
|
eq(string(), '<mymod-test>')
|
|
|
|
|
2022-09-30 13:44:44 -03:00
|
|
|
@mock.patch.object(config_key.GetKeysFrame, 'get_modifiers')
|
2018-12-30 00:25:09 -04:00
|
|
|
def test_final_key_selected(self, mock_modifiers):
|
|
|
|
dialog = self.dialog
|
|
|
|
key = dialog.list_keys_final
|
|
|
|
string = dialog.key_string.get
|
|
|
|
eq = self.assertEqual
|
|
|
|
|
|
|
|
mock_modifiers.return_value = ['Shift']
|
|
|
|
key.get.result = '{'
|
|
|
|
dialog.final_key_selected()
|
|
|
|
eq(string(), '<Shift-Key-braceleft>')
|
|
|
|
|
|
|
|
|
2022-09-30 13:44:44 -03:00
|
|
|
class CancelWindowTest(unittest.TestCase):
|
2018-12-30 00:25:09 -04:00
|
|
|
"Simulate user clicking [Cancel] button."
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
requires('gui')
|
|
|
|
cls.root = Tk()
|
|
|
|
cls.root.withdraw()
|
2022-09-30 13:44:44 -03:00
|
|
|
cls.dialog = config_key.GetKeysWindow(
|
|
|
|
cls.root, 'Title', '<<Test>>', [], _utest=True)
|
2018-12-30 00:25:09 -04:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
cls.dialog.cancel()
|
2022-09-30 13:44:44 -03:00
|
|
|
del cls.dialog
|
2018-12-30 00:25:09 -04:00
|
|
|
cls.root.update_idletasks()
|
|
|
|
cls.root.destroy()
|
2022-09-30 13:44:44 -03:00
|
|
|
del cls.root
|
2018-12-30 00:25:09 -04:00
|
|
|
|
2022-09-30 13:44:44 -03:00
|
|
|
@mock.patch.object(config_key.GetKeysFrame, 'ok')
|
|
|
|
def test_cancel(self, mock_frame_ok):
|
2018-12-30 00:25:09 -04:00
|
|
|
self.assertEqual(self.dialog.winfo_class(), 'Toplevel')
|
|
|
|
self.dialog.button_cancel.invoke()
|
|
|
|
with self.assertRaises(TclError):
|
|
|
|
self.dialog.winfo_class()
|
|
|
|
self.assertEqual(self.dialog.result, '')
|
2022-09-30 13:44:44 -03:00
|
|
|
mock_frame_ok.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
|
class OKWindowTest(unittest.TestCase):
|
|
|
|
"Simulate user clicking [OK] button."
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
requires('gui')
|
|
|
|
cls.root = Tk()
|
|
|
|
cls.root.withdraw()
|
|
|
|
cls.dialog = config_key.GetKeysWindow(
|
|
|
|
cls.root, 'Title', '<<Test>>', [], _utest=True)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
cls.dialog.cancel()
|
|
|
|
del cls.dialog
|
|
|
|
cls.root.update_idletasks()
|
|
|
|
cls.root.destroy()
|
|
|
|
del cls.root
|
|
|
|
|
|
|
|
@mock.patch.object(config_key.GetKeysFrame, 'ok')
|
|
|
|
def test_ok(self, mock_frame_ok):
|
|
|
|
self.assertEqual(self.dialog.winfo_class(), 'Toplevel')
|
|
|
|
self.dialog.button_ok.invoke()
|
|
|
|
with self.assertRaises(TclError):
|
|
|
|
self.dialog.winfo_class()
|
|
|
|
mock_frame_ok.assert_called()
|
|
|
|
|
|
|
|
|
|
|
|
class WindowResultTest(unittest.TestCase):
|
|
|
|
"Test window result get and set."
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
requires('gui')
|
|
|
|
cls.root = Tk()
|
|
|
|
cls.root.withdraw()
|
|
|
|
cls.dialog = config_key.GetKeysWindow(
|
|
|
|
cls.root, 'Title', '<<Test>>', [], _utest=True)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
cls.dialog.cancel()
|
|
|
|
del cls.dialog
|
|
|
|
cls.root.update_idletasks()
|
|
|
|
cls.root.destroy()
|
|
|
|
del cls.root
|
|
|
|
|
|
|
|
def test_result(self):
|
|
|
|
dialog = self.dialog
|
|
|
|
eq = self.assertEqual
|
|
|
|
|
|
|
|
dialog.result = ''
|
|
|
|
eq(dialog.result, '')
|
|
|
|
eq(dialog.frame.result,'')
|
|
|
|
|
|
|
|
dialog.result = 'bar'
|
|
|
|
eq(dialog.result,'bar')
|
|
|
|
eq(dialog.frame.result,'bar')
|
|
|
|
|
|
|
|
dialog.frame.result = 'foo'
|
|
|
|
eq(dialog.result, 'foo')
|
|
|
|
eq(dialog.frame.result,'foo')
|
2018-12-30 00:25:09 -04:00
|
|
|
|
|
|
|
|
2018-12-31 16:06:35 -04:00
|
|
|
class HelperTest(unittest.TestCase):
|
|
|
|
"Test module level helper functions."
|
|
|
|
|
|
|
|
def test_translate_key(self):
|
|
|
|
tr = config_key.translate_key
|
|
|
|
eq = self.assertEqual
|
|
|
|
|
|
|
|
# Letters return unchanged with no 'Shift'.
|
|
|
|
eq(tr('q', []), 'Key-q')
|
|
|
|
eq(tr('q', ['Control', 'Alt']), 'Key-q')
|
|
|
|
|
|
|
|
# 'Shift' uppercases single lowercase letters.
|
|
|
|
eq(tr('q', ['Shift']), 'Key-Q')
|
|
|
|
eq(tr('q', ['Control', 'Shift']), 'Key-Q')
|
|
|
|
eq(tr('q', ['Control', 'Alt', 'Shift']), 'Key-Q')
|
|
|
|
|
|
|
|
# Convert key name to keysym.
|
|
|
|
eq(tr('Page Up', []), 'Key-Prior')
|
|
|
|
# 'Shift' doesn't change case when it's not a single char.
|
|
|
|
eq(tr('*', ['Shift']), 'Key-asterisk')
|
|
|
|
|
|
|
|
|
2016-06-10 19:19:21 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(verbosity=2)
|