Home | History | Annotate | Download | only in cr
      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4 <!-- TODO(arv): Check in Closue unit tests and make this run as part of the
      5      tests -->
      6 <script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script>
      7 <script src="../cr.js"></script>
      8 <script src="promise.js"></script>
      9 <script>
     10 
     11 goog.require('goog.testing.jsunit');
     12 goog.require('goog.testing.MockClock');
     13 
     14 </script>
     15 </head>
     16 <body>
     17 <script>
     18 
     19 var mockClock;
     20 
     21 function setUp() {
     22   mockClock = new goog.testing.MockClock();
     23   mockClock.install();
     24 }
     25 
     26 function tearDown() {
     27   mockClock.uninstall();
     28 }
     29 
     30 const Promise = cr.Promise;
     31 
     32 function testCallbacks() {
     33   var calls1 = 0;
     34   var calls2 = 0;
     35   var V = {};
     36   function f1(v) {
     37     calls1++;
     38     assertEquals(V, v);
     39   }
     40   function f2(v) {
     41     calls2++;
     42     assertEquals(V, v);
     43   }
     44   var p = new Promise;
     45   p.addListener(f1);
     46   p.addListener(f2);
     47   p.value = V;
     48   assertEquals(1, calls1);
     49   assertEquals(1, calls2);
     50 }
     51 
     52 function testCallbacks2() {
     53   var calls1 = 0;
     54   var calls2 = 0;
     55   var V = {};
     56   function f1(v) {
     57     calls1++;
     58     assertEquals(V, v);
     59   }
     60   function f2(v) {
     61     calls2++;
     62     assertEquals(V, v);
     63   }
     64   var p = new Promise;
     65   p.addListener(f1);
     66   p.addListener(f2);
     67   p.removeListener(f1);
     68   p.value = V;
     69   assertEquals(0, calls1);
     70   assertEquals(1, calls2);
     71 }
     72 
     73 function testCallbacks3() {
     74   var calls1 = 0;
     75   var calls2 = 0;
     76   var V = {};
     77   function f1(v) {
     78     calls1++;
     79     assertEquals(V, v);
     80   }
     81   function f2(v) {
     82     calls2++;
     83     assertEquals(V, v);
     84   }
     85   var p = new Promise;
     86   p.addListener(f1);
     87   assertEquals(0, calls1);
     88   assertEquals(0, calls2);
     89   p.value = V;
     90   assertEquals(1, calls1);
     91   assertEquals(0, calls2);
     92   p.addListener(f2);
     93   assertEquals(1, calls1);
     94   assertEquals(1, calls2);
     95 }
     96 
     97 function testCallbacks4() {
     98   var calls1 = 0;
     99   var calls2 = 0;
    100   var V = {};
    101   function f1(v) {
    102     calls1++;
    103     assertEquals(V, v);
    104   }
    105   function f2(v) {
    106     calls2++;
    107     assertEquals(V, v);
    108   }
    109   var p = new Promise(V);
    110   p.addListener(f1);
    111   p.addListener(f2);
    112   assertEquals(1, calls1);
    113   assertEquals(1, calls2);
    114 }
    115 
    116 function testThisInCallback() {
    117   var calls = 0;
    118   var V = {};
    119   function f(v) {
    120     calls++;
    121     assertEquals(V, v);
    122     assertNotEquals(p, this);
    123   }
    124   var p = new Promise;
    125   p.addListener(f);
    126   p.value = V;
    127   assertEquals(1, calls);
    128 }
    129 
    130 function testPending() {
    131   var p = new Promise;
    132   assertEquals(undefined, p.value);
    133   assertFalse(p.done);
    134 }
    135 
    136 function testValueCanBeUndefined() {
    137   var p = new Promise;
    138   p.value = undefined;
    139   assertEquals(undefined, p.value);
    140   assertTrue(p.done);
    141 }
    142 
    143 function testDone() {
    144   var p = new Promise;
    145   assertFalse(p.done);
    146   p.value = 42;
    147   assertTrue(p.done);
    148 }
    149 
    150 function testWhen() {
    151   const V = {};
    152   var calls = 0;
    153   var p = new Promise;
    154   p.value = V;
    155   Promise.when(p, function(v) {
    156     assertEquals(V, v);
    157     calls++;
    158   });
    159   assertEquals(1, calls);
    160 }
    161 
    162 function testWhen2() {
    163   const V = {};
    164   var calls = 0;
    165   var p = new Promise;
    166   Promise.when(p, function(v) {
    167     assertEquals(V, v);
    168     calls++;
    169   });
    170   p.value = V;
    171   assertEquals(1, calls);
    172 }
    173 
    174 function testWait() {
    175   const S = {};
    176   var p = Promise.wait(1000, S);
    177   assertFalse(p.done);
    178   mockClock.tick(500);
    179   assertFalse(p.done);
    180   mockClock.tick(500);
    181   assertTrue(p.done);
    182   assertEquals(S, p.value);
    183 }
    184 
    185 function testAny() {
    186   var p1 = new Promise;
    187   var p2 = new Promise;
    188   var p3 = new Promise;
    189 
    190   var any = Promise.any(p1, p2, p3);
    191   p2.value = 2;
    192   assertEquals(2, any.value);
    193   p1.value = 1;
    194   assertEquals(2, any.value);
    195 }
    196 
    197 function testAll() {
    198   var p1 = new Promise;
    199   var p2 = new Promise;
    200   var p3 = new Promise;
    201 
    202   var pAll = Promise.all(p1, p2, p3);
    203   p1.value = 1;
    204   p2.value = 2;
    205   p3.value = 3;
    206   assertArrayEquals([1, 2, 3], pAll.value);
    207 }
    208 
    209 function testAllEmpty() {
    210   var pAll = Promise.all();
    211   assertArrayEquals([], pAll.value);
    212 }
    213 
    214 function testAllAlreadyDone() {
    215   var p1 = new Promise(1);
    216   var p2 = new Promise(2);
    217   var p3 = new Promise(3);
    218 
    219   var pAll = Promise.all(p1, p2, p3);
    220   assertArrayEquals([1, 2, 3], pAll.value);
    221 }
    222 
    223 function testEvent() {
    224   var p = Promise.event(document.body, 'foo');
    225   var e = new cr.Event('foo');
    226   document.body.dispatchEvent(e);
    227   assertEquals(e, p.value);
    228 }
    229 
    230 function testToString() {
    231   var p1 = new Promise;
    232   assertEquals('[object Promise]', String(p1));
    233 
    234   var p2 = new Promise;
    235   p2.value = 'Hello world';
    236   assertEquals('Hello world', String(p2));
    237 }
    238 
    239 function testValueOf() {
    240   var p = new Promise;
    241   p.value = 42;
    242 
    243   assertTrue(p < 43);
    244   assertTrue(p > 41);
    245   assertTrue(p == 42);
    246 }
    247 
    248 </script>
    249 </body>
    250 </html>
    251