1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title></title> 5 <style> 6 7 </style> 8 <script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script> 9 <script src="cr.js"></script> 10 <script src="cr/event_target.js"></script> 11 <script> 12 13 goog.require('goog.testing.jsunit'); 14 15 </script> 16 17 </head> 18 <body> 19 20 <script> 21 22 const EventTarget = cr.EventTarget; 23 24 function testDefineProperty() { 25 var obj = new EventTarget; 26 cr.defineProperty(obj, 'test'); 27 28 obj.test = 1; 29 assertEquals(1, obj.test); 30 assertEquals(1, obj.test_); 31 } 32 33 function testDefinePropertyOnClass() { 34 function C() {} 35 C.prototype = { 36 __proto__: EventTarget.prototype 37 }; 38 39 cr.defineProperty(C, 'test'); 40 41 var obj = new C; 42 assertUndefined(obj.test); 43 44 obj.test = 1; 45 assertEquals(1, obj.test); 46 assertEquals(1, obj.test_); 47 } 48 49 function testDefinePropertyWithSetter() { 50 var obj = new EventTarget; 51 52 var hit = false; 53 function onTestSet(value, oldValue) { 54 assertEquals(obj, this); 55 assertEquals(2, this.test); 56 assertUndefined(oldValue); 57 assertEquals(2, value); 58 hit = true; 59 } 60 cr.defineProperty(obj, 'test', cr.PropertyKind.JS, onTestSet); 61 obj.test = 2; 62 assertTrue(hit); 63 } 64 65 function testDefinePropertyEvent() { 66 var obj = new EventTarget; 67 cr.defineProperty(obj, 'test'); 68 obj.test = 1; 69 70 var count = 0; 71 function f(e) { 72 assertEquals('testChange', e.type); 73 assertEquals('test', e.propertyName); 74 assertEquals(1, e.oldValue); 75 assertEquals(2, e.newValue); 76 count++; 77 } 78 79 obj.addEventListener('testChange', f); 80 obj.test = 2; 81 assertEquals(2, obj.test); 82 assertEquals('Should have called the property change listener', 1, count); 83 84 obj.test = 2; 85 assertEquals(1, count); 86 } 87 88 function testDefinePropertyEventWithDefault() { 89 var obj = new EventTarget; 90 cr.defineProperty(obj, 'test', cr.PropertyKind.JS); 91 92 var count = 0; 93 function f(e) { 94 assertEquals('testChange', e.type); 95 assertEquals('test', e.propertyName); 96 assertUndefined(e.oldValue); 97 assertEquals(2, e.newValue); 98 count++; 99 } 100 101 obj.addEventListener('testChange', f); 102 103 obj.test = undefined; 104 assertEquals('Should not have called the property change listener', 0, count); 105 106 obj.test = 2; 107 assertEquals(2, obj.test); 108 assertEquals('Should have called the property change listener', 1, count); 109 110 obj.test = 2; 111 assertEquals(1, count); 112 } 113 114 function testDefinePropertyAttr() { 115 var obj = document.createElement('div'); 116 cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR); 117 118 obj.test = 'a'; 119 assertEquals('a', obj.test); 120 assertEquals('a', obj.getAttribute('test')); 121 122 obj.test = undefined; 123 assertEquals(undefined, obj.test); 124 assertFalse(obj.hasAttribute('test')); 125 } 126 127 function testDefinePropertyAttrOnClass() { 128 var obj = document.createElement('button'); 129 cr.defineProperty(HTMLButtonElement, 'test', cr.PropertyKind.ATTR); 130 131 assertEquals(null, obj.test); 132 133 obj.test = 'a'; 134 assertEquals('a', obj.test); 135 assertEquals('a', obj.getAttribute('test')); 136 137 obj.test = undefined; 138 assertEquals(undefined, obj.test); 139 assertFalse(obj.hasAttribute('test')); 140 } 141 142 function testDefinePropertyAttrWithSetter() { 143 var obj = document.createElement('div'); 144 145 var hit = false; 146 function onTestSet(value, oldValue) { 147 assertEquals(obj, this); 148 assertEquals(null, oldValue); 149 assertEquals('b', value); 150 assertEquals('b', this.test); 151 hit = true; 152 } 153 cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR, onTestSet); 154 obj.test = 'b'; 155 assertTrue(hit); 156 } 157 158 function testDefinePropertyAttrEvent() { 159 var obj = document.createElement('div'); 160 cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR); 161 162 var count = 0; 163 function f(e) { 164 assertEquals('testChange', e.type); 165 assertEquals('test', e.propertyName); 166 assertEquals(null, e.oldValue); 167 assertEquals('b', e.newValue); 168 count++; 169 } 170 171 obj.addEventListener('testChange', f); 172 173 obj.test = null; 174 assertEquals('Should not have called the property change listener', 0, count); 175 176 obj.test = 'b'; 177 assertEquals('b', obj.test); 178 assertEquals('Should have called the property change listener', 1, count); 179 180 obj.test = 'b'; 181 assertEquals(1, count); 182 } 183 184 function testDefinePropertyBoolAttr() { 185 var obj = document.createElement('div'); 186 cr.defineProperty(obj, 'test', cr.PropertyKind.BOOL_ATTR); 187 188 assertFalse(obj.test); 189 assertFalse(obj.hasAttribute('test')); 190 191 obj.test = true; 192 assertTrue(obj.test); 193 assertTrue(obj.hasAttribute('test')); 194 195 obj.test = false; 196 assertFalse(obj.test); 197 assertFalse(obj.hasAttribute('test')); 198 } 199 200 function testDefinePropertyBoolAttrEvent() { 201 var obj = document.createElement('div'); 202 cr.defineProperty(obj, 'test', cr.PropertyKind.BOOL_ATTR); 203 204 var count = 0; 205 function f(e) { 206 assertEquals('testChange', e.type); 207 assertEquals('test', e.propertyName); 208 assertEquals(false, e.oldValue); 209 assertEquals(true, e.newValue); 210 count++; 211 } 212 213 obj.addEventListener('testChange', f); 214 obj.test = true; 215 assertTrue(obj.test); 216 assertEquals('Should have called the property change listener', 1, count); 217 218 obj.test = true; 219 assertEquals(1, count); 220 } 221 222 function testDefinePropertyBoolAttrEvent() { 223 var obj = document.createElement('div'); 224 var hit = false; 225 function onTestSet(value, oldValue) { 226 assertEquals(obj, this); 227 assertTrue(this.test); 228 assertFalse(oldValue); 229 assertTrue(value); 230 hit = true; 231 } 232 cr.defineProperty(obj, 'test', cr.PropertyKind.BOOL_ATTR, onTestSet); 233 obj.test = true; 234 assertTrue(hit); 235 } 236 237 function testAddSingletonGetter() { 238 function Foo() {}; 239 cr.addSingletonGetter(Foo); 240 241 assertNotNull('Should add get instance function', Foo.getInstance); 242 243 var x = Foo.getInstance(); 244 assertNotNull('Should successfully create an object', x); 245 246 var y = Foo.getInstance(); 247 assertEquals('Should return the same object', x, y); 248 249 delete Foo.instance_; 250 251 var z = Foo.getInstance(); 252 assertNotNull('Should work after clearing for testing', z); 253 254 assertNotEquals('Should return a different object after clearing for testing', 255 x, z); 256 } 257 258 function testWithDoc() { 259 var d = {}; 260 261 assertEquals(document, cr.doc); 262 263 cr.withDoc(d, function() { 264 assertEquals(d, cr.doc); 265 }); 266 267 assertEquals(document, cr.doc); 268 } 269 270 function testDefineWithGetter() { 271 var v = 0; 272 cr.define('foo', function() { 273 return { 274 get v() { 275 return v; 276 } 277 } 278 }); 279 280 assertEquals(0, foo.v); 281 282 v = 1; 283 assertEquals(1, foo.v); 284 } 285 286 </script> 287 288 </body> 289 </html> 290