selenium-webdriver 2.48.2 → 2.53.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (534) hide show
  1. package/CHANGES.md +116 -0
  2. package/NOTICE +1 -1
  3. package/README.md +59 -9
  4. package/builder.js +375 -358
  5. package/chrome.js +549 -586
  6. package/edge.js +378 -0
  7. package/error.js +9 -6
  8. package/example/google_search.js +1 -1
  9. package/example/google_search_generator.js +2 -4
  10. package/example/logging.js +6 -2
  11. package/example/sample.js +21 -0
  12. package/executors.js +11 -27
  13. package/firefox/binary.js +149 -98
  14. package/firefox/extension.js +15 -19
  15. package/firefox/index.js +256 -163
  16. package/firefox/profile.js +223 -243
  17. package/http/index.js +487 -81
  18. package/http/util.js +40 -36
  19. package/ie.js +288 -309
  20. package/index.js +34 -125
  21. package/io/exec.js +58 -39
  22. package/io/index.js +72 -60
  23. package/lib/README +4 -0
  24. package/lib/actions.js +596 -0
  25. package/lib/by.js +277 -0
  26. package/lib/capabilities.js +450 -0
  27. package/lib/{webdriver/command.js → command.js} +91 -72
  28. package/lib/{webdriver/button.js → devmode.js} +13 -8
  29. package/lib/error.js +690 -0
  30. package/lib/events.js +212 -0
  31. package/lib/firefox/amd64/libnoblur64.so +0 -0
  32. package/lib/firefox/i386/libnoblur.so +0 -0
  33. package/lib/firefox/webdriver.json +5 -1
  34. package/lib/firefox/webdriver.xpi +0 -0
  35. package/lib/{webdriver/key.js → input.js} +84 -2
  36. package/lib/logging.js +670 -0
  37. package/lib/{webdriver/promise.js → promise.js} +817 -644
  38. package/lib/safari/client.js +266 -214
  39. package/lib/session.js +80 -0
  40. package/lib/{webdriver/serializable.js → symbols.js} +16 -19
  41. package/lib/test/build.js +4 -4
  42. package/lib/test/data/click_tests/html5_submit_buttons.html +2 -1
  43. package/lib/test/data/click_tests/overlapping_elements.html +15 -4
  44. package/lib/test/data/click_tests/partially_overlapping_elements.html +124 -0
  45. package/lib/test/data/click_tests/wrapped_overlapping_elements.html +13 -0
  46. package/lib/test/data/hidden_partially.html +45 -0
  47. package/lib/test/data/readOnlyPage.html +1 -1
  48. package/lib/test/data/selectPage.html +18 -2
  49. package/lib/test/fileserver.js +3 -4
  50. package/lib/test/index.js +7 -3
  51. package/lib/test/resources.js +1 -1
  52. package/lib/until.js +376 -0
  53. package/lib/webdriver.js +2440 -0
  54. package/net/index.js +4 -0
  55. package/net/portprober.js +16 -12
  56. package/opera.js +295 -314
  57. package/package.json +6 -5
  58. package/phantomjs.js +144 -147
  59. package/proxy.js +10 -6
  60. package/remote/index.js +289 -238
  61. package/safari.js +326 -311
  62. package/test/chrome/options_test.js +11 -9
  63. package/test/element_finding_test.js +51 -13
  64. package/test/execute_script_test.js +17 -3
  65. package/test/firefox/firefox_test.js +0 -32
  66. package/test/http/http_test.js +671 -61
  67. package/test/http/util_test.js +25 -23
  68. package/test/io_test.js +67 -1
  69. package/test/lib/by_test.js +139 -0
  70. package/test/lib/capabilities_test.js +111 -0
  71. package/test/lib/error_test.js +325 -0
  72. package/test/lib/events_test.js +177 -0
  73. package/test/lib/logging_test.js +272 -0
  74. package/test/{promise_aplus_test.js → lib/promise_aplus_test.js} +4 -13
  75. package/test/lib/promise_error_test.js +808 -0
  76. package/test/lib/promise_flow_test.js +2282 -0
  77. package/test/lib/promise_generator_test.js +308 -0
  78. package/test/lib/promise_test.js +996 -0
  79. package/test/lib/testutil.js +90 -0
  80. package/test/lib/until_test.js +400 -0
  81. package/test/lib/webdriver_test.js +2157 -0
  82. package/test/page_loading_test.js +7 -9
  83. package/test/remote_test.js +50 -3
  84. package/test/session_test.js +53 -0
  85. package/test/stale_element_test.js +2 -2
  86. package/test/testing/assert_test.js +374 -0
  87. package/test/testing/index_test.js +9 -11
  88. package/testing/assert.js +334 -17
  89. package/testing/index.js +12 -2
  90. package/_base.js +0 -200
  91. package/docs/Changes.html +0 -356
  92. package/docs/class_bot_Error.html +0 -15
  93. package/docs/class_webdriver_ActionSequence.html +0 -115
  94. package/docs/class_webdriver_Alert.html +0 -24
  95. package/docs/class_webdriver_AlertPromise.html +0 -92
  96. package/docs/class_webdriver_Capabilities.html +0 -60
  97. package/docs/class_webdriver_Command.html +0 -15
  98. package/docs/class_webdriver_EventEmitter.html +0 -35
  99. package/docs/class_webdriver_FileDetector.html +0 -21
  100. package/docs/class_webdriver_Locator.html +0 -12
  101. package/docs/class_webdriver_Serializable.html +0 -9
  102. package/docs/class_webdriver_Session.html +0 -13
  103. package/docs/class_webdriver_TouchSequence.html +0 -49
  104. package/docs/class_webdriver_UnhandledAlertError.html +0 -18
  105. package/docs/class_webdriver_WebDriver.html +0 -313
  106. package/docs/class_webdriver_WebDriver_Logs.html +0 -15
  107. package/docs/class_webdriver_WebDriver_Navigation.html +0 -16
  108. package/docs/class_webdriver_WebDriver_Options.html +0 -41
  109. package/docs/class_webdriver_WebDriver_TargetLocator.html +0 -38
  110. package/docs/class_webdriver_WebDriver_Timeouts.html +0 -29
  111. package/docs/class_webdriver_WebDriver_Window.html +0 -27
  112. package/docs/class_webdriver_WebElement.html +0 -223
  113. package/docs/class_webdriver_WebElementPromise.html +0 -274
  114. package/docs/class_webdriver_http_Executor.html +0 -22
  115. package/docs/class_webdriver_http_Request.html +0 -12
  116. package/docs/class_webdriver_http_Response.html +0 -13
  117. package/docs/class_webdriver_logging_Entry.html +0 -15
  118. package/docs/class_webdriver_logging_Level.html +0 -55
  119. package/docs/class_webdriver_logging_LogRecord.html +0 -45
  120. package/docs/class_webdriver_logging_Logger.html +0 -119
  121. package/docs/class_webdriver_logging_Preferences.html +0 -8
  122. package/docs/class_webdriver_promise_CancellationError.html +0 -17
  123. package/docs/class_webdriver_promise_ControlFlow.html +0 -153
  124. package/docs/class_webdriver_promise_Deferred.html +0 -84
  125. package/docs/class_webdriver_promise_MultipleUnhandledRejectionError.html +0 -14
  126. package/docs/class_webdriver_promise_Promise.html +0 -75
  127. package/docs/class_webdriver_stacktrace_Frame.html +0 -20
  128. package/docs/class_webdriver_stacktrace_Snapshot.html +0 -6
  129. package/docs/class_webdriver_testing_Assertion.html +0 -99
  130. package/docs/class_webdriver_testing_Assertion_DelegatingMatcher_.html +0 -13
  131. package/docs/class_webdriver_testing_ContainsMatcher.html +0 -11
  132. package/docs/class_webdriver_testing_NegatedAssertion.html +0 -97
  133. package/docs/class_webdriver_until_Condition.html +0 -7
  134. package/docs/dossier.css +0 -1
  135. package/docs/dossier.js +0 -86
  136. package/docs/enum_bot_ErrorCode.html +0 -3
  137. package/docs/enum_bot_Error_State.html +0 -2
  138. package/docs/enum_webdriver_Browser.html +0 -2
  139. package/docs/enum_webdriver_Button.html +0 -2
  140. package/docs/enum_webdriver_Capability.html +0 -33
  141. package/docs/enum_webdriver_CommandName.html +0 -3
  142. package/docs/enum_webdriver_Key.html +0 -12
  143. package/docs/enum_webdriver_logging_Type.html +0 -6
  144. package/docs/enum_webdriver_promise_ControlFlow_EventType.html +0 -9
  145. package/docs/index.html +0 -125
  146. package/docs/interface_webdriver_CommandExecutor.html +0 -9
  147. package/docs/interface_webdriver_http_Client.html +0 -10
  148. package/docs/interface_webdriver_promise_Thenable.html +0 -77
  149. package/docs/module_selenium-webdriver.html +0 -23
  150. package/docs/module_selenium-webdriver__base.html +0 -23
  151. package/docs/module_selenium-webdriver__base_class_Context.html +0 -5
  152. package/docs/module_selenium-webdriver_builder.html +0 -2
  153. package/docs/module_selenium-webdriver_builder_class_Builder.html +0 -138
  154. package/docs/module_selenium-webdriver_chrome.html +0 -90
  155. package/docs/module_selenium-webdriver_chrome_class_Driver.html +0 -280
  156. package/docs/module_selenium-webdriver_chrome_class_Options.html +0 -127
  157. package/docs/module_selenium-webdriver_chrome_class_ServiceBuilder.html +0 -46
  158. package/docs/module_selenium-webdriver_class_ActionSequence.html +0 -115
  159. package/docs/module_selenium-webdriver_class_Builder.html +0 -138
  160. package/docs/module_selenium-webdriver_class_Capabilities.html +0 -60
  161. package/docs/module_selenium-webdriver_class_Command.html +0 -15
  162. package/docs/module_selenium-webdriver_class_EventEmitter.html +0 -35
  163. package/docs/module_selenium-webdriver_class_FileDetector.html +0 -21
  164. package/docs/module_selenium-webdriver_class_Serializable.html +0 -9
  165. package/docs/module_selenium-webdriver_class_Session.html +0 -13
  166. package/docs/module_selenium-webdriver_class_WebDriver.html +0 -313
  167. package/docs/module_selenium-webdriver_class_WebElement.html +0 -223
  168. package/docs/module_selenium-webdriver_class_WebElementPromise.html +0 -274
  169. package/docs/module_selenium-webdriver_enum_Browser.html +0 -2
  170. package/docs/module_selenium-webdriver_enum_Button.html +0 -2
  171. package/docs/module_selenium-webdriver_enum_Capability.html +0 -33
  172. package/docs/module_selenium-webdriver_enum_CommandName.html +0 -3
  173. package/docs/module_selenium-webdriver_enum_Key.html +0 -12
  174. package/docs/module_selenium-webdriver_error.html +0 -4
  175. package/docs/module_selenium-webdriver_error_class_Error.html +0 -15
  176. package/docs/module_selenium-webdriver_error_enum_ErrorCode.html +0 -3
  177. package/docs/module_selenium-webdriver_executors.html +0 -10
  178. package/docs/module_selenium-webdriver_executors_class_DeferredExecutor.html +0 -12
  179. package/docs/module_selenium-webdriver_firefox.html +0 -68
  180. package/docs/module_selenium-webdriver_firefox_binary.html +0 -5
  181. package/docs/module_selenium-webdriver_firefox_binary_class_Binary.html +0 -20
  182. package/docs/module_selenium-webdriver_firefox_class_Binary.html +0 -20
  183. package/docs/module_selenium-webdriver_firefox_class_Driver.html +0 -276
  184. package/docs/module_selenium-webdriver_firefox_class_Options.html +0 -19
  185. package/docs/module_selenium-webdriver_firefox_class_Profile.html +0 -50
  186. package/docs/module_selenium-webdriver_firefox_extension.html +0 -8
  187. package/docs/module_selenium-webdriver_firefox_profile.html +0 -13
  188. package/docs/module_selenium-webdriver_firefox_profile_class_Profile.html +0 -50
  189. package/docs/module_selenium-webdriver_http.html +0 -9
  190. package/docs/module_selenium-webdriver_http_class_Executor.html +0 -22
  191. package/docs/module_selenium-webdriver_http_class_HttpClient.html +0 -16
  192. package/docs/module_selenium-webdriver_http_class_Request.html +0 -12
  193. package/docs/module_selenium-webdriver_http_class_Response.html +0 -13
  194. package/docs/module_selenium-webdriver_http_util.html +0 -17
  195. package/docs/module_selenium-webdriver_ie.html +0 -11
  196. package/docs/module_selenium-webdriver_ie_class_Driver.html +0 -274
  197. package/docs/module_selenium-webdriver_ie_class_Options.html +0 -89
  198. package/docs/module_selenium-webdriver_ie_enum_Level.html +0 -1
  199. package/docs/module_selenium-webdriver_io.html +0 -37
  200. package/docs/module_selenium-webdriver_io_exec.html +0 -6
  201. package/docs/module_selenium-webdriver_namespace_By.html +0 -56
  202. package/docs/module_selenium-webdriver_namespace_error.html +0 -4
  203. package/docs/module_selenium-webdriver_namespace_logging.html +0 -18
  204. package/docs/module_selenium-webdriver_namespace_promise.html +0 -163
  205. package/docs/module_selenium-webdriver_namespace_stacktrace.html +0 -17
  206. package/docs/module_selenium-webdriver_namespace_until.html +0 -82
  207. package/docs/module_selenium-webdriver_net.html +0 -7
  208. package/docs/module_selenium-webdriver_net_portprober.html +0 -12
  209. package/docs/module_selenium-webdriver_opera.html +0 -62
  210. package/docs/module_selenium-webdriver_opera_class_Driver.html +0 -276
  211. package/docs/module_selenium-webdriver_opera_class_Options.html +0 -44
  212. package/docs/module_selenium-webdriver_opera_class_ServiceBuilder.html +0 -35
  213. package/docs/module_selenium-webdriver_phantomjs.html +0 -2
  214. package/docs/module_selenium-webdriver_phantomjs_class_Driver.html +0 -299
  215. package/docs/module_selenium-webdriver_proxy.html +0 -28
  216. package/docs/module_selenium-webdriver_remote.html +0 -8
  217. package/docs/module_selenium-webdriver_remote_class_DriverService.html +0 -32
  218. package/docs/module_selenium-webdriver_remote_class_FileDetector.html +0 -22
  219. package/docs/module_selenium-webdriver_remote_class_SeleniumServer.html +0 -41
  220. package/docs/module_selenium-webdriver_safari.html +0 -9
  221. package/docs/module_selenium-webdriver_safari_class_Driver.html +0 -279
  222. package/docs/module_selenium-webdriver_safari_class_Options.html +0 -23
  223. package/docs/module_selenium-webdriver_testing.html +0 -74
  224. package/docs/module_selenium-webdriver_testing_assert.html +0 -9
  225. package/docs/namespace_bot.html +0 -4
  226. package/docs/namespace_bot_json.html +0 -20
  227. package/docs/namespace_bot_response.html +0 -18
  228. package/docs/namespace_bot_userAgent.html +0 -33
  229. package/docs/namespace_webdriver.html +0 -30
  230. package/docs/namespace_webdriver_By.html +0 -56
  231. package/docs/namespace_webdriver_http.html +0 -6
  232. package/docs/namespace_webdriver_logging.html +0 -70
  233. package/docs/namespace_webdriver_promise.html +0 -688
  234. package/docs/namespace_webdriver_stacktrace.html +0 -17
  235. package/docs/namespace_webdriver_testing.html +0 -7
  236. package/docs/namespace_webdriver_testing_assert.html +0 -9
  237. package/docs/namespace_webdriver_testing_asserts.html +0 -18
  238. package/docs/namespace_webdriver_until.html +0 -82
  239. package/docs/source/_base.js.src.html +0 -1
  240. package/docs/source/builder.js.src.html +0 -1
  241. package/docs/source/chrome.js.src.html +0 -1
  242. package/docs/source/error.js.src.html +0 -1
  243. package/docs/source/executors.js.src.html +0 -1
  244. package/docs/source/firefox/binary.js.src.html +0 -1
  245. package/docs/source/firefox/extension.js.src.html +0 -1
  246. package/docs/source/firefox/index.js.src.html +0 -1
  247. package/docs/source/firefox/profile.js.src.html +0 -1
  248. package/docs/source/http/index.js.src.html +0 -1
  249. package/docs/source/http/util.js.src.html +0 -1
  250. package/docs/source/ie.js.src.html +0 -1
  251. package/docs/source/index.js.src.html +0 -1
  252. package/docs/source/io/exec.js.src.html +0 -1
  253. package/docs/source/io/index.js.src.html +0 -1
  254. package/docs/source/lib/atoms/error.js.src.html +0 -1
  255. package/docs/source/lib/atoms/json.js.src.html +0 -1
  256. package/docs/source/lib/atoms/response.js.src.html +0 -1
  257. package/docs/source/lib/atoms/userAgent.js.src.html +0 -1
  258. package/docs/source/lib/goog/array/array.js.src.html +0 -1
  259. package/docs/source/lib/goog/asserts/asserts.js.src.html +0 -1
  260. package/docs/source/lib/goog/async/freelist.js.src.html +0 -1
  261. package/docs/source/lib/goog/async/nexttick.js.src.html +0 -1
  262. package/docs/source/lib/goog/async/run.js.src.html +0 -1
  263. package/docs/source/lib/goog/async/workqueue.js.src.html +0 -1
  264. package/docs/source/lib/goog/base.js.src.html +0 -1
  265. package/docs/source/lib/goog/debug/debug.js.src.html +0 -1
  266. package/docs/source/lib/goog/debug/entrypointregistry.js.src.html +0 -1
  267. package/docs/source/lib/goog/debug/error.js.src.html +0 -1
  268. package/docs/source/lib/goog/debug/logbuffer.js.src.html +0 -1
  269. package/docs/source/lib/goog/debug/logger.js.src.html +0 -1
  270. package/docs/source/lib/goog/debug/logrecord.js.src.html +0 -1
  271. package/docs/source/lib/goog/deps.js.src.html +0 -1
  272. package/docs/source/lib/goog/disposable/disposable.js.src.html +0 -1
  273. package/docs/source/lib/goog/disposable/idisposable.js.src.html +0 -1
  274. package/docs/source/lib/goog/dom/browserfeature.js.src.html +0 -1
  275. package/docs/source/lib/goog/dom/dom.js.src.html +0 -1
  276. package/docs/source/lib/goog/dom/nodetype.js.src.html +0 -1
  277. package/docs/source/lib/goog/dom/safe.js.src.html +0 -1
  278. package/docs/source/lib/goog/dom/tagname.js.src.html +0 -1
  279. package/docs/source/lib/goog/dom/tags.js.src.html +0 -1
  280. package/docs/source/lib/goog/dom/vendor.js.src.html +0 -1
  281. package/docs/source/lib/goog/events/browserevent.js.src.html +0 -1
  282. package/docs/source/lib/goog/events/browserfeature.js.src.html +0 -1
  283. package/docs/source/lib/goog/events/event.js.src.html +0 -1
  284. package/docs/source/lib/goog/events/eventid.js.src.html +0 -1
  285. package/docs/source/lib/goog/events/events.js.src.html +0 -1
  286. package/docs/source/lib/goog/events/eventtarget.js.src.html +0 -1
  287. package/docs/source/lib/goog/events/eventtype.js.src.html +0 -1
  288. package/docs/source/lib/goog/events/keycodes.js.src.html +0 -1
  289. package/docs/source/lib/goog/events/listenable.js.src.html +0 -1
  290. package/docs/source/lib/goog/events/listener.js.src.html +0 -1
  291. package/docs/source/lib/goog/events/listenermap.js.src.html +0 -1
  292. package/docs/source/lib/goog/fs/url.js.src.html +0 -1
  293. package/docs/source/lib/goog/functions/functions.js.src.html +0 -1
  294. package/docs/source/lib/goog/html/safehtml.js.src.html +0 -1
  295. package/docs/source/lib/goog/html/safescript.js.src.html +0 -1
  296. package/docs/source/lib/goog/html/safestyle.js.src.html +0 -1
  297. package/docs/source/lib/goog/html/safestylesheet.js.src.html +0 -1
  298. package/docs/source/lib/goog/html/safeurl.js.src.html +0 -1
  299. package/docs/source/lib/goog/html/trustedresourceurl.js.src.html +0 -1
  300. package/docs/source/lib/goog/html/uncheckedconversions.js.src.html +0 -1
  301. package/docs/source/lib/goog/i18n/bidi.js.src.html +0 -1
  302. package/docs/source/lib/goog/iter/iter.js.src.html +0 -1
  303. package/docs/source/lib/goog/json/json.js.src.html +0 -1
  304. package/docs/source/lib/goog/labs/testing/assertthat.js.src.html +0 -1
  305. package/docs/source/lib/goog/labs/testing/logicmatcher.js.src.html +0 -1
  306. package/docs/source/lib/goog/labs/testing/matcher.js.src.html +0 -1
  307. package/docs/source/lib/goog/labs/testing/numbermatcher.js.src.html +0 -1
  308. package/docs/source/lib/goog/labs/testing/objectmatcher.js.src.html +0 -1
  309. package/docs/source/lib/goog/labs/testing/stringmatcher.js.src.html +0 -1
  310. package/docs/source/lib/goog/labs/useragent/browser.js.src.html +0 -1
  311. package/docs/source/lib/goog/labs/useragent/engine.js.src.html +0 -1
  312. package/docs/source/lib/goog/labs/useragent/platform.js.src.html +0 -1
  313. package/docs/source/lib/goog/labs/useragent/util.js.src.html +0 -1
  314. package/docs/source/lib/goog/log/log.js.src.html +0 -1
  315. package/docs/source/lib/goog/math/box.js.src.html +0 -1
  316. package/docs/source/lib/goog/math/coordinate.js.src.html +0 -1
  317. package/docs/source/lib/goog/math/math.js.src.html +0 -1
  318. package/docs/source/lib/goog/math/rect.js.src.html +0 -1
  319. package/docs/source/lib/goog/math/size.js.src.html +0 -1
  320. package/docs/source/lib/goog/net/wrapperxmlhttpfactory.js.src.html +0 -1
  321. package/docs/source/lib/goog/net/xhrlike.js.src.html +0 -1
  322. package/docs/source/lib/goog/net/xmlhttp.js.src.html +0 -1
  323. package/docs/source/lib/goog/net/xmlhttpfactory.js.src.html +0 -1
  324. package/docs/source/lib/goog/object/object.js.src.html +0 -1
  325. package/docs/source/lib/goog/promise/promise.js.src.html +0 -1
  326. package/docs/source/lib/goog/promise/resolver.js.src.html +0 -1
  327. package/docs/source/lib/goog/promise/thenable.js.src.html +0 -1
  328. package/docs/source/lib/goog/reflect/reflect.js.src.html +0 -1
  329. package/docs/source/lib/goog/string/const.js.src.html +0 -1
  330. package/docs/source/lib/goog/string/string.js.src.html +0 -1
  331. package/docs/source/lib/goog/string/typedstring.js.src.html +0 -1
  332. package/docs/source/lib/goog/structs/collection.js.src.html +0 -1
  333. package/docs/source/lib/goog/structs/map.js.src.html +0 -1
  334. package/docs/source/lib/goog/structs/set.js.src.html +0 -1
  335. package/docs/source/lib/goog/structs/structs.js.src.html +0 -1
  336. package/docs/source/lib/goog/style/style.js.src.html +0 -1
  337. package/docs/source/lib/goog/testing/asserts.js.src.html +0 -1
  338. package/docs/source/lib/goog/testing/events/events.js.src.html +0 -1
  339. package/docs/source/lib/goog/testing/functionmock.js.src.html +0 -1
  340. package/docs/source/lib/goog/testing/jsunit.js.src.html +0 -1
  341. package/docs/source/lib/goog/testing/loosemock.js.src.html +0 -1
  342. package/docs/source/lib/goog/testing/mock.js.src.html +0 -1
  343. package/docs/source/lib/goog/testing/mockclock.js.src.html +0 -1
  344. package/docs/source/lib/goog/testing/mockcontrol.js.src.html +0 -1
  345. package/docs/source/lib/goog/testing/mockinterface.js.src.html +0 -1
  346. package/docs/source/lib/goog/testing/mockmatchers.js.src.html +0 -1
  347. package/docs/source/lib/goog/testing/objectpropertystring.js.src.html +0 -1
  348. package/docs/source/lib/goog/testing/propertyreplacer.js.src.html +0 -1
  349. package/docs/source/lib/goog/testing/recordfunction.js.src.html +0 -1
  350. package/docs/source/lib/goog/testing/stacktrace.js.src.html +0 -1
  351. package/docs/source/lib/goog/testing/strictmock.js.src.html +0 -1
  352. package/docs/source/lib/goog/testing/testcase.js.src.html +0 -1
  353. package/docs/source/lib/goog/testing/testrunner.js.src.html +0 -1
  354. package/docs/source/lib/goog/uri/uri.js.src.html +0 -1
  355. package/docs/source/lib/goog/uri/utils.js.src.html +0 -1
  356. package/docs/source/lib/goog/useragent/product.js.src.html +0 -1
  357. package/docs/source/lib/goog/useragent/product_isversion.js.src.html +0 -1
  358. package/docs/source/lib/goog/useragent/useragent.js.src.html +0 -1
  359. package/docs/source/lib/webdriver/actionsequence.js.src.html +0 -1
  360. package/docs/source/lib/webdriver/button.js.src.html +0 -1
  361. package/docs/source/lib/webdriver/capabilities.js.src.html +0 -1
  362. package/docs/source/lib/webdriver/command.js.src.html +0 -1
  363. package/docs/source/lib/webdriver/events.js.src.html +0 -1
  364. package/docs/source/lib/webdriver/http/http.js.src.html +0 -1
  365. package/docs/source/lib/webdriver/key.js.src.html +0 -1
  366. package/docs/source/lib/webdriver/locators.js.src.html +0 -1
  367. package/docs/source/lib/webdriver/logging.js.src.html +0 -1
  368. package/docs/source/lib/webdriver/promise.js.src.html +0 -1
  369. package/docs/source/lib/webdriver/serializable.js.src.html +0 -1
  370. package/docs/source/lib/webdriver/session.js.src.html +0 -1
  371. package/docs/source/lib/webdriver/stacktrace.js.src.html +0 -1
  372. package/docs/source/lib/webdriver/testing/asserts.js.src.html +0 -1
  373. package/docs/source/lib/webdriver/touchsequence.js.src.html +0 -1
  374. package/docs/source/lib/webdriver/until.js.src.html +0 -1
  375. package/docs/source/lib/webdriver/webdriver.js.src.html +0 -1
  376. package/docs/source/net/index.js.src.html +0 -1
  377. package/docs/source/net/portprober.js.src.html +0 -1
  378. package/docs/source/opera.js.src.html +0 -1
  379. package/docs/source/phantomjs.js.src.html +0 -1
  380. package/docs/source/proxy.js.src.html +0 -1
  381. package/docs/source/remote/index.js.src.html +0 -1
  382. package/docs/source/safari.js.src.html +0 -1
  383. package/docs/source/testing/assert.js.src.html +0 -1
  384. package/docs/source/testing/index.js.src.html +0 -1
  385. package/docs/types.js +0 -1
  386. package/lib/atoms/error.js +0 -202
  387. package/lib/atoms/json.js +0 -80
  388. package/lib/atoms/response.js +0 -110
  389. package/lib/atoms/userAgent.js +0 -257
  390. package/lib/goog/LICENSE +0 -176
  391. package/lib/goog/array/array.js +0 -1654
  392. package/lib/goog/asserts/asserts.js +0 -365
  393. package/lib/goog/async/freelist.js +0 -88
  394. package/lib/goog/async/nexttick.js +0 -241
  395. package/lib/goog/async/run.js +0 -133
  396. package/lib/goog/async/workqueue.js +0 -139
  397. package/lib/goog/base.js +0 -2548
  398. package/lib/goog/debug/debug.js +0 -653
  399. package/lib/goog/debug/entrypointregistry.js +0 -158
  400. package/lib/goog/debug/error.js +0 -63
  401. package/lib/goog/debug/logbuffer.js +0 -148
  402. package/lib/goog/debug/logger.js +0 -873
  403. package/lib/goog/debug/logrecord.js +0 -242
  404. package/lib/goog/deps.js +0 -151
  405. package/lib/goog/disposable/disposable.js +0 -307
  406. package/lib/goog/disposable/idisposable.js +0 -45
  407. package/lib/goog/dom/browserfeature.js +0 -72
  408. package/lib/goog/dom/dom.js +0 -2991
  409. package/lib/goog/dom/nodetype.js +0 -48
  410. package/lib/goog/dom/safe.js +0 -347
  411. package/lib/goog/dom/tagname.js +0 -160
  412. package/lib/goog/dom/tags.js +0 -41
  413. package/lib/goog/dom/vendor.js +0 -96
  414. package/lib/goog/events/browserevent.js +0 -400
  415. package/lib/goog/events/browserfeature.js +0 -85
  416. package/lib/goog/events/event.js +0 -143
  417. package/lib/goog/events/eventid.js +0 -47
  418. package/lib/goog/events/events.js +0 -984
  419. package/lib/goog/events/eventtarget.js +0 -394
  420. package/lib/goog/events/eventtype.js +0 -237
  421. package/lib/goog/events/keycodes.js +0 -427
  422. package/lib/goog/events/listenable.js +0 -335
  423. package/lib/goog/events/listener.js +0 -131
  424. package/lib/goog/events/listenermap.js +0 -308
  425. package/lib/goog/fs/url.js +0 -109
  426. package/lib/goog/functions/functions.js +0 -442
  427. package/lib/goog/html/safehtml.js +0 -757
  428. package/lib/goog/html/safescript.js +0 -234
  429. package/lib/goog/html/safestyle.js +0 -442
  430. package/lib/goog/html/safestylesheet.js +0 -276
  431. package/lib/goog/html/safeurl.js +0 -398
  432. package/lib/goog/html/trustedresourceurl.js +0 -224
  433. package/lib/goog/html/uncheckedconversions.js +0 -232
  434. package/lib/goog/i18n/bidi.js +0 -908
  435. package/lib/goog/iter/iter.js +0 -1302
  436. package/lib/goog/json/json.js +0 -363
  437. package/lib/goog/labs/testing/assertthat.js +0 -58
  438. package/lib/goog/labs/testing/logicmatcher.js +0 -212
  439. package/lib/goog/labs/testing/matcher.js +0 -80
  440. package/lib/goog/labs/testing/numbermatcher.js +0 -346
  441. package/lib/goog/labs/testing/objectmatcher.js +0 -317
  442. package/lib/goog/labs/testing/stringmatcher.js +0 -415
  443. package/lib/goog/labs/useragent/browser.js +0 -341
  444. package/lib/goog/labs/useragent/engine.js +0 -160
  445. package/lib/goog/labs/useragent/platform.js +0 -160
  446. package/lib/goog/labs/useragent/util.js +0 -148
  447. package/lib/goog/log/log.js +0 -197
  448. package/lib/goog/math/box.js +0 -398
  449. package/lib/goog/math/coordinate.js +0 -268
  450. package/lib/goog/math/math.js +0 -451
  451. package/lib/goog/math/rect.js +0 -476
  452. package/lib/goog/math/size.js +0 -227
  453. package/lib/goog/net/wrapperxmlhttpfactory.js +0 -71
  454. package/lib/goog/net/xhrlike.js +0 -124
  455. package/lib/goog/net/xmlhttp.js +0 -246
  456. package/lib/goog/net/xmlhttpfactory.js +0 -67
  457. package/lib/goog/object/object.js +0 -686
  458. package/lib/goog/promise/promise.js +0 -1350
  459. package/lib/goog/promise/resolver.js +0 -48
  460. package/lib/goog/promise/thenable.js +0 -126
  461. package/lib/goog/reflect/reflect.js +0 -77
  462. package/lib/goog/string/const.js +0 -182
  463. package/lib/goog/string/string.js +0 -1599
  464. package/lib/goog/string/typedstring.js +0 -48
  465. package/lib/goog/structs/collection.js +0 -56
  466. package/lib/goog/structs/map.js +0 -457
  467. package/lib/goog/structs/set.js +0 -279
  468. package/lib/goog/structs/structs.js +0 -354
  469. package/lib/goog/style/style.js +0 -2037
  470. package/lib/goog/testing/asserts.js +0 -1278
  471. package/lib/goog/testing/events/events.js +0 -767
  472. package/lib/goog/testing/functionmock.js +0 -175
  473. package/lib/goog/testing/jsunit.js +0 -162
  474. package/lib/goog/testing/loosemock.js +0 -242
  475. package/lib/goog/testing/mock.js +0 -645
  476. package/lib/goog/testing/mockclock.js +0 -606
  477. package/lib/goog/testing/mockcontrol.js +0 -220
  478. package/lib/goog/testing/mockinterface.js +0 -45
  479. package/lib/goog/testing/mockmatchers.js +0 -400
  480. package/lib/goog/testing/objectpropertystring.js +0 -68
  481. package/lib/goog/testing/propertyreplacer.js +0 -262
  482. package/lib/goog/testing/recordfunction.js +0 -215
  483. package/lib/goog/testing/stacktrace.js +0 -594
  484. package/lib/goog/testing/strictmock.js +0 -130
  485. package/lib/goog/testing/testcase.js +0 -1629
  486. package/lib/goog/testing/testrunner.js +0 -440
  487. package/lib/goog/uri/uri.js +0 -1524
  488. package/lib/goog/uri/utils.js +0 -1064
  489. package/lib/goog/useragent/product.js +0 -183
  490. package/lib/goog/useragent/product_isversion.js +0 -144
  491. package/lib/goog/useragent/useragent.js +0 -572
  492. package/lib/webdriver/actionsequence.js +0 -356
  493. package/lib/webdriver/builder.js +0 -207
  494. package/lib/webdriver/capabilities.js +0 -412
  495. package/lib/webdriver/events.js +0 -179
  496. package/lib/webdriver/firefoxdomexecutor.js +0 -175
  497. package/lib/webdriver/http/corsclient.js +0 -133
  498. package/lib/webdriver/http/http.js +0 -525
  499. package/lib/webdriver/http/xhrclient.js +0 -67
  500. package/lib/webdriver/locators.js +0 -257
  501. package/lib/webdriver/logging.js +0 -347
  502. package/lib/webdriver/session.js +0 -74
  503. package/lib/webdriver/stacktrace.js +0 -619
  504. package/lib/webdriver/test/builder_test.js +0 -53
  505. package/lib/webdriver/test/capabilities_test.js +0 -72
  506. package/lib/webdriver/test/events_test.js +0 -203
  507. package/lib/webdriver/test/http/corsclient_test.js +0 -151
  508. package/lib/webdriver/test/http/http_test.js +0 -428
  509. package/lib/webdriver/test/http/xhrclient_test.js +0 -196
  510. package/lib/webdriver/test/locators_test.js +0 -59
  511. package/lib/webdriver/test/logging_test.js +0 -85
  512. package/lib/webdriver/test/promise_error_test.js +0 -892
  513. package/lib/webdriver/test/promise_flow_test.js +0 -2365
  514. package/lib/webdriver/test/promise_generator_test.js +0 -323
  515. package/lib/webdriver/test/promise_test.js +0 -2012
  516. package/lib/webdriver/test/stacktrace_test.js +0 -480
  517. package/lib/webdriver/test/test_bootstrap.js +0 -67
  518. package/lib/webdriver/test/testing/asserts_test.js +0 -117
  519. package/lib/webdriver/test/testing/client_test.js +0 -88
  520. package/lib/webdriver/test/testing/testcase_test.js +0 -229
  521. package/lib/webdriver/test/testutil.js +0 -209
  522. package/lib/webdriver/test/testutil_test.js +0 -104
  523. package/lib/webdriver/test/until_test.js +0 -411
  524. package/lib/webdriver/test/webdriver_generator_test.js +0 -92
  525. package/lib/webdriver/test/webdriver_test.js +0 -2369
  526. package/lib/webdriver/testing/asserts.js +0 -487
  527. package/lib/webdriver/testing/client.js +0 -179
  528. package/lib/webdriver/testing/jsunit.js +0 -363
  529. package/lib/webdriver/testing/testcase.js +0 -168
  530. package/lib/webdriver/testing/window.js +0 -244
  531. package/lib/webdriver/touchsequence.js +0 -248
  532. package/lib/webdriver/until.js +0 -412
  533. package/lib/webdriver/webdriver.js +0 -2511
  534. package/test/_base_test.js +0 -129
