1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_constructor(t) {
t.plan(11);
var a = new OpenLayers.Protocol.Script({
url: "foo"
});
// 7 tests
t.eq(a.url, "foo", "constructor sets url");
t.eq(a.options.url, a.url, "constructor copies url to options.url");
t.eq(a.params, {}, "constructor sets params");
t.eq(a.options.params, undefined, "constructor does not copy params to options.params");
t.ok(a.format instanceof OpenLayers.Format.GeoJSON,
"constructor sets a GeoJSON format by default");
t.eq(a.callbackKey, 'callback',
"callbackKey is set to 'callback' by default");
t.eq(a.callbackPrefix, '',
"callbackPrefix is set to '' by default");
var params = {hello: "world"};
var b = new OpenLayers.Protocol.Script({
url: "bar",
params: params,
callbackKey: 'cb_key',
callbackPrefix: 'cb_prefix'
});
// 6 tests
t.eq(b.params, params, "constructor sets params");
t.eq(b.options.params, b.params, "constructor copies params to options.params");
t.eq(b.callbackKey, 'cb_key',
"callbackKey is set to 'cb_key'");
t.eq(b.callbackPrefix, 'cb_prefix',
"callbackPrefix is set to 'cb_prefix'");
}
function test_destroy(t) {
t.plan(3);
var aborted = false;
var protocol = new OpenLayers.Protocol.Script({
url: "bar",
params: {hello: "world"},
abort: function() {
aborted = true;
}
});
protocol.destroy();
t.ok(aborted, "destroy aborts request");
t.eq(protocol.params, null, "destroy nullifies params");
t.eq(protocol.format, null, "destroy nullifies format");
}
function test_read(t) {
t.plan(5);
var protocol = new OpenLayers.Protocol.Script({
'url': 'foo_url',
'params': {'k': 'foo_param'}
});
// fake XHR request object
var request = {'status': 200};
// options to pass to read
var readOptions = {
'url': 'bar_url',
'params': {'k': 'bar_param'}
};
var response;
protocol.createRequest = function(url, params, callback) {
// 4 tests
t.ok(this == protocol,
'createRequest called with correct scope');
t.ok(url == readOptions.url,
'createRequest called with correct url');
t.ok(params == readOptions.params,
'createRequest called with correct params');
t.ok(callback instanceof Function,
'createRequest called with a function as callback');
return 'foo_request';
};
var resp = protocol.read(readOptions);
t.eq(resp.priv, 'foo_request',
'response priv property set to what the createRequest method returns');
}
function test_read_bbox(t) {
t.plan(6);
var _createRequest = OpenLayers.Protocol.Script.prototype.createRequest;
var bounds = new OpenLayers.Bounds(1, 2, 3, 4);
var filter = new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.BBOX,
value: bounds,
projection: new OpenLayers.Projection("foo")
});
// log requests
var log, exp;
OpenLayers.Protocol.Script.prototype.createRequest = function(url, params,
callback) {
log.push(params.bbox);
return null;
};
// 1) issue request with default protocol
log = [];
new OpenLayers.Protocol.Script().read({filter: filter});
t.eq(log.length, 1, "1) createRequest called once");
t.ok(log[0] instanceof Array, "1) bbox param is array");
exp = bounds.toArray();
t.eq(log[0], exp, "1) bbox param doesn't include SRS id by default");
// 2) issue request with default protocol
log = [];
new OpenLayers.Protocol.Script({srsInBBOX: true}).read({filter: filter});
t.eq(log.length, 1, "2) createRequest called once");
t.ok(log[0] instanceof Array, "2) bbox param is array");
exp = bounds.toArray();
exp.push("foo");
t.eq(log[0], exp, "2) bbox param includes SRS id if srsInBBOX is true");
OpenLayers.Protocol.Script.prototype.createRequest = _createRequest;
}
function test_createRequest(t) {
t.plan(6);
var protocol = new OpenLayers.Protocol.Script({
callbackKey: 'cb_key',
callbackPrefix: 'cb_prefix:'
});
var _register = OpenLayers.Protocol.Script.register;
OpenLayers.Protocol.Script.register = function() {
return 'bar';
};
var script = protocol.createRequest('http://bar_url/', {'k': 'bar_param'}, 'bar_callback');
t.eq(script.type, 'text/javascript',
'created script has a correct type');
var params = OpenLayers.Util.getParameters(script.src);
t.eq(params.k, "bar_param", "custom query string param");
t.eq(params.cb_key, "cb_prefix:OpenLayers.Protocol.Script.registry.bar", "callback with prefix");
t.eq(script.id, 'OpenLayers_Protocol_Script_bar',
'created script has a correct id');
protocol.callbackTemplate = "customCallback(${id})";
script = protocol.createRequest('http://bar_url/', {'k': 'bar_param2'}, 'bar_callback');
params = OpenLayers.Util.getParameters(script.src);
t.eq(params.k, "bar_param2", "custom query string param");
t.eq(params.cb_key, "cb_prefix:customCallback(bar)", "custom callback with prefix");
OpenLayers.Protocol.Script.register = _register;
}
function test_destroyRequest(t) {
t.plan(2);
var protocol = new OpenLayers.Protocol.Script({});
var _unregister = OpenLayers.Protocol.Script.unregister;
OpenLayers.Protocol.Script.unregister = function(id) {
t.eq(id, 'foo', "destroyRequest calls unregister with correct id");
};
var script = {
id: 'script_foo'
};
protocol.destroyRequest(script);
t.eq(protocol.pendingRequests[script.id], null,
"destroyRequest nullifies the pending request");
OpenLayers.Protocol.Script.unregister = _unregister;
}
function test_handleResponse(t) {
t.plan(8);
var protocol = new OpenLayers.Protocol.Script();
// 2 tests (should be called only twive)
protocol.destroyRequest = function(priv) {
t.eq(priv, 'foo_priv', 'destroyRequest called with correct argument');
}
// 1 test (should be called only once)
protocol.parseFeatures = function(data) {
t.eq(data, 'foo_data', 'parseFeatures called with correct argument');
return 'foo_features';
}
var response = {
priv: 'foo_priv',
data: 'foo_data'
}
var options = {
// 2 tests (should be called twice)
scope: 'foo_scope',
callback: function(resp) {
t.eq(this, 'foo_scope', 'callback called with correct scope');
}
}
protocol.handleResponse(response, options);
// 2 tests
t.eq(response.code, OpenLayers.Protocol.Response.SUCCESS,
'response code correctly set');
t.eq(response.features, 'foo_features',
'response features takes a correct value');
response = {
priv: 'foo_priv'
}
protocol.handleResponse(response, options);
// 1 test
t.eq(response.code, OpenLayers.Protocol.Response.FAILURE,
'response code correctly set');
}
function test_parseFeatures(t) {
t.plan(1);
var protocol = new OpenLayers.Protocol.Script();
protocol.format = {
'read': function(data) {
t.ok(true, 'format.read called');
}
};
var ret = protocol.parseFeatures({foo: 'bar'});
}
function test_abort(t) {
t.plan(2);
var protocol = new OpenLayers.Protocol.Script();
// 1 test
protocol.destroyRequest = function(priv) {
t.eq(priv, 'foo_priv', 'destroyRequest called with correct argument');
}
var response = {
priv: 'foo_priv'
}
protocol.abort(response);
var calls = [];
protocol.pendingRequests = {
'foo': 'foo_request',
'bar': 'bar_request'
}
protocol.destroyRequest = function(priv) {
calls.push(priv);
}
protocol.abort();
// 1 test
t.eq(calls, ['foo_request', 'bar_request'],
'destroyRequest called for each pending requests');
}
</script>
</head>
<body>
</body>
</html>
|