/
EnactorTests.js
575 lines (507 loc) · 23.1 KB
/
EnactorTests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
Copyright 2013 OCAD University
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
// Declare dependencies
/*global fluid, jqUnit, expect, jQuery*/
// JSLint options
/*jslint white: true, funcinvoke: true, undef: true, newcap: true, nomen: true, regexp: true, bitwise: true, browser: true, forin: true, maxerr: 100, indent: 4 */
(function ($) {
fluid.registerNamespace("fluid.tests");
fluid.tests.testStyle = function (that, container, expectedDefaultFlag, expectedCssClass) {
var elements = that.getElements();
jqUnit.assertEquals("Default value: " + expectedDefaultFlag, expectedDefaultFlag, that.model.value);
jqUnit.assertEquals("Default css class: " + expectedCssClass, expectedCssClass, that.options.cssClass);
jqUnit.assertEquals("Default - css class is not applied", undefined, elements.attr("class"));
that.applier.requestChange("value", true);
jqUnit.assertEquals("True value - Css class has been applied", expectedCssClass, elements.attr("class"));
that.applier.requestChange("value", false);
jqUnit.assertEquals("False value - Css class has been removed", "", elements.attr("class"));
};
/*******************************************************************************
* Unit tests for fluid.uiOptions.styleElements
*******************************************************************************/
fluid.defaults("fluid.tests.styleElementsTests", {
gradeNames: ["fluid.test.testEnvironment", "autoInit"],
expectedCssClass: "fl-style-test",
expectedDefaultFlag: false,
container: ".flc-styleElements",
components: {
styleElements: {
type: "fluid.uiOptions.enactor.styleElements",
options: {
cssClass: "fl-style-test",
invokers: {
getElements: {
funcName: "fluid.tests.getElements",
args: ".flc-styleElements"
}
}
}
},
styleElementsTester: {
type: "fluid.tests.styleElementsTester"
}
}
});
fluid.tests.getElements = function (container) {
return $(container).children();
};
fluid.defaults("fluid.tests.styleElementsTester", {
gradeNames: ["fluid.test.testCaseHolder", "autoInit"],
modules: [{
name: "Test style element component",
tests: [{
expect: 5,
name: "Apply and reset the style",
type: "test",
func: "fluid.tests.testStyle",
args: ["{styleElements}", "{fluid.tests.styleElementsTests}.options.container", "{fluid.tests.styleElementsTests}.options.expectedDefaultFlag", "{fluid.tests.styleElementsTests}.options.expectedCssClass"]
}]
}]
});
/*******************************************************************************
* Unit tests for fluid.uiOptions.enactor.emphasizeLinks
*******************************************************************************/
fluid.defaults("fluid.tests.emphasizeLinksTests", {
gradeNames: ["fluid.test.testEnvironment", "autoInit"],
container: ".flc-emphasizeLinks",
expectedEmphasizeLinksClass: "fl-emphasizeLinks-test",
expectedDefaultFlag: false,
components: {
emphasizeLinks: {
type: "fluid.uiOptions.enactor.emphasizeLinks",
container: ".flc-emphasizeLinks",
options: {
cssClass: "fl-emphasizeLinks-test"
}
},
emphasizeLinksTester: {
type: "fluid.tests.emphasizeLinksTester"
}
}
});
fluid.defaults("fluid.tests.emphasizeLinksTester", {
gradeNames: ["fluid.test.testCaseHolder", "autoInit"],
modules: [{
name: "Test emphasize links enactor",
tests: [{
expect: 5,
name: "Apply and reset emphasized links",
type: "test",
func: "fluid.tests.testStyle",
args: ["{emphasizeLinks}", "{fluid.tests.emphasizeLinksTests}.options.container", "{fluid.tests.emphasizeLinksTests}.options.expectedDefaultFlag", "{fluid.tests.emphasizeLinksTests}.options.expectedEmphasizeLinksClass"]
}]
}]
});
/*******************************************************************************
* Unit tests for fluid.uiOptions.enactor.inputsLarger
*******************************************************************************/
fluid.defaults("fluid.tests.inputsLargerTests", {
gradeNames: ["fluid.test.testEnvironment", "autoInit"],
container: ".flc-inputsLarger",
expectedDefaultFlag: false,
expectedInputsLargerClass: "fl-text-larger",
components: {
inputsLarger: {
type: "fluid.uiOptions.enactor.inputsLarger",
container: ".flc-inputsLarger",
options: {
cssClass: "fl-text-larger"
}
},
inputsLargerTester: {
type: "fluid.tests.inputsLargerTester"
}
}
});
fluid.defaults("fluid.tests.inputsLargerTester", {
gradeNames: ["fluid.test.testCaseHolder", "autoInit"],
modules: [{
name: "Test inputs larger enactor",
tests: [{
expect: 5,
name: "Apply and reset larger inputs",
type: "test",
func: "fluid.tests.testStyle",
args: ["{inputsLarger}", "{fluid.tests.inputsLargerTests}.options.container", "{fluid.tests.inputsLargerTests}.options.expectedDefaultFlag", "{fluid.tests.inputsLargerTests}.options.expectedInputsLargerClass"]
}]
}]
});
/*******************************************************************************
* Unit tests for fluid.uiOptions.enactor.inputsLarger
*******************************************************************************/
fluid.defaults("fluid.tests.classSwapperTests", {
gradeNames: ["fluid.test.testEnvironment", "autoInit"],
expectedClass: "fl-test",
components: {
classSwapper: {
type: "fluid.uiOptions.enactor.classSwapper",
container: ".flc-classSwapper",
options: {
classes: {
"default": "",
"test": "fl-test"
}
}
},
classSwapperTester: {
type: "fluid.tests.classSwapperTester"
}
}
});
fluid.tests.testClassSwapper = function (that, expectedClass) {
var defaultStyle = that.container.attr("class");
jqUnit.assertEquals("The style class is not applied by default", defaultStyle, that.container.attr("class"));
that.applier.requestChange("value", "test");
jqUnit.assertEquals("The style class has been applied", defaultStyle + " " + expectedClass, that.container.attr("class"));
that.applier.requestChange("value", "");
jqUnit.assertEquals("The style class has been removed", defaultStyle, that.container.attr("class"));
};
fluid.defaults("fluid.tests.classSwapperTester", {
gradeNames: ["fluid.test.testCaseHolder", "autoInit"],
modules: [{
name: "Test class swapper enactor",
tests: [{
expect: 3,
name: "Swap css class",
type: "test",
func: "fluid.tests.testClassSwapper",
args: ["{classSwapper}", "{fluid.tests.classSwapperTests}.options.expectedClass"]
}]
}]
});
/*******************************************************************************
* Unit tests for getPx2EmFactor & getTextSizeInEm
*******************************************************************************/
var fontSizeMap = {
"xx-small": "9px",
"x-small": "11px",
"small": "13px",
"medium": "15px",
"large": "18px",
"x-large": "23px",
"xx-large": "30px"
};
fluid.defaults("fluid.tests.getSizeTests", {
gradeNames: ["fluid.test.testEnvironment", "autoInit"],
container: ".flc-getSize",
fontSizeMap: fontSizeMap,
expectedTestSize: 8,
expectedSizeAtUndetected: 1,
components: {
getSizeTester: {
type: "fluid.tests.getSizeTester"
}
}
});
fluid.tests.testGetSize = function (container, fontSizeMap, expectedTestSize, expectedSizeAtUndetected) {
container = $(container);
var px2emFactor = fluid.uiOptions.enactor.textSizer.getPx2EmFactor(container, fontSizeMap);
jqUnit.assertEquals("Check that the factor is pulled from the container correctly", expectedTestSize, px2emFactor);
container = $("html");
var textSizeInPx = fluid.uiOptions.enactor.getTextSizeInPx(container, fontSizeMap);
px2emFactor = fluid.uiOptions.enactor.textSizer.getPx2EmFactor(container, fontSizeMap);
var fontSizeInEm = fluid.uiOptions.enactor.textSizer.getTextSizeInEm(textSizeInPx, px2emFactor);
jqUnit.assertEquals("Unable to detect the text size in em for the DOM root element <html>. Always return 1em.", expectedSizeAtUndetected, fontSizeInEm);
};
fluid.defaults("fluid.tests.getSizeTester", {
gradeNames: ["fluid.test.testCaseHolder", "autoInit"],
modules: [{
name: "Test getPx2EmFactor & getTextSizeInEm",
tests: [{
expect: 2,
name: "Get text size in em",
type: "test",
func: "fluid.tests.testGetSize",
args: ["{fluid.tests.getSizeTests}.options.container", "{fluid.tests.getSizeTests}.options.fontSizeMap", "{fluid.tests.getSizeTests}.options.expectedTestSize", "{fluid.tests.getSizeTests}.options.expectedSizeAtUndetected"]
}]
}]
});
/*******************************************************************************
* Unit tests for fluid.uiOptions.enactor.textSizer
*******************************************************************************/
fluid.defaults("fluid.tests.textSizerTests", {
gradeNames: ["fluid.test.testEnvironment", "autoInit"],
components: {
textSizer: {
type: "fluid.uiOptions.enactor.textSizer",
container: ".flc-textSizer",
options: {
fontSizeMap: fontSizeMap
}
},
textSizerTester: {
type: "fluid.tests.textSizerTester"
}
}
});
fluid.tests.testTextSizer = function (that) {
var px2emFactor = fluid.uiOptions.enactor.textSizer.getPx2EmFactor(that.container, that.options.fontSizeMap);
var expectedInitialSize = Math.round(8 / px2emFactor * 10000) / 10000;
jqUnit.assertEquals("Check that the size is pulled from the container correctly", expectedInitialSize, that.initialSize);
that.applier.requestChange("value", 2);
jqUnit.assertEquals("The size should be doubled", "16px", that.container.css("fontSize"));
};
fluid.defaults("fluid.tests.textSizerTester", {
gradeNames: ["fluid.test.testCaseHolder", "autoInit"],
modules: [{
name: "Test text sizer enactor",
tests: [{
expect: 2,
name: "Apply text size in times",
type: "test",
func: "fluid.tests.testTextSizer",
args: ["{textSizer}"]
}]
}]
});
/*******************************************************************************
* Unit tests for getLineHeight & numerizeLineHeight
*******************************************************************************/
fluid.defaults("fluid.tests.getLineHeightTests", {
gradeNames: ["fluid.test.testEnvironment", "autoInit"],
container: ".flc-lineSpacer",
fontSizeMap: fontSizeMap,
expectedTestSize: 8,
expectedSizeAtUndetected: 1,
components: {
getLineHeightTester: {
type: "fluid.tests.getLineHeightTester"
}
}
});
fluid.tests.testGetLineHeight = function () {
// Mimic IE with its DOM lineHeight structure
var container = [{currentStyle: {lineHeight: "10"}}];
var lineHeight = fluid.uiOptions.enactor.lineSpacer.getLineHeight(container);
jqUnit.assertEquals("getLineHeight with IE simulation", "10", lineHeight);
container = [{currentStyle: {lineHeight: "14pt"}}];
lineHeight = fluid.uiOptions.enactor.lineSpacer.getLineHeight(container);
jqUnit.assertEquals("getLineHeight with IE simulation", "14pt", lineHeight);
container = $(".flc-lineSpacer");
lineHeight = fluid.uiOptions.enactor.lineSpacer.getLineHeight(container);
jqUnit.assertEquals("getLineHeight without IE simulation", "12px", lineHeight);
};
var testNumerizeLineHeight = function (lineHeight, expected) {
var container = $(".flc-lineSpacer");
var fontSize = fluid.uiOptions.enactor.getTextSizeInPx(container, fontSizeMap);
var numerizedLineHeight = fluid.uiOptions.enactor.lineSpacer.numerizeLineHeight(lineHeight, Math.round(fontSize));
jqUnit.assertEquals("line-height value '" + lineHeight + "' has been converted correctly", expected, numerizedLineHeight);
};
fluid.tests.testNumerizeLineHeight = function () {
var undefinedLineHeight;
testNumerizeLineHeight(undefinedLineHeight, 0);
testNumerizeLineHeight("normal", 1.2);
testNumerizeLineHeight("6px", 1);
testNumerizeLineHeight("1.5", 1.5);
};
fluid.defaults("fluid.tests.getLineHeightTester", {
gradeNames: ["fluid.test.testCaseHolder", "autoInit"],
modules: [{
name: "Test getLineHeight",
tests: [{
expect: 3,
name: "Get line height",
type: "test",
func: "fluid.tests.testGetLineHeight"
}]
}, {
name: "Test getLineHeight",
tests: [{
expect: 4,
name: "Get numerized line height",
type: "test",
func: "fluid.tests.testNumerizeLineHeight"
}]
}]
});
/*******************************************************************************
* Unit tests for fluid.uiOptions.enactor.lineSpacer
*******************************************************************************/
fluid.defaults("fluid.tests.lineSpacerTests", {
gradeNames: ["fluid.test.testEnvironment", "autoInit"],
components: {
lineSpacer: {
type: "fluid.uiOptions.enactor.lineSpacer",
container: ".flc-lineSpacer",
options: {
fontSizeMap: fontSizeMap
}
},
lineSpacerTester: {
type: "fluid.tests.lineSpacerTester"
}
}
});
// This is necessary to work around IE differences in handling line-height unitless factors
var convertLineHeightFactor = function (lineHeight, fontSize) {
// Continuing the work-around of jQuery + IE bug - http://bugs.jquery.com/ticket/2671
if (lineHeight.match(/[0-9]$/)) {
return Math.round(lineHeight * parseFloat(fontSize)) + "px";
} else {
return lineHeight;
}
};
fluid.tests.testLineSpacer = function (that) {
jqUnit.assertEquals("Check that the size is pulled from the container correctly", 2, Math.round(that.initialSize));
jqUnit.assertEquals("Check the line spacing size in pixels", "12px", convertLineHeightFactor(that.container.css("lineHeight"), that.container.css("fontSize")));
that.applier.requestChange("value", 2);
jqUnit.assertEquals("The size should be doubled", "24px", convertLineHeightFactor(that.container.css("lineHeight"), that.container.css("fontSize")));
};
fluid.defaults("fluid.tests.lineSpacerTester", {
gradeNames: ["fluid.test.testCaseHolder", "autoInit"],
modules: [{
name: "Test line spacer enactor",
tests: [{
expect: 3,
name: "Apply line spacing in times",
type: "test",
func: "fluid.tests.testLineSpacer",
args: ["{lineSpacer}"]
}]
}]
});
/*******************************************************************************
* Unit tests for fluid.uiOptions.enactor.tableOfContents
*******************************************************************************/
fluid.defaults("fluid.tests.tableOfContentsTests", {
gradeNames: ["fluid.test.testEnvironment", "autoInit"],
components: {
toc: {
type: "fluid.uiOptions.enactor.tableOfContentsEnactor",
container: ".flc-tableOfContents",
options: {
tocTemplate: "../../../../components/tableOfContents/html/TableOfContents.html"
}
},
tableOfContentsTester: {
type: "fluid.tests.tableOfContentsTester"
}
}
});
fluid.tests.checkTocLevels = function (that, expectedTocLevels) {
jqUnit.assertEquals("Table of contents has " + expectedTocLevels + " levels", expectedTocLevels, $(".flc-toc-tocContainer").children("ul").length);
};
fluid.tests.makeTocVisibilityChecker = function (that, expectedTocLevels, tocContainer, isShown) {
return function () {
jqUnit.assertEquals("Table of contents has " + expectedTocLevels + " levels", expectedTocLevels, $(".flc-toc-tocContainer").children("ul").length);
jqUnit.assertEquals("The visibility of the table of contents is " + isShown, isShown, $(tocContainer).is(":visible"));
};
};
fluid.defaults("fluid.tests.tableOfContentsTester", {
gradeNames: ["fluid.test.testCaseHolder", "autoInit"],
testOptions: {
trueValue: true,
falseValue: false,
tocContainer: ".flc-toc-tocContainer",
expectedNoTocLevels: 0,
expectedTocLevelsAtTrue: 1
},
modules: [{
name: "Test table of contents enactor",
tests: [{
expect: 5,
name: "Test in order: default, toc is on, toc is off",
sequence: [{
func: "fluid.tests.checkTocLevels",
args: ["{toc}", "{that}.options.testOptions.expectedNoTocLevels"]
}, {
func: "{toc}.applier.requestChange",
args: ["value", "{that}.options.testOptions.trueValue"]
}, {
listenerMaker: "fluid.tests.makeTocVisibilityChecker",
makerArgs: ["{toc}", "{that}.options.testOptions.expectedTocLevelsAtTrue", "{that}.options.testOptions.tocContainer", true],
event: "{toc}.events.afterTocRender"
}, {
func: "{toc}.applier.requestChange",
args: ["value", "{that}.options.testOptions.falseValue"]
}, {
listenerMaker: "fluid.tests.makeTocVisibilityChecker",
makerArgs: ["{toc}", "{that}.options.testOptions.expectedTocLevelsAtTrue", "{that}.options.testOptions.tocContainer", false],
event: "{toc}.events.onLateRefreshRelay"
}]
}]
}]
});
/*******************************************************************************
* Unit tests for fluid.uiOptions.IE6ColorInversion
*******************************************************************************/
fluid.defaults("fluid.tests.IE6ColorInversionTests", {
gradeNames: ["fluid.test.testEnvironment", "autoInit"],
components: {
setIE6ColorInversion: {
type: "fluid.uiOptions.enactor.IE6ColorInversion",
container: ".flc-IE6ColorInversion"
},
IE6ColorInversionTester: {
type: "fluid.tests.IE6ColorInversionTester"
}
}
});
function cleanStaticEnvironment() {
delete fluid.staticEnvironment.browserIE;
delete fluid.staticEnvironment.browserMajorVersion;
}
function withIE6Environment(withIt, testFunc) {
try {
cleanStaticEnvironment();
if (withIt) {
fluid.staticEnvironment.browserIE = fluid.typeTag("fluid.browser.msie");
fluid.staticEnvironment.browserMajorVersion = fluid.typeTag("fluid.browser.majorVersion.6");
}
testFunc();
} finally {
cleanStaticEnvironment();
}
}
function testIE6ColorInversion(withIt, testFunc) {
withIE6Environment(withIt, function () {
testFunc();
});
}
fluid.tests.testIE6ColorInversion = function (that, container) {
testIE6ColorInversion(false, function () {
that.applier.requestChange("value", "default");
jqUnit.assertEquals("fl-inverted-color is not touched", 1, $(container).has("." + that.options.styles.colorInversionClass).length);
});
testIE6ColorInversion(true, function () {
that.applier.requestChange("value", "default");
jqUnit.assertEquals("fl-inverted-color has been removed", 0, $(container).has("." + that.options.styles.colorInversionClass).length);
});
};
fluid.defaults("fluid.tests.IE6ColorInversionTester", {
gradeNames: ["fluid.test.testCaseHolder", "autoInit"],
testOptions: {
container: ".flc-IE6ColorInversion"
},
modules: [{
name: "Test style element component",
tests: [{
expect: 2,
name: "IE6 style is handled properly",
type: "test",
func: "fluid.tests.testIE6ColorInversion",
args: ["{setIE6ColorInversion}", "{that}.options.testOptions.container"]
}]
}]
});
$(document).ready(function () {
fluid.test.runTests([
"fluid.tests.styleElementsTests",
"fluid.tests.emphasizeLinksTests",
"fluid.tests.inputsLargerTests",
"fluid.tests.classSwapperTests",
"fluid.tests.getSizeTests",
"fluid.tests.textSizerTests",
"fluid.tests.getLineHeightTests",
"fluid.tests.lineSpacerTests",
"fluid.tests.tableOfContentsTests",
"fluid.tests.IE6ColorInversionTests"
]);
});
})(jQuery);