Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Unified Diff: extensions/test/data/utils_unittest.js

Issue 481853002: Add support for asynchronously loading modules from the background page. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: extensions/test/data/utils_unittest.js
diff --git a/extensions/test/data/utils_unittest.js b/extensions/test/data/utils_unittest.js
new file mode 100644
index 0000000000000000000000000000000000000000..1ecc42147a3ef7589ba2b18d5dcc30194cc85206
--- /dev/null
+++ b/extensions/test/data/utils_unittest.js
@@ -0,0 +1,137 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+var assert = requireNative('assert');
+var AssertTrue = assert.AssertTrue;
+var AssertFalse = assert.AssertFalse;
+var utils = require('utils');
+
+function testSuperClass() {
+ function SuperClassImpl() {}
+
+ SuperClassImpl.prototype = {
+ attrA: 'aSuper',
+ attrB: 'bSuper',
+ func: function() { return 'func'; },
+ superFunc: function() { return 'superFunc'; }
+ };
+
+ function SubClassImpl() {
+ SuperClassImpl.call(this);
+ }
+
+ SubClassImpl.prototype = {
+ __proto__: SuperClassImpl.prototype,
+ attrA: 'aSub',
+ attrC: 'cSub',
+ func: function() { return 'overridden'; },
+ subFunc: function() { return 'subFunc'; }
+ };
+
+ var SuperClass = utils.expose('SuperClass',
+ SuperClassImpl,
+ { functions: ['func', 'superFunc'],
+ properties: ['attrA', 'attrB'] });
+
+ var SubClass = utils.expose('SubClass',
+ SubClassImpl,
+ { superclass: SuperClass,
+ functions: ['subFunc'],
+ properties: ['attrC'] });
+
+ var supe = new SuperClass();
+ AssertTrue(supe.attrA == 'aSuper');
+ AssertTrue(supe.attrB == 'bSuper');
+ AssertFalse('attrC' in supe);
+ AssertTrue(supe.func() == 'func');
+ AssertTrue('superFunc' in supe);
+ AssertTrue(supe.superFunc() == 'superFunc');
+ AssertFalse('subFunc' in supe);
+ AssertTrue(supe instanceof SuperClass);
+
+ var sub = new SubClass();
+ AssertTrue(sub.attrA == 'aSub');
+ AssertTrue(sub.attrB == 'bSuper');
+ AssertTrue(sub.attrC == 'cSub');
+ AssertTrue(sub.func() == 'overridden');
+ AssertTrue(sub.superFunc() == 'superFunc');
+ AssertTrue('subFunc' in sub);
+ AssertTrue(sub.subFunc() == 'subFunc');
+ AssertTrue(sub instanceof SuperClass);
+ AssertTrue(sub instanceof SubClass);
+
+ function SubSubClassImpl() {}
+ SubSubClassImpl.prototype = Object.create(SubClassImpl.prototype);
+ SubSubClassImpl.prototype.subSubFunc = function() { return 'subsub'; }
+
+ var SubSubClass = utils.expose('SubSubClass',
+ SubSubClassImpl,
+ { superclass: SubClass,
+ functions: ['subSubFunc'] });
+ var subsub = new SubSubClass();
+ AssertTrue(subsub.attrA == 'aSub');
+ AssertTrue(subsub.attrB == 'bSuper');
+ AssertTrue(subsub.attrC == 'cSub');
+ AssertTrue(subsub.func() == 'overridden');
+ AssertTrue(subsub.superFunc() == 'superFunc');
+ AssertTrue(subsub.subFunc() == 'subFunc');
+ AssertTrue(subsub.subSubFunc() == 'subsub');
+ AssertTrue(subsub instanceof SuperClass);
+ AssertTrue(subsub instanceof SubClass);
+ AssertTrue(subsub instanceof SubSubClass);
+}
+
+function fakeApiFunction(shouldSucceed, numberOfResults, callback) {
+ if (shouldSucceed) {
+ var result = [];
+ for (var i = 0; i < numberOfResults; i++) {
+ result.push(i);
+ }
+ $Function.apply(callback, null, result);
+ return;
+ }
+ chrome.runtime.lastError = 'error message';
+ callback();
+ chrome.runtime.lastError = null;
+}
+
+function testPromiseNoResult() {
+ utils.promise(fakeApiFunction, true, 0).then(function(result) {
+ AssertTrue(result === undefined);
+ }).catch(function(e) {
+ AssertFalse(True);
+ });
+}
+
+function testPromiseOneResult() {
+ utils.promise(fakeApiFunction, true, 1).then(function(result) {
+ AssertTrue(result === 0);
+ }).catch(function(e) {
+ AssertFalse(True);
+ });
+}
+
+function testPromiseTwoResults() {
+ utils.promise(fakeApiFunction, true, 2).then(function(result) {
+ AssertTrue(result.length == 2);
+ AssertTrue(result[0] == 0);
+ AssertTrue(result[1] == 1);
+ }).catch(function(e) {
+ AssertFalse(True);
+ });
+}
+
+function testPromiseError() {
+ utils.promise(fakeApiFunction, false, 0).then(function(result) {
+ AssertFalse(True);
+ }).catch(function(e) {
+ AssertTrue(e.message == 'error message');
+ });
+}
+
+exports.testSuperClass = testSuperClass;
+exports.testPromiseNoResult = testPromiseNoResult;
+exports.testPromiseOneResult = testPromiseOneResult;
+exports.testPromiseTwoResults = testPromiseTwoResults;
+exports.testPromiseError = testPromiseError;
« extensions/renderer/resources/utils.js ('K') | « extensions/renderer/utils_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698