Skip to content

Commit 72564b2

Browse files
author
Ian Prest
committed
Testing: Added a very simple serialization test
-- Modified serial.js a little so it could be loaded by the test harness -- Removed dependency on some Angular/JQuery helper functions
1 parent d0329db commit 72564b2

File tree

4 files changed

+60
-23
lines changed

4 files changed

+60
-23
lines changed

render.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var $renderKey = {};
1+
var $renderKey = (typeof(exports) !== 'undefined') ? exports : {};
22
(function () {
33
"use strict";
44

serial.js

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,84 @@
1-
var $serial = {};
1+
var $serial = (typeof(exports) !== 'undefined') ? exports : {};
22
(function () {
33
"use strict";
44

55
// We need this so we can test locally and still save layouts to AWS
66
$serial.base_href = "http://www.keyboard-layout-editor.com";
77

8+
// Helper to copy an object; doesn't handle loops/circular refs, etc.
9+
function copy(o) {
10+
if (typeof(o) !== 'object') {
11+
// primitive value
12+
return o;
13+
} else if (o instanceof Array) {
14+
// array
15+
var result = [];
16+
for (var i = 0; i < o.length; i++) {
17+
result[i] = copy(o[i]);
18+
}
19+
return result;
20+
} else {
21+
// Object
22+
var result = {};
23+
for (var prop in o) {
24+
result[prop] = copy(o[prop]);
25+
}
26+
return result;
27+
}
28+
}
29+
function isEmptyObject(o) {
30+
for(var prop in o)
31+
return false;
32+
return true;
33+
}
34+
835
// Lenient JSON reader/writer
936
$serial.toJsonL = function(obj) {
1037
var res = [], key;
1138
if(obj instanceof Array) {
1239
obj.forEach(function(elem) { res.push($serial.toJsonL(elem)); });
1340
return '['+res.join(',')+']';
14-
}
41+
}
1542
if(typeof obj === 'object') {
1643
for(key in obj) { if(obj.hasOwnProperty(key)) { res.push(key+':'+$serial.toJsonL(obj[key])); } }
1744
return '{'+res.join(',')+'}';
1845
}
1946
if(typeof obj === 'number') {
2047
return Math.round10(obj,-4);
2148
}
22-
return angular.toJson(obj);
49+
return angular.toJson(obj);
2350
};
2451
$serial.fromJsonL = function(json) { return jsonl.parse(json); };
2552

2653
// function to sort the key array
2754
$serial.sortKeys = function(keys) {
2855
keys.sort(function(a,b) {
29-
return ((a.rotation_angle+360)%360 - (b.rotation_angle+360)%360) ||
30-
(a.rotation_x - b.rotation_x) ||
31-
(a.rotation_y - b.rotation_y) ||
32-
(a.y - b.y) ||
33-
(a.x - b.x);
56+
return ((a.rotation_angle+360)%360 - (b.rotation_angle+360)%360) ||
57+
(a.rotation_x - b.rotation_x) ||
58+
(a.rotation_y - b.rotation_y) ||
59+
(a.y - b.y) ||
60+
(a.x - b.x);
3461
});
3562
};
3663

3764
var _defaultKeyProps = {
3865
x: 0, y: 0, x2: 0, y2: 0, // position
3966
width: 1, height: 1, width2: 1, height2: 1, // size
4067
color: "#cccccc", text: ["#000000"], // colors
41-
labels:[], align: 4, fontheight: 3, fontheight2: 3, // label properties
68+
labels:[], align: 4, fontheight: 3, fontheight2: 3, // label properties
4269
rotation_angle: 0, rotation_x: 0, rotation_y: 0, // rotation
4370
profile: "", nub: false, ghost: false, stepped: false // misc
4471
};
4572
var _defaultMetaData = { backcolor: '#eeeeee', name: '', author: '', notes: '' };
46-
$serial.defaultKeyProps = function() { return angular.copy(_defaultKeyProps); };
47-
$serial.defaultMetaData = function() { return angular.copy(_defaultMetaData); };
48-
73+
$serial.defaultKeyProps = function() { return copy(_defaultKeyProps); };
74+
$serial.defaultMetaData = function() { return copy(_defaultMetaData); };
75+
4976
// Convert between our in-memory format & our serialized format
5077
function serializeProp(props, nname, val, defval) { if(val !== defval) { props[nname] = val; } return val; }
5178
$serial.serialize = function(keyboard) {
5279
var keys = keyboard.keys;
5380
var rows = [], row = [];
54-
var current = $serial.defaultKeyProps();
81+
var current = $serial.defaultKeyProps();
5582
current.text = current.text[0];
5683
var cluster = {r:0, rx:0, ry:0};
5784

@@ -60,7 +87,7 @@ var $serial = {};
6087
for(var metakey in keyboard.meta) {
6188
serializeProp(meta, metakey, keyboard.meta[metakey], _defaultMetaData[metakey]);
6289
}
63-
if(!$.isEmptyObject(meta)) {
90+
if(!isEmptyObject(meta)) {
6491
rows.push(meta);
6592
}
6693

@@ -76,7 +103,7 @@ var $serial = {};
76103
// start a new row when necessary
77104
var clusterChanged = (key.rotation_angle != cluster.r || key.rotation_x != cluster.rx || key.rotation_y != cluster.ry);
78105
var rowChanged = (key.y !== current.y);
79-
if(row.length>0 && (rowChanged || clusterChanged)) {
106+
if(row.length>0 && (rowChanged || clusterChanged)) {
80107
// Push the old row
81108
rows.push(row);
82109
row = [];
@@ -85,11 +112,11 @@ var $serial = {};
85112

86113
if(newRow) {
87114
// Set up for the new row
88-
current.y++;
115+
current.y++;
89116

90117
// 'y' is reset if *either* 'rx' or 'ry' are changed
91118
if(key.rotation_y != cluster.ry || key.rotation_x != cluster.rx)
92-
current.y = key.rotation_y;
119+
current.y = key.rotation_y;
93120
current.x = key.rotation_x; // always reset x to rx (which defaults to zero)
94121

95122
// Update current cluster
@@ -154,7 +181,7 @@ var $serial = {};
154181
for(var k = 0; k < rows[r].length; ++k) {
155182
var key = rows[r][k];
156183
if(typeof key === 'string') {
157-
var newKey = angular.copy(current);
184+
var newKey = copy(current);
158185
newKey.width2 = newKey.width2 === 0 ? current.width : current.width2;
159186
newKey.height2 = newKey.height2 === 0 ? current.height : current.height2;
160187
newKey.labels = key.split('\n');
@@ -199,7 +226,7 @@ var $serial = {};
199226
}
200227
return { meta:meta, keys:keys };
201228
}
202-
229+
203230
$serial.saveLayout = function($http, layout, success, error) {
204231
var data = angular.toJson(layout);
205232
var fn = CryptoJS.MD5(data).toString();
@@ -221,7 +248,7 @@ var $serial = {};
221248
transformRequest: angular.identity
222249
}).success(function() { success(fn); }).error(function(data, status) {
223250
if(status == 0) {
224-
// We seem to get a 'cancelled' notification even though the POST
251+
// We seem to get a 'cancelled' notification even though the POST
225252
// is successful, so we have to double-check.
226253
$http.get($serial.base_href+"/layouts/"+fn).success(function() { success(fn); }).error(error);
227254
} else {
@@ -230,5 +257,4 @@ var $serial = {};
230257
});
231258
});
232259
};
233-
234260
}());

tests/conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
exports.config = {
22
seleniumAddress: 'http://localhost:4444/wd/hub',
3-
specs: ['kb-spec.js']
3+
specs: ['kb-spec.js', 'kb-serial.js']
44
};

tests/kb-serial.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
$serial = require('../serial.js');
2+
3+
describe('keyboard serialization', function() {
4+
5+
it('should handle empty keyboard', function() {
6+
var original = [];
7+
var kbd = $serial.deserialize(original);
8+
expect($serial.serialize(kbd)).toEqual(original);
9+
});
10+
11+
});

0 commit comments

Comments
 (0)