Skip to content

Commit

Permalink
implemented basic test structure
Browse files Browse the repository at this point in the history
  • Loading branch information
cansik committed Feb 13, 2016
1 parent 0b312af commit cbd8100
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/colmark/routes/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@


@socketio.on('connect', namespace=DOCUMENT_NAMESPACE)
def mouse_connect():
def on_connect():
print('Client connected')


@socketio.on('disconnect', namespace=DOCUMENT_NAMESPACE)
def test_disconnect():
def on_disconnect():
print('Client disconnected')


Expand All @@ -20,6 +20,12 @@ def handle_message(message):
print('MSG: %s' % message)


@socketio.on('echo', namespace=DOCUMENT_NAMESPACE)
def handle_echo(message):
print('ECHO: %s' % message)
emit('echo', 'This was your message: %s' % message)


@socketio.on('add', namespace=DOCUMENT_NAMESPACE)
def handle_add(data):
print('Add: %s' % data)
Expand Down
Empty file added app/colmark/tests/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions app/colmark/tests/base_test_case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import unittest

__author__ = 'cansik'


class BaseTestCase(unittest.TestCase):

def setUp(self):
pass

def tearDown(self):
pass
41 changes: 41 additions & 0 deletions app/colmark/tests/document_sync_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import unittest

from socketIO_client import SocketIO, BaseNamespace, LoggingNamespace
from base_test_case import BaseTestCase
from colmark import config
from colmark.routes.document import DOCUMENT_NAMESPACE

__author__ = 'cansik'


class DocumentNamespace(LoggingNamespace):
_connected = True

def on_connect(self):
print('connected')

def on_error(self, data):
print('error')

def on_echo_response(self, *args):
print('echo_response', args)


class DocumentSyncTest(BaseTestCase):
def setUp(self):
super(DocumentSyncTest, self).setUp()
self.client = SocketIO('localhost', config.SERVER_PORT, DocumentNamespace)
self.doc = self.client.define(DocumentNamespace, DOCUMENT_NAMESPACE)

def tearDown(self):
super(DocumentSyncTest, self).tearDown()
self.client.disconnect()

def test_single_client(self):
self.doc.emit('echo', 'hello world')
print('sent echo')
self.client.wait(seconds=1)


if __name__ == '__main__':
unittest.main()
154 changes: 154 additions & 0 deletions input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<html>
<head>
<script>
onload = function () {
var inputBox = document.getElementById('myInput');
inputBox.oninput = function(e){
console.log('Input: ' + doGetCaretPosition(inputBox));
};

inputBox.onkeypress = function(e)
{
console.log('Pressed: ' + String.fromCharCode(e.keyCode));
};

inputBox.onpropertychange = inputBox.oninput; // for IE8
// e.onchange = e.oninput; // FF needs this in <select><option>...
// other things for onload()

var divInputBox = document.getElementById('myDivInput');

divInputBox.oninput = function(e){
//console.log('Input: ' + getCaretPosition(divInputBox));
};

divInputBox.onkeypress = function(e)
{
var pos = getCaretPosition(divInputBox);
var c = String.fromCharCode(e.keyCode);

//remove
if(e.keyCode != 13)
{
updateRemote(c, pos, false);
console.log('added "'+ c + '" at ' + getCaretPosition(divInputBox));
}
};

divInputBox.onkeyup = function(e)
{
var pos = getCaretPosition(divInputBox);

switch(e.keyCode)
{
//back
case 8:
console.log('removed key at ' + pos);
updateRemote('', pos, true);
break;

//tab
case 9:
console.log('added "tab" at ' + pos);
break;

//enter
case 13:
console.log('added "enter" at ' + pos);
break;
}
};

};

function updateRemote(v, pos, back)
{
var inputBox = document.getElementById('myInput');
var text = inputBox.value;
var l = text.length

var preText = text.substr(0, pos);
var postText = text.substr(pos, l);

var result = "";

if(back)
{
result = preText.substr(0, preText.length-1) + postText;
}
else{
result = preText + v + postText;
}
inputBox.value = result;
}

/*
** Returns the caret (cursor) position of the specified text field.
** Return value range is 0-oField.value.length.
*/
function doGetCaretPosition (oField) {

// Initialize
var iCaretPos = 0;

// IE Support
if (document.selection) {

// Set focus on the element
oField.focus();

// To get cursor position, get empty selection range
var oSel = document.selection.createRange();

// Move selection start to 0 position
oSel.moveStart('character', -oField.value.length);

// The caret position is selection length
iCaretPos = oSel.text.length;
}

// Firefox support
else if (oField.selectionStart || oField.selectionStart == '0')
iCaretPos = oField.selectionStart;

// Return results
return iCaretPos;
}

function getCaretPosition(editableDiv) {
var caretPos = 0,
sel, range;
if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount) {
range = sel.getRangeAt(0);
if (range.commonAncestorContainer.parentNode == editableDiv) {
caretPos = range.endOffset;
}
}
} else if (document.selection && document.selection.createRange) {
range = document.selection.createRange();
if (range.parentElement() == editableDiv) {
var tempEl = document.createElement("span");
editableDiv.insertBefore(tempEl, editableDiv.firstChild);
var tempRange = range.duplicate();
tempRange.moveToElementText(tempEl);
tempRange.setEndPoint("EndToEnd", range);
caretPos = tempRange.text.length;
}
}
return caretPos;
}
</script>
</head>

<body>
<input type=text id=myInput value="Hello World"/>
<br />
<hr />
<br />
<div id=myDivInput onClick="this.contentEditable='true';" tabindex="1">
Hello World
</div>
</body>
</html>

0 comments on commit cbd8100

Please sign in to comment.