1 | /* global document */
|
2 | /*
|
3 | * Copyright 2013 Amadeus s.a.s.
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 |
|
17 | /**
|
18 | * This script is included in the test page inside the iframe.
|
19 | * It provides base functionalities when
|
20 | * - interactive page, where the browser is not connected as slave
|
21 | * - start testing
|
22 | * - include a test in the page
|
23 | */
|
24 | (function () {
|
25 | var window = this;
|
26 | var attester = this.attester = window.parent.attester;
|
27 |
|
28 | if (!attester) {
|
29 | // For debug purposes all slave actions are redirected to console log
|
30 | attester = this.attester = {
|
31 | testStart: function (info) {
|
32 | console.log('testStart', info);
|
33 | },
|
34 | testEnd: function (info) {
|
35 | console.log('testEnd', info);
|
36 | },
|
37 | testError: function (info) {
|
38 | console.log('testError', info);
|
39 | },
|
40 | taskFinished: function (info) {
|
41 | console.log('taskFinished', info);
|
42 | },
|
43 | stackTrace: function () {
|
44 | return [];
|
45 | },
|
46 | coverage: function () {},
|
47 | installConsole: function () {},
|
48 | currentTask: {}
|
49 | };
|
50 | }
|
51 |
|
52 | attester.installConsole(window);
|
53 |
|
54 | // This should be implemented by test-types
|
55 | attester.currentTask.start = function () {};
|
56 | // Actions can be added by plugins and should be performed before the start
|
57 | attester.currentTask.actions = [];
|
58 |
|
59 | // By default we simply include a script in the page when we try to load a test
|
60 | if (!attester.currentTask.includeTests) {
|
61 | // It could be overridden by plugins and other script loaders
|
62 | attester.currentTask.includeTests = function (scripts, callback) {
|
63 | var info = {
|
64 | pending: scripts.length,
|
65 | callback: callback
|
66 | };
|
67 |
|
68 | for (var i = 0; i < scripts.length; i += 1) {
|
69 | loadScript(scripts[i], scriptLoaded(info));
|
70 | }
|
71 | };
|
72 | var loadScript = function (path, callback) {
|
73 | var script, head = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
|
74 | script = document.createElement("script");
|
75 | script.async = true;
|
76 | script.src = path;
|
77 | script.onload = script.onreadystatechange = function () {
|
78 | if ((script.readyState && script.readyState != "complete" && script.readyState != "loaded")) {
|
79 | return;
|
80 | }
|
81 | // Handle memory leak in IE
|
82 | script.onload = script.onreadystatechange = null;
|
83 | script = undefined;
|
84 | head = undefined;
|
85 | callback();
|
86 | };
|
87 | // Circumvent IE6 bugs with base elements (jQuery #2709 and #4378) by prepending
|
88 | head.insertBefore(script, head.firstChild);
|
89 | };
|
90 | var scriptLoaded = function (bind) {
|
91 | return function () {
|
92 | bind.pending -= 1;
|
93 | if (bind.pending === 0) {
|
94 | bind.callback();
|
95 | }
|
96 | };
|
97 | };
|
98 | }
|
99 |
|
100 | // Plugins might inject action to be called before running the tests, execute them
|
101 | attester.currentTask.__init__ = function () {
|
102 | if (attester.currentTask.actions.length === 0) {
|
103 | attester.currentTask.start();
|
104 | } else {
|
105 | var action = attester.currentTask.actions.shift();
|
106 | if (action.length > 0) {
|
107 | // Being action a function it means it accepts a callback, so it's asynchronous
|
108 | action(attester.currentTask.__init__);
|
109 | } else {
|
110 | action();
|
111 | attester.currentTask.__init__();
|
112 | }
|
113 | }
|
114 | };
|
115 | // attester.currentTask.__init__ is called by the last script in the page
|
116 | // this gives plugins the time to add actions
|
117 | })();
|