summaryrefslogtreecommitdiff
path: root/src/tests/efl_js/ecore_js_suite.js
blob: 85630afaf4e21aba832935bec02e8dcd9819d98f (plain)
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
#!/usr/bin/env node

// Preamble
function assert(condition, message) {
  if (!condition) {
      print("Assertion failed ", message);
      throw new Error(message || "Assertion failed");
  }
}

if(typeof process !== 'undefined')
{
    console.log('running from nodejs');
    console.log('path', process.env.NODE_PATH);

    efl = require('efl');
    assert(efl != null, "could not load efl node module");

    // Setup output aliases
    print = console.log;
    print_error = function() {
        if (process.argv.indexOf("--supress-errors") == -1)
            console.error.apply(null, arguments);
    };
    print_info = function() {
        if (process.argv.indexOf("--verbose") != -1)
            console.info.apply(null, arguments);
    };
    exit = efl.Ecore.Mainloop.quit;
}
else
{
    assert = function(test, message) { if (test !== true) throw message; };
    print('running from libv8')
    //FIXME Add levels to v8 tests
    print_error = print
    print_info = print
    exit = function() {}
}

// Global flag for suite success //
suite_success = true;
// Global test summary
suite_ok = 0;
suite_fail = []; // Will store the name of the failures

// Basic test function //
function start_test(test_name, test_func) {
  print("[ RUN         ]  ecore_js_suite: " + test_name);
  var test_result = true;
  try {
    test_func();
  } catch (e) {
    suite_success = false;
    test_result = false;
    print_error("Error: ", e, e.stack);
  }
  print("[        " + (test_result ? "PASS" : "FAIL") + " ]  ecore_js_suite: " + test_name);
  if (test_result)
    suite_ok += 1;
  else
    suite_fail.push(test_name);
}
// end Preamble

// ecore preamble
function abs(n) {
    if (n < 0)
        return n * -1;
    return n;
}
var TOLERANCE = 0.0001;
// end ecore preamble

start_test("ecore timers", function () {
    var p = 2.5;
    efl.Ecore.Timer.setPrecision(p);
    assert(abs(efl.Ecore.Timer.getPrecision() - p) < TOLERANCE);
    
    p = 0.5;
    efl.Ecore.Timer.setPrecision(p);
    assert(abs(efl.Ecore.Timer.getPrecision() - p) < TOLERANCE);
    
    var ncalls = 0;

    captured = false;
    efl.Ecore.Timer.add(1, function() {
        ++ncalls;
        if (ncalls != 4)
            return true;
        
        captured = true;
        efl.Ecore.Job.add(efl.Ecore.Mainloop.quit);
        return false;
    });

    efl.Ecore.Mainloop.begin();
    assert(captured);

    ncalls = 0;
    captured = false;

    efl.Ecore.Timer.addLoop(1, function() {
        ++ncalls;
        if (ncalls != 4)
            return true;

        captured = true;
        efl.Ecore.Job.add(efl.Ecore.Mainloop.quit);
        return false;
    });

    efl.Ecore.Mainloop.begin();
    assert(captured);

    captured = false;

    var timer = efl.Ecore.Timer.add(1, function() {
        captured = true;
        return false;
    });

    assert(timer.freeze_get() === false);

    timer.freeze();

    assert(timer.freeze_get() === true);

    timer.thaw();

    assert(timer.freeze_get() === false);

    timer.del();

    efl.Ecore.Timer.add(2, function() {
        efl.Ecore.Job.add(efl.Ecore.Mainloop.quit);
        return false;
    });

    efl.Ecore.Mainloop.begin();
    assert(captured === false);
});

// Ecore event
start_test("ecore event", function () {
    var myevent = efl.Ecore.Event.newType();

    captured = [0, 0, 0]

    var handler1 = efl.Ecore.Event.addHandler(myevent, function(event) {
        print_info("CALLBACK_PASS_ON pre assert");
        assert(efl.Ecore.Event.getCurrentType() === myevent);
        assert(event === myevent);
        captured[0] += 1;
        print_info("CALLBACK_PASS_ON post assert");
        return efl.Ecore.Mainloop.CALLBACK_PASS_ON;
    });

    var handler2 = efl.Ecore.Event.addHandler(myevent, function(event) {
        print_info("CALLBACK_DONE #1 pre assert");
        assert(efl.Ecore.Event.getCurrentType() === myevent);
        assert(event === myevent);
        captured[1] += 1;
        print_info("CALLBACK_DONE #1 post assert");
        return efl.Ecore.Mainloop.CALLBACK_DONE;
    });

    var handler3 = efl.Ecore.Event.addHandler(myevent, function(event) {
        print_info("CALLBACK_DONE #2 pre assert");
        assert(efl.Ecore.Event.getCurrentType() === myevent);
        assert(event === myevent);
        captured[2] += 1;
        print_info("CALLBACK_DONE #2 post assert");
        return efl.Ecore.Mainloop.CALLBACK_DONE;
    });

    efl.Ecore.Timer.add(1, function() {
        efl.Ecore.Event.add(myevent);
        print_info("Timer.add(1, callback) before assert");
        assert(captured[0] === 0 && captured[1] === 0 && captured[2] === 0);
        print_info("Timer.add(1, callback) after assert");
        efl.Ecore.Timer.add(1, function() {
            print_info("inner Timer.add(1, callback) before assert");
            assert(captured[0] === 1 && captured[1] === 1 && captured[2] === 0);
            print_info("inner Timer.add(1, callback) after assert");
            handler1.del();
            efl.Ecore.Event.add(myevent);
            efl.Ecore.Event.add(myevent);
            efl.Ecore.Event.add(myevent).del();
            efl.Ecore.Timer.add(1, function() {
                print_info("twice inner Timer.add(1, callback) before assert");
                assert(captured[0] === 1 && captured[1] === 3 && captured[2] === 0);
                print_info("twice inner Timer.add(1, callback) after assert");
                efl.Ecore.Mainloop.quit();
            });
        });
    });

    efl.Ecore.Mainloop.begin();

    efl.Ecore.Event.add(myevent);
    efl.Ecore.Event.add(myevent);
    efl.Ecore.Event.add(myevent);

    print_info("will add filter");

    var filter = efl.Ecore.Event.addFilter(function() {
        return {count: 0};
    }, function(loop_data, event) {
        assert(event === myevent);
        var c = loop_data.count;
        ++loop_data.count;
        return c != 0;
    }, function(loop_data) {});
    
    efl.Ecore.Timer.add(1, efl.Ecore.Mainloop.quit);
    efl.Ecore.Mainloop.begin();
    assert(captured[1] === 5);
    filter.del();

    efl.Ecore.Event.add(myevent);
    efl.Ecore.Event.add(myevent);
    efl.Ecore.Event.add(myevent);
    
    efl.Ecore.Timer.add(1, efl.Ecore.Mainloop.quit);
    efl.Ecore.Mainloop.begin();
    assert(captured[1] === 8);

    handler2.del();
    handler3.del();
});

    // Ecore job
