 Chromium Code Reviews
 Chromium Code Reviews Issue 228783007:
  rAc: make requestAutocomplete() return a promise.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/blink.git@master
    
  
    Issue 228783007:
  rAc: make requestAutocomplete() return a promise.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/blink.git@master| Index: LayoutTests/fast/forms/form-request-autocomplete.html | 
| diff --git a/LayoutTests/fast/forms/form-request-autocomplete.html b/LayoutTests/fast/forms/form-request-autocomplete.html | 
| index 7c3b99a9b245e3a96c71765cf3a804f081602114..1342f0e71d3cd22417d166d0f8e773815cf1ec03 100644 | 
| --- a/LayoutTests/fast/forms/form-request-autocomplete.html | 
| +++ b/LayoutTests/fast/forms/form-request-autocomplete.html | 
| @@ -6,17 +6,29 @@ | 
| jsTestIsAsync = true; | 
| var numErrors = 0; | 
| -var numErrorsExpected = 4; | 
| +var numErrorsExpected = 5; | 
| var eventsBubbleToBody = false; | 
| var eventsBubbleToDocument = false; | 
| var eventsBubbleToWindow = false; | 
| +function debugMsg(msg) | 
| +{ | 
| + debug('DEBUG ' + msg); | 
| +} | 
| + | 
| function runTests() | 
| { | 
| checkDynamicAttributes(); | 
| - checkNonUserGesture(); | 
| checkParsedAttributes(); | 
| checkEventsBubble(); | 
| + checkPromiseRejects(); | 
| 
yhirano
2014/04/17 10:57:48
Can you serialize the tests using Promises?
Otherw
 
Dan Beam
2014/04/17 23:02:04
Done. (order doesn't matter now)
 | 
| + checkNonUserGesture(); | 
| + debugMsg('finished running synchronous code'); | 
| +} | 
| + | 
| +function debugStart() | 
| +{ | 
| + debugMsg(arguments.callee.caller.name + ' running'); | 
| } | 
| function checkForEnumerableProperties(form) | 
| @@ -33,6 +45,8 @@ function checkForEnumerableProperties(form) | 
| function checkDynamicAttributes() | 
| { | 
| + debugStart(); | 
| + | 
| var form = document.createElement('form'); | 
| checkForEnumerableProperties(form); | 
| @@ -41,20 +55,10 @@ function checkDynamicAttributes() | 
| form.requestAutocomplete(); | 
| } | 
| -function checkNonUserGesture() | 
| -{ | 
| - var form = document.createElement('form'); | 
| - checkForEnumerableProperties(form); | 
| - form.onautocompleteerror = errorWithReason('disabled'); | 
| - | 
| - setTimeout(function() | 
| - { | 
| - form.requestAutocomplete(); | 
| - }, 0); | 
| -} | 
| - | 
| function checkParsedAttributes() | 
| { | 
| + debugStart(); | 
| + | 
| var form = document.forms[0]; | 
| if (!form || !form.onautocompleteerror || form.autocomplete != 'off') | 
| testFailed('failed to pick up parsed DOM attributes correctly'); | 
| @@ -65,6 +69,8 @@ function checkParsedAttributes() | 
| function checkEventsBubble() | 
| { | 
| + debugStart(); | 
| + | 
| var form = document.createElement('form'); | 
| form.autocomplete = 'off'; | 
| @@ -92,9 +98,52 @@ function checkEventsBubble() | 
| form.requestAutocomplete(); | 
| } | 
| +function checkPromiseRejects() | 
| +{ | 
| + debugStart(); | 
| + | 
| + var form = document.createElement('form'); | 
| + form.autocomplete = 'off'; | 
| + | 
| + var eventDispatched = false; | 
| + form.onautocompleteerror = function() { | 
| + eventDispatched = true; | 
| + }; | 
| + | 
| + var promise = form.requestAutocomplete(); | 
| + if (promise instanceof Promise) | 
| + testPassed('requestAutocomplete() returns a Promise'); | 
| + else | 
| + testFailed('expected requestAutocomplete() to return a Promise'); | 
| + | 
| + promise.catch(objectWithReason('disabled')); | 
| + promise.catch(function() { | 
| + if (eventDispatched) | 
| + testPassed('Promises were fullfilled after events were dispatched'); | 
| + else | 
| + testFailed('events should be dispatched before Promises are resolved'); | 
| + }); | 
| +} | 
| + | 
| +function checkNonUserGesture() | 
| +{ | 
| + debugStart(); | 
| + | 
| + var form = document.createElement('form'); | 
| + checkForEnumerableProperties(form); | 
| + form.onautocompleteerror = errorWithReason('disabled'); | 
| + | 
| + setTimeout(function() { | 
| + form.requestAutocomplete(); | 
| + }); | 
| +} | 
| + | 
| function errorWithReason(reason) | 
| { | 
| + var from = arguments.callee.caller.name; | 
| return function(event) { | 
| + debugMsg('handling error from ' + from); | 
| + | 
| if (event instanceof AutocompleteErrorEvent) | 
| testPassed('event is an AutocompleteErrorEvent'); | 
| else | 
| @@ -109,20 +158,47 @@ function errorWithReason(reason) | 
| }; | 
| } | 
| +function objectWithReason(reason) | 
| +{ | 
| + var from = arguments.callee.caller.name; | 
| + return function(result) { | 
| + debugMsg('handling error from ' + from); | 
| + | 
| + if (result instanceof Error) | 
| + testPassed('Promise received an Error result'); | 
| + else | 
| + testFailed('expected Promise argument to be an result'); | 
| + | 
| + if ('reason' in result) | 
| + testPassed('Promise result has "reason" key'); | 
| + else | 
| + testFailed('Promise result has no "reason" key'); | 
| + | 
| + if (result.reason == reason) | 
| + testPassed('got expected reason: ' + reason); | 
| + else | 
| + testFailed('wrong reason, expected: ' + reason + ', got: ' + result.error); | 
| + | 
| + onError(); | 
| + }; | 
| +} | 
| + | 
| function onError() | 
| { | 
| numErrors += 1; | 
| - if (numErrors > numErrorsExpected) { | 
| - testFailed('too many error events'); | 
| - } else if (numErrors == numErrorsExpected) { | 
| + if (numErrors == numErrorsExpected) { | 
| if (!eventsBubbleToBody) | 
| testFailed('no events bubbled to body'); | 
| + | 
| if (!eventsBubbleToDocument) | 
| testFailed('no events bubbled to document'); | 
| + | 
| if (!eventsBubbleToWindow) | 
| testFailed('no events bubbled to window'); | 
| + | 
| if (eventsBubbleToBody && eventsBubbleToDocument && eventsBubbleToWindow) | 
| testPassed('events bubbled as expected'); | 
| + | 
| testPassed('got expected number of error events (' + numErrorsExpected + ')'); | 
| finishJSTest(); | 
| } |