Home | History | Annotate | Download | only in js
      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 
    123 function testDefinePropertyAttrOnClass() {
    124   var obj = document.createElement('button');
    125   cr.defineProperty(HTMLButtonElement, 'test', cr.PropertyKind.ATTR);
    126 
    127   assertEquals(null, obj.test);
    128 
    129   obj.test = 'a';
    130   assertEquals('a', obj.test);
    131   assertEquals('a', obj.getAttribute('test'));
    132 }
    133 
    134 function testDefinePropertyAttrWithSetter() {
    135   var obj = document.createElement('div');
    136 
    137   var hit = false;
    138   function onTestSet(value, oldValue) {
    139     assertEquals(obj, this);
    140     assertEquals(null, oldValue);
    141     assertEquals('b', value);
    142     assertEquals('b', this.test);
    143     hit = true;
    144   }
    145   cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR, onTestSet);
    146   obj.test = 'b';
    147   assertTrue(hit);
    148 }
    149 
    150 function testDefinePropertyAttrEvent() {
    151   var obj = document.createElement('div');
    152   cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR);
    153 
    154   var count = 0;
    155   function f(e) {
    156     assertEquals('testChange', e.type);
    157     assertEquals('test', e.propertyName);
    158     assertEquals(null, e.oldValue);
    159     assertEquals('b', e.newValue);
    160     count++;
    161   }
    162 
    163   obj.addEventListener('testChange', f);
    164 
    165   obj.test = null;
    166   assertEquals('Should not have called the property change listener', 0, count);
    167 
    168   obj.test = 'b';
    169   assertEquals('b', obj.test);
    170   assertEquals('Should have called the property change listener', 1, count);
    171 
    172   obj.test = 'b';
    173   assertEquals(1, count);
    174 }
    175 
    176 function testDefinePropertyBoolAttr() {
    177   var obj = document.createElement('div');
    178   cr.defineProperty(obj, 'test', cr.PropertyKind.BOOL_ATTR);
    179 
    180   assertFalse(obj.test);
    181   assertFalse(obj.hasAttribute('test'));
    182 
    183   obj.test = true;
    184   assertTrue(obj.test);
    185   assertTrue(obj.hasAttribute('test'));
    186 
    187   obj.test = false;
    188   assertFalse(obj.test);
    189   assertFalse(obj.hasAttribute('test'));
    190 }
    191 
    192 function testDefinePropertyBoolAttrEvent() {
    193   var obj = document.createElement('div');
    194   cr.defineProperty(obj, 'test', cr.PropertyKind.BOOL_ATTR);
    195 
    196   var count = 0;
    197   function f(e) {
    198     assertEquals('testChange', e.type);
    199     assertEquals('test', e.propertyName);
    200     assertEquals(false, e.oldValue);
    201     assertEquals(true, e.newValue);
    202     count++;
    203   }
    204 
    205   obj.addEventListener('testChange', f);
    206   obj.test = true;
    207   assertTrue(obj.test);
    208   assertEquals('Should have called the property change listener', 1, count);
    209 
    210   obj.test = true;
    211   assertEquals(1, count);
    212 }
    213 
    214 function testDefinePropertyBoolAttrEvent() {
    215   var obj = document.createElement('div');
    216   var hit = false;
    217   function onTestSet(value, oldValue) {
    218     assertEquals(obj, this);
    219     assertTrue(this.test);
    220     assertFalse(oldValue);
    221     assertTrue(value);
    222     hit = true;
    223   }
    224   cr.defineProperty(obj, 'test', cr.PropertyKind.BOOL_ATTR, onTestSet);
    225   obj.test = true;
    226   assertTrue(hit);
    227 }
    228 
    229 function testAddSingletonGetter() {
    230   function Foo() {};
    231   cr.addSingletonGetter(Foo);
    232 
    233   assertNotNull('Should add get instance function', Foo.getInstance);
    234 
    235   var x = Foo.getInstance();
    236   assertNotNull('Should successfully create an object', x);
    237 
    238   var y = Foo.getInstance();
    239   assertEquals('Should return the same object', x, y);
    240 
    241   delete Foo.instance_;
    242 
    243   var z = Foo.getInstance();
    244   assertNotNull('Should work after clearing for testing', z);
    245 
    246   assertNotEquals('Should return a different object after clearing for testing',
    247       x, z);
    248 }
    249 
    250 function testWithDoc() {
    251   var d = {};
    252 
    253   assertEquals(document, cr.doc);
    254 
    255   cr.withDoc(d, function() {
    256     assertEquals(d, cr.doc);
    257   });
    258 
    259   assertEquals(document, cr.doc);
    260 }
    261 
    262 function testDefineWithGetter() {
    263   var v = 0;
    264   cr.define('foo', function() {
    265     return {
    266       get v() {
    267         return v;
    268       }
    269     }
    270   });
    271 
    272   assertEquals(0, foo.v);
    273 
    274   v = 1;
    275   assertEquals(1, foo.v);
    276 }
    277 
    278 </script>
    279 
    280 </body>
    281 </html>
    282