1 // Copyright 2015 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.
7 QUnit
.module('base.inherits', {
8 beforeEach: function() {
9 base_inherits
.setupTestClasses();
13 var base_inherits
= {};
15 base_inherits
.setupTestClasses = function() {
19 * @extends {base_inherits.ChildClass}
21 base_inherits
.GrandChildClass = function() {
22 base
.inherits(this, base_inherits
.ChildClass
);
23 this.name
= 'grandChild';
30 base_inherits
.GrandChildClass
.prototype.overrideMethod = function(arg
) {
31 return 'overrideMethod - grandChild - ' + arg
;
36 * @extends {base_inherits.ParentClass}
38 base_inherits
.ChildClass = function() {
39 base
.inherits(this, base_inherits
.ParentClass
, 'parentArg');
41 this.childOnly
= 'childOnly';
48 base_inherits
.ChildClass
.prototype.overrideMethod = function(arg
) {
49 return 'overrideMethod - child - ' + arg
;
52 /** @return {string} */
53 base_inherits
.ChildClass
.prototype.childMethod = function() {
61 base_inherits
.ParentClass = function(arg
) {
65 this.parentOnly
= 'parentOnly';
67 this.parentConstructorArg
= arg
;
69 // Record the parent method so that we can ensure that it is available in
70 // the parent constructor regardless of what type of object |this| points to.
71 this.parentMethodDuringCtor
= this.parentMethod
;
74 /** @return {string} */
75 base_inherits
.ParentClass
.prototype.parentMethod = function() {
76 return 'parentMethod';
83 base_inherits
.ParentClass
.prototype.overrideMethod = function(arg
) {
84 return 'overrideMethod - parent - ' + arg
;
89 QUnit
.test('should invoke parent constructor with the correct arguments',
91 var child
= new base_inherits
.ChildClass();
92 assert
.equal(child
.parentConstructorArg
, 'parentArg');
95 QUnit
.test('should preserve parent property and method', function(assert
) {
96 var child
= new base_inherits
.ChildClass();
97 assert
.equal(child
.parentOnly
, 'parentOnly');
98 assert
.equal(child
.parentMethod(), 'parentMethod');
101 QUnit
.test('should preserve instanceof', function(assert
) {
102 var child
= new base_inherits
.ChildClass();
103 var grandChild
= new base_inherits
.GrandChildClass();
104 assert
.ok(child
instanceof base_inherits
.ParentClass
);
105 assert
.ok(child
instanceof base_inherits
.ChildClass
);
106 assert
.ok(grandChild
instanceof base_inherits
.ParentClass
);
107 assert
.ok(grandChild
instanceof base_inherits
.ChildClass
);
108 assert
.ok(grandChild
instanceof base_inherits
.GrandChildClass
);
111 QUnit
.test('should override parent property and method', function(assert
) {
112 var child
= new base_inherits
.ChildClass();
113 assert
.equal(child
.name
, 'child');
114 assert
.equal(child
.overrideMethod('123'), 'overrideMethod - child - 123');
115 assert
.equal(child
.childOnly
, 'childOnly');
116 assert
.equal(child
.childMethod(), 'childMethod');
119 QUnit
.test('should work on an inheritance chain', function(assert
) {
120 var grandChild
= new base_inherits
.GrandChildClass();
121 assert
.equal(grandChild
.name
, 'grandChild');
122 assert
.equal(grandChild
.overrideMethod('246'),
123 'overrideMethod - grandChild - 246');
124 assert
.equal(grandChild
.childOnly
, 'childOnly');
125 assert
.equal(grandChild
.childMethod(), 'childMethod');
126 assert
.equal(grandChild
.parentOnly
, 'parentOnly');
127 assert
.equal(grandChild
.parentMethod(), 'parentMethod');
130 QUnit
.test('should be able to access parent class methods', function(assert
) {
131 var grandChild
= new base_inherits
.GrandChildClass();
133 assert
.equal(grandChild
.overrideMethod('789'),
134 'overrideMethod - grandChild - 789');
136 var childMethod
= base_inherits
.ChildClass
.prototype.overrideMethod
.
137 call(grandChild
, '81');
138 assert
.equal(childMethod
, 'overrideMethod - child - 81');
140 var parentMethod
= base_inherits
.ParentClass
.prototype.overrideMethod
.
141 call(grandChild
, '4');
142 assert
.equal(parentMethod
, 'overrideMethod - parent - 4');
145 QUnit
.test('parent ctor should have access to parent methods',
147 var child
= new base_inherits
.ChildClass();
148 assert
.ok(!!child
.parentMethodDuringCtor
);
150 var parent
= new base_inherits
.ParentClass('blah');
151 assert
.ok(!!parent
.parentMethodDuringCtor
);