extensions_test.js 18.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
/**
 * @license
 * Visual Blocks Editor
 *
 * Copyright 2017 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 Blockly.Extensions
 * @author Anm@anm.me (Andrew n marshall)
 */
'use strict';

function test_extension() {
  var workspace = new Blockly.Workspace();
  var block;
  try {
    assertUndefined(Blockly.Extensions.ALL_['extensions_test']);

    var numCallsToBefore = 0;
    var numCallsToAfter = 0;

    // Extension defined before the block type is defined.
    Blockly.Extensions.register('extensions_test_before', function () {
      numCallsToBefore++;
      this.extendedWithBefore = true;
    });

    Blockly.defineBlocksWithJsonArray([{
      "type": "extension_test_block",
      "message0": "extension_test_block",
      "extensions": ["extensions_test_before", "extensions_test_after"]
    }]);

    // Extension defined after the block type (but before instantiation).
    Blockly.Extensions.register('extensions_test_after', function () {
      numCallsToAfter++;
      this.extendedWithAfter = true;
    });

    assert(goog.isFunction(Blockly.Extensions.ALL_['extensions_test_before']));
    assert(goog.isFunction(Blockly.Extensions.ALL_['extensions_test_after']));
    assertEquals(0, numCallsToBefore);
    assertEquals(0, numCallsToAfter);

    block = new Blockly.Block(workspace, 'extension_test_block');

    assertEquals(1, numCallsToBefore);
    assertEquals(1, numCallsToAfter);
    assert(block.extendedWithBefore);
    assert(block.extendedWithAfter);
  } finally {
    block && block.dispose();
    workspace.dispose();

    delete Blockly.Extensions.ALL_['extensions_test_before'];
    delete Blockly.Extensions.ALL_['extensions_test_after'];
    delete Blockly.Blocks['extension_test_block'];
  }
}

function test_extension_missing() {
  var workspace = new Blockly.Workspace();
  var block;
  var exceptionWasThrown = false;
  try {
    assertUndefined(Blockly.Extensions.ALL_['missing_extension']);
    Blockly.defineBlocksWithJsonArray([{
      "type": "missing_extension_block",
      "message0": "missing_extension_block",
      "extensions": ["missing_extension"]
    }]);

    block = new Blockly.Block(workspace, 'missing_extension_block');
  } catch (e) {
    // Expected.
    exceptionWasThrown = true;
  } finally {
    block && block.dispose();
    workspace.dispose();
    delete Blockly.Blocks['missing_extension_block'];
  }
  assert(exceptionWasThrown);
}

function test_extension_not_a_function() {
  var exceptionWasThrown = false;
  try {
    assertUndefined(Blockly.Extensions.ALL_['extension_just_a_string']);
    Blockly.Extensions.register('extension_just_a_string', 'extension_just_a_string');
  } catch (e) {
    // Expected.
    exceptionWasThrown = true;
  } finally {
    delete Blockly.Extensions.ALL_['extension_just_a_string'];
  }
  assert(exceptionWasThrown);

  var exceptionWasThrown = false;
  try {
    assertUndefined(Blockly.Extensions.ALL_['extension_is_null']);
    Blockly.Extensions.register('extension_is_null', null);
  } catch (e) {
    // Expected.
    exceptionWasThrown = true;
  } finally {
    delete Blockly.Extensions.ALL_['extension_is_null'];
  }
  assert(exceptionWasThrown);

  var exceptionWasThrown = false;
  try {
    assertUndefined(Blockly.Extensions.ALL_['extension_is_undefined']);
    Blockly.Extensions.register('extension_is_undefined');
  } catch (e) {
    // Expected.
    exceptionWasThrown = true;
  } finally {
    delete Blockly.Extensions.ALL_['extension_is_undefined'];
  }
  assert(exceptionWasThrown);
}