@@ -0,0 +1,2282 @@
1
+ // Licensed to the Software Freedom Conservancy (SFC) under one
2
+ // or more contributor license agreements. See the NOTICE file
3
+ // distributed with this work for additional information
4
+ // regarding copyright ownership. The SFC licenses this file
5
+ // to you under the Apache License, Version 2.0 (the
6
+ // "License"); you may not use this file except in compliance
7
+ // with the License. You may obtain a copy of the License at
8
+ //
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ //
11
+ // Unless required by applicable law or agreed to in writing,
12
+ // software distributed under the License is distributed on an
13
+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ // KIND, either express or implied. See the License for the
15
+ // specific language governing permissions and limitations
16
+ // under the License.
17
+
18
+ 'use strict';
19
+
20
+ const assert = require('assert');
21
+ const fail = assert.fail;
22
+ const sinon = require('sinon');
23
+
24
+ const testutil = require('./testutil');
25
+ const promise = require('../../lib/promise');
26
+
27
+ const NativePromise = Promise;
28
+
29
+ // Aliases for readability.
30
+ const StubError = testutil.StubError;
31
+ const assertIsStubError = testutil.assertIsStubError;
32
+ const callbackPair = testutil.callbackPair;
33
+ const throwStubError = testutil.throwStubError;
34
+
35
+ describe('promise control flow', function() {
36
+ let flow, flowHistory, messages, uncaughtExceptions;
37
+
38
+ beforeEach(function setUp() {
39
+ promise.LONG_STACK_TRACES = false;
40
+ flow = new promise.ControlFlow();
41
+ promise.setDefaultFlow(flow);
42
+ messages = [];
43
+ flowHistory = [];
44
+
45
+ uncaughtExceptions = [];
46
+ flow.on(promise.ControlFlow.EventType.UNCAUGHT_EXCEPTION,
47
+ onUncaughtException);
48
+ });
49
+
50
+ afterEach(function tearDown() {
51
+ flow.removeAllListeners(
52
+ promise.ControlFlow.EventType.UNCAUGHT_EXCEPTION);
53
+ assert.deepEqual([], uncaughtExceptions,
54
+ 'There were uncaught exceptions');
55
+ flow.reset();
56
+ promise.LONG_STACK_TRACES = false;
57
+ });
58
+
59
+ function onUncaughtException(e) {
60
+ uncaughtExceptions.push(e);
61
+ }
62
+
63
+ function waitForAbort(opt_flow) {
64
+ var theFlow = opt_flow || flow;
65
+ theFlow.removeAllListeners(
66
+ promise.ControlFlow.EventType.UNCAUGHT_EXCEPTION);
67
+ return new NativePromise(function(fulfill, reject) {
68
+ theFlow.once(promise.ControlFlow.EventType.IDLE, function() {
69
+ reject(Error('expected flow to report an unhandled error'));
70
+ });
71
+ theFlow.once(
72
+ promise.ControlFlow.EventType.UNCAUGHT_EXCEPTION,
73
+ fulfill);
74
+ });
75
+ }
76
+
77
+ function waitForIdle(opt_flow) {
78
+ var theFlow = opt_flow || flow;
79
+ return new NativePromise(function(fulfill, reject) {
80
+ theFlow.once(promise.ControlFlow.EventType.IDLE, fulfill);
81
+ theFlow.once(
82
+ promise.ControlFlow.EventType.UNCAUGHT_EXCEPTION, reject);
83
+ });
84
+ }
85
+
86
+ function timeout(ms) {
87
+ return new NativePromise(function(fulfill) {
88
+ setTimeout(fulfill, ms);
89
+ });
90
+ }
91
+
92
+
93
+ function schedule(msg, opt_return) {
94
+ return scheduleAction(msg, function() {
95
+ return opt_return;
96
+ });
97
+ }
98
+
99
+ /**
100
+ * @param {string} value The value to push.
101
+ * @param {promise.Promise=} opt_taskPromise Promise to return from
102
+ * the task.
103
+ * @return {!promise.Promise} The result.
104
+ */
105
+ function schedulePush(value, opt_taskPromise) {
106
+ return scheduleAction(value, function() {
107
+ messages.push(value);
108
+ return opt_taskPromise;
109
+ });
110
+ }
111
+
112
+ /**
113
+ * @param {string} msg Debug message.
114
+ * @param {!Function} actionFn The function.
115
+ * @return {!promise.Promise} The function result.
116
+ */
117
+ function scheduleAction(msg, actionFn) {
118
+ return promise.controlFlow().execute(function() {
119
+ flowHistory.push(msg);
120
+ return actionFn();
121
+ }, msg);
122
+ }
123
+
124
+ /**
125
+ * @param {!Function} condition The condition function.
126
+ * @param {number=} opt_timeout The timeout.
127
+ * @param {string=} opt_message Optional message.
128
+ * @return {!promise.Promise} The wait result.
129
+ */
130
+ function scheduleWait(condition, opt_timeout, opt_message) {
131
+ var msg = opt_message || '';
132
+ // It's not possible to hook into when the wait itself is scheduled, so
133
+ // we record each iteration of the wait loop.
134
+ var count = 0;
135
+ return promise.controlFlow().wait(function() {
136
+ flowHistory.push((count++) + ': ' + msg);
137
+ return condition();
138
+ }, opt_timeout, msg);
139
+ }
140
+
141
+ function asyncRun(fn, opt_self) {
142
+ NativePromise.resolve().then(() => fn.call(opt_self));
143
+ }
144
+
145
+ function assertFlowHistory(var_args) {
146
+ var expected = Array.prototype.slice.call(arguments, 0);
147
+ assert.deepEqual(expected, flowHistory);
148
+ }
149
+
150
+ function assertMessages(var_args) {
151
+ var expected = Array.prototype.slice.call(arguments, 0);
152
+ assert.deepEqual(expected, messages);
153
+ }
154
+
155
+ function assertingMessages(var_args) {
156
+ var args = Array.prototype.slice.call(arguments, 0);
157
+ return () => assertMessages.apply(null, args);
158
+ }
159
+
160
+ function assertFlowIs(flow) {
161
+ assert.equal(flow, promise.controlFlow());
162
+ }
163
+
164
+ describe('testScheduling', function() {
165
+ it('aSimpleFunction', function() {
166
+ schedule('go');
167
+ return waitForIdle().then(function() {
168
+ assertFlowHistory('go');
169
+ });
170
+ });
171
+
172
+ it('aSimpleFunctionWithANonPromiseReturnValue', function() {
173
+ schedule('go', 123).then(function(value) {
174
+ assert.equal(123, value);
175
+ });
176
+ return waitForIdle().then(function() {
177
+ assertFlowHistory('go');
178
+ });
179
+ });
180
+
181
+ it('aSimpleSequence', function() {
182
+ schedule('a');
183
+ schedule('b');
184
+ schedule('c');
185
+ return waitForIdle().then(function() {
186
+ assertFlowHistory('a', 'b', 'c');
187
+ });
188
+ });
189
+
190
+ it('invokesCallbacksWhenTaskIsDone', function() {
191
+ var d = new promise.Deferred();
192
+ var called = false;
193
+ var done = schedule('a', d.promise).then(function(value) {
194
+ called = true;
195
+ assert.equal(123, value);
196
+ });
197
+ return timeout(5).then(function() {
198
+ assert.ok(!called);
199
+ d.fulfill(123);
200
+ return done;
201
+ }).
202
+ then(function() {
203
+ assertFlowHistory('a');
204
+ });
205
+ });
206
+
207
+ it('blocksUntilPromiseReturnedByTaskIsResolved', function() {
208
+ var done = promise.defer();
209
+ schedulePush('a', done.promise);
210
+ schedulePush('b');
211
+ setTimeout(function() {
212
+ done.fulfill();
213
+ messages.push('c');
214
+ }, 25);
215
+ return waitForIdle().then(assertingMessages('a', 'c', 'b'));
216
+ });
217
+
218
+ it('waitsForReturnedPromisesToResolve', function() {
219
+ var d1 = new promise.Deferred();
220
+ var d2 = new promise.Deferred();
221
+
222
+ var callback = sinon.spy();
223
+ schedule('a', d1.promise).then(callback);
224
+
225
+ return timeout(5).then(function() {
226
+ assert(!callback.called);
227
+ d1.fulfill(d2);
228
+ return timeout(5);
229
+ }).then(function() {
230
+ assert(!callback.called);
231
+ d2.fulfill('fluffy bunny');
232
+ return waitForIdle();
233
+ }).then(function() {
234
+ assert(callback.called);
235
+ assert.equal('fluffy bunny', callback.getCall(0).args[0]);
236
+ assertFlowHistory('a');
237
+ });
238
+ });
239
+
240
+ it('executesTasksInAFutureTurnAfterTheyAreScheduled', function() {
241
+ var count = 0;
242
+ function incr() { count++; }
243
+
244
+ scheduleAction('', incr);
245
+ assert.equal(0, count);
246
+ return waitForIdle().then(function() {
247
+ assert.equal(1, count);
248
+ });
249
+ });
250
+
251
+ it('executesOneTaskPerTurnOfTheEventLoop', function() {
252
+ var order = [];
253
+ function go() {
254
+ order.push(order.length / 2);
255
+ asyncRun(function() {
256
+ order.push('-');
257
+ });
258
+ }
259
+
260
+ scheduleAction('', go);
261
+ scheduleAction('', go);
262
+ return waitForIdle().then(function() {
263
+ assert.deepEqual([0, '-', 1, '-'], order);
264
+ })
265
+ });
266
+
267
+ it('firstScheduledTaskIsWithinACallback', function() {
268
+ promise.fulfilled().then(function() {
269
+ schedule('a');
270
+ schedule('b');
271
+ schedule('c');
272
+ }).then(function() {
273
+ assertFlowHistory('a', 'b', 'c');
274
+ });
275
+ return waitForIdle();
276
+ });
277
+
278
+ it('newTasksAddedWhileWaitingOnTaskReturnedPromise1', function() {
279
+ scheduleAction('a', function() {
280
+ var d = promise.defer();
281
+ setTimeout(function() {
282
+ schedule('c');
283
+ d.fulfill();
284
+ }, 10);
285
+ return d.promise;
286
+ });
287
+ schedule('b');
288
+ return waitForIdle().then(function() {
289
+ assertFlowHistory('a', 'b', 'c');
290
+ });
291
+ });
292
+
293
+ it('newTasksAddedWhileWaitingOnTaskReturnedPromise2', function() {
294
+ scheduleAction('a', function() {
295
+ var d = promise.defer();
296
+ setTimeout(function() {
297
+ schedule('c');
298
+ asyncRun(d.fulfill);
299
+ }, 10);
300
+ return d.promise;
301
+ });
302
+ schedule('b');
303
+ return waitForIdle().then(function() {
304
+ assertFlowHistory('a', 'c', 'b');
305
+ });
306
+ });
307
+ });
308
+
309
+ describe('testFraming', function() {
310
+ it('callbacksRunInANewFrame', function() {
311
+ schedule('a').then(function() {
312
+ schedule('c');
313
+ });
314
+ schedule('b');
315
+ return waitForIdle().then(function() {
316
+ assertFlowHistory('a', 'c', 'b');
317
+ });
318
+ });
319
+
320
+ it('lotsOfNesting', function() {
321
+ schedule('a').then(function() {
322
+ schedule('c').then(function() {
323
+ schedule('e').then(function() {
324
+ schedule('g');
325
+ });
326
+ schedule('f');
327
+ });
328
+ schedule('d');
329
+ });
330
+ schedule('b');
331
+
332
+ return waitForIdle().then(function() {
333
+ assertFlowHistory('a', 'c', 'e', 'g', 'f', 'd', 'b');
334
+ });
335
+ });
336
+
337
+ it('callbackReturnsPromiseThatDependsOnATask_1', function() {
338
+ schedule('a').then(function() {
339
+ schedule('b');
340
+ return promise.delayed(5).then(function() {
341
+ return schedule('c');
342
+ });
343
+ });
344
+ schedule('d');
345
+
346
+ return waitForIdle().then(function() {
347
+ assertFlowHistory('a', 'b', 'c', 'd');
348
+ });
349
+ });
350
+
351
+ it('callbackReturnsPromiseThatDependsOnATask_2', function() {
352
+ schedule('a').then(function() {
353
+ schedule('b');
354
+ return promise.delayed(5).
355
+ then(function() { return promise.delayed(5) }).
356
+ then(function() { return promise.delayed(5) }).
357
+ then(function() { return promise.delayed(5) }).
358
+ then(function() { return schedule('c'); });
359
+ });
360
+ schedule('d');
361
+
362
+ return waitForIdle().then(function() {
363
+ assertFlowHistory('a', 'b', 'c', 'd');
364
+ });
365
+ });
366
+
367
+ it('eachCallbackWaitsForAllScheduledTasksToComplete', function() {
368
+ schedule('a').
369
+ then(function() {
370
+ schedule('b');
371
+ schedule('c');
372
+ }).
373
+ then(function() {
374
+ schedule('d');
375
+ });
376
+ schedule('e');
377
+
378
+ return waitForIdle().then(function() {
379
+ assertFlowHistory('a', 'b', 'c', 'd', 'e');
380
+ });
381
+ });
382
+
383
+ it('eachCallbackWaitsForReturnTasksToComplete', function() {
384
+ schedule('a').
385
+ then(function() {
386
+ schedule('b');
387
+ return schedule('c');
388
+ }).
389
+ then(function() {
390
+ schedule('d');
391
+ });
392
+ schedule('e');
393
+
394
+ return waitForIdle().then(function() {
395
+ assertFlowHistory('a', 'b', 'c', 'd', 'e');
396
+ });
397
+ });
398
+
399
+ it('callbacksOnAResolvedPromiseInsertIntoTheCurrentFlow', function() {
400
+ promise.fulfilled().then(function() {
401
+ schedule('b');
402
+ });
403
+ schedule('a');
404
+
405
+ return waitForIdle().then(function() {
406
+ assertFlowHistory('b', 'a');
407
+ });
408
+ });
409
+
410
+ it('callbacksInterruptTheFlowWhenPromiseIsResolved', function() {
411
+ schedule('a').then(function() {
412
+ schedule('c');
413
+ });
414
+ schedule('b');
415
+
416
+ return waitForIdle().then(function() {
417
+ assertFlowHistory('a', 'c', 'b');
418
+ });
419
+ });
420
+
421
+ it('allCallbacksInAFrameAreScheduledWhenPromiseIsResolved', function() {
422
+ var a = schedule('a');
423
+ a.then(function() { schedule('b'); });
424
+ schedule('c');
425
+ a.then(function() { schedule('d'); });
426
+ schedule('e');
427
+
428
+ return waitForIdle().then(function() {
429
+ assertFlowHistory('a', 'b', 'c', 'd', 'e');
430
+ });
431
+ });
432
+
433
+ it('tasksScheduledInInActiveFrameDoNotGetPrecedence', function() {
434
+ var d = promise.fulfilled();
435
+ schedule('a');
436
+ schedule('b');
437
+ d.then(function() { schedule('c'); });
438
+
439
+ return waitForIdle().then(function() {
440
+ assertFlowHistory('a', 'b', 'c');
441
+ });
442
+ });
443
+
444
+ it('tasksScheduledInAFrameGetPrecedence_1', function() {
445
+ var a = schedule('a');
446
+ schedule('b').then(function() {
447
+ a.then(function() {
448
+ schedule('c');
449
+ schedule('d');
450
+ });
451
+ var e = schedule('e');
452
+ a.then(function() {
453
+ schedule('f');
454
+ e.then(function() {
455
+ schedule('g');
456
+ });
457
+ schedule('h');
458
+ });
459
+ schedule('i');
460
+ });
461
+ schedule('j');
462
+
463
+ return waitForIdle().then(function() {
464
+ assertFlowHistory('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
465
+ });
466
+ });
467
+ });
468
+
469
+ describe('testErrorHandling', function() {
470
+ it('thrownErrorsArePassedToTaskErrback', function() {
471
+ scheduleAction('function that throws', throwStubError).
472
+ then(fail, assertIsStubError);
473
+ return waitForIdle();
474
+ });
475
+
476
+ it('thrownErrorsPropagateThroughPromiseChain', function() {
477
+ scheduleAction('function that throws', throwStubError).
478
+ then(fail).
479
+ then(fail, assertIsStubError);
480
+ return waitForIdle();
481
+ });
482
+
483
+ it('catchesErrorsFromFailedTasksInAFrame', function() {
484
+ schedule('a').then(function() {
485
+ schedule('b');
486
+ scheduleAction('function that throws', throwStubError);
487
+ }).
488
+ then(fail, assertIsStubError);
489
+ return waitForIdle();
490
+ });
491
+
492
+ it('abortsIfOnlyTaskReturnsAnUnhandledRejection', function() {
493
+ scheduleAction('function that returns rejected promise', function() {
494
+ return promise.rejected(new StubError);
495
+ });
496
+ return waitForAbort().then(assertIsStubError);
497
+ });
498
+
499
+ it('abortsIfThereIsAnUnhandledRejection', function() {
500
+ promise.rejected(new StubError);
501
+ schedule('this should not run');
502
+ return waitForAbort().
503
+ then(assertIsStubError).
504
+ then(function() {
505
+ assertFlowHistory(/* none */);
506
+ });
507
+ });
508
+
509
+ it('abortsSequenceIfATaskFails', function() {
510
+ schedule('a');
511
+ schedule('b');
512
+ scheduleAction('c', throwStubError);
513
+ schedule('d'); // Should never execute.
514
+
515
+ return waitForAbort().
516
+ then(assertIsStubError).
517
+ then(function() {
518
+ assertFlowHistory('a', 'b', 'c');
519
+ });
520
+ });
521
+
522
+ it('abortsFromUnhandledFramedTaskFailures_1', function() {
523
+ schedule('outer task').then(function() {
524
+ scheduleAction('inner task', throwStubError);
525
+ });
526
+ schedule('this should not run');
527
+ return waitForAbort().
528
+ then(assertIsStubError).
529
+ then(function() {
530
+ assertFlowHistory('outer task', 'inner task');
531
+ });
532
+ });
533
+
534
+ it('abortsFromUnhandledFramedTaskFailures_2', function() {
535
+ schedule('a').then(function() {
536
+ schedule('b').then(function() {
537
+ scheduleAction('c', throwStubError);
538
+ // This should not execute.
539
+ schedule('d');
540
+ });
541
+ });
542
+
543
+ return waitForAbort().
544
+ then(assertIsStubError).
545
+ then(function() {
546
+ assertFlowHistory('a', 'b', 'c');
547
+ });
548
+ });
549
+
550
+ it('abortsWhenErrorBubblesUpFromFullyResolvingAnObject', function() {
551
+ var callback = sinon.spy();
552
+
553
+ scheduleAction('', function() {
554
+ var obj = {'foo': promise.rejected(new StubError)};
555
+ return promise.fullyResolved(obj).then(callback);
556
+ });
557
+
558
+ return waitForAbort().
559
+ then(assertIsStubError).
560
+ then(() => assert(!callback.called));
561
+ });
562
+
563
+ it('abortsWhenErrorBubblesUpFromFullyResolvingAnObject_withCallback', function() {
564
+ var callback1 = sinon.spy();
565
+ var callback2 = sinon.spy();
566
+
567
+ scheduleAction('', function() {
568
+ var obj = {'foo': promise.rejected(new StubError)};
569
+ return promise.fullyResolved(obj).then(callback1);
570
+ }).then(callback2);
571
+
572
+ return waitForAbort().
573
+ then(assertIsStubError).
574
+ then(() => assert(!callback1.called)).
575
+ then(() => assert(!callback2.called));
576
+ });
577
+
578
+ it('canCatchErrorsFromNestedTasks', function() {
579
+ var errback = sinon.spy();
580
+ schedule('a').
581
+ then(function() {
582
+ return scheduleAction('b', throwStubError);
583
+ }).
584
+ catch(errback);
585
+ return waitForIdle().then(function() {
586
+ assert(errback.called);
587
+ assertIsStubError(errback.getCall(0).args[0]);
588
+ });
589
+ });
590
+
591
+ it('nestedCommandFailuresCanBeCaughtAndSuppressed', function() {
592
+ var errback = sinon.spy();
593
+ schedule('a').then(function() {
594
+ return schedule('b').then(function() {
595
+ return schedule('c').then(function() {
596
+ throw new StubError;
597
+ });
598
+ });
599
+ }).catch(errback);
600
+ schedule('d');
601
+ return waitForIdle().
602
+ then(function() {
603
+ assert(errback.called);
604
+ assertIsStubError(errback.getCall(0).args[0]);
605
+ assertFlowHistory('a', 'b', 'c', 'd');
606
+ });
607
+ });
608
+
609
+ it('aTaskWithAnUnhandledPromiseRejection', function() {
610
+ schedule('a');
611
+ scheduleAction('sub-tasks', function() {
612
+ promise.rejected(new StubError);
613
+ });
614
+ schedule('should never run');
615
+
616
+ return waitForAbort().
617
+ then(assertIsStubError).
618
+ then(function() {
619
+ assertFlowHistory('a', 'sub-tasks');
620
+ });
621
+ });
622
+
623
+ it('aTaskThatReutrnsARejectedPromise', function() {
624
+ schedule('a');
625
+ scheduleAction('sub-tasks', function() {
626
+ return promise.rejected(new StubError);
627
+ });
628
+ schedule('should never run');
629
+
630
+ return waitForAbort().
631
+ then(assertIsStubError).
632
+ then(function() {
633
+ assertFlowHistory('a', 'sub-tasks');
634
+ });
635
+ });
636
+
637
+ it('discardsSubtasksIfTaskThrows', function() {
638
+ var pair = callbackPair(null, assertIsStubError);
639
+ scheduleAction('a', function() {
640
+ schedule('b');
641
+ schedule('c');
642
+ throwStubError();
643
+ }).then(pair.callback, pair.errback);
644
+ schedule('d');
645
+
646
+ return waitForIdle().
647
+ then(pair.assertErrback).
648
+ then(function() {
649
+ assertFlowHistory('a', 'd');
650
+ });
651
+ });
652
+
653
+ it('discardsRemainingSubtasksIfASubtaskFails', function() {
654
+ var pair = callbackPair(null, assertIsStubError);
655
+ scheduleAction('a', function() {
656
+ schedule('b');
657
+ scheduleAction('c', throwStubError);
658
+ schedule('d');
659
+ }).then(pair.callback, pair.errback);
660
+ schedule('e');
661
+
662
+ return waitForIdle().
663
+ then(pair.assertErrback).
664
+ then(function() {
665
+ assertFlowHistory('a', 'b', 'c', 'e');
666
+ });
667
+ });
668
+ });
669
+
670
+ describe('testTryModelingFinally', function() {
671
+ it('happyPath', function() {
672
+ /* Model:
673
+ try {
674
+ doFoo();
675
+ doBar();
676
+ } finally {
677
+ doBaz();
678
+ }
679
+ */
680
+ schedulePush('foo').
681
+ then(() => schedulePush('bar')).
682
+ thenFinally(() => schedulePush('baz'));
683
+ return waitForIdle().then(assertingMessages('foo', 'bar', 'baz'));
684
+ });
685
+
686
+ it('firstTryFails', function() {
687
+ /* Model:
688
+ try {
689
+ doFoo();
690
+ doBar();
691
+ } finally {
692
+ doBaz();
693
+ }
694
+ */
695
+
696
+ scheduleAction('doFoo and throw', function() {
697
+ messages.push('foo');
698
+ throw new StubError;
699
+ }).
700
+ then(function() { schedulePush('bar'); }).
701
+ thenFinally(function() { schedulePush('baz'); });
702
+
703
+ return waitForAbort().
704
+ then(assertIsStubError).
705
+ then(assertingMessages('foo', 'baz'));
706
+ });
707
+
708
+ it('secondTryFails', function() {
709
+ /* Model:
710
+ try {
711
+ doFoo();
712
+ doBar();
713
+ } finally {
714
+ doBaz();
715
+ }
716
+ */
717
+
718
+ schedulePush('foo').
719
+ then(function() {
720
+ return scheduleAction('doBar and throw', function() {
721
+ messages.push('bar');
722
+ throw new StubError;
723
+ });
724
+ }).
725
+ thenFinally(function() {
726
+ return schedulePush('baz');
727
+ });
728
+ return waitForAbort().
729
+ then(assertIsStubError).
730
+ then(assertingMessages('foo', 'bar', 'baz'));
731
+ });
732
+ });
733
+
734
+ describe('testTaskCallbacksInterruptFlow', function() {
735
+ it('(base case)', function() {
736
+ schedule('a').then(function() {
737
+ schedule('b');
738
+ });
739
+ schedule('c');
740
+ return waitForIdle().then(function() {
741
+ assertFlowHistory('a', 'b', 'c');
742
+ });
743
+ });
744
+
745
+ it('taskDependsOnImmediatelyFulfilledPromise', function() {
746
+ scheduleAction('a', function() {
747
+ return promise.fulfilled();
748
+ }).then(function() {
749
+ schedule('b');
750
+ });
751
+ schedule('c');
752
+ return waitForIdle().then(function() {
753
+ assertFlowHistory('a', 'b', 'c');
754
+ });
755
+ });
756
+
757
+ it('taskDependsOnPreviouslyFulfilledPromise', function() {
758
+ var aPromise = promise.fulfilled(123);
759
+ scheduleAction('a', function() {
760
+ return aPromise;
761
+ }).then(function(value) {
762
+ assert.equal(123, value);
763
+ schedule('b');
764
+ });
765
+ schedule('c');
766
+ return waitForIdle().then(function() {
767
+ assertFlowHistory('a', 'b', 'c');
768
+ });
769
+ });
770
+
771
+ it('taskDependsOnAsyncPromise', function() {
772
+ scheduleAction('a', function() {
773
+ return promise.delayed(25);
774
+ }).then(function() {
775
+ schedule('b');
776
+ });
777
+ schedule('c');
778
+ return waitForIdle().then(function() {
779
+ assertFlowHistory('a', 'b', 'c');
780
+ });
781
+ });
782
+
783
+ it('promiseChainedToTaskInterruptFlow', function() {
784
+ schedule('a').then(function() {
785
+ return promise.fulfilled();
786
+ }).then(function() {
787
+ return promise.fulfilled();
788
+ }).then(function() {
789
+ schedule('b');
790
+ });
791
+ schedule('c');
792
+ return waitForIdle().then(function() {
793
+ assertFlowHistory('a', 'b', 'c');
794
+ });
795
+ });
796
+
797
+ it('nestedTaskCallbacksInterruptFlowWhenResolved', function() {
798
+ schedule('a').then(function() {
799
+ schedule('b').then(function() {
800
+ schedule('c');
801
+ });
802
+ });
803
+ schedule('d');
804
+ return waitForIdle().then(function() {
805
+ assertFlowHistory('a', 'b', 'c', 'd');
806
+ });
807
+ });
808
+ });
809
+
810
+ describe('testDelayedNesting', function() {
811
+
812
+ it('1', function() {
813
+ var a = schedule('a');
814
+ schedule('b').then(function() {
815
+ a.then(function() { schedule('c'); });
816
+ schedule('d');
817
+ });
818
+ schedule('e');
819
+
820
+ return waitForIdle().then(function() {
821
+ assertFlowHistory('a', 'b', 'c', 'd', 'e');
822
+ });
823
+ });
824
+
825
+ it('2', function() {
826
+ var a = schedule('a');
827
+ schedule('b').then(function() {
828
+ a.then(function() { schedule('c'); });
829
+ schedule('d');
830
+ a.then(function() { schedule('e'); });
831
+ });
832
+ schedule('f');
833
+
834
+ return waitForIdle().then(function() {
835
+ assertFlowHistory('a', 'b', 'c', 'd', 'e', 'f');
836
+ });
837
+ });
838
+
839
+ it('3', function() {
840
+ var a = schedule('a');
841
+ schedule('b').then(function() {
842
+ a.then(function() { schedule('c'); });
843
+ a.then(function() { schedule('d'); });
844
+ });
845
+ schedule('e');
846
+
847
+ return waitForIdle().then(function() {
848
+ assertFlowHistory('a', 'b', 'c', 'd', 'e');
849
+ });
850
+ });
851
+
852
+ it('4', function() {
853
+ var a = schedule('a');
854
+ schedule('b').then(function() {
855
+ a.then(function() { schedule('c'); }).then(function() {
856
+ schedule('d');
857
+ });
858
+ a.then(function() { schedule('e'); });
859
+ });
860
+ schedule('f');
861
+
862
+ return waitForIdle().then(function() {
863
+ assertFlowHistory('a', 'b', 'c', 'd', 'e', 'f');
864
+ });
865
+ });
866
+
867
+ it('5', function() {
868
+ var a = schedule('a');
869
+ schedule('b').then(function() {
870
+ var c;
871
+ a.then(function() { c = schedule('c'); }).then(function() {
872
+ schedule('d');
873
+ a.then(function() { schedule('e'); });
874
+ c.then(function() { schedule('f'); });
875
+ schedule('g');
876
+ });
877
+ a.then(function() { schedule('h'); });
878
+ });
879
+ schedule('i');
880
+
881
+ return waitForIdle().then(function() {
882
+ assertFlowHistory('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
883
+ });
884
+ });
885
+ });
886
+
887
+ describe('testWaiting', function() {
888
+ it('onAConditionThatIsAlwaysTrue', function() {
889
+ scheduleWait(function() { return true;}, 0, 'waiting on true');
890
+ return waitForIdle().then(function() {
891
+ assertFlowHistory('0: waiting on true');
892
+ });
893
+ });
894
+
895
+ it('aSimpleCountingCondition', function() {
896
+ var count = 0;
897
+ scheduleWait(function() {
898
+ return ++count == 3;
899
+ }, 100, 'counting to 3');
900
+
901
+ return waitForIdle().then(function() {
902
+ assert.equal(3, count);
903
+ });
904
+ });
905
+
906
+ it('aConditionThatReturnsAPromise', function() {
907
+ var d = new promise.Deferred();
908
+ var count = 0;
909
+
910
+ scheduleWait(function() {
911
+ count += 1;
912
+ return d.promise;
913
+ }, 0, 'waiting for promise');
914
+
915
+ return timeout(50).then(function() {
916
+ assert.equal(1, count);
917
+ d.fulfill(123);
918
+ return waitForIdle();
919
+ });
920
+ });
921
+
922
+ it('aConditionThatReturnsAPromise_2', function() {
923
+ var count = 0;
924
+ scheduleWait(function() {
925
+ return promise.fulfilled(++count == 3);
926
+ }, 100, 'waiting for promise');
927
+
928
+ return waitForIdle().then(function() {
929
+ assert.equal(3, count);
930
+ });
931
+ });
932
+
933
+ it('aConditionThatReturnsATaskResult', function() {
934
+ var count = 0;
935
+ scheduleWait(function() {
936
+ return scheduleAction('increment count', function() {
937
+ return ++count == 3;
938
+ });
939
+ }, 100, 'counting to 3');
940
+ schedule('post wait');
941
+
942
+ return waitForIdle().then(function() {
943
+ assert.equal(3, count);
944
+ assertFlowHistory(
945
+ '0: counting to 3', 'increment count',
946
+ '1: counting to 3', 'increment count',
947
+ '2: counting to 3', 'increment count',
948
+ 'post wait');
949
+ });
950
+ });
951
+
952
+ it('conditionContainsASubtask', function() {
953
+ var count = 0;
954
+ scheduleWait(function() {
955
+ schedule('sub task');
956
+ return ++count == 3;
957
+ }, 100, 'counting to 3');
958
+ schedule('post wait');
959
+
960
+ return waitForIdle().then(function() {
961
+ assert.equal(3, count);
962
+ assertFlowHistory(
963
+ '0: counting to 3', 'sub task',
964
+ '1: counting to 3', 'sub task',
965
+ '2: counting to 3', 'sub task',
966
+ 'post wait');
967
+ });
968
+ });
969
+
970
+ it('cancelsWaitIfScheduledTaskFails', function() {
971
+ var pair = callbackPair(null, assertIsStubError);
972
+ scheduleWait(function() {
973
+ scheduleAction('boom', throwStubError);
974
+ schedule('this should not run');
975
+ return true;
976
+ }, 100, 'waiting to go boom').then(pair.callback, pair.errback);
977
+ schedule('post wait');
978
+
979
+ return waitForIdle().
980
+ then(pair.assertErrback).
981
+ then(function() {
982
+ assertFlowHistory(
983
+ '0: waiting to go boom', 'boom',
984
+ 'post wait');
985
+ });
986
+ });
987
+
988
+ it('failsIfConditionThrows', function() {
989
+ var callbacks = callbackPair(null, assertIsStubError);
990
+ scheduleWait(throwStubError, 0, 'goes boom').
991
+ then(callbacks.callback, callbacks.errback);
992
+ schedule('post wait');
993
+
994
+ return waitForIdle().
995
+ then(callbacks.assertErrback).
996
+ then(function() {
997
+ assertFlowHistory('0: goes boom', 'post wait');
998
+ });
999
+ });
1000
+
1001
+ it('failsIfConditionReturnsARejectedPromise', function() {
1002
+ var callbacks = callbackPair(null, assertIsStubError);
1003
+ scheduleWait(function() {
1004
+ return promise.rejected(new StubError);
1005
+ }, 0, 'goes boom').then(callbacks.callback, callbacks.errback);
1006
+ schedule('post wait');
1007
+
1008
+ return waitForIdle().
1009
+ then(callbacks.assertErrback).
1010
+ then(function() {
1011
+ assertFlowHistory('0: goes boom', 'post wait');
1012
+ });
1013
+ });
1014
+
1015
+ it('failsIfConditionHasUnhandledRejection', function() {
1016
+ var callbacks = callbackPair(null, assertIsStubError);
1017
+ scheduleWait(function() {
1018
+ promise.controlFlow().execute(throwStubError);
1019
+ }, 0, 'goes boom').then(callbacks.callback, callbacks.errback);
1020
+ schedule('post wait');
1021
+
1022
+ return waitForIdle().
1023
+ then(callbacks.assertErrback).
1024
+ then(function() {
1025
+ assertFlowHistory('0: goes boom', 'post wait');
1026
+ });
1027
+ });
1028
+
1029
+ it('failsIfConditionHasAFailedSubtask', function() {
1030
+ var callbacks = callbackPair(null, assertIsStubError);
1031
+ var count = 0;
1032
+ scheduleWait(function() {
1033
+ scheduleAction('maybe throw', function() {
1034
+ if (++count == 2) {
1035
+ throw new StubError;
1036
+ }
1037
+ });
1038
+ }, 100, 'waiting').then(callbacks.callback, callbacks.errback);
1039
+ schedule('post wait');
1040
+
1041
+ return waitForIdle().then(function() {
1042
+ assert.equal(2, count);
1043
+ assertFlowHistory(
1044
+ '0: waiting', 'maybe throw',
1045
+ '1: waiting', 'maybe throw',
1046
+ 'post wait');
1047
+ });
1048
+ });
1049
+
1050
+ it('pollingLoopWaitsForAllScheduledTasksInCondition', function() {
1051
+ var count = 0;
1052
+ scheduleWait(function() {
1053
+ scheduleAction('increment count', function() { ++count; });
1054
+ return count >= 3;
1055
+ }, 100, 'counting to 3');
1056
+ schedule('post wait');
1057
+
1058
+ return waitForIdle().then(function() {
1059
+ assert.equal(4, count);
1060
+ assertFlowHistory(
1061
+ '0: counting to 3', 'increment count',
1062
+ '1: counting to 3', 'increment count',
1063
+ '2: counting to 3', 'increment count',
1064
+ '3: counting to 3', 'increment count',
1065
+ 'post wait');
1066
+ });
1067
+ });
1068
+
1069
+ it('waitsForeverOnAZeroTimeout', function() {
1070
+ var done = false;
1071
+ setTimeout(function() {
1072
+ done = true;
1073
+ }, 150);
1074
+ var waitResult = scheduleWait(function() {
1075
+ return done;
1076
+ }, 0);
1077
+
1078
+ return timeout(75).then(function() {
1079
+ assert.ok(!done);
1080
+ return timeout(100);
1081
+ }).then(function() {
1082
+ assert.ok(done);
1083
+ return waitResult;
1084
+ });
1085
+ });
1086
+
1087
+ it('waitsForeverIfTimeoutOmitted', function() {
1088
+ var done = false;
1089
+ setTimeout(function() {
1090
+ done = true;
1091
+ }, 150);
1092
+ var waitResult = scheduleWait(function() {
1093
+ return done;
1094
+ });
1095
+
1096
+ return timeout(75).then(function() {
1097
+ assert.ok(!done);
1098
+ return timeout(100);
1099
+ }).then(function() {
1100
+ assert.ok(done);
1101
+ return waitResult;
1102
+ });
1103
+ });
1104
+
1105
+ it('timesOut_nonZeroTimeout', function() {
1106
+ var count = 0;
1107
+ scheduleWait(function() {
1108
+ count += 1;
1109
+ var ms = count === 2 ? 65 : 5;
1110
+ return promise.delayed(ms).then(function() {
1111
+ return false;
1112
+ });
1113
+ }, 60, 'counting to 3');
1114
+ return waitForAbort().then(function(e) {
1115
+ switch (count) {
1116
+ case 1:
1117
+ assertFlowHistory('0: counting to 3');
1118
+ break;
1119
+ case 2:
1120
+ assertFlowHistory('0: counting to 3', '1: counting to 3');
1121
+ break;
1122
+ default:
1123
+ fail('unexpected polling count: ' + count);
1124
+ }
1125
+ assert.ok(
1126
+ /^counting to 3\nWait timed out after \d+ms$/.test(e.message));
1127
+ });
1128
+ });
1129
+
1130
+ it('shouldFailIfConditionReturnsARejectedPromise', function() {
1131
+ scheduleWait(function() {
1132
+ return promise.rejected(new StubError);
1133
+ }, 100, 'returns rejected promise on first pass');
1134
+ return waitForAbort().then(assertIsStubError);
1135
+ });
1136
+
1137
+ it('scheduleWithIntermittentWaits', function() {
1138
+ schedule('a');
1139
+ scheduleWait(function() { return true; }, 0, 'wait 1');
1140
+ schedule('b');
1141
+ scheduleWait(function() { return true; }, 0, 'wait 2');
1142
+ schedule('c');
1143
+ scheduleWait(function() { return true; }, 0, 'wait 3');
1144
+
1145
+ return waitForIdle().then(function() {
1146
+ assertFlowHistory('a', '0: wait 1', 'b', '0: wait 2', 'c', '0: wait 3');
1147
+ });
1148
+ });
1149
+
1150
+ it('scheduleWithIntermittentAndNestedWaits', function() {
1151
+ schedule('a');
1152
+ scheduleWait(function() { return true; }, 0, 'wait 1').
1153
+ then(function() {
1154
+ schedule('d');
1155
+ scheduleWait(function() { return true; }, 0, 'wait 2');
1156
+ schedule('e');
1157
+ });
1158
+ schedule('b');
1159
+ scheduleWait(function() { return true; }, 0, 'wait 3');
1160
+ schedule('c');
1161
+ scheduleWait(function() { return true; }, 0, 'wait 4');
1162
+
1163
+ return waitForIdle().then(function() {
1164
+ assertFlowHistory(
1165
+ 'a', '0: wait 1', 'd', '0: wait 2', 'e', 'b', '0: wait 3', 'c',
1166
+ '0: wait 4');
1167
+ });
1168
+ });
1169
+
1170
+ it('requiresConditionToBeAPromiseOrFunction', function() {
1171
+ assert.throws(function() {
1172
+ flow.wait(1234, 0);
1173
+ });
1174
+ flow.wait(function() { return true;}, 0);
1175
+ flow.wait(promise.fulfilled(), 0);
1176
+ return waitForIdle();
1177
+ });
1178
+
1179
+ it('promiseThatDoesNotResolveBeforeTimeout', function() {
1180
+ var d = promise.defer();
1181
+ flow.wait(d.promise, 5).then(fail, function(e) {
1182
+ assert.ok(
1183
+ /Timed out waiting for promise to resolve after \d+ms/
1184
+ .test(e.message),
1185
+ 'unexpected error message: ' + e.message);
1186
+ });
1187
+ return waitForIdle().then(function() {
1188
+ assert.ok('Promise should not be cancelled', d.promise.isPending());
1189
+ });
1190
+ });
1191
+
1192
+ it('unboundedWaitOnPromiseResolution', function() {
1193
+ var messages = [];
1194
+ var d = promise.defer();
1195
+ var waitResult = flow.wait(d.promise).then(function(value) {
1196
+ messages.push('b');
1197
+ assert.equal(1234, value);
1198
+ });
1199
+ setTimeout(function() {
1200
+ messages.push('a');
1201
+ }, 5);
1202
+
1203
+ timeout(10).then(function() {
1204
+ assert.deepEqual(['a'], messages);
1205
+ assert.ok(waitResult.isPending());
1206
+ d.fulfill(1234);
1207
+ return waitResult;
1208
+ }).then(function() {
1209
+ assert.deepEqual(['a', 'b'], messages);
1210
+ });
1211
+
1212
+ return waitForIdle();
1213
+ });
1214
+ });
1215
+
1216
+ describe('testSubtasks', function() {
1217
+ it('(base case)', function() {
1218
+ schedule('a');
1219
+ scheduleAction('sub-tasks', function() {
1220
+ schedule('c');
1221
+ schedule('d');
1222
+ });
1223
+ schedule('b');
1224
+
1225
+ return waitForIdle().then(function() {
1226
+ assertFlowHistory('a', 'sub-tasks', 'c', 'd', 'b');
1227
+ });
1228
+ });
1229
+
1230
+ it('nesting', function() {
1231
+ schedule('a');
1232
+ scheduleAction('sub-tasks', function() {
1233
+ schedule('b');
1234
+ scheduleAction('sub-sub-tasks', function() {
1235
+ schedule('c');
1236
+ schedule('d');
1237
+ });
1238
+ schedule('e');
1239
+ });
1240
+ schedule('f');
1241
+
1242
+ return waitForIdle().then(function() {
1243
+ assertFlowHistory(
1244
+ 'a', 'sub-tasks', 'b', 'sub-sub-tasks', 'c', 'd', 'e', 'f');
1245
+ });
1246
+ });
1247
+
1248
+ it('taskReturnsSubTaskResult_1', function() {
1249
+ schedule('a');
1250
+ scheduleAction('sub-tasks', function() {
1251
+ return schedule('c');
1252
+ });
1253
+ schedule('b');
1254
+
1255
+ return waitForIdle().then(function() {
1256
+ assertFlowHistory('a', 'sub-tasks', 'c', 'b');
1257
+ });
1258
+ });
1259
+
1260
+ it('taskReturnsSubTaskResult_2', function() {
1261
+ let pair = callbackPair((value) => assert.equal(123, value));
1262
+ schedule('a');
1263
+ schedule('sub-tasks', promise.fulfilled(123)).then(pair.callback);
1264
+ schedule('b');
1265
+
1266
+ return waitForIdle().then(function() {
1267
+ assertFlowHistory('a', 'sub-tasks','b');
1268
+ pair.assertCallback();
1269
+ });
1270
+ });
1271
+
1272
+ it('taskReturnsPromiseThatDependsOnSubtask_1', function() {
1273
+ scheduleAction('a', function() {
1274
+ return promise.delayed(10).then(function() {
1275
+ schedule('b');
1276
+ });
1277
+ });
1278
+ schedule('c');
1279
+ return waitForIdle().then(function() {
1280
+ assertFlowHistory('a', 'b', 'c');
1281
+ });
1282
+ });
1283
+
1284
+ it('taskReturnsPromiseThatDependsOnSubtask_2', function() {
1285
+ scheduleAction('a', function() {
1286
+ return promise.fulfilled().then(function() {
1287
+ schedule('b');
1288
+ });
1289
+ });
1290
+ schedule('c');
1291
+ return waitForIdle().then(function() {
1292
+ assertFlowHistory('a', 'b', 'c');
1293
+ });
1294
+ });
1295
+
1296
+ it('taskReturnsPromiseThatDependsOnSubtask_3', function() {
1297
+ scheduleAction('a', function() {
1298
+ return promise.delayed(10).then(function() {
1299
+ return schedule('b');
1300
+ });
1301
+ });
1302
+ schedule('c');
1303
+ return waitForIdle().then(function() {
1304
+ assertFlowHistory('a', 'b', 'c');
1305
+ });
1306
+ });
1307
+
1308
+ it('taskReturnsPromiseThatDependsOnSubtask_4', function() {
1309
+ scheduleAction('a', function() {
1310
+ return promise.delayed(5).then(function() {
1311
+ return promise.delayed(5).then(function() {
1312
+ return schedule('b');
1313
+ });
1314
+ });
1315
+ });
1316
+ schedule('c');
1317
+ return waitForIdle().then(function() {
1318
+ assertFlowHistory('a', 'b', 'c');
1319
+ });
1320
+ });
1321
+
1322
+ it('taskReturnsPromiseThatDependsOnSubtask_5', function() {
1323
+ scheduleAction('a', function() {
1324
+ return promise.delayed(5).then(function() {
1325
+ return promise.delayed(5).then(function() {
1326
+ return promise.delayed(5).then(function() {
1327
+ return promise.delayed(5).then(function() {
1328
+ return schedule('b');
1329
+ });
1330
+ });
1331
+ });
1332
+ });
1333
+ });
1334
+ schedule('c');
1335
+ return waitForIdle().then(function() {
1336
+ assertFlowHistory('a', 'b', 'c');
1337
+ });
1338
+ });
1339
+
1340
+ it('taskReturnsPromiseThatDependsOnSubtask_6', function() {
1341
+ scheduleAction('a', function() {
1342
+ return promise.delayed(5).
1343
+ then(function() { return promise.delayed(5) }).
1344
+ then(function() { return promise.delayed(5) }).
1345
+ then(function() { return promise.delayed(5) }).
1346
+ then(function() { return schedule('b'); });
1347
+ });
1348
+ schedule('c');
1349
+ return waitForIdle().then(function() {
1350
+ assertFlowHistory('a', 'b', 'c');
1351
+ });
1352
+ });
1353
+
1354
+ it('subTaskFails_1', function() {
1355
+ schedule('a');
1356
+ scheduleAction('sub-tasks', function() {
1357
+ scheduleAction('sub-task that fails', throwStubError);
1358
+ });
1359
+ schedule('should never execute');
1360
+
1361
+ return waitForAbort().
1362
+ then(assertIsStubError).
1363
+ then(function() {
1364
+ assertFlowHistory('a', 'sub-tasks', 'sub-task that fails');
1365
+ });
1366
+ });
1367
+
1368
+ it('subTaskFails_2', function() {
1369
+ schedule('a');
1370
+ scheduleAction('sub-tasks', function() {
1371
+ return promise.rejected(new StubError);
1372
+ });
1373
+ schedule('should never execute');
1374
+
1375
+ return waitForAbort().
1376
+ then(assertIsStubError).
1377
+ then(function() {
1378
+ assertFlowHistory('a', 'sub-tasks');
1379
+ });
1380
+ });
1381
+
1382
+ it('subTaskFails_3', function() {
1383
+ var callbacks = callbackPair(null, assertIsStubError);
1384
+
1385
+ schedule('a');
1386
+ scheduleAction('sub-tasks', function() {
1387
+ return promise.rejected(new StubError);
1388
+ }).then(callbacks.callback, callbacks.errback);
1389
+ schedule('b');
1390
+
1391
+ return waitForIdle().
1392
+ then(function() {
1393
+ assertFlowHistory('a', 'sub-tasks', 'b');
1394
+ callbacks.assertErrback();
1395
+ });
1396
+ });
1397
+ });
1398
+
1399
+ describe('testEventLoopWaitsOnPendingPromiseRejections', function() {
1400
+ it('oneRejection', function() {
1401
+ var d = new promise.Deferred;
1402
+ scheduleAction('one', function() {
1403
+ return d.promise;
1404
+ });
1405
+ scheduleAction('two', function() {});
1406
+
1407
+ return timeout(50).then(function() {
1408
+ assertFlowHistory('one');
1409
+ d.reject(new StubError);
1410
+ return waitForAbort();
1411
+ }).
1412
+ then(assertIsStubError).
1413
+ then(function() {
1414
+ assertFlowHistory('one');
1415
+ });
1416
+ });
1417
+
1418
+ it('multipleRejections', function() {
1419
+ var once = Error('once');
1420
+ var twice = Error('twice');
1421
+
1422
+ scheduleAction('one', function() {
1423
+ promise.rejected(once);
1424
+ promise.rejected(twice);
1425
+ });
1426
+ var twoResult = scheduleAction('two', function() {});
1427
+
1428
+ flow.removeAllListeners(
1429
+ promise.ControlFlow.EventType.UNCAUGHT_EXCEPTION);
1430
+ return new NativePromise(function(fulfill, reject) {
1431
+ setTimeout(function() {
1432
+ reject(Error('Should have reported the two errors by now'));
1433
+ }, 50);
1434
+ flow.on(
1435
+ promise.ControlFlow.EventType.UNCAUGHT_EXCEPTION,
1436
+ fulfill);
1437
+ }).then(function(e) {
1438
+ assert.ok(e instanceof promise.MultipleUnhandledRejectionError,
1439
+ 'Not a MultipleUnhandledRejectionError');
1440
+ let errors = Array.from(e.errors);
1441
+ assert.deepEqual([once, twice], errors);
1442
+ assertFlowHistory('one');
1443
+ assert.ok(!twoResult.isPending(), 'Did not cancel the second task');
1444
+ });
1445
+ });
1446
+ });
1447
+
1448
+ describe('testCancelsPromiseReturnedByCallbackIfFrameFails', function() {
1449
+ it('promiseCallback', function() {
1450
+ var chainPair = callbackPair(null, assertIsStubError);
1451
+ var deferredPair = callbackPair(null, function(e) {
1452
+ assert.equal('CancellationError: StubError', e.toString(),
1453
+ 'callback result should be cancelled');
1454
+ });
1455
+
1456
+ var d = new promise.Deferred();
1457
+ d.then(deferredPair.callback, deferredPair.errback);
1458
+
1459
+ promise.fulfilled().
1460
+ then(function() {
1461
+ scheduleAction('boom', throwStubError);
1462
+ schedule('this should not run');
1463
+ return d.promise;
1464
+ }).
1465
+ then(chainPair.callback, chainPair.errback);
1466
+
1467
+ return waitForIdle().then(function() {
1468
+ assertFlowHistory('boom');
1469
+ chainPair.assertErrback('chain errback not invoked');
1470
+ deferredPair.assertErrback('deferred errback not invoked');
1471
+ });
1472
+ });
1473
+
1474
+ it('taskCallback', function() {
1475
+ var chainPair = callbackPair(null, assertIsStubError);
1476
+ var deferredPair = callbackPair(null, function(e) {
1477
+ assert.equal('CancellationError: StubError', e.toString(),
1478
+ 'callback result should be cancelled');
1479
+ });
1480
+
1481
+ var d = new promise.Deferred();
1482
+ d.then(deferredPair.callback, deferredPair.errback);
1483
+
1484
+ schedule('a').
1485
+ then(function() {
1486
+ scheduleAction('boom', throwStubError);
1487
+ schedule('this should not run');
1488
+ return d.promise;
1489
+ }).
1490
+ then(chainPair.callback, chainPair.errback);
1491
+
1492
+ return waitForIdle().then(function() {
1493
+ assertFlowHistory('a', 'boom');
1494
+ chainPair.assertErrback('chain errback not invoked');
1495
+ deferredPair.assertErrback('deferred errback not invoked');
1496
+ });
1497
+ });
1498
+ });
1499
+
1500
+ it('testMaintainsOrderInCallbacksWhenATaskReturnsAPromise', function() {
1501
+ schedule('__start__', promise.fulfilled()).
1502
+ then(function() {
1503
+ messages.push('a');
1504
+ schedulePush('b');
1505
+ messages.push('c');
1506
+ }).
1507
+ then(function() {
1508
+ messages.push('d');
1509
+ });
1510
+ schedulePush('e');
1511
+
1512
+ return waitForIdle().then(function() {
1513
+ assertFlowHistory('__start__', 'b', 'e');
1514
+ assertMessages('a', 'c', 'b', 'd', 'e');
1515
+ });
1516
+ });
1517
+
1518
+ it('testOwningFlowIsActivatedForExecutingTasks', function() {
1519
+ var defaultFlow = promise.controlFlow();
1520
+ var order = [];
1521
+
1522
+ promise.createFlow(function(flow) {
1523
+ assertFlowIs(flow);
1524
+ order.push(0);
1525
+
1526
+ defaultFlow.execute(function() {
1527
+ assertFlowIs(defaultFlow);
1528
+ order.push(1);
1529
+ });
1530
+ });
1531
+
1532
+ return waitForIdle().then(function() {
1533
+ assertFlowIs(defaultFlow);
1534
+ assert.deepEqual([0, 1], order);
1535
+ });
1536
+ });
1537
+
1538
+ it('testCreateFlowReturnsPromisePairedWithCreatedFlow', function() {
1539
+ return new NativePromise(function(fulfill, reject) {
1540
+ var newFlow;
1541
+ promise.createFlow(function(flow) {
1542
+ newFlow = flow;
1543
+ assertFlowIs(newFlow);
1544
+ }).then(function() {
1545
+ assertFlowIs(newFlow);
1546
+ waitForIdle(newFlow).then(fulfill, reject);
1547
+ });
1548
+ });
1549
+ });
1550
+
1551
+ it('testDeferredFactoriesCreateForActiveFlow_defaultFlow', function() {
1552
+ var e = Error();
1553
+ var defaultFlow = promise.controlFlow();
1554
+ promise.fulfilled().then(function() {
1555
+ assertFlowIs(defaultFlow);
1556
+ });
1557
+ promise.rejected(e).then(null, function(err) {
1558
+ assert.equal(e, err);
1559
+ assertFlowIs(defaultFlow);
1560
+ });
1561
+ promise.defer().then(function() {
1562
+ assertFlowIs(defaultFlow);
1563
+ });
1564
+
1565
+ return waitForIdle();
1566
+ });
1567
+
1568
+ it('testDeferredFactoriesCreateForActiveFlow_newFlow', function() {
1569
+ var e = Error();
1570
+ var newFlow = new promise.ControlFlow;
1571
+ newFlow.execute(function() {
1572
+ promise.fulfilled().then(function() {
1573
+ assertFlowIs(newFlow);
1574
+ });
1575
+
1576
+ promise.rejected(e).then(null, function(err) {
1577
+ assert.equal(e, err);
1578
+ assertFlowIs(newFlow);
1579
+ });
1580
+
1581
+ promise.defer().then(function() {
1582
+ assertFlowIs(newFlow);
1583
+ });
1584
+ }).then(function() {
1585
+ assertFlowIs(newFlow);
1586
+ });
1587
+
1588
+ return waitForIdle(newFlow);
1589
+ });
1590
+
1591
+ it('testFlowsSynchronizeWithThemselvesNotEachOther', function() {
1592
+ var defaultFlow = promise.controlFlow();
1593
+ schedulePush('a', 'a');
1594
+ promise.controlFlow().timeout(250);
1595
+ schedulePush('b', 'b');
1596
+
1597
+ promise.createFlow(function() {
1598
+ schedulePush('c', 'c');
1599
+ schedulePush('d', 'd');
1600
+ });
1601
+
1602
+ return waitForIdle().then(function() {
1603
+ assertMessages('a', 'c', 'd', 'b');
1604
+ });
1605
+ });
1606
+
1607
+ it('testUnhandledErrorsAreReportedToTheOwningFlow', function() {
1608
+ var error1 = Error('e1');
1609
+ var error2 = Error('e2');
1610
+
1611
+ var defaultFlow = promise.controlFlow();
1612
+ defaultFlow.removeAllListeners('uncaughtException');
1613
+
1614
+ var flow1Error = NativePromise.defer();
1615
+ flow1Error.promise.then(function(value) {
1616
+ assert.equal(error2, value);
1617
+ });
1618
+
1619
+ var flow2Error = NativePromise.defer();
1620
+ flow2Error.promise.then(function(value) {
1621
+ assert.equal(error1, value);
1622
+ });
1623
+
1624
+ promise.createFlow(function(flow) {
1625
+ flow.once('uncaughtException', flow2Error.resolve);
1626
+ promise.rejected(error1);
1627
+
1628
+ defaultFlow.once('uncaughtException', flow1Error.resolve);
1629
+ defaultFlow.execute(function() {
1630
+ promise.rejected(error2);
1631
+ });
1632
+ });
1633
+
1634
+ return NativePromise.all([flow1Error.promise, flow2Error.promise]);
1635
+ });
1636
+
1637
+ it('testCanSynchronizeFlowsByReturningPromiseFromOneToAnother', function() {
1638
+ var flow1 = new promise.ControlFlow;
1639
+ var flow1Done = NativePromise.defer();
1640
+ flow1.once('idle', flow1Done.resolve);
1641
+ flow1.once('uncaughtException', flow1Done.reject);
1642
+
1643
+ var flow2 = new promise.ControlFlow;
1644
+ var flow2Done = NativePromise.defer();
1645
+ flow2.once('idle', flow2Done.resolve);
1646
+ flow2.once('uncaughtException', flow2Done.reject);
1647
+
1648
+ flow1.execute(function() {
1649
+ schedulePush('a', 'a');
1650
+ return promise.delayed(25);
1651
+ }, 'start flow 1');
1652
+
1653
+ flow2.execute(function() {
1654
+ schedulePush('b', 'b');
1655
+ schedulePush('c', 'c');
1656
+ flow2.execute(function() {
1657
+ return flow1.execute(function() {
1658
+ schedulePush('d', 'd');
1659
+ }, 'flow 1 task');
1660
+ }, 'inject flow1 result into flow2');
1661
+ schedulePush('e', 'e');
1662
+ }, 'start flow 2');
1663
+
1664
+ return NativePromise.all([flow1Done.promise, flow2Done.promise]).
1665
+ then(function() {
1666
+ assertMessages('a', 'b', 'c', 'd', 'e');
1667
+ });
1668
+ });
1669
+
1670
+ it('testFramesWaitToCompleteForPendingRejections', function() {
1671
+ return new NativePromise(function(fulfill, reject) {
1672
+
1673
+ promise.controlFlow().execute(function() {
1674
+ promise.rejected(new StubError);
1675
+ }).then(fulfill, reject);
1676
+
1677
+ }).
1678
+ then(() => fail('expected to fail'), assertIsStubError);
1679
+ });
1680
+
1681
+ it('testSynchronizeErrorsPropagateToOuterFlow', function() {
1682
+ var outerFlow = new promise.ControlFlow;
1683
+ var innerFlow = new promise.ControlFlow;
1684
+
1685
+ var block = NativePromise.defer();
1686
+ innerFlow.execute(function() {
1687
+ return block.promise;
1688
+ }, 'block inner flow');
1689
+
1690
+ outerFlow.execute(function() {
1691
+ block.resolve();
1692
+ return innerFlow.execute(function() {
1693
+ promise.rejected(new StubError);
1694
+ }, 'trigger unhandled rejection error');
1695
+ }, 'run test');
1696
+
1697
+ return NativePromise.all([
1698
+ waitForIdle(innerFlow),
1699
+ waitForAbort(outerFlow).then(assertIsStubError)
1700
+ ]);
1701
+ });
1702
+
1703
+ it('testFailsIfErrbackThrows', function() {
1704
+ promise.rejected('').then(null, throwStubError);
1705
+ return waitForAbort().then(assertIsStubError);
1706
+ });
1707
+
1708
+ it('testFailsIfCallbackReturnsRejectedPromise', function() {
1709
+ promise.fulfilled().then(function() {
1710
+ return promise.rejected(new StubError);
1711
+ });
1712
+ return waitForAbort().then(assertIsStubError);
1713
+ });
1714
+
1715
+ it('testAbortsFrameIfTaskFails', function() {
1716
+ promise.fulfilled().then(function() {
1717
+ promise.controlFlow().execute(throwStubError);
1718
+ });
1719
+ return waitForAbort().then(assertIsStubError);
1720
+ });
1721
+
1722
+ it('testAbortsFramePromisedChainedFromTaskIsNotHandled', function() {
1723
+ promise.fulfilled().then(function() {
1724
+ promise.controlFlow().execute(function() {}).
1725
+ then(throwStubError);
1726
+ });
1727
+ return waitForAbort().then(assertIsStubError);
1728
+ });
1729
+
1730
+ it('testTrapsChainedUnhandledRejectionsWithinAFrame', function() {
1731
+ var pair = callbackPair(null, assertIsStubError);
1732
+ promise.fulfilled().then(function() {
1733
+ promise.controlFlow().execute(function() {}).
1734
+ then(throwStubError);
1735
+ }).then(pair.callback, pair.errback);
1736
+
1737
+ return waitForIdle().then(pair.assertErrback);
1738
+ });
1739
+
1740
+ it('testCancelsRemainingTasksIfFrameThrowsDuringScheduling', function() {
1741
+ var task1, task2;
1742
+ var pair = callbackPair(null, assertIsStubError);
1743
+ var flow = promise.controlFlow();
1744
+ flow.execute(function() {
1745
+ task1 = flow.execute(function() {});
1746
+ task2 = flow.execute(function() {});
1747
+ throw new StubError;
1748
+ }).then(pair.callback, pair.errback);
1749
+
1750
+ return waitForIdle().
1751
+ then(pair.assertErrback).
1752
+ then(function() {
1753
+ assert.ok(!task1.isPending());
1754
+ pair = callbackPair();
1755
+ return task1.then(pair.callback, pair.errback);
1756
+ }).
1757
+ then(function() {
1758
+ pair.assertErrback();
1759
+ assert.ok(!task2.isPending());
1760
+ pair = callbackPair();
1761
+ return task2.then(pair.callback, pair.errback);
1762
+ }).
1763
+ then(function() {
1764
+ pair.assertErrback();
1765
+ });
1766
+ });
1767
+
1768
+ it('testCancelsRemainingTasksInFrameIfATaskFails', function() {
1769
+ var task;
1770
+ var pair = callbackPair(null, assertIsStubError);
1771
+ var flow = promise.controlFlow();
1772
+ flow.execute(function() {
1773
+ flow.execute(throwStubError);
1774
+ task = flow.execute(function() {});
1775
+ }).then(pair.callback, pair.errback);
1776
+
1777
+ return waitForIdle().then(pair.assertErrback).then(function() {
1778
+ assert.ok(!task.isPending());
1779
+ pair = callbackPair();
1780
+ task.then(pair.callback, pair.errback);
1781
+ }).then(function() {
1782
+ pair.assertErrback();
1783
+ });
1784
+ });
1785
+
1786
+ it('testDoesNotModifyRejectionErrorIfPromiseNotInsideAFlow', function() {
1787
+ var error = Error('original message');
1788
+ var originalStack = error.stack;
1789
+ var originalStr = error.toString();
1790
+
1791
+ var pair = callbackPair(null, function(e) {
1792
+ assert.equal(error, e);
1793
+ assert.equal('original message', e.message);
1794
+ assert.equal(originalStack, e.stack);
1795
+ assert.equal(originalStr, e.toString());
1796
+ });
1797
+
1798
+ promise.rejected(error).then(pair.callback, pair.errback);
1799
+ return waitForIdle().then(pair.assertErrback);
1800
+ });
1801
+
1802
+ /** See https://github.com/SeleniumHQ/selenium/issues/444 */
1803
+ it('testMaintainsOrderWithPromiseChainsCreatedWithinAForeach_1', function() {
1804
+ var messages = [];
1805
+ flow.execute(function() {
1806
+ return promise.fulfilled(['a', 'b', 'c', 'd']);
1807
+ }, 'start').then(function(steps) {
1808
+ steps.forEach(function(step) {
1809
+ promise.fulfilled(step)
1810
+ .then(function() {
1811
+ messages.push(step + '.1');
1812
+ }).then(function() {
1813
+ messages.push(step + '.2');
1814
+ });
1815
+ })
1816
+ });
1817
+ return waitForIdle().then(function() {
1818
+ assert.deepEqual(
1819
+ ['a.1', 'a.2', 'b.1', 'b.2', 'c.1', 'c.2', 'd.1', 'd.2'],
1820
+ messages);
1821
+ });
1822
+ });
1823
+
1824
+ /** See https://github.com/SeleniumHQ/selenium/issues/444 */
1825
+ it('testMaintainsOrderWithPromiseChainsCreatedWithinAForeach_2', function() {
1826
+ var messages = [];
1827
+ flow.execute(function() {
1828
+ return promise.fulfilled(['a', 'b', 'c', 'd']);
1829
+ }, 'start').then(function(steps) {
1830
+ steps.forEach(function(step) {
1831
+ promise.fulfilled(step)
1832
+ .then(function() {
1833
+ messages.push(step + '.1');
1834
+ }).then(function() {
1835
+ flow.execute(function() {}, step + '.2').then(function() {
1836
+ messages.push(step + '.2');
1837
+ });
1838
+ });
1839
+ })
1840
+ });
1841
+ return waitForIdle().then(function() {
1842
+ assert.deepEqual(
1843
+ ['a.1', 'a.2', 'b.1', 'b.2', 'c.1', 'c.2', 'd.1', 'd.2'],
1844
+ messages);
1845
+ });
1846
+ });
1847
+
1848
+ /** See https://github.com/SeleniumHQ/selenium/issues/444 */
1849
+ it('testMaintainsOrderWithPromiseChainsCreatedWithinAForeach_3', function() {
1850
+ var messages = [];
1851
+ flow.execute(function() {
1852
+ return promise.fulfilled(['a', 'b', 'c', 'd']);
1853
+ }, 'start').then(function(steps) {
1854
+ steps.forEach(function(step) {
1855
+ promise.fulfilled(step)
1856
+ .then(function(){})
1857
+ .then(function() {
1858
+ messages.push(step + '.1');
1859
+ return flow.execute(function() {}, step + '.1');
1860
+ }).then(function() {
1861
+ flow.execute(function() {}, step + '.2').then(function(text) {
1862
+ messages.push(step + '.2');
1863
+ });
1864
+ });
1865
+ })
1866
+ });
1867
+ return waitForIdle().then(function() {
1868
+ assert.deepEqual(
1869
+ ['a.1', 'a.2', 'b.1', 'b.2', 'c.1', 'c.2', 'd.1', 'd.2'],
1870
+ messages);
1871
+ });
1872
+ });
1873
+
1874
+ /** See https://github.com/SeleniumHQ/selenium/issues/363 */
1875
+ it('testTasksScheduledInASeparateTurnOfTheEventLoopGetASeparateTaskQueue_2', function() {
1876
+ scheduleAction('a', () => promise.delayed(10));
1877
+ schedule('b');
1878
+ setTimeout(() => schedule('c'), 0);
1879
+
1880
+ return waitForIdle().then(function() {
1881
+ assertFlowHistory('a', 'c', 'b');
1882
+ });
1883
+ });
1884
+
1885
+ /** See https://github.com/SeleniumHQ/selenium/issues/363 */
1886
+ it('testTasksScheduledInASeparateTurnOfTheEventLoopGetASeparateTaskQueue_2', function() {
1887
+ scheduleAction('a', () => promise.delayed(10));
1888
+ schedule('b');
1889
+ schedule('c');
1890
+ setTimeout(function() {
1891
+ schedule('d');
1892
+ scheduleAction('e', () => promise.delayed(10));
1893
+ schedule('f');
1894
+ }, 0);
1895
+
1896
+ return waitForIdle().then(function() {
1897
+ assertFlowHistory('a', 'd', 'e', 'b', 'c', 'f');
1898
+ });
1899
+ });
1900
+
1901
+ /** See https://github.com/SeleniumHQ/selenium/issues/363 */
1902
+ it('testCanSynchronizeTasksFromAdjacentTaskQueues', function() {
1903
+ var task1 = scheduleAction('a', () => promise.delayed(10));
1904
+ schedule('b');
1905
+ setTimeout(function() {
1906
+ scheduleAction('c', () => task1);
1907
+ schedule('d');
1908
+ }, 0);
1909
+
1910
+ return waitForIdle().then(function() {
1911
+ assertFlowHistory('a', 'c', 'd', 'b');
1912
+ });
1913
+ });
1914
+
1915
+ describe('testCancellingAScheduledTask', function() {
1916
+ it('1', function() {
1917
+ var called = false;
1918
+ var task1 = scheduleAction('a', () => called = true);
1919
+ task1.cancel('no soup for you');
1920
+
1921
+ return waitForIdle().then(function() {
1922
+ assert.ok(!called);
1923
+ assertFlowHistory();
1924
+ return task1.catch(function(e) {
1925
+ assert.ok(e instanceof promise.CancellationError);
1926
+ assert.equal('no soup for you', e.message);
1927
+ });
1928
+ });
1929
+ });
1930
+
1931
+ it('2', function() {
1932
+ schedule('a');
1933
+ var called = false;
1934
+ var task2 = scheduleAction('b', () => called = true);
1935
+ schedule('c');
1936
+
1937
+ task2.cancel('no soup for you');
1938
+
1939
+ return waitForIdle().then(function() {
1940
+ assert.ok(!called);
1941
+ assertFlowHistory('a', 'c');
1942
+ return task2.catch(function(e) {
1943
+ assert.ok(e instanceof promise.CancellationError);
1944
+ assert.equal('no soup for you', e.message);
1945
+ });
1946
+ });
1947
+ });
1948
+
1949
+ it('3', function() {
1950
+ var called = false;
1951
+ var task = scheduleAction('a', () => called = true);
1952
+ task.cancel(new StubError);
1953
+
1954
+ return waitForIdle().then(function() {
1955
+ assert.ok(!called);
1956
+ assertFlowHistory();
1957
+ return task.catch(function(e) {
1958
+ assert.ok(e instanceof promise.CancellationError);
1959
+ });
1960
+ });
1961
+ });
1962
+
1963
+ it('4', function() {
1964
+ var seen = [];
1965
+ var task = scheduleAction('a', () => seen.push(1))
1966
+ .then(() => seen.push(2))
1967
+ .then(() => seen.push(3))
1968
+ .then(() => seen.push(4))
1969
+ .then(() => seen.push(5));
1970
+ task.cancel(new StubError);
1971
+
1972
+ return waitForIdle().then(function() {
1973
+ assert.deepEqual([], seen);
1974
+ assertFlowHistory();
1975
+ return task.catch(function(e) {
1976
+ assert.ok(e instanceof promise.CancellationError);
1977
+ });
1978
+ });
1979
+ });
1980
+
1981
+ it('fromWithinAnExecutingTask', function() {
1982
+ var called = false;
1983
+ var task;
1984
+ scheduleAction('a', function() {
1985
+ task.cancel('no soup for you');
1986
+ });
1987
+ task = scheduleAction('b', () => called = true);
1988
+ schedule('c');
1989
+
1990
+ return waitForIdle().then(function() {
1991
+ assert.ok(!called);
1992
+ assertFlowHistory('a', 'c');
1993
+ return task.catch(function(e) {
1994
+ assert.ok(e instanceof promise.CancellationError);
1995
+ assert.equal('no soup for you', e.message);
1996
+ });
1997
+ });
1998
+ });
1999
+ });
2000
+
2001
+ it('testCancellingAPendingTask', function() {
2002
+ var order = [];
2003
+ var unresolved = promise.defer();
2004
+
2005
+ var innerTask;
2006
+ var outerTask = scheduleAction('a', function() {
2007
+ order.push(1);
2008
+
2009
+ // Schedule a task that will never finish.
2010
+ innerTask = scheduleAction('a.1', function() {
2011
+ return unresolved.promise;
2012
+ });
2013
+
2014
+ // Since the outerTask is cancelled below, innerTask should be cancelled
2015
+ // with a DiscardedTaskError, which means its callbacks are silently
2016
+ // dropped - so this should never execute.
2017
+ innerTask.catch(function(e) {
2018
+ order.push(2);
2019
+ });
2020
+ });
2021
+ schedule('b');
2022
+
2023
+ outerTask.catch(function(e) {
2024
+ order.push(3);
2025
+ assert.ok(e instanceof promise.CancellationError);
2026
+ assert.equal('no soup for you', e.message);
2027
+ });
2028
+
2029
+ unresolved.promise.catch(function(e) {
2030
+ order.push(4);
2031
+ assert.ok(e instanceof promise.CancellationError);
2032
+ });
2033
+
2034
+ return timeout(10).then(function() {
2035
+ assert.deepEqual([1], order);
2036
+ assert.ok(unresolved.promise.isPending());
2037
+
2038
+ outerTask.cancel('no soup for you');
2039
+ return waitForIdle();
2040
+ }).then(function() {
2041
+ assertFlowHistory('a', 'a.1', 'b');
2042
+ assert.deepEqual([1, 3, 4], order);
2043
+ });
2044
+ });
2045
+
2046
+ it('testCancellingAPendingPromiseCallback', function() {
2047
+ var called = false;
2048
+
2049
+ var root = promise.fulfilled();
2050
+ root.then(function() {
2051
+ cb2.cancel('no soup for you');
2052
+ });
2053
+
2054
+ var cb2 = root.then(fail, fail); // These callbacks should never be called.
2055
+ cb2.then(fail, function(e) {
2056
+ called = true;
2057
+ assert.ok(e instanceof promise.CancellationError);
2058
+ assert.equal('no soup for you', e.message);
2059
+ });
2060
+
2061
+ return waitForIdle().then(function() {
2062
+ assert.ok(called);
2063
+ });
2064
+ });
2065
+
2066
+ describe('testResetFlow', function() {
2067
+ it('1', function() {
2068
+ var called = 0;
2069
+ var task = flow.execute(() => called++);
2070
+ task.thenFinally(() => called++);
2071
+
2072
+ return new Promise(function(fulfill) {
2073
+ flow.once('reset', fulfill);
2074
+ flow.reset();
2075
+
2076
+ }).then(function() {
2077
+ assert.equal(0, called);
2078
+ assert.ok(!task.isPending());
2079
+ return task;
2080
+
2081
+ }).then(fail, function(e) {
2082
+ assert.ok(e instanceof promise.CancellationError);
2083
+ assert.equal('ControlFlow was reset', e.message);
2084
+ });
2085
+ });
2086
+
2087
+ it('2', function() {
2088
+ var called = 0;
2089
+ var task1 = flow.execute(() => called++);
2090
+ task1.thenFinally(() => called++);
2091
+
2092
+ var task2 = flow.execute(() => called++);
2093
+ task2.thenFinally(() => called++);
2094
+
2095
+ var task3 = flow.execute(() => called++);
2096
+ task3.thenFinally(() => called++);
2097
+
2098
+ return new Promise(function(fulfill) {
2099
+ flow.once('reset', fulfill);
2100
+ flow.reset();
2101
+
2102
+ }).then(function() {
2103
+ assert.equal(0, called);
2104
+ assert.ok(!task1.isPending());
2105
+ assert.ok(!task2.isPending());
2106
+ assert.ok(!task3.isPending());
2107
+ });
2108
+ });
2109
+ });
2110
+
2111
+ describe('testPromiseFulfilledInsideTask', function() {
2112
+ it('1', function() {
2113
+ var order = [];
2114
+
2115
+ flow.execute(function() {
2116
+ var d = promise.defer();
2117
+
2118
+ d.promise.then(() => order.push('a'));
2119
+ d.promise.then(() => order.push('b'));
2120
+ d.promise.then(() => order.push('c'));
2121
+ d.fulfill();
2122
+
2123
+ flow.execute(() => order.push('d'));
2124
+
2125
+ }).then(() => order.push('fin'));
2126
+
2127
+ return waitForIdle().then(function() {
2128
+ assert.deepEqual(['a', 'b', 'c', 'd', 'fin'], order);
2129
+ });
2130
+ });
2131
+
2132
+ it('2', function() {
2133
+ var order = [];
2134
+
2135
+ flow.execute(function() {
2136
+ flow.execute(() => order.push('a'));
2137
+ flow.execute(() => order.push('b'));
2138
+
2139
+ var d = promise.defer();
2140
+ d.promise.then(() => order.push('c'));
2141
+ d.promise.then(() => order.push('d'));
2142
+ d.fulfill();
2143
+
2144
+ flow.execute(() => order.push('e'));
2145
+
2146
+ }).then(() => order.push('fin'));
2147
+
2148
+ return waitForIdle().then(function() {
2149
+ assert.deepEqual(['a', 'b', 'c', 'd', 'e', 'fin'], order);
2150
+ });
2151
+ });
2152
+
2153
+ it('3', function() {
2154
+ var order = [];
2155
+ var d = promise.defer();
2156
+ d.promise.then(() => order.push('c'));
2157
+ d.promise.then(() => order.push('d'));
2158
+
2159
+ flow.execute(function() {
2160
+ flow.execute(() => order.push('a'));
2161
+ flow.execute(() => order.push('b'));
2162
+
2163
+ d.promise.then(() => order.push('e'));
2164
+ d.fulfill();
2165
+
2166
+ flow.execute(() => order.push('f'));
2167
+
2168
+ }).then(() => order.push('fin'));
2169
+
2170
+ return waitForIdle().then(function() {
2171
+ assert.deepEqual(['c', 'd', 'a', 'b', 'e', 'f', 'fin'], order);
2172
+ });
2173
+ });
2174
+
2175
+ it('4', function() {
2176
+ var order = [];
2177
+ var d = promise.defer();
2178
+ d.promise.then(() => order.push('a'));
2179
+ d.promise.then(() => order.push('b'));
2180
+
2181
+ flow.execute(function() {
2182
+ flow.execute(function() {
2183
+ order.push('c');
2184
+ flow.execute(() => order.push('d'));
2185
+ d.promise.then(() => order.push('e'));
2186
+ });
2187
+ flow.execute(() => order.push('f'));
2188
+
2189
+ d.promise.then(() => order.push('g'));
2190
+ d.fulfill();
2191
+
2192
+ flow.execute(() => order.push('h'));
2193
+
2194
+ }).then(() => order.push('fin'));
2195
+
2196
+ return waitForIdle().then(function() {
2197
+ assert.deepEqual(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'fin'], order);
2198
+ });
2199
+ });
2200
+ });
2201
+
2202
+ describe('testSettledPromiseCallbacksInsideATask', function() {
2203
+ it('1', function() {
2204
+ var order = [];
2205
+ var p = promise.fulfilled();
2206
+
2207
+ flow.execute(function() {
2208
+ flow.execute(() => order.push('a'));
2209
+ p.then(() => order.push('b'));
2210
+ flow.execute(() => order.push('c'));
2211
+ p.then(() => order.push('d'));
2212
+ }).then(() => order.push('fin'));
2213
+
2214
+ return waitForIdle().then(function() {
2215
+ assert.deepEqual(['a', 'b', 'c', 'd', 'fin'], order);
2216
+ });
2217
+ });
2218
+
2219
+ it('2', function() {
2220
+ var order = [];
2221
+
2222
+ flow.execute(function() {
2223
+ flow.execute(() => order.push('a'))
2224
+ .then( () => order.push('c'));
2225
+ flow.execute(() => order.push('b'));
2226
+ }).then(() => order.push('fin'));
2227
+
2228
+ return waitForIdle().then(function() {
2229
+ assert.deepEqual(['a', 'c', 'b', 'fin'], order);
2230
+ });
2231
+ });
2232
+ });
2233
+
2234
+ it('testTasksDoNotWaitForNewlyCreatedPromises', function() {
2235
+ var order = [];
2236
+
2237
+ flow.execute(function() {
2238
+ var d = promise.defer();
2239
+
2240
+ // This is a normal promise, not a task, so the task for this callback is
2241
+ // considered volatile. Volatile tasks should be skipped when they reach
2242
+ // the front of the task queue.
2243
+ d.promise.then(() => order.push('a'));
2244
+
2245
+ flow.execute(() => order.push('b'));
2246
+ flow.execute(function() {
2247
+ flow.execute(() => order.push('c'));
2248
+ d.promise.then(() => order.push('d'));
2249
+ d.fulfill();
2250
+ });
2251
+ flow.execute(() => order.push('e'));
2252
+
2253
+ }).then(() => order.push('fin'));
2254
+
2255
+ return waitForIdle().then(function() {
2256
+ assert.deepEqual(['b', 'a', 'c', 'd', 'e', 'fin'], order);
2257
+ });
2258
+ });
2259
+
2260
+ it('testCallbackDependenciesDoNotDeadlock', function() {
2261
+ var order = [];
2262
+ var root = promise.defer();
2263
+ var dep = promise.fulfilled().then(function() {
2264
+ order.push('a');
2265
+ return root.promise.then(function() {
2266
+ order.push('b');
2267
+ });
2268
+ });
2269
+ // This callback depends on |dep|, which depends on another callback
2270
+ // attached to |root| via a chain.
2271
+ root.promise.then(function() {
2272
+ order.push('c');
2273
+ return dep.then(() => order.push('d'));
2274
+ }).then(() => order.push('fin'));
2275
+
2276
+ setTimeout(() => root.fulfill(), 20);
2277
+
2278
+ return waitForIdle().then(function() {
2279
+ assert.deepEqual(['a', 'b', 'c', 'd', 'fin'], order);
2280
+ });
2281
+ });
2282
+ });