[Extensions] Allow chrome.test.listenOnce() to return a promise
chrome.test.listenOnce() is a useful utility for waiting for an
extension API event to be triggered exactly once (thus not polluting
subsequent tests). For instance, you might do something like:
```
chrome.test.listenOnce(chrome.tabs.onCreated,
function(tab) {
// Verify the created tab.
// This might magically end the test, depending on what else is going
// on at the time.
});
chrome.tabs.create({...});
```
However, it is currently inherently callback-based, and relies upon the
callback ref-counting in the chrome.test infrastructure, which can be
confusing and easy to get wrong. This also doesn't play well with
promises and async / await.
Instead, tweak this to allow returning a promise from listenOnce() if
no function is provided. Now, the above can look like the following:
```
let tabCreated = chrome.test.listenOnce(chrome.tabs.onCreated);
chrome.tabs.create({...});
let tab = await tabCreated;
// Verify the created tab.
chrome.test.succeed(); // Deterministically and clearly end the test.
```
This is simpler and clearer to the reader, as well as reducing the
necessary callback nesting.
This should have no behavior change for existing callers, since they
were all required to pass a callback to chrome.test.listenOnce().
Bug: 480195314
Change-Id: If0113b24d047b786972b94d8f52460c49c5fe352
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7536021
Commit-Queue: Devlin Cronin <rdevlin.cronin@chromium.org>
Reviewed-by: Tim <tjudkins@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1578476}
NOKEYCHECK=True
GitOrigin-RevId: 3a5dfa349b5eb877e8febcd897f9998ab34c8709
1 file changed