workspace_test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @license
  3. * Blockly Tests
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. 'use strict';
  21. function test_emptyWorkspace() {
  22. var workspace = new Blockly.Workspace();
  23. assertEquals('Empty workspace (1).', 0, workspace.getTopBlocks(true).length);
  24. assertEquals('Empty workspace (2).', 0, workspace.getTopBlocks(false).length);
  25. assertEquals('Empty workspace (3).', 0, workspace.getAllBlocks().length);
  26. workspace.clear();
  27. assertEquals('Empty workspace (4).', 0, workspace.getTopBlocks(true).length);
  28. assertEquals('Empty workspace (5).', 0, workspace.getTopBlocks(false).length);
  29. assertEquals('Empty workspace (6).', 0, workspace.getAllBlocks().length);
  30. }
  31. function test_flatWorkspace() {
  32. var workspace = new Blockly.Workspace();
  33. var blockA = workspace.newBlock('');
  34. assertEquals('One block workspace (1).', 1, workspace.getTopBlocks(true).length);
  35. assertEquals('One block workspace (2).', 1, workspace.getTopBlocks(false).length);
  36. assertEquals('One block workspace (3).', 1, workspace.getAllBlocks().length);
  37. var blockB = workspace.newBlock('');
  38. assertEquals('Two block workspace (1).', 2, workspace.getTopBlocks(true).length);
  39. assertEquals('Two block workspace (2).', 2, workspace.getTopBlocks(false).length);
  40. assertEquals('Two block workspace (3).', 2, workspace.getAllBlocks().length);
  41. blockA.dispose();
  42. assertEquals('One block workspace (4).', 1, workspace.getTopBlocks(true).length);
  43. assertEquals('One block workspace (5).', 1, workspace.getTopBlocks(false).length);
  44. assertEquals('One block workspace (6).', 1, workspace.getAllBlocks().length);
  45. workspace.clear();
  46. assertEquals('Cleared workspace (1).', 0, workspace.getTopBlocks(true).length);
  47. assertEquals('Cleared workspace (2).', 0, workspace.getTopBlocks(false).length);
  48. assertEquals('Cleared workspace (3).', 0, workspace.getAllBlocks().length);
  49. }
  50. function test_maxBlocksWorkspace() {
  51. var workspace = new Blockly.Workspace();
  52. var blockA = workspace.newBlock('');
  53. var blockB = workspace.newBlock('');
  54. assertEquals('Infinite capacity.', Infinity, workspace.remainingCapacity());
  55. workspace.options.maxBlocks = 3;
  56. assertEquals('Three capacity.', 1, workspace.remainingCapacity());
  57. workspace.options.maxBlocks = 2;
  58. assertEquals('Two capacity.', 0, workspace.remainingCapacity());
  59. workspace.options.maxBlocks = 1;
  60. assertEquals('One capacity.', -1, workspace.remainingCapacity());
  61. workspace.options.maxBlocks = 0;
  62. assertEquals('Zero capacity.', -2, workspace.remainingCapacity());
  63. workspace.clear();
  64. assertEquals('Cleared capacity.', 0, workspace.remainingCapacity());
  65. }
  66. function test_getWorkspaceById() {
  67. var workspaceA = new Blockly.Workspace();
  68. var workspaceB = new Blockly.Workspace();
  69. assertEquals('Find workspaceA.', workspaceA,
  70. Blockly.Workspace.getById(workspaceA.id));
  71. assertEquals('Find workspaceB.', workspaceB,
  72. Blockly.Workspace.getById(workspaceB.id));
  73. assertEquals('No workspace found.', null,
  74. Blockly.Workspace.getById('I do not exist.'));
  75. workspaceA.dispose();
  76. assertEquals('Can\'t find workspaceA.', null,
  77. Blockly.Workspace.getById(workspaceA.id));
  78. assertEquals('WorkspaceB exists.', workspaceB,
  79. Blockly.Workspace.getById(workspaceB.id));
  80. }
  81. function test_getBlockById() {
  82. var workspace = new Blockly.Workspace();
  83. var blockA = workspace.newBlock('');
  84. var blockB = workspace.newBlock('');
  85. assertEquals('Find blockA.', blockA, workspace.getBlockById(blockA.id));
  86. assertEquals('Find blockB.', blockB, workspace.getBlockById(blockB.id));
  87. assertEquals('No block found.', null,
  88. workspace.getBlockById('I do not exist.'));
  89. blockA.dispose();
  90. assertEquals('Can\'t find blockA.', null, workspace.getBlockById(blockA.id));
  91. assertEquals('BlockB exists.', blockB, workspace.getBlockById(blockB.id));
  92. workspace.clear();
  93. assertEquals('Can\'t find blockB.', null, workspace.getBlockById(blockB.id));
  94. }