function test_parent_tooltip_when_inline() {
  var defaultTooltip = "defaultTooltip";
  var parentTooltip = "parentTooltip";

  var workspace = new Blockly.Workspace();
  var block;
  try {
    Blockly.defineBlocksWithJsonArray([
      {
        "type": "test_parent_tooltip_when_inline",
        "message0": "test_parent_tooltip_when_inline",
        "output": true,
        "tooltip": defaultTooltip,
        "extensions": ["parent_tooltip_when_inline"]
      },
      {
        "type": "test_parent",
        "message0": "%1",
        "args0": [
          {
            "type": "input_value",
            "name": "INPUT"
          }
        ],
        "tooltip": parentTooltip
      }
    ]);

    block = new Blockly.Block(workspace, 'test_parent_tooltip_when_inline');

    // Tooltip is dynamic after extension initialization.
    assert(goog.isFunction(block.tooltip));
    assertEquals(block.tooltip(), defaultTooltip);

    // Tooltip is normal before connected to parent.
    var parent = new Blockly.Block(workspace, 'test_parent');
    // Inputs default to inline in scratch-blocks.
    parent.setInputsInline(false);
    assertEquals(parent.tooltip, parentTooltip);
    assertFalse(!!parent.inputsInline);

    // Tooltip is normal when parent is not inline.
    parent.getInput('INPUT').connection.connect(block.outputConnection);
    assertEquals(block.getParent(), parent);
    assertEquals(block.tooltip(), defaultTooltip);

    // Tooltip is parent's when parent is inline.
    parent.setInputsInline(true);
    assertEquals(block.tooltip(), parentTooltip);

    // Tooltip revert when disconnected.
    parent.getInput('INPUT').connection.disconnect();
    assert(!block.getParent());
    assertEquals(block.tooltip(), defaultTooltip);
  } finally {
    block && block.dispose();
    workspace.dispose();

    delete Blockly.Blocks['test_parent_tooltip_when_inline'];
    delete Blockly.Blocks['test_parent'];
  }
}

function test_mixin_extension() {
  var TEST_MIXIN = {
    field: 'FIELD',
    method: function() {
      console.log('TEXT_MIXIN method()');
    }
  };

  var workspace = new Blockly.Workspace();
  var block;
  try {
    assertUndefined(Blockly.Extensions.ALL_['mixin_test']);

    // Extension defined before the block type is defined.
    Blockly.Extensions.registerMixin('mixin_test', TEST_MIXIN);
    assert(goog.isFunction(Blockly.Extensions.ALL_['mixin_test']));

    Blockly.defineBlocksWithJsonArray([{
      "type": "test_block_mixin",
      "message0": "test_block_mixin",
      "extensions": ["mixin_test"]
    }]);

    block = new Blockly.Block(workspace, 'test_block_mixin');

    assertEquals(TEST_MIXIN.field, block.field);
    assertEquals(TEST_MIXIN.method, block.method);
  } finally {
    block && block.dispose();
    workspace.dispose();

    delete Blockly.Extensions.ALL_['mixin_test'];
    delete Blockly.Blocks['test_block_mixin'];
  }
}

function test_bad_mixin_overwrites_local_value() {
  var TEST_MIXIN_BAD_INPUTLIST = {
    inputList: 'bad inputList'  // Defined in constructor
  };

  var workspace = new Blockly.Workspace();
  var block;
  try {
    assertUndefined(Blockly.Extensions.ALL_['mixin_bad_inputList']);

    // Extension defined before the block type is defined.
    Blockly.Extensions.registerMixin('mixin_bad_inputList', TEST_MIXIN_BAD_INPUTLIST);
    assert(goog.isFunction(Blockly.Extensions.ALL_['mixin_bad_inputList']));

    Blockly.defineBlocksWithJsonArray([{
      "type": "test_block_bad_inputList",
      "message0": "test_block_bad_inputList",
      "extensions": ["mixin_bad_inputList"]
    }]);

    try {
      block = new Blockly.Block(workspace, 'test_block_bad_inputList');
    } catch (e) {
      // Expected Error
      assert(e.message.indexOf('inputList') >= 0);  // Reference the conflict
      return;
    }
    fail('Expected error when constructing block');
  } finally {
    block && block.dispose();
    workspace.dispose();

    delete Blockly.Extensions.ALL_['mixin_bad_inputList'];
    delete Blockly.Blocks['test_block_bad_inputList'];
  }
}

function test_bad_mixin_overwrites_prototype() {
  var TEST_MIXIN_BAD_COLOUR = {
    colour_: 'bad colour_' // Defined on prototype
  };

  var workspace = new Blockly.Workspace();
  var block;
  try {
    assertUndefined(Blockly.Extensions.ALL_['mixin_bad_colour_']);

    // Extension defined before the block type is defined.
    Blockly.Extensions.registerMixin('mixin_bad_colour_', TEST_MIXIN_BAD_COLOUR);
    assert(goog.isFunction(Blockly.Extensions.ALL_['mixin_bad_colour_']));

    Blockly.defineBlocksWithJsonArray([{
      "type": "test_block_bad_colour",
      "message0": "test_block_bad_colour",
      "extensions": ["mixin_bad_colour_"]
    }]);

    try {
      block = new Blockly.Block(workspace, 'test_block_bad_colour');
    } catch (e) {
      // Expected Error
      assert(e.message.indexOf('colour_') >= 0);  // Reference the conflict
      return;
    }
    fail('Expected error when constructing block');
  } finally {
    block && block.dispose();
    workspace.dispose();

    delete Blockly.Extensions.ALL_['mixin_bad_colour_'];
    delete Blockly.Blocks['test_block_bad_colour'];
  }
}

