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);
8 function test_Class(t) {
10 var MyClass = OpenLayers.Class({
11 initialize: function () {
12 t.ok(false, "initialize should not be called");
16 "defining a class does not call the constructor for the class");
19 function test_Class_constructor(t) {
22 var MyClass = OpenLayers.Class({
24 classProp: {'bad': 'practice'},
25 initialize: function(a1, a2) {
26 this.prop = "instance property";
28 "initialize is called when a new instance is created");
30 "initialize is called with the proper first argument");
32 "initialize is called with the proper second argument");
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");
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");
53 function test_Class_inheritance(t) {
56 var BaseClass = OpenLayers.Class({
58 initialize: function() {
60 "base class constructor is not called during inheritance");
62 toString: function() {
63 return "toString inherited";
65 CLASS_NAME: "BaseClass"
68 var ChildClass = OpenLayers.Class(BaseClass, {
69 initialize: function() {
71 "child class constructor is called in creating an instance");
73 CLASS_NAME: "ChildClass"
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"],
83 "new object is an instance of the child class");
85 var F = OpenLayers.Class(Object, {});
86 t.ok(!("initialize" in Object.prototype), "no messing with non OL prototypes");
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({
93 var Bike = OpenLayers.Class(Vehicule, {
94 initialize: function() {
99 t.ok(b instanceof Vehicule, "a bike is a vehicule");
101 // test inheritance with something that has a non-function initialize property
102 var P = OpenLayers.Class({
105 var C = OpenLayers.Class(P, {
106 initialize: function() {
111 t.eq(P.prototype.initialize, "foo", "Class restores custom initialize property.");
115 function test_Class_multiple_inheritance(t) {
117 var BaseClass1 = OpenLayers.Class({
121 initialize: function() {
123 "only called when an instance of this class is created");
125 CLASS_NAME: "BaseClass1"
128 var BaseClass2 = OpenLayers.Class({
130 initialize: function() {
132 "base class constructor is not called during inheritance");
134 CLASS_NAME: "BaseClass1"
137 var ChildClass = OpenLayers.Class(BaseClass1, BaseClass2, {
138 initialize: function(arg) {
139 if(this.prop == "base1") {
140 this.variable = "child";
143 "only child class constructor is called on initialization");
145 CLASS_NAME: "ChildClass"
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"],
158 "object is an instance of child class");
160 var base1 = new BaseClass1();
161 t.eq(base1.override, "base1",
162 "inheritance doesn't mess with parents");
166 // Remove this at 3.0
167 function test_Class_backwards(t) {
169 // test that a new style class supports old style inheritance
170 var NewClass = OpenLayers.Class({
172 initialize: function() {
173 t.ok(false, "the base class is never instantiated");
175 toString: function() {
180 var OldClass = OpenLayers.Class.create();
181 OldClass.prototype = OpenLayers.Class.inherit(NewClass, {
183 initialize: function() {
184 t.ok(true, "only the child class constructor is called");
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");
198 // Remove this at 3.0
199 function test_Class_create (t) {
201 var cls = OpenLayers.Class.create();
203 initialize: function () {
205 t.ok(this instanceof cls,
206 "initialize is called on the right class");
208 t.ok(true, "initialize is called");
212 t.eq(typeof obj, "object", "obj is an object");
214 t.ok(obj instanceof cls,
215 "object is of the right class");
217 t.ok(true, "this test doesn't work in IE");
220 // Remove this at 3.0
221 function test_Class_inherit (t) {
223 var A = OpenLayers.Class.create();
228 initialize: function () {
234 var B = OpenLayers.Class.create();
236 B.prototype = OpenLayers.Class.inherit( A, {
237 initialize: function () {
238 A.prototype.initialize.apply(this, arguments);
244 var mixin = OpenLayers.Class.create()
249 t.eq( initA, 0, "class A not inited" );
250 t.eq( initB, 0, "class B not inited" );
253 t.eq( objA.count, 1, "object A init" );
254 t.eq( initA, 1, "class A init" );
256 t.ok( objA instanceof A, "obj A isa A" );
258 t.ok( true, "IE sucks" );
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" );
265 t.ok( objB instanceof A, "obj B isa A" );
266 t.ok( objB instanceof B, "obj B isa B" );
268 t.ok( true, "IE sucks" );
269 t.ok( true, "IE sucks" );
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" );
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" );
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" );
287 t.ok( true, "IE sucks" );
288 t.ok( true, "IE sucks" );
289 t.ok( true, "IE sucks" );
290 t.ok( true, "IE sucks" );
292 t.eq( objC.mixed, true, "class C has mixin properties" );
295 function test_Class_isInstanceOf(t) {
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");
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");