Skip to content

Commit

Permalink
change MAX_QUADS from a prototype property to a constant property, us…
Browse files Browse the repository at this point in the history
…e sinon.spy for testing the warning
  • Loading branch information
Molly Lloyd committed Aug 14, 2016
1 parent f3cb65b commit 9534060
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 53 deletions.
10 changes: 6 additions & 4 deletions js/data/bucket/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ function SymbolBucket(options) {
this.adjustedIconSize = options.adjustedIconSize;
this.fontstack = options.fontstack;

// this constant is based on the size of the glyphQuadEndIndex and iconQuadEndIndex
// in the symbol_instances StructArrayType
// eg the max valid UInt16 is 65,535
this.MAX_QUADS = 65535;

}

// this constant is based on the size of the glyphQuadEndIndex and iconQuadEndIndex
// in the symbol_instances StructArrayType
// eg the max valid UInt16 is 65,535
SymbolBucket.MAX_QUADS = 65535;

SymbolBucket.prototype = util.inherit(Bucket, {});

SymbolBucket.prototype.serialize = function() {
Expand Down
97 changes: 48 additions & 49 deletions test/js/data/symbol_bucket.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var test = require('tap').test;
var sinon = require('sinon');
var fs = require('fs');
var path = require('path');
var Protobuf = require('pbf');
Expand All @@ -12,54 +13,55 @@ var SymbolInstancesArray = require('../../../js/symbol/symbol_instances');
var SymbolQuadsArray = require('../../../js/symbol/symbol_quads');
var GlyphAtlas = require('../../../js/symbol/glyph_atlas');
var StyleLayer = require('../../../js/style/style_layer');
var util = require('../../../js/util/util');

// Load a point feature from fixture tile.
var vt = new VectorTile(new Protobuf(new Uint8Array(fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf')))));
var feature = vt.layers.place_label.feature(10);
var glyphs = JSON.parse(fs.readFileSync(path.join(__dirname, '/../../fixtures/fontstack-glyphs.json')));

test('SymbolBucket', function(t) {
/*eslint new-cap: 0*/
var buffers = {};
var collisionBoxArray = new CollisionBoxArray();
var symbolQuadsArray = new SymbolQuadsArray();
var symbolInstancesArray = new SymbolInstancesArray();
var collision = new Collision(0, 0, collisionBoxArray);
var atlas = new GlyphAtlas();
for (var id in glyphs) {
glyphs[id].bitmap = true;
glyphs[id].rect = atlas.addGlyph(id, 'Test', glyphs[id], 3);
}
/*eslint new-cap: 0*/
var buffers = {};
var collisionBoxArray = new CollisionBoxArray();
var symbolQuadsArray = new SymbolQuadsArray();
var symbolInstancesArray = new SymbolInstancesArray();
var collision = new Collision(0, 0, collisionBoxArray);
var atlas = new GlyphAtlas();
for (var id in glyphs) {
glyphs[id].bitmap = true;
glyphs[id].rect = atlas.addGlyph(id, 'Test', glyphs[id], 3);
}

var stacks = { 'Test': glyphs };

var stacks = { 'Test': glyphs };
function bucketSetup() {
var layer = new StyleLayer({
id: 'test',
type: 'symbol',
layout: { 'text-font': ['Test'] }
});

function bucketSetup() {
var layer = new StyleLayer({
id: 'test',
type: 'symbol',
layout: { 'text-font': ['Test'] }
});
var bucket = new SymbolBucket({
buffers: buffers,
overscaling: 1,
zoom: 0,
collisionBoxArray: collisionBoxArray,
symbolInstancesArray: symbolInstancesArray,
symbolQuadsArray: symbolQuadsArray,
layer: layer,
childLayers: [layer],
tileExtent: 4096
});
bucket.createArrays();
bucket.textFeatures = ['abcde'];
bucket.features = [feature];
return bucket;
}

var bucket = new SymbolBucket({
buffers: buffers,
overscaling: 1,
zoom: 0,
collisionBoxArray: collisionBoxArray,
symbolInstancesArray: symbolInstancesArray,
symbolQuadsArray: symbolQuadsArray,
layer: layer,
childLayers: [layer],
tileExtent: 4096
});
bucket.createArrays();
bucket.textFeatures = ['abcde'];
bucket.features = [feature];
return bucket;
}

test('SymbolBucket', function(t) {
var bucketA = bucketSetup();
var bucketB = bucketSetup();
var bucketC = bucketSetup();

// add feature from bucket A
var a = collision.grid.keys.length;
Expand All @@ -72,20 +74,17 @@ test('SymbolBucket', function(t) {
t.equal(bucketB.populateArrays(collision, stacks), undefined);
var b2 = collision.grid.keys.length;
t.equal(a2, b2, 'detects collision and does not place feature');
t.end();
});


// Use a custom console.warn to count warnings
var numWarnings = 0;
var warn = console.warn;
console.warn = function(warning) {
if (warning.includes("Too many symbols being rendered in a tile.") || warning.includes("Too many glyphs being rendered in a tile.")) {
numWarnings++;
}
};
test('SymbolBucket integer overflow', function(t) {
var bucket = bucketSetup();
var spy = sinon.spy(util, 'warnOnce');

bucketC.MAX_QUADS = 14;
t.equal(bucketC.populateArrays(collision, stacks), undefined);
t.equal(numWarnings, 2, 'integer overflow warning is triggered when glyph and/or symbol quad exceeds MAX_QUADS');
// Put it back
console.warn = warn;
bucket.MAX_QUADS = 5;
bucket.populateArrays(collision, stacks);
t.ok(spy.calledTwice, 'warning is triggered when glyph and/or symbol quad exceeds MAX_QUADS');
t.end();
});

0 comments on commit 9534060

Please sign in to comment.