function test_mutator_mixin() {
  var workspace = new Blockly.Workspace();
  var block;

  try {
    Blockly.defineBlocksWithJsonArray([{
      "type": "mutator_test_block",
      "message0": "mutator_test_block",
      "mutator": "mutator_test"
    }]);

    // Events code calls mutationToDom and expects it to give back a meaningful
    // value.
    Blockly.Events.disable();
    Blockly.Extensions.registerMutator('mutator_test',
      {
        domToMutation: function() {
          return 'domToMutationFn';
        },
        mutationToDom: function() {
          return 'mutationToDomFn';
        },
        compose: function() {
          return 'composeFn';
        },
        decompose: function() {
          return 'decomposeFn';
        }
      });

    block = new Blockly.Block(workspace, 'mutator_test_block');

    // Make sure all of the functions were installed correctly.
    assertEquals(block.domToMutation(), 'domToMutationFn');
    assertEquals(block.mutationToDom(), 'mutationToDomFn');
    assertEquals(block.compose(), 'composeFn');
    assertEquals(block.decompose(), 'decomposeFn');
  } finally {
    if (block) {
      block.dispose();
    }
    workspace.dispose();
    Blockly.Events.enable();
    delete Blockly.Extensions.ALL_['mutator_test'];
  }
}

function test_mutator_mixin_no_dialog() {
  var workspace = new Blockly.Workspace();
  var block;

  try {
    Blockly.defineBlocksWithJsonArray([{
      "type": "mutator_test_block",
      "message0": "mutator_test_block",
      "mutator": "mutator_test"
    }]);

    // Events code calls mutationToDom and expects it to give back a meaningful
    // value.
    Blockly.Events.disable();
    assertUndefined(Blockly.Extensions.ALL_['mutator_test']);
    Blockly.Extensions.registerMutator('mutator_test',
      {
        domToMutation: function() {
          return 'domToMutationFn';
        },
        mutationToDom: function() {
          return 'mutationToDomFn';
        }
      });

    block = new Blockly.Block(workspace, 'mutator_test_block');

    // Make sure all of the functions were installed correctly.
    assertEquals(block.domToMutation(), 'domToMutationFn');
    assertEquals(block.mutationToDom(), 'mutationToDomFn');
    assertFalse(block.hasOwnProperty('compose'));
    assertFalse(block.hasOwnProperty('decompose'));
  } finally {
    if (block) {
      block.dispose();
    }
    workspace.dispose();
    Blockly.Events.enable();
    delete Blockly.Extensions.ALL_['mutator_test'];
  }
}

// Explicitly check all four things that could be missing.
function test_mutator_mixin_no_decompose_fails() {
  var exceptionWasThrown = false;
  try {
    Blockly.Extensions.registerMutator('mutator_test',
      {
        domToMutation: function() {
          return 'domToMutationFn';
        },
        mutationToDom: function() {
          return 'mutationToDomFn';
        },
        compose: function() {
          return 'composeFn';
        }
      });
  } catch (e) {
    // Expected.
    exceptionWasThrown = true;
  } finally {
    delete Blockly.Extensions.ALL_['mutator_test'];
  }
  assertTrue(exceptionWasThrown);
}

function test_mutator_mixin_no_compose_fails() {
  var exceptionWasThrown = false;
  try {
    Blockly.Extensions.registerMutator('mutator_test',
      {
        domToMutation: function() {
          return 'domToMutationFn';
        },
        mutationToDom: function() {
          return 'mutationToDomFn';
        },
        decompose: function() {
          return 'decomposeFn';
        }
      });
  } catch (e) {
    // Expected.
    exceptionWasThrown = true;
  } finally {
    delete Blockly.Extensions.ALL_['mutator_test'];
  }
  assertTrue(exceptionWasThrown);
}

function test_mutator_mixin_no_domToMutation_fails() {
  var exceptionWasThrown = false;
  try {
    Blockly.Extensions.registerMutator('mutator_test',
      {
        mutationToDom: function() {
          return 'mutationToDomFn';
        },
        compose: function() {
          return 'composeFn';
        },
        decompose: function() {
          return 'decomposeFn';
        }
      });
  } catch (e) {
    // Expected.
    exceptionWasThrown = true;
  } finally {
    delete Blockly.Extensions.ALL_['mutator_test'];
  }
  assertTrue(exceptionWasThrown);
}

function test_mutator_mixin_no_mutationToDom_fails() {
  var exceptionWasThrown = false;
  try {
    Blockly.Extensions.registerMutator('mutator_test',
      {
        domToMutation: function() {
          return 'domToMutationFn';
        },
        compose: function() {
          return 'composeFn';
        },
        decompose: function() {
          return 'decomposeFn';
        }
      });
  } catch (e) {
    // Expected.
    exceptionWasThrown = true;
  } finally {
    delete Blockly.Extensions.ALL_['mutator_test'];
  }
  assertTrue(exceptionWasThrown);
}

