1 // Copyright 2014 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 var assert = requireNative('assert');
6 var AssertTrue = assert.AssertTrue;
7 var AssertFalse = assert.AssertFalse;
8 var utils = require('utils');
10 function testSuperClass() {
11 function SuperClassImpl() {}
13 SuperClassImpl.prototype = {
16 func: function() { return 'func'; },
17 superFunc: function() { return 'superFunc'; }
20 function SubClassImpl() {
21 SuperClassImpl.call(this);
24 SubClassImpl.prototype = {
25 __proto__: SuperClassImpl.prototype,
28 func: function() { return 'overridden'; },
29 subFunc: function() { return 'subFunc'; }
32 var SuperClass = utils.expose('SuperClass',
34 { functions: ['func', 'superFunc'],
35 properties: ['attrA', 'attrB'] });
37 var SubClass = utils.expose('SubClass',
39 { superclass: SuperClass,
40 functions: ['subFunc'],
41 properties: ['attrC'] });
43 var supe = new SuperClass();
44 AssertTrue(supe.attrA == 'aSuper');
45 AssertTrue(supe.attrB == 'bSuper');
46 AssertFalse('attrC' in supe);
47 AssertTrue(supe.func() == 'func');
48 AssertTrue('superFunc' in supe);
49 AssertTrue(supe.superFunc() == 'superFunc');
50 AssertFalse('subFunc' in supe);
51 AssertTrue(supe instanceof SuperClass);
53 var sub = new SubClass();
54 AssertTrue(sub.attrA == 'aSub');
55 AssertTrue(sub.attrB == 'bSuper');
56 AssertTrue(sub.attrC == 'cSub');
57 AssertTrue(sub.func() == 'overridden');
58 AssertTrue(sub.superFunc() == 'superFunc');
59 AssertTrue('subFunc' in sub);
60 AssertTrue(sub.subFunc() == 'subFunc');
61 AssertTrue(sub instanceof SuperClass);
62 AssertTrue(sub instanceof SubClass);
64 function SubSubClassImpl() {}
65 SubSubClassImpl.prototype = Object.create(SubClassImpl.prototype);
66 SubSubClassImpl.prototype.subSubFunc = function() { return 'subsub'; }
68 var SubSubClass = utils.expose('SubSubClass',
70 { superclass: SubClass,
71 functions: ['subSubFunc'] });
72 var subsub = new SubSubClass();
73 AssertTrue(subsub.attrA == 'aSub');
74 AssertTrue(subsub.attrB == 'bSuper');
75 AssertTrue(subsub.attrC == 'cSub');
76 AssertTrue(subsub.func() == 'overridden');
77 AssertTrue(subsub.superFunc() == 'superFunc');
78 AssertTrue(subsub.subFunc() == 'subFunc');
79 AssertTrue(subsub.subSubFunc() == 'subsub');
80 AssertTrue(subsub instanceof SuperClass);
81 AssertTrue(subsub instanceof SubClass);
82 AssertTrue(subsub instanceof SubSubClass);
85 function fakeApiFunction(shouldSucceed, numberOfResults, callback) {
88 for (var i = 0; i < numberOfResults; i++) {
91 $Function.apply(callback, null, result);
94 chrome.runtime.lastError = 'error message';
96 chrome.runtime.lastError = null;
99 function testPromiseNoResult() {
100 utils.promise(fakeApiFunction, true, 0).then(function(result) {
101 AssertTrue(result === undefined);
102 }).catch(function(e) {
107 function testPromiseOneResult() {
108 utils.promise(fakeApiFunction, true, 1).then(function(result) {
109 AssertTrue(result === 0);
110 }).catch(function(e) {
115 function testPromiseTwoResults() {
116 utils.promise(fakeApiFunction, true, 2).then(function(result) {
117 AssertTrue(result.length == 2);
118 AssertTrue(result[0] == 0);
119 AssertTrue(result[1] == 1);
120 }).catch(function(e) {
125 function testPromiseError() {
126 utils.promise(fakeApiFunction, false, 0).then(function(result) {
128 }).catch(function(e) {
129 AssertTrue(e.message == 'error message');
133 exports.testSuperClass = testSuperClass;
134 exports.testPromiseNoResult = testPromiseNoResult;
135 exports.testPromiseOneResult = testPromiseOneResult;
136 exports.testPromiseTwoResults = testPromiseTwoResults;
137 exports.testPromiseError = testPromiseError;