]> dev.renevier.net Git - syp.git/blob - openlayers/tests/BaseTypes/Class.html
initial commit
[syp.git] / openlayers / tests / BaseTypes / Class.html
1 <html>
2 <head>
3   <script src="../../lib/OpenLayers.js"></script>
4   <script type="text/javascript">
5     // remove this next line at 3.0
6     var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
7
8     function test_Class(t) {
9         t.plan(1);
10         var MyClass = OpenLayers.Class({
11             initialize: function () {
12                 t.ok(false, "initialize should not be called");
13             }
14         });
15         t.ok(true,
16              "defining a class does not call the constructor for the class");
17     }
18
19     function test_Class_constructor(t) {
20         t.plan(7);
21         
22         var MyClass = OpenLayers.Class({
23             prop: null,
24             classProp: {'bad': 'practice'},
25             initialize: function(a1, a2) {
26                 this.prop = "instance property";
27                 t.ok(true,
28                      "initialize is called when a new instance is created");
29                 t.eq(a1, arg1,
30                      "initialize is called with the proper first argument");
31                 t.eq(a2, arg2,
32                      "initialize is called with the proper second argument");
33             },
34             CLASS_NAME: "MyClass"
35         });
36
37         var arg1 = "anArg";
38         var arg2 = {"another": "arg"};
39         var myObj = new MyClass(arg1, arg2);
40         t.eq(MyClass.prop, null,
41              "creating a new instance doesn't modify the class");
42         t.eq(myObj.prop, "instance property",
43              "the new instance is assigned a property in the constructor");
44         t.eq(myObj["CLASS_NAME"], "MyClass",
45              "the new object is an instance of MyClass");
46
47         // allow for modification of class properties
48         MyClass.prototype.classProp.bad = "good";
49         t.eq(myObj.classProp.bad, "good",
50              "modifying a class property modifies properties of the instance");
51     }
52
53     function test_Class_inheritance(t) {
54         t.plan(7);
55         
56         var BaseClass = OpenLayers.Class({
57             prop: "base",
58             initialize: function() {
59                 t.ok(false,
60                      "base class constructor is not called during inheritance");
61             },
62             toString: function() {
63                 return "toString inherited";
64             },
65             CLASS_NAME: "BaseClass"
66         });
67         
68         var ChildClass = OpenLayers.Class(BaseClass, {
69             initialize: function() {
70                 t.ok(true,
71                      "child class constructor is called in creating an instance");
72             },
73             CLASS_NAME: "ChildClass"
74         });
75         
76         var child = new ChildClass();
77         t.eq(child.prop, "base",
78              "instance of child inherits properties from base");
79         t.eq(child.toString(), "toString inherited",
80              "instance of child inherits toString method from base");
81         t.eq(child["CLASS_NAME"],
82              "ChildClass",
83              "new object is an instance of the child class");
84         
85         var F = OpenLayers.Class(Object, {});
86         t.ok(!("initialize" in Object.prototype), "no messing with non OL prototypes");
87
88         // test with an abstract class (i.e. a class that doesn't have an initialize
89         // method) as the parent class
90         var Vehicule = OpenLayers.Class({
91             numWheels: null
92         });
93         var Bike = OpenLayers.Class(Vehicule, {
94             initialize: function() {
95                 this.numWheels = 2;
96             }
97         });
98         var b = new Bike();
99         t.ok(b instanceof Vehicule, "a bike is a vehicule");
100         
101         // test inheritance with something that has a non-function initialize property
102         var P = OpenLayers.Class({
103             initialize: "foo"
104         });
105         var C = OpenLayers.Class(P, {
106             initialize: function() {
107                 // pass
108             }
109         });
110         var c = new C();
111         t.eq(P.prototype.initialize, "foo", "Class restores custom initialize property.");
112         
113     }
114     
115     function test_Class_multiple_inheritance(t) {
116         t.plan(7);
117         var BaseClass1 = OpenLayers.Class({
118             override: "base1",
119             prop: "base1",
120             variable: null,
121             initialize: function() {
122                 t.ok(true,
123                      "only called when an instance of this class is created");
124             },
125             CLASS_NAME: "BaseClass1"
126         });
127
128         var BaseClass2 = OpenLayers.Class({
129             override: "base2",
130             initialize: function() {
131                 t.ok(false,
132                      "base class constructor is not called during inheritance");
133             },
134             CLASS_NAME: "BaseClass1"
135         });
136         
137         var ChildClass = OpenLayers.Class(BaseClass1, BaseClass2, {
138             initialize: function(arg) {
139                 if(this.prop == "base1") {
140                     this.variable = "child";
141                 }
142                 t.ok(true,
143                      "only child class constructor is called on initialization");
144             },
145             CLASS_NAME: "ChildClass"
146         });
147         
148         var arg = "child";
149         var child = new ChildClass(arg);
150         t.eq(child.variable, arg,
151              "inheritance works before construction");
152         t.eq(child.prop, "base1",
153              "properties are inherited with multiple classes")
154         t.eq(child.override, "base2",
155              "properties are inherited in the expected order");
156         t.eq(child["CLASS_NAME"],
157              "ChildClass",
158              "object is an instance of child class");
159         
160         var base1 = new BaseClass1();
161         t.eq(base1.override, "base1",
162              "inheritance doesn't mess with parents");
163
164     }
165     
166     // Remove this at 3.0
167     function test_Class_backwards(t) {
168         t.plan(4);
169         // test that a new style class supports old style inheritance
170         var NewClass = OpenLayers.Class({
171             newProp: "new",
172             initialize: function() {
173                 t.ok(false, "the base class is never instantiated");
174             },
175             toString: function() {
176                 return "new style";
177             }
178         });
179         
180         var OldClass = OpenLayers.Class.create();
181         OldClass.prototype = OpenLayers.Class.inherit(NewClass, {
182             oldProp: "old",
183             initialize: function() {
184                 t.ok(true, "only the child class constructor is called");
185             }
186         });
187         
188         var oldObj = new OldClass();
189         t.eq(oldObj.oldProp, "old",
190              "old style classes can still be instantiated");
191         t.eq(oldObj.newProp, "new",
192              "old style inheritance of properties works with new style base");
193         t.eq(oldObj.toString(), "new style",
194              "toString inheritance works with backwards style");
195         
196     }
197
198     // Remove this at 3.0
199     function test_Class_create (t) {
200         t.plan( 3 );
201         var cls = OpenLayers.Class.create();
202         cls.prototype = {
203             initialize: function () {
204                 if (isMozilla)
205                     t.ok(this instanceof cls,
206                                 "initialize is called on the right class");
207                 else
208                     t.ok(true, "initialize is called");
209             }
210         };
211         var obj = new cls();
212         t.eq(typeof obj, "object", "obj is an object");
213         if (isMozilla)
214             t.ok(obj instanceof cls,
215                         "object is of the right class");
216         else
217             t.ok(true, "this test doesn't work in IE");
218     }
219
220     // Remove this at 3.0
221     function test_Class_inherit (t) {
222         t.plan( 20 );
223         var A = OpenLayers.Class.create();
224         var initA = 0;
225         A.prototype = {
226             count: 0,
227             mixed: false,
228             initialize: function () {
229                 initA++;
230                 this.count++;
231             }
232         };
233
234         var B = OpenLayers.Class.create();
235         var initB = 0;
236         B.prototype = OpenLayers.Class.inherit( A, {
237             initialize: function () {
238                 A.prototype.initialize.apply(this, arguments);
239                 initB++;
240                 this.count++;
241             }
242         });
243
244         var mixin = OpenLayers.Class.create()
245         mixin.prototype = {
246             mixed: true
247         };
248
249         t.eq( initA, 0, "class A not inited" );
250         t.eq( initB, 0, "class B not inited" );
251
252         var objA = new A();
253         t.eq( objA.count, 1, "object A init" );
254         t.eq( initA, 1, "class A init" );
255         if (isMozilla) 
256             t.ok( objA instanceof A, "obj A isa A" );
257         else
258             t.ok( true, "IE sucks" );
259
260         var objB = new B();
261         t.eq( initA, 2, "class A init" );
262         t.eq( initB, 1, "class B init" );
263         t.eq( objB.count, 2, "object B init twice" );
264         if (isMozilla) {
265             t.ok( objB instanceof A, "obj B isa A" );
266             t.ok( objB instanceof B, "obj B isa B" );
267         } else {
268             t.ok( true, "IE sucks" );
269             t.ok( true, "IE sucks" );
270         }
271
272         var C = OpenLayers.Class.create();
273         C.prototype = OpenLayers.Class.inherit( B, mixin, {count: 0} );
274         t.eq( initA, 2, "class A init unchanged" );
275         t.eq( initB, 1, "class B init unchanged" );
276         
277         var objC = new C();
278         t.eq( initA, 3, "class A init changed" );
279         t.eq( initB, 2, "class B init changed" );
280         t.eq( objC.count, 2, "object C init changed" );
281         if (isMozilla) {
282             t.ok( objC instanceof A, "obj C isa A" );
283             t.ok( objC instanceof B, "obj C isa B" );
284             t.ok( objC instanceof C, "obj C isa C" );
285             t.ok( !(objC instanceof mixin), "obj C isn'ta mixin" );
286         } else {
287             t.ok( true, "IE sucks" );
288             t.ok( true, "IE sucks" );
289             t.ok( true, "IE sucks" );
290             t.ok( true, "IE sucks" );
291         }
292         t.eq( objC.mixed, true, "class C has mixin properties" );
293     }
294
295     function test_Class_isInstanceOf(t) {
296         t.plan(7);
297         var wfs = new OpenLayers.Layer.WFS({});
298         var drag = new OpenLayers.Control.DragFeature({});
299         t.ok(wfs instanceof OpenLayers.Layer.WFS, "isInstanceOf(WFS)");
300         t.ok(wfs instanceof OpenLayers.Layer, "isInstanceOf(Layer)");
301         t.ok(!(wfs instanceof OpenLayers.Format), "not isInstanceOf(Format)");
302         t.ok(drag instanceof OpenLayers.Control, "drag is a control");
303         t.ok(!(drag instanceof OpenLayers.Layer), "drag is not a layer");
304
305         //test a class with multiple inheritance
306         var BadClass=OpenLayers.Class(OpenLayers.Layer.WFS, OpenLayers.Control.DragFeature);
307         var bad = new BadClass({});
308         t.ok(!(bad instanceof OpenLayers.Control), "bad is a control, but it is also a layer and we cannot have two superclasses");
309         t.ok(bad instanceof OpenLayers.Layer, "bad is a layer, it inherits from the layer first");
310     }
311   </script>
312 </head>
313 <body>
314 </body>
315 </html>