PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/qooxdoo/framework/source/class/qx/test/ui/form/Placeholder.js

https://github.com/Wkasel/qooxdoo
JavaScript | 325 lines | 221 code | 69 blank | 35 comment | 32 complexity | d88c35b5c6323d36995ac53395579a78 MD5 | raw file
  1. /* ************************************************************************
  2. qooxdoo - the new era of web development
  3. http://qooxdoo.org
  4. Copyright:
  5. 2004-2009 1&1 Internet AG, Germany, http://www.1und1.de
  6. License:
  7. LGPL: http://www.gnu.org/licenses/lgpl.html
  8. EPL: http://www.eclipse.org/org/documents/epl-v10.php
  9. See the LICENSE file in the project's top-level directory for details.
  10. Authors:
  11. * Martin Wittemann (martinwittemann)
  12. ************************************************************************ */
  13. qx.Class.define("qx.test.ui.form.Placeholder",
  14. {
  15. extend : qx.test.ui.LayoutTestCase,
  16. members :
  17. {
  18. __testInit : function(clazz, childControlName) {
  19. var widget = new clazz();
  20. widget.setValue("affe");
  21. widget.setPlaceholder("aaa");
  22. this.getRoot().add(widget);
  23. this.flush();
  24. this.assertEquals("affe", this.__getVisibleValueOf(widget), "placeholder visible");
  25. this.assertEquals("affe", widget.getValue(), "Wrong value returned.");
  26. this.assertFalse(this.__isPlaceholderVisible(widget));
  27. widget.destroy();
  28. widget = new clazz();
  29. widget.setPlaceholder("abc");
  30. this.getRoot().add(widget);
  31. // sync the appearance
  32. this.__syncAppearance(widget);
  33. this.assertTrue(this.__isPlaceholderVisible(widget));
  34. this.assertEquals("abc", this.__getPlaceholderValueOf(widget), "placeholder not visible");
  35. this.assertNull(widget.getValue(), "Wrong value returned.");
  36. // get rid of the widget
  37. widget.destroy();
  38. },
  39. __testChangeValue: function(clazz) {
  40. var widget = new clazz();
  41. widget.setPlaceholder("abc");
  42. this.getRoot().add(widget);
  43. // set a value
  44. widget.setValue("def");
  45. this.assertEquals("def", widget.getValue(), "wrong value");
  46. this.assertEquals("def", this.__getVisibleValueOf(widget), "wrong visible value");
  47. this.assertFalse(this.__isPlaceholderVisible(widget));
  48. // remove the value
  49. widget.resetValue();
  50. // sync the appearance
  51. this.__syncAppearance(widget);
  52. this.assertNull(widget.getValue(), "wrong value");
  53. this.assertTrue(this.__isPlaceholderVisible(widget));
  54. this.assertEquals("abc", this.__getPlaceholderValueOf(widget), "wrong visible value");
  55. // get rid of the widget
  56. widget.destroy();
  57. },
  58. __testFocus: function(clazz) {
  59. var widget = new clazz();
  60. widget.setPlaceholder("abc");
  61. this.getRoot().add(widget);
  62. // test focus in
  63. widget.focus();
  64. this.flush();
  65. this.assertEquals("", this.__getVisibleValueOf(widget), "wrong visible value after focus");
  66. this.assertFalse(this.__isPlaceholderVisible(widget));
  67. // test focus out
  68. this.getRoot().focus();
  69. this.flush();
  70. var timer = qx.util.TimerManager.getInstance();
  71. timer.start(function() {
  72. this.resume(function() {
  73. this.getRoot().focus();
  74. this.flush();
  75. this.assertTrue(this.__isPlaceholderVisible(widget));
  76. this.assertEquals("abc", this.__getPlaceholderValueOf(widget), "wrong visible value after blur");
  77. // get rid of the widget
  78. widget.destroy();
  79. }, this);
  80. }, 0, this, null, 300);
  81. this.wait();
  82. },
  83. __testRemovePlaceholder: function(clazz) {
  84. var widget = new clazz();
  85. widget.setPlaceholder("abc");
  86. widget.setPlaceholder(null);
  87. this.assertFalse(this.__isPlaceholderVisible(widget));
  88. this.assertNull(widget.getValue(), "wrong value");
  89. this.assertEquals("", this.__getVisibleValueOf(widget), "wrong visible value after focus");
  90. // get rid of the widget
  91. widget.destroy();
  92. },
  93. __testDisabled: function(clazz) {
  94. var widget = new clazz();
  95. this.getRoot().add(widget);
  96. widget.setPlaceholder("abc");
  97. widget.setEnabled(false);
  98. this.flush();
  99. this.assertNull(widget.getValue(), "wrong value");
  100. this.assertFalse(this.__isPlaceholderVisible(widget));
  101. this.assertEquals("", this.__getVisibleValueOf(widget), "wrong visible value");
  102. widget.setEnabled(true);
  103. // sync the appearance
  104. this.__syncAppearance(widget);
  105. this.assertNull(widget.getValue(), "wrong value");
  106. this.assertTrue(this.__isPlaceholderVisible(widget));
  107. this.assertEquals("abc", this.__getPlaceholderValueOf(widget), "wrong visible value");
  108. // get rid of the widget
  109. widget.destroy();
  110. },
  111. __hasTextfieldChildControl : function(widget) {
  112. return (
  113. qx.Class.isSubClassOf(widget.constructor, qx.ui.form.ComboBox) ||
  114. qx.Class.isSubClassOf(widget.constructor, qx.ui.form.DateField));
  115. },
  116. __syncAppearance : function(widget) {
  117. if (qx.Class.isSubClassOf(widget.constructor, qx.ui.form.AbstractField)) {
  118. widget.syncAppearance();
  119. } else if (this.__hasTextfieldChildControl(widget)) {
  120. widget.getChildControl("textfield").syncAppearance();
  121. }
  122. },
  123. __getVisibleValueOf: function(widget) {
  124. if (qx.Class.isSubClassOf(widget.constructor, qx.ui.form.AbstractField)) {
  125. return widget.getContentElement().getValue();
  126. } else if (this.__hasTextfieldChildControl(widget)) {
  127. return widget.getChildControl("textfield").getContentElement().getValue();
  128. }
  129. },
  130. __getPlaceholderValueOf: function(widget) {
  131. var useQxPlaceholder = !qx.core.Environment.get("css.placeholder") ||
  132. (qx.core.Environment.get("engine.name") == "gecko" &&
  133. parseFloat(qx.core.Environment.get("engine.version")) >= 2);
  134. if (!useQxPlaceholder) {
  135. if (qx.Class.isSubClassOf(widget.constructor, qx.ui.form.AbstractField)) {
  136. return widget.getContentElement().getAttribute("placeholder");
  137. } else if (this.__hasTextfieldChildControl(widget)) {
  138. return widget.getChildControl("textfield").getContentElement().getAttribute("placeholder");
  139. }
  140. } else {
  141. if (qx.Class.isSubClassOf(widget.constructor, qx.ui.form.AbstractField)) {
  142. return widget.getContainerElement().getChildren()[1].getValue();
  143. } else if (this.__hasTextfieldChildControl(widget)) {
  144. return widget.getChildControl("textfield").getContainerElement().getChildren()[1].getValue();
  145. }
  146. }
  147. },
  148. __isPlaceholderVisible: function(widget) {
  149. var useQxPlaceholder = !qx.core.Environment.get("css.placeholder") ||
  150. (qx.core.Environment.get("engine.name") == "gecko" &&
  151. parseFloat(qx.core.Environment.get("engine.version")) >= 2);
  152. if (!useQxPlaceholder) {
  153. var contentElem;
  154. if (qx.Class.isSubClassOf(widget.constructor, qx.ui.form.AbstractField)) {
  155. contentElem = widget.getContentElement();
  156. return widget.getValue() == null &&
  157. contentElem.getAttribute("placeholder") != "" &&
  158. contentElem.getAttribute("placeholder") != null &&
  159. !qx.ui.core.FocusHandler.getInstance().isFocused(widget);
  160. } else if (this.__hasTextfieldChildControl(widget)) {
  161. contentElem = widget.getChildControl("textfield").getContentElement();
  162. return widget.getChildControl("textfield").getValue() == null &&
  163. contentElem.getAttribute("placeholder") != "" &&
  164. contentElem.getAttribute("placeholder") != null &&
  165. !qx.ui.core.FocusHandler.getInstance().isFocused(widget);
  166. }
  167. } else {
  168. if (qx.Class.isSubClassOf(widget.constructor, qx.ui.form.AbstractField)) {
  169. return widget.getContainerElement().getChildren()[1].getStyle("visibility") != "hidden";
  170. } else if (this.__hasTextfieldChildControl(widget)) {
  171. return widget.getChildControl("textfield").getContainerElement().getChildren()[1].getStyle("visibility") != "hidden";
  172. }
  173. }
  174. },
  175. /////////// TextField ///////////
  176. testInitTextField : function() {
  177. this.__testInit(qx.ui.form.TextField);
  178. },
  179. testChangeValueTextField : function() {
  180. this.__testChangeValue(qx.ui.form.TextField);
  181. },
  182. testFocusTextField: function() {
  183. this.__testFocus(qx.ui.form.TextField);
  184. },
  185. testRemovePlaceholderTextField: function() {
  186. this.__testRemovePlaceholder(qx.ui.form.TextField);
  187. },
  188. testDisabledTextField: function() {
  189. this.__testDisabled(qx.ui.form.TextField);
  190. },
  191. /////////// TextArea ///////////
  192. testInitTextArea : function() {
  193. this.__testInit(qx.ui.form.TextArea);
  194. },
  195. testChangeValueTextArea : function() {
  196. this.__testChangeValue(qx.ui.form.TextArea);
  197. },
  198. testFocusTextArea: function() {
  199. this.__testFocus(qx.ui.form.TextArea);
  200. },
  201. testRemovePlaceholderTextArea: function() {
  202. this.__testRemovePlaceholder(qx.ui.form.TextArea);
  203. },
  204. testDisabledTextArea: function() {
  205. this.__testDisabled(qx.ui.form.TextArea);
  206. },
  207. /////////// PasswordField ///////////
  208. testInitPasswordField : function() {
  209. this.__testInit(qx.ui.form.PasswordField);
  210. },
  211. testChangeValuePasswordField : function() {
  212. this.__testChangeValue(qx.ui.form.PasswordField);
  213. },
  214. testFocusPasswordField: function() {
  215. this.__testFocus(qx.ui.form.PasswordField);
  216. },
  217. testRemovePlaceholderPasswordField: function() {
  218. this.__testRemovePlaceholder(qx.ui.form.PasswordField);
  219. },
  220. testDisabledPasswordField: function() {
  221. this.__testDisabled(qx.ui.form.PasswordField);
  222. },
  223. /////////// ComboBox ///////////
  224. testInitComboBox : function() {
  225. this.__testInit(qx.ui.form.ComboBox);
  226. },
  227. testChangeValueComboBox : function() {
  228. this.__testChangeValue(qx.ui.form.ComboBox);
  229. },
  230. testFocusComboBox: function() {
  231. this.__testFocus(qx.ui.form.ComboBox);
  232. },
  233. testRemovePlaceholderComboBox: function() {
  234. this.__testRemovePlaceholder(qx.ui.form.ComboBox);
  235. },
  236. testDisabledComboBox: function() {
  237. this.__testDisabled(qx.ui.form.ComboBox);
  238. },
  239. /////////// DateField ///////////
  240. testFocusDateField: function() {
  241. this.__testFocus(qx.ui.form.DateField);
  242. },
  243. testRemovePlaceholderDateField: function() {
  244. this.__testRemovePlaceholder(qx.ui.form.DateField);
  245. },
  246. testDisabledDateField: function() {
  247. this.__testDisabled(qx.ui.form.DateField);
  248. }
  249. }
  250. });