OSMD_Test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import chai = require("chai");
  2. import {Instrument} from "../../../src/MusicalScore/Instrument";
  3. import {Note} from "../../../src/MusicalScore/VoiceData/Note";
  4. import {OpenSheetMusicDisplay} from "../../../src/OpenSheetMusicDisplay/OpenSheetMusicDisplay";
  5. import {VoiceEntry} from "../../../src/MusicalScore/VoiceData/VoiceEntry";
  6. import {TestUtils} from "../../Util/TestUtils";
  7. describe("OpenSheetMusicDisplay Main Export", () => {
  8. let container1: HTMLElement;
  9. it("no container", (done: MochaDone) => {
  10. chai.expect(() => {
  11. return new OpenSheetMusicDisplay(undefined);
  12. }).to.throw(/container/);
  13. done();
  14. });
  15. it("container", (done: MochaDone) => {
  16. const div: HTMLElement = TestUtils.getDivElement(document);
  17. chai.expect(() => {
  18. return new OpenSheetMusicDisplay(div);
  19. }).to.not.throw(Error);
  20. done();
  21. });
  22. it("load MXL from string", (done: MochaDone) => {
  23. const mxl: string = TestUtils.getMXL("Mozart_Clarinet_Quintet_Excerpt.mxl");
  24. const div: HTMLElement = TestUtils.getDivElement(document);
  25. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  26. opensheetmusicdisplay.load(mxl).then(
  27. (_: {}) => {
  28. opensheetmusicdisplay.render();
  29. done();
  30. },
  31. done
  32. );
  33. });
  34. it("load invalid MXL from string", (done: MochaDone) => {
  35. const mxl: string = "\x50\x4b\x03\x04";
  36. const div: HTMLElement = TestUtils.getDivElement(document);
  37. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  38. opensheetmusicdisplay.load(mxl).then(
  39. (_: {}) => {
  40. done(new Error("Corrupted MXL appears to be loaded correctly"));
  41. },
  42. (exc: Error) => {
  43. if (exc.message.toLowerCase().match(/invalid/)) {
  44. done();
  45. } else {
  46. done(new Error("Unexpected error: " + exc.message));
  47. }
  48. }
  49. );
  50. });
  51. it("load XML string", (done: MochaDone) => {
  52. const score: Document = TestUtils.getScore("MuzioClementi_SonatinaOpus36No1_Part1.xml");
  53. const xml: string = new XMLSerializer().serializeToString(score);
  54. const div: HTMLElement = TestUtils.getDivElement(document);
  55. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  56. opensheetmusicdisplay.load(xml).then(
  57. (_: {}) => {
  58. opensheetmusicdisplay.render();
  59. done();
  60. },
  61. done
  62. );
  63. });
  64. it("load XML Document", (done: MochaDone) => {
  65. const score: Document = TestUtils.getScore("MuzioClementi_SonatinaOpus36No1_Part1.xml");
  66. const div: HTMLElement = TestUtils.getDivElement(document);
  67. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  68. opensheetmusicdisplay.load(score).then(
  69. (_: {}) => {
  70. opensheetmusicdisplay.render();
  71. done();
  72. },
  73. done
  74. );
  75. });
  76. it("load MXL Document by URL", (done: MochaDone) => {
  77. const url: string = "base/test/data/Mozart_Clarinet_Quintet_Excerpt.mxl";
  78. const div: HTMLElement = TestUtils.getDivElement(document);
  79. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  80. opensheetmusicdisplay.load(url).then(
  81. (_: {}) => {
  82. opensheetmusicdisplay.render();
  83. done();
  84. },
  85. done
  86. );
  87. });
  88. it("load something invalid by URL", (done: MochaDone) => {
  89. const url: string = "https://www.google.com";
  90. const div: HTMLElement = TestUtils.getDivElement(document);
  91. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  92. opensheetmusicdisplay.load(url).then(
  93. (_: {}) => {
  94. done(new Error("Invalid URL appears to be loaded correctly"));
  95. },
  96. (exc: Error) => {
  97. if (exc.message.toLowerCase().match(/opensheetmusicdisplay.*invalid/)) {
  98. done();
  99. } else {
  100. done(new Error("Unexpected error: " + exc.message));
  101. }
  102. }
  103. );
  104. }).timeout(5000);
  105. it("load invalid URL", (done: MochaDone) => {
  106. const url: string = "https://www.afjkhfjkauu2ui3z2uiu.com";
  107. const div: HTMLElement = TestUtils.getDivElement(document);
  108. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  109. opensheetmusicdisplay.load(url).then(
  110. (_: {}) => {
  111. done(new Error("Invalid URL appears to be loaded correctly"));
  112. },
  113. (exc: Error) => {
  114. if (exc.message.toLowerCase().match(/url/)) {
  115. done();
  116. } else {
  117. done(new Error("Unexpected error: " + exc.message));
  118. }
  119. }
  120. );
  121. }).timeout(5000);
  122. it("load invalid XML string", (done: MochaDone) => {
  123. const xml: string = "<?xml";
  124. const div: HTMLElement = TestUtils.getDivElement(document);
  125. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  126. opensheetmusicdisplay.load(xml).then(
  127. (_: {}) => {
  128. done(new Error("Corrupted XML appears to be loaded correctly"));
  129. },
  130. (exc: Error) => {
  131. if (exc.message.toLowerCase().match(/partwise/)) {
  132. done();
  133. } else {
  134. done(new Error("Unexpected error: " + exc.message));
  135. }
  136. }
  137. );
  138. });
  139. it("render without loading", (done: MochaDone) => {
  140. const div: HTMLElement = TestUtils.getDivElement(document);
  141. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  142. chai.expect(() => {
  143. return opensheetmusicdisplay.render();
  144. }).to.throw(/load/);
  145. done();
  146. });
  147. before((): void => {
  148. // Create the container for the "test width" test
  149. container1 = TestUtils.getDivElement(document);
  150. });
  151. after((): void => {
  152. // Destroy the container for the "test width" test
  153. document.body.removeChild(container1);
  154. });
  155. it("test width 500", (done: MochaDone) => {
  156. const div: HTMLElement = container1;
  157. div.style.width = "500px";
  158. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  159. const score: Document = TestUtils.getScore("MuzioClementi_SonatinaOpus36No1_Part1.xml");
  160. opensheetmusicdisplay.load(score).then(
  161. (_: {}) => {
  162. opensheetmusicdisplay.render();
  163. chai.expect(div.offsetWidth).to.equal(500);
  164. done();
  165. },
  166. done
  167. ).catch(done);
  168. });
  169. it("test width 200", (done: MochaDone) => {
  170. const div: HTMLElement = container1;
  171. div.style.width = "200px";
  172. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  173. const score: Document = TestUtils.getScore("MuzioClementi_SonatinaOpus36No1_Part1.xml");
  174. opensheetmusicdisplay.load(score).then(
  175. (_: {}) => {
  176. opensheetmusicdisplay.render();
  177. chai.expect(div.offsetWidth).to.equal(200);
  178. done();
  179. },
  180. done
  181. ).catch(done);
  182. });
  183. describe("cursor", () => {
  184. let opensheetmusicdisplay: OpenSheetMusicDisplay;
  185. beforeEach((done: MochaDone) => {
  186. const div: HTMLElement = container1;
  187. opensheetmusicdisplay = TestUtils.createOpenSheetMusicDisplay(div);
  188. const score: Document = TestUtils.getScore("MuzioClementi_SonatinaOpus36No1_Part1.xml");
  189. opensheetmusicdisplay.load(score).then(
  190. (_: {}) => {
  191. opensheetmusicdisplay.render();
  192. opensheetmusicdisplay.cursor.show();
  193. done();
  194. },
  195. done
  196. ).catch(done);
  197. });
  198. describe("get AllVoicesUnderCursor", () => {
  199. it("retreives all voices under cursor", () => {
  200. const voiceEntries: VoiceEntry[] = opensheetmusicdisplay.cursor.AllVoicesUnderCursor;
  201. chai.expect(voiceEntries.length).to.equal(2);
  202. });
  203. });
  204. describe("VoicesUnderCursor", () => {
  205. it("retreives voices for a specific instrument under cursor", () => {
  206. const voiceEntries: VoiceEntry[] = opensheetmusicdisplay.cursor.VoicesUnderCursor();
  207. chai.expect(voiceEntries.length).to.equal(2);
  208. });
  209. it("retreives all voices under cursor when instrument not specified", () => {
  210. const instrument: Instrument = opensheetmusicdisplay.Sheet.Instruments[1];
  211. const voiceEntries: VoiceEntry[] = opensheetmusicdisplay.cursor.VoicesUnderCursor(instrument);
  212. chai.expect(voiceEntries.length).to.equal(1);
  213. });
  214. });
  215. describe("NotesUnderCursor", () => {
  216. it("gets notes for a specific instrument under cursor", () => {
  217. const instrument: Instrument = opensheetmusicdisplay.Sheet.Instruments[0];
  218. const notes: Note[] = opensheetmusicdisplay.cursor.NotesUnderCursor(instrument);
  219. chai.expect(notes.length).to.equal(1);
  220. });
  221. it("gets all notes under cursor when instrument unspecified", () => {
  222. const notes: Note[] = opensheetmusicdisplay.cursor.NotesUnderCursor();
  223. chai.expect(notes.length).to.equal(2);
  224. });
  225. });
  226. });
  227. });