connection_test.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**
  2. * @license
  3. * Blockly Tests
  4. *
  5. * Copyright 2016 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. /**
  21. * @fileoverview Tests for connection logic.
  22. * @author fenichel@google.com (Rachel Fenichel)
  23. */
  24. 'use strict';
  25. var input;
  26. var output;
  27. var previous;
  28. var next;
  29. var dummyWorkspace;
  30. function connectionTest_setUp() {
  31. dummyWorkspace = {};
  32. function createDummyBlock() {
  33. return {
  34. workspace: dummyWorkspace,
  35. isShadow: function() {return false;}
  36. };
  37. }
  38. input = new Blockly.Connection(createDummyBlock(),
  39. Blockly.INPUT_VALUE);
  40. output = new Blockly.Connection(createDummyBlock(),
  41. Blockly.OUTPUT_VALUE);
  42. previous = new Blockly.Connection(createDummyBlock(),
  43. Blockly.PREVIOUS_STATEMENT);
  44. next = new Blockly.Connection(createDummyBlock(),
  45. Blockly.NEXT_STATEMENT);
  46. }
  47. function connectionTest_tearDown() {
  48. input = null;
  49. output = null;
  50. previous = null;
  51. next = null;
  52. dummyWorkspace = null;
  53. }
  54. var isMovableFn = function() { return true; };
  55. /**
  56. * These tests check that the reasons for failures to connect are consistent
  57. * (internal view of error states).
  58. */
  59. function testCanConnectWithReason_TargetNull() {
  60. connectionTest_setUp();
  61. assertEquals(Blockly.Connection.REASON_TARGET_NULL,
  62. input.canConnectWithReason_(null));
  63. connectionTest_tearDown();
  64. }
  65. function testCanConnectWithReason_Disconnect() {
  66. connectionTest_setUp();
  67. var tempConnection = new Blockly.Connection({workspace: dummyWorkspace, isMovable: isMovableFn},
  68. Blockly.OUTPUT_VALUE);
  69. Blockly.Connection.connectReciprocally_(input, tempConnection);
  70. assertEquals(Blockly.Connection.CAN_CONNECT,
  71. input.canConnectWithReason_(output));
  72. connectionTest_tearDown();
  73. }
  74. function testCanConnectWithReason_DifferentWorkspaces() {
  75. connectionTest_setUp();
  76. input = new Blockly.Connection({workspace: {}}, Blockly.INPUT_VALUE);
  77. output = new Blockly.Connection({workspace: dummyWorkspace},
  78. Blockly.OUTPUT_VALUE);
  79. assertEquals(Blockly.Connection.REASON_DIFFERENT_WORKSPACES,
  80. input.canConnectWithReason_(output));
  81. connectionTest_tearDown();
  82. }
  83. function testCanConnectWithReason_Self() {
  84. connectionTest_setUp();
  85. var block = {type_: "test block"};
  86. input.sourceBlock_ = block;
  87. assertEquals(Blockly.Connection.REASON_SELF_CONNECTION,
  88. input.canConnectWithReason_(input));
  89. connectionTest_tearDown();
  90. }
  91. function testCanConnectWithReason_Type() {
  92. connectionTest_setUp();
  93. assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
  94. input.canConnectWithReason_(previous));
  95. assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
  96. input.canConnectWithReason_(next));
  97. assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
  98. output.canConnectWithReason_(previous));
  99. assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
  100. output.canConnectWithReason_(next));
  101. assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
  102. previous.canConnectWithReason_(input));
  103. assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
  104. previous.canConnectWithReason_(output));
  105. assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
  106. next.canConnectWithReason_(input));
  107. assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
  108. next.canConnectWithReason_(output));
  109. connectionTest_tearDown();
  110. }
  111. function testCanConnectWithReason_CanConnect() {
  112. connectionTest_setUp();
  113. assertEquals(Blockly.Connection.CAN_CONNECT,
  114. previous.canConnectWithReason_(next));
  115. assertEquals(Blockly.Connection.CAN_CONNECT,
  116. next.canConnectWithReason_(previous));
  117. assertEquals(Blockly.Connection.CAN_CONNECT,
  118. input.canConnectWithReason_(output));
  119. assertEquals(Blockly.Connection.CAN_CONNECT,
  120. output.canConnectWithReason_(input));
  121. connectionTest_tearDown();
  122. }
  123. /**
  124. * The next set of tests checks that exceptions are being thrown at the correct
  125. * times (external view of errors).
  126. */
  127. function testCheckConnection_Self() {
  128. connectionTest_setUp();
  129. var block = {type_: "test block"};
  130. input.sourceBlock_ = block;
  131. try {
  132. input.checkConnection_(input);
  133. fail();
  134. } catch (e) {
  135. // expected
  136. }
  137. connectionTest_tearDown();
  138. }
  139. function testCheckConnection_TypeInputPrev() {
  140. connectionTest_setUp();
  141. try {
  142. input.checkConnection_(previous);
  143. fail();
  144. } catch (e) {
  145. // expected
  146. }
  147. connectionTest_tearDown();
  148. }
  149. function testCheckConnection_TypeInputNext() {
  150. connectionTest_setUp();
  151. try {
  152. input.checkConnection_(next);
  153. fail();
  154. } catch (e) {
  155. // expected
  156. }
  157. connectionTest_tearDown();
  158. }
  159. function testCheckConnection_TypeOutputPrev() {
  160. connectionTest_setUp();
  161. try {
  162. output.checkConnection_(previous);
  163. fail();
  164. } catch (e) {
  165. // expected
  166. }
  167. connectionTest_tearDown();
  168. }
  169. function testCheckConnection_TypePrevInput() {
  170. connectionTest_setUp();
  171. try {
  172. previous.checkConnection_(input);
  173. fail();
  174. } catch (e) {
  175. // expected
  176. }
  177. connectionTest_tearDown();
  178. }
  179. function testCheckConnection_TypePrevOutput() {
  180. connectionTest_setUp();
  181. try {
  182. previous.checkConnection_(output);
  183. fail();
  184. } catch (e) {
  185. // expected
  186. }
  187. connectionTest_tearDown();
  188. }
  189. function testCheckConnection_TypeNextInput() {
  190. connectionTest_setUp();
  191. try {
  192. next.checkConnection_(input);
  193. fail();
  194. } catch (e) {
  195. // expected
  196. }
  197. connectionTest_tearDown();
  198. }
  199. function testCheckConnection_TypeNextOutput() {
  200. connectionTest_setUp();
  201. try {
  202. next.checkConnection_(output);
  203. fail();
  204. } catch (e) {
  205. // expected
  206. }
  207. connectionTest_tearDown();
  208. }
  209. function test_isConnectionAllowed_Distance() {
  210. var sharedWorkspace = {};
  211. // Two connections of opposite types near each other.
  212. var one = helper_createConnection(5 /* x */, 10 /* y */,
  213. Blockly.INPUT_VALUE, null, true);
  214. one.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
  215. var two = helper_createConnection(10 /* x */, 15 /* y */,
  216. Blockly.OUTPUT_VALUE, null, true);
  217. two.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
  218. assertTrue(one.isConnectionAllowed(two, 20.0));
  219. // Move connections farther apart.
  220. two.x_ = 100;
  221. two.y_ = 100;
  222. assertFalse(one.isConnectionAllowed(two, 20.0));
  223. }
  224. function test_isConnectionAllowed_Unrendered() {
  225. var sharedWorkspace = {};
  226. var one = helper_createConnection(5 /* x */, 10 /* y */,
  227. Blockly.INPUT_VALUE);
  228. one.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
  229. // Don't offer to connect an already connected left (male) value plug to
  230. // an available right (female) value plug.
  231. var two = helper_createConnection(0, 0, Blockly.OUTPUT_VALUE);
  232. two.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
  233. assertTrue(one.isConnectionAllowed(two));
  234. var three = helper_createConnection(0, 0, Blockly.INPUT_VALUE);
  235. three.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
  236. Blockly.Connection.connectReciprocally_(two, three);
  237. assertFalse(one.isConnectionAllowed(two));
  238. // Don't connect two connections on the same block.
  239. two.sourceBlock_ = one.sourceBlock_;
  240. assertFalse(one.isConnectionAllowed(two));
  241. }
  242. function test_isConnectionAllowed_NoNext() {
  243. var sharedWorkspace = {};
  244. var one = helper_createConnection(0, 0, Blockly.NEXT_STATEMENT);
  245. one.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
  246. one.sourceBlock_.nextConnection = one;
  247. var two = helper_createConnection(0, 0, Blockly.PREVIOUS_STATEMENT);
  248. two.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
  249. assertTrue(two.isConnectionAllowed(one));
  250. var three = helper_createConnection(0, 0, Blockly.PREVIOUS_STATEMENT);
  251. three.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
  252. three.sourceBlock_.previousConnection = three;
  253. Blockly.Connection.connectReciprocally_(one, three);
  254. // A terminal block is allowed to replace another terminal block.
  255. assertTrue(two.isConnectionAllowed(one));
  256. }
  257. function testCheckConnection_Okay() {
  258. connectionTest_setUp();
  259. previous.checkConnection_(next);
  260. next.checkConnection_(previous);
  261. input.checkConnection_(output);
  262. output.checkConnection_(input);
  263. connectionTest_tearDown();
  264. }