Skip to content

Commit f866126

Browse files
authored
Merge pull request #8689 from BenHenning/improve-workspace-svg-test-robustness
fix: Improve the robustness of workspace SVG tests.
2 parents 16c8600 + 94794af commit f866126

1 file changed

Lines changed: 108 additions & 168 deletions

File tree

tests/mocha/workspace_svg_test.js

Lines changed: 108 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,53 @@ suite('WorkspaceSvg', function () {
408408
});
409409

410410
suite('cleanUp', function () {
411+
assert.blockIsAtOrigin = function (actual, message) {
412+
assert.blockHasPosition(actual, 0, 0, message || 'block is at origin');
413+
};
414+
415+
assert.blockHasPositionX = function (actual, expectedX, message) {
416+
const position = actual.getRelativeToSurfaceXY();
417+
message = message || 'block has x value of ' + expectedX;
418+
assert.equal(position.x, expectedX, message);
419+
};
420+
421+
assert.blockHasPositionY = function (actual, expectedY, message) {
422+
const position = actual.getRelativeToSurfaceXY();
423+
message = message || 'block has y value of ' + expectedY;
424+
assert.equal(position.y, expectedY, message);
425+
};
426+
427+
assert.blockHasPosition = function (actual, expectedX, expectedY, message) {
428+
assert.blockHasPositionX(actual, expectedX, message);
429+
assert.blockHasPositionY(actual, expectedY, message);
430+
};
431+
432+
assert.blockIsAtNotOrigin = function (actual, message) {
433+
const position = actual.getRelativeToSurfaceXY();
434+
message = message || 'block is not at origin';
435+
assert.isTrue(position.x != 0 || position.y != 0, message);
436+
};
437+
438+
assert.blocksDoNotIntersect = function (a, b, message) {
439+
const rectA = a.getBoundingRectangle();
440+
const rectB = b.getBoundingRectangle();
441+
assert.isFalse(rectA.intersects(rectB), message || "a,b don't intersect");
442+
};
443+
444+
assert.blockIsAbove = function (a, b, message) {
445+
// Block a is above b iff a's bottom extreme is < b's top extreme.
446+
const rectA = a.getBoundingRectangle();
447+
const rectB = b.getBoundingRectangle();
448+
assert.isBelow(rectA.bottom, rectB.top, message || 'a is above b');
449+
};
450+
451+
assert.blockIsBelow = function (a, b, message) {
452+
// Block a is below b iff a's top extreme is > b's bottom extreme.
453+
const rectA = a.getBoundingRectangle();
454+
const rectB = b.getBoundingRectangle();
455+
assert.isAbove(rectA.top, rectB.bottom, message || 'a is below b');
456+
};
457+
411458
test('empty workspace does not change', function () {
412459
this.workspace.cleanUp();
413460

@@ -429,13 +476,8 @@ suite('WorkspaceSvg', function () {
429476
this.workspace.cleanUp();
430477

431478
const blocks = this.workspace.getTopBlocks(true);
432-
const origin = new Blockly.utils.Coordinate(0, 0);
433479
assert.equal(blocks.length, 1, 'workspace has one top-level block');
434-
assert.deepEqual(
435-
blocks[0].getRelativeToSurfaceXY(),
436-
origin,
437-
'block is at origin',
438-
);
480+
assert.blockIsAtOrigin(blocks[0]);
439481
});
440482

441483
test('single block at (10, 15) is moved to (0, 0)', function () {
@@ -453,14 +495,9 @@ suite('WorkspaceSvg', function () {
453495

454496
const topBlocks = this.workspace.getTopBlocks(true);
455497
const allBlocks = this.workspace.getAllBlocks(false);
456-
const origin = new Blockly.utils.Coordinate(0, 0);
457498
assert.equal(topBlocks.length, 1, 'workspace has one top-level block');
458499
assert.equal(allBlocks.length, 1, 'workspace has one block overall');
459-
assert.deepEqual(
460-
topBlocks[0].getRelativeToSurfaceXY(),
461-
origin,
462-
'block is at origin',
463-
);
500+
assert.blockIsAtOrigin(topBlocks[0]);
464501
});
465502

466503
test('single block at (10, 15) with child is moved as unit to (0, 0)', function () {
@@ -487,19 +524,10 @@ suite('WorkspaceSvg', function () {
487524

488525
const topBlocks = this.workspace.getTopBlocks(true);
489526
const allBlocks = this.workspace.getAllBlocks(false);
490-
const origin = new Blockly.utils.Coordinate(0, 0);
491527
assert.equal(topBlocks.length, 1, 'workspace has one top-level block');
492528
assert.equal(allBlocks.length, 2, 'workspace has two blocks overall');
493-
assert.deepEqual(
494-
topBlocks[0].getRelativeToSurfaceXY(),
495-
origin,
496-
'block is at origin',
497-
);
498-
assert.notDeepEqual(
499-
allBlocks[1].getRelativeToSurfaceXY(),
500-
origin,
501-
'child is not at origin',
502-
);
529+
assert.blockIsAtOrigin(topBlocks[0]); // Parent block.
530+
assert.blockIsAtNotOrigin(allBlocks[1]); // Child block.
503531
});
504532

505533
// TODO(#8676): Reenable once test passes reliably.
@@ -524,19 +552,9 @@ suite('WorkspaceSvg', function () {
524552
const topBlocks = this.workspace.getTopBlocks(true);
525553
const block1 = this.workspace.getBlockById('block1');
526554
const block2 = this.workspace.getBlockById('block2');
527-
const origin = new Blockly.utils.Coordinate(0, 0);
528-
const belowBlock2 = new Blockly.utils.Coordinate(0, 50);
529555
assert.equal(topBlocks.length, 2, 'workspace has two top-level blocks');
530-
assert.deepEqual(
531-
block2.getRelativeToSurfaceXY(),
532-
origin,
533-
'block2 is at origin',
534-
);
535-
assert.deepEqual(
536-
block1.getRelativeToSurfaceXY(),
537-
belowBlock2,
538-
'block1 is below block2',
539-
);
556+
assert.blockIsAtOrigin(block2);
557+
assert.blockIsBelow(block1, block2);
540558
});
541559

542560
// TODO(#8676): Reenable once test passes reliably.
@@ -564,19 +582,9 @@ suite('WorkspaceSvg', function () {
564582
const topBlocks = this.workspace.getTopBlocks(true);
565583
const block1 = this.workspace.getBlockById('block1');
566584
const block2 = this.workspace.getBlockById('block2');
567-
const origin = new Blockly.utils.Coordinate(0, 0);
568-
const belowBlock1 = new Blockly.utils.Coordinate(0, 50);
569585
assert.equal(topBlocks.length, 2, 'workspace has two top-level blocks');
570-
assert.deepEqual(
571-
block1.getRelativeToSurfaceXY(),
572-
origin,
573-
'block1 is at origin',
574-
);
575-
assert.deepEqual(
576-
block2.getRelativeToSurfaceXY(),
577-
belowBlock1,
578-
'block2 is below block1',
579-
);
586+
assert.blockIsAtOrigin(block1);
587+
assert.blockIsBelow(block2, block1);
580588
});
581589

582590
test('two overlapping blocks with snapping are moved to grid-aligned positions', function () {
@@ -605,19 +613,9 @@ suite('WorkspaceSvg', function () {
605613
const topBlocks = this.workspace.getTopBlocks(true);
606614
const block1 = this.workspace.getBlockById('block1');
607615
const block2 = this.workspace.getBlockById('block2');
608-
const snappedOffOrigin = new Blockly.utils.Coordinate(10, 10);
609-
const belowBlock1 = new Blockly.utils.Coordinate(10, 70);
610616
assert.equal(topBlocks.length, 2, 'workspace has two top-level blocks');
611-
assert.deepEqual(
612-
block1.getRelativeToSurfaceXY(),
613-
snappedOffOrigin,
614-
'block1 is near origin',
615-
);
616-
assert.deepEqual(
617-
block2.getRelativeToSurfaceXY(),
618-
belowBlock1,
619-
'block2 is below block1',
620-
);
617+
assert.blockHasPosition(block1, 10, 10, 'block1 is at snapped origin');
618+
assert.blockIsBelow(block2, block1);
621619
});
622620

623621
// TODO(#8676): Reenable once test passes reliably.
@@ -653,36 +651,28 @@ suite('WorkspaceSvg', function () {
653651
const allBlocks = this.workspace.getAllBlocks(false);
654652
const block1 = this.workspace.getBlockById('block1');
655653
const block2 = this.workspace.getBlockById('block2');
656-
const origin = new Blockly.utils.Coordinate(0, 0);
657-
const belowBlock1 = new Blockly.utils.Coordinate(0, 50);
658-
const block1Pos = block1.getRelativeToSurfaceXY();
659-
const block2Pos = block2.getRelativeToSurfaceXY();
660-
const block1ChildPos = block1.getChildren()[0].getRelativeToSurfaceXY();
661-
const block2ChildPos = block2.getChildren()[0].getRelativeToSurfaceXY();
654+
const block1Child = block1.getChildren()[0];
655+
const block2Child = block2.getChildren()[0];
656+
657+
// Note that the x position tests below are verifying that each block's
658+
// child isn't exactly aligned with it (however, they does overlap since
659+
// the child block has an input connection with its parent).
662660
assert.equal(topBlocks.length, 2, 'workspace has two top-level block2');
663661
assert.equal(allBlocks.length, 4, 'workspace has four blocks overall');
664-
assert.deepEqual(block1Pos, origin, 'block1 is at origin');
665-
assert.deepEqual(block2Pos, belowBlock1, 'block2 is below block1');
666-
assert.isAbove(
667-
block1ChildPos.x,
668-
block1Pos.x,
669-
"block1's child is right of it",
670-
);
671-
assert.isBelow(
672-
block1ChildPos.y,
673-
block2Pos.y,
674-
"block1's child is above block 2",
675-
);
662+
assert.blockIsAtOrigin(block1);
663+
assert.blockIsBelow(block2, block1);
676664
assert.isAbove(
677-
block2ChildPos.x,
678-
block2Pos.x,
679-
"block2's child is right of it",
665+
block1.getChildren()[0].getRelativeToSurfaceXY().x,
666+
block1.getRelativeToSurfaceXY().x,
667+
"block1's child is right of its start",
680668
);
669+
assert.blockIsAbove(block1Child, block2);
681670
assert.isAbove(
682-
block2ChildPos.y,
683-
block1Pos.y,
684-
"block2's child is below block 1",
671+
block2.getChildren()[0].getRelativeToSurfaceXY().x,
672+
block2.getRelativeToSurfaceXY().x,
673+
"block2's child is right of its start",
685674
);
675+
assert.blockIsBelow(block2Child, block1);
686676
});
687677

688678
// TODO(#8676): Reenable once test passes reliably.
@@ -742,19 +732,9 @@ suite('WorkspaceSvg', function () {
742732
const topBlocks = this.workspace.getTopBlocks(true);
743733
const block1 = this.workspace.getBlockById('block1');
744734
const block2 = this.workspace.getBlockById('block2');
745-
const origin = new Blockly.utils.Coordinate(0, 0);
746-
const belowBlock1 = new Blockly.utils.Coordinate(0, 144);
747735
assert.equal(topBlocks.length, 2, 'workspace has two top-level blocks');
748-
assert.deepEqual(
749-
block1.getRelativeToSurfaceXY(),
750-
origin,
751-
'block1 is at origin',
752-
);
753-
assert.deepEqual(
754-
block2.getRelativeToSurfaceXY(),
755-
belowBlock1,
756-
'block2 is below block1',
757-
);
736+
assert.blockIsAtOrigin(block1);
737+
assert.blockIsBelow(block2, block1);
758738
});
759739

760740
test('five overlapping blocks are moved in-order as one column', function () {
@@ -780,32 +760,21 @@ suite('WorkspaceSvg', function () {
780760
this.workspace.cleanUp();
781761

782762
const topBlocks = this.workspace.getTopBlocks(true);
783-
const block1Pos = this.workspace
784-
.getBlockById('block1')
785-
.getRelativeToSurfaceXY();
786-
const block2Pos = this.workspace
787-
.getBlockById('block2')
788-
.getRelativeToSurfaceXY();
789-
const block3Pos = this.workspace
790-
.getBlockById('block3')
791-
.getRelativeToSurfaceXY();
792-
const block4Pos = this.workspace
793-
.getBlockById('block4')
794-
.getRelativeToSurfaceXY();
795-
const block5Pos = this.workspace
796-
.getBlockById('block5')
797-
.getRelativeToSurfaceXY();
798-
const origin = new Blockly.utils.Coordinate(0, 0);
763+
const block1 = this.workspace.getBlockById('block1');
764+
const block2 = this.workspace.getBlockById('block2');
765+
const block3 = this.workspace.getBlockById('block3');
766+
const block4 = this.workspace.getBlockById('block4');
767+
const block5 = this.workspace.getBlockById('block5');
799768
assert.equal(topBlocks.length, 5, 'workspace has five top-level blocks');
800-
assert.deepEqual(block1Pos, origin, 'block1 is at origin');
801-
assert.equal(block2Pos.x, 0, 'block2.x is at 0');
802-
assert.equal(block3Pos.x, 0, 'block3.x is at 0');
803-
assert.equal(block4Pos.x, 0, 'block4.x is at 0');
804-
assert.equal(block5Pos.x, 0, 'block5.x is at 0');
805-
assert.isAbove(block2Pos.y, block1Pos.y, 'block2 is below block1');
806-
assert.isAbove(block3Pos.y, block2Pos.y, 'block3 is below block2');
807-
assert.isAbove(block4Pos.y, block3Pos.y, 'block4 is below block3');
808-
assert.isAbove(block5Pos.y, block4Pos.y, 'block5 is below block4');
769+
assert.blockIsAtOrigin(block1);
770+
assert.blockHasPositionX(block2, 0);
771+
assert.blockHasPositionX(block3, 0);
772+
assert.blockHasPositionX(block4, 0);
773+
assert.blockHasPositionX(block5, 0);
774+
assert.blockIsBelow(block2, block1);
775+
assert.blockIsBelow(block3, block2);
776+
assert.blockIsBelow(block4, block3);
777+
assert.blockIsBelow(block5, block4);
809778
});
810779

811780
test('single immovable block at (10, 15) is not moved', function () {
@@ -824,14 +793,9 @@ suite('WorkspaceSvg', function () {
824793

825794
const topBlocks = this.workspace.getTopBlocks(true);
826795
const allBlocks = this.workspace.getAllBlocks(false);
827-
const origPos = new Blockly.utils.Coordinate(10, 15);
828796
assert.equal(topBlocks.length, 1, 'workspace has one top-level block');
829797
assert.equal(allBlocks.length, 1, 'workspace has one block overall');
830-
assert.deepEqual(
831-
topBlocks[0].getRelativeToSurfaceXY(),
832-
origPos,
833-
'block is at (10, 15)',
834-
);
798+
assert.blockHasPosition(topBlocks[0], 10, 15);
835799
});
836800

837801
test('multiple block types immovable blocks are not moved', function () {
@@ -914,53 +878,29 @@ suite('WorkspaceSvg', function () {
914878
this.workspace.cleanUp();
915879

916880
const topBlocks = this.workspace.getTopBlocks(true);
917-
const block1Rect = this.workspace
918-
.getBlockById('block1')
919-
.getBoundingRectangle();
920-
const block2Rect = this.workspace
921-
.getBlockById('block2')
922-
.getBoundingRectangle();
923-
const block3Rect = this.workspace
924-
.getBlockById('block3')
925-
.getBoundingRectangle();
926-
const block4Rect = this.workspace
927-
.getBlockById('block4')
928-
.getBoundingRectangle();
929-
const block5Rect = this.workspace
930-
.getBlockById('block5')
931-
.getBoundingRectangle();
881+
const block1 = this.workspace.getBlockById('block1');
882+
const block2 = this.workspace.getBlockById('block2');
883+
const block3 = this.workspace.getBlockById('block3');
884+
const block4 = this.workspace.getBlockById('block4');
885+
const block5 = this.workspace.getBlockById('block5');
932886
assert.equal(topBlocks.length, 5, 'workspace has five top-level blocks');
933887
// Check that immovable blocks haven't moved.
934-
assert.equal(block2Rect.left, 10, 'block2.x is at 10');
935-
assert.equal(block2Rect.top, 20, 'block2.y is at 20');
936-
assert.equal(block5Rect.left, 20, 'block5.x is at 20');
937-
assert.equal(block5Rect.top, 200, 'block5.y is at 200');
888+
assert.blockHasPosition(block2, 10, 20);
889+
assert.blockHasPosition(block5, 20, 200);
938890
// Check that movable positions have correctly been left-aligned.
939-
assert.equal(block1Rect.left, 0, 'block1.x is at 0');
940-
assert.equal(block3Rect.left, 0, 'block3.x is at 0');
941-
assert.equal(block4Rect.left, 0, 'block4.x is at 0');
891+
assert.blockHasPositionX(block1, 0);
892+
assert.blockHasPositionX(block3, 0);
893+
assert.blockHasPositionX(block4, 0);
942894
// Block order should be: 2, 1, 3, 5, 4 since 2 and 5 are immovable.
943-
assert.isAbove(block1Rect.top, block2Rect.top, 'block1 is below block2');
944-
assert.isAbove(block3Rect.top, block1Rect.top, 'block3 is below block1');
945-
assert.isAbove(block5Rect.top, block3Rect.top, 'block5 is below block3');
946-
assert.isAbove(block4Rect.top, block5Rect.top, 'block4 is below block5');
895+
assert.blockIsBelow(block1, block2);
896+
assert.blockIsBelow(block3, block1);
897+
assert.blockIsBelow(block5, block3);
898+
assert.blockIsBelow(block4, block5);
947899
// Ensure no blocks intersect (can check in order due to the position verification above).
948-
assert.isFalse(
949-
block2Rect.intersects(block1Rect),
950-
'block2/block1 do not intersect',
951-
);
952-
assert.isFalse(
953-
block1Rect.intersects(block3Rect),
954-
'block1/block3 do not intersect',
955-
);
956-
assert.isFalse(
957-
block3Rect.intersects(block5Rect),
958-
'block3/block5 do not intersect',
959-
);
960-
assert.isFalse(
961-
block5Rect.intersects(block4Rect),
962-
'block5/block4 do not intersect',
963-
);
900+
assert.blocksDoNotIntersect(block2, block1);
901+
assert.blocksDoNotIntersect(block1, block3);
902+
assert.blocksDoNotIntersect(block3, block5);
903+
assert.blocksDoNotIntersect(block5, block4);
964904
});
965905
});
966906

0 commit comments

Comments
 (0)