You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

472 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*!
  2. * Ext JS Library 3.3.1
  3. * Copyright(c) 2006-2010 Sencha Inc.
  4. * licensing@sencha.com
  5. * http://www.sencha.com/license
  6. */
  7. Ext.test.session.addTest('Ext.Container', {
  8. name: 'Test Ext.Container methods',
  9. test_add: function(){
  10. var ct = new Ext.Container({
  11. defaultType: 'box'
  12. });
  13. ct.add(new Ext.Component());
  14. Y.Assert.areEqual(1, ct.items.getCount(), 'Check adding a single item works');
  15. ct.add(new Ext.Component({itemId: 'a'}), new Ext.Component({itemId: 'b'}), new Ext.Component({itemId: 'c'}));
  16. Y.Assert.areEqual(4, ct.items.getCount(), 'Test add with param array');
  17. Y.Assert.areEqual('b', ct.items.itemAt(2).itemId, 'Test that they are correctly added in order');
  18. ct.add({
  19. xtype: 'component'
  20. });
  21. Y.Assert.areEqual(5, ct.items.getCount(), 'Test for lazy instantiation');
  22. ct.add({});
  23. Y.Assert.isTrue(ct.items.last().isXType('box'), 'Test default type');
  24. ct.destroy();
  25. ct = null;
  26. },
  27. test_cascade: function(){
  28. var ct = new Ext.Container({itemId: 'ct', depth: 0}),
  29. i,
  30. j,
  31. k,
  32. ci,
  33. cj,
  34. order = [ct.itemId];
  35. /* our container will have:
  36. * 3 items at depth 1
  37. * 9 items at depth 2
  38. * 27 items at depth 3
  39. */
  40. for(i = 0; i < 3; ++i){
  41. id = 'item' + i;
  42. ci = ct.add({
  43. itemId: id,
  44. depth: 1
  45. });
  46. order.push(id);
  47. for(j = 0; j < 3; ++j){
  48. id = 'item' + i + '_' + j;
  49. cj = ci.add({
  50. itemId: id,
  51. depth: 2
  52. });
  53. order.push(id);
  54. for(k = 0; k < 3; ++k){
  55. id = 'item' + i + '_' + j + '_' + k;
  56. cj.add({
  57. itemId: id,
  58. depth: 3
  59. });
  60. order.push(id);
  61. }
  62. }
  63. }
  64. var cnt = 0,
  65. items = [];
  66. ct.cascade(function(c){
  67. items.push(c.itemId);
  68. ++cnt;
  69. });
  70. Y.Assert.areEqual(40, cnt, 'Test each item is iterated');
  71. Y.ArrayAssert.itemsAreEqual(order, items, 'Test that items are iterated depth first');
  72. items = [];
  73. ct.cascade(function(){
  74. items.push(this.itemId);
  75. });
  76. Y.ArrayAssert.itemsAreEqual(order, items, 'Test that the scope defaults to the current component');
  77. cnt = 0;
  78. ct.cascade(function(c){
  79. cnt += c.depth * this.val;
  80. }, {val: 3});
  81. Y.Assert.areEqual(306, cnt, 'Test that custom scope works');
  82. cnt = 0;
  83. ct.cascade(function(a, b){
  84. cnt += this.depth * (a + b);
  85. }, null, [1, 2]);
  86. Y.Assert.areEqual(306, cnt, 'Test that custom args work');
  87. cnt = 0;
  88. ct.cascade(function(){
  89. if(this.itemId == 'item0' || this.itemId == 'item2'){
  90. return false;
  91. }
  92. ++cnt;
  93. });
  94. Y.Assert.areEqual(14, cnt, 'Test that returning false stops iteration on a branch');
  95. ct.destroy();
  96. ct = null;
  97. },
  98. test_find: function(){
  99. var ct = new Ext.Container({
  100. prop: 'foo',
  101. special: 'main',
  102. items: [{
  103. xtype: 'container',
  104. prop: 'foo',
  105. id: 'a',
  106. items: [{}, {prop: 'foo', id: 'd'}, {}, {}, {}, {
  107. xtype: 'container',
  108. items: [{}, {}, {}, {}]
  109. }]
  110. },{
  111. xtype: 'container',
  112. prop: 'bar',
  113. id: 'b',
  114. items: [{prop: 'foo', id: 'e'}, {prop: 'food'}, {
  115. xtype: 'container',
  116. items: {
  117. xtype: 'container',
  118. items: {
  119. xtype: 'container',
  120. items: {
  121. xtype: 'container',
  122. items: {
  123. prop: 'foo',
  124. id: 'f'
  125. }
  126. }
  127. }
  128. }
  129. }]
  130. },{
  131. prop: 'foo',
  132. xtype: 'box',
  133. id: 'c'
  134. }]
  135. });
  136. var arr = ct.find('prop', 'foo');
  137. Y.Assert.areEqual(5, arr.length, 'Ensure correct items are found, at all depths');
  138. Y.ArrayAssert.itemsAreEqual(['a', 'd', 'e', 'f', 'c'], Ext.pluck(arr, 'id'), 'Test order the items are found, should be depth first');
  139. arr = ct.find('none', 'x');
  140. Y.ArrayAssert.isEmpty(arr, 'Test with non existent property');
  141. arr = ct.find('foo', 'not here');
  142. Y.ArrayAssert.isEmpty(arr, 'Test with property that exists, but no matches');
  143. arr = ct.items.first().find('prop', 'foo');
  144. Y.Assert.areEqual(1, arr.length, 'Ensure correct items are found, at one child level');
  145. Y.ArrayAssert.itemsAreEqual(['d'], Ext.pluck(arr, 'id'), 'Test order the items are found');
  146. arr = ct.find('special', 'main');
  147. Y.ArrayAssert.isEmpty(arr, 'Ensure the container itself isn\'t found');
  148. ct.destroy();
  149. ct = null;
  150. },
  151. test_findBy: function(){
  152. var ct = new Ext.Container({
  153. foo: 0,
  154. items: [{
  155. foo: 1,
  156. items: [{
  157. foo: 5
  158. },{
  159. foo: 6
  160. },{
  161. foo: 7
  162. }]
  163. },{
  164. foo: 2,
  165. items: [{
  166. foo: 8
  167. },{
  168. foo: 9
  169. },{
  170. foo: 10
  171. }]
  172. },{
  173. foo: 3,
  174. items: {
  175. foo: 11
  176. }
  177. },{
  178. foo: 4,
  179. items: [{
  180. foo: 12
  181. },{
  182. foo: 13
  183. },{
  184. foo: 14,
  185. items: [{
  186. foo: 15
  187. },{
  188. foo: 16
  189. }]
  190. }]
  191. }]
  192. });
  193. var arr = ct.findBy(function(){
  194. return this.foo % 2 == 0;
  195. });
  196. Y.Assert.areEqual(8, arr.length, 'Test correct items are returned, also tests that scope defaults to the component');
  197. Y.ArrayAssert.itemsAreEqual([6, 2, 8, 10, 4, 12, 14, 16], Ext.pluck(arr, 'foo'), 'Test items are returned in the correct order');
  198. arr = ct.findBy(function(c){
  199. return c.foo == 0;
  200. });
  201. Y.ArrayAssert.isEmpty(arr, 'Ensure that the container is not included in the findBy');
  202. arr = ct.findBy(function(c){
  203. return c.foo % this.val == 0;
  204. }, {val: 3});
  205. Y.Assert.areEqual(5, arr.length, 'Test correct items are returned, also test custom scope');
  206. ct.destroy();
  207. ct = null;
  208. },
  209. test_findByType: function(){
  210. var ct = new Ext.Container({
  211. items: [{
  212. xtype: 'box'
  213. },{
  214. xtype: 'component'
  215. },{
  216. xtype: 'container',
  217. items: [{
  218. xtype: 'panel',
  219. items: {
  220. xtype: 'panel',
  221. items: {
  222. xtype: 'box'
  223. }
  224. }
  225. },{
  226. xtype: 'container',
  227. items: {
  228. xtype: 'container',
  229. items: {
  230. xtype: 'container',
  231. items: {
  232. xtype: 'box'
  233. }
  234. }
  235. }
  236. },{
  237. xtype: 'box'
  238. }]
  239. },{
  240. xtype: 'form',
  241. items: {
  242. xtype: 'fieldset',
  243. items: [{
  244. xtype: 'datefield'
  245. },{
  246. xtype: 'textfield'
  247. }]
  248. }
  249. }]
  250. });
  251. var arr = ct.findByType('component');
  252. Y.Assert.areEqual(15, arr.length, 'Check correct number of items are returned');
  253. arr = ct.findByType('box', true);
  254. Y.Assert.areEqual(4, arr.length, 'Test shallow parameter is respected');
  255. arr = ct.findByType('grid');
  256. Y.Assert.areEqual(0, arr.length, 'Ensure that no items are returned if there\'s no matches');
  257. arr = ct.findByType('container');
  258. Y.Assert.areEqual(8, arr.length, 'Check all items are found at different depths');
  259. ct.destroy();
  260. ct = null;
  261. },
  262. test_getComponent: function(){
  263. var component1 = new Ext.Component({itemId: 'd'}),
  264. component2 = new Ext.Component({itemId: 'e'}),
  265. component3 = new Ext.Component();
  266. var ct = new Ext.Container({
  267. items: [{
  268. id: 'a'
  269. },{
  270. itemId: 'b'
  271. },{
  272. itemId: 'c'
  273. },{
  274. //nothing
  275. }, component1, {
  276. //nothing
  277. }, component2]
  278. });
  279. Y.Assert.areEqual('b', ct.getComponent(1).itemId, 'Test accessing by index');
  280. Y.Assert.areEqual('a', ct.getComponent(0).id, 'Test accessing by index');
  281. Y.Assert.isUndefined(ct.getComponent(100), 'Test grabbing an index outside of bounds');
  282. Y.Assert.isUndefined(ct.getComponent(null), 'Test with passing null');
  283. Y.Assert.isUndefined(ct.getComponent('foo'), 'Test with passing id that doesn\'t exist');
  284. Y.Assert.isUndefined(ct.getComponent(component3), 'Test with passing a component that doesn\'t belong to the container');
  285. Y.Assert.areEqual(component1, ct.getComponent(component1), 'Test passing in a component instance');
  286. Y.Assert.areEqual(component2, ct.getComponent(component2), 'Test passing in a component instance');
  287. Y.Assert.areEqual('d', ct.getComponent('d').itemId, 'Test passing in itemId');
  288. Y.Assert.areEqual('c', ct.getComponent('c').itemId, 'Test the above with an itemId');
  289. component3.destroy();
  290. ct.destroy();
  291. ct = null;
  292. },
  293. test_insert: function(){
  294. var ct = new Ext.Container({
  295. items: [{
  296. itemId: 'first',
  297. xtype: 'component'
  298. },{
  299. xtype: 'component'
  300. },{
  301. xtype: 'component'
  302. }]
  303. });
  304. ct.insert(1, {itemId: 'a'});
  305. Y.Assert.areEqual('a', ct.items.itemAt(1).itemId, 'Test simple insert');
  306. ct.insert(0, {itemId: 'b'});
  307. Y.Assert.areEqual('b', ct.items.first().itemId, 'Test insert at 0');
  308. ct.insert(ct.items.getCount(), {itemId: 'c'});
  309. Y.Assert.areEqual('c', ct.items.last().itemId, 'Test inserting at the last index');
  310. ct.insert(100, {itemId: 'd'});
  311. Y.Assert.areEqual('d', ct.items.last().itemId, 'Test inserting well after the last index');
  312. ct.insert(2, {itemId: 'e'}, {itemId: 'f'}, {itemId: 'g'});
  313. Y.Assert.areEqual('e', ct.items.itemAt(2).itemId, 'Test inserting multiple items at once');
  314. Y.Assert.areEqual('f', ct.items.itemAt(3).itemId, 'Test inserting multiple items at once');
  315. Y.Assert.areEqual('g', ct.items.itemAt(4).itemId, 'Test inserting multiple items at once');
  316. ct.insert(3, new Ext.Component({itemId: 'h'}));
  317. Y.Assert.areEqual('h', ct.items.itemAt(3).itemId, 'Test simple insert');
  318. Y.Assert.areEqual('first', ct.items.itemAt(1).itemId, 'Test original component held position');
  319. ct.destroy();
  320. ct = null;
  321. },
  322. test_remove: function(){
  323. var items = [],
  324. ct,
  325. c,
  326. component1 = new Ext.Component(),
  327. component2 = new Ext.Component(),
  328. component3 = new Ext.Component();
  329. for(var i = 0; i < 10; ++i){
  330. items.push({itemId: 'item' + i});
  331. }
  332. items.push(component1, component2);
  333. ct = new Ext.Container({items: items});
  334. ct.remove(0);
  335. Y.Assert.areEqual(11, ct.items.getCount(), 'Test remove by index');
  336. c = ct.remove(100);
  337. Y.Assert.areEqual(11, ct.items.getCount(), 'Test remove by index, where the index doesn\'t exist')
  338. Y.Assert.isUndefined(c, 'Ensure undefined is returned if the item doesn\'t exist');
  339. c = ct.remove(component1);
  340. Y.Assert.areEqual(10, ct.items.getCount(), 'Test remove with a component instance');
  341. Y.Assert.areEqual(component1, c, 'Test component is returned correctly');
  342. c = ct.remove(component3);
  343. Y.Assert.areEqual(10, ct.items.getCount(), 'Test removing an instance that doesn\'t exist in the component');
  344. Y.Assert.isUndefined(c, 'Ensure that the empty is returned in this case');
  345. c = ct.remove('item5');
  346. Y.Assert.areEqual(9, ct.items.getCount(), 'Test removing with an itemId');
  347. Y.Assert.areEqual('item5', c.itemId, 'Ensure component is returned properly');
  348. //test autoDestroy behaviour
  349. c = ct.remove(0);
  350. Y.Assert.isTrue(c.isDestroyed, 'Test that container autoDestroy/true is respected');
  351. c = ct.remove(0, true);
  352. Y.Assert.isTrue(c.isDestroyed, 'Test that container autoDestroy/true and destroy/true works');
  353. c = ct.remove(0, false);
  354. Y.Assert.isUndefined(c.isDestroyed, 'Test that destroy/false overrides autoDestroy/true');
  355. ct.autoDestroy = false;
  356. c = ct.remove(0);
  357. Y.Assert.isUndefined(c.isDestroyed, 'Test that autoDestroy/false is respected');
  358. c = ct.remove(0, false);
  359. Y.Assert.isUndefined(c.isDestroyed, 'Test autoDestroy/false and destroy/false works');
  360. c = ct.remove(0, true);
  361. Y.Assert.isTrue(c.isDestroyed, 'Test destroy/true overrides autoDestroy/false');
  362. component3.destroy();
  363. ct.destroy();
  364. ct = null;
  365. },
  366. test_removeAll: function(){
  367. var ct = new Ext.Container(),
  368. arr,
  369. i;
  370. arr = ct.removeAll();
  371. Y.ArrayAssert.isEmpty(arr, 'Test removeAll with no items returns an empty array');
  372. for(i = 0; i < 3; ++i){
  373. ct.add({
  374. itemId: 'item' + i
  375. });
  376. }
  377. arr = ct.removeAll();
  378. Y.Assert.areEqual(0, ct.items.getCount(), 'Ensure all items are removed');
  379. Y.ArrayAssert.itemsAreEqual(['item0', 'item1', 'item2'], Ext.pluck(arr, 'itemId'), 'Ensure they are removed in a consistent order');
  380. for(i = 0; i < 2; ++i){
  381. ct.add({
  382. itemId: 'item' + i
  383. });
  384. }
  385. arr = ct.removeAll(true);
  386. Y.ArrayAssert.itemsAreEqual([true, true], Ext.pluck(arr, 'isDestroyed'), 'Check destroy is honoured');
  387. ct.autoDestroy = true;
  388. for(i = 0; i < 3; ++i){
  389. ct.add({
  390. itemId: 'item' + i
  391. });
  392. }
  393. arr = ct.removeAll();
  394. Y.ArrayAssert.itemsAreEqual([true, true, true], Ext.pluck(arr, 'isDestroyed'), 'Check autoDestroy is honoured');
  395. ct.destroy();
  396. ct = null;
  397. }
  398. });