Merge pull request #4990 from fredj/getTileJSON

Add new ol.source.TileJSON#getTileJSON function
This commit is contained in:
Frédéric Junod
2016-03-09 11:44:35 +01:00
2 changed files with 47 additions and 1 deletions
+16 -1
View File
@@ -29,6 +29,12 @@ goog.require('ol.source.TileImage');
*/ */
ol.source.TileJSON = function(options) { ol.source.TileJSON = function(options) {
/**
* @type {TileJSON}
* @private
*/
this.tileJSON_ = null;
goog.base(this, { goog.base(this, {
attributions: options.attributions, attributions: options.attributions,
cacheSize: options.cacheSize, cacheSize: options.cacheSize,
@@ -85,6 +91,15 @@ ol.source.TileJSON.prototype.onXHRError_ = function(event) {
}; };
/**
* @return {TileJSON} The tilejson object.
* @api
*/
ol.source.TileJSON.prototype.getTileJSON = function() {
return this.tileJSON_;
};
/** /**
* @protected * @protected
* @param {TileJSON} tileJSON Tile JSON. * @param {TileJSON} tileJSON Tile JSON.
@@ -134,7 +149,7 @@ ol.source.TileJSON.prototype.handleTileJSONResponse = function(tileJSON) {
}) })
]); ]);
} }
this.tileJSON_ = tileJSON;
this.setState(ol.source.State.READY); this.setState(ol.source.State.READY);
}; };
@@ -3,6 +3,33 @@ goog.provide('ol.test.source.TileJSON');
describe('ol.source.TileJSON', function() { describe('ol.source.TileJSON', function() {
describe('constructor', function() {
it('returns a tileJSON source', function() {
var source = new ol.source.TileJSON({
url: 'spec/ol/data/tilejson.json'
});
expect(source).to.be.a(ol.source.Source);
expect(source).to.be.a(ol.source.TileJSON);
});
});
describe('#getTileJSON', function() {
it('parses the tilejson file', function() {
var source = new ol.source.TileJSON({
url: 'spec/ol/data/tilejson.json'
});
source.on('change', function() {
if (source.getState() === 'ready') {
var tileJSON = source.getTileJSON();
expect(tileJSON.name).to.eql('Geography Class');
expect(tileJSON.version).to.eql('1.0.0');
}
});
});
});
describe('#getState', function() { describe('#getState', function() {
it('returns ol.source.State.ERROR on HTTP 404', function() { it('returns ol.source.State.ERROR on HTTP 404', function() {
@@ -11,6 +38,7 @@ describe('ol.source.TileJSON', function() {
}); });
source.on('change', function() { source.on('change', function() {
expect(source.getState()).to.eql('error'); expect(source.getState()).to.eql('error');
expect(source.getTileJSON()).to.eql(null);
}); });
}); });
@@ -20,6 +48,7 @@ describe('ol.source.TileJSON', function() {
}); });
source.on('change', function() { source.on('change', function() {
expect(source.getState()).to.eql('error'); expect(source.getState()).to.eql('error');
expect(source.getTileJSON()).to.eql(null);
}); });
}); });
@@ -29,6 +58,7 @@ describe('ol.source.TileJSON', function() {
}); });
source.on('change', function() { source.on('change', function() {
expect(source.getState()).to.eql('error'); expect(source.getState()).to.eql('error');
expect(source.getTileJSON()).to.eql(null);
}); });
}); });
@@ -93,5 +123,6 @@ describe('ol.source.TileJSON', function() {
goog.require('ol.events'); goog.require('ol.events');
goog.require('ol.source.State'); goog.require('ol.source.State');
goog.require('ol.source.Source');
goog.require('ol.source.TileJSON'); goog.require('ol.source.TileJSON');
goog.require('ol.Observable'); goog.require('ol.Observable');