/devtools/qooxdoo-1.5-sdk/framework/source/class/qx/test/data/controller/Object.js

https://github.com/Seldaiendil/meyeOS · JavaScript · 429 lines · 214 code · 112 blank · 103 comment · 6 complexity · 094f329935e9aa305f47f1b2ecdacbd7 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.data.controller.Object",
  14. {
  15. extend : qx.dev.unit.TestCase,
  16. members :
  17. {
  18. __label1: null,
  19. __label2: null,
  20. __model: null,
  21. __controller: null,
  22. setUp : function()
  23. {
  24. this.__label1 = new qx.ui.basic.Label();
  25. this.__label2 = new qx.ui.basic.Label();
  26. this.__model = new qx.ui.core.Widget();
  27. this.__controller = new qx.data.controller.Object(this.__model);
  28. },
  29. tearDown : function()
  30. {
  31. this.__model.dispose();
  32. this.__label2.dispose();
  33. this.__label1.dispose();
  34. },
  35. testOneToOne: function() {
  36. // Tie the label1s content to the zindex of the model
  37. this.__controller.addTarget(this.__label1, "value", "zIndex");
  38. // set a new zIndex to the model
  39. this.__model.setZIndex(10);
  40. // test for the binding
  41. this.assertEquals("10", this.__label1.getValue(), "Binding does not work!");
  42. },
  43. testOneToTwo: function() {
  44. // Tie the label1s content to the zindex of the model
  45. this.__controller.addTarget(this.__label1, "value", "zIndex");
  46. // Tie the label2s content to the zindex of the model
  47. this.__controller.addTarget(this.__label2, "value", "zIndex");
  48. // set a new zIndex to the model
  49. this.__model.setZIndex(10);
  50. // test for the binding
  51. this.assertEquals("10", this.__label1.getValue(), "Binding1 does not work!");
  52. this.assertEquals("10", this.__label2.getValue(), "Binding2 does not work!");
  53. },
  54. testChangeModel: function() {
  55. // Tie the labels content to the zindex of the model
  56. this.__controller.addTarget(this.__label1, "value", "zIndex");
  57. this.__controller.addTarget(this.__label2, "value", "zIndex");
  58. // set an old zIndex
  59. this.__model.setZIndex(10);
  60. // create a new model with a different zIndex
  61. var newModel = new qx.ui.core.Widget();
  62. newModel.setZIndex(20);
  63. // dispose the old model to check that the controller can handle that
  64. this.__model.dispose();
  65. // set the new Model
  66. this.__controller.setModel(newModel);
  67. // test for the binding
  68. this.assertEquals("20", this.__label1.getValue(), "Binding1 does not work!");
  69. this.assertEquals("20", this.__label2.getValue(), "Binding2 does not work!");
  70. },
  71. testRemoveOneBinding: function() {
  72. // set a zIndex
  73. this.__model.setZIndex(20);
  74. // Tie the labels content to the zindex of the model
  75. this.__controller.addTarget(this.__label1, "value", "zIndex");
  76. this.__controller.addTarget(this.__label2, "value", "zIndex");
  77. // test for the binding
  78. this.assertEquals("20", this.__label1.getValue(), "Binding1 does not work!");
  79. this.assertEquals("20", this.__label2.getValue(), "Binding2 does not work!");
  80. // remove one target
  81. this.__controller.removeTarget(this.__label1, "value", "zIndex");
  82. // set a new zIndex
  83. this.__model.setZIndex(5);
  84. // test for the binding
  85. this.assertEquals("20", this.__label1.getValue(), "Binding1 has not been removed!");
  86. this.assertEquals("5", this.__label2.getValue(), "Binding2 has been removed!");
  87. },
  88. testRemoveUnexistantTarget: function() {
  89. // test some cases
  90. this.__controller.removeTarget(this.__label1, "value", "zIndex");
  91. this.__controller.removeTarget(null, "AFFE", "AFFEN");
  92. // set a target for testing
  93. this.__controller.addTarget(this.__label1, "value", "zIndex");
  94. // test the same cases again
  95. this.__controller.removeTarget(this.__label1, "value", "zIndex");
  96. this.__controller.removeTarget(null, "AFFE", "AFFEN");
  97. },
  98. testTowToTwo: function() {
  99. // set up two links
  100. this.__controller.addTarget(this.__label1, "value", "zIndex");
  101. this.__controller.addTarget(this.__label2, "value", "visibility");
  102. // set the values
  103. this.__model.setZIndex(11);
  104. this.__model.setVisibility("visible");
  105. // test for the binding
  106. this.assertEquals("11", this.__label1.getValue(), "Binding1 does not work!");
  107. this.assertEquals("visible", this.__label2.getValue(), "Binding2 does not work!");
  108. // set new values
  109. this.__model.setZIndex(15);
  110. this.__model.setVisibility("hidden");
  111. // test again for the binding
  112. this.assertEquals("15", this.__label1.getValue(), "Binding1 does not work!");
  113. this.assertEquals("hidden", this.__label2.getValue(), "Binding2 does not work!");
  114. },
  115. testOneToOneBi: function() {
  116. // Tie the label1s content to the zindex of the model
  117. this.__controller.addTarget(this.__label1, "value", "zIndex", true);
  118. // set a new zIndex to the model
  119. this.__model.setZIndex(10);
  120. // test for the binding
  121. this.assertEquals("10", this.__label1.getValue(), "Binding does not work!");
  122. // set a new content
  123. this.__label1.setValue("20");
  124. // test the reverse binding
  125. this.assertEquals(20, this.__model.getZIndex(), "Reverse-Binding does not work!");
  126. },
  127. testOneToTwoBi: function() {
  128. // Tie the label1s content to the zindex of the model
  129. this.__controller.addTarget(this.__label1, "value", "zIndex", true);
  130. // Tie the label2s content to the zindex of the model
  131. this.__controller.addTarget(this.__label2, "value", "zIndex", true);
  132. // set a new zIndex to the model
  133. this.__model.setZIndex(10);
  134. // test for the binding
  135. this.assertEquals("10", this.__label1.getValue(), "Binding1 does not work!");
  136. this.assertEquals("10", this.__label2.getValue(), "Binding2 does not work!");
  137. // change one label
  138. this.__label1.setValue("100");
  139. // test for the binding
  140. this.assertEquals(100, this.__model.getZIndex(), "Reverse Binding does not work!");
  141. this.assertEquals("100", this.__label2.getValue(), "Binding2 does not work!");
  142. // change the other label
  143. this.__label2.setValue("200");
  144. // test for the binding
  145. this.assertEquals(200, this.__model.getZIndex(), "Reverse Binding does not work!");
  146. this.assertEquals("200", this.__label1.getValue(), "Binding1 does not work!");
  147. },
  148. testChangeModelBi: function() {
  149. // Tie the labels content to the zindex of the model
  150. this.__controller.addTarget(this.__label1, "value", "zIndex", true);
  151. this.__controller.addTarget(this.__label2, "value", "zIndex", true);
  152. // set an old zIndex
  153. this.__model.setZIndex(10);
  154. // create a new model with a different zIndex
  155. var newModel = new qx.ui.core.Widget();
  156. newModel.setZIndex(20);
  157. // set the new Model
  158. this.__controller.setModel(newModel);
  159. // test for the binding
  160. this.assertEquals("20", this.__label1.getValue(), "Binding1 does not work!");
  161. this.assertEquals("20", this.__label2.getValue(), "Binding2 does not work!");
  162. // set the zIndex in a label
  163. this.__label2.setValue("11");
  164. // test for the bindings (working and should not work)
  165. this.assertEquals("11", this.__label1.getValue(), "Binding1 does not work!");
  166. this.assertEquals(11, newModel.getZIndex(), "Reverse-Binding does not work!");
  167. this.assertEquals(10, this.__model.getZIndex(), "Binding has not been removed.");
  168. },
  169. testConverting: function() {
  170. // create the options map
  171. var opt = {
  172. converter: function(value) {
  173. if (value > 10) {
  174. return "A";
  175. }
  176. return "B";
  177. }
  178. };
  179. // Tie the labels content to the zindex of the model
  180. this.__controller.addTarget(this.__label1, "value", "zIndex", false, opt);
  181. // set a zIndex and test it
  182. this.__model.setZIndex(11);
  183. this.assertEquals("A", this.__label1.getValue(), "Converter does not work!");
  184. // set a zIndex and test it
  185. this.__model.setZIndex(5);
  186. this.assertEquals("B", this.__label1.getValue(), "Converter does not work!");
  187. },
  188. testConvertingBi: function() {
  189. // create the options map for source to target
  190. var opt = {
  191. converter: function(value) {
  192. if (value > 10) {
  193. return "A";
  194. }
  195. return "B";
  196. }
  197. };
  198. // create the options map for target to source
  199. var revOpt = {
  200. converter: function(value) {
  201. if (value == "A") {
  202. return 11;
  203. }
  204. return 10;
  205. }
  206. };
  207. // Tie the labels content to the zindex of the model
  208. this.__controller.addTarget(this.__label1, "value", "zIndex", true, opt, revOpt);
  209. // set a zIndex and test it
  210. this.__model.setZIndex(11);
  211. this.assertEquals("A", this.__label1.getValue(), "Converter does not work!");
  212. // set a zIndex and test it
  213. this.__model.setZIndex(5);
  214. this.assertEquals("B", this.__label1.getValue(), "Converter does not work!");
  215. // change the target and check the model
  216. this.__label1.setValue("A");
  217. this.assertEquals(11, this.__model.getZIndex(), "Back-Converter does not work!");
  218. this.__label1.setValue("B");
  219. this.assertEquals(10, this.__model.getZIndex(), "Back-Converter does not work!");
  220. },
  221. testChangeModelCon: function() {
  222. // create the options map
  223. var opt = {
  224. converter: function(value) {
  225. if (value > 10) {
  226. return "A";
  227. }
  228. return "B";
  229. }
  230. };
  231. // Tie the labels content to the zindex of the model
  232. this.__controller.addTarget(this.__label1, "value", "zIndex", false, opt);
  233. this.__controller.addTarget(this.__label2, "value", "zIndex", false, opt);
  234. // set an old zIndex
  235. this.__model.setZIndex(3);
  236. // create a new model with a different zIndex
  237. var newModel = new qx.ui.core.Widget();
  238. newModel.setZIndex(20);
  239. // set the new Model
  240. this.__controller.setModel(newModel);
  241. // test for the binding
  242. this.assertEquals("A", this.__label1.getValue(), "Binding1 does not work!");
  243. this.assertEquals("A", this.__label2.getValue(), "Binding2 does not work!");
  244. },
  245. testSetLateModel: function() {
  246. // create a blank controller
  247. this.__controller = new qx.data.controller.Object();
  248. // set the model
  249. this.__controller.setModel(this.__model);
  250. // Tie the label1s content to the zindex of the model
  251. this.__controller.addTarget(this.__label1, "value", "zIndex");
  252. // set a new zIndex to the model
  253. this.__model.setZIndex(10);
  254. // test for the binding
  255. this.assertEquals("10", this.__label1.getValue(), "Binding does not work!");
  256. },
  257. testSetModelNull: function() {
  258. // Tie the label1s content to the zindex of the model
  259. this.__controller.addTarget(this.__label1, "value", "zIndex");
  260. // set the model of the controller to null and back
  261. this.__controller.setModel(null);
  262. this.__controller.setModel(this.__model);
  263. // set a new zIndex to the model
  264. this.__model.setZIndex(10);
  265. // test for the binding
  266. this.assertEquals("10", this.__label1.getValue(), "Binding does not work!");
  267. },
  268. testCreateWithoutModel: function() {
  269. // create a new controller
  270. this.__controller.dispose();
  271. this.__controller = new qx.data.controller.Object();
  272. // Tie the label1s content to the zindex of the model
  273. this.__controller.addTarget(this.__label1, "value", "zIndex");
  274. // set a new zIndex to the model
  275. this.__model.setZIndex(10);
  276. this.__controller.setModel(this.__model);
  277. // test for the binding
  278. this.assertEquals("10", this.__label1.getValue(), "Binding does not work!");
  279. },
  280. testTargetArrayBi : function() {
  281. var selectbox = new qx.ui.form.SelectBox();
  282. for (var i = 0; i < 10; i++) {
  283. selectbox.add(new qx.ui.form.ListItem("item " + i).set({model: i}));
  284. }
  285. this.__controller.addTarget(selectbox, "modelSelection[0]", "zIndex", true);
  286. // selectbox --> model
  287. selectbox.setSelection([selectbox.getSelectables()[6]]);
  288. this.assertEquals(6, this.__model.getZIndex());
  289. // model --> selectbox
  290. this.__model.setZIndex(3);
  291. this.assertEquals(3, selectbox.getSelection()[0].getModel());
  292. selectbox.dispose();
  293. },
  294. testDispose : function() {
  295. // Tie the label1s content to the zindex of the model
  296. this.__controller.addTarget(this.__label1, "value", "zIndex", true);
  297. // create a common startbase
  298. this.__label1.setZIndex(7);
  299. // dispose the controller to remove the bindings
  300. this.__controller.dispose();
  301. // set a new zIndex to the model
  302. this.__model.setZIndex(10);
  303. // test if the binding has been removed and reseted
  304. this.assertEquals(null, this.__label1.getValue(), "Binding does not work!");
  305. // set a new content
  306. this.__label1.setValue("20");
  307. // test the reverse binding
  308. this.assertEquals(10, this.__model.getZIndex(), "Reverse-Binding does not work!");
  309. }
  310. }
  311. });