source: products/quintagroup.plonetabs/branches/tests/quintagroup/plonetabs/tests/ecmaunits/js/test_actions.js @ 3402

Last change on this file since 3402 was 906, checked in by crchemist, 17 years ago

Updating version to show trunk status

File size: 8.2 KB
Line 
1if (typeof(qpt) == "undefined") {
2    var qpt = {};
3}
4
5// test implementation of TimerCounter
6qpt.TimerCounter = function() {
7
8this.initialize = function(delay, func, restart) {
9    this.delay = delay;
10    this.func = func;
11    if (typeof(restart) == 'undefined') {
12        restart = false;
13    }
14    this.restart = restart;
15    this.timer = null;
16};
17
18this.start = function() {
19    if (this.timer) {
20;;;     kukit.E = 'Timer already started.';
21
22        throw new Error(kukit.E);
23    }
24    this.timeout();
25};
26
27this.timeout = function() {
28    // Call the event action
29    this.func();
30    // Restart the timer
31    if (this.restart) {
32        this.timer = null;
33        this.start();
34    }
35};
36
37this.clear = function() {
38    if (this.timer) {
39        window.clearTimeout(this.timer);
40        this.timer = null;
41    }
42    this.restart = false;
43};
44this.initialize.apply(this, arguments);
45};
46
47qpt.ActionsTestCase = function() {
48    this.name = 'qpt.ActionsTestCase';
49
50    this.setUp = function() {
51    };
52
53    this.tearDown = function() {
54    };
55
56    this._action = function(actName) {
57        return kukit.actionsGlobalRegistry.get(actName);
58    };
59
60    this._oper = function(node, parms) {
61        oper = new kukit.op.Oper({'node': node, 'parms': parms});
62        return oper;
63    };
64
65    this.testRedirectTo = function() {
66        // we aren't able to test this action entirely
67        // because it's impossible to override window object
68        // by dummy, thus we will navigate off from test page
69        // in case testing new urls
70        var act = this._action('plonetabs-redirectTo');
71        var url_holder = window.location.href;
72        if (window.location.hash != '#quintagroup.com') {
73            if (window.location.hash) {
74                url_holder = url_holder.slice(0, -window.location.hash.length);
75            };
76            url_holder = url_holder + '#quintagroup.com';
77        };
78
79        act(this._oper(null, {'hash':'#quintagroup.com'}));
80        this.assertEquals(url_holder, window.location.href);
81    };
82
83    this.testToggleCollapsible = function() {
84        var act = this._action('plonetabs-toggleCollapsible');
85
86        // create test content
87        var section = document.createElement('div');
88        section.id = 'collabsibleSection';
89        var handle = document.createElement('span');
90        handle.appendChild(document.createTextNode('handle'));
91        section.appendChild(handle);
92
93        // first check default parameters
94        act(this._oper(handle, {}));
95        this.assertEquals(section.className, 'collapsedBlock');
96
97        // now check expansion
98        act(this._oper(handle, {'collapsed' : 'collapsedBlock',
99                                 'expanded' : 'exp',
100                                 'collapse': 'false'}));
101        this.assertEquals(section.className, 'exp');
102    };
103
104    this.testResetForm = function() {
105        var act = this._action('plonetabs-resetForm');
106
107        // create test form
108        var _form = document.createElement('form');
109        var state_var;
110        _form.reset = function() {state_var='reset';};
111        var _input = document.createElement('input');
112        _form.appendChild(_input);
113        _input.type = 'text';
114        _input.name = 'test';
115        _input.value = 'test value';
116
117        // first check default behavior
118        act(this._oper(_form, {}));
119        this.assertEquals(state_var, 'reset');
120
121        // and now pass it something invalid
122        state_var = 'invalid data';
123        act(this._oper(_input, {}));
124        this.assertEquals(state_var, 'invalid data');
125    };
126
127    this.testBlur = function() {
128        var act = this._action('plonetabs-blur');
129
130        // create blurrable element
131        var _input = document.createElement('input');
132        _input.type = 'text';
133        _input.name = 'test';
134        _input.value = 'test value';
135        var state_var;
136        _input.blur = function() {state_var='blur';};
137
138        // first check good behavior
139        act(this._oper(_input, {}));
140        this.assertEquals(state_var, 'blur');
141
142        // now check with bad input element
143        state_var = 'invalid data';
144        act(this._oper(document.body, {}));
145        this.assertEquals(state_var, 'invalid data');
146    };
147
148    this.testHandleServerError = function() {
149        var act = this._action('plonetabs-handleServerError');
150
151        // patch global alert function to be able test something
152        var _orig_alert = window.alert;
153        var message;
154        window.alert = function(m){message=m;};
155
156        // first pass some message explicitly
157        act(this._oper(null, {'message':'Hi there!'}));
158        this.assertEquals('Hi there!', message);
159
160        // now check some deeper behavior, don't pass message,
161        // insteas set kukit.E error message
162        kukit.E = 'client_reason="invalid KSS response"';
163        act(this._oper(null, {}));
164        this.assertNotEquals(message.indexOf('Check your portal error log.'), -1);
165
166        // revert patch
167        window.alert = _orig_alert;
168    };
169
170    this.testGenerateId = function() {
171        var act = this._action('plonetabs-generateId');
172
173        // create test content
174        var _target = document.createElement('input');
175        _target.id = 'tabstest-target';
176        _target.style.display = 'none';
177        _target.type = 'text';
178        _target.value = '';
179        document.body.appendChild(_target);
180        var _source = document.createElement('input');
181        _source.type = 'text';
182        _source.value = 'title';
183
184        // check default behavior
185        act(this._oper(_source, {'target':'tabstest-target', 'var_name':'initialValue'}));
186        this.assertEquals(kukit.engine.stateVariables['initialValue'], 'title');
187
188        // set some invalid for id characters into source field
189        _source.value = '"bad title"';
190        act(this._oper(_source, {'target':'tabstest-target', 'var_name':'initialValue'}));
191        this.assertEquals(_target.value, 'bad title');
192
193        // cleanup test trash
194        document.body.removeChild(_target);
195        delete kukit.engine.stateVariables['initialValue'];
196    };
197
198    this.testReplaceOrInsert = function() {
199        var act = this._action('plonetabs-replaceOrInsert');
200
201        // create test content
202        var container = document.createElement('div');
203        container.id = 'plonetabs-test-container';
204        container.style.display = 'none';
205        document.body.appendChild(container);
206        var span1 = document.createElement('span');
207        span1.id = 'plonetabs-span1';
208        span1.appendChild(document.createTextNode('span #1'));
209        container.appendChild(span1);
210        var span2 = document.createElement('span');
211        span2.appendChild(document.createTextNode('span #2'));
212        container.appendChild(span2);
213
214        // first check simpler behavior
215        act(this._oper(document.body,
216                       {'selector': 'plonetabs-span1',
217                        'selectorType': 'htmlid',
218                        'html': '<span id="plonetabs-newspan">hi</div>',
219                        'withKssSetup': false}));
220        this.assertEquals(document.getElementById('plonetabs-newspan').innerHTML, 'hi')
221
222        // then check some more complicated behavior
223        // some time maybe I'll write it, sorry but right now I'm too lazy for this
224
225        // cleanup document body from test content
226        document.body.removeChild(container);
227    };
228
229    this.testTimeout = function() {
230        var act = this._action('plonetabs-timeout');
231
232        // patch global alert function to be able test something
233        var _orig_alert = window.alert;
234        var message;
235        window.alert = function(m){message=m;};
236
237        // patch kukit timer
238        var _orig_counter = kukit.ut.TimerCounter;
239        kukit.ut.TimerCounter = qpt.TimerCounter;
240
241        act(this._oper(document.body, {'cmd_name': 'alert',
242                                       'delay': 100,
243                                       'repeat': 'false',
244                                       'message': 'timeout'}));
245        this.assertEquals(message, 'timeout');
246
247        // revert patches
248        window.alert = _orig_alert;
249        kukit.ut.TimerCounter = _orig_counter;
250    };
251
252};
253
254qpt.ActionsTestCase.prototype = new TestCase;
255
256
257if (typeof(testcase_registry) != 'undefined') {
258    testcase_registry.registerTestCase(qpt.ActionsTestCase, 'qpt.ActionsTestCase');
259}
Note: See TracBrowser for help on using the repository browser.