Commit d332588e authored by carlosperate's avatar carlosperate

Refactored the requesthandler to separate handler from actions and gui methods.

Unit test for the gui and actions still under work.
parent dda4a15b
This diff is collapsed.
# -*- coding: utf-8 -*-
#
# Receives and responds to the HTTP request from the Python server.
#
# Copyright (c) 2015 carlosperate https://github.com/carlosperate/
# Licensed under the Apache License, Version 2.0 (the "License"):
# http://www.apache.org/licenses/LICENSE-2.0
#
from __future__ import unicode_literals, absolute_import
try:
# 2.x name
import Tkinter
import tkFileDialog
except ImportError:
# 3.x name
import tkinter as Tkinter
import tkinter.filedialog as tkFileDialog
#
# Dealing with Directories and files
#
def browse_file_dialog():
"""
Opens a file browser and selects executable files
:return: Full path to selected file
"""
root = Tkinter.Tk()
# Make window almost invisible to focus it and ensure directory browser
# doesn't end up loading in the background behind main window.
root.withdraw()
root.overrideredirect(True)
root.geometry('0x0+0+0')
root.deiconify()
root.lift()
root.focus_force()
root.update()
file_path = tkFileDialog.askopenfilename()
root.destroy()
return file_path
def browse_dir_dialog():
"""
Opens a directory browser to select a folder.
:return: Full path to the selected folder
"""
root = Tkinter.Tk()
# Make window almost invisible to focus it and ensure directory browser
# doesn't end up loading in the background behind main window.
root.withdraw()
root.overrideredirect(True)
root.geometry('0x0+0+0')
root.deiconify()
root.lift()
root.focus_force()
file_path = tkFileDialog.askdirectory(
parent=root, initialdir="/", title='Please select a directory')
root.destroy()
return file_path
This diff is collapsed.
# -*- coding: utf-8 -*-
#
# Starts an HTTP server.
#
# Copyright (c) 2015 carlosperate https://github.com/carlosperate/
# Licensed under the Apache License, Version 2.0 (the "License"):
# http://www.apache.org/licenses/LICENSE-2.0
#
from __future__ import unicode_literals, absolute_import
import os
try:
......@@ -7,7 +15,7 @@ except ImportError:
# 3.x name
import http.server as BaseHTTPServer
import ardublocklyserver.requesthandler
from ardublocklyserver.requesthandler import BlocklyRequestHandler
ADDRESS = '0.0.0.0'
PORT = 8000
......@@ -18,9 +26,7 @@ def start_server(document_root):
print('Setting HTTP Server Document Root to: \n\t' + document_root + "\n")
os.chdir(document_root)
server_address = (ADDRESS, PORT)
server = BaseHTTPServer.HTTPServer(
server_address,
ardublocklyserver.requesthandler.BlocklyRequestHandler)
server = BaseHTTPServer.HTTPServer(server_address, BlocklyRequestHandler)
print('Launching the HTTP service...')
server.serve_forever()
print('The Server closed unexpectedly!!')
......
# -*- coding: utf-8 -*-
#
# Unit test for the gui module.
#
# Copyright (c) 2015 carlosperate https://github.com/carlosperate/
# Licensed under the Apache License, Version 2.0 (the "License"):
# http://www.apache.org/licenses/LICENSE-2.0
#
from __future__ import unicode_literals, absolute_import
import os
import unittest
import mock
try:
import ardublocklyserver.gui as gui
except ImportError:
import sys
file_dir = os.path.dirname(os.path.realpath(__file__))
package_dir = os.path.dirname(os.path.dirname(file_dir))
sys.path.insert(0, package_dir)
import ardublocklyserver.gui as gui
class GuiTestCase(unittest.TestCase):
"""
Tests for gu i module
"""
#
# Tests for checking browsing for paths and files
#
@mock.patch('ardublocklyserver.gui.tkFileDialog')
def test_browse_file(self, mock_file_select):
test_file = 'test_file'
mock_file_select.askopenfilename.return_value = test_file
new_file = gui.browse_file_dialog()
self.assertEqual(new_file, test_file)
def test_browse_file_cancel(self):
canceled_file = ''
print('A file browser window will open, to successfully run this test '
'press cancel or close the window!!!\n')
#raw_input('Press "Enter" to continue...')
function_file = gui.browse_file_dialog()
self.assertEqual(canceled_file, function_file)
@mock.patch('ardublocklyserver.gui.tkFileDialog')
def test_browse_path(self, mock_path_select):
test_path = 'test_path'
mock_path_select.askopenfilename.return_value = test_path
new_path = gui.browse_file_dialog()
self.assertEqual(new_path, test_path)
def test_browse_path_cancel(self):
canceled_path = ''
print('A path browser window will open, to successfully run this test '
'press cancel or close the window!!!\n')
#raw_input('Press "Enter" to continue...')
function_path = gui.browse_dir_dialog()
self.assertEqual(canceled_path, function_path)
if __name__ == '__main__':
unittest.main()
......@@ -26,67 +26,8 @@ class BlocklyRequestHandlerTestCase(unittest.TestCase):
Tests for BlocklyRequestHandler module
"""
#
# Command line tests
#
@mock.patch('ardublocklyserver.requesthandler.subprocess.Popen')
@mock.patch('ardublocklyserver.requesthandler.create_sketch_default')
@mock.patch.object(
requesthandler.ServerCompilerSettings, 'get_compiler_dir',
autospec=True)
def test_command_line_launch(self, mock_settings, mock_sketch, mock_popen):
"""
Tests that a compiler path and arduino sketch path can be set
and that a command line can be launched to open the sketch in the
Arduino IDE.
"""
#TODO: This test is outdated and needs to be rewritten
# Set the compiler settings
test_sketch_path = os.path.join(os.getcwd(), 'sketch.ino')
mock_sketch.return_value = test_sketch_path
test_compiler_dir = os.path.join(os.getcwd(), 'arduino.exe')
mock_settings = requesthandler.ServerCompilerSettings()
mock_settings.__compiler_dir = test_compiler_dir
requesthandler.ServerCompilerSettings().launch_IDE_only = True
# Build expected string and run test
expected_command = test_compiler_dir + ' "' + test_sketch_path + '"'
requesthandler.load_arduino_cli()
#mock_popen.system.assert_called_with(expected_command)
#
# Tests for checking browsing for paths and files
#
@mock.patch('ardublocklyserver.requesthandler.tkFileDialog')
def test_browse_file(self, mock_file_select):
test_file = 'test_file'
mock_file_select.askopenfilename.return_value = test_file
new_file = requesthandler.browse_file()
self.assertEqual(new_file, test_file)
def test_browse_file_cancel(self):
canceled_file = ''
print('A file browser window will open, to successfully run this test '
'press cancel or close the window!!!\n')
#raw_input('Press "Enter" to continue...')
function_file = requesthandler.browse_file()
self.assertEqual(canceled_file, function_file)
@mock.patch('ardublocklyserver.requesthandler.tkFileDialog')
def test_browse_path(self, mock_path_select):
test_path = 'test_path'
mock_path_select.askopenfilename.return_value = test_path
new_path = requesthandler.browse_file()
self.assertEqual(new_path, test_path)
def test_browse_path_cancel(self):
canceled_path = ''
print('A path browser window will open, to successfully run this test '
'press cancel or close the window!!!\n')
#raw_input('Press "Enter" to continue...')
function_path = requesthandler.browse_dir()
self.assertEqual(canceled_path, function_path)
def test_todo(self):
pass
if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment