OSMD_Test.ts 11 KB

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