Skip to content

Commit

Permalink
Tests for UTFGrid layer and control.
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed Mar 4, 2012
1 parent dc4df92 commit 268b842
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/OpenLayers/Control/UTFGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ OpenLayers.Control.UTFGrid = OpenLayers.Class(OpenLayers.Control, {
var layer, idx;
for (var i=0, len=layers.length; i<len; i++) {
layer = layers[i];
idx = this.map.layers.indexOf(layer);
idx = OpenLayers.Util.indexOf(this.map.layers, layer);
infoLookup[idx] = layer.getFeatureInfo(lonLat);
}
this.callback(infoLookup); // perhaps pass tile, lonLat?
Expand Down
118 changes: 118 additions & 0 deletions tests/Control/UTFGrid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script>
/**
* Because browsers that implement requestAnimationFrame may not execute
* animation functions while a window is not displayed (e.g. in a hidden
* iframe as in these tests), we mask the native implementations here. The
* native requestAnimationFrame functionality is tested in Util.html and
* in PanZoom.html (where a popup is opened before panning). The panTo tests
* here will test the fallback setTimeout implementation for animation.
*/
window.requestAnimationFrame =
window.webkitRequestAnimationFrame =
window.mozRequestAnimationFrame =
window.oRequestAnimationFrame =
window.msRequestAnimationFrame = null;
</script>
<script src="../OLLoader.js"></script>
<script type="text/javascript">

var map, layer, control;
var log;
function setUp() {
layer = new OpenLayers.Layer.UTFGrid({
url: "../data/utfgrid/world_utfgrid/${z}/${x}/${y}.json",
isBaseLayer: true,
utfgridResolution: 4
});
map = new OpenLayers.Map({
div: "map",
projection: "EPSG:900913",
layers: [layer],
center: [0, 0],
zoom: 1
});
log = [];
control = new OpenLayers.Control.UTFGrid({
callback: function(infoLookup) {
log.push(infoLookup);
}
});
map.addControl(control);
}

function tearDown() {
map.destroy();
map = null;
layer = null;
control = null;
log = [];
}

function test_constructor(t) {
t.plan(2);

var control = new OpenLayers.Control.UTFGrid();
t.ok(control instanceof OpenLayers.Control.UTFGrid, "utfgrid instance");
t.eq(control.handlerMode, "click", "control mode");

control.destroy();

}

function test_handleEvent(t) {
setUp();

var cases = [{
evt: {xy: {x: 100, y: 70}},
arg: {
"0": {
id: "207",
data: {
NAME: "United States",
POP2005: 299846449
}
}
}
}, {
evt: {xy: {x: 350, y: 20}},
arg: {
"0": {
id: "245",
data: {
NAME: "Russia",
POP2005: 143953092
}
}
}
}];

var len = cases.length;
t.plan(3*len);

// wait for tile loading to finish
t.delay_call(0.5, function() {
var c, arg;
for (var i=0; i<len; ++i) {
c = cases[i];
t.eq(log.length, i, i + ": log length before");
control.handleEvent(c.evt);
t.eq(log.length, i+1, i + ": log length after");
t.eq(log[i], c.arg, i + ": callback arg");
}

tearDown();
});

}

</script>
</head>
<body>
<div id="map" style="height: 256px; width: 512px"></div>
</body>
</html>

115 changes: 115 additions & 0 deletions tests/Layer/UTFGrid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script>
/**
* Because browsers that implement requestAnimationFrame may not execute
* animation functions while a window is not displayed (e.g. in a hidden
* iframe as in these tests), we mask the native implementations here. The
* native requestAnimationFrame functionality is tested in Util.html and
* in PanZoom.html (where a popup is opened before panning). The panTo tests
* here will test the fallback setTimeout implementation for animation.
*/
window.requestAnimationFrame =
window.webkitRequestAnimationFrame =
window.mozRequestAnimationFrame =
window.oRequestAnimationFrame =
window.msRequestAnimationFrame = null;
</script>
<script src="../OLLoader.js"></script>
<script type="text/javascript">

var map, layer;
function setUp() {
layer = new OpenLayers.Layer.UTFGrid({
url: "../data/utfgrid/world_utfgrid/${z}/${x}/${y}.json",
isBaseLayer: true,
utfgridResolution: 4
});
map = new OpenLayers.Map({
div: "map",
projection: "EPSG:900913",
layers: [layer],
center: [0, 0],
zoom: 1
});
}

function tearDown() {
map.destroy();
map = null;
layer = null;
}

function test_constructor(t) {
t.plan(4);

var layer = new OpenLayers.Layer.UTFGrid({
name: "foo",
url: "path/to/tiles/${z}/${x}/${y}",
utfgridResolution: 8
});
t.ok(layer instanceof OpenLayers.Layer.UTFGrid, "utfgrid instance");
t.eq(layer.name, "foo", "layer name");
t.eq(layer.url, "path/to/tiles/${z}/${x}/${y}", "layer url");
t.eq(layer.utfgridResolution, 8, "layer utfgridResolution");

layer.destroy();

}

function test_clone(t) {
t.plan(3);
setUp();

var clone = layer.clone();
t.ok(layer instanceof OpenLayers.Layer.UTFGrid, "utfgrid instance");
t.eq(layer.url, "../data/utfgrid/world_utfgrid/${z}/${x}/${y}.json", "layer url");
t.eq(layer.utfgridResolution, 4, "layer utfgridResolution");
clone.destroy();

tearDown();
}

function test_getFeatureInfo(t) {
t.plan(2);
setUp();

// wait for tile loading to finish
t.delay_call(0.5, function() {
var loc = new OpenLayers.LonLat(-110, 45).transform("EPSG:4326", "EPSG:900913");
var info = layer.getFeatureInfo(loc);

t.eq(info.id, "207", "feature id");
t.eq(info.data, {POP2005: 299846449, NAME: "United States"}, "feature data");

tearDown();
});

}

function test_getFeatureId(t) {
t.plan(2);
setUp();

// wait for tile loading to finish
t.delay_call(0.5, function() {
var ca = new OpenLayers.LonLat(-110, 55).transform("EPSG:4326", "EPSG:900913");
var ru = new OpenLayers.LonLat(90, 75).transform("EPSG:4326", "EPSG:900913");

t.eq(layer.getFeatureId(ca), "24", "feature id for ca");
t.eq(layer.getFeatureId(ru), "245", "feature id for ru");

tearDown();
});

}

</script>
</head>
<body>
<div id="map" style="height: 256px; width: 512px"></div>
</body>
</html>

14 changes: 10 additions & 4 deletions tests/Tile/UTFGrid.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@
tile.parseData('{"foo": "bar"}');
t.eq(tile.json, {foo: "bar"}, "valid json parsed");

var err;
var err, obj;
try {
tile.parseData('foo bar');
obj = tile.parseData('foo bar');
} catch (e) {
err = e;
}
t.ok(err instanceof Error, "throws on invalid json");
// The JSON format doesn't actually throw on IE6, so we also check
// for undefined here.
t.ok(err instanceof Error || obj === undefined, "throws on invalid json");

tearDown();
}
Expand Down Expand Up @@ -215,7 +217,11 @@
});
}

