File: Class.html

package info (click to toggle)
openlayers 2.11%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 60,144 kB
  • ctags: 10,906
  • sloc: xml: 7,435; python: 778; sh: 68; makefile: 30
file content (480 lines) | stat: -rw-r--r-- 15,352 bytes parent folder | download
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    // remove this next line at 3.0
    var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);

    function test_Class(t) {
        t.plan(1);
        var MyClass = OpenLayers.Class({
            initialize: function () {
                t.ok(false, "initialize should not be called");
            }
        });
        t.ok(true,
             "defining a class does not call the constructor for the class");
    }

    function test_Class_constructor(t) {
        t.plan(7);
        
        var MyClass = OpenLayers.Class({
            prop: null,
            classProp: {'bad': 'practice'},
            initialize: function(a1, a2) {
                this.prop = "instance property";
                t.ok(true,
                     "initialize is called when a new instance is created");
                t.eq(a1, arg1,
                     "initialize is called with the proper first argument");
                t.eq(a2, arg2,
                     "initialize is called with the proper second argument");
            },
            CLASS_NAME: "MyClass"
        });

        var arg1 = "anArg";
        var arg2 = {"another": "arg"};
        var myObj = new MyClass(arg1, arg2);
        t.eq(MyClass.prop, null,
             "creating a new instance doesn't modify the class");
        t.eq(myObj.prop, "instance property",
             "the new instance is assigned a property in the constructor");
        t.eq(myObj["CLASS_NAME"], "MyClass",
             "the new object is an instance of MyClass");

        // allow for modification of class properties
        MyClass.prototype.classProp.bad = "good";
        t.eq(myObj.classProp.bad, "good",
             "modifying a class property modifies properties of the instance");
    }

    function test_Class_inheritance(t) {
        t.plan(7);
        
        var BaseClass = OpenLayers.Class({
            prop: "base",
            initialize: function() {
                t.ok(false,
                     "base class constructor is not called during inheritance");
            },
            toString: function() {
                return "toString inherited";
            },
            CLASS_NAME: "BaseClass"
        });
        
        var ChildClass = OpenLayers.Class(BaseClass, {
            initialize: function() {
                t.ok(true,
                     "child class constructor is called in creating an instance");
            },
            CLASS_NAME: "ChildClass"
        });
        
        var child = new ChildClass();
        t.eq(child.prop, "base",
             "instance of child inherits properties from base");
        t.eq(child.toString(), "toString inherited",
             "instance of child inherits toString method from base");
        t.eq(child["CLASS_NAME"],
             "ChildClass",
             "new object is an instance of the child class");
        
        var F = OpenLayers.Class(Object, {});
        t.ok(!("initialize" in Object.prototype), "no messing with non OL prototypes");

        // test with an abstract class (i.e. a class that doesn't have an initialize
        // method) as the parent class
        var Vehicule = OpenLayers.Class({
            numWheels: null
        });
        var Bike = OpenLayers.Class(Vehicule, {
            initialize: function() {
                this.numWheels = 2;
            }
        });
        var b = new Bike();
        t.ok(b instanceof Vehicule, "a bike is a vehicule");
        
        // test inheritance with something that has a non-function initialize property
        var P = OpenLayers.Class({
            initialize: "foo"
        });
        var C = OpenLayers.Class(P, {
            initialize: function() {
                // pass
            }
        });
        var c = new C();
        t.eq(P.prototype.initialize, "foo", "Class restores custom initialize property.");
        
    }
    
    function test_Class_multiple_inheritance(t) {
        t.plan(7);
        var BaseClass1 = OpenLayers.Class({
            override: "base1",
            prop: "base1",
            variable: null,
            initialize: function() {
                t.ok(true,
                     "only called when an instance of this class is created");
            },
            CLASS_NAME: "BaseClass1"
        });

        var BaseClass2 = OpenLayers.Class({
            override: "base2",
            initialize: function() {
                t.ok(false,
                     "base class constructor is not called during inheritance");
            },
            CLASS_NAME: "BaseClass1"
        });
        
        var ChildClass = OpenLayers.Class(BaseClass1, BaseClass2, {
            initialize: function(arg) {
                if(this.prop == "base1") {
                    this.variable = "child";
                }
                t.ok(true,
                     "only child class constructor is called on initialization");
            },
            CLASS_NAME: "ChildClass"
        });
        
        var arg = "child";
        var child = new ChildClass(arg);
        t.eq(child.variable, arg,
             "inheritance works before construction");
        t.eq(child.prop, "base1",
             "properties are inherited with multiple classes")
        t.eq(child.override, "base2",
             "properties are inherited in the expected order");
        t.eq(child["CLASS_NAME"],
             "ChildClass",
             "object is an instance of child class");
        
        var base1 = new BaseClass1();
        t.eq(base1.override, "base1",
             "inheritance doesn't mess with parents");

    }

    function test_inheritance_chain(t) {
        t.plan(1);
        var A = new OpenLayers.Class({
            initialize: function() {
                this.a = 'foo';
            }
        });
        var B = new OpenLayers.Class(A, {});
        var C = new OpenLayers.Class(B, {
            initialize: function() {
                B.prototype.initialize.apply(this, arguments);
                this.a = this.a + 'bar';
            }
        });
        var c = new C;
        t.eq(c.a, 'foobar', 'constructor at the root is called');
    }
    
    // Remove this at 3.0
    function test_Class_backwards(t) {
        t.plan(4);
        // test that a new style class supports old style inheritance
        var NewClass = OpenLayers.Class({
            newProp: "new",
            initialize: function() {
                t.ok(false, "the base class is never instantiated");
            },
            toString: function() {
                return "new style";
            }
        });
        
        var OldClass = OpenLayers.Class.create();
        OldClass.prototype = OpenLayers.Class.inherit(NewClass, {
            oldProp: "old",
            initialize: function() {
                t.ok(true, "only the child class constructor is called");
            }
        });
        
        var oldObj = new OldClass();
        t.eq(oldObj.oldProp, "old",
             "old style classes can still be instantiated");
        t.eq(oldObj.newProp, "new",
             "old style inheritance of properties works with new style base");
        t.eq(oldObj.toString(), "new style",
             "toString inheritance works with backwards style");
        
    }

    // Remove this at 3.0
    function test_Class_create (t) {
        t.plan( 3 );
        var cls = OpenLayers.Class.create();
        cls.prototype = {
            initialize: function () {
                if (isMozilla)
                    t.ok(this instanceof cls,
                                "initialize is called on the right class");
                else
                    t.ok(true, "initialize is called");
            }
        };
        var obj = new cls();
        t.eq(typeof obj, "object", "obj is an object");
        if (isMozilla)
            t.ok(obj instanceof cls,
                        "object is of the right class");
        else
            t.ok(true, "this test doesn't work in IE");
    }

    // Remove this at 3.0
    function test_Class_inherit (t) {
        t.plan( 20 );
        var A = OpenLayers.Class.create();
        var initA = 0;
        A.prototype = {
            count: 0,
            mixed: false,
            initialize: function () {
                initA++;
                this.count++;
            }
        };

        var B = OpenLayers.Class.create();
        var initB = 0;
        B.prototype = OpenLayers.Class.inherit( A, {
            initialize: function () {
                A.prototype.initialize.apply(this, arguments);
                initB++;
                this.count++;
            }
        });

        var mixin = OpenLayers.Class.create()
        mixin.prototype = {
            mixed: true
        };

        t.eq( initA, 0, "class A not inited" );
        t.eq( initB, 0, "class B not inited" );

        var objA = new A();
        t.eq( objA.count, 1, "object A init" );
        t.eq( initA, 1, "class A init" );
        if (isMozilla) 
            t.ok( objA instanceof A, "obj A isa A" );
        else
            t.ok( true, "IE sucks" );

        var objB = new B();
        t.eq( initA, 2, "class A init" );
        t.eq( initB, 1, "class B init" );
        t.eq( objB.count, 2, "object B init twice" );
        if (isMozilla) {
            t.ok( objB instanceof A, "obj B isa A" );
            t.ok( objB instanceof B, "obj B isa B" );
        } else {
            t.ok( true, "IE sucks" );
            t.ok( true, "IE sucks" );
        }

        var C = OpenLayers.Class.create();
        C.prototype = OpenLayers.Class.inherit( B, mixin, {count: 0} );
        t.eq( initA, 2, "class A init unchanged" );
        t.eq( initB, 1, "class B init unchanged" );
        
        var objC = new C();
        t.eq( initA, 3, "class A init changed" );
        t.eq( initB, 2, "class B init changed" );
        t.eq( objC.count, 2, "object C init changed" );
        if (isMozilla) {
            t.ok( objC instanceof A, "obj C isa A" );
            t.ok( objC instanceof B, "obj C isa B" );
            t.ok( objC instanceof C, "obj C isa C" );
            t.ok( !(objC instanceof mixin), "obj C isn'ta mixin" );
        } else {
            t.ok( true, "IE sucks" );
            t.ok( true, "IE sucks" );
            t.ok( true, "IE sucks" );
            t.ok( true, "IE sucks" );
        }
        t.eq( objC.mixed, true, "class C has mixin properties" );
    }

    function test_Class_isInstanceOf(t) {
        t.plan(7);
        var wfs = new OpenLayers.Layer.WFS({});
        var drag = new OpenLayers.Control.DragFeature({});
        t.ok(wfs instanceof OpenLayers.Layer.WFS, "isInstanceOf(WFS)");
        t.ok(wfs instanceof OpenLayers.Layer, "isInstanceOf(Layer)");
        t.ok(!(wfs instanceof OpenLayers.Format), "not isInstanceOf(Format)");
        t.ok(drag instanceof OpenLayers.Control, "drag is a control");
        t.ok(!(drag instanceof OpenLayers.Layer), "drag is not a layer");

        //test a class with multiple inheritance
        var BadClass=OpenLayers.Class(OpenLayers.Layer.WFS, OpenLayers.Control.DragFeature);
        var bad = new BadClass({});
        t.ok(!(bad instanceof OpenLayers.Control), "bad is a control, but it is also a layer and we cannot have two superclasses");
        t.ok(bad instanceof OpenLayers.Layer, "bad is a layer, it inherits from the layer first");
    }

    //
    // IGN's GeoPortal API overwrite prototypes of OpenLayers constructors.
    // The tests below aim to cover their usage pattens.
    //

    // the overwrite function under test
    function overwrite(C, o) {
        if(typeof o.initialize === "function" &&
            C === C.prototype.initialize) {
            // OL 2.11

            var proto = C.prototype;
            var staticProps = OpenLayers.Util.extend({}, C);

            C = o.initialize;

            C.prototype = proto;
            OpenLayers.Util.extend(C, staticProps);
        }
        OpenLayers.Util.extend(C.prototype, o);
        return C;
    }

    function test_overwrite_1(t) {
        // overwrite constructor
        t.plan(1);
        var A = OpenLayers.Class({
            initialize: function() {
                this.a = "foo";
            }
        });
        A = overwrite(A, {
            initialize: function() {
                this.a = "bar";
            }
        });
        var a = new A;
        t.eq(a.a, "bar", "ctor overwritten");
    }

    function test_overwrite_2(t) {
        // overwrite regular method
        t.plan(1);
        var A = OpenLayers.Class({
            initialize: function() {
            },
            method: function() {
                this.a = "foo";
            }
        });
        A = overwrite(A, {
            method: function() {
                this.a = "bar";
            }
        });
        var a = new A;
        a.method();
        t.eq(a.a, "bar", "method overwritten");
    }

    function test_overwrite_3(t) {
        // overwrite constructor of subclass
        t.plan(1);
        var A = OpenLayers.Class({
            initialize: function() {
                this.a = "foo";
            }
        });
        var B = OpenLayers.Class(A, {
            initialize: function() {
                A.prototype.initialize.call(this);
            }
        });
        B = overwrite(B, {
            initialize: function() {
                A.prototype.initialize.call(this);
                this.a = "bar";
            }
        });
        var b = new B;
        t.eq(b.a, "bar", "ctor overwritten");
    }

    function test_overwrite_4(t) {
        // overwrite constructor of parent class
        t.plan(1);
        var A = OpenLayers.Class({
            initialize: function() {
                this.a = "foo";
            }
        });
        var B = OpenLayers.Class(A, {
            initialize: function() {
                A.prototype.initialize.call(this);
            }
        });
        A = overwrite(A, {
            initialize: function() {
                this.a = "bar";
            }
        });
        var b = new B;
        t.eq(b.a, "bar", "ctor overwritten");
    }

    function test_overwrite_5(t) {
        // overwrite constructor of parent class, which itself
        // doesn't defined "initialize"
        t.plan(2);
        var A = OpenLayers.Class({
            initialize: function() {
                this.a = "foo";
            }
        });
        var B = OpenLayers.Class(A, {});
        var _A = A;
        A = overwrite(A, {
            initialize: function() {
                this.a = "bar";
            }
        });
        var b = new B;
        t.ok(A.prototype === _A.prototype, "A and _A share the prototype");
        t.eq(b.a, "bar", "ctor overwritten");
    }

    function test_overwrite_6(t) {
        // with static methods
        t.plan(1);
        var A = OpenLayers.Class({
            initialize: function() {
            }
        });
        A.staticMethod = function() {};
        A = overwrite(A, {
            initialize: function() {
            }
        });
        var exc = false;
        try {
            A.staticMethod();
        } catch(e) {
            exc = true;
        }
        t.ok(!exc, "static method still there");
    }
  </script>
</head>
<body>
</body>
</html>