def build(self): """Initializes the user interface and starts timed events.""" #TODO: Reorganize and consolidate; make necessary helper functions self.title = 'Open Lighting Architecture' self.ui_queue = Queue() self.layout = BoxLayout(orientation='vertical') self.selected_universe_service = UniverseSelectedService() self.ola_listener = OLAListener(self.ui_queue, self.create_select_server, self.create_ola_client, self.start_ola, self.stop_ola) #Screen creation and layout placing self.screen_tabs = ScreenTabs() self.monitor_screen = MonitorScreen(self.ola_listener, self.selected_universe_service, name='DMX Monitor') self.console_screen = ConsoleScreen(self.ola_listener, self.selected_universe_service, name='DMX Console') self.devsets = MainScreen(self.ola_listener, self.selected_universe_service, name='Device Settings') self.screen_tabs.ids.monitor_screen.add_widget(self.monitor_screen) self.screen_tabs.ids.console_screen.add_widget(self.console_screen) self.screen_tabs.ids.settings_screen.add_widget(self.devsets) self.layout.add_widget(self.screen_tabs) Clock.schedule_interval(lambda dt: self.display_tasks(), self.EVENT_POLL_INTERVAL) Clock.schedule_interval(self._update_clock, 1 / 60.) return self.layout
def setUp(self): """Creates an OLAListener object with proper mock values""" self.ui_queue = Queue() self.on_start = MagicMock() self.on_stop = MagicMock() self.ola_listener = OLAListener( self.ui_queue, self.create_mock_select_server, self.create_mock_ola_client, self.on_start, self.on_stop ) self.ola_listener.start() time.sleep(0.1) # Give the OLAListenerThread time to initialize
class RPiUI(App): """Class for drawing and handling the Kivy application itself.""" EVENT_POLL_INTERVAL = 1 / 20 UNIVERSE_POLL_INTERVAL = 1 / 10 index = NumericProperty(-1) time = NumericProperty(0) current_title = StringProperty() screen_names = ListProperty([]) def build(self): """Initializes the user interface and starts timed events.""" #TODO: Reorganize and consolidate; make necessary helper functions self.title = 'Open Lighting Architecture' self.ui_queue = Queue() self.layout = BoxLayout(orientation='vertical') self.selected_universe_service = UniverseSelectedService() self.ola_listener = OLAListener(self.ui_queue, self.create_select_server, self.create_ola_client, self.start_ola, self.stop_ola) #Screen creation and layout placing self.screen_tabs = ScreenTabs() self.monitor_screen = MonitorScreen(self.ola_listener, self.selected_universe_service, name='DMX Monitor') self.console_screen = ConsoleScreen(self.ola_listener, self.selected_universe_service, name='DMX Console') self.devsets = MainScreen(self.ola_listener, self.selected_universe_service, name='Device Settings') self.screen_tabs.ids.monitor_screen.add_widget(self.monitor_screen) self.screen_tabs.ids.console_screen.add_widget(self.console_screen) self.screen_tabs.ids.settings_screen.add_widget(self.devsets) self.layout.add_widget(self.screen_tabs) Clock.schedule_interval(lambda dt: self.display_tasks(), self.EVENT_POLL_INTERVAL) Clock.schedule_interval(self._update_clock, 1 / 60.) return self.layout def on_start(self): """Executed after build()""" signal(SIGINT, self.stop) #Captures ctrl-c to exit correctly self.ola_listener.start() def on_stop(self): """Executed when the application quits""" self.ola_listener.stop() def on_pause(self): """Pausing is not allowed; the application will close instead""" return False def on_resume(self): """Because pausing is not allowed, nothing to do here""" pass @staticmethod def create_select_server(): return SelectServer() @staticmethod def create_ola_client(): return OlaClient() def start_ola(self): """Executed when OLAD starts, enables proper UI actions""" self.devsets.start_ola(self.UNIVERSE_POLL_INTERVAL) def stop_ola(self): """Executed when OLAD stops, disables proper UI actions""" self.devsets.stop_ola() def display_tasks(self): """Polls for events that need to update the UI, then updates the UI accordingly. """ try: event = self.ui_queue.get(False) event.run() except Empty: pass def _update_clock(self, dt): self.time = time() def olad_listen(self, dt): self.ola_listener.listen(self)
class TestOLAListener(unittest.TestCase): """Tests the system which gets requests from the UI and evaluates them using a selectserver. """ def setUp(self): """Creates an OLAListener object with proper mock values""" self.ui_queue = Queue() self.on_start = MagicMock() self.on_stop = MagicMock() self.ola_listener = OLAListener( self.ui_queue, self.create_mock_select_server, self.create_mock_ola_client, self.on_start, self.on_stop ) self.ola_listener.start() time.sleep(0.1) # Give the OLAListenerThread time to initialize @staticmethod def create_mock_select_server(): return MockSelectServer() @staticmethod def create_mock_ola_client(): return MockOlaClient() def clear_ui_queue(self): """Executes every UIEvent in the UI Queue, then terminates""" try: event = self.ui_queue.get(True, 0.2) event.run() self.clear_ui_queue() except Empty: pass def test_pull_universes(self): """Tests the OLAListener's pull_universes method""" self.callback_executed = False def callback(status, universes): self.assertEqual(universes[0].id, 123) # Mock values self.assertEqual(universes[0].name, "Test Universe") self.callback_executed = True self.ola_listener.pull_universes(callback) self.clear_ui_queue() self.assertTrue(self.callback_executed) def test_pull_devices(self): """Tests the OLAListener's pull_devices method""" self.callback_executed = False def callback(status, devices): self.assertEqual(devices[0].id, 123) # Mock values self.assertEqual(devices[0].name, "Test Device") self.callback_executed = True self.ola_listener.pull_devices(callback) self.clear_ui_queue() self.assertTrue(self.callback_executed) def test_patch(self): """Tests the OLAListener's patch method. Right now, just ensures callback is called, more to be added. """ self.callback_executed = False def callback(status): self.callback_executed = True self.ola_listener.patch(1, 1, False, 2, "Test Patch", callback) self.clear_ui_queue() self.assertTrue(self.callback_executed) def test_unpatch(self): """Tests the OLALstener's unpatch method. The request to unpatch universe 50 ensures the callback should be called, as there is a port in the dummy olaclient that has this universe number assigned. """ self.callback_executed = False def callback(status): self.callback_executed = True self.ola_listener.unpatch(50, callback) self.clear_ui_queue() self.assertTrue(self.callback_executed) def test_fetch_dmx(self): """Tests the OLAListener's fetch_dmx method""" self.callback_executed = False def callback(status, universe, data): self.assertEqual(data[3], 3) self.callback_executed = True self.ola_listener.fetch_dmx(1, callback) self.clear_ui_queue() self.assertTrue(self.callback_executed) def test_start_dmx_listener(self): """Tests the OLAListener's start_dmx_listener method""" self.callback_executed = False def callback(status): self.callback_executed = True self.ola_listener.start_dmx_listener(1, None, callback) self.clear_ui_queue() self.assertTrue(self.callback_executed) def test_stop_dmx_listener(self): """Tests the OLAListener's stop_dmx_listener method""" self.callback_executed = False def callback(status): self.callback_executed = True self.ola_listener.stop_dmx_listener(1, None, callback) self.clear_ui_queue() self.assertTrue(self.callback_executed) def test_send_dmx(self): """Tests the OLAListener's stop_dmx method""" self.callback_executed = False def callback(status): self.callback_executed = True self.ola_listener.send_dmx(1, [0, 1, 2, 3], callback) self.clear_ui_queue() self.assertTrue(self.callback_executed)