Переглянути джерело

test: add new (skipped) test for PlaybackManager.getSheetDuration() (use .only instead of .skip to test. can crash browser)

sschmidTU 1 рік тому
батько
коміт
44740bf7d4

+ 2 - 25
test/Common/FileIO/Xml_Test.ts

@@ -15,31 +15,8 @@ describe("XML interface", () => {
     const doc: Document = parser.parseFromString(xmlTestData, "text/xml");
     const documentElement: IXmlElement = new IXmlElement(doc.documentElement);
 
-
-    // Test all the following xml files:
-    const xmlTestset: string[] = [
-        "ActorPreludeSample.xml",
-        "Beethoven_AnDieFerneGeliebte.xml",
-        "CharlesGounod_Meditation.xml",
-        "Debussy_Mandoline.xml",
-        "Dichterliebe01.xml",
-        "JohannSebastianBach_Air.xml",
-        "JohannSebastianBach_PraeludiumInCDur_BWV846_1.xml",
-        "JosephHaydn_ConcertanteCello.xml",
-        "Mozart_AnChloe.xml",
-        "Mozart_DasVeilchen.xml",
-        "MuzioClementi_SonatinaOpus36No1_Part1.xml",
-        "MuzioClementi_SonatinaOpus36No1_Part2.xml",
-        "MuzioClementi_SonatinaOpus36No3_Part1.xml",
-        "MuzioClementi_SonatinaOpus36No3_Part2.xml",
-        "Saltarello.xml",
-        "ScottJoplin_EliteSyncopations.xml",
-        "ScottJoplin_The_Entertainer.xml",
-        "TelemannWV40.102_Sonate-Nr.1.1-Dolce.xml",
-        "TelemannWV40.102_Sonate-Nr.1.2-Allegro-F-Dur.xml",
-        "VariousChordTests.musicxml",
-    ];
-    for (const score of xmlTestset) {
+    // test all test files
+    for (const score of TestUtils.XmlTestSet) {
         testFile(score);
     }
 

+ 102 - 0
test/MusicalScore/Playback/PlaybackManager_Test.ts

@@ -0,0 +1,102 @@
+import {TestUtils} from "../../Util/TestUtils";
+import {OpenSheetMusicDisplay} from "../../../src/OpenSheetMusicDisplay/OpenSheetMusicDisplay";
+import { LinearTimingSource } from "../../../src/Playback/TimingSources/LinearTimingSource";
+import { PlaybackManager } from "../../../src/Playback/PlaybackManager";
+import { BasicAudioPlayer } from "../../../src/Playback/BasicAudioPlayer";
+
+describe("PlaybackManager", () => {
+    const div: HTMLElement = document.createElement("div");
+    const osmd: OpenSheetMusicDisplay =
+        TestUtils.createOpenSheetMusicDisplay(div);
+
+    // for (const score of TestUtils.XmlTestSet) {
+    //     testFile(score);
+    // }
+
+    // use it.only to test only this.
+    //   TODO if just using it() (all tests), this can still crash the browser somehow.
+    it.skip("getSheetDuration total", (done: Mocha.Done) => {
+        //let currentFileIndex: number = 0;
+        loadNextFile(0, done);
+    }).timeout(10000);
+
+    function loadNextFile(fileIndex: number, done: Mocha.Done): void {
+        if (fileIndex > TestUtils.XmlTestSet.length - 1) {
+            done();
+        }
+        const score: Document = TestUtils.getScore(TestUtils.XmlTestSet[fileIndex]);
+        try {
+            osmd.load(score).then(
+                (result) => {
+                const timingSource: LinearTimingSource = new LinearTimingSource();
+                timingSource.Settings = osmd.Sheet.SheetPlaybackSetting;
+                timingSource.logEnabled = false;
+                const playbackManager: PlaybackManager = new PlaybackManager(
+                    timingSource, undefined, new BasicAudioPlayer(), undefined);
+                osmd.PlaybackManager = playbackManager;
+                playbackManager.initialize(osmd.Sheet.MusicPartManager);
+                const timeWithoutRepeats: number = playbackManager.getSheetDurationInMs(false);
+                const timeWithRepeats: number = playbackManager.getSheetDurationInMsWithRepeats();
+                console.log("time with    repeats: " + timeWithRepeats.toFixed(2) + " for " + osmd.Sheet.Title.text);
+                console.log("time without repeats: " + timeWithoutRepeats.toFixed(2));
+                chai.expect(timeWithRepeats >= timeWithoutRepeats).to.equal(true);
+                loadNextFile(fileIndex + 1, done);
+            },
+            (err) => {
+                // couldn't read file
+                if (fileIndex < TestUtils.XmlTestSet.length - 1) {
+                    loadNextFile(fileIndex + 1, done);
+                } else {
+                    done();
+                    // not sure why this if/else block is necessary, but otherwise it infinite loops when a file couldn't be loaded.
+                    //   somehow the catch block doesn't seem to get executed.
+                }
+            });
+        } catch (err) {
+            console.log("error: " + err);
+            loadNextFile(fileIndex + 1, done);
+            // we need to catch the error and call the next file,
+            //   otherwise done() never gets called and the test hangs
+        }
+    }
+
+    // this variation of doing the test is similar to the loop in Xml_Test.ts,
+    //   but seems to crash the browser because the for loop calls too many tests at once that are too demanding.
+    //   (Xml_Test does the same, but it looks like the test is simple enough to not overload the browser)
+    // function testFile(scoreName: string): void {
+    //     it("getSheetDuration: " + scoreName, (done: Mocha.Done) => {
+    //         //let timingSource: LinearTimingSource; // somehow this will be undefined in load().then()
+    //         //let playbackManager: PlaybackManager;
+
+    //         if (false) {
+    //             initialize(osmd, undefined, undefined);
+    //         }
+    //         const score: Document = TestUtils.getScore(scoreName);
+    //         osmd.load(score).then(() => {
+    //             const timingSource: LinearTimingSource = new LinearTimingSource();
+    //             timingSource.Settings = osmd.Sheet.SheetPlaybackSetting;
+    //             timingSource.logEnabled = false;
+    //             const playbackManager = new PlaybackManager(timingSource, undefined, new BasicAudioPlayer(), undefined);
+    //             osmd.PlaybackManager = playbackManager;
+    //             playbackManager.initialize(osmd.Sheet.MusicPartManager);
+    //             const timeWithoutRepeats: number = playbackManager.getSheetDurationInMs(false);
+    //             const timeWithRepeats: number = playbackManager.getSheetDurationInMsWithRepeats();
+    //             console.log("time with    repeats: " + timeWithRepeats    + " for " + osmd.Sheet.Title.text);
+    //             console.log("time without repeats: " + timeWithoutRepeats);
+    //             chai.expect(timeWithRepeats >= timeWithoutRepeats).to.equal(true);
+    //             done();
+    //         });
+    //     }).timeout(10000);
+    // }
+
+    // function initialize(osmd, timingSource, playbackManager) {
+    //     timingSource = new LinearTimingSource();
+    //     playbackManager = new PlaybackManager(timingSource, undefined, new BasicAudioPlayer(), undefined);
+    //     // timingSource.reset();
+    //     // timingSource.pause();
+    //     //playbackManager.removeListener(osmd.cursor); // only necessary if no duplicate checks in addListener
+    //     // playbackManager.addListener(osmd.cursor);
+    //     playbackManager.reset();
+    //     osmd.PlaybackManager = playbackManager;
+    // }
+});

+ 24 - 0
test/Util/TestUtils.ts

@@ -42,4 +42,28 @@ export class TestUtils {
     public static createOpenSheetMusicDisplay(div: HTMLElement): OpenSheetMusicDisplay {
         return new OpenSheetMusicDisplay(div, {autoResize: false});
     }
+
+    // Test all the following xml files:
+    public static XmlTestSet: string[] = [
+        "ActorPreludeSample.xml",
+        "Beethoven_AnDieFerneGeliebte.xml",
+        "CharlesGounod_Meditation.xml",
+        "Debussy_Mandoline.xml",
+        "Dichterliebe01.xml",
+        "JohannSebastianBach_Air.xml",
+        "JohannSebastianBach_PraeludiumInCDur_BWV846_1.xml",
+        "JosephHaydn_ConcertanteCello.xml",
+        "Mozart_AnChloe.xml",
+        "Mozart_DasVeilchen.xml",
+        "MuzioClementi_SonatinaOpus36No1_Part1.xml",
+        "MuzioClementi_SonatinaOpus36No1_Part2.xml",
+        "MuzioClementi_SonatinaOpus36No3_Part1.xml",
+        "MuzioClementi_SonatinaOpus36No3_Part2.xml",
+        "Saltarello.xml",
+        "ScottJoplin_EliteSyncopations.xml",
+        "ScottJoplin_The_Entertainer.xml",
+        "TelemannWV40.102_Sonate-Nr.1.1-Dolce.xml",
+        "TelemannWV40.102_Sonate-Nr.1.2-Allegro-F-Dur.xml",
+        //"VariousChordTests.musicxml", // doesn't exist anymore
+    ];
 }