function test_use_mutator_as_extension_fails() {
  var workspace = new Blockly.Workspace();
  var block;
  var exceptionWasThrown = false;

  try {
    Blockly.defineBlocksWithJsonArray([{
      "type": "mutator_test_block",
      "message0": "mutator_test_block",
      "extensions": ["mutator_test"]
    }]);

    Blockly.Events.disable();
    assertUndefined(Blockly.Extensions.ALL_['mutator_test']);
    Blockly.Extensions.registerMutator('mutator_test',
      {
        domToMutation: function() {
          return 'domToMutationFn';
        },
        mutationToDom: function() {
          return 'mutationToDomFn';
        }
      });

    // Events code calls mutationToDom and expects it to give back a meaningful
    // value.
    block = new Blockly.Block(workspace, 'mutator_test_block');
  } catch (e) {
    // Expected
    exceptionWasThrown = true;
    // Should have failed on apply, not on register.
    assertNotNull(Blockly.Extensions.ALL_['mutator_test']);
  } finally {
    if (block) {
      block.dispose();
    }
    workspace.dispose();
    Blockly.Events.enable();
    delete Blockly.Extensions.ALL_['mutator_test'];
  }
  assertTrue(exceptionWasThrown);
}

function test_use_mutator_mixin_as_extension_fails() {
  var workspace = new Blockly.Workspace();
  var block;
  var exceptionWasThrown = false;

  try {
    Blockly.defineBlocksWithJsonArray([{
      "type": "mutator_test_block",
      "message0": "mutator_test_block",
      "extensions": ["mutator_test"]
    }]);

    // Events code calls mutationToDom and expects it to give back a meaningful
    // value.
    Blockly.Events.disable();
    assertUndefined(Blockly.Extensions.ALL_['mutator_test']);
    Blockly.Extensions.registerMixin('mutator_test',
      {
        domToMutation: function() {
          return 'domToMutationFn';
        },
        mutationToDom: function() {
          return 'mutationToDomFn';
        }
      });

    block = new Blockly.Block(workspace, 'mutator_test_block');
  } catch (e) {
    // Expected
    exceptionWasThrown = true;
    // Should have failed on apply, not on register.
    assertNotNull(Blockly.Extensions.ALL_['mutator_test']);
  } finally {
    if (block) {
      block.dispose();
    }
    workspace.dispose();
    Blockly.Events.enable();
    delete Blockly.Extensions.ALL_['mutator_test'];
  }
  assertTrue(exceptionWasThrown);
}

function test_use_extension_as_mutator_fails() {
  var workspace = new Blockly.Workspace();
  var block;
  var exceptionWasThrown = false;

  try {
    Blockly.defineBlocksWithJsonArray([{
      "type": "mutator_test_block",
      "message0": "mutator_test_block",
      "mutator": ["extensions_test"]
    }]);

    // Events code calls mutationToDom and expects it to give back a meaningful
    // value.
    Blockly.Events.disable();
    assertUndefined(Blockly.Extensions.ALL_['extensions_test']);
    Blockly.Extensions.register('extensions_test', function() {
      return 'extensions_test_fn';
    });

    block = new Blockly.Block(workspace, 'mutator_test_block');
  } catch (e) {
    // Expected
    exceptionWasThrown = true;
    // Should have failed on apply, not on register.
    assertNotNull(Blockly.Extensions.ALL_['extensions_test']);
  } finally {
    if (block) {
      block.dispose();
    }
    workspace.dispose();
    Blockly.Events.enable();
    delete Blockly.Extensions.ALL_['extensions_test'];
  }
  assertTrue(exceptionWasThrown);
}

function test_mutator_mixin_plus_function() {
  var workspace = new Blockly.Workspace();
  var block;
  var fnWasCalled = false;

  try {
    Blockly.defineBlocksWithJsonArray([{
      "type": "mutator_test_block",
      "message0": "mutator_test_block",
      "mutator": ["extensions_test"]
    }]);

    Blockly.Events.disable();
    assertUndefined(Blockly.Extensions.ALL_['extensions_test']);
    Blockly.Extensions.registerMutator('extensions_test',
      {
        domToMutation: function() {
          return 'domToMutationFn';
        },
        mutationToDom: function() {
          return 'mutationToDomFn';
        }
      },
      function() {
        fnWasCalled = true;
      }
    );

    // Events code calls mutationToDom and expects it to give back a meaningful
    // value.
    block = new Blockly.Block(workspace, 'mutator_test_block');
  } finally {
    if (block) {
      block.dispose();
    }
    workspace.dispose();
    Blockly.Events.enable();
    delete Blockly.Extensions.ALL_['extensions_test'];
  }
  assertTrue(fnWasCalled);
}