start_test("ecore jobs", function () {
    captured = false;

    efl.Ecore.Job.add(function() {
        captured = true;
        efl.Ecore.Mainloop.quit();
    });

    assert(captured === false);
    efl.Ecore.Mainloop.begin();
    assert(captured === true);

    captured = false;
    var job = efl.Ecore.Job.add(function() {
        captured = true;
    });
    efl.Ecore.Job.add(efl.Ecore.Mainloop.quit);
    job.del();
    efl.Ecore.Mainloop.begin();
    assert(captured === false);
});

start_test("ecore idle", function () {
    // Ecore idle
    var counter = 1;
    captured = [0, 0, 0, 0, 0];

    efl.Ecore.Idle.add(function() {
        captured[0] = counter;
        counter += 1;
        efl.Ecore.Job.add(function() { print_info('ecore job handler 1'); });
        return efl.Ecore.Mainloop.CALLBACK_DONE;
    });

    efl.Ecore.Idle.addEnterer(function() {
        captured[1] = counter;
        counter += 1;
        return efl.Ecore.Mainloop.CALLBACK_DONE;
    });

    efl.Ecore.Idle.addEnterer(function() {
        captured[2] = counter;
        counter += 1;
        return efl.Ecore.Mainloop.CALLBACK_DONE;
    });

    efl.Ecore.Idle.addEntererBefore(function() {
        captured[3] = counter;
        counter += 1;
        return efl.Ecore.Mainloop.CALLBACK_DONE;
    });

    efl.Ecore.Idle.addExiter(function() {
        captured[4] = counter;
        counter += 1;
        efl.Ecore.Mainloop.quit();
        return efl.Ecore.Mainloop.CALLBACK_DONE;
    });

    efl.Ecore.Mainloop.begin();

    assert(captured[0] === 4, "Ecore.Idle.add test");
    assert(captured[1] === 2, "Ecore.Idle.addEnterer test");
    assert(captured[2] === 3, "Ecore.Idle.addEnterer test two");
    assert(captured[3] === 1, "Ecore.Idle.addEntererBefore test");
    assert(captured[4] === 5, "Ecore.Idle.addExiter test");
});

// Ecore animator
start_test("ecore animator", function () {
    efl.Ecore.Animator.setFrametime(1);
    assert(efl.Ecore.Animator.getFrametime() === 1);
    efl.Ecore.Animator.setFrametime(1 / 50);
    assert(efl.Ecore.Animator.getFrametime() === (1 / 50));

    assert(efl.Ecore.Animator.posMap(0.5, efl.Ecore.Animator.POS_MAP_LINEAR, 0, 0)
           === 0.5);

    efl.Ecore.Animator.setSource(efl.Ecore.Animator.SOURCE_TIMER);
    assert(efl.Ecore.Animator.getSource() === efl.Ecore.Animator.SOURCE_TIMER);
});

// Ecore poller
start_test("ecore poller", function () {
    efl.Ecore.Poller.setPollInterval(efl.Ecore.Poller.CORE, 42);
    assert(efl.Ecore.Poller.getPollInterval(efl.Ecore.Poller.CORE) === 42);
    efl.Ecore.Poller.setPollInterval(efl.Ecore.Poller.CORE, 2);
    assert(efl.Ecore.Poller.getPollInterval(efl.Ecore.Poller.CORE) === 2);
});

start_test("ecore throttle", function () {
    // Ecore throttle

    efl.Ecore.Throttle.adjust(3);
    assert(efl.Ecore.Throttle.get() === 3);
    efl.Ecore.Throttle.adjust(-3);
    assert(efl.Ecore.Throttle.get() === 0);
});

// footer
if (!suite_success) {
  print ("[ Total tests run: %s ]", suite_ok + suite_fail.length);
  print ("[ Total successful: %s ]", suite_ok);
  print ("[ Total failures: %s ]", suite_fail.length);
  print ("[ Tests failed: ]");
  for (var i = 0; i < suite_fail.length; i++) {
    print ("[    %s]", suite_fail[i]);
  };
  assert(false, "[ Test suite fail ]");
} else {
  print ("[ Test execution with success ]");
  print ("[ Total tests run: %s ]", suite_ok);
}

exit();