mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-25 08:01:20 -08:00
test(vast): [PKG-VAST-02] cover SDK failures and lifecycle races
This commit is contained in:
1 parent
4f39d6f2b9
commit
d5e1feadf5
16 files changed
+1402
-12
No files matched your search
@@ -0,0 +1,60 @@
|
||||
// Controlled wrapper boundary, not an implementation of Google IMA or media playback.
|
||||
// Self-contained so the same recorder can run in Node and a real browser realm.
|
||||
export function createVastSdk(options = {}) {
|
||||
const state = { players: [], loads: 0, errors: [], ...options }
|
||||
let resolveLoad
|
||||
let rejectLoad
|
||||
const pending = new Promise((resolve, reject) => {
|
||||
resolveLoad = resolve
|
||||
rejectLoad = reject
|
||||
})
|
||||
const ima = { AdsRenderingSettings: class {}, AdsRequest: class {} }
|
||||
const sdk = {
|
||||
loadImaSdk() {
|
||||
state.loads++
|
||||
return state.deferLoad ? pending : Promise.resolve(ima)
|
||||
},
|
||||
PlayerOptions: class {},
|
||||
Player: class {
|
||||
constructor(...args) {
|
||||
if (state.constructError)
|
||||
throw state.constructError
|
||||
this.args = args
|
||||
this.requests = []
|
||||
this.listeners = new Map()
|
||||
this.destroyCalls = 0
|
||||
state.players.push(this)
|
||||
}
|
||||
|
||||
addEventListener(name, callback) {
|
||||
if (state.listenError)
|
||||
throw state.listenError
|
||||
if (!this.listeners.has(name))
|
||||
this.listeners.set(name, new Set())
|
||||
this.listeners.get(name).add(callback)
|
||||
}
|
||||
|
||||
removeEventListener(name, callback) {
|
||||
this.listeners.get(name)?.delete(callback)
|
||||
}
|
||||
|
||||
emit(name, detail) {
|
||||
for (const callback of [...(this.listeners.get(name) || [])]) callback({ type: name, detail })
|
||||
}
|
||||
|
||||
playAds(request) {
|
||||
if (state.playError)
|
||||
throw state.playError
|
||||
this.requests.push(request)
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.destroyCalls++
|
||||
if (state.destroyError)
|
||||
throw state.destroyError
|
||||
this.listeners.clear()
|
||||
}
|
||||
},
|
||||
}
|
||||
return { state, sdk, ima, resolveLoad: () => resolveLoad(ima), rejectLoad }
|
||||
}
|
||||
Reference in new issue
Block a user