Skip to content

Commit

Permalink
Add basic play-out tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tvanlaerhoven committed Oct 23, 2024
1 parent 2aec7a0 commit a1c61a1
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 31 deletions.
77 changes: 64 additions & 13 deletions e2e/src/tests/Basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,85 @@ import mp4 from '../res/mp4.json';
import { getTestPlayer } from '../components/TestableTHEOplayerView';
import { expect, waitForPlayerEventType, waitForPlayerEventTypes } from '../utils/Actions';

function testBasicPlayout(spec: TestScope, title: string, source: SourceDescription, autoplay: boolean) {
const SEEK_THRESHOLD = 1e-1;

async function preparePlayerWithSource(source: SourceDescription, autoplay: boolean) {
const player = await getTestPlayer();
const eventsPromise = waitForPlayerEventType(player, PlayerEventType.SOURCE_CHANGE);
const eventsPromiseAutoPlay = waitForPlayerEventTypes(player, [PlayerEventType.SOURCE_CHANGE, PlayerEventType.PLAY, PlayerEventType.PLAYING]);

// Start autoplay
player.autoplay = autoplay;
player.source = source;

// Wait for `sourcechange`, `play` and `playing` events.
if (autoplay) {
await eventsPromiseAutoPlay;
} else {
await eventsPromise;
}
return player;
}

function testBasicPlayout(spec: TestScope, title: string, source: SourceDescription) {
spec.describe(title, function () {
spec.it('dispatches sourcechange, play and playing in order', async function () {
const player = await getTestPlayer();
const eventsPromise = waitForPlayerEventTypes(player, [PlayerEventType.SOURCE_CHANGE, PlayerEventType.PLAY, PlayerEventType.PLAYING]);
spec.it('dispatches sourcechange event on setting a source without autoplay', async function () {
// Set source and wait for playback
const player = await preparePlayerWithSource(source, false);

// Start autoplay
player.autoplay = autoplay;
player.source = source;
// Still playing
expect(player.paused).toBeTruthy();
});

// Expect events.
await eventsPromise;
spec.it('dispatches sourcechange, play and playing events in order on setting a source with autoplay', async function () {
// Set source and wait for playback
const player = await preparePlayerWithSource(source, true);

// Still playing
expect(player.paused).toBeFalsy();
});

spec.it('dispatches a seeked event after seeking', async function () {
// Set source and wait for playback
const player = await preparePlayerWithSource(source, true);

// Seek
const seekPromise = waitForPlayerEventType(player, PlayerEventType.SEEKED);
player.currentTime = 10e3;
const seekTime = 10e3;
player.currentTime = seekTime;

// Wait for `seeked` event.
await seekPromise;

// Expect currentTime to be updated.
expect(player.currentTime).toBeSmallerThanOrEqual(seekTime + SEEK_THRESHOLD);
});

spec.it('dispatches paused, play and playing events after pausing & resuming playback', async function () {
// Set source and wait for playback
const player = await preparePlayerWithSource(source, true);

// Pause play-out.
const pausePromise = waitForPlayerEventType(player, PlayerEventType.PAUSE);
player.pause();

// Wait for 'paused' event.
await pausePromise;

// Resume play-out.
const playPromises = waitForPlayerEventTypes(player, [PlayerEventType.PLAY, PlayerEventType.PLAYING]);
player.play();

// Wait for 'play' and 'playing' events.
await playPromises;
});
});
}

export default function (spec: TestScope) {
if (Platform.OS === 'android' || Platform.OS === 'web') {
testBasicPlayout(spec, 'Set DASH source and auto-play', dash[0], true);
testBasicPlayout(spec, 'Set DASH source and auto-play', dash[0]);
}
testBasicPlayout(spec, 'Set HLS source and auto-play', hls[0], true);
testBasicPlayout(spec, 'Set mp4 source and auto-play', mp4[0], true);
testBasicPlayout(spec, 'Set HLS source and auto-play', hls[0]);
testBasicPlayout(spec, 'Set mp4 source and auto-play', mp4[0]);
}
60 changes: 42 additions & 18 deletions e2e/src/utils/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const waitForPlayerEvents = async <EType extends Event<PlayerEventType>>(
}
const expectedEvent = eventMap[0].event;
events.push(receivedEvent);
console.debug('[waitForPlayerEvents]', `Received event '${JSON.stringify(receivedEvent.type)}' - waiting for ${expectedEvent.type}`);
console.debug('[waitForPlayerEvents]', `Received event ${JSON.stringify(receivedEvent.type)} - waiting for ${expectedEvent.type}`);
const index = eventMap.findIndex((e) => propsMatch(e.event, receivedEvent));
const isExpected = index <= 0;

Expand Down Expand Up @@ -108,34 +108,58 @@ const withTimeOut = (promise: Promise<any>, timeout: number): Promise<any> => {
});
};

export function expect(actual: any) {
export function expect(actual: any, desc?: string) {
const descPrefix = desc ? `${desc}: ` : '';

const logPass = (msg: string) => console.log(`${descPrefix}${msg} ✅`);
const throwErr = (msg: string) => {
throw new Error(`${descPrefix}${msg} ❌`);
};

return {
toBe(expected: any) {
if (actual !== expected) {
throw new Error(`Expected ${actual} to be ${expected} ❌`);
}
console.log(`Passed: ${actual} == ${expected} ✅`);
if (actual === expected) logPass(`${actual} == ${expected}`);
else throwErr(`Expected ${actual} to be ${expected}`);
},

toNotBe(expected: any) {
if (actual !== expected) logPass(`${actual} != ${expected}`);
else throwErr(`Expected ${actual} not to be ${expected}`);
},

toEqual(expected: any) {
if (JSON.stringify(actual) !== JSON.stringify(expected)) {
throw new Error(`Expected ${JSON.stringify(actual)} to equal ${JSON.stringify(expected)} ❌`);
}
console.log(`Passed: ${JSON.stringify(actual)} !== ${JSON.stringify(expected)} ✅`);
if (JSON.stringify(actual) === JSON.stringify(expected)) logPass(`Expected ${actual} to equal ${expected}`);
else throwErr(`Expected ${actual} to equal ${expected}`);
},

toBeGreaterThan(expected: number) {
if (actual > expected) logPass(`${actual} > ${expected}`);
else throwErr(`Expected ${actual} to be greater than ${expected}`);
},

toBeGreaterThanOrEqual(expected: number) {
if (actual >= expected) logPass(`${actual} >= ${expected}`);
else throwErr(`Expected ${actual} to be greater than or equal to ${expected}`);
},

toBeSmallerThan(expected: number) {
if (actual < expected) logPass(`${actual} < ${expected}`);
else throwErr(`Expected ${actual} to be smaller than ${expected}`);
},

toBeSmallerThanOrEqual(expected: number) {
if (actual <= expected) logPass(`${actual} <= ${expected}`);
else throwErr(`Expected ${actual} to be smaller than or equal to ${expected}`);
},

toBeTruthy() {
if (!actual) {
throw new Error(`Expected ${actual} to be truthy ❌`);
}
console.log(`Passed: ${actual} is truthy ✅`);
if (actual) logPass(`${actual} is truthy`);
else throwErr(`Expected ${actual} to be truthy`);
},

toBeFalsy() {
if (actual) {
throw new Error(`Expected ${actual} to be falsy ❌`);
}
console.log(`Passed: ${actual} is falsy ✅`);
if (!actual) logPass(`${actual} is falsy`);
else throwErr(`Expected ${actual} to be falsy`);
},
};
}
Expand Down

0 comments on commit a1c61a1

Please sign in to comment.