Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / webapp / base / js / base_inherits_unittest.js
blobca9881429451341fcada8c7484b4e14fa6efa3ee
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.
5 'use strict';
7 QUnit.module('base.inherits', {
8 beforeEach: function() {
9 base_inherits.setupTestClasses();
11 });
13 var base_inherits = {};
15 base_inherits.setupTestClasses = function() {
17 /**
18 * @constructor
19 * @extends {base_inherits.ChildClass}
21 base_inherits.GrandChildClass = function() {
22 base.inherits(this, base_inherits.ChildClass);
23 this.name = 'grandChild';
26 /**
27 * @param {string} arg
28 * @return {string}
30 base_inherits.GrandChildClass.prototype.overrideMethod = function(arg) {
31 return 'overrideMethod - grandChild - ' + arg;
34 /**
35 * @constructor
36 * @extends {base_inherits.ParentClass}
38 base_inherits.ChildClass = function() {
39 base.inherits(this, base_inherits.ParentClass, 'parentArg');
40 this.name = 'child';
41 this.childOnly = 'childOnly';
44 /**
45 * @param {string} arg
46 * @return {string}
48 base_inherits.ChildClass.prototype.overrideMethod = function(arg) {
49 return 'overrideMethod - child - ' + arg;
52 /** @return {string} */
53 base_inherits.ChildClass.prototype.childMethod = function() {
54 return 'childMethod';
57 /**
58 * @param {string} arg
59 * @constructor
61 base_inherits.ParentClass = function(arg) {
62 /** @type {string} */
63 this.name = 'parent';
64 /** @type {string} */
65 this.parentOnly = 'parentOnly';
66 /** @type {string} */
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';
79 /**
80 * @param {string} arg
81 * @return {string}
83 base_inherits.ParentClass.prototype.overrideMethod = function(arg) {
84 return 'overrideMethod - parent - ' + arg;
89 QUnit.test('should invoke parent constructor with the correct arguments',
90 function(assert) {
91 var child = new base_inherits.ChildClass();
92 assert.equal(child.parentConstructorArg, 'parentArg');
93 });
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');
99 });
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',
146 function(assert) {
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);