OSMD_Test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import chai = require("chai");
  2. import { OpenSheetMusicDisplay } from "../../../src/OpenSheetMusicDisplay/OpenSheetMusicDisplay";
  3. import { TestUtils } from "../../Util/TestUtils";
  4. import { VoiceEntry, Instrument, Note, Staff, Voice, GraphicalStaffEntry, GraphicalNote,
  5. Fraction, Pitch, AccidentalEnum, DrawingParametersEnum, IOSMDOptions } from "../../../src";
  6. describe("OpenSheetMusicDisplay Main Export", () => {
  7. let container1: HTMLElement;
  8. it("no container", (done: MochaDone) => {
  9. chai.expect(() => {
  10. return new OpenSheetMusicDisplay(undefined);
  11. }).to.throw(/container/);
  12. done();
  13. });
  14. it("container", (done: MochaDone) => {
  15. const div: HTMLElement = TestUtils.getDivElement(document);
  16. chai.expect(() => {
  17. return new OpenSheetMusicDisplay(div);
  18. }).to.not.throw(Error);
  19. done();
  20. });
  21. it("multiple instances", () => {
  22. const musicSheetFragmentContainer: HTMLElement = TestUtils.getDivElement(document);
  23. const fullMusicSheetContainer: HTMLElement = TestUtils.getDivElement(document);
  24. const musicSheetFragmentOptions: IOSMDOptions = {
  25. drawComposer: false,
  26. drawCredits: false,
  27. drawFingerings: false,
  28. drawHiddenNotes: false,
  29. drawLyricist: false,
  30. drawPartAbbreviations: false,
  31. drawPartNames: false,
  32. drawSubtitle: false,
  33. drawTitle: false,
  34. drawUpToMeasureNumber: 1,
  35. drawingParameters: DrawingParametersEnum.compact
  36. };
  37. const fullMusicSheetOptions: IOSMDOptions = {
  38. drawUpToMeasureNumber: 10
  39. };
  40. const musicSheetFragment: OpenSheetMusicDisplay = new OpenSheetMusicDisplay(
  41. musicSheetFragmentContainer,
  42. musicSheetFragmentOptions
  43. );
  44. const fullMusicSheet: OpenSheetMusicDisplay = new OpenSheetMusicDisplay(fullMusicSheetContainer, fullMusicSheetOptions);
  45. const musicSheet: Document = TestUtils.getScore("MuzioClementi_SonatinaOpus36No1_Part1.xml");
  46. const musicSheetXML: string = new XMLSerializer().serializeToString(musicSheet);
  47. return musicSheetFragment.load(musicSheetXML)
  48. .then(() => {
  49. musicSheetFragment.render();
  50. return fullMusicSheet.load(musicSheetXML);
  51. })
  52. .then(() => {
  53. fullMusicSheet.render();
  54. // Verify that the music sheet fragment has its options set correctly.
  55. chai.expect(musicSheetFragment.Sheet.Rules.RenderComposer).to.equal(musicSheetFragmentOptions.drawComposer);
  56. chai.expect(musicSheetFragment.Sheet.Rules.RenderFingerings).to.equal(musicSheetFragmentOptions.drawFingerings);
  57. chai.expect(musicSheetFragment.Sheet.Rules.RenderLyricist).to.equal(musicSheetFragmentOptions.drawLyricist);
  58. chai.expect(musicSheetFragment.Sheet.Rules.RenderPartAbbreviations).to.equal(musicSheetFragmentOptions.drawPartAbbreviations);
  59. chai.expect(musicSheetFragment.Sheet.Rules.RenderPartNames).to.equal(musicSheetFragmentOptions.drawPartNames);
  60. chai.expect(musicSheetFragment.Sheet.Rules.RenderSubtitle).to.equal(musicSheetFragmentOptions.drawSubtitle);
  61. chai.expect(musicSheetFragment.Sheet.Rules.RenderTitle).to.equal(musicSheetFragmentOptions.drawTitle);
  62. chai.expect(musicSheetFragment.Sheet.Rules.MaxMeasureToDrawIndex).to.equal(musicSheetFragmentOptions.drawUpToMeasureNumber - 1);
  63. // Verify that the full music sheet has its options set correctly.
  64. chai.expect(fullMusicSheet.Sheet.Rules.RenderComposer).to.not.equal(musicSheetFragmentOptions.drawComposer);
  65. chai.expect(fullMusicSheet.Sheet.Rules.RenderFingerings).to.not.equal(musicSheetFragmentOptions.drawFingerings);
  66. chai.expect(fullMusicSheet.Sheet.Rules.RenderLyricist).to.not.equal(musicSheetFragmentOptions.drawLyricist);
  67. chai.expect(fullMusicSheet.Sheet.Rules.RenderPartAbbreviations).to.not.equal(musicSheetFragmentOptions.drawPartAbbreviations);
  68. chai.expect(fullMusicSheet.Sheet.Rules.RenderPartNames).to.not.equal(musicSheetFragmentOptions.drawPartNames);
  69. chai.expect(fullMusicSheet.Sheet.Rules.RenderSubtitle).to.not.equal(musicSheetFragmentOptions.drawSubtitle);
  70. chai.expect(fullMusicSheet.Sheet.Rules.RenderTitle).to.not.equal(musicSheetFragmentOptions.drawTitle);
  71. chai.expect(fullMusicSheet.Sheet.Rules.MaxMeasureToDrawIndex).to.equal(fullMusicSheetOptions.drawUpToMeasureNumber - 1);
  72. });
  73. });
  74. it("load MXL from string", (done: MochaDone) => {
  75. const mxl: string = TestUtils.getMXL("Mozart_Clarinet_Quintet_Excerpt.mxl");
  76. const div: HTMLElement = TestUtils.getDivElement(document);
  77. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  78. opensheetmusicdisplay.load(mxl).then(
  79. (_: {}) => {
  80. opensheetmusicdisplay.render();
  81. done();
  82. },
  83. done
  84. );
  85. });
  86. it("load invalid MXL from string", (done: MochaDone) => {
  87. const mxl: string = "\x50\x4b\x03\x04";
  88. const div: HTMLElement = TestUtils.getDivElement(document);
  89. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  90. opensheetmusicdisplay.load(mxl).then(
  91. (_: {}) => {
  92. done(new Error("Corrupted MXL appears to be loaded correctly"));
  93. },
  94. (exc: Error) => {
  95. if (exc.message.toLowerCase().match(/invalid/)) {
  96. done();
  97. } else {
  98. done(new Error("Unexpected error: " + exc.message));
  99. }
  100. }
  101. );
  102. });
  103. it("load XML string", (done: MochaDone) => {
  104. const score: Document = TestUtils.getScore("MuzioClementi_SonatinaOpus36No1_Part1.xml");
  105. const xml: string = new XMLSerializer().serializeToString(score);
  106. const div: HTMLElement = TestUtils.getDivElement(document);
  107. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  108. opensheetmusicdisplay.load(xml).then(
  109. (_: {}) => {
  110. opensheetmusicdisplay.render();
  111. done();
  112. },
  113. done
  114. );
  115. });
  116. it("load XML Document", (done: MochaDone) => {
  117. const score: Document = TestUtils.getScore("MuzioClementi_SonatinaOpus36No1_Part1.xml");
  118. const div: HTMLElement = TestUtils.getDivElement(document);
  119. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  120. opensheetmusicdisplay.load(score).then(
  121. (_: {}) => {
  122. opensheetmusicdisplay.render();
  123. done();
  124. },
  125. done
  126. );
  127. });
  128. it("Timeout from server", (done: MochaDone) => {
  129. const score: string = "https://httpstat.us/408";
  130. const div: HTMLElement = TestUtils.getDivElement(document);
  131. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  132. opensheetmusicdisplay.load(score).then(
  133. (_: {}) => {
  134. done(new Error("Unexpected response from server"));
  135. },
  136. (exc: Error) => {
  137. done();
  138. }
  139. );
  140. });
  141. it("load MXL Document by URL", (done: MochaDone) => {
  142. const url: string = "base/test/data/Mozart_Clarinet_Quintet_Excerpt.mxl";
  143. const div: HTMLElement = TestUtils.getDivElement(document);
  144. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  145. opensheetmusicdisplay.load(url).then(
  146. (_: {}) => {
  147. opensheetmusicdisplay.render();
  148. done();
  149. },
  150. done
  151. );
  152. });
  153. it("load something invalid by URL", (done: MochaDone) => {
  154. const url: string = "https://www.google.com";
  155. const div: HTMLElement = TestUtils.getDivElement(document);
  156. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  157. opensheetmusicdisplay.load(url).then(
  158. (_: {}) => {
  159. done(new Error("Invalid URL appears to be loaded correctly"));
  160. },
  161. (exc: Error) => {
  162. if (exc.message.toLowerCase().match(/opensheetmusicdisplay.*invalid/)) {
  163. done();
  164. } else {
  165. done(new Error("Unexpected error: " + exc.message));
  166. }
  167. }
  168. );
  169. }).timeout(5000);
  170. it("load invalid URL", (done: MochaDone) => {
  171. const url: string = "https://www.afjkhfjkauu2ui3z2uiu.com";
  172. const div: HTMLElement = TestUtils.getDivElement(document);
  173. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  174. opensheetmusicdisplay.load(url).then(
  175. (_: {}) => {
  176. done(new Error("Invalid URL appears to be loaded correctly"));
  177. },
  178. (exc: Error) => {
  179. if (exc.message.toLowerCase().match(/url/)) {
  180. done();
  181. } else {
  182. done(new Error("Unexpected error: " + exc.message));
  183. }
  184. }
  185. );
  186. }).timeout(5000);
  187. it("load invalid XML string", (done: MochaDone) => {
  188. const xml: string = "<?xml";
  189. const div: HTMLElement = TestUtils.getDivElement(document);
  190. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  191. opensheetmusicdisplay.load(xml).then(
  192. (_: {}) => {
  193. done(new Error("Corrupted XML appears to be loaded correctly"));
  194. },
  195. (exc: Error) => {
  196. if (exc.message.toLowerCase().match(/partwise/)) {
  197. done();
  198. } else {
  199. done(new Error("Unexpected error: " + exc.message));
  200. }
  201. }
  202. );
  203. });
  204. it("render without loading", (done: MochaDone) => {
  205. const div: HTMLElement = TestUtils.getDivElement(document);
  206. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  207. chai.expect(() => {
  208. return opensheetmusicdisplay.render();
  209. }).to.throw(/load/);
  210. done();
  211. });
  212. before((): void => {
  213. // Create the container for the "test width" test
  214. container1 = TestUtils.getDivElement(document);
  215. });
  216. after((): void => {
  217. // Destroy the container for the "test width" test
  218. document.body.removeChild(container1);
  219. });
  220. it("test width 500", (done: MochaDone) => {
  221. const div: HTMLElement = container1;
  222. div.style.width = "500px";
  223. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  224. const score: Document = TestUtils.getScore("MuzioClementi_SonatinaOpus36No1_Part1.xml");
  225. opensheetmusicdisplay.load(score).then(
  226. (_: {}) => {
  227. opensheetmusicdisplay.render();
  228. chai.expect(div.offsetWidth).to.equal(500);
  229. done();
  230. },
  231. done
  232. ).catch(done);
  233. });
  234. it("test width 200", (done: MochaDone) => {
  235. const div: HTMLElement = container1;
  236. div.style.width = "200px";
  237. const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
  238. const score: Document = TestUtils.getScore("MuzioClementi_SonatinaOpus36No1_Part1.xml");
  239. opensheetmusicdisplay.load(score).then(
  240. (_: {}) => {
  241. opensheetmusicdisplay.render();
  242. chai.expect(div.offsetWidth).to.equal(200);
  243. done();
  244. },
  245. done
  246. ).catch(done);
  247. });
  248. describe("cursor with hidden instrument", () => {
  249. let osmd: OpenSheetMusicDisplay;
  250. beforeEach(() => {
  251. const div: HTMLElement = TestUtils.getDivElement(document);
  252. osmd = TestUtils.createOpenSheetMusicDisplay(div);
  253. const score: Document =
  254. TestUtils.getScore("MuzioClementi_SonatinaOpus36No1_Part1.xml");
  255. return osmd.load(score)
  256. .then(() => {
  257. osmd.render();
  258. });
  259. });
  260. it("should move cursor after instrument is hidden", () => {
  261. osmd.Sheet.Instruments[1].Visible = false;
  262. osmd.render();
  263. osmd.cursor.show();
  264. for (let i: number = 0; i < 100; i++) {
  265. osmd.cursor.next();
  266. }
  267. });
  268. });
  269. describe("cursor", () => {
  270. let opensheetmusicdisplay: OpenSheetMusicDisplay;
  271. beforeEach((done: MochaDone) => {
  272. const div: HTMLElement = container1;
  273. opensheetmusicdisplay = TestUtils.createOpenSheetMusicDisplay(div);
  274. const score: Document = TestUtils.getScore("MuzioClementi_SonatinaOpus36No1_Part1.xml");
  275. opensheetmusicdisplay.load(score).then(
  276. (_: {}) => {
  277. opensheetmusicdisplay.render();
  278. opensheetmusicdisplay.cursor.show();
  279. done();
  280. },
  281. done
  282. ).catch(done);
  283. });
  284. describe("get AllVoicesUnderCursor", () => {
  285. it("retrieves all voices under cursor", () => {
  286. const voiceEntries: VoiceEntry[] = opensheetmusicdisplay.cursor.VoicesUnderCursor();
  287. chai.expect(voiceEntries.length).to.equal(2);
  288. });
  289. });
  290. describe("VoicesUnderCursor", () => {
  291. it("retrieves voices for a specific instrument under cursor", () => {
  292. const voiceEntries: VoiceEntry[] = opensheetmusicdisplay.cursor.VoicesUnderCursor();
  293. chai.expect(voiceEntries.length).to.equal(2);
  294. });
  295. it("retrieves all voices under cursor when instrument not specified", () => {
  296. const instrument: Instrument = opensheetmusicdisplay.Sheet.Instruments[1];
  297. const voiceEntries: VoiceEntry[] = opensheetmusicdisplay.cursor.VoicesUnderCursor(instrument);
  298. chai.expect(voiceEntries.length).to.equal(1);
  299. });
  300. });
  301. describe("NotesUnderCursor", () => {
  302. it("gets notes for a specific instrument under cursor", () => {
  303. const instrument: Instrument = opensheetmusicdisplay.Sheet.Instruments[0];
  304. const notes: Note[] = opensheetmusicdisplay.cursor.NotesUnderCursor(instrument);
  305. chai.expect(notes.length).to.equal(1);
  306. });
  307. it("gets all notes under cursor when instrument unspecified", () => {
  308. const notes: Note[] = opensheetmusicdisplay.cursor.NotesUnderCursor();
  309. chai.expect(notes.length).to.equal(2);
  310. });
  311. });
  312. describe("updateGraphic", () => {
  313. it("updates the graphical sheet with mutations on the music sheet", () => {
  314. const staff: Staff = opensheetmusicdisplay.Sheet.Staves[0];
  315. const voice: Voice = staff.Voices[0];
  316. const voiceEntry: VoiceEntry = voice.VoiceEntries[0];
  317. const numNotesBefore: number = voiceEntry.Notes.length;
  318. // Validate current state
  319. {
  320. const graphicalStaffEntry: GraphicalStaffEntry = opensheetmusicdisplay.GraphicSheet.getStaffEntry(0);
  321. const graphicalNotes: GraphicalNote[] = graphicalStaffEntry.findVoiceEntryGraphicalNotes(voiceEntry);
  322. chai.expect(graphicalNotes.length).to.equal(numNotesBefore);
  323. }
  324. const newNote: Note = new Note(
  325. voiceEntry,
  326. voiceEntry.ParentSourceStaffEntry,
  327. new Fraction(1),
  328. new Pitch(11, 2, AccidentalEnum.NATURAL),
  329. voiceEntry.ParentSourceStaffEntry.VerticalContainerParent.ParentMeasure);
  330. // note: if the pitch is such that the voice entry frequencies aren't ordered correctly,
  331. // Vexflow will complain about unsorted pitches. see below
  332. voiceEntry.Notes.push(newNote);
  333. // we could do something like voiceEntry.sort() here to prevent the Vexflow warning about unsorted pitches,
  334. // but for now sort() only exists on GraphicalVoiceEntry.
  335. opensheetmusicdisplay.updateGraphic();
  336. {
  337. const graphicalStaffEntry: GraphicalStaffEntry = opensheetmusicdisplay.GraphicSheet.getStaffEntry(0);
  338. const graphicalNotes: GraphicalNote[] = graphicalStaffEntry.findVoiceEntryGraphicalNotes(voiceEntry);
  339. chai.expect(graphicalNotes.length).to.equal(numNotesBefore + 1);
  340. }
  341. });
  342. });
  343. });
  344. });