PageRenderTime 93ms CodeModel.GetById 2ms app.highlight 83ms RepoModel.GetById 2ms app.codeStats 0ms

/test/spec/NativeMenu-test.js

https://bitbucket.org/stephenabritton/brackets
JavaScript | 1627 lines | 1270 code | 277 blank | 80 comment | 9 complexity | 2cbdb94b819a92dbe725616f78916da8 MD5 | raw file

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

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

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