From 1f00ed05d867e7a2ed6d75cd333291d90f52b243 Mon Sep 17 00:00:00 2001 From: Val Erastov Date: Tue, 27 Dec 2016 01:41:35 -0800 Subject: [PATCH] add couple tests for solver --- web/test/cases/constraints.js | 44 ++++++++++++++++++++++++++++ web/test/cases/parametric.js | 18 ++++++++++++ web/test/cases/segment.js | 48 +++++++++--------------------- web/test/suites.js | 14 ++++++--- web/test/test.js | 6 ++++ web/test/utils/mouse-event.js | 5 +++- web/test/utils/sketcher-utils.js | 50 +++++++++++++++++++++++++++++--- 7 files changed, 142 insertions(+), 43 deletions(-) create mode 100644 web/test/cases/constraints.js create mode 100644 web/test/cases/parametric.js diff --git a/web/test/cases/constraints.js b/web/test/cases/constraints.js new file mode 100644 index 00000000..5a3d96e3 --- /dev/null +++ b/web/test/cases/constraints.js @@ -0,0 +1,44 @@ +import * as test from '../test' +import * as sketcher_utils from '../utils/sketcher-utils' + +export default { + testCoincident: function (env) { + test.emptySketch(env.test((win, app) => { + const s1 = new sketcher_utils.TestSegment(10, 10, 100, 100); + const s2 = new sketcher_utils.TestSegment(50, 10, 150, 100); + s1.add(app); + s2.add(app); + env.assertEquals(2, app.viewer.activeLayer.objects.length); + env.assertEquals(0, sketcher_utils.getConstraints(app).length); + sketcher_utils.click(app, s1.b); + sketcher_utils.click(app, s2.a, {shiftKey: true}); + env.assertEquals(2, app.viewer.selected.length); + app.actions['coincident'].action(); + var constraints = sketcher_utils.getConstraints(app); + env.assertEquals(1, constraints.length); + env.assertEquals('coi', constraints[0].NAME); + env.done(); + })); + }, + + testPerpendicular: function (env) { + test.emptySketch(env.test((win, app) => { + const s1 = new sketcher_utils.TestSegment(10, 10, 100, 100); + const s2 = new sketcher_utils.TestSegment(50, 10, 150, 100); + const ss1 = s1.add(app); + const ss2 = s2.add(app); + env.assertEquals(2, app.viewer.activeLayer.objects.length); + env.assertEquals(0, sketcher_utils.getConstraints(app).length); + sketcher_utils.click(app, s1.middle()); + sketcher_utils.click(app, s2.middle(), {shiftKey: true}); + app.actions['perpendicularConstraint'].action(); + const constraints = sketcher_utils.getConstraints(app); + env.assertEquals(1, constraints.length); + env.assertEquals('perpendicular', constraints[0].NAME); + const dotProduct = sketcher_utils.segmentAsVector(ss1).dot(sketcher_utils.segmentAsVector(ss2)); + env.assertFloatEquals(0, dotProduct); + env.done(); + })); + } + +} \ No newline at end of file diff --git a/web/test/cases/parametric.js b/web/test/cases/parametric.js new file mode 100644 index 00000000..08aaf1c4 --- /dev/null +++ b/web/test/cases/parametric.js @@ -0,0 +1,18 @@ +import * as test from '../test' + +export default { + testRedundantElimination: function (env) { + test.emptySketch(env.test((win, app) => { + env.fail('implement me'); + env.done(); + })); + }, + + testNotEliminateCoupledRedundantConstraints: function (env) { + test.emptySketch(env.test((win, app) => { + env.fail('implement me'); + env.done(); + })); + } + +} \ No newline at end of file diff --git a/web/test/cases/segment.js b/web/test/cases/segment.js index ad885e85..757ab38e 100644 --- a/web/test/cases/segment.js +++ b/web/test/cases/segment.js @@ -4,23 +4,11 @@ import * as keyboard from '../utils/keyboard' import {TestMouseEvent} from '../utils/mouse-event' import Vector from '../../app/math/vector'; -function addSegment(app, aX, aY, bX, bY) { - app.actions['addSegment'].action(); - const tool = app.viewer.toolManager.tool; - tool.mousemove(new TestMouseEvent(aX, aY)); - tool.mouseup(new TestMouseEvent(aX, aY)); - tool.mousemove(new TestMouseEvent(bX, bY)); - const segment = tool.line; - tool.mouseup(new TestMouseEvent(bX, bY)); - app.viewer.toolManager.releaseControl(); - return segment; -} - export default { testSegmentWizard: function(env) { test.emptySketch(env.test((win, app) => { env.assertEquals(0, app.viewer.activeLayer.objects.length); - addSegment(app, 10, 10, 100, 100); + sketcher_utils.addSegment(app, 10, 10, 100, 100); env.assertEquals(1, app.viewer.activeLayer.objects.length); const segment = app.viewer.activeLayer.objects[0]; env.assertEquals('TCAD.TWO.Segment', segment._class); @@ -33,7 +21,7 @@ export default { testSaveLoad: function(env) { test.emptySketch(env.test((win, app) => { env.assertEquals(0, app.viewer.activeLayer.objects.length); - addSegment(app, 10, 10, 100, 100); + sketcher_utils.addSegment(app, 10, 10, 100, 100); app.actions['save'].action(); test.sketch(env.test((win, app) => { env.assertEquals(1, app.viewer.activeLayer.objects.length); @@ -46,9 +34,9 @@ export default { testSelection: function(env) { test.emptySketch(env.test((win, app) => { - addSegment(app, 10, 10, 100, 100); + sketcher_utils.addSegment(app, 10, 10, 100, 100); env.assertEquals(0, app.viewer.selected.length); - sketcher_utils.click(app, 50, 50); + sketcher_utils.clickXY(app, 50, 50); env.assertEquals(1, app.viewer.selected.length); env.done(); })); @@ -56,10 +44,10 @@ export default { testSelectionNeighborhood: function(env) { test.emptySketch(env.test((win, app) => { - addSegment(app, 10, 10, 100, 100); + sketcher_utils.addSegment(app, 10, 10, 100, 100); env.assertEquals(0, app.viewer.selected.length); // this point technically isn't on the line but should trigger the selection - sketcher_utils.click(app, 55, 50); + sketcher_utils.clickXY(app, 55, 50); env.assertEquals(1, app.viewer.selected.length); env.assertEquals('TCAD.TWO.Segment', app.viewer.selected[0]._class); env.done(); @@ -68,9 +56,9 @@ export default { testRemove: function(env) { test.emptySketch(env.test((win, app) => { - const segment = addSegment(app, 10, 10, 100, 100); + const segment = sketcher_utils.addSegment(app, 10, 10, 100, 100); env.assertEquals(1, app.viewer.activeLayer.objects.length); - sketcher_utils.click(app, 50, 50); + sketcher_utils.clickXY(app, 50, 50); const keyboardEvent = keyboard.keyCode('keydown', 8); win.dispatchEvent(keyboardEvent); env.assertEquals(0, app.viewer.activeLayer.objects.length); @@ -80,8 +68,8 @@ export default { testSnapFirstPoint: function(env) { test.emptySketch(env.test((win, app) => { - const s1 = addSegment(app, 10, 10, 100, 100); - const s2 = addSegment(app, 102, 102, 50, 10); + const s1 = sketcher_utils.addSegment(app, 10, 10, 100, 100); + const s2 = sketcher_utils.addSegment(app, 102, 102, 50, 10); const constraints = sketcher_utils.getConstraints(app); env.assertEquals(1, constraints.length); env.assertEquals('coi', constraints[0].NAME); @@ -95,8 +83,8 @@ export default { testSnapSecondPoint: function(env) { test.emptySketch(env.test((win, app) => { - const s1 = addSegment(app, 10, 10, 100, 100); - const s2 = addSegment(app, 50, 10, 102, 102); + const s1 = sketcher_utils.addSegment(app, 10, 10, 100, 100); + const s2 = sketcher_utils.addSegment(app, 50, 10, 102, 102); const constraints = sketcher_utils.getConstraints(app); env.assertEquals(1, constraints.length); env.assertEquals('coi', constraints[0].NAME); @@ -110,7 +98,7 @@ export default { testEndPointMove: function(env) { test.emptySketch(env.test((win, app) => { - const segment = addSegment(app, 10, 10, 100, 100); + const segment = sketcher_utils.addSegment(app, 10, 10, 100, 100); sketcher_utils.move(app, vec(100, 100), vec(200, 150)); //should be still env.assertPoint2DEquals(sketcher_utils.toModel(app, 10, 10), segment.a); @@ -124,7 +112,7 @@ export default { test.emptySketch(env.test((win, app) => { const initA = vec(10, 10); const initB = vec(100, 100); - const segment = addSegment(app, initA.x, initA.y, initB.x, initB.y); + const segment = sketcher_utils.addSegment(app, initA.x, initA.y, initB.x, initB.y); const from = vec(50, 50); const moveDelta = vec(100, 50); sketcher_utils.move(app, from, from.plus(moveDelta)); @@ -139,11 +127,3 @@ export default { function vec(x, y, z) { return new Vector(x, y, z); } - -function collectObjects(visitable) { - const objects = []; - visitable.accept((o) => { - objects.push(o); - }); - return objects; -} \ No newline at end of file diff --git a/web/test/suites.js b/web/test/suites.js index 89c7e2b2..650b0113 100644 --- a/web/test/suites.js +++ b/web/test/suites.js @@ -1,9 +1,15 @@ export default { - SketcherIO: [ - TestCase('segment'), - TestCase('arc') + SketcherObjects: [ + TestCase('segment'), + TestCase('arc'), ], - + + SketcherSolver: [ + TestCase('constraints'), + TestCase('parametric'), + + ], + SketcherTools: [ ], diff --git a/web/test/test.js b/web/test/test.js index 212af68a..a20c2527 100644 --- a/web/test/test.js +++ b/web/test/test.js @@ -63,6 +63,12 @@ export class TestEnv { } } + assertFloatEquals(expected, actual, msg) { + if (Math.abs(expected - actual) >= 1E-6) { + this.fail('assertFloatEquals: Expected: ' + expected + ' but was ' + actual, msg); + } + } + assertPointXY2DEquals(expectedX, expectedY, actual, msg) { if (actual.x !== expectedX || actual.y !== expectedY) { this.fail('assertPoint2DEquals: Expected: (' + expectedX + ', ' + expectedY + ') but was (' + actual.x + ', ' + actual.y + ')' , msg); diff --git a/web/test/utils/mouse-event.js b/web/test/utils/mouse-event.js index 93f36145..0d197aa5 100644 --- a/web/test/utils/mouse-event.js +++ b/web/test/utils/mouse-event.js @@ -1,4 +1,4 @@ -export function TestMouseEvent(x, y, type) { +export function TestMouseEvent(x, y, type, attrs) { this.type = type ? type : 'click'; this.canBubble = true; this.cancelable = true; @@ -17,4 +17,7 @@ export function TestMouseEvent(x, y, type) { this.metaKey = false; this.button = 0; this.relatedTarget = null; + if (attrs) { + Object.assign(this, attrs); + } } diff --git a/web/test/utils/sketcher-utils.js b/web/test/utils/sketcher-utils.js index f804b57b..0524aa5d 100644 --- a/web/test/utils/sketcher-utils.js +++ b/web/test/utils/sketcher-utils.js @@ -1,4 +1,5 @@ import {TestMouseEvent} from './mouse-event' +import Vector from '../../app/math/vector'; export function toModel(app, x, y) { return app.viewer._screenToModel(x, y); @@ -9,12 +10,20 @@ export function toModelP(app, point) { } export function getConstraints(app) { - return app.viewer.parametricManager.subSystems[0].constraints; + const subSystems = app.viewer.parametricManager.subSystems; + if (subSystems.length == 0) { + return []; + } + return subSystems[0].constraints; } -export function click(app, x, y) { - app.viewer.toolManager.tool.mousedown(new TestMouseEvent(x, y)); - app.viewer.toolManager.tool.mouseup(new TestMouseEvent(x, y)); +export function click(app, point, attrs) { + clickXY(app, point.x, point.y, attrs); +} + +export function clickXY(app, x, y, attrs) { + app.viewer.toolManager.tool.mousedown(new TestMouseEvent(x, y, 'mousedown', attrs)); + app.viewer.toolManager.tool.mouseup(new TestMouseEvent(x, y, 'mouseup', attrs)); } export function move(app, from, to) { @@ -23,3 +32,36 @@ export function move(app, from, to) { toolManager.tool.mousemove(new TestMouseEvent(to.x, to.y)); toolManager.tool.mouseup(new TestMouseEvent(to.x, to.y)); } + +export function addSegment(app, aX, aY, bX, bY) { + app.actions['addSegment'].action(); + const tool = app.viewer.toolManager.tool; + tool.mousemove(new TestMouseEvent(aX, aY)); + tool.mouseup(new TestMouseEvent(aX, aY)); + tool.mousemove(new TestMouseEvent(bX, bY)); + const segment = tool.line; + tool.mouseup(new TestMouseEvent(bX, bY)); + app.viewer.toolManager.releaseControl(); + return segment; +} + +export function segmentAsVector(segment) { + return new Vector(segment.b.x - segment.a.x, segment.b.y - segment.a.y); +} + +export class TestSegment { + constructor(aX, aY, bX, bY) { + this.a = new Vector(aX, aY); + this.b = new Vector(bX, bY); + this.v = this.b.minus(this.a); + } + + middle() { + const half = this.v.multiply(0.5); + return this.a.plus(half); + } + + add(app) { + return addSegment(app, this.a.x, this.a.y, this.b.x, this.b.y); + } +} \ No newline at end of file