wisemapping-frontend/packages/mindplot/test/playground/layout/TestSuite.js
Paulo Gustavo Veiga 6ba917cf32 Squashed commit of the following:
commit 81e9041e19344df18d8e91a31edb69ea0bce6bf7
Author: Paulo Gustavo Veiga <pveiga@wisemapping.com>
Date:   Sun Dec 5 14:35:11 2021 -0800

    Fix serialization bug

commit 290223f15aa3902ef3291e0f392a92d04e9ccd0e
Author: Paulo Gustavo Veiga <pveiga@wisemapping.com>
Date:   Sun Dec 5 09:36:59 2021 -0800

    Fix serialization bug

commit 3c1a2e37cf7be34780b760eb6f2fd3e3ac577ed1
Author: Paulo Gustavo Veiga <pveiga@wisemapping.com>
Date:   Sun Dec 5 09:24:13 2021 -0800

    Fix missing references

commit 98a7fd3e1659eb32cee43144da4a10b98cfcfed0
Author: Paulo Gustavo Veiga <pveiga@wisemapping.com>
Date:   Sun Dec 5 09:08:38 2021 -0800

    Fix compile

commit 9f5146b353f0fdb34a5b83db6d120bc09a96a073
Merge: 218e074 d3dc44c
Author: Paulo Gustavo Veiga <pveiga@wisemapping.com>
Date:   Sun Dec 5 09:03:17 2021 -0800

    Merge branch 'develop' into feature/mindplot_tests_playground

commit 218e074b7330d0a02ac7afee0b644737f3bb6b03
Author: Paulo Gustavo Veiga <pveiga@wisemapping.com>
Date:   Sat Dec 4 17:46:22 2021 -0800

    Add jquery missing dependency

commit c01a748a84a2359dcf1a49d9593369c9fefe1e76
Author: Paulo Gustavo Veiga <pveiga@wisemapping.com>
Date:   Sat Dec 4 17:41:47 2021 -0800

    Remove undercore

commit d680b063b819563f4fae38571055aad4c55180b7
Author: Paulo Gustavo Veiga <pveiga@wisemapping.com>
Date:   Sat Dec 4 17:36:58 2021 -0800

    Add missing dependencies

commit be9aa21245e84c97fa9fe3545299c6bb01772ef1
Author: Paulo Gustavo Veiga <pveiga@wisemapping.com>
Date:   Sat Dec 4 17:25:23 2021 -0800

    Fix missign dependencies.

commit 2472fa453c43b64286552edb7120d4d2c395eba4
Author: Paulo Gustavo Veiga <pveiga@wisemapping.com>
Date:   Sat Dec 4 17:08:48 2021 -0800

    Fix compilation

commit a013d07dc15a3bddda1e42cad373abbdfc525c74
Merge: f2ac8fe a81e984
Author: Paulo Gustavo Veiga <pveiga@wisemapping.com>
Date:   Sat Dec 4 16:57:19 2021 -0800

    Merge branch 'develop' into feature/mindplot_tests_playground

commit f2ac8fe17de071a09f80a20f49eb42c66b5f2f2b
Author: Paulo Gustavo Veiga <pveiga@wisemapping.com>
Date:   Thu Dec 2 18:59:03 2021 -0800

    Hack: Add declaration of designer as global variable.

commit 432167acbb055fba19f1e8469db4ffe8ad7793b7
Author: Paulo Gustavo Veiga <pveiga@wisemapping.com>
Date:   Thu Dec 2 18:51:13 2021 -0800

    Hack the initialization of extended hotkey pluggin.

commit 794db35e88f6bd232094b4f104c8fe6a8fab34be
Author: Paulo Gustavo Veiga <pveiga@wisemapping.com>
Date:   Thu Dec 2 17:57:21 2021 -0800

    Add keyboard function

commit e47a48d51f1af0ebde4ebea07cb467a8e5a7a372
Author: Matias Arriola <matias.arriola@gmail.com>
Date:   Thu Dec 2 14:50:43 2021 -0300

    Fix editor snapshot

