PageRenderTime 88ms CodeModel.GetById 2ms app.highlight 79ms RepoModel.GetById 1ms app.codeStats 0ms

/test/spec/NativeMenu-test.js

http://github.com/adobe/brackets
JavaScript | 1659 lines | 1293 code | 284 blank | 82 comment | 9 complexity | 78c46a99b908cac27455138bed18d4a5 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/*
   2 * Copyright (c) 2013 - present Adobe Systems Incorporated. All rights reserved.
   3 *
   4 * Permission is hereby granted, free of charge, to any person obtaining a
   5 * copy of this software and associated documentation files (the "Software"),
   6 * to deal in the Software without restriction, including without limitation
   7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   8 * and/or sell copies of the Software, and to permit persons to whom the
   9 * Software is furnished to do so, subject to the following conditions:
  10 *
  11 * The above copyright notice and this permission notice shall be included in
  12 * all copies or substantial portions of the Software.
  13 *
  14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20 * DEALINGS IN THE SOFTWARE.
  21 *
  22 */
  23
  24/*global describe, it, expect, beforeEach, afterEach, waitsFor, runs */
  25
  26define(function (require, exports, module) {
  27    'use strict';
  28
  29    require("utils/Global");
  30
  31    // Don't run tests when running in browser
  32    if (brackets.inBrowser) {
  33        return;
  34    }
  35
  36    // These are tests for the low-level file io routines in brackets-app. Make sure
  37    // you have the latest brackets-app before running.
  38
  39    describe("Native Menus", function () {
  40
  41        var PLACEHOLDER_MENU_ID     = "placeholder",
  42            PLACEHOLDER_MENU_TITLE  = "MENU",
  43            TEST_MENU_TITLE         = "TEST",
  44            TEST_MENU_ID            = "test",
  45            TEST_MENU_ITEM          = "Item 1",
  46            TEST_MENU_ITEM_ID       = "item1";
  47
  48        it("should have a brackets.app namespace", function () {
  49            var complete = false,
  50                error = 0;
  51
  52            expect(brackets.app).toBeTruthy();
  53
  54            // Add an empty native menu so the menu bar doesn't keep flashing
  55            runs(function () {
  56                brackets.app.addMenu(PLACEHOLDER_MENU_TITLE, PLACEHOLDER_MENU_ID, "", "", function (err) {
  57                    complete = true;
  58                    error = err;
  59                });
  60            });
  61
  62            waitsFor(function () { return complete; });
  63
  64            expect(error).toBe(0);
  65        });
  66
  67        describe("addMenu", function () {
  68
  69            it("should add a menu", function () {
  70                var complete = false,
  71                    error = 0,
  72                    title;
  73
  74                // Make sure menu isn't present
  75                runs(function () {
  76                    brackets.app.getMenuTitle(TEST_MENU_ID, function (err) {
  77                        complete = true;
  78                        error = err;
  79                    });
  80                });
  81
  82                waitsFor(function () { return complete; });
  83
  84                runs(function () {
  85                    expect(error).toBe(brackets.fs.ERR_NOT_FOUND);
  86                });
  87
  88                // Add menu
  89                runs(function () {
  90                    complete = false;
  91                    brackets.app.addMenu(TEST_MENU_TITLE, TEST_MENU_ID, "", "", function (err) {
  92                        complete = true;
  93                        error = err;
  94                    });
  95                });
  96
  97                waitsFor(function () { return complete; });
  98
  99                runs(function () {
 100                    expect(error).toBe(0);
 101                });
 102
 103                // Verify menu is found
 104                runs(function () {
 105                    complete = false;
 106                    brackets.app.getMenuTitle(TEST_MENU_ID, function (err, titleStr) {
 107                        complete = true;
 108                        error = err;
 109                        title = titleStr;
 110                    });
 111                });
 112
 113                waitsFor(function () { return complete; });
 114
 115                runs(function () {
 116                    expect(error).toBe(0);
 117                    expect(title).toBe(TEST_MENU_TITLE);
 118                });
 119
 120                // Remove menu
 121                runs(function () {
 122                    complete = false;
 123                    brackets.app.removeMenu(TEST_MENU_ID, function (err) {
 124                        complete = true;
 125                        // Ignore error
 126                    });
 127                });
 128                waitsFor(function () { return complete; });
 129            });
 130
 131            it("should return an error if invalid parameters are passed", function () {
 132                var complete = false,
 133                    error = 0;
 134
 135                runs(function () {
 136                    brackets.app.addMenu(TEST_MENU_TITLE, TEST_MENU_ID, 42, "", function (err) {
 137                        complete = true;
 138                        error = err;
 139                    });
 140                });
 141
 142                waitsFor(function () { return complete; });
 143
 144                runs(function () {
 145                    expect(error).toBe(brackets.fs.ERR_INVALID_PARAMS);
 146                });
 147            });
 148        }); // describe("addMenu")
 149
 150        describe("addMenu (with reference)", function () {
 151            var complete = false,
 152                error = 0,
 153                parentId,
 154                position = -1;
 155
 156            beforeEach(function () {
 157                runs(function () {
 158                    complete = false;
 159                    brackets.app.addMenu(TEST_MENU_TITLE, TEST_MENU_ID, "", "", function (err) {
 160                        complete = true;
 161                        error = err;
 162                    });
 163                });
 164
 165                waitsFor(function () { return complete; });
 166
 167                runs(function () {
 168                    expect(error).toBe(0);
 169                });
 170            });
 171
 172            afterEach(function () {
 173                runs(function () {
 174                    complete = false;
 175                    brackets.app.removeMenu(TEST_MENU_ID, function (err) {
 176                        complete = true;
 177                        error = err;
 178                    });
 179                });
 180
 181                waitsFor(function () { return complete; });
 182
 183                runs(function () {
 184                    expect(error).toBe(0);
 185                });
 186            });
 187
 188            it("should add new menu in last position of list", function () {
 189                error = 0;
 190                runs(function () {
 191                    complete = false;
 192                    brackets.app.addMenu("Custom1", "menu-unittest1", "", "", function (err) {
 193                        complete = true;
 194                        error = err;
 195                    });
 196                });
 197
 198                waitsFor(function () { return complete; });
 199
 200                runs(function () {
 201                    expect(error).toBe(0);
 202                });
 203
 204                // Verify menu is found
 205                runs(function () {
 206                    complete = false;
 207                    parentId = null;
 208                    position = -1;
 209                    brackets.app.getMenuPosition("menu-unittest1", function (err, parent, index) {
 210                        complete = true;
 211                        error = err;
 212                        parentId = parent;
 213                        position = index;
 214                    });
 215                });
 216
 217                waitsFor(function () { return complete; });
 218
 219                runs(function () {
 220                    expect(error).toBe(0);
 221                    expect(parentId).toBe("");
 222                    expect(position).toBeGreaterThan(0);
 223                });
 224
 225                // Remove menu
 226                runs(function () {
 227                    complete = false;
 228                    brackets.app.removeMenu("menu-unittest1", function (err) {
 229                        complete = true;
 230                        error = err;
 231                    });
 232                });
 233                waitsFor(function () { return complete; });
 234
 235                runs(function () {
 236                    expect(error).toBe(0);
 237                });
 238            });
 239
 240            it("should add new menu after reference menu", function () {
 241                var targetPos = -1;
 242                error = 0;
 243
 244                runs(function () {
 245                    complete = false;
 246                    brackets.app.addMenu("CustomFirst", "menu-unittest-first", "first", "", function (err) {
 247                        complete = true;
 248                        error = err;
 249                    });
 250                });
 251
 252                waitsFor(function () { return complete; });
 253
 254                runs(function () {
 255                    expect(error).toBe(0);
 256                });
 257
 258                runs(function () {
 259                    complete = false;
 260                    brackets.app.addMenu("CustomAfter", "menu-unittest-after", "after", "menu-unittest-first", function (err) {
 261                        complete = true;
 262                        error = err;
 263                    });
 264                });
 265
 266                waitsFor(function () { return complete; });
 267
 268                runs(function () {
 269                    expect(error).toBe(0);
 270                });
 271
 272                // Verify menu is found
 273                runs(function () {
 274                    complete = false;
 275                    parentId = null;
 276                    position = -1;
 277                    targetPos = -1;
 278                    brackets.app.getMenuPosition("menu-unittest-first", function (err, parent, index) {
 279                        complete = true;
 280                        error = err;
 281                        parentId = parent;
 282                        position = index;
 283                        targetPos = position + 1;
 284                    });
 285                });
 286
 287                waitsFor(function () { return complete; });
 288
 289                runs(function () {
 290                    expect(error).toBe(0);
 291                    expect(parentId).toBe("");
 292                });
 293
 294                // Verify menu is found
 295                runs(function () {
 296                    complete = false;
 297                    parentId = null;
 298                    position = -1;
 299                    brackets.app.getMenuPosition("menu-unittest-after", function (err, parent, index) {
 300                        complete = true;
 301                        error = err;
 302                        parentId = parent;
 303                        position = index;
 304                    });
 305                });
 306
 307                waitsFor(function () { return complete; });
 308
 309                runs(function () {
 310                    expect(error).toBe(0);
 311                    expect(parentId).toBe("");
 312                    expect(position).toBe(targetPos);
 313                });
 314
 315                // Remove menu
 316                runs(function () {
 317                    complete = false;
 318                    brackets.app.removeMenu("menu-unittest-first", function (err) {
 319                        complete = true;
 320                        // Ignore error
 321                    });
 322                });
 323                waitsFor(function () { return complete; });
 324
 325                runs(function () {
 326                    complete = false;
 327                    brackets.app.removeMenu("menu-unittest-after", function (err) {
 328                        complete = true;
 329                        // Ignore error
 330                    });
 331                });
 332                waitsFor(function () { return complete; });
 333            });
 334
 335            it("should add new menu before reference menu", function () {
 336                var targetPos = -1;
 337                error = 0;
 338
 339                runs(function () {
 340                    complete = false;
 341                    brackets.app.addMenu("CustomLast", "menu-unittest-last", "last", "", function (err) {
 342                        complete = true;
 343                        error = err;
 344                    });
 345                });
 346
 347                waitsFor(function () { return complete; });
 348
 349                runs(function () {
 350                    expect(error).toBe(0);
 351                });
 352
 353                runs(function () {
 354                    complete = false;
 355                    brackets.app.addMenu("CustomBefore", "menu-unittest-before", "before", "menu-unittest-last", function (err) {
 356                        complete = true;
 357                        error = err;
 358                    });
 359                });
 360
 361                waitsFor(function () { return complete; });
 362
 363                runs(function () {
 364                    expect(error).toBe(0);
 365                });
 366
 367                // Verify menu is found
 368                runs(function () {
 369                    complete = false;
 370                    parentId = null;
 371                    position = -1;
 372                    targetPos = -1;
 373                    brackets.app.getMenuPosition("menu-unittest-last", function (err, parent, index) {
 374                        complete = true;
 375                        error = err;
 376                        parentId = parent;
 377                        position = index;
 378                        targetPos = position - 1;
 379                    });
 380                });
 381
 382                waitsFor(function () { return complete; });
 383
 384                runs(function () {
 385                    expect(error).toBe(0);
 386                    expect(parentId).toBe("");
 387                });
 388
 389                // Verify menu is found
 390                runs(function () {
 391                    complete = false;
 392                    parentId = null;
 393                    position = -1;
 394                    brackets.app.getMenuPosition("menu-unittest-before", function (err, parent, index) {
 395                        complete = true;
 396                        error = err;
 397                        parentId = parent;
 398                        position = index;
 399                    });
 400                });
 401
 402                waitsFor(function () { return complete; });
 403
 404                runs(function () {
 405                    expect(error).toBe(0);
 406                    expect(parentId).toBe("");
 407                    expect(position).toBe(targetPos);
 408                });
 409
 410                // Remove menu
 411                runs(function () {
 412                    complete = false;
 413                    brackets.app.removeMenu("menu-unittest-last", function (err) {
 414                        complete = true;
 415                        // Ignore error
 416                    });
 417                });
 418                waitsFor(function () { return complete; });
 419
 420                runs(function () {
 421                    complete = false;
 422                    brackets.app.removeMenu("menu-unittest-before", function (err) {
 423                        complete = true;
 424                        // Ignore error
 425                    });
 426                });
 427                waitsFor(function () { return complete; });
 428            });
 429
 430            it("should add new menu at end of list when reference menu doesn't exist", function () {
 431                error = 0;
 432                runs(function () {
 433                    complete = false;
 434                    brackets.app.addMenu("Custom4", "menu-unittest4", "after", "NONEXISTANT", function (err) {
 435                        complete = true;
 436                        error = err;
 437                    });
 438                });
 439
 440                waitsFor(function () { return complete; });
 441
 442                runs(function () {
 443                    expect(error).toBe(brackets.fs.ERR_NOT_FOUND);
 444                });
 445
 446                // Verify menu is found
 447                runs(function () {
 448                    complete = false;
 449                    parentId = null;
 450                    position = -1;
 451                    brackets.app.getMenuPosition("menu-unittest4", function (err, parent, index) {
 452                        complete = true;
 453                        error = err;
 454                        parentId = parent;
 455                        position = index;
 456                    });
 457                });
 458
 459                waitsFor(function () { return complete; });
 460
 461                runs(function () {
 462                    expect(error).toBe(0);
 463                    expect(parentId).toBe("");
 464                    expect(position).toBeGreaterThan(0);
 465                });
 466
 467                // Remove menu
 468                runs(function () {
 469                    complete = false;
 470                    brackets.app.removeMenu("menu-unittest4", function (err) {
 471                        complete = true;
 472                        // Ignore error
 473                    });
 474                });
 475                waitsFor(function () { return complete; });
 476            });
 477
 478        }); // describe("addMenu (with reference)")
 479
 480        describe("addMenuItem", function () {
 481            var complete = false,
 482                error = 0,
 483                title;
 484
 485            beforeEach(function () {
 486                runs(function () {
 487                    complete = false;
 488                    brackets.app.addMenu(TEST_MENU_TITLE, TEST_MENU_ID, "", "", function (err) {
 489                        complete = true;
 490                        error = err;
 491                    });
 492                });
 493
 494                waitsFor(function () { return complete; });
 495
 496                runs(function () {
 497                    expect(error).toBe(0);
 498                });
 499            });
 500
 501            afterEach(function () {
 502                runs(function () {
 503                    complete = false;
 504                    brackets.app.removeMenu(TEST_MENU_ID, function (err) {
 505                        complete = true;
 506                        error = err;
 507                    });
 508                });
 509
 510                waitsFor(function () { return complete; });
 511
 512                runs(function () {
 513                    expect(error).toBe(0);
 514                });
 515            });
 516
 517            it("should add a menu item", function () {
 518                error = 0;
 519                runs(function () {
 520                    complete = false;
 521                    brackets.app.addMenuItem(TEST_MENU_ID, TEST_MENU_ITEM, TEST_MENU_ITEM_ID, "", "", "", "", function (err) {
 522                        complete = true;
 523                        error = err;
 524                    });
 525                });
 526
 527                waitsFor(function () { return complete; });
 528
 529                runs(function () {
 530                    expect(error).toBe(0);
 531                });
 532
 533                // Verify item
 534                runs(function () {
 535                    complete = false;
 536                    brackets.app.getMenuTitle(TEST_MENU_ITEM_ID, function (err, titleStr) {
 537                        complete = true;
 538                        error = err;
 539                        title = titleStr;
 540                    });
 541                });
 542
 543                waitsFor(function () { return complete; });
 544
 545                runs(function () {
 546                    expect(error).toBe(0);
 547                    expect(title).toBe(TEST_MENU_ITEM);
 548                    complete = false;
 549                    brackets.app.removeMenuItem(TEST_MENU_ITEM_ID, function (err) {
 550                        complete = true;
 551                    });
 552                });
 553
 554                waitsFor(function () { return complete; });
 555            });
 556
 557            it("should return an error if invalid parameters are passed", function () {
 558                runs(function () {
 559                    error = 0;
 560                    complete = false;
 561                    brackets.app.addMenuItem(TEST_MENU_ID, TEST_MENU_ITEM, TEST_MENU_ITEM_ID, "", 42, "", "", function (err) {
 562                        complete = true;
 563                        error = err;
 564                    });
 565                });
 566
 567                waitsFor(function () { return complete; });
 568
 569                runs(function () {
 570                    expect(error).toBe(brackets.fs.ERR_INVALID_PARAMS);
 571                });
 572            });
 573        }); // describe("addMenuItem")
 574
 575        describe("addMenuItem (with reference)", function () {
 576            var complete = false,
 577                error = 0,
 578                title,
 579                parentId = null,
 580                position = -1;
 581
 582            beforeEach(function () {
 583                runs(function () {
 584                    complete = false;
 585                    brackets.app.addMenu(TEST_MENU_TITLE, TEST_MENU_ID, "", "", function (err) {
 586                        complete = true;
 587                        error = err;
 588                    });
 589                });
 590
 591                waitsFor(function () { return complete; });
 592
 593                runs(function () {
 594                    expect(error).toBe(0);
 595                });
 596
 597                // Add a menu item into the empty menu
 598                runs(function () {
 599                    complete = false;
 600                    brackets.app.addMenuItem(TEST_MENU_ID, TEST_MENU_ITEM, TEST_MENU_ITEM_ID, "", "", "", "", function (err) {
 601                        complete = true;
 602                        error = err;
 603                    });
 604                });
 605
 606                waitsFor(function () { return complete; });
 607
 608                runs(function () {
 609                    expect(error).toBe(0);
 610                });
 611
 612            });
 613
 614            afterEach(function () {
 615                runs(function () {
 616                    complete = false;
 617                    brackets.app.removeMenuItem(TEST_MENU_ITEM_ID, function (err) {
 618                        complete = true;
 619                    });
 620                });
 621                waitsFor(function () { return complete; });
 622
 623                runs(function () {
 624                    complete = false;
 625                    brackets.app.removeMenu(TEST_MENU_ID, function (err) {
 626                        complete = true;
 627                        error = err;
 628                    });
 629                });
 630                waitsFor(function () { return complete; });
 631
 632                runs(function () {
 633                    expect(error).toBe(0);
 634                });
 635            });
 636
 637            it("should add a menu item in first position of menu", function () {
 638                error = 0;
 639                runs(function () {
 640                    complete = false;
 641                    brackets.app.addMenuItem(TEST_MENU_ID, "Brackets Test Command Custom 1", "Menu-test.command01", "", "", "first", "", function (err) {
 642                        complete = true;
 643                        error = err;
 644                    });
 645                });
 646
 647                waitsFor(function () { return complete; });
 648
 649                runs(function () {
 650                    expect(error).toBe(0);
 651                });
 652
 653                // Verify item is found in the right position
 654                runs(function () {
 655                    complete = false;
 656                    parentId = null;
 657                    position = -1;
 658                    brackets.app.getMenuPosition("Menu-test.command01", function (err, parent, index) {
 659                        complete = true;
 660                        error = err;
 661                        parentId = parent;
 662                        position = index;
 663                    });
 664                });
 665
 666                waitsFor(function () { return complete; });
 667
 668                runs(function () {
 669                    expect(error).toBe(0);
 670                    expect(parentId).toBe(TEST_MENU_ID);
 671                    expect(position).toBe(0);
 672                });
 673
 674                // Verify item
 675                runs(function () {
 676                    complete = false;
 677                    brackets.app.getMenuTitle("Menu-test.command01", function (err, titleStr) {
 678                        complete = true;
 679                        error = err;
 680                        title = titleStr;
 681                    });
 682                });
 683
 684                waitsFor(function () { return complete; });
 685
 686                runs(function () {
 687                    expect(error).toBe(0);
 688                    expect(title).toBe("Brackets Test Command Custom 1");
 689                });
 690
 691                runs(function () {
 692                    complete = false;
 693                    brackets.app.removeMenuItem("Menu-test.command01", function (err) {
 694                        complete = true;
 695                    });
 696                });
 697                waitsFor(function () { return complete; });
 698            });
 699
 700            it("should add a menu item in last position of menu", function () {
 701                error = 0;
 702                runs(function () {
 703                    complete = false;
 704                    brackets.app.addMenuItem(TEST_MENU_ID, "Brackets Test Command Custom 2", "Menu-test.command02", "", "", "last", "", function (err) {
 705                        complete = true;
 706                        error = err;
 707                    });
 708                });
 709
 710                waitsFor(function () { return complete; });
 711
 712                runs(function () {
 713                    expect(error).toBe(0);
 714                });
 715
 716                // Verify item is found in the right position
 717                runs(function () {
 718                    complete = false;
 719                    parentId = null;
 720                    position = -1;
 721                    brackets.app.getMenuPosition("Menu-test.command02", function (err, parent, index) {
 722                        complete = true;
 723                        error = err;
 724                        parentId = parent;
 725                        position = index;
 726                    });
 727                });
 728
 729                waitsFor(function () { return complete; });
 730
 731                runs(function () {
 732                    expect(error).toBe(0);
 733                    expect(parentId).toBe(TEST_MENU_ID);
 734                    expect(position).toBe(1);
 735                });
 736
 737                // Verify item
 738                runs(function () {
 739                    complete = false;
 740                    brackets.app.getMenuTitle("Menu-test.command02", function (err, titleStr) {
 741                        complete = true;
 742                        error = err;
 743                        title = titleStr;
 744                    });
 745                });
 746
 747                waitsFor(function () { return complete; });
 748
 749                runs(function () {
 750                    expect(error).toBe(0);
 751                    expect(title).toBe("Brackets Test Command Custom 2");
 752                });
 753
 754                runs(function () {
 755                    complete = false;
 756                    brackets.app.removeMenuItem("Menu-test.command02", function (err) {
 757                        complete = true;
 758                    });
 759                });
 760                waitsFor(function () { return complete; });
 761            });
 762
 763
 764            it("should add a menu item after the referenced menu item", function () {
 765                error = 0;
 766                runs(function () {
 767                    complete = false;
 768                    brackets.app.addMenuItem(TEST_MENU_ID, "Brackets Test Command Custom 3", "Menu-test.command03", "", "", "after", TEST_MENU_ITEM_ID, function (err) {
 769                        complete = true;
 770                        error = err;
 771                    });
 772                });
 773
 774                waitsFor(function () { return complete; });
 775
 776                runs(function () {
 777                    expect(error).toBe(0);
 778                });
 779
 780                // Verify item is found in the right position
 781                runs(function () {
 782                    complete = false;
 783                    parentId = null;
 784                    position = -1;
 785                    brackets.app.getMenuPosition("Menu-test.command03", function (err, parent, index) {
 786                        complete = true;
 787                        error = err;
 788                        parentId = parent;
 789                        position = index;
 790                    });
 791                });
 792
 793                waitsFor(function () { return complete; });
 794
 795                runs(function () {
 796                    expect(error).toBe(0);
 797                    expect(parentId).toBe(TEST_MENU_ID);
 798                    expect(position).toBe(1);
 799                });
 800
 801                // Verify item
 802                runs(function () {
 803                    complete = false;
 804                    brackets.app.getMenuTitle("Menu-test.command03", function (err, titleStr) {
 805                        complete = true;
 806                        error = err;
 807                        title = titleStr;
 808                    });
 809                });
 810
 811                waitsFor(function () { return complete; });
 812
 813                runs(function () {
 814                    expect(error).toBe(0);
 815                    expect(title).toBe("Brackets Test Command Custom 3");
 816                });
 817
 818                runs(function () {
 819                    complete = false;
 820                    brackets.app.removeMenuItem("Menu-test.command03", function (err) {
 821                        complete = true;
 822                    });
 823                });
 824                waitsFor(function () { return complete; });
 825            });
 826
 827            it("should add a menu item before the referenced menu item", function () {
 828                error = 0;
 829                runs(function () {
 830                    complete = false;
 831                    brackets.app.addMenuItem(TEST_MENU_ID, "Brackets Test Command Custom 4", "Menu-test.command04", "", "", "before", TEST_MENU_ITEM_ID, function (err) {
 832                        complete = true;
 833                        error = err;
 834                    });
 835                });
 836
 837                waitsFor(function () { return complete; });
 838
 839                runs(function () {
 840                    expect(error).toBe(0);
 841                });
 842
 843                // Verify item is found in the right position
 844                runs(function () {
 845                    complete = false;
 846                    parentId = null;
 847                    position = -1;
 848                    brackets.app.getMenuPosition("Menu-test.command04", function (err, parent, index) {
 849                        complete = true;
 850                        error = err;
 851                        parentId = parent;
 852                        position = index;
 853                    });
 854                });
 855
 856                waitsFor(function () { return complete; });
 857
 858                runs(function () {
 859                    expect(error).toBe(0);
 860                    expect(parentId).toBe(TEST_MENU_ID);
 861                    expect(position).toBe(0);
 862                });
 863
 864                // Verify item
 865                runs(function () {
 866                    complete = false;
 867                    brackets.app.getMenuTitle("Menu-test.command04", function (err, titleStr) {
 868                        complete = true;
 869                        error = err;
 870                        title = titleStr;
 871                    });
 872                });
 873
 874                waitsFor(function () { return complete; });
 875
 876                runs(function () {
 877                    expect(error).toBe(0);
 878                    expect(title).toBe("Brackets Test Command Custom 4");
 879                });
 880
 881                runs(function () {
 882                    complete = false;
 883                    brackets.app.removeMenuItem("Menu-test.command04", function (err) {
 884                        complete = true;
 885                    });
 886                });
 887                waitsFor(function () { return complete; });
 888            });
 889
 890            it("should add a menu item at the end when reference menu item doesn't exist", function () {
 891                error = 0;
 892                runs(function () {
 893                    complete = false;
 894                    brackets.app.addMenuItem(TEST_MENU_ID, "Brackets Test Command Custom 5", "Menu-test.command05", "", "", "before", "NONEXISTANT", function (err) {
 895                        complete = true;
 896                        error = err;
 897                    });
 898                });
 899
 900                waitsFor(function () { return complete; });
 901
 902                runs(function () {
 903                    expect(error).toBe(brackets.fs.ERR_NOT_FOUND);
 904                });
 905
 906                // Verify item is found in the right position
 907                runs(function () {
 908                    complete = false;
 909                    parentId = null;
 910                    position = -1;
 911                    brackets.app.getMenuPosition("Menu-test.command05", function (err, parent, index) {
 912                        complete = true;
 913                        error = err;
 914                        parentId = parent;
 915                        position = index;
 916                    });
 917                });
 918
 919                waitsFor(function () { return complete; });
 920
 921                runs(function () {
 922                    expect(error).toBe(0);
 923                    expect(parentId).toBe(TEST_MENU_ID);
 924                    expect(position).toBe(1);
 925                });
 926
 927                // Verify item
 928                runs(function () {
 929                    complete = false;
 930                    brackets.app.getMenuTitle("Menu-test.command05", function (err, titleStr) {
 931                        complete = true;
 932                        error = err;
 933                        title = titleStr;
 934                    });
 935                });
 936
 937                waitsFor(function () { return complete; });
 938
 939                runs(function () {
 940                    expect(error).toBe(0);
 941                    expect(title).toBe("Brackets Test Command Custom 5");
 942                });
 943
 944                runs(function () {
 945                    complete = false;
 946                    brackets.app.removeMenuItem("Menu-test.command05", function (err) {
 947                        complete = true;
 948                    });
 949                });
 950                waitsFor(function () { return complete; });
 951            });
 952
 953            it("should add menu items to beginning and end of menu section", function () {
 954                var complete,
 955                    error,
 956                    index,
 957                    parent;
 958
 959                // set up test menu and menu items
 960                var SECTION_MENU = "menuitem-sectiontest";
 961                runs(function () {
 962                    brackets.app.addMenu("Section Test", "menuitem-sectiontest", "", "", function (err) {});
 963                    brackets.app.addMenuItem(SECTION_MENU, "Command 10", "Menu-test.command10", "", "", "", "", function (err) {});
 964                    brackets.app.addMenuItem(SECTION_MENU, "Command 11", "Menu-test.command11", "", "", "", "", function (err) {});
 965                    brackets.app.addMenuItem(SECTION_MENU, "---", String(Date.now()), "", "", "", "", function (err) {});
 966                    brackets.app.addMenuItem(SECTION_MENU, "Command 12", "Menu-test.command12", "", "", "", "", function (err) {});
 967                    brackets.app.addMenuItem(SECTION_MENU, "Command 13", "Menu-test.command13", "", "", "", "", function (err) {});
 968                });
 969
 970                // Add new menu to END of menuSectionCmd10
 971                runs(function () {
 972                    complete = false;
 973                    error = 0;
 974                    brackets.app.addMenuItem(SECTION_MENU, "Command 14", "Menu-test.command14", "", "", "lastInSection", "Menu-test.command10", function (err) {
 975                        complete = true;
 976                        error = err;
 977                    });
 978                });
 979
 980                waitsFor(function () { return complete; });
 981
 982                runs(function () {
 983                    complete = false;
 984                    error = 0;
 985                    brackets.app.getMenuPosition("Menu-test.command14", function (err, par, idx) {
 986                        complete = true;
 987                        error = err;
 988                        parent = par;
 989                        index = idx;
 990                    });
 991                });
 992
 993                waitsFor(function () { return complete; });
 994
 995                runs(function () {
 996                    expect(error).toBe(0);
 997                    expect(index).toBe(2);
 998                });
 999
1000                // Add new menu to END of menuSectionCmd2
1001                runs(function () {
1002                    complete = false;
1003                    error = 0;
1004                    brackets.app.addMenuItem(SECTION_MENU, "Command 15", "Menu-test.command15", "", "", "lastInSection", "Menu-test.command13", function (err) {
1005                        complete = true;
1006                        error = err;
1007                    });
1008                });
1009
1010                waitsFor(function () { return complete; });
1011
1012                runs(function () {
1013                    complete = false;
1014                    error = 0;
1015                    brackets.app.getMenuPosition("Menu-test.command15", function (err, par, idx) {
1016                        complete = true;
1017                        error = err;
1018                        parent = par;
1019                        index = idx;
1020                    });
1021                });
1022
1023                waitsFor(function () { return complete; });
1024
1025                runs(function () {
1026                    expect(error).toBe(0);
1027                    expect(index).toBe(6);
1028                });
1029
1030                // Add new menu to BEGINNING of menuSectionCmd0
1031                runs(function () {
1032                    complete = false;
1033                    error = 0;
1034                    brackets.app.addMenuItem(SECTION_MENU, "Command 16", "Menu-test.command16", "", "", "firstInSection", "Menu-test.command11", function (err) {
1035                        complete = true;
1036                        error = err;
1037                    });
1038                });
1039
1040                waitsFor(function () { return complete; });
1041
1042                runs(function () {
1043                    complete = false;
1044                    error = 0;
1045                    brackets.app.getMenuPosition("Menu-test.command16", function (err, par, idx) {
1046                        complete = true;
1047                        error = err;
1048                        parent = par;
1049                        index = idx;
1050                    });
1051                });
1052
1053                waitsFor(function () { return complete; });
1054
1055                runs(function () {
1056                    expect(error).toBe(0);
1057                    expect(index).toBe(0);
1058                });
1059
1060                // Add new menu to BEGINNING of menuSectionCmd2
1061                runs(function () {
1062                    complete = false;
1063                    error = 0;
1064                    brackets.app.addMenuItem(SECTION_MENU, "Command 17", "Menu-test.command17", "", "", "firstInSection", "Menu-test.command12", function (err) {
1065                        complete = true;
1066                        error = err;
1067                    });
1068                });
1069
1070                waitsFor(function () { return complete; });
1071
1072                runs(function () {
1073                    complete = false;
1074                    error = 0;
1075                    brackets.app.getMenuPosition("Menu-test.command17", function (err, par, idx) {
1076                        complete = true;
1077                        error = err;
1078                        parent = par;
1079                        index = idx;
1080                    });
1081                });
1082
1083                waitsFor(function () { return complete; });
1084
1085                runs(function () {
1086                    expect(error).toBe(0);
1087                    expect(index).toBe(5);
1088                });
1089                runs(function () {
1090                    brackets.app.removeMenuItem("Menu-test.command10", function (err) {});
1091                    brackets.app.removeMenuItem("Menu-test.command11", function (err) {});
1092                    brackets.app.removeMenuItem("Menu-test.command12", function (err) {});
1093                    brackets.app.removeMenuItem("Menu-test.command13", function (err) {});
1094                    brackets.app.removeMenuItem("Menu-test.command14", function (err) {});
1095                    brackets.app.removeMenuItem("Menu-test.command15", function (err) {});
1096                    brackets.app.removeMenuItem("Menu-test.command16", function (err) {});
1097                    brackets.app.removeMenuItem("Menu-test.command17", function (err) {});
1098                    brackets.app.removeMenu(SECTION_MENU, function (err) {});
1099                });
1100            });
1101        });  // describe("addMenuItem (with reference)")
1102
1103        describe("removeMenu", function () {
1104            var complete = false,
1105                error = 0;
1106
1107            it("should remove a menu", function () {
1108                runs(function () {
1109                    brackets.app.addMenu(TEST_MENU_TITLE, TEST_MENU_ID, "", "", function (err) {
1110                        complete = true;
1111                        error = err;
1112                    });
1113                });
1114
1115                waitsFor(function () { return complete; });
1116
1117                runs(function () {
1118                    expect(error).toBe(0);
1119                });
1120
1121                runs(function () {
1122                    complete = false;
1123                    brackets.app.removeMenu(TEST_MENU_ID, function (err) {
1124                        complete = true;
1125                        error = err;
1126                    });
1127                });
1128
1129                waitsFor(function () { return complete; });
1130
1131                runs(function () {
1132                    expect(error).toBe(0);
1133                });
1134            });
1135
1136            it("should return an error if invalid parameters are passed", function () {
1137                complete = false;
1138                error = 0;
1139
1140                runs(function () {
1141                    brackets.app.removeMenu(42, function (err) {
1142                        complete = true;
1143                        error = err;
1144                    });
1145                });
1146
1147                waitsFor(function () { return complete; });
1148
1149                runs(function () {
1150                    expect(error).toBe(brackets.fs.ERR_INVALID_PARAMS);
1151                });
1152            });
1153
1154            it("should return an error if the menu can't be found", function () {
1155                complete = false;
1156                error = 0;
1157
1158                runs(function () {
1159                    brackets.app.removeMenu(TEST_MENU_ID, function (err) {
1160                        complete = true;
1161                        error = err;
1162                    });
1163                });
1164
1165                waitsFor(function () { return complete; });
1166
1167                runs(function () {
1168                    expect(error).toBe(brackets.fs.ERR_NOT_FOUND);
1169                });
1170            });
1171        });
1172
1173        describe("removeMenuItem", function () {
1174            var ITEM_ID = TEST_MENU_ITEM_ID + "1";
1175
1176            beforeEach(function () {
1177                var complete = false,
1178                    error = 0;
1179
1180                runs(function () {
1181                    brackets.app.addMenu(TEST_MENU_TITLE, TEST_MENU_ID, "", "", function (err) {
1182                        if (err) {
1183                            complete = true;
1184                            error = err;
1185                        } else {
1186                            brackets.app.addMenuItem(TEST_MENU_ID, TEST_MENU_ITEM, ITEM_ID, "", "", "", "", function (err) {
1187                                complete = true;
1188                                error = err;
1189                            });
1190                        }
1191                    });
1192                });
1193
1194                waitsFor(function () { return complete; });
1195
1196                runs(function () {
1197                    expect(error).toBe(0);
1198                });
1199            });
1200
1201            afterEach(function () {
1202                var complete = false,
1203                    error = 0;
1204
1205                runs(function () {
1206                    brackets.app.removeMenuItem(ITEM_ID, function (err) {
1207                        // Ignore the error from removeMenuItem(). The item may have
1208                        // already been removed by the test.
1209                        brackets.app.removeMenu(TEST_MENU_ID, function (err) {
1210                            complete = true;
1211                            error = err;
1212                        });
1213                    });
1214                });
1215
1216                waitsFor(function () { return complete; });
1217
1218                runs(function () {
1219                    expect(error).toBe(0);
1220                });
1221            });
1222
1223            it("should remove a menu item", function () {
1224                var complete = false,
1225                    error = 0;
1226
1227                runs(function () {
1228                    brackets.app.removeMenuItem(ITEM_ID, function (err) {
1229                        complete = true;
1230                        error = err;
1231                    });
1232                });
1233
1234                waitsFor(function () { return complete; }, "calling removeMenuItem");
1235
1236                runs(function () {
1237                    expect(error).toBe(0);
1238                });
1239
1240                // Make sure it's gone
1241                runs(function () {
1242                    complete = false;
1243                    brackets.app.getMenuTitle(ITEM_ID, function (err, titleStr) {
1244                        complete = true;
1245                        error = err;
1246                    });
1247                });
1248
1249                waitsFor(function () { return complete; }, "calling getMenuTitle");
1250
1251                runs(function () {
1252                    expect(error).toBe(brackets.fs.ERR_NOT_FOUND);
1253                });
1254            });
1255            it("should return an error if invalid parameters are passed", function () {
1256                var complete = false,
1257                    error = 0;
1258
1259                runs(function () {
1260                    brackets.app.removeMenuItem(42, function (err) {
1261                        complete = true;
1262                        error = err;
1263                    });
1264                });
1265
1266                waitsFor(function () { return complete; }, "calling removeMenuItem");
1267
1268                runs(function () {
1269                    expect(error).toBe(brackets.fs.ERR_INVALID_PARAMS);
1270                });
1271            });
1272            it("should return an error if the menu item can't be found", function () {
1273                var complete = false,
1274                    error = 0;
1275
1276                runs(function () {
1277                    brackets.app.removeMenuItem(ITEM_ID + "foo", function (err) {
1278                        complete = true;
1279                        error = err;
1280                    });
1281                });
1282
1283                waitsFor(function () { return complete; }, "calling removeMenuItem");
1284
1285                runs(function () {
1286                    expect(error).toBe(brackets.fs.ERR_NOT_FOUND);
1287                });
1288            });
1289        });
1290
1291        describe("getMenuItemState setMenuItemState", function () {
1292            var ITEM_ID = TEST_MENU_ITEM_ID + "2";
1293
1294            beforeEach(function () {
1295                var complete = false,
1296                    error = 0;
1297
1298                runs(function () {
1299                    brackets.app.addMenu(TEST_MENU_TITLE, TEST_MENU_ID, "", "", function (err) {
1300                        if (err) {
1301                            complete = true;
1302                            error = err;
1303                        } else {
1304                            brackets.app.addMenuItem(TEST_MENU_ID, TEST_MENU_ITEM, ITEM_ID, "", "", "", "", function (err) {
1305                                complete = true;
1306                                error = err;
1307                            });
1308                        }
1309                    });
1310                });
1311
1312                waitsFor(function () { return complete; });
1313
1314                runs(function () {
1315                    expect(error).toBe(0);
1316                });
1317            });
1318
1319            afterEach(function () {
1320                var complete = false,
1321                    error = 0;
1322
1323                runs(function () {
1324                    brackets.app.removeMenuItem(ITEM_ID, function (err) {
1325                        // Ignore errors from removeMenuItem() and always remove
1326                        // the menu too. This is cleanup time so it's okay if
1327                        // an error gets missed here.
1328                        brackets.app.removeMenu(TEST_MENU_ID, function (err) {
1329                            complete = true;
1330                            error = err;
1331                        });
1332                    });
1333                });
1334
1335                waitsFor(function () { return complete; });
1336
1337                runs(function () {
1338                    expect(error).toBe(0);
1339                });
1340            });
1341            it("should be able to set enabled state", function () {
1342                var complete = false,
1343                    enabled = false,
1344                    error = 0;
1345
1346                // Should start out enabled
1347                runs(function () {
1348                    brackets.app.getMenuItemState(ITEM_ID, function (err, bEnabled, bChecked) {
1349                        complete = true;
1350                        enabled = bEnabled;
1351                        error = err;
1352                    });
1353                });
1354
1355                waitsFor(function () { return complete; });
1356
1357                runs(function () {
1358                    expect(error).toBe(0);
1359//                    expect(enabled).toBe(true);
1360                });
1361
1362                // Enable it
1363                runs(function () {
1364                    complete = false;
1365                    brackets.app.setMenuItemState(ITEM_ID, false, false, function (err) {
1366                        complete = true;
1367                        error = err;
1368                    });
1369                });
1370
1371                waitsFor(function () { return complete; });
1372
1373                runs(function () {
1374                    expect(error).toBe(0);
1375                });
1376
1377                // Make sure it is enabled
1378                runs(function () {
1379                    complete = false;
1380                    brackets.app.getMenuItemState(ITEM_ID, function (err, bEnabled, bChecked) {
1381                        complete = true;
1382                        enabled = bEnabled;
1383                        error = err;
1384                    });
1385                });
1386
1387                wa

Large files files are truncated, but you can click here to view the full file