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