Commit 2bcda774 authored by Devlin Cronin's avatar Devlin Cronin Committed by Commit Bot

[Extensions Bindings] Remove utils.promise

utils.promise had a few different issues, and it's only used in tests.
Just remove it.

Bug: 745020
Change-Id: Ic6306103d5d17f5cceb371f4b1f8362e7234b27f
Reviewed-on: https://chromium-review.googlesource.com/575518Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Commit-Queue: Devlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487358}
parent a7fe98c0
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
// found in the LICENSE file. // found in the LICENSE file.
var nativeDeepCopy = requireNative('utils').deepCopy; var nativeDeepCopy = requireNative('utils').deepCopy;
var DCHECK = requireNative('logging').DCHECK;
var WARNING = requireNative('logging').WARNING;
var logActivity = requireNative('activityLogger'); var logActivity = requireNative('activityLogger');
var exceptionHandler = require('uncaught_exception_handler'); var exceptionHandler = require('uncaught_exception_handler');
...@@ -190,38 +188,6 @@ function deepCopy(value) { ...@@ -190,38 +188,6 @@ function deepCopy(value) {
return nativeDeepCopy(value); return nativeDeepCopy(value);
} }
/**
* Wrap an asynchronous API call to a function |func| in a promise. The
* remaining arguments will be passed to |func|. Returns a promise that will be
* resolved to the result passed to the callback or rejected if an error occurs
* (if chrome.runtime.lastError is set). If there are multiple results, the
* promise will be resolved with an array containing those results.
*
* For example,
* promise(chrome.storage.get, 'a').then(function(result) {
* // Use result.
* }).catch(function(error) {
* // Report error.message.
* });
*/
function promise(func) {
var args = $Array.slice(arguments, 1);
DCHECK(typeof func == 'function');
return new Promise(function(resolve, reject) {
args.push(function() {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError));
return;
}
if (arguments.length <= 1)
resolve(arguments[0]);
else
resolve($Array.slice(arguments));
});
$Function.apply(func, null, args);
});
}
// DO NOT USE. This causes problems with safe builtins, and makes migration to // DO NOT USE. This causes problems with safe builtins, and makes migration to
// native bindings more difficult. // native bindings more difficult.
function handleRequestWithPromiseDoNotUse( function handleRequestWithPromiseDoNotUse(
...@@ -259,6 +225,5 @@ exports.$set('lookup', lookup); ...@@ -259,6 +225,5 @@ exports.$set('lookup', lookup);
exports.$set('defineProperty', defineProperty); exports.$set('defineProperty', defineProperty);
exports.$set('expose', expose); exports.$set('expose', expose);
exports.$set('deepCopy', deepCopy); exports.$set('deepCopy', deepCopy);
exports.$set('promise', promise);
exports.$set('handleRequestWithPromiseDoNotUse', exports.$set('handleRequestWithPromiseDoNotUse',
handleRequestWithPromiseDoNotUse); handleRequestWithPromiseDoNotUse);
...@@ -59,21 +59,5 @@ TEST_F(UtilsUnittest, SuperClass) { ...@@ -59,21 +59,5 @@ TEST_F(UtilsUnittest, SuperClass) {
RunTest("testSuperClass"); RunTest("testSuperClass");
} }
TEST_F(UtilsUnittest, PromiseNoResult) {
RunTestWithPromises("testPromiseNoResult");
}
TEST_F(UtilsUnittest, PromiseOneResult) {
RunTestWithPromises("testPromiseOneResult");
}
TEST_F(UtilsUnittest, PromiseTwoResults) {
RunTestWithPromises("testPromiseTwoResults");
}
TEST_F(UtilsUnittest, PromiseError) {
RunTestWithPromises("testPromiseError");
}
} // namespace } // namespace
} // namespace extensions } // namespace extensions
...@@ -28,17 +28,27 @@ binding.registerCustomHook(function(bindingsAPI) { ...@@ -28,17 +28,27 @@ binding.registerCustomHook(function(bindingsAPI) {
}); });
var apiFunction = binding.generate().getDevices; var apiFunction = binding.generate().getDevices;
function runFunction() {
apiFunction(function() {
if (shouldSucceed)
test.assertNoLastError();
else
test.assertTrue(!!chrome.runtime.lastError);
test.succeed();
});
}
unittestBindings.exportTests([ unittestBindings.exportTests([
// Test that a keep alive is created and destroyed for a successful API call. // Test that a keep alive is created and destroyed for a successful API call.
function testKeepAliveWithSuccessfulCall() { function testKeepAliveWithSuccessfulCall() {
shouldSucceed = true; shouldSucceed = true;
utils.promise(apiFunction).then(test.succeed, test.fail); runFunction();
}, },
// Test that a keep alive is created and destroyed for an unsuccessful API // Test that a keep alive is created and destroyed for an unsuccessful API
// call. // call.
function testKeepAliveWithError() { function testKeepAliveWithError() {
shouldSucceed = false; shouldSucceed = false;
utils.promise(apiFunction).then(test.fail, test.succeed); runFunction();
}, },
], test.runTests, exports); ], test.runTests, exports);
...@@ -92,56 +92,4 @@ function testSuperClass() { ...@@ -92,56 +92,4 @@ function testSuperClass() {
AssertTrue(subsub instanceof SubSubClass); 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.testSuperClass = testSuperClass;
exports.testPromiseNoResult = testPromiseNoResult;
exports.testPromiseOneResult = testPromiseOneResult;
exports.testPromiseTwoResults = testPromiseTwoResults;
exports.testPromiseError = testPromiseError;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment