Home | History | Annotate | Download | only in src
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 'use strict';
      6 
      7 base.require('ui');
      8 
      9 base.unittest.testSuite('ui', function() {
     10   var TestElement = ui.define('div');
     11   TestElement.prototype = {
     12     __proto__: HTMLDivElement.prototype,
     13 
     14     decorate: function() {
     15       if (!this.decorateCallCount)
     16         this.decorateCallCount = 0;
     17       this.decorateCallCount++;
     18     }
     19   };
     20 
     21   var Base = ui.define('div');
     22   Base.prototype = {
     23     __proto__: HTMLDivElement.prototype,
     24     decorate: function() {
     25       this.decoratedAsBase = true;
     26     },
     27     set baseProperty(v) {
     28       this.basePropertySet = v;
     29     }
     30   };
     31 
     32   test('decorateOnceViaNew', function() {
     33     var testElement = new TestElement();
     34     assertEquals(1, testElement.decorateCallCount);
     35   });
     36 
     37   test('decorateOnceDirectly', function() {
     38     var testElement = document.createElement('div');
     39     ui.decorate(testElement, TestElement);
     40     assertEquals(1, testElement.decorateCallCount);
     41   });
     42 
     43   test('toString', function() {
     44     assertEquals('div', Base.toString());
     45 
     46     var Sub = ui.define('Sub', Base);
     47     assertEquals('div::sub', Sub.toString());
     48 
     49     var SubSub = ui.define('Marine', Sub);
     50     assertEquals('div::sub::marine', SubSub.toString());
     51   });
     52 
     53   test('basicDefines', function() {
     54     var baseInstance = new Base();
     55     assertTrue(baseInstance instanceof Base);
     56     assertTrue(baseInstance.decoratedAsBase);
     57 
     58     baseInstance.basePropertySet = 7;
     59     assertEquals(7, baseInstance.basePropertySet);
     60   });
     61 
     62   test('subclassing', function() {
     63     var Sub = ui.define('Sub', Base);
     64     Sub.prototype = {
     65       __proto__: Base.prototype,
     66       decorate: function() {
     67         this.decoratedAsSub = true;
     68       }
     69     };
     70 
     71     var subInstance = new Sub();
     72     assertTrue(subInstance instanceof Sub);
     73     assertTrue(subInstance.decoratedAsSub);
     74 
     75     assertTrue(subInstance instanceof Base);
     76     assertFalse(subInstance.decoratedAsBase);
     77 
     78     subInstance.baseProperty = true;
     79     assertTrue(subInstance.basePropertySet);
     80   });
     81 
     82   var NoArgs = ui.define('div');
     83   NoArgs.prototype = {
     84     __proto__: HTMLDivElement.prototype,
     85     decorate: function() {
     86       this.noArgsDecorated_ = true;
     87     },
     88     get noArgsDecorated() {
     89       return this.noArgsDecorated_;
     90     }
     91   };
     92 
     93   var Args = ui.define('args', NoArgs);
     94   Args.prototype = {
     95     __proto__: NoArgs.prototype,
     96     decorate: function(first) {
     97       this.first_ = first;
     98       this.argsDecorated_ = true;
     99     },
    100     get first() {
    101       return this.first_;
    102     },
    103     get argsDecorated() {
    104       return this.argsDecorated_;
    105     }
    106   };
    107 
    108   var ArgsChild = ui.define('child', Args);
    109   ArgsChild.prototype = {
    110     __proto__: Args.prototype,
    111     decorate: function(_, second) {
    112       this.second_ = second;
    113       this.argsChildDecorated_ = true;
    114     },
    115     get second() {
    116       return this.second_;
    117     },
    118     get decorated() {
    119       return this.decorated_;
    120     },
    121     get argsChildDecorated() {
    122       return this.argsChildDecorated_ = true;
    123     }
    124   };
    125 
    126   var ArgsDecoratingChild = ui.define('child', Args);
    127   ArgsDecoratingChild.prototype = {
    128     __proto__: Args.prototype,
    129     decorate: function(first, second) {
    130       Args.prototype.decorate.call(this, first);
    131       this.second_ = second;
    132       this.argsDecoratingChildDecorated_ = true;
    133     },
    134     get second() {
    135       return this.second_;
    136     },
    137     get decorated() {
    138       return this.decorated_;
    139     },
    140     get argsDecoratingChildDecorated() {
    141       return this.argsChildDecorated_ = true;
    142     }
    143   };
    144 
    145   test('decorate_noArguments', function() {
    146     var noArgs;
    147     assertDoesNotThrow(function() {
    148       noArgs = new NoArgs();
    149     });
    150     assertTrue(noArgs.noArgsDecorated);
    151   });
    152 
    153   test('decorate_arguments', function() {
    154     var args = new Args('this is first');
    155     assertEquals('this is first', args.first);
    156     assertTrue(args.argsDecorated);
    157     assertFalse(args.noArgsDecorated);
    158   });
    159 
    160   test('decorate_subclassArguments', function() {
    161     var argsChild = new ArgsChild('this is first', 'and second');
    162     assertUndefined(argsChild.first);
    163     assertEquals('and second', argsChild.second);
    164 
    165     assertTrue(argsChild.argsChildDecorated);
    166     assertFalse(argsChild.argsDecorated);
    167     assertFalse(argsChild.noArgsDecorated);
    168   });
    169 
    170   test('decorate_subClassCallsParentDecorate', function() {
    171     var argsDecoratingChild = new ArgsDecoratingChild(
    172         'this is first', 'and second');
    173     assertEquals('this is first', argsDecoratingChild.first);
    174     assertEquals('and second', argsDecoratingChild.second);
    175     assertTrue(argsDecoratingChild.argsDecoratingChildDecorated);
    176     assertTrue(argsDecoratingChild.argsDecorated);
    177     assertFalse(argsDecoratingChild.noArgsDecorated);
    178   });
    179 });
    180