From 6f19923a918f7fb63a444b0c99950fdbe9d2f74c Mon Sep 17 00:00:00 2001 From: Christoph Berg Date: Tue, 8 Nov 2022 11:40:39 +0100 Subject: New upstream version 20221106+py3 --- tests/unit/base.py | 9 ++++ tests/unit/test_directory.py | 25 +++++++++ tests/unit/test_memedit_edits.py | 18 ++----- tests/unit/test_settings.py | 24 +++++++++ tests/unit/test_shiftdialog.py | 17 ++---- tests/unit/test_wxui_radiothread.py | 105 ++++++++++++++++++++++++++++++++++++ 6 files changed, 173 insertions(+), 25 deletions(-) create mode 100644 tests/unit/test_wxui_radiothread.py (limited to 'tests/unit') diff --git a/tests/unit/base.py b/tests/unit/base.py index 1e112ce..8b90a44 100644 --- a/tests/unit/base.py +++ b/tests/unit/base.py @@ -48,3 +48,12 @@ def mock_gtk(): def unmock_gtk(): for module in pygtk_mocks: del sys.modules[module] + + +class BaseGTKTest(BaseTest): + def setUp(self): + super(BaseGTKTest, self).setUp() + try: + import gtk + except ImportError: + self.skipTest('pygtk not available') diff --git a/tests/unit/test_directory.py b/tests/unit/test_directory.py index d48bb48..c447173 100644 --- a/tests/unit/test_directory.py +++ b/tests/unit/test_directory.py @@ -1,5 +1,7 @@ import base64 +import glob import json +import os import tempfile from tests.unit import base @@ -67,3 +69,26 @@ class TestDirectory(base.BaseTest): self.assertEqual('Barmaster 2000', radio.MODEL) self.assertEqual('A', radio.VARIANT) + +class TestDetectBruteForce(base.BaseTest): + def test_detect_all(self): + # Attempt a brute-force detection of all test images. + # + # This confirms that no test image is detected by more than one + # radio class. If it is, fail and report those classes. + + path = os.path.dirname(__file__) + path = os.path.join(path, '..', 'images', '*.img') + test_images = glob.glob(path) + self.assertNotEqual(0, len(test_images)) + for image in test_images: + detections = [] + filedata = open(image, 'rb').read() + for cls in directory.RADIO_TO_DRV: + if not hasattr(cls, 'match_model'): + continue + if cls.match_model(filedata, image): + detections.append(cls) + if len(detections) > 1: + raise Exception('Detection of %s failed: %s' % (image, + detections)) diff --git a/tests/unit/test_memedit_edits.py b/tests/unit/test_memedit_edits.py index fdf971c..b024353 100644 --- a/tests/unit/test_memedit_edits.py +++ b/tests/unit/test_memedit_edits.py @@ -6,21 +6,13 @@ from tests.unit import base __builtins__["_"] = lambda s: s -memedit = None - - -class TestEdits(base.BaseTest): - def setUp(self): - global memedit - super(TestEdits, self).setUp() - base.mock_gtk() - from chirp.ui import memedit as memedit_module - memedit = memedit_module +try: + from chirp.ui import memedit +except ImportError: + memedit = None - def tearDown(self): - super(TestEdits, self).tearDown() - base.unmock_gtk() +class TestEdits(base.BaseGTKTest): def _test_tone_column_change(self, col, ini_tmode='', ini_cmode='', exp_tmode=None, exp_cmode=None): diff --git a/tests/unit/test_settings.py b/tests/unit/test_settings.py index ccc4308..3fb84f0 100644 --- a/tests/unit/test_settings.py +++ b/tests/unit/test_settings.py @@ -54,6 +54,28 @@ class TestSettingValues(base.BaseTest): self._set_and_catch(value, "Jkl", "Xyz") self.assertEqual(value.get_options(), opts) + def test_radio_setting_value_list_by_index(self): + opts = ["Abc", "Def", "Ghi"] + value = settings.RadioSettingValueList(opts, current_index=1) + self.assertEqual(value.get_value(), "Def") + self.assertEqual(int(value), 1) + self._set_and_test(value, "Def", "Ghi", "Abc") + self._set_and_catch(value, "Jkl", "Xyz") + self.assertEqual(value.get_options(), opts) + + # Make sure we int() the index, as we may pass a bitwise object + value = settings.RadioSettingValueList(opts, current_index='1') + self.assertEqual(value.get_value(), "Def") + + value.set_index(0) + self.assertEqual(value.get_value(), 'Abc') + + # Same here, int() the index + value.set_index('0') + self.assertEqual(value.get_value(), 'Abc') + + self.assertRaises(IndexError, value.set_index, 7) + def test_radio_setting_value_string(self): value = settings.RadioSettingValueString(1, 5, "foo", autopad=False) self.assertEqual(value.get_value(), "foo") @@ -111,6 +133,8 @@ class TestSettingContainers(base.BaseTest): self.assertEqual(rs.value, val) rs.value = False self.assertEqual(val.get_value(), False) + self.assertEqual('foo:False', str(rs)) + self.assertEqual('[RadioSetting foo:False]', repr(rs)) def test_radio_setting_multi(self): val1 = settings.RadioSettingValueBoolean(True) diff --git a/tests/unit/test_shiftdialog.py b/tests/unit/test_shiftdialog.py index fe0b62d..227c1af 100644 --- a/tests/unit/test_shiftdialog.py +++ b/tests/unit/test_shiftdialog.py @@ -5,6 +5,10 @@ except ImportError: from mox3 import mox from tests.unit import base +try: + from chirp.ui import shiftdialog +except ImportError: + shiftdialog = None from chirp import chirp_common from chirp import errors @@ -50,18 +54,7 @@ class FakeRadioThread(object): pass -class ShiftDialogTest(base.BaseTest): - def setUp(self): - global shiftdialog - super(ShiftDialogTest, self).setUp() - base.mock_gtk() - from chirp.ui import shiftdialog as shiftdialog_module - shiftdialog = shiftdialog_module - - def tearDown(self): - super(ShiftDialogTest, self).tearDown() - base.unmock_gtk() - +class ShiftDialogTest(base.BaseGTKTest): def _test_hole(self, fn, starting, arg, expected): radio = FakeRadio(*tuple(starting)) radio.get_features().memory_bounds = (0, 5) diff --git a/tests/unit/test_wxui_radiothread.py b/tests/unit/test_wxui_radiothread.py new file mode 100644 index 0000000..c60da8c --- /dev/null +++ b/tests/unit/test_wxui_radiothread.py @@ -0,0 +1,105 @@ +import sys +import time +from unittest import mock + +sys.modules['wx'] = wx = mock.MagicMock() + +from tests.unit import base +from chirp.wxui import radiothread + + +class TestRadioThread(base.BaseTest): + def setUp(self): + super().setUp() + + def test_radiojob(self): + radio = mock.MagicMock() + editor = mock.MagicMock() + job = radiothread.RadioJob(editor, 'get_memory', [12], {}) + self.assertIsNone(job.dispatch(radio)) + radio.get_memory.assert_called_once_with(12) + self.assertEqual(job.result, radio.get_memory.return_value) + + def test_radiojob_exception(self): + radio = mock.MagicMock() + radio.get_memory.side_effect = ValueError('some error') + editor = mock.MagicMock() + job = radiothread.RadioJob(editor, 'get_memory', [12], {}) + self.assertIsNone(job.dispatch(radio)) + radio.get_memory.assert_called_once_with(12) + self.assertIsInstance(job.result, ValueError) + + def test_thread(self): + radio = mock.MagicMock() + radio.get_features.side_effect = ValueError('some error') + editor = mock.MagicMock() + # Simulate an edit conflict with the first event by returning + # False for "delivered" to force us to queue an event. + editor.radio_thread_event.side_effect = [False, True, True, True] + thread = radiothread.RadioThread(radio) + mem = mock.MagicMock() + job1id = thread.submit(editor, 'get_memory', 12) + job2id = thread.submit(editor, 'set_memory', mem) + job3id = thread.submit(editor, 'get_features') + # We have to start the thread after we submit the main jobs so + # the order is stable for comparison. + thread.start() + + # Wait for the main jobs to be processed before we signal exit + while not all([radio.get_memory.called, + radio.set_memory.called, + radio.get_features.called]): + time.sleep(0.1) + + thread.end() + thread.join(5) + self.assertFalse(thread.is_alive()) + radio.get_memory.assert_called_once_with(12) + radio.set_memory.assert_called_once_with(mem) + radio.get_features.assert_called_once_with() + self.assertEqual(4, editor.radio_thread_event.call_count) + + # We expect the jobs to be delivered in order of + # priority. Since we return False for the first call to + # radio_thread_event(), job2 should be queued and then + # delivered first on the next cycle. + expected_order = [job2id, job2id, job3id, job1id] + for i, (jobid, call) in enumerate( + zip(expected_order, + editor.radio_thread_event.call_args_list)): + job = call[0][0] + self.assertEqual(jobid, job.id) + + # We should call non-blocking for every call except the last + # one, when the queue is empty + editor.radio_thread_event.assert_has_calls([ + mock.call(mock.ANY, block=False), + mock.call(mock.ANY, block=False), + mock.call(mock.ANY, block=False), + mock.call(mock.ANY, block=True), + ]) + + def test_thread_abort_priority(self): + radio = mock.MagicMock() + radio.get_features.side_effect = ValueError('some error') + editor = mock.MagicMock() + thread = radiothread.RadioThread(radio) + mem = mock.MagicMock() + job1id = thread.submit(editor, 'get_memory', 12) + job2id = thread.submit(editor, 'set_memory', mem) + job3id = thread.submit(editor, 'get_features') + thread.end() + # We have to start the thread after we submit the main jobs so + # the order is stable for comparison. + thread.start() + + thread.join(5) + self.assertFalse(thread.is_alive()) + + # Our end sentinel should have gone to the head of the queue + # so that exiting the application does not leave a thread + # running in the background fetching hundreds of memories. + radio.get_memory.assert_not_called() + radio.set_memory.assert_not_called() + radio.get_features.assert_not_called() + wx.PostEvent.assert_not_called() -- cgit v1.2.3