def test_browse_not_serving(): """ If the user attempts to open their browser at their website, but the local web server isn't running, then display a friendly message explaining so. """ editor = mock.MagicMock() view = mock.MagicMock() wm = WebMode(editor, view) wm.runner = None with mock.patch("mu.modes.web.webbrowser") as mock_browser: wm.browse(None) assert mock_browser.open.call_count == 0 assert view.show_message.call_count == 1
def test_browse(): """ The user's default web browser is opened to the correct URL for the root endpoint of the site served from localhost. """ editor = mock.MagicMock() view = mock.MagicMock() wm = WebMode(editor, view) wm.runner = True with mock.patch("mu.modes.web.webbrowser") as mock_browser: wm.browse(None) expected = "http://127.0.0.1:5000/" mock_browser.open.assert_called_once_with(expected)
def test_stop_server(): """ Stopping the server will send SIGINT to the child process and remove the Python runner pane from the UI. """ editor = mock.MagicMock() view = mock.MagicMock() wm = WebMode(editor, view) mock_runner = mock.MagicMock() wm.runner = mock_runner wm.stop_server() mock_runner.stop_process.assert_called_once_with() assert wm.runner is None view.remove_python_runner.assert_called_once_with()
def test_stop_server(): """ Stopping the server will send SIGINT to the child process and remove the Python runner pane from the UI. """ editor = mock.MagicMock() view = mock.MagicMock() wm = WebMode(editor, view) mock_runner = mock.MagicMock() mock_runner.process.processId.return_value = 666 # ;-) wm.runner = mock_runner with mock.patch("os.kill") as mock_kill: wm.stop_server() mock_kill.assert_called_once_with(666, signal.SIGINT) mock_runner.process.waitForFinished.assert_called_once_with() assert wm.runner is None view.remove_python_runner.assert_called_once_with()
def test_run_toggle_off(): """ Check the handler for toggling the local server stops the process and reverts the UI state. """ editor = mock.MagicMock() view = mock.MagicMock() wm = WebMode(editor, view) wm.runner = True wm.stop_server = mock.MagicMock() wm.set_buttons = mock.MagicMock() wm.run_toggle(None) wm.stop_server.assert_called_once_with() slot = wm.view.button_bar.slots["play"] assert slot.setIcon.call_count == 1 slot.setText.assert_called_once_with("Run") slot.setToolTip.assert_called_once_with("Run the web server.") wm.set_buttons.assert_called_once_with(modes=True)
def test_stop_server_with_error(): """ If killing the server's child process encounters a problem (perhaps the process is already dead), then log this and tidy up. """ editor = mock.MagicMock() view = mock.MagicMock() wm = WebMode(editor, view) mock_runner = mock.MagicMock() mock_runner.process.processId.return_value = 666 # ;-) wm.runner = mock_runner with mock.patch("os.kill", side_effect=Exception("Bang")) as mock_kill, \ mock.patch("mu.modes.web.logger.error") as mock_log: wm.stop_server() mock_kill.assert_called_once_with(666, signal.SIGINT) assert mock_log.call_count == 2 mock_runner.process.waitForFinished.assert_called_once_with() assert wm.runner is None view.remove_python_runner.assert_called_once_with()
def test_run_toggle_on(): """ Check the handler for running the local server starts the sub-process and updates the UI state. """ editor = mock.MagicMock() view = mock.MagicMock() wm = WebMode(editor, view) wm.runner = None def runner(wm=wm): wm.runner = True wm.start_server = mock.MagicMock(side_effect=runner) wm.set_buttons = mock.MagicMock() wm.run_toggle(None) wm.start_server.assert_called_once_with() slot = wm.view.button_bar.slots["run"] assert slot.setIcon.call_count == 1 slot.setText.assert_called_once_with("Stop") slot.setToolTip.assert_called_once_with("Stop the web server.") wm.set_buttons.assert_called_once_with(modes=False)