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

Side by Side Diff: third_party/WebKit/LayoutTests/bluetooth/resources/bluetooth-helpers.js

Issue 1382743002: bluetooth: Add characteristicvaluechanged event (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-notifications-1
Patch Set: Address jyasskin's comments Created 5 years, 2 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 unified diff | Download patch
OLDNEW
1 'use strict'; 1 'use strict';
2 2
3 // Sometimes we need to test that using either the name, alias, or UUID 3 // Sometimes we need to test that using either the name, alias, or UUID
4 // produces the same result. The following objects help us do that. 4 // produces the same result. The following objects help us do that.
5 var generic_access = { 5 var generic_access = {
6 alias: 0x1800, 6 alias: 0x1800,
7 name: 'generic_access', 7 name: 'generic_access',
8 uuid: '00001800-0000-1000-8000-00805f9b34fb' 8 uuid: '00001800-0000-1000-8000-00805f9b34fb'
9 }; 9 };
10 var device_name = { 10 var device_name = {
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 } 125 }
126 126
127 function runGarbageCollection() 127 function runGarbageCollection()
128 { 128 {
129 // Run gc() as a promise. 129 // Run gc() as a promise.
130 return new Promise( 130 return new Promise(
131 function(resolve, reject) { 131 function(resolve, reject) {
132 GCController.collect(); 132 GCController.collect();
133 setTimeout(resolve, 0); 133 setTimeout(resolve, 0);
134 }); 134 });
135 return Promise.resolve();
136 } 135 }
136
137 // Creates |num_listeners| promises. Each adds an event listener
138 // to object. The promises resolve once the object fires |event| but
139 // reject if the event is fired before |object|.|func|() resolves.
140 // Returns a promise that fulfills with the result of |object|.|func()|
141 // and |event.target.value| of each of the other promises.
142 function assert_event_fires_after_promise(object, func, event, num_listeners) {
143 num_listeners = num_listeners !== undefined ? num_listeners : 1;
144
145 if (object[func] === undefined) {
146 return Promise.reject('Function \'' + func + '\' not available in object.');
147 }
148 let should_resolve = false;
149 let event_promises = [];
150 for (let i = 0; i < num_listeners; i++) {
151 event_promises.push(new Promise((resolve, reject) => {
152 let event_listener = (e) => {
153 object.removeEventListener(event_listener);
154 if (should_resolve) {
155 resolve(e.target.value);
156 } else {
157 reject(event + ' was triggered before the promise resolved.');
158 }
159 };
160 object.addEventListener(event, event_listener);
161 }));
162 }
163 return object[func]().then(result => {
164 should_resolve = true;
165 return Promise.all([Promise.resolve(result),
Jeffrey Yasskin 2015/10/16 01:40:18 You can omit the "Promise.resolve" here. Arguments
ortuno 2015/10/16 19:41:00 Done.
166 ...event_promises]);
167 });
168 }
169
170 // Returns a promise that resolves after 100ms unless
171 // the the event is fired on the object in which case
172 // the promise rejects.
173 function assert_no_events(object, event_name) {
174 return new Promise((resolve, reject) => {
175 let event_listener = (e) => {
176 object.removeEventListener(event, event_listener);
177 assert_unreached('Object should not fire an event.');
178 };
179 object.addEventListener(event_name, event_listener);
180 setTimeout(resolve, 100);
Jeffrey Yasskin 2015/10/16 01:40:18 And remove the listener after the timeout.
ortuno 2015/10/16 19:41:00 Done.
181 });
182 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698