-
Notifications
You must be signed in to change notification settings - Fork 13
/
PlayerTesting.ts
240 lines (231 loc) · 7.32 KB
/
PlayerTesting.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import {
Player,
PlayerConfig,
Event,
SourceConfig,
ReadyEvent,
TimeChangedEvent,
} from 'bitmovin-player-react-native';
import { EventType } from './EventType';
import PlayerTestWorld from './PlayerTestWorld';
import {
SingleEventExpectation,
MultipleEventsExpectation,
} from './expectations';
/**
* Starts a player test with the given configuration and test function.
* @param config The player configuration to use for the test. Pass `{}` to use the default configuration.
* @param fn The test function to run.
* @returns A promise that resolves when the test is finished.
* @throws An error if the test fails.
* @example
* ```typescript
* await startPlayerTest({}, async () => {
* // ...
* });
* ```
* @see {@link PlayerConfig}
*/
export const startPlayerTest = async (
config: PlayerConfig,
fn: () => Promise<void>
): Promise<void> => {
return await PlayerTestWorld.shared.startPlayerTest(config, fn);
};
/**
* Calls the given function with the player instance.
* @param fn The function to call.
* @returns A promise that resolves when the function is finished.
* @example
* ```typescript
* await callPlayer(async (player) => {
* // ...
* });
* ```
* @see {@link Player}
*/
export const callPlayer = async <T>(
fn: (player: Player) => Promise<T>
): Promise<T> => {
return await PlayerTestWorld.shared.callPlayer(fn);
};
/**
* Expects the given event to occur.
* @param expectationConvertible The event to expect.
* @param timeoutSeconds The number of seconds to wait for the event to occur.
* @returns A promise that resolves when the function is finished.
* @throws An error if the event does not occur.
* @example
* ```typescript
* await callPlayerAndExpectEvent(async (player) => {
* // ...
* }, EventType.Play);
* ```
* @see {@link Player}
* @see {@link EventType}
*/
export const expectEvent = async <T extends Event>(
expectationConvertible: SingleEventExpectation | EventType,
timeoutSeconds: number = 10
): Promise<T> => {
return await PlayerTestWorld.shared.expectEvent(
expectationConvertible,
timeoutSeconds
);
};
/**
* Expects the given events to occur.
* @param expectationsConvertible The events to expect.
* @param timeoutSeconds The number of seconds to wait for the events to occur.
* @returns A promise that resolves when the function is finished.
* @throws An error if the events do not occur.
* @example
* ```typescript
* await callPlayerAndExpectEvents(async (player) => {
* // ...
* }, [EventType.Play, EventType.Playing]);
* ```
*
* ```typescript
* await callPlayerAndExpectEvents(async (player) => {
* // ...
* }, EventSequence(EventType.Play, EventType.Playing));
* ```
* @see {@link Player}
* @see {@link EventType}
*/
export const expectEvents = async (
expectationsConvertible: MultipleEventsExpectation | EventType[],
timeoutSeconds: number = 10
): Promise<Event[]> => {
return await PlayerTestWorld.shared.expectEvents(
expectationsConvertible,
timeoutSeconds
);
};
/**
* Starts listening for the specified `SingleEventExpectation` before executing the passed `fn` function.
* This is the race-condition-safe version of calling `callPlayer` and `expectEvent` after that.
* Useful when events are directly tied to calls in the `fn` function block and therefore synchronously emitted.
* @param fn The function to call.
* @param expectationConvertible The event to expect.
* @param timeoutSeconds The number of seconds to wait for the event to occur.
* @returns A promise that resolves when the function is finished.
* @throws An error if the event does not occur.
* @example
* ```typescript
* await callPlayerAndExpectEvent(async (player) => {
* // ...
* }, EventType.Play);
* ```
* @see {@link Player}
* @see {@link EventType}
*/
export const callPlayerAndExpectEvent = async <E extends Event>(
fn: (player: Player) => void,
expectationConvertible: SingleEventExpectation | EventType,
timeoutSeconds: number = 10
): Promise<E> => {
return await PlayerTestWorld.shared.callPlayerAndExpectEvent(
fn,
expectationConvertible,
timeoutSeconds
);
};
/**
* Starts listening for the specified `MultipleEventExpectation` before executing the passed `fn` function.
* This is the race-condition-safe version of calling `callPlayer` and `expectEvents` after that.
* Useful when events are directly tied to calls in the `fn` function block and therefore synchronously emitted.
* @param fn The function to call.
* @param expectationsConvertible The events to expect.
* @param timeoutSeconds The number of seconds to wait for the events to occur.
* @returns A promise that resolves when the function is finished.
* @throws An error if the events do not occur.
* @example
* ```typescript
* await callPlayerAndExpectEvents(async (player) => {
* // ...
* }, [EventType.Play, EventType.Playing]);
* ```
*
* ```typescript
* await callPlayerAndExpectEvents(async (player) => {
* // ...
* }, EventSequence(EventType.Play, EventType.Playing));
* ```
* @see {@link Player}
* @see {@link EventType}
*/
export const callPlayerAndExpectEvents = async (
fn: (player: Player) => void,
expectationsConvertible: MultipleEventsExpectation | EventType[],
timeoutSeconds: number = 10
): Promise<Event[]> => {
return await PlayerTestWorld.shared.callPlayerAndExpectEvents(
fn,
expectationsConvertible,
timeoutSeconds
);
};
/**
* Loads the given source configuration and expects `ReadyEvent` to occur.
* @param sourceConfig The source configuration to load.
* @param timeoutSeconds The number of seconds to wait for the event to occur.
* @returns A promise that resolves when the function is finished.
* @throws An error if the event does not occur.
* @example
* ```typescript
* await loadSourceConfig({
* url: 'https://bitmovin-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8',
* type: SourceType.HLS,
* });
* ```
* @see {@link SourceConfig}
* @see {@link EventType}
* @see {@link ReadyEvent}
*/
export const loadSourceConfig = async (
sourceConfig: SourceConfig,
timeoutSeconds: number = 10
): Promise<ReadyEvent> => {
return await PlayerTestWorld.shared.loadSourceConfig(
sourceConfig,
timeoutSeconds
);
};
/**
* Plays the player for the given time and expects `TimeChangedEvent` to occur.
* @param time The time to play for.
* @param timeoutSeconds The number of seconds to wait for the event to occur.
* @returns A promise that resolves when the function is finished.
* @throws An error if the event does not occur.
* @example
* ```typescript
* await playFor(5);
* ```
* @see {@link TimeChangedEvent}
*/
export const playFor = async (
time: number,
timeoutSeconds: number = 10
): Promise<TimeChangedEvent> => {
return await PlayerTestWorld.shared.playFor(time, timeoutSeconds);
};
/**
* Plays the player until the given time and expects `TimeChangedEvent` to occur.
* @param time The time to play until.
* @param timeoutSeconds The number of seconds to wait for the event to occur.
* @returns A promise that resolves when the function is finished.
* @throws An error if the event does not occur.
* @example
* ```typescript
* await playUntil(5);
* ```
* @see {@link TimeChangedEvent}
*/
export const playUntil = async (
time: number,
timeoutSeconds: number = 10
): Promise<TimeChangedEvent> => {
return await PlayerTestWorld.shared.playUntil(time, timeoutSeconds);
};