[WIP] Breadcrumb
[odoo/odoo.git] / addons / web / static / src / js / test_support.js
1 openerp.test_support = {
2     setup_connection: function (connection) {
3         var origin = location.protocol+"//"+location.host;
4         _.extend(connection, {
5             origin: origin,
6             prefix: origin,
7             server: origin, // keep chs happy
8             //openerp.web.qweb.default_dict['_s'] = this.origin;
9             rpc_function: connection.rpc_json,
10             session_id: false,
11             uid: false,
12             username: false,
13             user_context: {},
14             db: false,
15             openerp_entreprise: false,
16 //            this.module_list = openerp._modules.slice();
17 //            this.module_loaded = {};
18 //            _(this.module_list).each(function (mod) {
19 //                self.module_loaded[mod] = true;
20 //            });
21             context: {},
22             shortcuts: [],
23             active_id: null
24         });
25         return connection.session_reload();
26     },
27     module: function (title, tested_core, nonliterals) {
28         var conf = QUnit.config.openerp = {};
29         QUnit.module(title, {
30             setup: function () {
31                 QUnit.stop();
32                 var oe = conf.openerp = window.openerp.init();
33                 window.openerp.web[tested_core](oe);
34                 var done = openerp.test_support.setup_connection(oe.connection);
35                 if (nonliterals) {
36                     done = done.pipe(function () {
37                         return oe.connection.rpc('/tests/add_nonliterals', {
38                             domains: nonliterals.domains || [],
39                             contexts: nonliterals.contexts || []
40                         }).then(function (r) {
41                             oe.domains = r.domains;
42                             oe.contexts = r.contexts;
43                         });
44                     });
45                 }
46                 done.always(QUnit.start)
47                     .then(function () {
48                         conf.openerp = oe;
49                     }, function (e) {
50                         QUnit.test(title, function () {
51                             console.error(e);
52                             QUnit.ok(false, 'Could not obtain a session:' + e.debug);
53                         });
54                     });
55             }
56         });
57     },
58     test: function (title, fn) {
59         var conf = QUnit.config.openerp;
60         QUnit.test(title, function () {
61             QUnit.stop();
62             fn(conf.openerp);
63         });
64     },
65     expect: function (promise, fn) {
66         promise.always(QUnit.start)
67                .done(function () { QUnit.ok(false, 'RPC requests should not succeed'); })
68                .fail(function (e) {
69             if (e.code !== 200) {
70                 QUnit.equal(e.code, 200, 'Testing connector should raise RPC faults');
71                 if (typeof console !== 'undefined' && console.error) {
72                     console.error(e.data.debug);
73                 }
74                 return;
75             }
76             fn(e.data.fault_code);
77         })
78     }
79 };