Skip to content

Commit

Permalink
test life-cycle order between parent and children
Browse files Browse the repository at this point in the history
  • Loading branch information
jareguo committed Jan 11, 2017
1 parent 3d18726 commit 894b3bc
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 32 deletions.
12 changes: 7 additions & 5 deletions cocos2d/core/CCNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -1071,14 +1071,16 @@ var Node = cc.Class({

_deactivateChildComponents: function () {
// 和 _activeRecursively 类似但不修改 this._activeInHierarchy
var originCount = this._components.length;
for (var c = 0; c < originCount; ++c) {
var component = this._components[c];
var i, originCount = this._components.length;
for (i = 0; i < originCount; ++i) {
var component = this._components[i];
component.__onNodeActivated(false);
}
// deactivate children recursively
for (var i = 0, len = this.childrenCount; i < len; ++i) {
var entity = this._children[i];
var children = this._children;
originCount = children.length;
for (i = 0; i < originCount; ++i) {
var entity = children[i];
if (entity._active) {
entity._deactivateChildComponents();
}
Expand Down
29 changes: 29 additions & 0 deletions test/qunit/unit/_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,35 @@ function fastArrayEqual (actual, expected, message) {
}
}

function createNodes (data) {
var nodes = {};
function createNode (data) {
var node = new cc.Node();
for (var key in data) {
var value = data[key];
if (typeof value === 'object') {
var child = createNode(value);
child.parent = node;
nodes[key] = child;
}
else if (key === 'comps') {
if (Array.isArray(value)) {
for (var i = 0; i < value.length; i++) {
node.addComponent(value[i]);
}
}
else {
node.addComponent(value);
}
nodes[key + 'Comps'] = node._components.slice();
}
}
return node;
}
nodes.root = createNode(data);
return nodes;
}

// output test states

//QUnit.testStart = function(test) {
Expand Down
207 changes: 180 additions & 27 deletions test/qunit/unit/test-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,6 @@ test('activation logic for component in hierarchy', function () {
restComp = createDisabledComp(node, 'rest');
}

var child = new cc.Node();
createDisabledComp(child, 'child');

cc.director.getScene().addChild(node);

strictEqual(node.active, false, 'node should be deactivated');
Expand All @@ -418,37 +415,39 @@ test('activation logic for component in hierarchy', function () {
cc.game.step();
});

test('could deactivate parent in onLoad', function () {
test('could deactivate parent in onLoad if activate from parent to child', function () {
strictEqual(StillInvokeRestCompsOnSameNode, false, 'test cases not implemented if "StillInvokeRestCompsOnSameNode"');
strictEqual(StillInvokeOnEnableOnSameComp, false, 'test cases not implemented if "StillInvokeOnEnableOnSameComp"');

var parent = new cc.Node();
var prevNode = new cc.Node();
var node = new cc.Node();
var nextNode = new cc.Node();
parent.addChild(prevNode);
parent.addChild(node);
parent.addChild(nextNode);
var child = new cc.Node();
node.addChild(child);
var nodes = createNodes({
prevNode: {},
testerNode: {
comment: 'the node that tester component attached',
child: {}
},
nextNode: {}
});
var parentToDeactivate = nodes.root;
var prevNode = nodes.prevNode;
var testerNode = nodes.testerNode;
var nextNode = nodes.nextNode;
var child = nodes.child;

// init parent
var compOfParent = createNormalComp(parent);
var compOfParent = createNormalComp(parentToDeactivate);

// init prevNode
var compOfPrevNode = createNormalComp(prevNode);

// init nextNode
var compOfNextNode = createDisabledComp(nextNode, 'next node\'s');
createDisabledComp(nextNode, 'next node\'s');

// init node

var previousComp = createNormalComp(node);

var testComp = node.addComponent(cc.Component);
var previousComp = createNormalComp(testerNode);
var testComp = testerNode.addComponent(cc.Component);
testComp.onLoad = function () {
// deactivate parent
parent.active = false;
parentToDeactivate.active = false;
};
if (StillInvokeOnEnableOnSameComp) {
testComp.onEnable = new Callback(function () {
Expand All @@ -466,22 +465,22 @@ test('activation logic for component in hierarchy', function () {

var restComp;
if (StillInvokeRestCompsOnSameNode) {
restComp = createNormalComp(node);
restComp = createNormalComp(testerNode);
}
else {
restComp = createDisabledComp(node, 'rest');
restComp = createDisabledComp(testerNode, 'rest');
}

// init child
createDisabledComp(child, 'child');

// ACTIVATE
cc.director.getScene().addChild(parent);
cc.director.getScene().addChild(parentToDeactivate);

// test

strictEqual(parent.active, false, 'parent should be deactivated');
strictEqual(node.active, true, 'node should be deactivated');
strictEqual(parentToDeactivate.active, false, 'parent should be deactivated');
strictEqual(testerNode.active, true, 'node should be deactivated');
compShouldBeActivated(compOfParent, 'parent node\'s');
compShouldBeActivated(compOfPrevNode, 'previous node\'s');
compShouldBeActivated(previousComp, 'previous');
Expand All @@ -498,6 +497,88 @@ test('activation logic for component in hierarchy', function () {
cc.game.step();
});

// test('could deactivate parent in onLoad', function () {
// strictEqual(StillInvokeRestCompsOnSameNode, false, 'test cases not implemented if "StillInvokeRestCompsOnSameNode"');
// strictEqual(StillInvokeOnEnableOnSameComp, false, 'test cases not implemented if "StillInvokeOnEnableOnSameComp"');
//
// var nodes = createNodes({
// prevNode: {},
// testerNode: {
// comment: 'the node that tester component attached',
// child: {}
// },
// nextNode: {}
// });
// var parentToDeactivate = nodes.root;
// var prevNode = nodes.prevNode;
// var testerNode = nodes.testerNode;
// var nextNode = nodes.nextNode;
// var child = nodes.child;
//
// // init parent
// createDisabledComp(parentToDeactivate, 'parent component');
//
// // init prevNode
// var compOfPrevNode = createNormalComp(prevNode);
//
// // init nextNode
// createDisabledComp(nextNode, 'next node\'s');
//
// // init node
// var previousComp = createNormalComp(testerNode);
// var testComp = testerNode.addComponent(cc.Component);
// testComp.onLoad = function () {
// // deactivate parent
// parentToDeactivate.active = false;
// };
// if (StillInvokeOnEnableOnSameComp) {
// testComp.onEnable = new Callback(function () {
// this.onDisable.enable();
// }).enable();
// testComp.onDisable = new Callback().disable('onDisable should called after onEnable');
// }
// else {
// testComp.onEnable = new Callback().disable(
// 'onEnable of testing component should not be called (deactivated)');
// testComp.onDisable = new Callback().disable(
// 'onDisable of testing component should not be called (deactivated)');
// }
// testComp.update = new Callback().disable('update of testing component should not be called (deactivated)');
//
// var restComp;
// if (StillInvokeRestCompsOnSameNode) {
// restComp = createNormalComp(testerNode);
// }
// else {
// restComp = createDisabledComp(testerNode, 'rest');
// }
//
// // init child
// var childComp = createNormalComp(child);
//
// // ACTIVATE
// cc.director.getScene().addChild(parentToDeactivate);
//
// // test
//
// strictEqual(parentToDeactivate.active, false, 'parent should be deactivated');
// strictEqual(testerNode.active, true, 'node should be deactivated');
// compShouldBeActivated(childComp, 'child node\'s');
// compShouldBeActivated(compOfPrevNode, 'previous node\'s');
// compShouldBeActivated(previousComp, 'previous');
//
// if (StillInvokeOnEnableOnSameComp) {
// testComp.onEnable.once('onEnable of test component should still be called').disable();
// testComp.onDisable.once('onDisable of test component should still be called').disable();
// }
//
// if (StillInvokeRestCompsOnSameNode) {
// compShouldBeActivated(restComp, 'rest');
// }
//
// cc.game.step();
// });

test('could deactivate self node in onEnable', function () {
var node = new cc.Node();

Expand Down Expand Up @@ -536,7 +617,53 @@ test('activation logic for component in hierarchy', function () {
});
})();

test('call onLoad on dynamic created child component in onLoad', function () {
test('life-cycle order between parent and children', function () {
var parent = new cc.Node('parent');
var child = new cc.Node('child');
child.parent = parent;

// init parent
var childComp = child.addComponent(cc.Component);
var parentComp = parent.addComponent(cc.Component);

function assertOrder (firstComp, lastComp, method) {
lastComp[method] = new Callback().disable(lastComp.node.name + '\'s ' + method + ' should be called after ' + firstComp.node.name + '\'s');
firstComp[method] = new Callback(function () {
lastComp[method].enable();
}).enable();
}

assertOrder(parentComp, childComp, 'onLoad');
assertOrder(childComp, parentComp, 'onDestroy');
// assertOrder(parentComp, childComp, 'onDestroy');

assertOrder(parentComp, childComp, 'onEnable');
assertOrder(parentComp, childComp, 'onDisable');
// assertOrder(childComp, parentComp, 'onDisable');

assertOrder(parentComp, childComp, 'start');
assertOrder(parentComp, childComp, 'update');

parentComp._destruct = function () {};
childComp._destruct = function () {};

// ACTIVATE
cc.director.getScene().addChild(parent);
cc.game.step();
parent.destroy();
cc.game.step();

// if (parentComp.update.calledCount === 0 || childComp.update.calledCount === 0) {
// ok(false, 'update should be called');
// }
if (parentComp.onDestroy.calledCount === 0 || childComp.onDestroy.calledCount === 0) {
ok(false, 'onDestroy should be called');
}

expect(0);
});

test('The onLoad of dynamic created child component', function () {
var node = new cc.Node();
var child = new cc.Node();

Expand All @@ -555,13 +682,39 @@ test('call onLoad on dynamic created child component in onLoad', function () {
var testComp = node.addComponent(cc.Component);
testComp.onLoad = function () {
child.addComponent(ChildComponent);
strictEqual(onLoadCalled, true, 'Child onLoad should be called during onLoad');
strictEqual(onLoadCalled, true, 'should be called during onLoad');
};

child.parent = node;
node.parent = cc.director.getScene();
});

test('The onLoad of dynamic created parent component', function () {
var node = new cc.Node();
var parent = new cc.Node();

var onLoadCalled = false;

var ParentComponent = cc.Class({
extends: cc.Component,
editor: {
executeInEditMode: true
},
onLoad: function () {
onLoadCalled = true;
}
});

var testComp = node.addComponent(cc.Component);
testComp.onLoad = function () {
parent.addComponent(ParentComponent);
strictEqual(onLoadCalled, true, 'should be called during onLoad');
};

node.parent = parent;
parent.parent = cc.director.getScene();
});

test('destroy', function () {
var parent = new cc.Node();
var child = new cc.Node();
Expand Down

0 comments on commit 894b3bc

Please sign in to comment.