2021-12-28 14:02:29 +00:00
|
|
|
import 'pathseg'
|
|
|
|
import { NS } from '../../../instrumented/svgcanvas/namespaces.js'
|
|
|
|
import * as draw from '../../../instrumented/svgcanvas/draw.js'
|
|
|
|
import * as units from '../../../instrumented/common/units.js'
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
describe('draw.Drawing', function () {
|
|
|
|
const addOwnSpies = (obj) => {
|
2021-12-28 14:02:29 +00:00
|
|
|
const methods = Object.keys(obj)
|
2019-11-24 03:29:24 +00:00
|
|
|
methods.forEach((method) => {
|
2021-12-28 14:02:29 +00:00
|
|
|
cy.spy(obj, method)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const LAYER_CLASS = draw.Layer.CLASS_NAME
|
|
|
|
const NONCE = 'foo'
|
|
|
|
const LAYER1 = 'Layer 1'
|
|
|
|
const LAYER2 = 'Layer 2'
|
|
|
|
const LAYER3 = 'Layer 3'
|
2019-11-24 03:29:24 +00:00
|
|
|
const PATH_ATTR = {
|
|
|
|
// clone will convert relative to absolute, so the test for equality fails.
|
|
|
|
// d: 'm7.38867,57.38867c0,-27.62431 22.37569,-50 50,-50c27.62431,0 50,22.37569 50,50c0,27.62431 -22.37569,50 -50,50c-27.62431,0 -50,-22.37569 -50,-50z',
|
|
|
|
d: 'M7.389,57.389C7.389,29.764 29.764,7.389 57.389,7.389C85.013,7.389 107.389,29.764 107.389,57.389C107.389,85.013 85.013,107.389 57.389,107.389C29.764,107.389 7.389,85.013 7.389,57.389z',
|
|
|
|
transform: 'rotate(45 57.388671875000036,57.388671874999986) ',
|
|
|
|
'stroke-width': '5',
|
|
|
|
stroke: '#660000',
|
|
|
|
fill: '#ff0000'
|
2021-12-28 14:02:29 +00:00
|
|
|
}
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
units.init(
|
|
|
|
/**
|
|
|
|
* @implements {module:units.ElementContainer}
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
// used by units.shortFloat - call path: cloneLayer -> copyElem -> convertPath -> pathDSegment -> shortFloat
|
2021-12-28 14:02:29 +00:00
|
|
|
getRoundDigits () { return 3 }
|
2019-11-24 03:29:24 +00:00
|
|
|
}
|
2021-12-28 14:02:29 +00:00
|
|
|
)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
// Simplifying from svgcanvas.js usage
|
2021-12-28 14:02:29 +00:00
|
|
|
const idprefix = 'svg_'
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
const getCurrentDrawing = function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
return currentDrawing_
|
|
|
|
}
|
|
|
|
const setCurrentGroup = () => { /* empty fn */ }
|
2019-11-24 03:29:24 +00:00
|
|
|
draw.init(
|
|
|
|
/**
|
|
|
|
* @implements {module:draw.DrawCanvasInit}
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
getCurrentDrawing,
|
|
|
|
setCurrentGroup
|
|
|
|
}
|
2021-12-28 14:02:29 +00:00
|
|
|
)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {module:utilities.SVGElementJSON} jsonMap
|
|
|
|
* @returns {SVGElement}
|
|
|
|
*/
|
|
|
|
function createSVGElement (jsonMap) {
|
2021-12-28 14:02:29 +00:00
|
|
|
const elem = document.createElementNS(NS.SVG, jsonMap.element)
|
|
|
|
Object.entries(jsonMap.attr).forEach(([attr, value]) => {
|
|
|
|
elem.setAttribute(attr, value)
|
|
|
|
})
|
|
|
|
return elem
|
2019-11-24 03:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const setupSVGWith3Layers = function (svgElem) {
|
2021-12-28 14:02:29 +00:00
|
|
|
const layer1 = document.createElementNS(NS.SVG, 'g')
|
|
|
|
const layer1Title = document.createElementNS(NS.SVG, 'title')
|
|
|
|
layer1Title.append(LAYER1)
|
|
|
|
layer1.append(layer1Title)
|
|
|
|
svgElem.append(layer1)
|
|
|
|
|
|
|
|
const layer2 = document.createElementNS(NS.SVG, 'g')
|
|
|
|
const layer2Title = document.createElementNS(NS.SVG, 'title')
|
|
|
|
layer2Title.append(LAYER2)
|
|
|
|
layer2.append(layer2Title)
|
|
|
|
svgElem.append(layer2)
|
|
|
|
|
|
|
|
const layer3 = document.createElementNS(NS.SVG, 'g')
|
|
|
|
const layer3Title = document.createElementNS(NS.SVG, 'title')
|
|
|
|
layer3Title.append(LAYER3)
|
|
|
|
layer3.append(layer3Title)
|
|
|
|
svgElem.append(layer3)
|
|
|
|
|
|
|
|
return [layer1, layer2, layer3]
|
|
|
|
}
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
const createSomeElementsInGroup = function (group) {
|
|
|
|
group.append(
|
|
|
|
createSVGElement({
|
|
|
|
element: 'path',
|
|
|
|
attr: PATH_ATTR
|
|
|
|
}),
|
|
|
|
// createSVGElement({
|
|
|
|
// element: 'path',
|
|
|
|
// attr: {d: 'M0,1L2,3'}
|
|
|
|
// }),
|
|
|
|
createSVGElement({
|
|
|
|
element: 'rect',
|
2021-05-12 23:00:09 +00:00
|
|
|
attr: { x: '0', y: '1', width: '5', height: '10' }
|
2019-11-24 03:29:24 +00:00
|
|
|
}),
|
|
|
|
createSVGElement({
|
|
|
|
element: 'line',
|
2021-05-12 23:00:09 +00:00
|
|
|
attr: { x1: '0', y1: '1', x2: '5', y2: '6' }
|
2019-11-24 03:29:24 +00:00
|
|
|
})
|
2021-12-28 14:02:29 +00:00
|
|
|
)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
const g = createSVGElement({
|
|
|
|
element: 'g',
|
|
|
|
attr: {}
|
2021-12-28 14:02:29 +00:00
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
g.append(createSVGElement({
|
|
|
|
element: 'rect',
|
2021-05-12 23:00:09 +00:00
|
|
|
attr: { x: '0', y: '1', width: '5', height: '10' }
|
2021-12-28 14:02:29 +00:00
|
|
|
}))
|
|
|
|
group.append(g)
|
|
|
|
return 4
|
|
|
|
}
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
const cleanupSVG = function (svgElem) {
|
2021-12-28 14:02:29 +00:00
|
|
|
while (svgElem.firstChild) { svgElem.firstChild.remove() }
|
|
|
|
}
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
let sandbox; let currentDrawing_; let svg; let svgN
|
2019-11-24 03:29:24 +00:00
|
|
|
beforeEach(() => {
|
2021-12-28 14:02:29 +00:00
|
|
|
sandbox = document.createElement('div')
|
|
|
|
sandbox.id = 'sandbox'
|
|
|
|
sandbox.style.visibility = 'hidden'
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
svg = document.createElementNS(NS.SVG, 'svg')
|
2019-11-24 03:29:24 +00:00
|
|
|
// Firefox throws exception in getBBox() when svg is not attached to DOM.
|
2021-12-28 14:02:29 +00:00
|
|
|
sandbox.append(svg)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
// Set up <svg> with nonce.
|
2021-12-28 14:02:29 +00:00
|
|
|
svgN = document.createElementNS(NS.SVG, 'svg')
|
|
|
|
svgN.setAttributeNS(NS.XMLNS, 'xmlns:se', NS.SE)
|
|
|
|
svgN.setAttributeNS(NS.SE, 'se:nonce', NONCE)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const svgContent = document.createElementNS(NS.SVG, 'svg')
|
|
|
|
currentDrawing_ = new draw.Drawing(svgContent, idprefix)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test draw module', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(draw)
|
|
|
|
assert.equal(typeof draw, typeof {})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(draw.Drawing)
|
|
|
|
assert.equal(typeof draw.Drawing, typeof function () { /* empty fn */ })
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test document creation', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
let doc
|
2019-11-24 03:29:24 +00:00
|
|
|
try {
|
2021-12-28 14:02:29 +00:00
|
|
|
doc = new draw.Drawing()
|
|
|
|
assert.ok(false, 'Created drawing without a valid <svg> element')
|
2019-11-24 03:29:24 +00:00
|
|
|
} catch (e) {
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(true)
|
2019-11-24 03:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-12-28 14:02:29 +00:00
|
|
|
doc = new draw.Drawing(svg)
|
|
|
|
assert.ok(doc)
|
|
|
|
assert.equal(typeof doc, typeof {})
|
2019-11-24 03:29:24 +00:00
|
|
|
} catch (e) {
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(false, 'Could not create document from valid <svg> element: ' + e)
|
2019-11-24 03:29:24 +00:00
|
|
|
}
|
2021-12-28 14:02:29 +00:00
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test nonce', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
let doc = new draw.Drawing(svg)
|
|
|
|
assert.equal(doc.getNonce(), '')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
doc = new draw.Drawing(svgN)
|
|
|
|
assert.equal(doc.getNonce(), NONCE)
|
|
|
|
assert.equal(doc.getSvgElem().getAttributeNS(NS.SE, 'nonce'), NONCE)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
doc.clearNonce()
|
|
|
|
assert.ok(!doc.getNonce())
|
|
|
|
assert.ok(!doc.getSvgElem().getAttributeNS(NS.SE, 'se:nonce'))
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
doc.setNonce(NONCE)
|
|
|
|
assert.equal(doc.getNonce(), NONCE)
|
|
|
|
assert.equal(doc.getSvgElem().getAttributeNS(NS.SE, 'nonce'), NONCE)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test getId() and getNextId() without nonce', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const elem2 = document.createElementNS(NS.SVG, 'circle')
|
|
|
|
elem2.id = 'svg_2'
|
|
|
|
svg.append(elem2)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const doc = new draw.Drawing(svg)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getId(), 'svg_0')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getNextId(), 'svg_1')
|
|
|
|
assert.equal(doc.getId(), 'svg_1')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getNextId(), 'svg_3')
|
|
|
|
assert.equal(doc.getId(), 'svg_3')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getNextId(), 'svg_4')
|
|
|
|
assert.equal(doc.getId(), 'svg_4')
|
2019-11-24 03:29:24 +00:00
|
|
|
// clean out svg document
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test getId() and getNextId() with prefix without nonce', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const prefix = 'Bar-'
|
|
|
|
const doc = new draw.Drawing(svg, prefix)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getId(), prefix + '0')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getNextId(), prefix + '1')
|
|
|
|
assert.equal(doc.getId(), prefix + '1')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getNextId(), prefix + '2')
|
|
|
|
assert.equal(doc.getId(), prefix + '2')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getNextId(), prefix + '3')
|
|
|
|
assert.equal(doc.getId(), prefix + '3')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test getId() and getNextId() with nonce', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const prefix = 'svg_' + NONCE
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const elem2 = document.createElementNS(NS.SVG, 'circle')
|
|
|
|
elem2.id = prefix + '_2'
|
|
|
|
svgN.append(elem2)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const doc = new draw.Drawing(svgN)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getId(), prefix + '_0')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getNextId(), prefix + '_1')
|
|
|
|
assert.equal(doc.getId(), prefix + '_1')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getNextId(), prefix + '_3')
|
|
|
|
assert.equal(doc.getId(), prefix + '_3')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getNextId(), prefix + '_4')
|
|
|
|
assert.equal(doc.getId(), prefix + '_4')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svgN)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test getId() and getNextId() with prefix with nonce', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const PREFIX = 'Bar-'
|
|
|
|
const doc = new draw.Drawing(svgN, PREFIX)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const prefix = PREFIX + NONCE + '_'
|
|
|
|
assert.equal(doc.getId(), prefix + '0')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getNextId(), prefix + '1')
|
|
|
|
assert.equal(doc.getId(), prefix + '1')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getNextId(), prefix + '2')
|
|
|
|
assert.equal(doc.getId(), prefix + '2')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(doc.getNextId(), prefix + '3')
|
|
|
|
assert.equal(doc.getId(), prefix + '3')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svgN)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test releaseId()', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const doc = new draw.Drawing(svg)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const firstId = doc.getNextId()
|
|
|
|
/* const secondId = */ doc.getNextId()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const result = doc.releaseId(firstId)
|
|
|
|
assert.ok(result)
|
|
|
|
assert.equal(doc.getNextId(), firstId)
|
|
|
|
assert.equal(doc.getNextId(), 'svg_3')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(!doc.releaseId('bad-id'))
|
|
|
|
assert.ok(doc.releaseId(firstId))
|
|
|
|
assert.ok(!doc.releaseId(firstId))
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test getNumLayers', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
assert.equal(typeof drawing.getNumLayers, typeof function () { /* empty fn */ })
|
|
|
|
assert.equal(drawing.getNumLayers(), 0)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
setupSVGWith3Layers(svg)
|
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(drawing.getNumLayers(), 3)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test hasLayer', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
setupSVGWith3Layers(svg)
|
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(typeof drawing.hasLayer, typeof function () { /* empty fn */ })
|
|
|
|
assert.ok(!drawing.hasLayer('invalid-layer'))
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(drawing.hasLayer(LAYER3))
|
|
|
|
assert.ok(drawing.hasLayer(LAYER2))
|
|
|
|
assert.ok(drawing.hasLayer(LAYER1))
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test identifyLayers() with empty document', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
assert.equal(drawing.getCurrentLayer(), null)
|
2019-11-24 03:29:24 +00:00
|
|
|
// By default, an empty document gets an empty group created.
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
// Check that <svg> element now has one child node
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(drawing.getSvgElem().hasChildNodes())
|
|
|
|
assert.equal(drawing.getSvgElem().childNodes.length, 1)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
// Check that all_layers are correctly set up.
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(drawing.getNumLayers(), 1)
|
|
|
|
const emptyLayer = drawing.all_layers[0]
|
|
|
|
assert.ok(emptyLayer)
|
|
|
|
const layerGroup = emptyLayer.getGroup()
|
|
|
|
assert.equal(layerGroup, drawing.getSvgElem().firstChild)
|
|
|
|
assert.equal(layerGroup.tagName, 'g')
|
|
|
|
assert.equal(layerGroup.getAttribute('class'), LAYER_CLASS)
|
|
|
|
assert.ok(layerGroup.hasChildNodes())
|
|
|
|
assert.equal(layerGroup.childNodes.length, 1)
|
|
|
|
const firstChild = layerGroup.childNodes.item(0)
|
|
|
|
assert.equal(firstChild.tagName, 'title')
|
|
|
|
|
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test identifyLayers() with some layers', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
setupSVGWith3Layers(svg)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(svg.childNodes.length, 3)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(drawing.getNumLayers(), 3)
|
|
|
|
assert.equal(drawing.all_layers[0].getGroup(), svg.childNodes.item(0))
|
|
|
|
assert.equal(drawing.all_layers[1].getGroup(), svg.childNodes.item(1))
|
|
|
|
assert.equal(drawing.all_layers[2].getGroup(), svg.childNodes.item(2))
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(drawing.all_layers[0].getGroup().getAttribute('class'), LAYER_CLASS)
|
|
|
|
assert.equal(drawing.all_layers[1].getGroup().getAttribute('class'), LAYER_CLASS)
|
|
|
|
assert.equal(drawing.all_layers[2].getGroup().getAttribute('class'), LAYER_CLASS)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test identifyLayers() with some layers and orphans', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
setupSVGWith3Layers(svg)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const orphan1 = document.createElementNS(NS.SVG, 'rect')
|
|
|
|
const orphan2 = document.createElementNS(NS.SVG, 'rect')
|
|
|
|
svg.append(orphan1, orphan2)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(svg.childNodes.length, 5)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(drawing.getNumLayers(), 4)
|
|
|
|
assert.equal(drawing.all_layers[0].getGroup(), svg.childNodes.item(0))
|
|
|
|
assert.equal(drawing.all_layers[1].getGroup(), svg.childNodes.item(1))
|
|
|
|
assert.equal(drawing.all_layers[2].getGroup(), svg.childNodes.item(2))
|
|
|
|
assert.equal(drawing.all_layers[3].getGroup(), svg.childNodes.item(3))
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(drawing.all_layers[0].getGroup().getAttribute('class'), LAYER_CLASS)
|
|
|
|
assert.equal(drawing.all_layers[1].getGroup().getAttribute('class'), LAYER_CLASS)
|
|
|
|
assert.equal(drawing.all_layers[2].getGroup().getAttribute('class'), LAYER_CLASS)
|
|
|
|
assert.equal(drawing.all_layers[3].getGroup().getAttribute('class'), LAYER_CLASS)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const layer4 = drawing.all_layers[3].getGroup()
|
|
|
|
assert.equal(layer4.tagName, 'g')
|
|
|
|
assert.equal(layer4.childNodes.length, 3)
|
|
|
|
assert.equal(layer4.childNodes.item(1), orphan1)
|
|
|
|
assert.equal(layer4.childNodes.item(2), orphan2)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test getLayerName()', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
setupSVGWith3Layers(svg)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(drawing.getNumLayers(), 3)
|
|
|
|
assert.equal(drawing.getLayerName(0), LAYER1)
|
|
|
|
assert.equal(drawing.getLayerName(1), LAYER2)
|
|
|
|
assert.equal(drawing.getLayerName(2), LAYER3)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test getCurrentLayer()', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
setupSVGWith3Layers(svg)
|
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(drawing.getCurrentLayer)
|
|
|
|
assert.equal(typeof drawing.getCurrentLayer, typeof function () { /* empty fn */ })
|
|
|
|
assert.ok(drawing.getCurrentLayer())
|
|
|
|
assert.equal(drawing.getCurrentLayer(), drawing.all_layers[2].getGroup())
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test setCurrentLayer() and getCurrentLayerName()', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
setupSVGWith3Layers(svg)
|
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(drawing.setCurrentLayer)
|
|
|
|
assert.equal(typeof drawing.setCurrentLayer, typeof function () { /* empty fn */ })
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing.setCurrentLayer(LAYER2)
|
|
|
|
assert.equal(drawing.getCurrentLayerName(), LAYER2)
|
|
|
|
assert.equal(drawing.getCurrentLayer(), drawing.all_layers[1].getGroup())
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing.setCurrentLayer(LAYER3)
|
|
|
|
assert.equal(drawing.getCurrentLayerName(), LAYER3)
|
|
|
|
assert.equal(drawing.getCurrentLayer(), drawing.all_layers[2].getGroup())
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test setCurrentLayerName()', function () {
|
|
|
|
const mockHrService = {
|
|
|
|
changeElement () {
|
|
|
|
// empty
|
|
|
|
}
|
2021-12-28 14:02:29 +00:00
|
|
|
}
|
|
|
|
addOwnSpies(mockHrService)
|
|
|
|
|
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
setupSVGWith3Layers(svg)
|
|
|
|
drawing.identifyLayers()
|
|
|
|
|
|
|
|
assert.ok(drawing.setCurrentLayerName)
|
|
|
|
assert.equal(typeof drawing.setCurrentLayerName, typeof function () { /* empty fn */ })
|
|
|
|
|
|
|
|
const oldName = drawing.getCurrentLayerName()
|
|
|
|
const newName = 'New Name'
|
|
|
|
assert.ok(drawing.layer_map[oldName])
|
|
|
|
assert.equal(drawing.layer_map[newName], undefined) // newName shouldn't exist.
|
|
|
|
const result = drawing.setCurrentLayerName(newName, mockHrService)
|
|
|
|
assert.equal(result, newName)
|
|
|
|
assert.equal(drawing.getCurrentLayerName(), newName)
|
2019-11-24 03:29:24 +00:00
|
|
|
// Was the map updated?
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(drawing.layer_map[oldName], undefined)
|
|
|
|
assert.equal(drawing.layer_map[newName], drawing.current_layer)
|
2019-11-24 03:29:24 +00:00
|
|
|
// Was mockHrService called?
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(mockHrService.changeElement.calledOnce)
|
|
|
|
assert.equal(oldName, mockHrService.changeElement.getCall(0).args[1]['#text'])
|
|
|
|
assert.equal(newName, mockHrService.changeElement.getCall(0).args[0].textContent)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test createLayer()', function () {
|
|
|
|
const mockHrService = {
|
2020-10-04 21:07:31 +00:00
|
|
|
startBatchCommand () { /* empty fn */ },
|
|
|
|
endBatchCommand () { /* empty fn */ },
|
|
|
|
insertElement () { /* empty fn */ }
|
2021-12-28 14:02:29 +00:00
|
|
|
}
|
|
|
|
addOwnSpies(mockHrService)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
setupSVGWith3Layers(svg)
|
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(drawing.createLayer)
|
|
|
|
assert.equal(typeof drawing.createLayer, typeof function () { /* empty fn */ })
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const NEW_LAYER_NAME = 'Layer A'
|
|
|
|
const layerG = drawing.createLayer(NEW_LAYER_NAME, mockHrService)
|
|
|
|
assert.equal(drawing.getNumLayers(), 4)
|
|
|
|
assert.equal(layerG, drawing.getCurrentLayer())
|
|
|
|
assert.equal(layerG.getAttribute('class'), LAYER_CLASS)
|
|
|
|
assert.equal(NEW_LAYER_NAME, drawing.getCurrentLayerName())
|
|
|
|
assert.equal(NEW_LAYER_NAME, drawing.getLayerName(3))
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(layerG, mockHrService.insertElement.getCall(0).args[0])
|
|
|
|
assert.ok(mockHrService.startBatchCommand.calledOnce)
|
|
|
|
assert.ok(mockHrService.endBatchCommand.calledOnce)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test mergeLayer()', function () {
|
|
|
|
const mockHrService = {
|
2020-10-04 21:07:31 +00:00
|
|
|
startBatchCommand () { /* empty fn */ },
|
|
|
|
endBatchCommand () { /* empty fn */ },
|
|
|
|
moveElement () { /* empty fn */ },
|
|
|
|
removeElement () { /* empty fn */ }
|
2021-12-28 14:02:29 +00:00
|
|
|
}
|
|
|
|
addOwnSpies(mockHrService)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
const layers = setupSVGWith3Layers(svg)
|
|
|
|
const elementCount = createSomeElementsInGroup(layers[2]) + 1 // +1 for title element
|
|
|
|
assert.equal(layers[1].childElementCount, 1)
|
|
|
|
assert.equal(layers[2].childElementCount, elementCount)
|
|
|
|
drawing.identifyLayers()
|
|
|
|
assert.equal(drawing.getCurrentLayer(), layers[2])
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(drawing.mergeLayer)
|
|
|
|
assert.equal(typeof drawing.mergeLayer, typeof function () { /* empty fn */ })
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing.mergeLayer(mockHrService)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(drawing.getNumLayers(), 2)
|
|
|
|
assert.equal(svg.childElementCount, 2)
|
|
|
|
assert.equal(drawing.getCurrentLayer(), layers[1])
|
|
|
|
assert.equal(layers[1].childElementCount, elementCount)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
// check history record
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(mockHrService.startBatchCommand.calledOnce)
|
|
|
|
assert.ok(mockHrService.endBatchCommand.calledOnce)
|
|
|
|
assert.equal(mockHrService.startBatchCommand.getCall(0).args[0], 'Merge Layer')
|
|
|
|
assert.equal(mockHrService.moveElement.callCount, elementCount - 1) // -1 because the title was not moved.
|
|
|
|
assert.equal(mockHrService.removeElement.callCount, 2) // remove group and title.
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test mergeLayer() when no previous layer to merge', function () {
|
|
|
|
const mockHrService = {
|
2020-10-04 21:07:31 +00:00
|
|
|
startBatchCommand () { /* empty fn */ },
|
|
|
|
endBatchCommand () { /* empty fn */ },
|
|
|
|
moveElement () { /* empty fn */ },
|
|
|
|
removeElement () { /* empty fn */ }
|
2021-12-28 14:02:29 +00:00
|
|
|
}
|
|
|
|
addOwnSpies(mockHrService)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
const layers = setupSVGWith3Layers(svg)
|
|
|
|
drawing.identifyLayers()
|
|
|
|
drawing.setCurrentLayer(LAYER1)
|
|
|
|
assert.equal(drawing.getCurrentLayer(), layers[0])
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing.mergeLayer(mockHrService)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(drawing.getNumLayers(), 3)
|
|
|
|
assert.equal(svg.childElementCount, 3)
|
|
|
|
assert.equal(drawing.getCurrentLayer(), layers[0])
|
|
|
|
assert.equal(layers[0].childElementCount, 1)
|
|
|
|
assert.equal(layers[1].childElementCount, 1)
|
|
|
|
assert.equal(layers[2].childElementCount, 1)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
// check history record
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(mockHrService.startBatchCommand.callCount, 0)
|
|
|
|
assert.equal(mockHrService.endBatchCommand.callCount, 0)
|
|
|
|
assert.equal(mockHrService.moveElement.callCount, 0)
|
|
|
|
assert.equal(mockHrService.removeElement.callCount, 0)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test mergeAllLayers()', function () {
|
|
|
|
const mockHrService = {
|
2020-10-04 21:07:31 +00:00
|
|
|
startBatchCommand () { /* empty fn */ },
|
|
|
|
endBatchCommand () { /* empty fn */ },
|
|
|
|
moveElement () { /* empty fn */ },
|
|
|
|
removeElement () { /* empty fn */ }
|
2021-12-28 14:02:29 +00:00
|
|
|
}
|
|
|
|
addOwnSpies(mockHrService)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
const layers = setupSVGWith3Layers(svg)
|
|
|
|
const elementCount = createSomeElementsInGroup(layers[0]) + 1 // +1 for title element
|
|
|
|
createSomeElementsInGroup(layers[1])
|
|
|
|
createSomeElementsInGroup(layers[2])
|
|
|
|
assert.equal(layers[0].childElementCount, elementCount)
|
|
|
|
assert.equal(layers[1].childElementCount, elementCount)
|
|
|
|
assert.equal(layers[2].childElementCount, elementCount)
|
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(drawing.mergeAllLayers)
|
|
|
|
assert.equal(typeof drawing.mergeAllLayers, typeof function () { /* empty fn */ })
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing.mergeAllLayers(mockHrService)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(drawing.getNumLayers(), 1)
|
|
|
|
assert.equal(svg.childElementCount, 1)
|
|
|
|
assert.equal(drawing.getCurrentLayer(), layers[0])
|
|
|
|
assert.equal(layers[0].childElementCount, elementCount * 3 - 2) // -2 because two titles were deleted.
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
// check history record
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(mockHrService.startBatchCommand.callCount, 3) // mergeAllLayers + 2 * mergeLayer
|
|
|
|
assert.equal(mockHrService.endBatchCommand.callCount, 3)
|
|
|
|
assert.equal(mockHrService.startBatchCommand.getCall(0).args[0], 'Merge all Layers')
|
|
|
|
assert.equal(mockHrService.startBatchCommand.getCall(1).args[0], 'Merge Layer')
|
|
|
|
assert.equal(mockHrService.startBatchCommand.getCall(2).args[0], 'Merge Layer')
|
2019-11-24 03:29:24 +00:00
|
|
|
// moveElement count is times 3 instead of 2, because one layer's elements were moved twice.
|
|
|
|
// moveElement count is minus 3 because the three titles were not moved.
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(mockHrService.moveElement.callCount, elementCount * 3 - 3)
|
|
|
|
assert.equal(mockHrService.removeElement.callCount, 2 * 2) // remove group and title twice.
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test cloneLayer()', function () {
|
|
|
|
const mockHrService = {
|
2020-10-04 21:07:31 +00:00
|
|
|
startBatchCommand () { /* empty fn */ },
|
|
|
|
endBatchCommand () { /* empty fn */ },
|
|
|
|
insertElement () { /* empty fn */ }
|
2021-12-28 14:02:29 +00:00
|
|
|
}
|
|
|
|
addOwnSpies(mockHrService)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
const layers = setupSVGWith3Layers(svg)
|
|
|
|
const layer3 = layers[2]
|
|
|
|
const elementCount = createSomeElementsInGroup(layer3) + 1 // +1 for title element
|
|
|
|
assert.equal(layer3.childElementCount, elementCount)
|
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(drawing.cloneLayer)
|
|
|
|
assert.equal(typeof drawing.cloneLayer, typeof function () { /* empty fn */ })
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const clone = drawing.cloneLayer('clone', mockHrService)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(drawing.getNumLayers(), 4)
|
|
|
|
assert.equal(svg.childElementCount, 4)
|
|
|
|
assert.equal(drawing.getCurrentLayer(), clone)
|
|
|
|
assert.equal(clone.childElementCount, elementCount)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
// check history record
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(mockHrService.startBatchCommand.calledOnce) // mergeAllLayers + 2 * mergeLayer
|
|
|
|
assert.ok(mockHrService.endBatchCommand.calledOnce)
|
|
|
|
assert.equal(mockHrService.startBatchCommand.getCall(0).args[0], 'Duplicate Layer')
|
|
|
|
assert.equal(mockHrService.insertElement.callCount, 1)
|
|
|
|
assert.equal(mockHrService.insertElement.getCall(0).args[0], clone)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
// check that path is cloned properly
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(clone.childNodes.length, elementCount)
|
|
|
|
const path = clone.childNodes[1]
|
|
|
|
assert.equal(path.id, 'svg_1')
|
|
|
|
assert.equal(path.getAttribute('d'), PATH_ATTR.d)
|
|
|
|
assert.equal(path.getAttribute('transform'), PATH_ATTR.transform)
|
|
|
|
assert.equal(path.getAttribute('fill'), PATH_ATTR.fill)
|
|
|
|
assert.equal(path.getAttribute('stroke'), PATH_ATTR.stroke)
|
|
|
|
assert.equal(path.getAttribute('stroke-width'), PATH_ATTR['stroke-width'])
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
// check that g is cloned properly
|
2021-12-28 14:02:29 +00:00
|
|
|
const g = clone.childNodes[4]
|
|
|
|
assert.equal(g.childNodes.length, 1)
|
|
|
|
assert.equal(g.id, 'svg_4')
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test getLayerVisibility()', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
setupSVGWith3Layers(svg)
|
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(drawing.getLayerVisibility)
|
|
|
|
assert.equal(typeof drawing.getLayerVisibility, typeof function () { /* empty fn */ })
|
|
|
|
assert.ok(drawing.getLayerVisibility(LAYER1))
|
|
|
|
assert.ok(drawing.getLayerVisibility(LAYER2))
|
|
|
|
assert.ok(drawing.getLayerVisibility(LAYER3))
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test setLayerVisibility()', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
setupSVGWith3Layers(svg)
|
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(drawing.setLayerVisibility)
|
|
|
|
assert.equal(typeof drawing.setLayerVisibility, typeof function () { /* empty fn */ })
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing.setLayerVisibility(LAYER3, false)
|
|
|
|
drawing.setLayerVisibility(LAYER2, true)
|
|
|
|
drawing.setLayerVisibility(LAYER1, false)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(!drawing.getLayerVisibility(LAYER1))
|
|
|
|
assert.ok(drawing.getLayerVisibility(LAYER2))
|
|
|
|
assert.ok(!drawing.getLayerVisibility(LAYER3))
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing.setLayerVisibility(LAYER3, 'test-string')
|
|
|
|
assert.ok(!drawing.getLayerVisibility(LAYER3))
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test getLayerOpacity()', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
setupSVGWith3Layers(svg)
|
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(drawing.getLayerOpacity)
|
|
|
|
assert.equal(typeof drawing.getLayerOpacity, typeof function () { /* empty fn */ })
|
|
|
|
assert.strictEqual(drawing.getLayerOpacity(LAYER1), 1.0)
|
|
|
|
assert.strictEqual(drawing.getLayerOpacity(LAYER2), 1.0)
|
|
|
|
assert.strictEqual(drawing.getLayerOpacity(LAYER3), 1.0)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test setLayerOpacity()', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
setupSVGWith3Layers(svg)
|
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.ok(drawing.setLayerOpacity)
|
|
|
|
assert.equal(typeof drawing.setLayerOpacity, typeof function () { /* empty fn */ })
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing.setLayerOpacity(LAYER1, 0.4)
|
|
|
|
drawing.setLayerOpacity(LAYER2, 'invalid-string')
|
|
|
|
drawing.setLayerOpacity(LAYER3, -1.4)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.strictEqual(drawing.getLayerOpacity(LAYER1), 0.4)
|
|
|
|
assert.strictEqual(drawing.getLayerOpacity(LAYER2), 1.0)
|
|
|
|
assert.strictEqual(drawing.getLayerOpacity(LAYER3), 1.0)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing.setLayerOpacity(LAYER3, 100)
|
|
|
|
assert.strictEqual(drawing.getLayerOpacity(LAYER3), 1.0)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
cleanupSVG(svg)
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test deleteCurrentLayer()', function () {
|
2021-12-28 14:02:29 +00:00
|
|
|
const drawing = new draw.Drawing(svg)
|
|
|
|
setupSVGWith3Layers(svg)
|
|
|
|
drawing.identifyLayers()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing.setCurrentLayer(LAYER2)
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
const curLayer = drawing.getCurrentLayer()
|
|
|
|
assert.equal(curLayer, drawing.all_layers[1].getGroup())
|
|
|
|
const deletedLayer = drawing.deleteCurrentLayer()
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
assert.equal(curLayer, deletedLayer)
|
|
|
|
assert.equal(drawing.getNumLayers(), 2)
|
|
|
|
assert.equal(LAYER1, drawing.all_layers[0].getName())
|
|
|
|
assert.equal(LAYER3, drawing.all_layers[1].getName())
|
|
|
|
assert.equal(drawing.getCurrentLayer(), drawing.all_layers[1].getGroup())
|
|
|
|
})
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
it('Test svgedit.draw.randomizeIds()', function () {
|
|
|
|
// Confirm in LET_DOCUMENT_DECIDE mode that the document decides
|
|
|
|
// if there is a nonce.
|
2021-12-28 14:02:29 +00:00
|
|
|
let drawing = new draw.Drawing(svgN.cloneNode(true))
|
|
|
|
assert.ok(drawing.getNonce())
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing = new draw.Drawing(svg.cloneNode(true))
|
|
|
|
assert.ok(!drawing.getNonce())
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
// Confirm that a nonce is set once we're in ALWAYS_RANDOMIZE mode.
|
2021-12-28 14:02:29 +00:00
|
|
|
draw.randomizeIds(true, drawing)
|
|
|
|
assert.ok(drawing.getNonce())
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
// Confirm new drawings in ALWAYS_RANDOMIZE mode have a nonce.
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing = new draw.Drawing(svg.cloneNode(true))
|
|
|
|
assert.ok(drawing.getNonce())
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing.clearNonce()
|
|
|
|
assert.ok(!drawing.getNonce())
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
// Confirm new drawings in NEVER_RANDOMIZE mode do not have a nonce
|
|
|
|
// but that their se:nonce attribute is left alone.
|
2021-12-28 14:02:29 +00:00
|
|
|
draw.randomizeIds(false, drawing)
|
|
|
|
assert.ok(!drawing.getNonce())
|
|
|
|
assert.ok(drawing.getSvgElem().getAttributeNS(NS.SE, 'nonce'))
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing = new draw.Drawing(svg.cloneNode(true))
|
|
|
|
assert.ok(!drawing.getNonce())
|
2019-11-24 03:29:24 +00:00
|
|
|
|
2021-12-28 14:02:29 +00:00
|
|
|
drawing = new draw.Drawing(svgN.cloneNode(true))
|
|
|
|
assert.ok(!drawing.getNonce())
|
|
|
|
})
|
|
|
|
})
|