function test_getFeatureId_demo(t) {
// While I dislike committing tests that aren't run, I'd like to make an
// exception here. This test (or something like it) should pass. When
// https://github.com/mapbox/utfgrid-spec/issues/1 is resolved, we should
// either modify this or update demo.json and enable the test.
function xtest_getFeatureId_demo(t) {
/**
* The UTFGrid 1.2 spec (https://github.com/mapbox/utfgrid-spec/blob/master/1.2/utfgrid.md)
* links to a demo.json to be used for testing implementations. This
Expand Down
2 changes: 2 additions & 0 deletions tests/list-tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<li>Control/Split.html</li>
<li>Control/TouchNavigation.html</li>
<li>Control/TransformFeature.html</li>
<li>Control/UTFGrid.html</li>
<li>Control/WMSGetFeatureInfo.html</li>
<li>Control/WMTSGetFeatureInfo.html</li>
<li>Control/PanPanel.html</li>
Expand Down Expand Up @@ -164,6 +165,7 @@
<li>Layer/Text.html</li>
<li>Layer/TileCache.html</li>
<li>Layer/TMS.html</li>
<li>Layer/UTFGrid.html</li>
<li>Layer/Vector.html</li>
<li>Layer/Vector/RootContainer.html</li>
<li>Layer/WMS.html</li>
Expand Down

0 comments on commit 268b842

Please sign in to comment.