/src/sap.ui.core/test/sap/ui/core/qunit/ValidationHooks.qunit.html

https://gitlab.com/crsr/openui5 · HTML · 415 lines · 371 code · 38 blank · 6 comment · 0 complexity · 30b00ed7450e4c2ad81d035b8b3caa0d MD5 · raw file

  1. <!DOCTYPE HTML>
  2. <!--
  3. Tested validation hooks for types
  4. Author: d050084
  5. -->
  6. <html>
  7. <head>
  8. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  9. <!-- Initialization -->
  10. <script id="sap-ui-bootstrap" type="text/javascript"
  11. src="../../../../../resources/sap-ui-core.js"
  12. data-sap-ui-theme="sap_bluecrystal" data-sap-ui-libs="sap.ui.commons">
  13. </script>
  14. <link rel="stylesheet"
  15. href="../../../../../resources/sap/ui/thirdparty/qunit.css" type="text/css"
  16. media="screen" />
  17. <script type="text/javascript"
  18. src="../../../../../resources/sap/ui/thirdparty/qunit.js"></script>
  19. <script type="text/javascript"
  20. src="../../../../../resources/sap/ui/qunit/qunit-junit.js"></script>
  21. <script type="text/javascript"
  22. src="../../../../../resources/sap/ui/qunit/QUnitUtils.js"></script>
  23. <!-- Test functions -->
  24. <script language="javascript">
  25. var oModel;
  26. var oTxt;
  27. function setup(sPath, oType){
  28. oModel = new sap.ui.model.json.JSONModel();
  29. oModel.setData({
  30. visibleItems : 3,
  31. test : "hello",
  32. rating : "4"
  33. });
  34. sap.ui.getCore().setModel(oModel);
  35. oTxt = new sap.ui.commons.TextField();
  36. oTxt.bindValue(sPath, oType);
  37. oTxt.placeAt("target1");
  38. };
  39. function teardown(){
  40. oTxt.destroy();
  41. }
  42. asyncTest("validation error", function(){
  43. var oType = new sap.ui.model.type.String(null, {
  44. minLength : 1,
  45. maxLength : 5
  46. });
  47. setup("/test", oType);
  48. var bSuccess = false;
  49. sap.ui.getCore().attachValidationError(handler);
  50. function handler(oEvent){
  51. equal(oEvent.getParameter("element").getId(), oTxt.getId(), "element");
  52. equal(oEvent.getParameter('property'), "value", "property");
  53. equal(oEvent.getParameter("newValue"), "blubbbbb", "new value");
  54. equal(oEvent.getParameter("oldValue"), "hello", "old value");
  55. equal(oEvent.getParameter("type"), oType, "type");
  56. ok(oEvent.getParameter("exception") instanceof sap.ui.model.ValidateException, "exception instance");
  57. equal(oEvent.getParameter("exception").violatedConstraints.length, 1, " violated constraints size");
  58. equal(oEvent.getParameter("exception").violatedConstraints[0], "maxLength", "violated constraints check");
  59. bSuccess = true;
  60. };
  61. oTxt.setValue("blubbbbb");
  62. setTimeout(function() { // delay the following test
  63. start(); // resume normal testing
  64. ok(bSuccess, "error occurred sucessfully");
  65. sap.ui.getCore().detachValidationError(handler);
  66. teardown(handler);
  67. }, 100);
  68. });
  69. asyncTest("parse error", function(){
  70. var oType = new sap.ui.model.type.Integer();
  71. setup("/visibleItems", oType);
  72. var bSuccess = false;
  73. sap.ui.getCore().attachParseError(handler);
  74. function handler(oEvent){
  75. equal(oEvent.getParameter("element").getId(), oTxt.getId(), "element");
  76. equal(oEvent.getParameter('property'), "value", "property");
  77. equal(oEvent.getParameter("newValue"), "blubbbbb", "new value");
  78. equal(oEvent.getParameter("oldValue"), 3, "old value");
  79. equal(oEvent.getParameter("type"), oType, "type");
  80. ok(oEvent.getParameter("exception") instanceof sap.ui.model.ParseException, "exception instance");
  81. bSuccess = true;
  82. };
  83. oTxt.setValue("blubbbbb");
  84. setTimeout(function() { // delay the following test
  85. start(); // resume normal testing
  86. ok(bSuccess, "error occurred sucessfully");
  87. sap.ui.getCore().detachParseError(handler);
  88. teardown(handler);
  89. }, 100);
  90. });
  91. asyncTest("format error", function(){
  92. var oType = new sap.ui.model.type.String();
  93. var oRating = new sap.ui.commons.RatingIndicator();
  94. oRating.bindValue("/rating", oType);
  95. oRating.placeAt("target2");
  96. setup("/rating", oType);
  97. var bSuccess = false;
  98. sap.ui.getCore().attachFormatError(handler);
  99. function handler(oEvent){
  100. equal(oEvent.getParameter("element").getId(), oRating.getId(), "element");
  101. equal(oEvent.getParameter('property'), "value", "property");
  102. equal(oEvent.getParameter("newValue"), "blubbbbb", "new value");
  103. equal(oEvent.getParameter("oldValue"), 4, "old value");
  104. equal(oEvent.getParameter("type"), oType, "type");
  105. ok(oEvent.getParameter("exception") instanceof sap.ui.model.FormatException, "exception instance");
  106. bSuccess = true;
  107. };
  108. oTxt.setValue("blubbbbb");
  109. setTimeout(function() { // delay the following test
  110. start(); // resume normal testing
  111. ok(bSuccess, "error occurred sucessfully");
  112. sap.ui.getCore().detachFormatError(handler);
  113. oRating.destroy();
  114. teardown(handler);
  115. }, 100);
  116. });
  117. asyncTest("validation success", function(){
  118. var oType = new sap.ui.model.type.String();
  119. setup("/test", oType);
  120. var bSuccess = false;
  121. sap.ui.getCore().attachValidationSuccess(handler);
  122. function handler(oEvent){
  123. equal(oEvent.getParameter("element").getId(), oTxt.getId(), "element");
  124. equal(oEvent.getParameter('property'), "value", "property");
  125. equal(oEvent.getParameter("newValue"), "blubbbbb", "new value");
  126. equal(oEvent.getParameter("oldValue"), "hello", "old value");
  127. equal(oEvent.getParameter("type"), oType, "type");
  128. bSuccess = true;
  129. };
  130. oTxt.setValue("blubbbbb");
  131. setTimeout(function() { // delay the following test
  132. start(); // resume normal testing
  133. ok(bSuccess, "validation was sucessful");
  134. sap.ui.getCore().detachValidationSuccess(handler);
  135. teardown(handler);
  136. }, 100);
  137. });
  138. asyncTest("event checks 1", function(){
  139. var oType = new sap.ui.model.type.Integer();
  140. setup("/visibleItems", oType);
  141. var bSuccess = false;
  142. var iEventCount = 0;
  143. sap.ui.getCore().attachParseError(handler);
  144. sap.ui.getCore().attachValidationError(handler2);
  145. sap.ui.getCore().attachFormatError(handler3);
  146. sap.ui.getCore().attachValidationSuccess(handler4);
  147. function handler(oEvent){
  148. bSuccess = true;
  149. iEventCount ++;
  150. };
  151. function handler2(oEvent){
  152. bSuccess = false;
  153. iEventCount ++;
  154. };
  155. function handler3(oEvent){
  156. bSuccess = false;
  157. iEventCount ++;
  158. };
  159. function handler4(oEvent){
  160. bSuccess = false;
  161. iEventCount ++;
  162. };
  163. oTxt.setValue("blubbbbb");
  164. setTimeout(function() { // delay the following test
  165. start(); // resume normal testing
  166. ok(bSuccess, "parse error occurred sucessfully and no other events occurred");
  167. ok(iEventCount == 1 , "event count should be 1");
  168. sap.ui.getCore().detachParseError(handler);
  169. sap.ui.getCore().detachFormatError(handler);
  170. sap.ui.getCore().detachValidationError(handler);
  171. sap.ui.getCore().detachValidationSuccess(handler);
  172. teardown(handler);
  173. }, 100);
  174. });
  175. asyncTest("event checks 2", function(){
  176. var oType = new sap.ui.model.type.String(null, {
  177. minLength : 1,
  178. maxLength : 5
  179. });
  180. setup("/test", oType);
  181. var iEventCount = 0;
  182. var bSuccess = false;
  183. sap.ui.getCore().attachParseError(handler);
  184. sap.ui.getCore().attachValidationError(handler2);
  185. sap.ui.getCore().attachFormatError(handler3);
  186. sap.ui.getCore().attachValidationSuccess(handler4);
  187. function handler(oEvent){
  188. bSuccess = false;
  189. iEventCount ++;
  190. };
  191. function handler2(oEvent){
  192. bSuccess = true;
  193. iEventCount ++;
  194. };
  195. function handler3(oEvent){
  196. bSuccess = false;
  197. iEventCount ++;
  198. };
  199. function handler4(oEvent){
  200. bSuccess = false;
  201. iEventCount ++;
  202. };
  203. oTxt.setValue("blubbbbb");
  204. setTimeout(function() { // delay the following test
  205. start(); // resume normal testing
  206. ok(bSuccess, "validation error occurred sucessfully and no other events occurred");
  207. ok(iEventCount == 1 , "event count should be 1");
  208. sap.ui.getCore().detachParseError(handler);
  209. sap.ui.getCore().detachFormatError(handler);
  210. sap.ui.getCore().detachValidationError(handler);
  211. sap.ui.getCore().detachValidationSuccess(handler);
  212. teardown(handler);
  213. }, 100);
  214. });
  215. asyncTest("validation success on core and control --> bubbling", function(){
  216. var oType = new sap.ui.model.type.String();
  217. setup("/test", oType);
  218. var bSuccess = false;
  219. sap.ui.getCore().attachValidationSuccess(handler);
  220. oTxt.attachValidationSuccess(handler);
  221. var iCount = 0;
  222. function handler(oEvent){
  223. equal(oEvent.getParameter("element").getId(), oTxt.getId(), "element");
  224. equal(oEvent.getParameter('property'), "value", "property");
  225. equal(oEvent.getParameter("newValue"), "blubbbbb", "new value");
  226. equal(oEvent.getParameter("oldValue"), "hello", "old value");
  227. equal(oEvent.getParameter("type"), oType, "type");
  228. iCount++;
  229. bSuccess = true;
  230. };
  231. oTxt.setValue("blubbbbb");
  232. setTimeout(function() { // delay the following test
  233. start(); // resume normal testing
  234. ok(bSuccess, "validation was sucessful");
  235. equal(iCount, 2, "validation event count");
  236. sap.ui.getCore().detachValidationSuccess(handler);
  237. oTxt.detachValidationSuccess(handler);
  238. teardown(handler);
  239. }, 100);
  240. });
  241. asyncTest("validation error on core and control --> bubbling", function(){
  242. var oType = new sap.ui.model.type.String(null, {
  243. minLength : 1,
  244. maxLength : 5
  245. });
  246. setup("/test", oType);
  247. var bSuccess = false;
  248. sap.ui.getCore().attachValidationError(handler);
  249. oTxt.attachValidationError(handler);
  250. var iCount = 0;
  251. function handler(oEvent){
  252. equal(oEvent.getParameter("element").getId(), oTxt.getId(), "element");
  253. equal(oEvent.getParameter('property'), "value", "property");
  254. equal(oEvent.getParameter("newValue"), "blubbbbb", "new value");
  255. equal(oEvent.getParameter("oldValue"), "hello", "old value");
  256. equal(oEvent.getParameter("type"), oType, "type");
  257. ok(oEvent.getParameter("exception") instanceof sap.ui.model.ValidateException, "exception instance");
  258. equal(oEvent.getParameter("exception").violatedConstraints.length, 1, " violated constraints size");
  259. equal(oEvent.getParameter("exception").violatedConstraints[0], "maxLength", "violated constraints check");
  260. bSuccess = true;
  261. iCount++;
  262. };
  263. oTxt.setValue("blubbbbb");
  264. setTimeout(function() { // delay the following test
  265. start(); // resume normal testing
  266. ok(bSuccess, "error occurred sucessfully");
  267. equal(iCount, 2, "validation event count");
  268. sap.ui.getCore().detachValidationError(handler);
  269. oTxt.detachValidationError(handler);
  270. teardown(handler);
  271. }, 100);
  272. });
  273. asyncTest("validation error on core and control --> cancel bubbling", function(){
  274. var oType = new sap.ui.model.type.String(null, {
  275. minLength : 1,
  276. maxLength : 5
  277. });
  278. setup("/test", oType);
  279. var bSuccess = false;
  280. sap.ui.getCore().attachValidationError(handler);
  281. oTxt.attachValidationError(handler);
  282. var iCount = 0;
  283. function handler(oEvent){
  284. equal(oEvent.getParameter("element").getId(), oTxt.getId(), "element");
  285. equal(oEvent.getParameter('property'), "value", "property");
  286. equal(oEvent.getParameter("newValue"), "blubbbbb", "new value");
  287. equal(oEvent.getParameter("oldValue"), "hello", "old value");
  288. equal(oEvent.getParameter("type"), oType, "type");
  289. ok(oEvent.getParameter("exception") instanceof sap.ui.model.ValidateException, "exception instance");
  290. equal(oEvent.getParameter("exception").violatedConstraints.length, 1, " violated constraints size");
  291. equal(oEvent.getParameter("exception").violatedConstraints[0], "maxLength", "violated constraints check");
  292. bSuccess = true;
  293. iCount++;
  294. oEvent.cancelBubble();
  295. };
  296. oTxt.setValue("blubbbbb");
  297. setTimeout(function() { // delay the following test
  298. start(); // resume normal testing
  299. ok(bSuccess, "error occurred sucessfully");
  300. equal(iCount, 1, "validation event count");
  301. sap.ui.getCore().detachValidationError(handler);
  302. oTxt.detachValidationError(handler);
  303. teardown(handler);
  304. }, 100);
  305. });
  306. asyncTest("parse error on core and control --> bubbling", function(){
  307. var oType = new sap.ui.model.type.Integer();
  308. setup("/visibleItems", oType);
  309. var bSuccess = false;
  310. oTxt.attachParseError(handler);
  311. sap.ui.getCore().attachParseError(handler);
  312. var iCount = 0;
  313. function handler(oEvent){
  314. equal(oEvent.getParameter("element").getId(), oTxt.getId(), "element");
  315. equal(oEvent.getParameter('property'), "value", "property");
  316. equal(oEvent.getParameter("newValue"), "blubbbbb", "new value");
  317. equal(oEvent.getParameter("oldValue"), 3, "old value");
  318. equal(oEvent.getParameter("type"), oType, "type");
  319. ok(oEvent.getParameter("exception") instanceof sap.ui.model.ParseException, "exception instance");
  320. bSuccess = true;
  321. iCount++;
  322. };
  323. oTxt.setValue("blubbbbb");
  324. setTimeout(function() { // delay the following test
  325. start(); // resume normal testing
  326. ok(bSuccess, "error occurred sucessfully");
  327. oTxt.detachParseError(handler);
  328. equal(iCount, 2, "validation event count");
  329. sap.ui.getCore().detachParseError(handler);
  330. teardown(handler);
  331. }, 100);
  332. });
  333. asyncTest("format error on core and control --> cancel bubbling", function(){
  334. var oType = new sap.ui.model.type.String();
  335. var oRating = new sap.ui.commons.RatingIndicator();
  336. oRating.bindValue("/rating", oType);
  337. oRating.placeAt("target2");
  338. setup("/rating", oType);
  339. var bSuccess = false;
  340. sap.ui.getCore().attachFormatError(handler);
  341. oTxt.attachFormatError(handler);
  342. var iCount = 0;
  343. function handler(oEvent){
  344. equal(oEvent.getParameter("element").getId(), oRating.getId(), "element");
  345. equal(oEvent.getParameter('property'), "value", "property");
  346. equal(oEvent.getParameter("newValue"), "blubbbbb", "new value");
  347. equal(oEvent.getParameter("oldValue"), 4, "old value");
  348. equal(oEvent.getParameter("type"), oType, "type");
  349. ok(oEvent.getParameter("exception") instanceof sap.ui.model.FormatException, "exception instance");
  350. bSuccess = true;
  351. iCount++;
  352. oEvent.cancelBubble();
  353. };
  354. oTxt.setValue("blubbbbb");
  355. setTimeout(function() { // delay the following test
  356. start(); // resume normal testing
  357. ok(bSuccess, "error occurred sucessfully");
  358. sap.ui.getCore().detachFormatError(handler);
  359. oTxt.detachFormatError(handler);
  360. equal(iCount, 1, "validation event count");
  361. oRating.destroy();
  362. teardown(handler);
  363. }, 100);
  364. });
  365. </script>
  366. </head>
  367. <body>
  368. <h1 id="qunit-header">QUnit tests: Validation Hooks</h1>
  369. <h2 id="qunit-banner"></h2>
  370. <h2 id="qunit-userAgent"></h2>
  371. <div id="qunit-testrunner-toolbar"></div>
  372. <ol id="qunit-tests"></ol>
  373. <br>
  374. <div id="target1"></div>
  375. <div id="target2"></div>
  376. </body>
  377. </html>