Index: third_party/WebKit/LayoutTests/resources/bluetooth/bluetooth-helpers.js |
diff --git a/third_party/WebKit/LayoutTests/resources/bluetooth/bluetooth-helpers.js b/third_party/WebKit/LayoutTests/resources/bluetooth/bluetooth-helpers.js |
index a73cbc6a3df0e6f3a0301f3dddeaeb8cd32f8b9b..4128c6c22c280dcc068c5c20b5b4b5d551c1a219 100644 |
--- a/third_party/WebKit/LayoutTests/resources/bluetooth/bluetooth-helpers.js |
+++ b/third_party/WebKit/LayoutTests/resources/bluetooth/bluetooth-helpers.js |
@@ -263,39 +263,62 @@ function eventPromise(target, type, options) { |
}); |
} |
-// Creates |num_listeners| promises. Each adds an event listener |
-// to object. The promises resolve once the object fires |event| but |
-// reject if the event is fired before |object|.|func|() resolves. |
-// Returns a promise that fulfills with the result of |object|.|func()| |
-// and |event.target.value| of each of the other promises. |
-// If |ignore_event_promise_order| is set true, this function will ignore |
-// the relative order of the event and the promise; otherwise it will assert |
-// the event is triggered after the promise is resolved. |
-function assert_event_fires_after_promise(object, func, event, num_listeners, ignore_event_promise_order) { |
- num_listeners = num_listeners !== undefined ? num_listeners : 1; |
- |
- if (object[func] === undefined) { |
- return Promise.reject('Function \'' + func + '\' not available in object.'); |
- } |
- let should_resolve = false; |
- let event_promises = []; |
+ |
+ |
+function assert_promise_event_order_(should_be_first, object, func, event, num_listeners) { |
scheib
2017/04/21 06:32:12
comment the parameters.. especially should_be_firs
ortuno
2017/04/26 05:56:19
Moved the comment here.
|
+ let order = []; |
+ let event_promises = [] |
for (let i = 0; i < num_listeners; i++) { |
- event_promises.push(new Promise((resolve, reject) => { |
+ event_promises.push(new Promise(resolve => { |
let event_listener = (e) => { |
object.removeEventListener(event, event_listener); |
- if (should_resolve || ignore_event_promise_order) { |
- resolve(e.target.value); |
- } else { |
- reject(event + ' was triggered before the promise resolved.'); |
- } |
- }; |
+ order.push('event'); |
+ resolve(e.target.value); |
+ } |
object.addEventListener(event, event_listener); |
})); |
} |
- return object[func]().then(result => { |
- should_resolve = true; |
- return Promise.all([result, ...event_promises]); |
+ |
+ let func_promise = object[func]().then(result => { |
+ order.push('promiseresolved'); |
+ return result; |
}); |
+ |
+ return Promise.all([func_promise, ...event_promises]) |
+ .then((result) => { |
+ if (should_be_first !== order[0]) { |
+ throw should_be_first === 'event' ? |
+ `'${event}' was fired before promise resolved.` : |
scheib
2017/04/21 06:32:12
I think the error messages are swapped:
e.g. the c
ortuno
2017/04/26 05:56:19
Fixed.
|
+ `Promise resolved before '${event}' was fired.`; |
+ } |
+ |
+ if (order[0] !== 'promiseresolved' && |
+ order[order.length - 1] !== 'promiseresolved') { |
+ throw 'Promise resolved in between event listeners.'; |
+ } |
+ |
+ return result; |
+ }); |
+} |
+ |
+// Attaches |num_listeners| |event| listeners to |object|. If all events have |
+// been fired after the promise resolved, returns a promise that fulfills with |
+// the result of |object|.|func()| and |event.target.value| of each of event |
+// listeners. Otherwise throws an error. |
+function assert_event_fires_after_promise( |
+ object, func, event, num_listeners=1) { |
+ return assert_promise_event_order_( |
+ 'promiseresolved', object, func, event, num_listeners); |
+} |
+ |
+// Attaches |num_listeners| |event| listeners to |object|. If all events have |
+// been fired before the promise resolved, returns a promise that fulfills with |
+// the result of |object|.|func()| and |event.target.value| of each of the |
+// event listeners. Otherwise throws an error. |
+function assert_event_fires_before_promise( |
+ object, func, event, num_listeners=1) { |
+ return assert_promise_event_order_( |
+ 'event', object, func, event, num_listeners); |
} |
// Returns a promise that resolves after 100ms unless |