123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- /**
- * @license
- * Blockly Tests
- *
- * Copyright 2016 Google Inc.
- * https://developers.google.com/blockly/
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * @fileoverview Tests for connection logic.
- * @author fenichel@google.com (Rachel Fenichel)
- */
- 'use strict';
- var input;
- var output;
- var previous;
- var next;
- var dummyWorkspace;
- function connectionTest_setUp() {
- dummyWorkspace = {};
- function createDummyBlock() {
- return {
- workspace: dummyWorkspace,
- isShadow: function() {return false;}
- };
- }
- input = new Blockly.Connection(createDummyBlock(),
- Blockly.INPUT_VALUE);
- output = new Blockly.Connection(createDummyBlock(),
- Blockly.OUTPUT_VALUE);
- previous = new Blockly.Connection(createDummyBlock(),
- Blockly.PREVIOUS_STATEMENT);
- next = new Blockly.Connection(createDummyBlock(),
- Blockly.NEXT_STATEMENT);
- }
- function connectionTest_tearDown() {
- input = null;
- output = null;
- previous = null;
- next = null;
- dummyWorkspace = null;
- }
- var isMovableFn = function() { return true; };
- /**
- * These tests check that the reasons for failures to connect are consistent
- * (internal view of error states).
- */
- function testCanConnectWithReason_TargetNull() {
- connectionTest_setUp();
- assertEquals(Blockly.Connection.REASON_TARGET_NULL,
- input.canConnectWithReason_(null));
- connectionTest_tearDown();
- }
- function testCanConnectWithReason_Disconnect() {
- connectionTest_setUp();
- var tempConnection = new Blockly.Connection({workspace: dummyWorkspace, isMovable: isMovableFn},
- Blockly.OUTPUT_VALUE);
- Blockly.Connection.connectReciprocally_(input, tempConnection);
- assertEquals(Blockly.Connection.CAN_CONNECT,
- input.canConnectWithReason_(output));
- connectionTest_tearDown();
- }
- function testCanConnectWithReason_DifferentWorkspaces() {
- connectionTest_setUp();
- input = new Blockly.Connection({workspace: {}}, Blockly.INPUT_VALUE);
- output = new Blockly.Connection({workspace: dummyWorkspace},
- Blockly.OUTPUT_VALUE);
- assertEquals(Blockly.Connection.REASON_DIFFERENT_WORKSPACES,
- input.canConnectWithReason_(output));
- connectionTest_tearDown();
- }
- function testCanConnectWithReason_Self() {
- connectionTest_setUp();
- var block = {type_: "test block"};
- input.sourceBlock_ = block;
- assertEquals(Blockly.Connection.REASON_SELF_CONNECTION,
- input.canConnectWithReason_(input));
- connectionTest_tearDown();
- }
- function testCanConnectWithReason_Type() {
- connectionTest_setUp();
- assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
- input.canConnectWithReason_(previous));
- assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
- input.canConnectWithReason_(next));
- assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
- output.canConnectWithReason_(previous));
- assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
- output.canConnectWithReason_(next));
- assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
- previous.canConnectWithReason_(input));
- assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
- previous.canConnectWithReason_(output));
- assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
- next.canConnectWithReason_(input));
- assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
- next.canConnectWithReason_(output));
- connectionTest_tearDown();
- }
- function testCanConnectWithReason_CanConnect() {
- connectionTest_setUp();
- assertEquals(Blockly.Connection.CAN_CONNECT,
- previous.canConnectWithReason_(next));
- assertEquals(Blockly.Connection.CAN_CONNECT,
- next.canConnectWithReason_(previous));
- assertEquals(Blockly.Connection.CAN_CONNECT,
- input.canConnectWithReason_(output));
- assertEquals(Blockly.Connection.CAN_CONNECT,
- output.canConnectWithReason_(input));
- connectionTest_tearDown();
- }
- /**
- * The next set of tests checks that exceptions are being thrown at the correct
- * times (external view of errors).
- */
- function testCheckConnection_Self() {
- connectionTest_setUp();
- var block = {type_: "test block"};
- input.sourceBlock_ = block;
- try {
- input.checkConnection_(input);
- fail();
- } catch (e) {
- // expected
- }
- connectionTest_tearDown();
- }
- function testCheckConnection_TypeInputPrev() {
- connectionTest_setUp();
- try {
- input.checkConnection_(previous);
- fail();
- } catch (e) {
- // expected
- }
- connectionTest_tearDown();
- }
- function testCheckConnection_TypeInputNext() {
- connectionTest_setUp();
- try {
- input.checkConnection_(next);
- fail();
- } catch (e) {
- // expected
- }
- connectionTest_tearDown();
- }
- function testCheckConnection_TypeOutputPrev() {
- connectionTest_setUp();
- try {
- output.checkConnection_(previous);
- fail();
- } catch (e) {
- // expected
- }
- connectionTest_tearDown();
- }
- function testCheckConnection_TypePrevInput() {
- connectionTest_setUp();
- try {
- previous.checkConnection_(input);
- fail();
- } catch (e) {
- // expected
- }
- connectionTest_tearDown();
- }
- function testCheckConnection_TypePrevOutput() {
- connectionTest_setUp();
- try {
- previous.checkConnection_(output);
- fail();
- } catch (e) {
- // expected
- }
- connectionTest_tearDown();
- }
- function testCheckConnection_TypeNextInput() {
- connectionTest_setUp();
- try {
- next.checkConnection_(input);
- fail();
- } catch (e) {
- // expected
- }
- connectionTest_tearDown();
- }
- function testCheckConnection_TypeNextOutput() {
- connectionTest_setUp();
- try {
- next.checkConnection_(output);
- fail();
- } catch (e) {
- // expected
- }
- connectionTest_tearDown();
- }
- function test_isConnectionAllowed_Distance() {
- var sharedWorkspace = {};
- // Two connections of opposite types near each other.
- var one = helper_createConnection(5 /* x */, 10 /* y */,
- Blockly.INPUT_VALUE, null, true);
- one.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
- var two = helper_createConnection(10 /* x */, 15 /* y */,
- Blockly.OUTPUT_VALUE, null, true);
- two.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
- assertTrue(one.isConnectionAllowed(two, 20.0));
- // Move connections farther apart.
- two.x_ = 100;
- two.y_ = 100;
- assertFalse(one.isConnectionAllowed(two, 20.0));
- }
- function test_isConnectionAllowed_Unrendered() {
- var sharedWorkspace = {};
- var one = helper_createConnection(5 /* x */, 10 /* y */,
- Blockly.INPUT_VALUE);
- one.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
- // Don't offer to connect an already connected left (male) value plug to
- // an available right (female) value plug.
- var two = helper_createConnection(0, 0, Blockly.OUTPUT_VALUE);
- two.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
- assertTrue(one.isConnectionAllowed(two));
- var three = helper_createConnection(0, 0, Blockly.INPUT_VALUE);
- three.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
- Blockly.Connection.connectReciprocally_(two, three);
- assertFalse(one.isConnectionAllowed(two));
- // Don't connect two connections on the same block.
- two.sourceBlock_ = one.sourceBlock_;
- assertFalse(one.isConnectionAllowed(two));
- }
- function test_isConnectionAllowed_NoNext() {
- var sharedWorkspace = {};
- var one = helper_createConnection(0, 0, Blockly.NEXT_STATEMENT);
- one.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
- one.sourceBlock_.nextConnection = one;
- var two = helper_createConnection(0, 0, Blockly.PREVIOUS_STATEMENT);
- two.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
- assertTrue(two.isConnectionAllowed(one));
- var three = helper_createConnection(0, 0, Blockly.PREVIOUS_STATEMENT);
- three.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
- three.sourceBlock_.previousConnection = three;
- Blockly.Connection.connectReciprocally_(one, three);
- // A terminal block is allowed to replace another terminal block.
- assertTrue(two.isConnectionAllowed(one));
- }
- function testCheckConnection_Okay() {
- connectionTest_setUp();
- previous.checkConnection_(next);
- next.checkConnection_(previous);
- input.checkConnection_(output);
- output.checkConnection_(input);
- connectionTest_tearDown();
- }
|