def test_clear(self): push_events('device 1', [ new_event(EV_KEY, CODE_1, 1), new_event(EV_KEY, CODE_2, 1), new_event(EV_KEY, CODE_3, 1) ] * 15) self.create_helper() reader.start_reading('device 1') time.sleep(START_READING_DELAY + EVENT_READ_TIMEOUT * 3) reader.read() self.assertEqual(len(reader._unreleased), 3) self.assertIsNotNone(reader.previous_event) self.assertIsNotNone(reader.previous_result) # make the helper send more events to the reader time.sleep(EVENT_READ_TIMEOUT * 2) self.assertTrue(reader._results.poll()) reader.clear() self.assertFalse(reader._results.poll()) self.assertEqual(reader.read(), None) self.assertEqual(len(reader._unreleased), 0) self.assertIsNone(reader.get_unreleased_keys()) self.assertIsNone(reader.previous_event) self.assertIsNone(reader.previous_result) self.tearDown()
def test_change_device(self): push_events('device 1', [ new_event(EV_KEY, 1, 1), ] * 100) push_events('device 2', [ new_event(EV_KEY, 2, 1), ] * 100) self.create_helper() reader.start_reading('device 1') time.sleep(0.1) self.assertEqual(reader.read(), Key(EV_KEY, 1, 1)) reader.start_reading('device 2') # it's plausible that right after sending the new read command more # events from the old device might still appear. Give the helper # some time to handle the new command. time.sleep(0.1) reader.clear() time.sleep(0.1) self.assertEqual(reader.read(), Key(EV_KEY, 2, 1))
def test_terminate(self): self.create_helper() reader.start_reading('device 1') push_events('device 1', [new_event(EV_KEY, CODE_3, 1)]) time.sleep(START_READING_DELAY + EVENT_READ_TIMEOUT) self.assertTrue(reader._results.poll()) reader.terminate() reader.clear() time.sleep(EVENT_READ_TIMEOUT) # no new events arrive after terminating push_events('device 1', [new_event(EV_KEY, CODE_3, 1)]) time.sleep(EVENT_READ_TIMEOUT * 3) self.assertFalse(reader._results.poll())
def test_push_events(self): """Test that push_event works properly between helper and reader. Using push_events after the helper is already forked should work, as well as using push_event twice """ def create_helper(): # this will cause pending events to be copied over to the helper # process def start_helper(): helper = RootHelper() helper.run() self.helper = multiprocessing.Process(target=start_helper) self.helper.start() time.sleep(0.1) def wait_for_results(): # wait for the helper to send stuff for _ in range(10): time.sleep(EVENT_READ_TIMEOUT) if reader._results.poll(): break event = new_event(EV_KEY, 102, 1) create_helper() reader.start_reading(groups.find(key='Foo Device 2')) time.sleep(START_READING_DELAY) push_events('Foo Device 2', [event]) wait_for_results() self.assertTrue(reader._results.poll()) reader.clear() self.assertFalse(reader._results.poll()) # can push more events to the helper that is inside a separate # process, which end up being sent to the reader push_events('Foo Device 2', [event]) wait_for_results() self.assertTrue(reader._results.poll())
def on_keycode_input_focus(self, *_): """Refresh useful usage information.""" reader.clear() self.show_press_key() self.window.can_modify_mapping()
def quick_cleanup(log=True): """Reset the applications state.""" if log: print('quick cleanup') for device in list(pending_events.keys()): try: while pending_events[device][1].poll(): pending_events[device][1].recv() except (UnpicklingError, EOFError): pass # setup new pipes for the next test pending_events[device] = None setup_pipe(device) try: reader.terminate() except (BrokenPipeError, OSError): pass if asyncio.get_event_loop().is_running(): for task in asyncio.all_tasks(): task.cancel() if not macro_variables.process.is_alive(): raise AssertionError('the SharedDict manager is not running anymore') macro_variables._stop() join_children() macro_variables._start() if os.path.exists(tmp): shutil.rmtree(tmp) config.path = os.path.join(get_config_path(), 'config.json') config.clear_config() config.save_config() system_mapping.populate() custom_mapping.empty() custom_mapping.clear_config() custom_mapping.changed = False clear_write_history() for name in list(uinputs.keys()): del uinputs[name] for device in list(active_macros.keys()): del active_macros[device] for device in list(unreleased.keys()): del unreleased[device] for path in list(fixtures.keys()): if path not in _fixture_copy: del fixtures[path] for path in list(_fixture_copy.keys()): fixtures[path] = copy.deepcopy(_fixture_copy[path]) os.environ.update(environ_copy) for device in list(os.environ.keys()): if device not in environ_copy: del os.environ[device] reader.clear() for _, pipe in pending_events.values(): assert not pipe.poll() assert macro_variables.is_alive(1)