commit 9bf8baef1216352ed1f9dde4658c479c0044b093
Author: Matias Arriola <matias.arriola@gmail.com>
Date:   Thu Dec 2 13:14:13 2021 -0300

    Add editor test to mindplot playground

    Add missing imports and fix BootstrapDialog references

commit b2dcc3034fc638f58cdb59e96ab7d6e2435f634a
Author: Matias Arriola <matias.arriola@gmail.com>
Date:   Thu Dec 2 11:56:10 2021 -0300

    Add embedded test to mindplot playground

commit 08b514e1aa7b51c4df56b2b3cb7f28c191adeae7
Author: Matias Arriola <matias.arriola@gmail.com>
Date:   Thu Dec 2 10:27:31 2021 -0300

    Fix mindplot layout test

    Upgrade raphael to avoid error
    Load raphael from context-loader
2021-12-05 15:50:37 -08:00

580 lines
22 KiB
JavaScript

/*
* Copyright [2015] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import LayoutManager from '../../../src/components/layout/LayoutManager';
const TestSuite = new Class({
Extends: ChildrenSorterStrategy,
initialize() {
$('#basicTest').css('display', 'block');
// this.testAligned();
this.testBaselineAligned1();
this.testBaselineAligned2();
this.testEvents();
this.testEventsComplex();
this.testDisconnect();
this.testReconnect();
this.testRemoveNode();
this.testSize();
this.testReconnectSingleNode();
},
testAligned() {
console.log('testAligned:');
const position = { x: 0, y: 0 };
const manager = new LayoutManager(0, TestSuite.ROOT_NODE_SIZE);
manager.addNode(1, TestSuite.NODE_SIZE, position).connectNode(0, 1, 0);
manager.addNode(2, TestSuite.NODE_SIZE, position).connectNode(1, 2, 0);
manager.addNode(3, TestSuite.NODE_SIZE, position).connectNode(2, 3, 0);
manager.addNode(4, TestSuite.NODE_SIZE, position).connectNode(3, 4, 0);
manager.addNode(5, TestSuite.NODE_SIZE, position).connectNode(0, 5, 2);
manager.addNode(6, TestSuite.NODE_SIZE, position).connectNode(0, 6, 4);
manager.addNode(7, TestSuite.NODE_SIZE, position).connectNode(0, 7, 6);
manager.layout();
manager.plot('testAligned', { width: 1200, height: 200 });
// Child nodes should be vertically aligned
$assert(
manager.find(1).getPosition().y == manager.find(2).getPosition().y,
'Child nodes are not vertically aligned',
);
$assert(
manager.find(1).getPosition().y == manager.find(3).getPosition().y,
'Child nodes are not vertically aligned',
);
$assert(
manager.find(1).getPosition().y == manager.find(4).getPosition().y,
'Child nodes are not vertically aligned',
);
// Siblings should be horizontally aligned
$assert(
manager.find(1).getPosition().x == manager.find(5).getPosition().x,
'Sibling nodes are not horizontally aligned',
);
$assert(
manager.find(1).getPosition().x == manager.find(6).getPosition().x,
'Sibling nodes are not horizontally aligned',
);
$assert(
manager.find(1).getPosition().x == manager.find(7).getPosition().x,
'Sibling nodes are not horizontally aligned',
);
console.log('OK!\n\n');
},
testBaselineAligned1() {
console.log('testBaselineAligned1:');
const position = { x: 0, y: 0 };
const manager = new LayoutManager(0, TestSuite.ROOT_NODE_SIZE);
manager.addNode(1, TestSuite.NODE_SIZE, position).connectNode(0, 1, 0);
manager.addNode(3, TestSuite.NODE_SIZE, position).connectNode(1, 3, 0);
manager.addNode(5, TestSuite.NODE_SIZE, position).connectNode(3, 5, 0);
manager.addNode(6, { width: 140, height: 130 }, position).connectNode(3, 6, 1);
manager.addNode(7, TestSuite.NODE_SIZE, position).connectNode(6, 7, 0);
manager.addNode(8, TestSuite.NODE_SIZE, position).connectNode(7, 8, 0);
manager.addNode(9, TestSuite.NODE_SIZE, position).connectNode(7, 9, 1);
manager.addNode(10, TestSuite.NODE_SIZE, position).connectNode(7, 10, 2);
manager.addNode(11, TestSuite.NODE_SIZE, position).connectNode(7, 11, 3);
manager.addNode(12, TestSuite.NODE_SIZE, position).connectNode(7, 12, 4);
manager.addNode(13, TestSuite.NODE_SIZE, position).connectNode(7, 13, 5);
manager.addNode(14, TestSuite.NODE_SIZE, position).connectNode(7, 14, 6);
manager.addNode(15, TestSuite.NODE_SIZE, position).connectNode(7, 15, 7);
manager.addNode(16, TestSuite.NODE_SIZE, position).connectNode(7, 16, 8);
manager.addNode(17, TestSuite.NODE_SIZE, position).connectNode(7, 17, 9);
manager.addNode(29, TestSuite.NODE_SIZE, position).connectNode(6, 29, 1);
manager.addNode(30, TestSuite.NODE_SIZE, position).connectNode(6, 30, 2);
manager.addNode(31, { width: 100, height: 50 }, position).connectNode(6, 31, 3);
manager.addNode(4, TestSuite.NODE_SIZE, position).connectNode(1, 4, 1);
manager.addNode(18, { width: 80, height: 70 }, position).connectNode(4, 18, 0);
manager.addNode(19, TestSuite.NODE_SIZE, position).connectNode(18, 19, 0);
manager.addNode(20, TestSuite.NODE_SIZE, position).connectNode(19, 20, 0);
manager.addNode(21, TestSuite.NODE_SIZE, position).connectNode(20, 21, 0);
manager.addNode(2, TestSuite.NODE_SIZE, position).connectNode(0, 2, 1);
manager.addNode(22, TestSuite.NODE_SIZE, position).connectNode(2, 22, 0);
manager.addNode(24, TestSuite.NODE_SIZE, position).connectNode(22, 24, 0);
manager.addNode(23, { width: 80, height: 50 }, position).connectNode(2, 23, 1);
manager.addNode(25, { width: 80, height: 40 }, position).connectNode(23, 25, 0);
manager.addNode(26, { width: 80, height: 80 }, position).connectNode(25, 26, 0);
manager.addNode(27, TestSuite.NODE_SIZE, position).connectNode(26, 27, 0);
manager.addNode(28, { width: 80, height: 80 }, position).connectNode(27, 28, 0);
// manager.layout();
// manager.plot("testBaselineAligned1", {width:1600,height:800});
console.log('OK!\n\n');
},
testBaselineAligned2() {
console.log('testBaselineAligned2:');
const position = { x: 0, y: 0 };
const manager = new LayoutManager(0, TestSuite.ROOT_NODE_SIZE);
manager.addNode(1, TestSuite.NODE_SIZE, position).connectNode(0, 1, 0);
manager.addNode(2, { width: 130, height: 200 }, position).connectNode(1, 2, 0);
manager.addNode(3, TestSuite.NODE_SIZE, position).connectNode(2, 3, 0);
manager.addNode(4, TestSuite.NODE_SIZE, position).connectNode(2, 4, 1);
manager.addNode(5, TestSuite.NODE_SIZE, position).connectNode(2, 5, 2);
manager.addNode(6, TestSuite.NODE_SIZE, position).connectNode(2, 6, 3);
manager.layout();
manager.plot('testBaselineAligned2', { width: 1600, height: 800 });
console.log('OK!\n\n');
},
testEvents() {
console.log('testEvents:');
const position = { x: 0, y: 0 };
const manager = new LayoutManager(0, TestSuite.ROOT_NODE_SIZE);
manager.addNode(1, TestSuite.NODE_SIZE, position);
manager.addNode(2, TestSuite.NODE_SIZE, position);
manager.addNode(3, TestSuite.NODE_SIZE, position);
manager.addNode(4, TestSuite.NODE_SIZE, { x: 0, y: 60 });
manager.connectNode(0, 1, 0);
manager.connectNode(0, 2, 1);
manager.connectNode(1, 3, 0);
const events = [];
manager.addEvent('change', (event) => {
console.log(
`\tUpdated nodes: {id:${event.getId()
}, order: ${event.getOrder()
},position: {${event.getPosition().x
},${event.getPosition().y
}}`,
);
events.push(event);
});
manager.layout(true);
manager.plot('testEvents1', { width: 800, height: 200 });
console.log('\t--- Layout without changes should not affect the tree ---');
events.empty();
manager.layout(true);
manager.plot('testEvents2', { width: 800, height: 200 });
// Check no events where fired
$assert(events.length == 0, 'Unnecessary tree updated.');
console.log('OK!\n\n');
},
testEventsComplex() {
console.log('testEventsComplex:');
const position = { x: 0, y: 0 };
const manager = new LayoutManager(0, TestSuite.ROOT_NODE_SIZE);
manager.addNode(1, TestSuite.NODE_SIZE, position);
manager.addNode(2, TestSuite.NODE_SIZE, position);
manager.addNode(3, TestSuite.NODE_SIZE, position);
manager.addNode(4, TestSuite.NODE_SIZE, position);
manager.addNode(5, TestSuite.NODE_SIZE, position);
manager.addNode(6, TestSuite.NODE_SIZE, { x: 0, y: 60 });
manager.connectNode(0, 1, 0);
manager.connectNode(0, 2, 1);
manager.connectNode(0, 3, 2);
manager.connectNode(3, 4, 0);
manager.connectNode(3, 5, 1);
const events = [];
manager.addEvent('change', (event) => {
console.log(
`\tUpdated nodes: {id:${event.getId()
}, order: ${event.getOrder()
},position: {${event.getPosition().x
},${event.getPosition().y
}}`,
);
events.push(event);
});
manager.layout(true);
manager.plot('testEventsComplex1', { width: 800, height: 200 });
console.log('\t--- Connect a new node ---');
events.empty();
manager.connectNode(3, 6, 2);
manager.layout(true);
manager.plot('testEventsComplex2', { width: 800, height: 200 });
// Check only 4 nodes were repositioned
console.log(events.length);
$assert(events.length == 4, 'Only 4 nodes should be repositioned.');
console.log('OK!\n\n');
},
testDisconnect() {
console.log('testDisconnect:');
const position = { x: 0, y: 0 };
const manager = new LayoutManager(0, TestSuite.ROOT_NODE_SIZE);
manager.addNode(1, TestSuite.NODE_SIZE, position);
manager.addNode(2, TestSuite.NODE_SIZE, position);
manager.addNode(3, TestSuite.NODE_SIZE, position);
manager.addNode(4, TestSuite.NODE_SIZE, position);
manager.addNode(5, TestSuite.NODE_SIZE, position);
manager.addNode(6, TestSuite.NODE_SIZE, position);
manager.addNode(7, TestSuite.NODE_SIZE, position);
manager.connectNode(0, 1, 0);
manager.connectNode(1, 2, 0);
manager.connectNode(1, 3, 1);
manager.connectNode(1, 4, 2);
manager.connectNode(4, 5, 0);
manager.connectNode(5, 6, 0);
manager.connectNode(5, 7, 1);
const events = [];
manager.addEvent('change', (event) => {
const pos = event.getPosition();
const posStr = pos ? `,position: {${pos.x},${pos.y}` : '';
const node = manager.find(event.getId());
console.log(
`\tUpdated nodes: {id:${event.getId()
}, order: ${event.getOrder()
},${posStr
}}`,
);
events.push(event);
});
manager.layout(true);
manager.plot('testDisconnect1', { width: 1200, height: 400 });
console.log('--- Disconnect node 2 ---');
events.empty();
manager.disconnectNode(2);
manager.layout(true);
manager.plot('testDisconnect2', { width: 1200, height: 400 });
// Check that orders have been shifted accordingly
$assert(manager.find(2).getOrder() == 0, 'Node 2 should have order 0');
$assert(manager.find(3).getOrder() == 0, 'Node 3 should now have order 0');
$assert(manager.find(4).getOrder() == 1, 'Node 4 should have order 1');
console.log('--- Disconnect node 4 ---');
manager.disconnectNode(4);
manager.layout(true);
manager.plot('testDisconnect3', { width: 1200, height: 400 });
// Check that nodes 1 and 3 are now vertically aligned
$assert(
manager.find(1).getPosition().y == manager.find(3).getPosition().y,
'Nodes 1 and 3 should now be vertically aligned',
);
console.log('OK!\n\n');
},
testReconnect() {
console.log('testReconnect:');
const position = { x: 0, y: 0 };
const manager = new LayoutManager(0, TestSuite.ROOT_NODE_SIZE);
manager.addNode(1, TestSuite.NODE_SIZE, position);
manager.addNode(2, TestSuite.NODE_SIZE, position);
manager.addNode(3, TestSuite.NODE_SIZE, position);
manager.addNode(4, TestSuite.NODE_SIZE, position);
manager.addNode(5, TestSuite.NODE_SIZE, position);
manager.addNode(6, TestSuite.NODE_SIZE, position);
manager.addNode(7, TestSuite.NODE_SIZE, position);
manager.addNode(8, TestSuite.NODE_SIZE, position);
manager.addNode(9, TestSuite.NODE_SIZE, position);
manager.addNode(10, TestSuite.NODE_SIZE, position);
manager.addNode(11, TestSuite.NODE_SIZE, position);
manager.addNode(12, TestSuite.NODE_SIZE, position);
manager.connectNode(0, 1, 0);
manager.connectNode(0, 2, 1);
manager.connectNode(0, 3, 2);
manager.connectNode(0, 4, 3);
manager.connectNode(0, 5, 4);
manager.connectNode(1, 6, 0);
manager.connectNode(1, 7, 1);
manager.connectNode(7, 8, 0);
manager.connectNode(8, 9, 0);
manager.connectNode(5, 10, 0);
manager.connectNode(6, 11, 0);
manager.connectNode(6, 12, 1);
manager.layout();
manager.plot('testReconnect1', { width: 1200, height: 400 });
// Reconnect node 6 to node 4
console.log('\tReconnect node 6 to node 4');
manager.disconnectNode(6);
manager.connectNode(4, 6, 0);
manager.layout();
manager.plot('testReconnect2', { width: 1200, height: 400 });
// Check nodes are left aligned correctly
$assert(
manager.find(1).getPosition().y == manager.find(7).getPosition().y,
'Nodes 1 and 7 should be vertically aligned',
);
$assert(
manager.find(4).getPosition().y == manager.find(6).getPosition().y,
'Nodes 4 and 6 should be vertically aligned',
);
$assert(
manager.find(4).getPosition().x > manager.find(6).getPosition().x,
'Node 6 and their children should be to the left of node 4',
);
$assert(
manager.find(6).getPosition().x > manager.find(11).getPosition().x
&& manager.find(11).getPosition().x == manager.find(12).getPosition().x,
'Nodes 11 and 12 should be to the left of node 6 and horizontally aligned',
);
console.log('OK!\n\n');
},
testRemoveNode() {
console.log('testRemoveNode:');
const position = { x: 0, y: 0 };
const manager = new LayoutManager(0, TestSuite.ROOT_NODE_SIZE);
// Prepare a sample graph ...
manager.addNode(1, TestSuite.NODE_SIZE, position);
manager.addNode(2, TestSuite.NODE_SIZE, position);
manager.addNode(3, TestSuite.NODE_SIZE, position);
manager.addNode(4, TestSuite.NODE_SIZE, position);
manager.addNode(5, TestSuite.NODE_SIZE, position);
manager.addNode(6, TestSuite.NODE_SIZE, position);
manager.addNode(7, TestSuite.NODE_SIZE, position);
manager.addNode(8, TestSuite.NODE_SIZE, position);
manager.addNode(9, TestSuite.NODE_SIZE, position);
manager.connectNode(0, 1, 0);
manager.connectNode(0, 5, 1);
manager.connectNode(0, 6, 2);
manager.connectNode(0, 7, 3);
manager.connectNode(0, 8, 4);
manager.connectNode(0, 9, 5);
manager.connectNode(1, 2, 0);
manager.connectNode(1, 3, 1);
manager.connectNode(3, 4, 0);
const events = [];
manager.addEvent('change', (event) => {
const pos = event.getPosition();
const posStr = pos ? `,position: {${pos.x},${event.getPosition().y}` : '';
events.push(event);
});
manager.layout(true);
manager.plot('testRemoveNode1', { width: 1000, height: 200 });
console.log('\t--- Remove node 3 ---');
manager.removeNode(3);
manager.layout(true);
manager.plot('testRemoveNode2', { width: 1000, height: 200 });
// Check nodes are correctly aligned and node 6 is aligned with the root node
$assert(
manager.find(1).getPosition().y == manager.find(2).getPosition().y,
'Nodes 1 and 2 should be vertically algined',
);
$assert(
manager.find(6).getPosition().y == manager.find(0).getPosition().y,
'Node 6 should be aligned to the root node',
);
console.log('\t--- Remove node 6 ---');
manager.removeNode(6);
manager.layout(true);
manager.plot('testRemoveNode3', { width: 1000, height: 200 });
// Check orders were shifted accordingly
$assert(manager.find(8).getOrder() == 2, 'Node 8 should have order 2');
console.log('\t--- Remove node 5 ---');
manager.removeNode(5);
manager.layout(true);
manager.plot('testRemoveNode4', { width: 1000, height: 200 });
// Check orders were shifted accordingly
$assert(manager.find(7).getOrder() == 1, 'Node 7 should have order 1');
$assert(manager.find(9).getOrder() == 3, 'Node 9 should have order 3');
console.log('OK!\n\n');
},
testSize() {
console.log('testSize:');
const position = { x: 0, y: 0 };
const manager = new LayoutManager(0, TestSuite.ROOT_NODE_SIZE);
manager.addNode(1, { width: 60, height: 60 }, position);
manager.addNode(2, TestSuite.NODE_SIZE, position);
manager.addNode(3, { width: 260, height: 30 }, position);
manager.addNode(4, TestSuite.NODE_SIZE, position);
manager.addNode(5, TestSuite.NODE_SIZE, position);
manager.addNode(7, { width: 80, height: 80 }, position);
manager.addNode(8, TestSuite.NODE_SIZE, position);
manager.addNode(9, { width: 30, height: 30 }, position);
manager.addNode(10, TestSuite.NODE_SIZE, position);
manager.addNode(11, TestSuite.NODE_SIZE, position);
manager.addNode(12, { width: 100, height: 70 }, position);
manager.addNode(13, TestSuite.NODE_SIZE, position);
manager.addNode(14, TestSuite.NODE_SIZE, position);
manager.addNode(15, TestSuite.NODE_SIZE, position);
manager.addNode(16, TestSuite.NODE_SIZE, position);
manager.addNode(17, TestSuite.NODE_SIZE, position);
manager.connectNode(0, 1, 0);
manager.connectNode(1, 16, 0);
manager.connectNode(0, 2, 1);
manager.connectNode(0, 3, 2);
manager.connectNode(0, 4, 3);
manager.connectNode(0, 5, 4);
manager.connectNode(4, 7, 0);
manager.connectNode(7, 15, 0);
manager.connectNode(7, 17, 1);
manager.connectNode(4, 8, 1);
manager.connectNode(8, 9, 0);
manager.connectNode(3, 10, 0);
manager.connectNode(3, 11, 1);
manager.connectNode(9, 12, 0);
manager.connectNode(9, 13, 1);
manager.connectNode(13, 14, 0);
manager.layout();
manager.plot('testSize1', { width: 1400, height: 400 });
// Check that all enlarged nodes shift children accordingly
$assert(
manager.find(10).getPosition().x > manager.find(3).getPosition().x
&& manager.find(10).getPosition().x == manager.find(11).getPosition().x,
'Nodes 10 and 11 should be horizontally algined and to the right of enlarged node 3',
);
const xPosNode7 = manager.find(7).getPosition().x;
const xPosNode8 = manager.find(8).getPosition().x;
manager.updateNodeSize(4, { width: 100, height: 30 });
manager.layout();
manager.plot('testSize2', { width: 1400, height: 400 });
// Check that all enlarged nodes shift children accordingly
$assert(
manager.find(2).getPosition().x - manager.find(4).getPosition().x == 10,
'Node 4 should have been shifted by 10',
);
$assert(
xPosNode7 - manager.find(7).getPosition().x == 20,
'Node 7 should have been shifted by 20',
);
$assert(
xPosNode8 - manager.find(8).getPosition().x == 20,
'Node 8 should have been shifted by 20',
);
const graph2 = manager.plot('testSize3', { width: 1400, height: 400 });
this._plotPrediction(graph2, manager.predict(0, null, { x: -145, y: 400 }));
this._plotPrediction(graph2, manager.predict(9, null, { x: -330, y: 70 }));
this._plotPrediction(graph2, manager.predict(9, null, { x: -330, y: 120 }));
this._plotPrediction(graph2, manager.predict(0, null, { x: 15, y: 20 }));
// TODO(gb): make asserts
const graph3 = manager.plot('testSize4', { width: 1400, height: 400 });
this._plotPrediction(graph3, manager.predict(0, null, null));
this._plotPrediction(graph3, manager.predict(9, null, null));
this._plotPrediction(graph3, manager.predict(3, null, null));
this._plotPrediction(graph3, manager.predict(1, null, null));
// TODO(gb): make asserts
const yPosNode2 = manager.find(2).getPosition().y;
manager.updateNodeSize(7, { width: 80, height: 120 });
manager.layout();
manager.plot('testSize5', { width: 1400, height: 400 });
// Check that all enlarged nodes shift children accordingly
$assert(
yPosNode2 - manager.find(2).getPosition().y == 20,
'Node 2 should have been shifted by 20',
);
console.log('OK!\n\n');
},
testReconnectSingleNode() {
console.log('testReconnectSingleNode:');
const position = { x: 0, y: 0 };
const manager = new LayoutManager(0, TestSuite.ROOT_NODE_SIZE);
// Prepare a sample graph ...
manager.addNode(1, TestSuite.NODE_SIZE, position);
manager.connectNode(0, 1, 0);
manager.layout();
const graph = manager.plot('testReconnectSingleNode1', { width: 1000, height: 400 });
const prediction = manager.predict(0, null, { x: -50, y: 0 });
this._plotPrediction(graph, prediction);
// Check prediction is to the left of the root node
$assert(
prediction.position.x < manager.find(0).getPosition().x,
'Prediction should be to the left of the root node',
);
$assert(prediction.order == 1, 'Prediction should have order 1');
manager.disconnectNode(1);
manager.connectNode(0, 1, 1);
manager.layout();
manager.plot('testReconnectSingleNode2', { width: 1000, height: 400 });
// Check reconnected node is to the left of the root node
$assert(
manager.find(1).getPosition().x < manager.find(0).getPosition().x,
'Node 1 should now be to the left of the root node',
);
$assert(manager.find(1).getOrder() == 1, 'Node 1 should now have order 0');
},
_plotPrediction(canvas, prediction) {
if (!canvas) {
console.warn('no canvas in _plotPrediction. Remove this method if plot() not in use');
return;
}
const { position } = prediction;
const { order } = prediction;
console.log(
`\t\tprediction {order:${order
}, position: (${position.x
},${position.y
})}`,
);
const cx = position.x + canvas.width / 2 - TestSuite.NODE_SIZE.width / 2;
const cy = position.y + canvas.height / 2 - TestSuite.NODE_SIZE.height / 2;
canvas.rect(cx, cy, TestSuite.NODE_SIZE.width, TestSuite.NODE_SIZE.height);
},
});
(TestSuite.NODE_SIZE = { width: 80, height: 30 }),
(TestSuite.ROOT_NODE_SIZE = { width: 120, height: 40 });
export default TestSuite;