mirror of
https://github.com/nasa/openmct.git
synced 2024-12-23 15:02:23 +00:00
[Fixed Position] Add unit tests for new code
Refactored ElementProxy and UnitAccessorMutator slightly to improve encasulation. Added unit tests for UnitAccessorMutator
This commit is contained in:
parent
40c68e6399
commit
3ed0880c6e
@ -174,54 +174,6 @@ define(
|
||||
return this.resizeHandles;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set whether this elements's position is determined in terms of grid
|
||||
* units or pixels.
|
||||
* @param {string} key Which unit to use, px or grid
|
||||
*/
|
||||
ElementProxy.prototype.setUnits = function (key) {
|
||||
if (key === 'px' && this.element.useGrid === true) {
|
||||
this.element.useGrid = false;
|
||||
this.convertCoordsTo('px');
|
||||
} else if (key === 'grid' && this.element.useGrid === false) {
|
||||
this.element.useGrid = true;
|
||||
this.convertCoordsTo('grid');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert this element's coordinates and size from pixels to grid units,
|
||||
* or vice-versa.
|
||||
* @param {string} unit When called with 'px', converts grid units to
|
||||
* pixels; when called with 'grid', snaps element
|
||||
* to grid units
|
||||
*/
|
||||
ElementProxy.prototype.convertCoordsTo = function (unit) {
|
||||
var gridSize = this.gridSize;
|
||||
var element = this.element;
|
||||
var minWidth = this.getMinWidth();
|
||||
var minHeight = this.getMinHeight();
|
||||
if (unit === 'px') {
|
||||
element.x = element.x * gridSize[0];
|
||||
element.y = element.y * gridSize[1];
|
||||
element.width = element.width * gridSize[0];
|
||||
element.height = element.height * gridSize[1];
|
||||
if (element.x2 && element.y2) {
|
||||
element.x2 = element.x2 * gridSize[0];
|
||||
element.y2 = element.y2 * gridSize[1];
|
||||
}
|
||||
} else if (unit === 'grid') {
|
||||
element.x = Math.round(element.x / gridSize[0]);
|
||||
element.y = Math.round(element.y / gridSize[1]);
|
||||
element.width = Math.max(Math.round(element.width / gridSize[0]), minWidth);
|
||||
element.height = Math.max(Math.round(element.height / gridSize[1]), minHeight);
|
||||
if (element.x2 && element.y2) {
|
||||
element.x2 = Math.round(element.x2 / gridSize[0]);
|
||||
element.y2 = Math.round(element.y2 / gridSize[1]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns which grid size the element is currently using.
|
||||
* @return {number[]} The current grid size in [x,y] form if the element
|
||||
|
@ -35,14 +35,17 @@ define(
|
||||
* upon
|
||||
*/
|
||||
function UnitAccessorMutator(elementProxy) {
|
||||
var self = this;
|
||||
|
||||
this.elementProxy = elementProxy;
|
||||
return function (useGrid) {
|
||||
var current = elementProxy.element.useGrid;
|
||||
if (arguments.length > 0) {
|
||||
elementProxy.element.useGrid = useGrid;
|
||||
if (useGrid && !current) {
|
||||
elementProxy.convertCoordsTo('grid');
|
||||
self.convertCoordsTo('grid');
|
||||
} else if (!useGrid && current) {
|
||||
elementProxy.convertCoordsTo('px');
|
||||
self.convertCoordsTo('px');
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,6 +53,40 @@ define(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* For the elementProxy object called upon, convert its element's
|
||||
* coordinates and size from pixels to grid units, or vice-versa.
|
||||
* @param {string} unit When called with 'px', converts grid units to
|
||||
* pixels; when called with 'grid', snaps element
|
||||
* to grid units
|
||||
*/
|
||||
UnitAccessorMutator.prototype.convertCoordsTo = function (unit) {
|
||||
var proxy = this.elementProxy,
|
||||
gridSize = proxy.gridSize,
|
||||
element = proxy.element,
|
||||
minWidth = proxy.getMinWidth(),
|
||||
minHeight = proxy.getMinHeight();
|
||||
if (unit === 'px') {
|
||||
element.x = element.x * gridSize[0];
|
||||
element.y = element.y * gridSize[1];
|
||||
element.width = element.width * gridSize[0];
|
||||
element.height = element.height * gridSize[1];
|
||||
if (element.x2 && element.y2) {
|
||||
element.x2 = element.x2 * gridSize[0];
|
||||
element.y2 = element.y2 * gridSize[1];
|
||||
}
|
||||
} else if (unit === 'grid') {
|
||||
element.x = Math.round(element.x / gridSize[0]);
|
||||
element.y = Math.round(element.y / gridSize[1]);
|
||||
element.width = Math.max(Math.round(element.width / gridSize[0]), minWidth);
|
||||
element.height = Math.max(Math.round(element.height / gridSize[1]), minHeight);
|
||||
if (element.x2 && element.y2) {
|
||||
element.x2 = Math.round(element.x2 / gridSize[0]);
|
||||
element.y2 = Math.round(element.y2 / gridSize[1]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return UnitAccessorMutator;
|
||||
}
|
||||
);
|
||||
|
@ -0,0 +1,72 @@
|
||||
define(
|
||||
['../../src/elements/UnitAccessorMutator'],
|
||||
function (UnitAccessorMutator) {
|
||||
|
||||
var GRID_SIZE = [13,17];
|
||||
|
||||
describe("An elementProxy.gridSize accessor-mutator", function () {
|
||||
var mockElementProxy,
|
||||
testElement,
|
||||
uAM;
|
||||
|
||||
beforeEach(function () {
|
||||
testElement = {
|
||||
x: 2,
|
||||
y: 3,
|
||||
width: 4,
|
||||
height: 5,
|
||||
useGrid: true
|
||||
};
|
||||
|
||||
mockElementProxy = {
|
||||
element: testElement,
|
||||
gridSize: GRID_SIZE,
|
||||
getMinHeight: jasmine.createSpy('minHeight'),
|
||||
getMinWidth: jasmine.createSpy('minWidth')
|
||||
};
|
||||
|
||||
uAM = new UnitAccessorMutator(mockElementProxy);
|
||||
|
||||
mockElementProxy.getMinWidth.andReturn(1);
|
||||
mockElementProxy.getMinHeight.andReturn(1);
|
||||
|
||||
});
|
||||
|
||||
it("allows access to useGrid", function () {
|
||||
expect(uAM()).toEqual(mockElementProxy.element.useGrid);
|
||||
});
|
||||
|
||||
it("allows mutation of useGrid", function () {
|
||||
uAM(false);
|
||||
expect(mockElementProxy.element.useGrid).toEqual(false);
|
||||
});
|
||||
|
||||
it("converts coordinates appropriately", function () {
|
||||
uAM(false);
|
||||
expect(mockElementProxy.element.x).toEqual(26);
|
||||
expect(mockElementProxy.element.y).toEqual(51);
|
||||
expect(mockElementProxy.element.width).toEqual(52);
|
||||
expect(mockElementProxy.element.height).toEqual(85);
|
||||
uAM(true);
|
||||
expect(mockElementProxy.element.x).toEqual(2);
|
||||
expect(mockElementProxy.element.y).toEqual(3);
|
||||
expect(mockElementProxy.element.width).toEqual(4);
|
||||
expect(mockElementProxy.element.height).toEqual(5);
|
||||
});
|
||||
|
||||
it("doesn't covert coordinates unecessarily", function () {
|
||||
uAM(false);
|
||||
expect(mockElementProxy.element.x).toEqual(26);
|
||||
expect(mockElementProxy.element.y).toEqual(51);
|
||||
expect(mockElementProxy.element.width).toEqual(52);
|
||||
expect(mockElementProxy.element.height).toEqual(85);
|
||||
uAM(false);
|
||||
expect(mockElementProxy.element.x).toEqual(26);
|
||||
expect(mockElementProxy.element.y).toEqual(51);
|
||||
expect(mockElementProxy.element.width).toEqual(52);
|
||||
expect(mockElementProxy.element.height).toEqual(85);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
);
|
Loading…
Reference in New Issue
Block a user