Commit 65dad858 authored by Romain Courteaud's avatar Romain Courteaud

renderJS: add getMethodList method to check the gadget interface implementation

parent 861ece55
...@@ -642,6 +642,13 @@ ...@@ -642,6 +642,13 @@
); );
} }
function registerMethod(gadget_klass, method_name, method_type) {
if (!gadget_klass.hasOwnProperty('__method_type_dict')) {
gadget_klass.__method_type_dict = {};
}
gadget_klass.__method_type_dict[method_name] = method_type;
}
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// RenderJSGadget.declareJob // RenderJSGadget.declareJob
// gadget internal method, which trigger execution // gadget internal method, which trigger execution
...@@ -659,6 +666,7 @@ ...@@ -659,6 +666,7 @@
context.__job_list.push([name, callback, argument_list]); context.__job_list.push([name, callback, argument_list]);
} }
}; };
registerMethod(this, name, 'job');
// Allow chain // Allow chain
return this; return this;
}; };
...@@ -688,6 +696,7 @@ ...@@ -688,6 +696,7 @@
} }
return ensurePushableQueue(callback, argument_list, context); return ensurePushableQueue(callback, argument_list, context);
}; };
registerMethod(this, name, 'method');
// Allow chain // Allow chain
return this; return this;
}; };
...@@ -697,6 +706,21 @@ ...@@ -697,6 +706,21 @@
// Returns the list of gadget prototype // Returns the list of gadget prototype
return this.__interface_list; return this.__interface_list;
}) })
.declareMethod('getMethodList', function getMethodList(type) {
// Returns the list of gadget methods
var key,
method_list = [],
method_dict = this.constructor.__method_type_dict || {};
for (key in method_dict) {
if (method_dict.hasOwnProperty(key)) {
if ((type === undefined) ||
(type === method_dict[key])) {
method_list.push(key);
}
}
}
return method_list;
})
.declareMethod('getRequiredCSSList', function getRequiredCSSList() { .declareMethod('getRequiredCSSList', function getRequiredCSSList() {
// Returns a list of CSS required by the gadget // Returns a list of CSS required by the gadget
return this.__required_css_list; return this.__required_css_list;
...@@ -804,7 +828,7 @@ ...@@ -804,7 +828,7 @@
gadget gadget
); );
}; };
registerMethod(this, name, 'acquired_method');
// Allow chain // Allow chain
return this; return this;
}; };
...@@ -1657,7 +1681,7 @@ ...@@ -1657,7 +1681,7 @@
TmpConstructor.__ready_list = []; TmpConstructor.__ready_list = [];
TmpConstructor.__service_list = RenderJSGadget.__service_list.slice(); TmpConstructor.__service_list = RenderJSGadget.__service_list.slice();
TmpConstructor.prototype.__path = url; TmpConstructor.prototype.__path = url;
root_gadget = new RenderJSEmbeddedGadget(); root_gadget = new TmpConstructor();
setAqParent(root_gadget, createLastAcquisitionGadget()); setAqParent(root_gadget, createLastAcquisitionGadget());
declare_method_list_waiting = [ declare_method_list_waiting = [
...@@ -1665,7 +1689,8 @@ ...@@ -1665,7 +1689,8 @@
"getRequiredCSSList", "getRequiredCSSList",
"getRequiredJSList", "getRequiredJSList",
"getPath", "getPath",
"getTitle" "getTitle",
"getMethodList"
]; ];
// Inform parent gadget about declareMethod calls here. // Inform parent gadget about declareMethod calls here.
......
...@@ -38,10 +38,15 @@ ...@@ -38,10 +38,15 @@
RenderJSIframeGadget = __RenderJSIframeGadget; RenderJSIframeGadget = __RenderJSIframeGadget;
// Keep track of the root gadget // Keep track of the root gadget
renderJS(window).ready(function (g) { renderJS(window)
root_gadget_defer.resolve([g, this]); .ready(function (g) {
}); root_gadget_defer.resolve([g, this]);
})
.declareMethod('fakeRootMethod1')
.declareMethod('fakeRootMethod2')
.declareJob('fakeRootJob1')
.declareJob('fakeRootJob2')
.declareAcquiredMethod('fakeRootAcquiredMethod1', 'fakeParentMethod1');
QUnit.config.testTimeout = 10000; QUnit.config.testTimeout = 10000;
// QUnit.config.reorder = false; // QUnit.config.reorder = false;
...@@ -1144,6 +1149,83 @@ ...@@ -1144,6 +1149,83 @@
}); });
}); });
/////////////////////////////////////////////////////////////////
// RenderJSGadget.getMethodList
/////////////////////////////////////////////////////////////////
module("RenderJSGadget.getMethodList", {
setup: function () {
renderJS.clearGadgetKlassList();
}
});
test('returns method', function () {
// Check that getMethodList return a Promise
// Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget;
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
gadget = new Klass();
Klass.__method_type_dict = {
getFoo: 'type_foo',
getBar: 'type_bar',
getBar2: 'type_bar'
};
stop();
expect(4);
gadget.getMethodList()
.then(function (method_list) {
deepEqual(method_list, ['getFoo', 'getBar', 'getBar2']);
})
.then(function (method_list) {
return gadget.getMethodList('type_bar');
})
.then(function (method_list) {
deepEqual(method_list, ['getBar', 'getBar2']);
})
.then(function (method_list) {
return gadget.getMethodList('type_foo');
})
.then(function (method_list) {
deepEqual(method_list, ['getFoo']);
})
.then(function (method_list) {
return gadget.getMethodList('type_foobar');
})
.then(function (method_list) {
deepEqual(method_list, []);
})
.always(function () {
start();
});
});
test('default value', function () {
// Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget;
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
gadget = new Klass();
stop();
expect(1);
gadget.getMethodList()
.then(function (result) {
deepEqual(result, []);
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// RenderJSGadget.getRequiredCSSList // RenderJSGadget.getRequiredCSSList
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -5877,7 +5959,7 @@ ...@@ -5877,7 +5959,7 @@
} }
stop(); stop();
expect(21); expect(25);
root_gadget_defer.promise root_gadget_defer.promise
.then(function (root_gadget_list) { .then(function (root_gadget_list) {
var root_gadget = root_gadget_list[0], var root_gadget = root_gadget_list[0],
...@@ -5943,15 +6025,32 @@ ...@@ -5943,15 +6025,32 @@
deepEqual(root_gadget.__sub_gadget_dict, {}); deepEqual(root_gadget.__sub_gadget_dict, {});
deepEqual(root_gadget_klass.__service_list, []); deepEqual(root_gadget_klass.__service_list, []);
deepEqual(root_gadget.__job_list, []); deepEqual(root_gadget.__job_list, []);
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return root_gadget.declareGadget("./embedded.html", { return root_gadget.declareGadget("./embedded.html", {
sandbox: 'iframe', sandbox: 'iframe',
element: document.querySelector('#qunit-fixture') element: document.querySelector('#qunit-fixture')
}) });
.fail(function (e) { })
ok(false, e); .push(function () {
}); return RSVP.all([
root_gadget.getMethodList(),
root_gadget.getMethodList('method'),
root_gadget.getMethodList('job'),
root_gadget.getMethodList('acquired_method')
]);
})
.push(function (result_list) {
deepEqual(result_list[0], ['fakeRootMethod1', 'fakeRootMethod2',
'fakeRootJob1', 'fakeRootJob2',
'fakeRootAcquiredMethod1']);
deepEqual(result_list[1], ['fakeRootMethod1', 'fakeRootMethod2']);
deepEqual(result_list[2], ['fakeRootJob1', 'fakeRootJob2']);
deepEqual(result_list[3], ['fakeRootAcquiredMethod1']);
})
.fail(function (e) {
ok(false, e);
}); });
}) })
.fail(function (e) { .fail(function (e) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment