mirror of
https://github.com/google/blockly.git
synced 2026-01-11 02:47:09 +01:00
Adds tests for metrics manager (#4635)
This commit is contained in:
@@ -555,8 +555,8 @@ Blockly.Bubble.prototype.getOverlap_ = function(relativeMin, viewMetrics) {
|
||||
* Calculate what the optimal horizontal position of the top-left corner of the
|
||||
* bubble is (relative to the anchor point) so that the most area of the
|
||||
* bubble is shown.
|
||||
* @param {!Blockly.MetricsManager.ContainerRegion} viewMetrics The view metrics of the
|
||||
* workspace the bubble will appear in.
|
||||
* @param {!Blockly.MetricsManager.ContainerRegion} viewMetrics The view metrics
|
||||
* of the workspace the bubble will appear in.
|
||||
* @return {number} The optimal horizontal position of the top-left corner
|
||||
* of the bubble.
|
||||
* @private
|
||||
@@ -613,8 +613,8 @@ Blockly.Bubble.prototype.getOptimalRelativeLeft_ = function(viewMetrics) {
|
||||
* Calculate what the optimal vertical position of the top-left corner of
|
||||
* the bubble is (relative to the anchor point) so that the most area of the
|
||||
* bubble is shown.
|
||||
* @param {!Blockly.MetricsManager.ContainerRegion} viewMetrics The view metrics of the
|
||||
* workspace the bubble will appear in.
|
||||
* @param {!Blockly.MetricsManager.ContainerRegion} viewMetrics The view metrics
|
||||
* of the workspace the bubble will appear in.
|
||||
* @return {number} The optimal vertical position of the top-left corner
|
||||
* of the bubble.
|
||||
* @private
|
||||
|
||||
@@ -57,7 +57,8 @@ Blockly.IMetricsManager.prototype.getContentDimensionsExact_;
|
||||
* coordinates. Returns 0 for the width and height if the workspace has a
|
||||
* category toolbox instead of a simple toolbox.
|
||||
* @param {boolean=} opt_own Whether to only return the workspace's own flyout.
|
||||
* @return {!Blockly.utils.Size} The width and height of the flyout.
|
||||
* @return {!Blockly.MetricsManager.ToolboxMetrics} The width and height of the
|
||||
* flyout.
|
||||
* @public
|
||||
*/
|
||||
Blockly.IMetricsManager.prototype.getFlyoutMetrics;
|
||||
@@ -113,11 +114,11 @@ Blockly.IMetricsManager.prototype.getViewMetrics;
|
||||
*
|
||||
* If the workspace does not have a fixed width and height then it is the
|
||||
* metrics of the area that content can be placed.
|
||||
* @param {boolean=} opt_getWorkspaceCoordinates True to get the content metrics
|
||||
* in workspace coordinates, false to get them in pixel coordinates.
|
||||
* @param {!Blockly.MetricsManager.ContainerRegion=} opt_viewMetrics The view
|
||||
* metrics if they have been previously computed. Passing in null may cause
|
||||
* the view metrics to be computed again, if it is needed.
|
||||
* @param {boolean=} opt_getWorkspaceCoordinates True to get the content metrics
|
||||
* in workspace coordinates, false to get them in pixel coordinates.
|
||||
* @return {!Blockly.MetricsManager.ContainerRegion} The
|
||||
* metrics for the content container.
|
||||
* @public
|
||||
|
||||
@@ -153,14 +153,17 @@ Blockly.MetricsManager.prototype.getContentDimensionsExact_ = function() {
|
||||
* coordinates. Returns 0 for the width and height if the workspace has a
|
||||
* category toolbox instead of a simple toolbox.
|
||||
* @param {boolean=} opt_own Whether to only return the workspace's own flyout.
|
||||
* @return {!Blockly.utils.Size} The width and height of the flyout.
|
||||
* @return {!Blockly.MetricsManager.ToolboxMetrics} The width and height of the
|
||||
* flyout.
|
||||
* @public
|
||||
*/
|
||||
Blockly.MetricsManager.prototype.getFlyoutMetrics = function(opt_own) {
|
||||
var flyoutDimensions =
|
||||
this.getDimensionsPx_(this.workspace_.getFlyout(opt_own));
|
||||
return new Blockly.utils.Size(
|
||||
flyoutDimensions.width, flyoutDimensions.height);
|
||||
var flyoutDimensions = this.getDimensionsPx_(this.workspace_.getFlyout(opt_own));
|
||||
return {
|
||||
width: flyoutDimensions.width,
|
||||
height: flyoutDimensions.height,
|
||||
position: this.workspace_.toolboxPosition
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -206,8 +209,9 @@ Blockly.MetricsManager.prototype.getAbsoluteMetrics = function() {
|
||||
var toolboxMetrics = this.getToolboxMetrics();
|
||||
var flyoutMetrics = this.getFlyoutMetrics(true);
|
||||
var doesToolboxExist = !!this.workspace_.getToolbox();
|
||||
var toolboxPosition = this.workspace_.toolboxPosition;
|
||||
var doesFlyoutExist = !!this.workspace_.getFlyout(true);
|
||||
var toolboxPosition =
|
||||
doesToolboxExist ? toolboxMetrics.position : flyoutMetrics.position;
|
||||
|
||||
if (doesToolboxExist && toolboxPosition == Blockly.TOOLBOX_AT_LEFT) {
|
||||
absoluteLeft = toolboxMetrics.width;
|
||||
@@ -243,7 +247,9 @@ Blockly.MetricsManager.prototype.getViewMetrics = function(
|
||||
var svgMetrics = this.getSvgMetrics();
|
||||
var toolboxMetrics = this.getToolboxMetrics();
|
||||
var flyoutMetrics = this.getFlyoutMetrics(true);
|
||||
var toolboxPosition = this.workspace_.toolboxPosition;
|
||||
var doesToolboxExist = !!this.workspace_.getToolbox();
|
||||
var toolboxPosition =
|
||||
doesToolboxExist ? toolboxMetrics.position : flyoutMetrics.position;
|
||||
|
||||
if (this.workspace_.getToolbox()) {
|
||||
if (toolboxPosition == Blockly.TOOLBOX_AT_TOP ||
|
||||
@@ -282,17 +288,17 @@ Blockly.MetricsManager.prototype.getViewMetrics = function(
|
||||
* metrics of the area that content can be placed. This area is computed by
|
||||
* getting the rectangle around the top bounded elements on the workspace and
|
||||
* adding padding to all sides.
|
||||
* @param {!Blockly.MetricsManager.ContainerRegion=} opt_viewMetrics The view
|
||||
* metrics if they have been previously computed. Passing in null may cause
|
||||
* the view metrics to be computed again, if it is needed.
|
||||
* @param {boolean=} opt_getWorkspaceCoordinates True to get the content metrics
|
||||
* in workspace coordinates, false to get them in pixel coordinates.
|
||||
* @param {!Blockly.MetricsManager.ContainerRegion=} opt_viewMetrics The view
|
||||
* metrics if they have been previously computed. Not passing in view
|
||||
* metrics may cause them to be computed again.
|
||||
* @return {!Blockly.MetricsManager.ContainerRegion} The
|
||||
* metrics for the content container.
|
||||
* @public
|
||||
*/
|
||||
Blockly.MetricsManager.prototype.getContentMetrics = function(
|
||||
opt_viewMetrics, opt_getWorkspaceCoordinates) {
|
||||
opt_getWorkspaceCoordinates, opt_viewMetrics) {
|
||||
var scale = opt_getWorkspaceCoordinates ? this.workspace_.scale : 1;
|
||||
var contentDimensions = null;
|
||||
if (this.workspace_.isContentBounded()) {
|
||||
@@ -347,7 +353,7 @@ Blockly.MetricsManager.prototype.getMetrics = function() {
|
||||
var svgMetrics = this.getSvgMetrics();
|
||||
var absoluteMetrics = this.getAbsoluteMetrics();
|
||||
var viewMetrics = this.getViewMetrics();
|
||||
var contentMetrics = this.getContentMetrics(viewMetrics);
|
||||
var contentMetrics = this.getContentMetrics(false, viewMetrics);
|
||||
|
||||
return {
|
||||
contentHeight: contentMetrics.height,
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2019 Google LLC
|
||||
* Copyright 2021 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Metrics tests.
|
||||
* @author samelh@google.com (Sam El-Husseini)
|
||||
* @fileoverview Test for the metrics manager.
|
||||
* @author aschmiedt@google.com (Abby Schmiedt)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
suite('Metrics', function() {
|
||||
var SCROLL_X = 10;
|
||||
var SCROLL_Y = 10;
|
||||
function assertDimensionsMatch(toCheck, left, top, width, height) {
|
||||
chai.assert.equal(top, toCheck.top, 'Top did not match.');
|
||||
chai.assert.equal(left, toCheck.left, 'Left did not match.');
|
||||
@@ -23,14 +25,14 @@ suite('Metrics', function() {
|
||||
function makeMockWs(scale, x, y, width, height) {
|
||||
return {
|
||||
getBlocksBoundingBox: function() {
|
||||
return {
|
||||
top: y,
|
||||
bottom: y + height,
|
||||
left: x,
|
||||
right: x + width
|
||||
};
|
||||
return {top: y, bottom: y + height, left: x, right: x + width};
|
||||
},
|
||||
scale: scale
|
||||
getToolbox: function() {},
|
||||
getFlyout: function() {},
|
||||
scale: scale,
|
||||
scrollX: SCROLL_X,
|
||||
scrollY: SCROLL_Y,
|
||||
isContentBounded: function() {}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -41,85 +43,307 @@ suite('Metrics', function() {
|
||||
sharedTestTeardown.call(this);
|
||||
});
|
||||
|
||||
test('GetContentDimensionsExact - empty', function() {
|
||||
var ws = makeMockWs(1, 0, 0, 0, 0);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var defaultZoom = metricsManager.getContentDimensionsExact_(ws);
|
||||
assertDimensionsMatch(defaultZoom, 0, 0, 0, 0);
|
||||
suite('getContentDimensionsExact_', function() {
|
||||
test('Empty', function() {
|
||||
var ws = makeMockWs(1, 0, 0, 0, 0);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var defaultZoom = metricsManager.getContentDimensionsExact_(ws);
|
||||
assertDimensionsMatch(defaultZoom, 0, 0, 0, 0);
|
||||
});
|
||||
test('Empty zoom in', function() {
|
||||
var ws = makeMockWs(2, 0, 0, 0, 0);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomIn = metricsManager.getContentDimensionsExact_(ws);
|
||||
assertDimensionsMatch(zoomIn, 0, 0, 0, 0);
|
||||
});
|
||||
test('Empty zoom out', function() {
|
||||
var ws = makeMockWs(.5, 0, 0, 0, 0);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomOut = metricsManager.getContentDimensionsExact_(ws);
|
||||
assertDimensionsMatch(zoomOut, 0, 0, 0, 0);
|
||||
});
|
||||
test('Non empty at origin', function() {
|
||||
var ws = makeMockWs(1, 0, 0, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var defaultZoom = metricsManager.getContentDimensionsExact_(ws);
|
||||
// Pixel and ws units are the same at default zoom.
|
||||
assertDimensionsMatch(defaultZoom, 0, 0, 100, 100);
|
||||
});
|
||||
test('Non empty at origin zoom in', function() {
|
||||
var ws = makeMockWs(2, 0, 0, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomIn = metricsManager.getContentDimensionsExact_(ws);
|
||||
// 1 ws unit = 2 pixels at this zoom level.
|
||||
assertDimensionsMatch(zoomIn, 0, 0, 200, 200);
|
||||
});
|
||||
test('Non empty at origin zoom out', function() {
|
||||
var ws = makeMockWs(.5, 0, 0, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomOut = metricsManager.getContentDimensionsExact_(ws);
|
||||
// 1 ws unit = 0.5 pixels at this zoom level.
|
||||
assertDimensionsMatch(zoomOut, 0, 0, 50, 50);
|
||||
});
|
||||
test('Non empty positive origin', function() {
|
||||
var ws = makeMockWs(1, 10, 10, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var defaultZoom = metricsManager.getContentDimensionsExact_(ws);
|
||||
// Pixel and ws units are the same at default zoom.
|
||||
assertDimensionsMatch(defaultZoom, 10, 10, 100, 100);
|
||||
});
|
||||
test('Non empty positive origin zoom in', function() {
|
||||
var ws = makeMockWs(2, 10, 10, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomIn = metricsManager.getContentDimensionsExact_(ws);
|
||||
// 1 ws unit = 2 pixels at this zoom level.
|
||||
assertDimensionsMatch(zoomIn, 20, 20, 200, 200);
|
||||
});
|
||||
test('Non empty positive origin zoom out', function() {
|
||||
var ws = makeMockWs(.5, 10, 10, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomOut = metricsManager.getContentDimensionsExact_(ws);
|
||||
// 1 ws unit = 0.5 pixels at this zoom level.
|
||||
assertDimensionsMatch(zoomOut, 5, 5, 50, 50);
|
||||
});
|
||||
test('Non empty negative origin', function() {
|
||||
var ws = makeMockWs(1, -10, -10, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var defaultZoom = metricsManager.getContentDimensionsExact_(ws);
|
||||
// Pixel and ws units are the same at default zoom.
|
||||
assertDimensionsMatch(defaultZoom, -10, -10, 100, 100);
|
||||
});
|
||||
test('Non empty negative origin zoom in', function() {
|
||||
var ws = makeMockWs(2, -10, -10, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomIn = metricsManager.getContentDimensionsExact_(ws);
|
||||
// 1 ws unit = 2 pixels at this zoom level.
|
||||
assertDimensionsMatch(zoomIn, -20, -20, 200, 200);
|
||||
});
|
||||
test('Non empty negative origin zoom out', function() {
|
||||
var ws = makeMockWs(.5, -10, -10, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomOut = metricsManager.getContentDimensionsExact_(ws);
|
||||
// 1 ws unit = 0.5 pixels at this zoom level.
|
||||
assertDimensionsMatch(zoomOut, -5, -5, 50, 50);
|
||||
});
|
||||
});
|
||||
test('GetContentDimensionsExact - empty zoom in', function() {
|
||||
var ws = makeMockWs(2, 0, 0, 0, 0);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomIn = metricsManager.getContentDimensionsExact_(ws);
|
||||
assertDimensionsMatch(zoomIn, 0, 0, 0, 0);
|
||||
|
||||
suite('getContentDimensionsBounded_', function() {
|
||||
setup(function() {
|
||||
this.ws = makeMockWs(1, 0, 0, 0, 0);
|
||||
this.metricsManager = new Blockly.MetricsManager(this.ws);
|
||||
this.contentDimensionsStub =
|
||||
sinon.stub(this.metricsManager, 'getContentDimensionsExact_');
|
||||
});
|
||||
test('Empty workspace', function() {
|
||||
// The location of the viewport.
|
||||
var mockViewMetrics = {top: 0, left: 0, width: 200, height: 200};
|
||||
// The bounding box around the blocks on the screen.
|
||||
var mockContentDimensions = {top: 0, left: 0, width: 0, height: 0};
|
||||
this.contentDimensionsStub.returns(mockContentDimensions);
|
||||
|
||||
var contentMetrics =
|
||||
this.metricsManager.getContentDimensionsBounded_(mockViewMetrics);
|
||||
|
||||
// Should add half the view width to all sides.
|
||||
assertDimensionsMatch(contentMetrics, -200, -200, 400, 400);
|
||||
});
|
||||
test('Non empty workspace', function() {
|
||||
// The location of the viewport.
|
||||
var mockViewMetrics = {top: 0, left: 0, width: 200, height: 200};
|
||||
// The bounding box around the blocks on the screen.
|
||||
var mockContentDimensions = {top: 100, left: 100, width: 50, height: 50};
|
||||
this.contentDimensionsStub.returns(mockContentDimensions);
|
||||
|
||||
var contentMetrics =
|
||||
this.metricsManager.getContentDimensionsBounded_(mockViewMetrics);
|
||||
|
||||
// Should add half of the view width to all sides.
|
||||
assertDimensionsMatch(contentMetrics, -50, -50, 350, 350);
|
||||
});
|
||||
});
|
||||
test('GetContentDimensionsExact - empty zoom out', function() {
|
||||
var ws = makeMockWs(.5, 0, 0, 0, 0);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomOut = metricsManager.getContentDimensionsExact_(ws);
|
||||
assertDimensionsMatch(zoomOut, 0, 0, 0, 0);
|
||||
|
||||
suite('getAbsoluteMetrics', function() {
|
||||
setup(function() {
|
||||
this.ws = makeMockWs(1, 0, 0, 0, 0);
|
||||
this.metricsManager = new Blockly.MetricsManager(this.ws);
|
||||
this.toolboxMetricsStub =
|
||||
sinon.stub(this.metricsManager, 'getToolboxMetrics');
|
||||
this.flyoutMetricsStub =
|
||||
sinon.stub(this.metricsManager, 'getFlyoutMetrics');
|
||||
this.getToolboxStub =
|
||||
sinon.stub(this.metricsManager.workspace_, 'getToolbox');
|
||||
this.getFlyoutStub =
|
||||
sinon.stub(this.metricsManager.workspace_, 'getFlyout');
|
||||
});
|
||||
test('Toolbox at left', function() {
|
||||
this.toolboxMetricsStub.returns({width: 107, height: 0, position: 2});
|
||||
this.flyoutMetricsStub.returns({});
|
||||
this.getToolboxStub.returns(true);
|
||||
this.getFlyoutStub.returns(false);
|
||||
|
||||
var absoluteMetrics = this.metricsManager.getAbsoluteMetrics();
|
||||
|
||||
assertDimensionsMatch(absoluteMetrics, 107, 0);
|
||||
});
|
||||
test('Toolbox at top', function() {
|
||||
this.toolboxMetricsStub.returns({width: 0, height: 107, position: 0});
|
||||
this.flyoutMetricsStub.returns({});
|
||||
this.getToolboxStub.returns(true);
|
||||
this.getFlyoutStub.returns(false);
|
||||
|
||||
var absoluteMetrics = this.metricsManager.getAbsoluteMetrics();
|
||||
|
||||
assertDimensionsMatch(absoluteMetrics, 0, 107);
|
||||
});
|
||||
test('Flyout at left', function() {
|
||||
this.toolboxMetricsStub.returns({});
|
||||
this.flyoutMetricsStub.returns({width: 107, height: 0, position: 2});
|
||||
this.getToolboxStub.returns(false);
|
||||
this.getFlyoutStub.returns(true);
|
||||
|
||||
var absoluteMetrics = this.metricsManager.getAbsoluteMetrics();
|
||||
|
||||
assertDimensionsMatch(absoluteMetrics, 107, 0);
|
||||
});
|
||||
test('Flyout at top', function() {
|
||||
this.toolboxMetricsStub.returns({});
|
||||
this.flyoutMetricsStub.returns({width: 0, height: 107, position: 0});
|
||||
this.getToolboxStub.returns(false);
|
||||
this.getFlyoutStub.returns(true);
|
||||
|
||||
var absoluteMetrics = this.metricsManager.getAbsoluteMetrics();
|
||||
|
||||
assertDimensionsMatch(absoluteMetrics, 0, 107);
|
||||
});
|
||||
});
|
||||
test('GetContentDimensionsExact - non empty at origin', function() {
|
||||
var ws = makeMockWs(1, 0, 0, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var defaultZoom = metricsManager.getContentDimensionsExact_(ws);
|
||||
// Pixel and ws units are the same at default zoom.
|
||||
assertDimensionsMatch(defaultZoom, 0, 0, 100, 100);
|
||||
|
||||
suite('getViewMetrics', function() {
|
||||
setup(function() {
|
||||
this.ws = makeMockWs(1, 0, 0, 0, 0);
|
||||
this.metricsManager = new Blockly.MetricsManager(this.ws);
|
||||
this.toolboxMetricsStub =
|
||||
sinon.stub(this.metricsManager, 'getToolboxMetrics');
|
||||
this.flyoutMetricsStub =
|
||||
sinon.stub(this.metricsManager, 'getFlyoutMetrics');
|
||||
this.getToolboxStub =
|
||||
sinon.stub(this.metricsManager.workspace_, 'getToolbox');
|
||||
this.getFlyoutStub =
|
||||
sinon.stub(this.metricsManager.workspace_, 'getFlyout');
|
||||
this.svgMetricsStub = sinon.stub(this.metricsManager, 'getSvgMetrics');
|
||||
});
|
||||
test('Toolbox at left', function() {
|
||||
this.toolboxMetricsStub.returns({width: 107, height: 0, position: 2});
|
||||
this.flyoutMetricsStub.returns({});
|
||||
this.svgMetricsStub.returns({width: 500, height: 500});
|
||||
this.getToolboxStub.returns(true);
|
||||
this.getFlyoutStub.returns(false);
|
||||
|
||||
var viewMetrics = this.metricsManager.getViewMetrics();
|
||||
|
||||
assertDimensionsMatch(viewMetrics, -SCROLL_X, -SCROLL_Y, 393, 500);
|
||||
});
|
||||
test('Toolbox at top', function() {
|
||||
this.toolboxMetricsStub.returns({width: 0, height: 107, position: 0});
|
||||
this.flyoutMetricsStub.returns({});
|
||||
this.svgMetricsStub.returns({width: 500, height: 500});
|
||||
this.getToolboxStub.returns(true);
|
||||
this.getFlyoutStub.returns(false);
|
||||
|
||||
var viewMetrics = this.metricsManager.getViewMetrics();
|
||||
|
||||
assertDimensionsMatch(viewMetrics, -SCROLL_X, -SCROLL_Y, 500, 393);
|
||||
});
|
||||
test('Flyout at left', function() {
|
||||
this.toolboxMetricsStub.returns({});
|
||||
this.flyoutMetricsStub.returns({width: 107, height: 0, position: 2});
|
||||
this.svgMetricsStub.returns({width: 500, height: 500});
|
||||
this.getToolboxStub.returns(false);
|
||||
this.getFlyoutStub.returns(true);
|
||||
|
||||
var viewMetrics = this.metricsManager.getViewMetrics();
|
||||
|
||||
assertDimensionsMatch(viewMetrics, -SCROLL_X, -SCROLL_Y, 393, 500);
|
||||
});
|
||||
test('Flyout at top', function() {
|
||||
this.toolboxMetricsStub.returns({});
|
||||
this.flyoutMetricsStub.returns({width: 0, height: 107, position: 0});
|
||||
this.svgMetricsStub.returns({width: 500, height: 500});
|
||||
this.getToolboxStub.returns(false);
|
||||
this.getFlyoutStub.returns(true);
|
||||
|
||||
var viewMetrics = this.metricsManager.getViewMetrics();
|
||||
|
||||
assertDimensionsMatch(viewMetrics, -SCROLL_X, -SCROLL_Y, 500, 393);
|
||||
});
|
||||
test('Get view metrics in workspace coordinates ', function() {
|
||||
var scale = 2;
|
||||
var getWorkspaceCoordinates = true;
|
||||
|
||||
this.ws.scale = scale;
|
||||
this.toolboxMetricsStub.returns({});
|
||||
this.flyoutMetricsStub.returns({width: 0, height: 107, position: 0});
|
||||
this.svgMetricsStub.returns({width: 500, height: 500});
|
||||
this.getToolboxStub.returns(false);
|
||||
this.getFlyoutStub.returns(true);
|
||||
|
||||
var viewMetrics =
|
||||
this.metricsManager.getViewMetrics(getWorkspaceCoordinates);
|
||||
|
||||
assertDimensionsMatch(
|
||||
viewMetrics, -SCROLL_X / scale, -SCROLL_Y / scale, 500 / scale,
|
||||
393 / scale);
|
||||
});
|
||||
});
|
||||
test('GetContentDimensionsExact - non empty at origin zoom in', function() {
|
||||
var ws = makeMockWs(2, 0, 0, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomIn = metricsManager.getContentDimensionsExact_(ws);
|
||||
// 1 ws unit = 2 pixels at this zoom level.
|
||||
assertDimensionsMatch(zoomIn, 0, 0, 200, 200);
|
||||
});
|
||||
test('GetContentDimensionsExact - non empty at origin zoom out', function() {
|
||||
var ws = makeMockWs(.5, 0, 0, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomOut = metricsManager.getContentDimensionsExact_(ws);
|
||||
// 1 ws unit = 0.5 pixels at this zoom level.
|
||||
assertDimensionsMatch(zoomOut, 0, 0, 50, 50);
|
||||
});
|
||||
test('GetContentDimensionsExact - non empty positive origin', function() {
|
||||
var ws = makeMockWs(1, 10, 10, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var defaultZoom = metricsManager.getContentDimensionsExact_(ws);
|
||||
// Pixel and ws units are the same at default zoom.
|
||||
assertDimensionsMatch(defaultZoom, 10, 10, 100, 100);
|
||||
});
|
||||
test('GetContentDimensionsExact - non empty positive origin zoom in', function() {
|
||||
var ws = makeMockWs(2, 10, 10, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomIn = metricsManager.getContentDimensionsExact_(ws);
|
||||
// 1 ws unit = 2 pixels at this zoom level.
|
||||
assertDimensionsMatch(zoomIn, 20, 20, 200, 200);
|
||||
});
|
||||
test('GetContentDimensionsExact - non empty positive origin zoom out', function() {
|
||||
var ws = makeMockWs(.5, 10, 10, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomOut = metricsManager.getContentDimensionsExact_(ws);
|
||||
// 1 ws unit = 0.5 pixels at this zoom level.
|
||||
assertDimensionsMatch(zoomOut, 5, 5, 50, 50);
|
||||
});
|
||||
test('GetContentDimensionsExact - non empty negative origin', function() {
|
||||
var ws = makeMockWs(1, -10, -10, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var defaultZoom = metricsManager.getContentDimensionsExact_(ws);
|
||||
// Pixel and ws units are the same at default zoom.
|
||||
assertDimensionsMatch(defaultZoom, -10, -10, 100, 100);
|
||||
});
|
||||
test('GetContentDimensionsExact - non empty negative origin zoom in', function() {
|
||||
var ws = makeMockWs(2, -10, -10, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomIn = metricsManager.getContentDimensionsExact_(ws);
|
||||
// 1 ws unit = 2 pixels at this zoom level.
|
||||
assertDimensionsMatch(zoomIn, -20, -20, 200, 200);
|
||||
});
|
||||
test('GetContentDimensionsExact - non empty negative origin zoom out', function() {
|
||||
var ws = makeMockWs(.5, -10, -10, 100, 100);
|
||||
var metricsManager = new Blockly.MetricsManager(ws);
|
||||
var zoomOut = metricsManager.getContentDimensionsExact_(ws);
|
||||
// 1 ws unit = 0.5 pixels at this zoom level.
|
||||
assertDimensionsMatch(zoomOut, -5, -5, 50, 50);
|
||||
|
||||
suite('getContentMetrics', function() {
|
||||
setup(function() {
|
||||
this.ws = makeMockWs(1, 0, 0, 0, 0);
|
||||
this.metricsManager = new Blockly.MetricsManager(this.ws);
|
||||
this.viewMetricsStub = sinon.stub(this.metricsManager, 'getViewMetrics');
|
||||
this.isContentBoundedStub =
|
||||
sinon.stub(this.metricsManager.workspace_, 'isContentBounded');
|
||||
this.getBoundedMetricsStub =
|
||||
sinon.stub(this.metricsManager, 'getContentDimensionsBounded_');
|
||||
this.getExactMetricsStub =
|
||||
sinon.stub(this.metricsManager, 'getContentDimensionsExact_');
|
||||
});
|
||||
test('Content Dimensions in pixel coordinates bounded ws', function() {
|
||||
this.isContentBoundedStub.returns(true);
|
||||
this.getBoundedMetricsStub.returns(
|
||||
{height: 100, width: 100, left: 100, top: 100});
|
||||
|
||||
var contentMetrics = this.metricsManager.getContentMetrics(false);
|
||||
|
||||
// Should return what getContentDimensionsBounded_ returns.
|
||||
assertDimensionsMatch(contentMetrics, 100, 100, 100, 100);
|
||||
sinon.assert.calledOnce(this.getBoundedMetricsStub);
|
||||
sinon.assert.calledOnce(this.viewMetricsStub);
|
||||
});
|
||||
test('Content Dimensions in pixel coordinates exact ws', function() {
|
||||
this.isContentBoundedStub.returns(false);
|
||||
this.getExactMetricsStub.returns(
|
||||
{height: 100, width: 100, left: 100, top: 100});
|
||||
|
||||
var contentMetrics = this.metricsManager.getContentMetrics(false);
|
||||
|
||||
// Should return what getContentDimensionsExact_ returns.
|
||||
assertDimensionsMatch(contentMetrics, 100, 100, 100, 100);
|
||||
sinon.assert.calledOnce(this.getExactMetricsStub);
|
||||
sinon.assert.notCalled(this.viewMetricsStub);
|
||||
});
|
||||
test('Content Dimensions in ws coordinates bounded ws', function() {
|
||||
var getWorkspaceCoordinates = true;
|
||||
this.ws.scale = 2;
|
||||
this.isContentBoundedStub.returns(true);
|
||||
this.getBoundedMetricsStub.returns(
|
||||
{height: 100, width: 100, left: 100, top: 100});
|
||||
|
||||
var contentMetrics =
|
||||
this.metricsManager.getContentMetrics(getWorkspaceCoordinates);
|
||||
|
||||
assertDimensionsMatch(contentMetrics, 50, 50, 50, 50);
|
||||
sinon.assert.calledOnce(this.getBoundedMetricsStub);
|
||||
sinon.assert.calledOnce(this.viewMetricsStub);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user