summaryrefslogtreecommitdiff
path: root/addons/web/static
diff options
context:
space:
mode:
authorXavier Morel <xmo@openerp.com>2012-10-25 17:47:45 +0200
committerXavier Morel <xmo@openerp.com>2012-10-25 17:47:45 +0200
commit99a2dd3938c7d7d4b7e9c724ec8acbb5eeedf35f (patch)
treeaca872a490542cb318626f806e8a3d3d2db0c1fd /addons/web/static
parent1cf9b2a60da502de27aa67e8318f41ad6692e5a9 (diff)
[ADD] first 90% of non-sucky testing API, last 90% left
also all existing tests shoud now be completely broken and need to be fixed bzr revid: xmo@openerp.com-20121025154745-rw2gktfd6tp68k2m
Diffstat (limited to 'addons/web/static')
-rw-r--r--addons/web/static/src/js/chrome.js4
-rw-r--r--addons/web/static/src/js/testing.js179
-rw-r--r--addons/web/static/src/js/views.js4
-rw-r--r--addons/web/static/test/test.html62
-rw-r--r--addons/web/static/test/testing.js97
5 files changed, 183 insertions, 163 deletions
diff --git a/addons/web/static/src/js/chrome.js b/addons/web/static/src/js/chrome.js
index 3cb4437d1c3..80f965a6200 100644
--- a/addons/web/static/src/js/chrome.js
+++ b/addons/web/static/src/js/chrome.js
@@ -296,14 +296,14 @@ instance.web.DatabaseManager = instance.web.Widget.extend({
$('.oe_secondary_menus_container,.oe_user_menu_placeholder').empty();
var fetch_db = this.rpc("/web/database/get_list", {}).pipe(
function(result) {
- self.db_list = result.db_list;
+ self.db_list = result;
},
function (_, ev) {
ev.preventDefault();
self.db_list = null;
});
var fetch_langs = this.rpc("/web/session/get_lang_list", {}).then(function(result) {
- self.lang_list = result.lang_list;
+ self.lang_list = result;
});
return $.when(fetch_db, fetch_langs).then(self.do_render);
},
diff --git a/addons/web/static/src/js/testing.js b/addons/web/static/src/js/testing.js
new file mode 100644
index 00000000000..4eadc3a5415
--- /dev/null
+++ b/addons/web/static/src/js/testing.js
@@ -0,0 +1,179 @@
+// Test support structures and methods for OpenERP
+openerp.testing = {};
+(function (testing) {
+ var dependencies = {
+ corelib: [],
+ coresetup: ['corelib'],
+ data: ['corelib', 'coresetup'],
+ dates: [],
+ formats: ['coresetup', 'dates'],
+ chrome: ['corelib', 'coresetup'],
+ views: ['corelib', 'coresetup', 'data', 'chrome'],
+ search: ['data', 'coresetup', 'formats'],
+ list: ['views', 'data'],
+ form: ['data', 'views', 'list', 'formats'],
+ list_editable: ['list', 'form', 'data'],
+ };
+
+ testing.dependencies = window['oe_all_dependencies'] || [];
+ testing.current_module = null;
+ testing.templates = { };
+ testing.add_template = function (name) {
+ var xhr = QWeb2.Engine.prototype.get_xhr();
+ xhr.open('GET', name, false);
+ xhr.send(null);
+ (testing.templates[testing.current_module] =
+ testing.templates[testing.current_module] || [])
+ .push(xhr.responseXML);
+ };
+ /**
+ * Function which does not do anything
+ */
+ testing.noop = function () { };
+ /**
+ * Alter provided instance's ``session`` attribute to make response
+ * mockable:
+ *
+ * * The ``responses`` parameter can be used to provide a map of (RPC)
+ * paths (e.g. ``/web/view/load``) to a function returning a response
+ * to the query.
+ * * ``instance.session`` grows a ``responses`` attribute which is
+ * a map of the same (and is in fact initialized to the ``responses``
+ * parameter if one is provided)
+ *
+ * Note that RPC requests to un-mocked URLs will be rejected with an
+ * error message: only explicitly specified urls will get a response.
+ *
+ * Mocked sessions will *never* perform an actual RPC connection.
+ *
+ * @param instance openerp instance being initialized
+ * @param {Object} [responses]
+ */
+ testing.mockifyRPC = function (instance, responses) {
+ var session = instance.session;
+ session.responses = responses || {};
+ session.rpc_function = function (url, payload) {
+ var fn, params;
+ var needle = payload.params.model + ':' + payload.params.method;
+ if (url.url === '/web/dataset/call_kw'
+ && needle in this.responses) {
+ fn = this.responses[needle];
+ params = [
+ payload.params.args || [],
+ payload.params.kwargs || {}
+ ];
+ } else {
+ fn = this.responses[url.url];
+ params = [payload];
+ }
+
+ if (!fn) {
+ return $.Deferred().reject({}, 'failed',
+ _.str.sprintf("Url %s not found in mock responses, with arguments %s",
+ url.url, JSON.stringify(payload.params))
+ ).promise();
+ }
+ try {
+ return $.when(fn.apply(null, params)).pipe(function (result) {
+ // Wrap for RPC layer unwrapper thingy
+ return {result: result};
+ });
+ } catch (e) {
+ // not sure why this looks like that
+ return $.Deferred().reject({}, 'failed', String(e));
+ }
+ };
+ };
+
+ var _load = function (instance, module, loaded) {
+ if (!loaded) { loaded = []; }
+
+ var deps = dependencies[module];
+ if (!deps) { throw new Error("Unknown dependencies for " + module); }
+
+ var to_load = _.difference(deps, loaded);
+ while (!_.isEmpty(to_load)) {
+ _load(instance, to_load[0], loaded);
+ to_load = _.difference(deps, loaded);
+ }
+ openerp.web[module](instance);
+ loaded.push(module);
+ };
+
+ testing.section = function (name, body) {
+ QUnit.module(testing.current_module + '.' + name);
+ body(testing.case);
+ };
+ testing.case = function (name, options, callback) {
+ if (_.isFunction(options)) {
+ callback = options;
+ options = {};
+ }
+
+ var module = testing.current_module;
+ var module_index = _.indexOf(testing.dependencies, module);
+ var module_deps = testing.dependencies.slice(
+ // If module not in deps (because only tests, no JS) -> indexOf
+ // returns -1 -> index becomes 0 -> replace with ``undefined`` so
+ // Array#slice returns a full copy
+ 0, module_index + 1 || undefined);
+ QUnit.test(name, function (env) {
+ var instance = openerp.init(module_deps);
+ if (_.isNumber(options.asserts)) {
+ expect(options.asserts)
+ }
+
+ if (options.templates) {
+ for(var i=0; i<module_deps.length; ++i) {
+ var dep = module_deps[i];
+ var templates = testing.templates[dep];
+ if (_.isEmpty(templates)) { continue; }
+
+ for (var j=0; j < templates.length; ++j) {
+ instance.web.qweb.add_template(templates[j]);
+ }
+ }
+ }
+
+ var mock, async = false;
+ switch (options.rpc) {
+ case 'mock':
+ async = true;
+ testing.mockifyRPC(instance);
+ mock = function (spec, handler) {
+ instance.session.responses[spec] = handler;
+ };
+ break;
+ case 'rpc':
+ async = true;
+ }
+
+ // TODO: explicit dependencies options for web sub-modules (will deprecate _load/instanceFor)
+ var result = callback(instance, $('#qunit-fixture'), mock);
+
+ if (!(result && _.isFunction(result.then))) {
+ if (async) {
+ ok(false, "asynchronous test cases must return a promise");
+ }
+ return;
+ }
+
+ stop();
+ if (!_.isNumber(options.asserts)) {
+ ok(false, "asynchronous test cases must specify the "
+ + "number of assertions they expect");
+ }
+ result.then(function () {
+ start();
+ }, function (error) {
+ start();
+ if (options.fail_on_rejection === false) {
+ return;
+ }
+ ok(false, typeof error === 'object' && error.message
+ ? error.message
+ : JSON.stringify([].slice.call(arguments)));
+ })
+ });
+ };
+})(openerp.testing);
diff --git a/addons/web/static/src/js/views.js b/addons/web/static/src/js/views.js
index d4b676f2c7a..9390f753be7 100644
--- a/addons/web/static/src/js/views.js
+++ b/addons/web/static/src/js/views.js
@@ -776,8 +776,8 @@ instance.web.ViewManagerAction = instance.web.ViewManager.extend({
name: "JS Tests",
target: 'new',
type : 'ir.actions.act_url',
- url: '/web/static/test/test.html'
- })
+ url: '/web/tests?mod=*'
+ });
break;
case 'perm_read':
var ids = current_view.get_selected_ids();
diff --git a/addons/web/static/test/test.html b/addons/web/static/test/test.html
deleted file mode 100644
index 209723af5a0..00000000000
--- a/addons/web/static/test/test.html
+++ /dev/null
@@ -1,62 +0,0 @@
-<!DOCTYPE html>
-<html style="height: 100%">
-<head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <title>OpenERP Web Test Suite</title>
- <link rel="shortcut icon" href="/web/static/src/img/favicon.ico" type="image/x-icon"/>
-
- <link rel="stylesheet" href="/web/static/lib/qunit/qunit.css">
- <script src="/web/static/lib/qunit/qunit.js" type="text/javascript"></script>
-
- <script src="/web/static/lib/underscore/underscore.js" type="text/javascript"></script>
- <script src="/web/static/lib/underscore/underscore.string.js" type="text/javascript"></script>
- <script src="/web/static/lib/backbone/backbone.js" type="text/javascript"></script>
-
- <!-- jquery -->
- <script src="/web/static/lib/jquery/jquery-1.7.2.js"></script>
- <script src="/web/static/lib/jquery.ui/js/jquery-ui-1.8.17.custom.min.js"></script>
- <script src="/web/static/lib/jquery.ba-bbq/jquery.ba-bbq.js"></script>
-
- <script src="/web/static/lib/datejs/globalization/en-US.js"></script>
- <script src="/web/static/lib/datejs/core.js"></script>
- <script src="/web/static/lib/datejs/parser.js"></script>
- <script src="/web/static/lib/datejs/sugarpak.js"></script>
- <script src="/web/static/lib/datejs/extras.js"></script>
-
- <script src="/web/static/lib/qweb/qweb2.js"></script>
-
- <script src="/web/static/lib/py.js/lib/py.js"></script>
-
- <script src="/web/static/src/js/boot.js"></script>
- <script src="/web/static/src/js/corelib.js"></script>
- <script src="/web/static/src/js/coresetup.js"></script>
- <script src="/web/static/src/js/dates.js"></script>
- <script src="/web/static/src/js/formats.js"></script>
- <script src="/web/static/src/js/chrome.js"></script>
- <script src="/web/static/src/js/data.js"></script>
- <script src="/web/static/src/js/views.js"></script>
- <script src="/web/static/src/js/search.js"></script>
- <script src="/web/static/src/js/view_form.js"></script>
- <script src="/web/static/src/js/view_list.js"></script>
- <script src="/web/static/src/js/view_list_editable.js"></script>
-
- <script src="/web/static/test/testing.js"></script>
- <script type="text/javascript">
- QUnit.config.testTimeout = 2000;
- </script>
-</head>
- <body id="oe" class="openerp">
- <div id="qunit"></div>
- <div id="qunit-fixture"></div>
- </body>
- <script type="text/javascript" src="/web/static/test/class.js"></script>
- <script type="text/javascript" src="/web/static/test/registry.js"></script>
- <script type="text/javascript" src="/web/static/test/form.js"></script>
- <script type="text/javascript" src="/web/static/test/list-utils.js"></script>
- <script type="text/javascript" src="/web/static/test/formats.js"></script>
- <script type="text/javascript" src="/web/static/test/rpc.js"></script>
- <script type="text/javascript" src="/web/static/test/evals.js"></script>
- <script type="text/javascript" src="/web/static/test/search.js"></script>
- <script type="text/javascript" src="/web/static/test/Widget.js"></script>
- <script type="text/javascript" src="/web/static/test/list-editable.js"></script>
-</html>
diff --git a/addons/web/static/test/testing.js b/addons/web/static/test/testing.js
deleted file mode 100644
index 1be8e94e74b..00000000000
--- a/addons/web/static/test/testing.js
+++ /dev/null
@@ -1,97 +0,0 @@
-// Test support structures and methods for OpenERP
-openerp.testing = (function () {
- var xhr = QWeb2.Engine.prototype.get_xhr();
- xhr.open('GET', '/web/static/src/xml/base.xml', false);
- xhr.send(null);
- var doc = xhr.responseXML;
-
- var dependencies = {
- corelib: [],
- coresetup: ['corelib'],
- data: ['corelib', 'coresetup'],
- dates: [],
- formats: ['coresetup', 'dates'],
- chrome: ['corelib', 'coresetup'],
- views: ['corelib', 'coresetup', 'data', 'chrome'],
- search: ['data', 'coresetup', 'formats'],
- list: ['views', 'data'],
- form: ['data', 'views', 'list', 'formats'],
- list_editable: ['list', 'form', 'data'],
- };
-
- return {
- /**
- * Function which does not do anything
- */
- noop: function () { },
- /**
- * Loads 'base.xml' template file into qweb for the provided instance
- *
- * @param instance openerp instance being initialized, to load the template file in
- */
- loadTemplate: function (instance) {
- instance.web.qweb.add_template(doc);
- },
- /**
- * Alter provided instance's ``session`` attribute to make response
- * mockable:
- *
- * * The ``responses`` parameter can be used to provide a map of (RPC)
- * paths (e.g. ``/web/view/load``) to a function returning a response
- * to the query.
- * * ``instance.session`` grows a ``responses`` attribute which is
- * a map of the same (and is in fact initialized to the ``responses``
- * parameter if one is provided)
- *
- * Note that RPC requests to un-mocked URLs will be rejected with an
- * error message: only explicitly specified urls will get a response.
- *
- * Mocked sessions will *never* perform an actual RPC connection.
- *
- * @param instance openerp instance being initialized
- * @param {Object} [responses]
- */
- mockifyRPC: function (instance, responses) {
- var session = instance.session;
- session.responses = responses || {};
- session.rpc_function = function (url, payload) {
- var fn = this.responses[url.url + ':' + payload.params.method]
- || this.responses[url.url];
-
- if (!fn) {
- return $.Deferred().reject({}, 'failed',
- _.str.sprintf("Url %s not found in mock responses, with arguments %s",
- url.url, JSON.stringify(payload.params))
- ).promise();
- }
- return $.when(fn(payload));
- };
- },
- /**
- * Creates an openerp web instance loading the specified module after
- * all of its dependencies.
- *
- * @param {String} module
- * @returns OpenERP Web instance
- */
- instanceFor: function (module) {
- var instance = openerp.init([]);
- this._load(instance, module);
- return instance;
- },
- _load: function (instance, module, loaded) {
- if (!loaded) { loaded = []; }
-
- var deps = dependencies[module];
- if (!deps) { throw new Error("Unknown dependencies for " + module); }
-
- var to_load = _.difference(deps, loaded);
- while (!_.isEmpty(to_load)) {
- this._load(instance, to_load[0], loaded);
- to_load = _.difference(deps, loaded);
- }
- openerp.web[module](instance);
- loaded.push(module);
- }
- }
-})();