From 1868098eb4ac73be93a0c0928951554d2b6f0a50 Mon Sep 17 00:00:00 2001 From: Andrei Cioara Date: Fri, 11 Sep 2015 16:52:05 -0700 Subject: [PATCH 01/12] [refactorMouseDispatcher] extracted svg creation in a beforeEach --- test/dispatchers/mouseDispatcherTests.ts | 552 +++++++++++------------ 1 file changed, 272 insertions(+), 280 deletions(-) diff --git a/test/dispatchers/mouseDispatcherTests.ts b/test/dispatchers/mouseDispatcherTests.ts index 83c1ba7c25..1049f59ebc 100644 --- a/test/dispatchers/mouseDispatcherTests.ts +++ b/test/dispatchers/mouseDispatcherTests.ts @@ -3,292 +3,284 @@ describe("Dispatchers", () => { describe("Mouse Dispatcher", () => { - it("getDispatcher() creates only one Dispatcher.Mouse per ", () => { - let svg = TestMethods.generateSVG(); + describe("Basic usage", () => { + let SVG_WIDTH = 400; + let SVG_HEIGHT = 400; - let md1 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - assert.isNotNull(md1, "created a new Dispatcher on an SVG"); - let md2 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - assert.strictEqual(md1, md2, "returned the existing Dispatcher if called again with same "); + let svg: d3.Selection; - svg.remove(); - }); - - it("lastMousePosition() defaults to a non-null value", () => { - let svg = TestMethods.generateSVG(); - - let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - let p = md.lastMousePosition(); - assert.isNotNull(p, "returns a value after initialization"); - assert.isNotNull(p.x, "x value is set"); - assert.isNotNull(p.y, "y value is set"); - - svg.remove(); - }); - - it("can remove callbacks by passing null", () => { - let targetWidth = 400, targetHeight = 400; - let target = TestMethods.generateSVG(targetWidth, targetHeight); - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - target.append("rect").attr("width", targetWidth).attr("height", targetHeight); - - let targetX = 17; - let targetY = 76; - - let md = Plottable.Dispatchers.Mouse.getDispatcher( target.node()); - - let cb1Called = false; - let cb1 = (p: Plottable.Point, e: MouseEvent) => cb1Called = true; - let cb2Called = false; - let cb2 = (p: Plottable.Point, e: MouseEvent) => cb2Called = true; - - md.onMouseMove(cb1); - md.onMouseMove(cb2); - TestMethods.triggerFakeMouseEvent("mousemove", target, targetX, targetY); - assert.isTrue(cb1Called, "callback 1 was called on mousemove"); - assert.isTrue(cb2Called, "callback 2 was called on mousemove"); - - cb1Called = false; - cb2Called = false; - md.offMouseMove(cb1); - TestMethods.triggerFakeMouseEvent("mousemove", target, targetX, targetY); - assert.isFalse(cb1Called, "callback was not called after blanking"); - assert.isTrue(cb2Called, "callback 2 was still called"); - - target.remove(); - }); - - it("doesn't call callbacks if not in the DOM", () => { - let targetWidth = 400, targetHeight = 400; - let target = TestMethods.generateSVG(targetWidth, targetHeight); - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - target.append("rect").attr("width", targetWidth).attr("height", targetHeight); - - let targetX = 17; - let targetY = 76; - - let md = Plottable.Dispatchers.Mouse.getDispatcher( target.node()); - - let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => callbackWasCalled = true; - - md.onMouseMove(callback); - TestMethods.triggerFakeMouseEvent("mousemove", target, targetX, targetY); - assert.isTrue(callbackWasCalled, "callback was called on mousemove"); - - target.remove(); - callbackWasCalled = false; - TestMethods.triggerFakeMouseEvent("mousemove", target, targetX, targetY); - assert.isFalse(callbackWasCalled, "callback was not called after was removed from DOM"); - - md.offMouseMove(callback); - }); - - it("doesn't call callbacks for clicks if obscured by overlay", () => { - let targetWidth = 400, targetHeight = 400; - let target = TestMethods.generateSVG(targetWidth, targetHeight); - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - target.append("rect").attr("width", targetWidth).attr("height", targetHeight); - - let targetX = 17; - let targetY = 76; - - let md = Plottable.Dispatchers.Mouse.getDispatcher( target.node()); - - let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => callbackWasCalled = true; - - md.onMouseDown(callback); - TestMethods.triggerFakeMouseEvent("mousedown", target, targetX, targetY); - assert.isTrue(callbackWasCalled, "callback was called on mousedown"); - - let element = target[0][0]; - let position = { x: 0, y: 0 }; - while (element != null) { - position.x += (element.offsetLeft || element.clientLeft || 0); - position.y += (element.offsetTop || element.clientTop || 0); - element = (element.offsetParent || element.parentNode); - } - - let overlay = TestMethods.getSVGParent().append("div") - .style({ - height: "400px", - width: "400px", - position: "absolute", - top: position.y + "px", - left: position.x + "px" - }); - - callbackWasCalled = false; - TestMethods.triggerFakeMouseEvent("mousedown", overlay, targetX, targetY); - assert.isFalse(callbackWasCalled, "callback was not called on mousedown on overlay"); - - md.offMouseDown(callback); - target.remove(); - overlay.remove(); - }); - - it("calls callbacks on mouseover, mousemove, and mouseout", () => { - let targetWidth = 400, targetHeight = 400; - let target = TestMethods.generateSVG(targetWidth, targetHeight); - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - target.append("rect").attr("width", targetWidth).attr("height", targetHeight); - - let targetX = 17; - let targetY = 76; - let expectedPoint = { - x: targetX, - y: targetY - }; - - let md = Plottable.Dispatchers.Mouse.getDispatcher( target.node()); - - let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => { - callbackWasCalled = true; - TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); - assert.isNotNull(e, "mouse event was passed to the callback"); - }; - - md.onMouseMove(callback); - - TestMethods.triggerFakeMouseEvent("mouseover", target, targetX, targetY); - assert.isTrue(callbackWasCalled, "callback was called on mouseover"); - callbackWasCalled = false; - TestMethods.triggerFakeMouseEvent("mousemove", target, targetX, targetY); - assert.isTrue(callbackWasCalled, "callback was called on mousemove"); - callbackWasCalled = false; - TestMethods.triggerFakeMouseEvent("mouseout", target, targetX, targetY); - assert.isTrue(callbackWasCalled, "callback was called on mouseout"); - - md.offMouseMove(callback); - target.remove(); - }); - - it("onMouseDown()", () => { - let targetWidth = 400, targetHeight = 400; - let target = TestMethods.generateSVG(targetWidth, targetHeight); - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - target.append("rect").attr("width", targetWidth).attr("height", targetHeight); - - let targetX = 17; - let targetY = 76; - let expectedPoint = { - x: targetX, - y: targetY - }; - - let md = Plottable.Dispatchers.Mouse.getDispatcher( target.node()); - - let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => { - callbackWasCalled = true; - TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); - assert.isNotNull(e, "mouse event was passed to the callback"); - }; - - md.onMouseDown(callback); - - TestMethods.triggerFakeMouseEvent("mousedown", target, targetX, targetY); - assert.isTrue(callbackWasCalled, "callback was called on mousedown"); - - md.offMouseDown(callback); - target.remove(); - }); - - it("onMouseUp()", () => { - let targetWidth = 400, targetHeight = 400; - let target = TestMethods.generateSVG(targetWidth, targetHeight); - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - target.append("rect").attr("width", targetWidth).attr("height", targetHeight); - - let targetX = 17; - let targetY = 76; - let expectedPoint = { - x: targetX, - y: targetY - }; - - let md = Plottable.Dispatchers.Mouse.getDispatcher( target.node()); - - let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => { - callbackWasCalled = true; - TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); - assert.isNotNull(e, "mouse event was passed to the callback"); - }; - - md.onMouseUp(callback); - - TestMethods.triggerFakeMouseEvent("mouseup", target, targetX, targetY); - assert.isTrue(callbackWasCalled, "callback was called on mouseup"); - - md.offMouseUp(callback); - target.remove(); - }); - - it("onWheel()", () => { - // HACKHACK PhantomJS doesn't implement fake creation of WheelEvents - // https://github.com/ariya/phantomjs/issues/11289 - if (window.PHANTOMJS) { - return; - } - let targetWidth = 400, targetHeight = 400; - let svg = TestMethods.generateSVG(targetWidth, targetHeight); - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - svg.append("rect").attr("width", targetWidth).attr("height", targetHeight); - - let targetX = 17; - let targetY = 76; - let expectedPoint = { - x: targetX, - y: targetY - }; - let targetDeltaY = 10; - - let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - - let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: WheelEvent) => { - callbackWasCalled = true; - assert.strictEqual(e.deltaY, targetDeltaY, "deltaY value was passed to callback"); - TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); - assert.isNotNull(e, "mouse event was passed to the callback"); - }; - - md.onWheel(callback); - - TestMethods.triggerFakeWheelEvent("wheel", svg, targetX, targetY, targetDeltaY); - assert.isTrue(callbackWasCalled, "callback was called on wheel"); - - md.offWheel(callback); - svg.remove(); - }); - - it("onDblClick()", () => { - let targetWidth = 400, targetHeight = 400; - let target = TestMethods.generateSVG(targetWidth, targetHeight); - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - target.append("rect").attr("width", targetWidth).attr("height", targetHeight); - - let targetX = 17; - let targetY = 76; - - let md = Plottable.Dispatchers.Mouse.getDispatcher( target.node()); + beforeEach(() => { + svg = TestMethods.generateSVG(SVG_WIDTH, SVG_HEIGHT); + }) - let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => { - callbackWasCalled = true; - assert.isNotNull(e, "mouse event was passed to the callback"); - }; + it("getDispatcher() creates only one Dispatcher.Mouse per ", () => { + let md1 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + assert.isNotNull(md1, "created a new Dispatcher on an SVG"); + let md2 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + assert.strictEqual(md1, md2, "returned the existing Dispatcher if called again with same "); + + svg.remove(); + }); + + it("lastMousePosition() defaults to a non-null value", () => { + let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + let p = md.lastMousePosition(); + assert.isNotNull(p, "returns a value after initialization"); + assert.isNotNull(p.x, "x value is set"); + assert.isNotNull(p.y, "y value is set"); + + svg.remove(); + }); + + it("can remove callbacks by passing null", () => { + // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space + svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); + + let targetX = 17; + let targetY = 76; + + let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + + let cb1Called = false; + let cb1 = (p: Plottable.Point, e: MouseEvent) => cb1Called = true; + let cb2Called = false; + let cb2 = (p: Plottable.Point, e: MouseEvent) => cb2Called = true; + + md.onMouseMove(cb1); + md.onMouseMove(cb2); + TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); + assert.isTrue(cb1Called, "callback 1 was called on mousemove"); + assert.isTrue(cb2Called, "callback 2 was called on mousemove"); + + cb1Called = false; + cb2Called = false; + md.offMouseMove(cb1); + TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); + assert.isFalse(cb1Called, "callback was not called after blanking"); + assert.isTrue(cb2Called, "callback 2 was still called"); + + svg.remove(); + }); + + it("doesn't call callbacks if not in the DOM", () => { + // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space + svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); + + let targetX = 17; + let targetY = 76; + + let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + + let callbackWasCalled = false; + let callback = (p: Plottable.Point, e: MouseEvent) => callbackWasCalled = true; + + md.onMouseMove(callback); + TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); + assert.isTrue(callbackWasCalled, "callback was called on mousemove"); + + svg.remove(); + callbackWasCalled = false; + TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); + assert.isFalse(callbackWasCalled, "callback was not called after was removed from DOM"); + + md.offMouseMove(callback); + }); + + it("doesn't call callbacks for clicks if obscured by overlay", () => { + // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space + svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); + + let targetX = 17; + let targetY = 76; + + let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + + let callbackWasCalled = false; + let callback = (p: Plottable.Point, e: MouseEvent) => callbackWasCalled = true; + + md.onMouseDown(callback); + TestMethods.triggerFakeMouseEvent("mousedown", svg, targetX, targetY); + assert.isTrue(callbackWasCalled, "callback was called on mousedown"); + + let element = svg[0][0]; + let position = { x: 0, y: 0 }; + while (element != null) { + position.x += (element.offsetLeft || element.clientLeft || 0); + position.y += (element.offsetTop || element.clientTop || 0); + element = (element.offsetParent || element.parentNode); + } + + let overlay = TestMethods.getSVGParent().append("div") + .style({ + height: "400px", + width: "400px", + position: "absolute", + top: position.y + "px", + left: position.x + "px" + }); + + callbackWasCalled = false; + TestMethods.triggerFakeMouseEvent("mousedown", overlay, targetX, targetY); + assert.isFalse(callbackWasCalled, "callback was not called on mousedown on overlay"); + + md.offMouseDown(callback); + svg.remove(); + overlay.remove(); + }); + + it("calls callbacks on mouseover, mousemove, and mouseout", () => { + // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space + svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); + + let targetX = 17; + let targetY = 76; + let expectedPoint = { + x: targetX, + y: targetY + }; + + let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + + let callbackWasCalled = false; + let callback = (p: Plottable.Point, e: MouseEvent) => { + callbackWasCalled = true; + TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); + assert.isNotNull(e, "mouse event was passed to the callback"); + }; + + md.onMouseMove(callback); + + TestMethods.triggerFakeMouseEvent("mouseover", svg, targetX, targetY); + assert.isTrue(callbackWasCalled, "callback was called on mouseover"); + callbackWasCalled = false; + TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); + assert.isTrue(callbackWasCalled, "callback was called on mousemove"); + callbackWasCalled = false; + TestMethods.triggerFakeMouseEvent("mouseout", svg, targetX, targetY); + assert.isTrue(callbackWasCalled, "callback was called on mouseout"); + + md.offMouseMove(callback); + svg.remove(); + }); + + it("onMouseDown()", () => { + // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space + svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); + + let targetX = 17; + let targetY = 76; + let expectedPoint = { + x: targetX, + y: targetY + }; + + let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + + let callbackWasCalled = false; + let callback = (p: Plottable.Point, e: MouseEvent) => { + callbackWasCalled = true; + TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); + assert.isNotNull(e, "mouse event was passed to the callback"); + }; + + md.onMouseDown(callback); + + TestMethods.triggerFakeMouseEvent("mousedown", svg, targetX, targetY); + assert.isTrue(callbackWasCalled, "callback was called on mousedown"); + + md.offMouseDown(callback); + svg.remove(); + }); + + it("onMouseUp()", () => { + // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space + svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); + + let targetX = 17; + let targetY = 76; + let expectedPoint = { + x: targetX, + y: targetY + }; + + let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + + let callbackWasCalled = false; + let callback = (p: Plottable.Point, e: MouseEvent) => { + callbackWasCalled = true; + TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); + assert.isNotNull(e, "mouse event was passed to the callback"); + }; + + md.onMouseUp(callback); + + TestMethods.triggerFakeMouseEvent("mouseup", svg, targetX, targetY); + assert.isTrue(callbackWasCalled, "callback was called on mouseup"); + + md.offMouseUp(callback); + svg.remove(); + }); + + it("onWheel()", () => { + // HACKHACK PhantomJS doesn't implement fake creation of WheelEvents + // https://github.com/ariya/phantomjs/issues/11289 + if (window.PHANTOMJS) { + return; + } + + // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space + svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); + + let targetX = 17; + let targetY = 76; + let expectedPoint = { + x: targetX, + y: targetY + }; + let targetDeltaY = 10; + + let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + + let callbackWasCalled = false; + let callback = (p: Plottable.Point, e: WheelEvent) => { + callbackWasCalled = true; + assert.strictEqual(e.deltaY, targetDeltaY, "deltaY value was passed to callback"); + TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); + assert.isNotNull(e, "mouse event was passed to the callback"); + }; + + md.onWheel(callback); + + TestMethods.triggerFakeWheelEvent("wheel", svg, targetX, targetY, targetDeltaY); + assert.isTrue(callbackWasCalled, "callback was called on wheel"); + + md.offWheel(callback); + svg.remove(); + }); + + it("onDblClick()", () => { + // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space + svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); + + let targetX = 17; + let targetY = 76; + + let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - md.onDblClick(callback); + let callbackWasCalled = false; + let callback = (p: Plottable.Point, e: MouseEvent) => { + callbackWasCalled = true; + assert.isNotNull(e, "mouse event was passed to the callback"); + }; - TestMethods.triggerFakeMouseEvent("dblclick", target, targetX, targetY); - assert.isTrue(callbackWasCalled, "callback was called on dblClick"); + md.onDblClick(callback); - md.offDblClick(callback); - target.remove(); + TestMethods.triggerFakeMouseEvent("dblclick", svg, targetX, targetY); + assert.isTrue(callbackWasCalled, "callback was called on dblClick"); + + md.offDblClick(callback); + svg.remove(); + }); }); }); }); From ab065f21f54c4100b808774e37e5fbdc03861e88 Mon Sep 17 00:00:00 2001 From: Andrei Cioara Date: Fri, 11 Sep 2015 17:05:59 -0700 Subject: [PATCH 02/12] [refactorMouseDispatcher] finished callbacks --- test/dispatchers/mouseDispatcherTests.ts | 291 ++++++++++++----------- 1 file changed, 150 insertions(+), 141 deletions(-) diff --git a/test/dispatchers/mouseDispatcherTests.ts b/test/dispatchers/mouseDispatcherTests.ts index 1049f59ebc..0baa5fcb2e 100644 --- a/test/dispatchers/mouseDispatcherTests.ts +++ b/test/dispatchers/mouseDispatcherTests.ts @@ -3,6 +3,133 @@ describe("Dispatchers", () => { describe("Mouse Dispatcher", () => { + describe("Callbacks", () => { + let SVG_WIDTH = 400; + let SVG_HEIGHT = 400; + + let targetX = 17; + let targetY = 76; + let expectedPoint = { + x: targetX, + y: targetY + }; + + let svg: d3.Selection; + let mouseDispatcher: Plottable.Dispatchers.Mouse; + + beforeEach(() => { + svg = TestMethods.generateSVG(SVG_WIDTH, SVG_HEIGHT); + // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space + svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); + + mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + }); + + it("onMouseDown()", () => { + let callbackWasCalled = false; + let callback = (p: Plottable.Point, e: MouseEvent) => { + callbackWasCalled = true; + TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); + assert.isNotNull(e, "mouse event was passed to the callback"); + }; + + assert.strictEqual(mouseDispatcher.onMouseDown(callback), mouseDispatcher, + "setting the mouseDown callback returns the dispatcher"); + + TestMethods.triggerFakeMouseEvent("mousedown", svg, targetX, targetY); + assert.isTrue(callbackWasCalled, "callback was called on mouseDown"); + + assert.strictEqual(mouseDispatcher.offMouseDown(callback), mouseDispatcher, + "unsetting the mouseDown callback returns the dispatcher"); + + callbackWasCalled = false; + TestMethods.triggerFakeMouseEvent("mousedown", svg, targetX, targetY); + assert.isFalse(callbackWasCalled, "callback was disconnected from the dispatcher"); + + svg.remove(); + }); + + it("onMouseUp()", () => { + let callbackWasCalled = false; + let callback = (p: Plottable.Point, e: MouseEvent) => { + callbackWasCalled = true; + TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); + assert.isNotNull(e, "mouse event was passed to the callback"); + }; + + assert.strictEqual(mouseDispatcher.onMouseUp(callback), mouseDispatcher, + "setting the mouseUp callback returns the dispatcher"); + + TestMethods.triggerFakeMouseEvent("mouseup", svg, targetX, targetY); + assert.isTrue(callbackWasCalled, "callback was called on mouseUp"); + + assert.strictEqual(mouseDispatcher.offMouseUp(callback), mouseDispatcher, + "unsetting the mouseUp callback returns the dispatcher"); + + callbackWasCalled = false; + TestMethods.triggerFakeMouseEvent("mouseup", svg, targetX, targetY); + assert.isFalse(callbackWasCalled, "callback was disconnected from the dispatcher"); + + svg.remove(); + }); + + it("onWheel()", () => { + // HACKHACK PhantomJS doesn't implement fake creation of WheelEvents + // https://github.com/ariya/phantomjs/issues/11289 + if (window.PHANTOMJS) { + return; + } + + let targetDeltaY = 10; + + let callbackWasCalled = false; + let callback = (p: Plottable.Point, e: WheelEvent) => { + callbackWasCalled = true; + assert.strictEqual(e.deltaY, targetDeltaY, "deltaY value was passed to callback"); + TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); + assert.isNotNull(e, "mouse event was passed to the callback"); + }; + + assert.strictEqual(mouseDispatcher.onWheel(callback), mouseDispatcher, + "setting the wheel callback returns the dispatcher"); + + TestMethods.triggerFakeWheelEvent("wheel", svg, targetX, targetY, targetDeltaY); + assert.isTrue(callbackWasCalled, "callback was called on wheel"); + + assert.strictEqual(mouseDispatcher.offWheel(callback), mouseDispatcher, + "unsetting the wheel callback returns the dispatcher"); + + callbackWasCalled = false; + TestMethods.triggerFakeWheelEvent("wheel", svg, targetX, targetY, targetDeltaY); + assert.isFalse(callbackWasCalled, "callback was disconnected from the dispatcher"); + + svg.remove(); + }); + + it("onDblClick()", () => { + let callbackWasCalled = false; + let callback = (p: Plottable.Point, e: MouseEvent) => { + callbackWasCalled = true; + assert.isNotNull(e, "mouse event was passed to the callback"); + }; + + assert.strictEqual(mouseDispatcher.onDblClick(callback), mouseDispatcher, + "setting the dblClick callback returns the dispatcher"); + + TestMethods.triggerFakeMouseEvent("dblclick", svg, targetX, targetY); + assert.isTrue(callbackWasCalled, "callback was called on dblClick"); + + assert.strictEqual(mouseDispatcher.offDblClick(callback), mouseDispatcher, + "unsetting the dblClick callback returns the dispatcher"); + + callbackWasCalled = false; + TestMethods.triggerFakeMouseEvent("dblclick", svg, targetX, targetY); + assert.isFalse(callbackWasCalled, "callback was disconnected from the dispatcher"); + + svg.remove(); + }); + }); + describe("Basic usage", () => { let SVG_WIDTH = 400; let SVG_HEIGHT = 400; @@ -11,23 +138,23 @@ describe("Dispatchers", () => { beforeEach(() => { svg = TestMethods.generateSVG(SVG_WIDTH, SVG_HEIGHT); - }) + }); it("getDispatcher() creates only one Dispatcher.Mouse per ", () => { - let md1 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - assert.isNotNull(md1, "created a new Dispatcher on an SVG"); - let md2 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - assert.strictEqual(md1, md2, "returned the existing Dispatcher if called again with same "); + let dispatcher1 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + assert.isNotNull(dispatcher1, "created a new Dispatcher on an SVG"); + let dispatcher2 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + assert.strictEqual(dispatcher1, dispatcher2, "returned the existing Dispatcher if called again with same "); svg.remove(); }); it("lastMousePosition() defaults to a non-null value", () => { - let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - let p = md.lastMousePosition(); - assert.isNotNull(p, "returns a value after initialization"); - assert.isNotNull(p.x, "x value is set"); - assert.isNotNull(p.y, "y value is set"); + let mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + let point = mouseDispatcher.lastMousePosition(); + assert.isNotNull(point, "returns a value after initialization"); + assert.isNotNull(point.x, "x value is set"); + assert.isNotNull(point.y, "y value is set"); svg.remove(); }); @@ -39,22 +166,22 @@ describe("Dispatchers", () => { let targetX = 17; let targetY = 76; - let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + let mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); let cb1Called = false; let cb1 = (p: Plottable.Point, e: MouseEvent) => cb1Called = true; let cb2Called = false; let cb2 = (p: Plottable.Point, e: MouseEvent) => cb2Called = true; - md.onMouseMove(cb1); - md.onMouseMove(cb2); + mouseDispatcher.onMouseMove(cb1); + mouseDispatcher.onMouseMove(cb2); TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); assert.isTrue(cb1Called, "callback 1 was called on mousemove"); assert.isTrue(cb2Called, "callback 2 was called on mousemove"); cb1Called = false; cb2Called = false; - md.offMouseMove(cb1); + mouseDispatcher.offMouseMove(cb1); TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); assert.isFalse(cb1Called, "callback was not called after blanking"); assert.isTrue(cb2Called, "callback 2 was still called"); @@ -69,12 +196,12 @@ describe("Dispatchers", () => { let targetX = 17; let targetY = 76; - let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + let mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); let callbackWasCalled = false; let callback = (p: Plottable.Point, e: MouseEvent) => callbackWasCalled = true; - md.onMouseMove(callback); + mouseDispatcher.onMouseMove(callback); TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); assert.isTrue(callbackWasCalled, "callback was called on mousemove"); @@ -83,7 +210,7 @@ describe("Dispatchers", () => { TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); assert.isFalse(callbackWasCalled, "callback was not called after was removed from DOM"); - md.offMouseMove(callback); + mouseDispatcher.offMouseMove(callback); }); it("doesn't call callbacks for clicks if obscured by overlay", () => { @@ -93,12 +220,12 @@ describe("Dispatchers", () => { let targetX = 17; let targetY = 76; - let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + let mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); let callbackWasCalled = false; let callback = (p: Plottable.Point, e: MouseEvent) => callbackWasCalled = true; - md.onMouseDown(callback); + mouseDispatcher.onMouseDown(callback); TestMethods.triggerFakeMouseEvent("mousedown", svg, targetX, targetY); assert.isTrue(callbackWasCalled, "callback was called on mousedown"); @@ -123,7 +250,7 @@ describe("Dispatchers", () => { TestMethods.triggerFakeMouseEvent("mousedown", overlay, targetX, targetY); assert.isFalse(callbackWasCalled, "callback was not called on mousedown on overlay"); - md.offMouseDown(callback); + mouseDispatcher.offMouseDown(callback); svg.remove(); overlay.remove(); }); @@ -139,7 +266,7 @@ describe("Dispatchers", () => { y: targetY }; - let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + let mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); let callbackWasCalled = false; let callback = (p: Plottable.Point, e: MouseEvent) => { @@ -148,7 +275,7 @@ describe("Dispatchers", () => { assert.isNotNull(e, "mouse event was passed to the callback"); }; - md.onMouseMove(callback); + mouseDispatcher.onMouseMove(callback); TestMethods.triggerFakeMouseEvent("mouseover", svg, targetX, targetY); assert.isTrue(callbackWasCalled, "callback was called on mouseover"); @@ -159,128 +286,10 @@ describe("Dispatchers", () => { TestMethods.triggerFakeMouseEvent("mouseout", svg, targetX, targetY); assert.isTrue(callbackWasCalled, "callback was called on mouseout"); - md.offMouseMove(callback); - svg.remove(); - }); - - it("onMouseDown()", () => { - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); - - let targetX = 17; - let targetY = 76; - let expectedPoint = { - x: targetX, - y: targetY - }; - - let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - - let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => { - callbackWasCalled = true; - TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); - assert.isNotNull(e, "mouse event was passed to the callback"); - }; - - md.onMouseDown(callback); - - TestMethods.triggerFakeMouseEvent("mousedown", svg, targetX, targetY); - assert.isTrue(callbackWasCalled, "callback was called on mousedown"); - - md.offMouseDown(callback); + mouseDispatcher.offMouseMove(callback); svg.remove(); }); - it("onMouseUp()", () => { - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); - - let targetX = 17; - let targetY = 76; - let expectedPoint = { - x: targetX, - y: targetY - }; - - let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - - let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => { - callbackWasCalled = true; - TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); - assert.isNotNull(e, "mouse event was passed to the callback"); - }; - - md.onMouseUp(callback); - - TestMethods.triggerFakeMouseEvent("mouseup", svg, targetX, targetY); - assert.isTrue(callbackWasCalled, "callback was called on mouseup"); - - md.offMouseUp(callback); - svg.remove(); - }); - - it("onWheel()", () => { - // HACKHACK PhantomJS doesn't implement fake creation of WheelEvents - // https://github.com/ariya/phantomjs/issues/11289 - if (window.PHANTOMJS) { - return; - } - - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); - - let targetX = 17; - let targetY = 76; - let expectedPoint = { - x: targetX, - y: targetY - }; - let targetDeltaY = 10; - - let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - - let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: WheelEvent) => { - callbackWasCalled = true; - assert.strictEqual(e.deltaY, targetDeltaY, "deltaY value was passed to callback"); - TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); - assert.isNotNull(e, "mouse event was passed to the callback"); - }; - - md.onWheel(callback); - - TestMethods.triggerFakeWheelEvent("wheel", svg, targetX, targetY, targetDeltaY); - assert.isTrue(callbackWasCalled, "callback was called on wheel"); - - md.offWheel(callback); - svg.remove(); - }); - - it("onDblClick()", () => { - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); - - let targetX = 17; - let targetY = 76; - - let md = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - - let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => { - callbackWasCalled = true; - assert.isNotNull(e, "mouse event was passed to the callback"); - }; - - md.onDblClick(callback); - - TestMethods.triggerFakeMouseEvent("dblclick", svg, targetX, targetY); - assert.isTrue(callbackWasCalled, "callback was called on dblClick"); - - md.offDblClick(callback); - svg.remove(); - }); }); }); }); From 069121d193c03367d7f2fca9f758a4cf61bb2ca4 Mon Sep 17 00:00:00 2001 From: Andrei Cioara Date: Fri, 11 Sep 2015 17:06:51 -0700 Subject: [PATCH 03/12] [refactorMouseDispatcher] hiding svg sizes --- test/dispatchers/mouseDispatcherTests.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/dispatchers/mouseDispatcherTests.ts b/test/dispatchers/mouseDispatcherTests.ts index 0baa5fcb2e..d4af34a6f2 100644 --- a/test/dispatchers/mouseDispatcherTests.ts +++ b/test/dispatchers/mouseDispatcherTests.ts @@ -4,9 +4,6 @@ describe("Dispatchers", () => { describe("Mouse Dispatcher", () => { describe("Callbacks", () => { - let SVG_WIDTH = 400; - let SVG_HEIGHT = 400; - let targetX = 17; let targetY = 76; let expectedPoint = { @@ -18,6 +15,8 @@ describe("Dispatchers", () => { let mouseDispatcher: Plottable.Dispatchers.Mouse; beforeEach(() => { + let SVG_WIDTH = 400; + let SVG_HEIGHT = 400; svg = TestMethods.generateSVG(SVG_WIDTH, SVG_HEIGHT); // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); From 3018373a3f0399c25d92ba4fbd24bf143f66bb04 Mon Sep 17 00:00:00 2001 From: Andrei Cioara Date: Fri, 11 Sep 2015 17:26:02 -0700 Subject: [PATCH 04/12] [refactorMouseDispatcher] moved all callback code to the callback describe --- test/dispatchers/mouseDispatcherTests.ts | 97 +++++++----------------- 1 file changed, 29 insertions(+), 68 deletions(-) diff --git a/test/dispatchers/mouseDispatcherTests.ts b/test/dispatchers/mouseDispatcherTests.ts index d4af34a6f2..5eed2259a4 100644 --- a/test/dispatchers/mouseDispatcherTests.ts +++ b/test/dispatchers/mouseDispatcherTests.ts @@ -3,6 +3,33 @@ describe("Dispatchers", () => { describe("Mouse Dispatcher", () => { + describe("Basic usage", () => { + let svg: d3.Selection; + + beforeEach(() => { + svg = TestMethods.generateSVG(); + }); + + it("getDispatcher() creates only one Dispatcher.Mouse per ", () => { + let dispatcher1 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + assert.isNotNull(dispatcher1, "created a new Dispatcher on an SVG"); + let dispatcher2 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + assert.strictEqual(dispatcher1, dispatcher2, "returned the existing Dispatcher if called again with same "); + + svg.remove(); + }); + + it("lastMousePosition() defaults to a non-null value", () => { + let mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + let point = mouseDispatcher.lastMousePosition(); + assert.isNotNull(point, "returns a value after initialization"); + assert.isNotNull(point.x, "x value is set"); + assert.isNotNull(point.y, "y value is set"); + + svg.remove(); + }); + }); + describe("Callbacks", () => { let targetX = 17; let targetY = 76; @@ -127,46 +154,8 @@ describe("Dispatchers", () => { svg.remove(); }); - }); - - describe("Basic usage", () => { - let SVG_WIDTH = 400; - let SVG_HEIGHT = 400; - - let svg: d3.Selection; - - beforeEach(() => { - svg = TestMethods.generateSVG(SVG_WIDTH, SVG_HEIGHT); - }); - - it("getDispatcher() creates only one Dispatcher.Mouse per ", () => { - let dispatcher1 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - assert.isNotNull(dispatcher1, "created a new Dispatcher on an SVG"); - let dispatcher2 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - assert.strictEqual(dispatcher1, dispatcher2, "returned the existing Dispatcher if called again with same "); - - svg.remove(); - }); - - it("lastMousePosition() defaults to a non-null value", () => { - let mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - let point = mouseDispatcher.lastMousePosition(); - assert.isNotNull(point, "returns a value after initialization"); - assert.isNotNull(point.x, "x value is set"); - assert.isNotNull(point.y, "y value is set"); - - svg.remove(); - }); - - it("can remove callbacks by passing null", () => { - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); - - let targetX = 17; - let targetY = 76; - - let mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + it("can register two callbacks for the samme mouse dispatcher", () => { let cb1Called = false; let cb1 = (p: Plottable.Point, e: MouseEvent) => cb1Called = true; let cb2Called = false; @@ -189,14 +178,6 @@ describe("Dispatchers", () => { }); it("doesn't call callbacks if not in the DOM", () => { - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); - - let targetX = 17; - let targetY = 76; - - let mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - let callbackWasCalled = false; let callback = (p: Plottable.Point, e: MouseEvent) => callbackWasCalled = true; @@ -213,14 +194,6 @@ describe("Dispatchers", () => { }); it("doesn't call callbacks for clicks if obscured by overlay", () => { - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); - - let targetX = 17; - let targetY = 76; - - let mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - let callbackWasCalled = false; let callback = (p: Plottable.Point, e: MouseEvent) => callbackWasCalled = true; @@ -255,18 +228,6 @@ describe("Dispatchers", () => { }); it("calls callbacks on mouseover, mousemove, and mouseout", () => { - // HACKHACK: PhantomJS can't measure SVGs unless they have something in them occupying space - svg.append("rect").attr("width", SVG_WIDTH).attr("height", SVG_HEIGHT); - - let targetX = 17; - let targetY = 76; - let expectedPoint = { - x: targetX, - y: targetY - }; - - let mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); - let callbackWasCalled = false; let callback = (p: Plottable.Point, e: MouseEvent) => { callbackWasCalled = true; @@ -288,7 +249,7 @@ describe("Dispatchers", () => { mouseDispatcher.offMouseMove(callback); svg.remove(); }); - }); + }); }); From c6f46127f85e31e3d5e8aa2fa2d4a13fdecc9ce9 Mon Sep 17 00:00:00 2001 From: Andrei Cioara Date: Fri, 11 Sep 2015 17:30:59 -0700 Subject: [PATCH 05/12] [refactorMouseDispatcher] Finished code refactor --- test/dispatchers/mouseDispatcherTests.ts | 54 ++++++++++++------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/test/dispatchers/mouseDispatcherTests.ts b/test/dispatchers/mouseDispatcherTests.ts index 5eed2259a4..241c9807b2 100644 --- a/test/dispatchers/mouseDispatcherTests.ts +++ b/test/dispatchers/mouseDispatcherTests.ts @@ -53,10 +53,10 @@ describe("Dispatchers", () => { it("onMouseDown()", () => { let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => { + let callback = (point: Plottable.Point, event: MouseEvent) => { callbackWasCalled = true; - TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); - assert.isNotNull(e, "mouse event was passed to the callback"); + TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); + assert.isNotNull(event, "mouse event was passed to the callback"); }; assert.strictEqual(mouseDispatcher.onMouseDown(callback), mouseDispatcher, @@ -77,10 +77,10 @@ describe("Dispatchers", () => { it("onMouseUp()", () => { let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => { + let callback = (point: Plottable.Point, event: MouseEvent) => { callbackWasCalled = true; - TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); - assert.isNotNull(e, "mouse event was passed to the callback"); + TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); + assert.isNotNull(event, "mouse event was passed to the callback"); }; assert.strictEqual(mouseDispatcher.onMouseUp(callback), mouseDispatcher, @@ -109,11 +109,11 @@ describe("Dispatchers", () => { let targetDeltaY = 10; let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: WheelEvent) => { + let callback = (point: Plottable.Point, event: WheelEvent) => { callbackWasCalled = true; - assert.strictEqual(e.deltaY, targetDeltaY, "deltaY value was passed to callback"); - TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); - assert.isNotNull(e, "mouse event was passed to the callback"); + assert.strictEqual(event.deltaY, targetDeltaY, "deltaY value was passed to callback"); + TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); + assert.isNotNull(event, "mouse event was passed to the callback"); }; assert.strictEqual(mouseDispatcher.onWheel(callback), mouseDispatcher, @@ -134,9 +134,10 @@ describe("Dispatchers", () => { it("onDblClick()", () => { let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => { + let callback = (point: Plottable.Point, event: MouseEvent) => { callbackWasCalled = true; - assert.isNotNull(e, "mouse event was passed to the callback"); + TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); + assert.isNotNull(event, "mouse event was passed to the callback"); }; assert.strictEqual(mouseDispatcher.onDblClick(callback), mouseDispatcher, @@ -157,9 +158,9 @@ describe("Dispatchers", () => { it("can register two callbacks for the samme mouse dispatcher", () => { let cb1Called = false; - let cb1 = (p: Plottable.Point, e: MouseEvent) => cb1Called = true; + let cb1 = () => cb1Called = true; let cb2Called = false; - let cb2 = (p: Plottable.Point, e: MouseEvent) => cb2Called = true; + let cb2 = () => cb2Called = true; mouseDispatcher.onMouseMove(cb1); mouseDispatcher.onMouseMove(cb2); @@ -179,7 +180,7 @@ describe("Dispatchers", () => { it("doesn't call callbacks if not in the DOM", () => { let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => callbackWasCalled = true; + let callback = () => callbackWasCalled = true; mouseDispatcher.onMouseMove(callback); TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); @@ -195,7 +196,7 @@ describe("Dispatchers", () => { it("doesn't call callbacks for clicks if obscured by overlay", () => { let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => callbackWasCalled = true; + let callback = () => callbackWasCalled = true; mouseDispatcher.onMouseDown(callback); TestMethods.triggerFakeMouseEvent("mousedown", svg, targetX, targetY); @@ -209,14 +210,13 @@ describe("Dispatchers", () => { element = (element.offsetParent || element.parentNode); } - let overlay = TestMethods.getSVGParent().append("div") - .style({ - height: "400px", - width: "400px", - position: "absolute", - top: position.y + "px", - left: position.x + "px" - }); + let overlay = TestMethods.getSVGParent().append("div").style({ + height: "400px", + width: "400px", + position: "absolute", + top: position.y + "px", + left: position.x + "px" + }); callbackWasCalled = false; TestMethods.triggerFakeMouseEvent("mousedown", overlay, targetX, targetY); @@ -229,10 +229,10 @@ describe("Dispatchers", () => { it("calls callbacks on mouseover, mousemove, and mouseout", () => { let callbackWasCalled = false; - let callback = (p: Plottable.Point, e: MouseEvent) => { + let callback = (point: Plottable.Point, event: MouseEvent) => { callbackWasCalled = true; - TestMethods.assertPointsClose(p, expectedPoint, 0.5, "mouse position is correct"); - assert.isNotNull(e, "mouse event was passed to the callback"); + TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); + assert.isNotNull(event, "mouse event was passed to the callback"); }; mouseDispatcher.onMouseMove(callback); From f36fd08e9052af4188ad1994d4bc797b90f224ab Mon Sep 17 00:00:00 2001 From: Andrei Cioara Date: Fri, 11 Sep 2015 17:33:13 -0700 Subject: [PATCH 06/12] [refactorMouseDispatcher] test namesemantics --- test/dispatchers/mouseDispatcherTests.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/dispatchers/mouseDispatcherTests.ts b/test/dispatchers/mouseDispatcherTests.ts index 241c9807b2..c5d01bd289 100644 --- a/test/dispatchers/mouseDispatcherTests.ts +++ b/test/dispatchers/mouseDispatcherTests.ts @@ -10,7 +10,7 @@ describe("Dispatchers", () => { svg = TestMethods.generateSVG(); }); - it("getDispatcher() creates only one Dispatcher.Mouse per ", () => { + it("creates only one Dispatcher.Mouse per using getDispatcher() ", () => { let dispatcher1 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); assert.isNotNull(dispatcher1, "created a new Dispatcher on an SVG"); let dispatcher2 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); @@ -19,7 +19,7 @@ describe("Dispatchers", () => { svg.remove(); }); - it("lastMousePosition() defaults to a non-null value", () => { + it("returns non-null value for default lastMousePosition()", () => { let mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); let point = mouseDispatcher.lastMousePosition(); assert.isNotNull(point, "returns a value after initialization"); @@ -51,7 +51,7 @@ describe("Dispatchers", () => { mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); }); - it("onMouseDown()", () => { + it("calls the mouseDown callback", () => { let callbackWasCalled = false; let callback = (point: Plottable.Point, event: MouseEvent) => { callbackWasCalled = true; @@ -75,7 +75,7 @@ describe("Dispatchers", () => { svg.remove(); }); - it("onMouseUp()", () => { + it("calls the mouseUp callback", () => { let callbackWasCalled = false; let callback = (point: Plottable.Point, event: MouseEvent) => { callbackWasCalled = true; @@ -99,7 +99,7 @@ describe("Dispatchers", () => { svg.remove(); }); - it("onWheel()", () => { + it("calls the wheel callback", () => { // HACKHACK PhantomJS doesn't implement fake creation of WheelEvents // https://github.com/ariya/phantomjs/issues/11289 if (window.PHANTOMJS) { @@ -132,7 +132,7 @@ describe("Dispatchers", () => { svg.remove(); }); - it("onDblClick()", () => { + it("calls the dblClick callback", () => { let callbackWasCalled = false; let callback = (point: Plottable.Point, event: MouseEvent) => { callbackWasCalled = true; From 0e4718a9aa376810f91beb875f012c9749d8de5d Mon Sep 17 00:00:00 2001 From: Andrei Cioara Date: Fri, 11 Sep 2015 17:59:34 -0700 Subject: [PATCH 07/12] [refactorMouseDispatcher] fixed failing test --- test/dispatchers/mouseDispatcherTests.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/dispatchers/mouseDispatcherTests.ts b/test/dispatchers/mouseDispatcherTests.ts index c5d01bd289..4929592f64 100644 --- a/test/dispatchers/mouseDispatcherTests.ts +++ b/test/dispatchers/mouseDispatcherTests.ts @@ -103,6 +103,7 @@ describe("Dispatchers", () => { // HACKHACK PhantomJS doesn't implement fake creation of WheelEvents // https://github.com/ariya/phantomjs/issues/11289 if (window.PHANTOMJS) { + svg.remove(); return; } From 9ff4fd3980588f5d5382b270700a196b30f065b5 Mon Sep 17 00:00:00 2001 From: Andrei Cioara Date: Mon, 21 Sep 2015 15:20:59 -0700 Subject: [PATCH 08/12] [refactorMouseDispatcher] CR fixes --- test/dispatchers/mouseDispatcherTests.ts | 37 +++++++++++++++++------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/test/dispatchers/mouseDispatcherTests.ts b/test/dispatchers/mouseDispatcherTests.ts index 4929592f64..4d582a299e 100644 --- a/test/dispatchers/mouseDispatcherTests.ts +++ b/test/dispatchers/mouseDispatcherTests.ts @@ -5,22 +5,24 @@ describe("Dispatchers", () => { describe("Basic usage", () => { let svg: d3.Selection; + let svgNode: SVGElement; beforeEach(() => { svg = TestMethods.generateSVG(); + svgNode = svg.node(); }); it("creates only one Dispatcher.Mouse per using getDispatcher() ", () => { - let dispatcher1 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + let dispatcher1 = Plottable.Dispatchers.Mouse.getDispatcher(svgNode); assert.isNotNull(dispatcher1, "created a new Dispatcher on an SVG"); - let dispatcher2 = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + let dispatcher2 = Plottable.Dispatchers.Mouse.getDispatcher(svgNode); assert.strictEqual(dispatcher1, dispatcher2, "returned the existing Dispatcher if called again with same "); svg.remove(); }); it("returns non-null value for default lastMousePosition()", () => { - let mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher( svg.node()); + let mouseDispatcher = Plottable.Dispatchers.Mouse.getDispatcher(svgNode); let point = mouseDispatcher.lastMousePosition(); assert.isNotNull(point, "returns a value after initialization"); assert.isNotNull(point.x, "x value is set"); @@ -57,6 +59,7 @@ describe("Dispatchers", () => { callbackWasCalled = true; TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); assert.isNotNull(event, "mouse event was passed to the callback"); + assert.isTrue(event instanceof MouseEvent, "the event passed is an instance of MouseEvent"); }; assert.strictEqual(mouseDispatcher.onMouseDown(callback), mouseDispatcher, @@ -81,6 +84,7 @@ describe("Dispatchers", () => { callbackWasCalled = true; TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); assert.isNotNull(event, "mouse event was passed to the callback"); + assert.isTrue(event instanceof MouseEvent, "the event passed is an instance of MouseEvent"); }; assert.strictEqual(mouseDispatcher.onMouseUp(callback), mouseDispatcher, @@ -115,6 +119,7 @@ describe("Dispatchers", () => { assert.strictEqual(event.deltaY, targetDeltaY, "deltaY value was passed to callback"); TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); assert.isNotNull(event, "mouse event was passed to the callback"); + assert.isTrue(event instanceof MouseEvent, "the event passed is an instance of MouseEvent"); }; assert.strictEqual(mouseDispatcher.onWheel(callback), mouseDispatcher, @@ -139,6 +144,7 @@ describe("Dispatchers", () => { callbackWasCalled = true; TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); assert.isNotNull(event, "mouse event was passed to the callback"); + assert.isTrue(event instanceof MouseEvent, "the event passed is an instance of MouseEvent"); }; assert.strictEqual(mouseDispatcher.onDblClick(callback), mouseDispatcher, @@ -176,6 +182,7 @@ describe("Dispatchers", () => { assert.isFalse(cb1Called, "callback was not called after blanking"); assert.isTrue(cb2Called, "callback 2 was still called"); + mouseDispatcher.offMouseMove(cb2); svg.remove(); }); @@ -204,19 +211,20 @@ describe("Dispatchers", () => { assert.isTrue(callbackWasCalled, "callback was called on mousedown"); let element = svg[0][0]; - let position = { x: 0, y: 0 }; + // Getting the absolute coordinates of the SVG in order to place the overlay at the right location + let topLeftCorner = { x: 0, y: 0 }; while (element != null) { - position.x += (element.offsetLeft || element.clientLeft || 0); - position.y += (element.offsetTop || element.clientTop || 0); + topLeftCorner.x += (element.offsetLeft || element.clientLeft || 0); + topLeftCorner.y += (element.offsetTop || element.clientTop || 0); element = (element.offsetParent || element.parentNode); } let overlay = TestMethods.getSVGParent().append("div").style({ height: "400px", width: "400px", - position: "absolute", - top: position.y + "px", - left: position.x + "px" + topLeftCorner: "absolute", + top: topLeftCorner.y + "px", + left: topLeftCorner.x + "px" }); callbackWasCalled = false; @@ -228,7 +236,7 @@ describe("Dispatchers", () => { overlay.remove(); }); - it("calls callbacks on mouseover, mousemove, and mouseout", () => { + it("calls mouseMove callback on mouseover, mousemove, and mouseout", () => { let callbackWasCalled = false; let callback = (point: Plottable.Point, event: MouseEvent) => { callbackWasCalled = true; @@ -248,6 +256,15 @@ describe("Dispatchers", () => { assert.isTrue(callbackWasCalled, "callback was called on mouseout"); mouseDispatcher.offMouseMove(callback); + + callbackWasCalled = false; + TestMethods.triggerFakeMouseEvent("mouseover", svg, targetX, targetY); + assert.isFalse(callbackWasCalled, "disconnected dispatcher callback not called on mouseover"); + TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); + assert.isFalse(callbackWasCalled, "disconnected dispatcher callback not called on mousemove"); + TestMethods.triggerFakeMouseEvent("mouseout", svg, targetX, targetY); + assert.isFalse(callbackWasCalled, "disconnected dispatcher callback not called on mouseout"); + svg.remove(); }); }); From 3fcfb023ee3b17a354ee7a6cdac174c6f82cc0ed Mon Sep 17 00:00:00 2001 From: Andrei Cioara Date: Mon, 21 Sep 2015 15:21:59 -0700 Subject: [PATCH 09/12] [refactorMouseDispatcher] forgotten event checking --- test/dispatchers/mouseDispatcherTests.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/dispatchers/mouseDispatcherTests.ts b/test/dispatchers/mouseDispatcherTests.ts index 4d582a299e..5feba8cb09 100644 --- a/test/dispatchers/mouseDispatcherTests.ts +++ b/test/dispatchers/mouseDispatcherTests.ts @@ -242,6 +242,7 @@ describe("Dispatchers", () => { callbackWasCalled = true; TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); assert.isNotNull(event, "mouse event was passed to the callback"); + assert.isTrue(event instanceof MouseEvent, "the event passed is an instance of MouseEvent"); }; mouseDispatcher.onMouseMove(callback); From 4e849683530eaa11d052affa7460479f435df8af Mon Sep 17 00:00:00 2001 From: Andrei Cioara Date: Mon, 21 Sep 2015 15:22:57 -0700 Subject: [PATCH 10/12] [refactorMouseDispatcher] moved mouseMove test higher up --- test/dispatchers/mouseDispatcherTests.ts | 66 ++++++++++++------------ 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/test/dispatchers/mouseDispatcherTests.ts b/test/dispatchers/mouseDispatcherTests.ts index 5feba8cb09..117c3394e4 100644 --- a/test/dispatchers/mouseDispatcherTests.ts +++ b/test/dispatchers/mouseDispatcherTests.ts @@ -163,6 +163,39 @@ describe("Dispatchers", () => { svg.remove(); }); + it("calls mouseMove callback on mouseover, mousemove, and mouseout", () => { + let callbackWasCalled = false; + let callback = (point: Plottable.Point, event: MouseEvent) => { + callbackWasCalled = true; + TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); + assert.isNotNull(event, "mouse event was passed to the callback"); + assert.isTrue(event instanceof MouseEvent, "the event passed is an instance of MouseEvent"); + }; + + mouseDispatcher.onMouseMove(callback); + + TestMethods.triggerFakeMouseEvent("mouseover", svg, targetX, targetY); + assert.isTrue(callbackWasCalled, "callback was called on mouseover"); + callbackWasCalled = false; + TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); + assert.isTrue(callbackWasCalled, "callback was called on mousemove"); + callbackWasCalled = false; + TestMethods.triggerFakeMouseEvent("mouseout", svg, targetX, targetY); + assert.isTrue(callbackWasCalled, "callback was called on mouseout"); + + mouseDispatcher.offMouseMove(callback); + + callbackWasCalled = false; + TestMethods.triggerFakeMouseEvent("mouseover", svg, targetX, targetY); + assert.isFalse(callbackWasCalled, "disconnected dispatcher callback not called on mouseover"); + TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); + assert.isFalse(callbackWasCalled, "disconnected dispatcher callback not called on mousemove"); + TestMethods.triggerFakeMouseEvent("mouseout", svg, targetX, targetY); + assert.isFalse(callbackWasCalled, "disconnected dispatcher callback not called on mouseout"); + + svg.remove(); + }); + it("can register two callbacks for the samme mouse dispatcher", () => { let cb1Called = false; let cb1 = () => cb1Called = true; @@ -235,39 +268,6 @@ describe("Dispatchers", () => { svg.remove(); overlay.remove(); }); - - it("calls mouseMove callback on mouseover, mousemove, and mouseout", () => { - let callbackWasCalled = false; - let callback = (point: Plottable.Point, event: MouseEvent) => { - callbackWasCalled = true; - TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); - assert.isNotNull(event, "mouse event was passed to the callback"); - assert.isTrue(event instanceof MouseEvent, "the event passed is an instance of MouseEvent"); - }; - - mouseDispatcher.onMouseMove(callback); - - TestMethods.triggerFakeMouseEvent("mouseover", svg, targetX, targetY); - assert.isTrue(callbackWasCalled, "callback was called on mouseover"); - callbackWasCalled = false; - TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); - assert.isTrue(callbackWasCalled, "callback was called on mousemove"); - callbackWasCalled = false; - TestMethods.triggerFakeMouseEvent("mouseout", svg, targetX, targetY); - assert.isTrue(callbackWasCalled, "callback was called on mouseout"); - - mouseDispatcher.offMouseMove(callback); - - callbackWasCalled = false; - TestMethods.triggerFakeMouseEvent("mouseover", svg, targetX, targetY); - assert.isFalse(callbackWasCalled, "disconnected dispatcher callback not called on mouseover"); - TestMethods.triggerFakeMouseEvent("mousemove", svg, targetX, targetY); - assert.isFalse(callbackWasCalled, "disconnected dispatcher callback not called on mousemove"); - TestMethods.triggerFakeMouseEvent("mouseout", svg, targetX, targetY); - assert.isFalse(callbackWasCalled, "disconnected dispatcher callback not called on mouseout"); - - svg.remove(); - }); }); }); From 842f18ef29649ddc053fae4e752e41680b693220 Mon Sep 17 00:00:00 2001 From: Andrei Cioara Date: Mon, 21 Sep 2015 17:34:48 -0700 Subject: [PATCH 11/12] [refactorMouseDispatcher] using instanceOf assert --- test/dispatchers/mouseDispatcherTests.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/dispatchers/mouseDispatcherTests.ts b/test/dispatchers/mouseDispatcherTests.ts index 117c3394e4..804128a6eb 100644 --- a/test/dispatchers/mouseDispatcherTests.ts +++ b/test/dispatchers/mouseDispatcherTests.ts @@ -59,7 +59,7 @@ describe("Dispatchers", () => { callbackWasCalled = true; TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); assert.isNotNull(event, "mouse event was passed to the callback"); - assert.isTrue(event instanceof MouseEvent, "the event passed is an instance of MouseEvent"); + assert.instanceOf(event, MouseEvent, "the event passed is an instance of MouseEvent"); }; assert.strictEqual(mouseDispatcher.onMouseDown(callback), mouseDispatcher, @@ -84,7 +84,7 @@ describe("Dispatchers", () => { callbackWasCalled = true; TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); assert.isNotNull(event, "mouse event was passed to the callback"); - assert.isTrue(event instanceof MouseEvent, "the event passed is an instance of MouseEvent"); + assert.instanceOf(event, MouseEvent, "the event passed is an instance of MouseEvent"); }; assert.strictEqual(mouseDispatcher.onMouseUp(callback), mouseDispatcher, @@ -119,7 +119,7 @@ describe("Dispatchers", () => { assert.strictEqual(event.deltaY, targetDeltaY, "deltaY value was passed to callback"); TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); assert.isNotNull(event, "mouse event was passed to the callback"); - assert.isTrue(event instanceof MouseEvent, "the event passed is an instance of MouseEvent"); + assert.instanceOf(event, MouseEvent, "the event passed is an instance of MouseEvent"); }; assert.strictEqual(mouseDispatcher.onWheel(callback), mouseDispatcher, @@ -144,7 +144,7 @@ describe("Dispatchers", () => { callbackWasCalled = true; TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); assert.isNotNull(event, "mouse event was passed to the callback"); - assert.isTrue(event instanceof MouseEvent, "the event passed is an instance of MouseEvent"); + assert.instanceOf(event, MouseEvent, "the event passed is an instance of MouseEvent"); }; assert.strictEqual(mouseDispatcher.onDblClick(callback), mouseDispatcher, @@ -169,7 +169,7 @@ describe("Dispatchers", () => { callbackWasCalled = true; TestMethods.assertPointsClose(point, expectedPoint, 0.5, "mouse position is correct"); assert.isNotNull(event, "mouse event was passed to the callback"); - assert.isTrue(event instanceof MouseEvent, "the event passed is an instance of MouseEvent"); + assert.instanceOf(event, MouseEvent, "the event passed is an instance of MouseEvent"); }; mouseDispatcher.onMouseMove(callback); From c58125c1395104366a8389cfb460a591f465d8ad Mon Sep 17 00:00:00 2001 From: Andrei Cioara Date: Mon, 21 Sep 2015 17:38:33 -0700 Subject: [PATCH 12/12] [refactorMouseDispatcher] fixed typo --- test/dispatchers/mouseDispatcherTests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dispatchers/mouseDispatcherTests.ts b/test/dispatchers/mouseDispatcherTests.ts index 804128a6eb..5a7ae6a1ff 100644 --- a/test/dispatchers/mouseDispatcherTests.ts +++ b/test/dispatchers/mouseDispatcherTests.ts @@ -196,7 +196,7 @@ describe("Dispatchers", () => { svg.remove(); }); - it("can register two callbacks for the samme mouse dispatcher", () => { + it("can register two callbacks for the same mouse dispatcher", () => { let cb1Called = false; let cb1 = () => cb1Called = true; let cb2Called = false;