Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(Ads): Add more basic Interstitials unit tests #7687

Merged
merged 5 commits into from
Nov 28, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 172 additions & 14 deletions test/ads/interstitial_ad_manager_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ describe('Interstitial Ad manager', () => {
let adContainer;
/** @type {!shaka.Player} */
let player;
/** @type {!shaka.test.FakeVideo} */
let mockVideo;
/** @type {!HTMLVideoElement} */
let video;
/** @type {!jasmine.Spy} */
let onEventSpy;
/** @type {!shaka.ads.InterstitialAdManager} */
let interstitialAdManager;

beforeEach(() => {
// Allows us to use a timer instead of requestVideoFrameCallback
// (which doesn't work well in all platform tests)
spyOn(shaka.util.Platform, 'isSmartTV').and.returnValue(true);

function dependencyInjector(player) {
// Create a networking engine that always returns an empty buffer.
networkingEngine = new shaka.test.FakeNetworkingEngine();
Expand All @@ -32,10 +36,15 @@ describe('Interstitial Ad manager', () => {
adContainer =
/** @type {!HTMLElement} */ (document.createElement('div'));
player = new shaka.Player(null, null, dependencyInjector);
mockVideo = new shaka.test.FakeVideo();
video = shaka.test.UiUtils.createVideoElement();
onEventSpy = jasmine.createSpy('onEvent');
interstitialAdManager = new shaka.ads.InterstitialAdManager(
adContainer, player, mockVideo, shaka.test.Util.spyFunc(onEventSpy));
adContainer, player, video, shaka.test.Util.spyFunc(onEventSpy));
const config = shaka.util.PlayerConfiguration.createDefault().ads;
// We always support multiple video elements so that we can properly
// control timing in unit tests.
config.supportsMultipleMediaElements = true;
interstitialAdManager.configure(config);
});

afterEach(async () => {
Expand Down Expand Up @@ -1063,20 +1072,169 @@ describe('Interstitial Ad manager', () => {
expect(onEventSpy).toHaveBeenCalledWith(
jasmine.objectContaining(eventValue1));
});

it('ignore empty', async () => {
const vmap = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<vmap:VMAP xmlns:vmap="http://www.iab.net/videosuite/vmap"',
' version="1.0">',
'</vmap:VMAP>',
].join('');

networkingEngine.setResponseText('test:/vmap', vmap);

await interstitialAdManager.addAdUrlInterstitial('test:/vmap');

expect(onEventSpy).not.toHaveBeenCalled();
});
});

it('ignore empty', async () => {
const vmap = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<vmap:VMAP xmlns:vmap="http://www.iab.net/videosuite/vmap"',
' version="1.0">',
'</vmap:VMAP>',
].join('');
it('plays pre-roll correctly', async () => {
const metadata = {
startTime: 0,
endTime: null,
values: [
{
key: 'ID',
data: 'PREROLL',
},
{
key: 'CUE',
data: 'PRE',
},
{
key: 'X-ASSET-URI',
data: 'test.m3u8',
},
{
key: 'X-RESTRICT',
data: 'SKIP,JUMP',
},
],
};
await interstitialAdManager.addMetadata(metadata);

networkingEngine.setResponseText('test:/vmap', vmap);
video.dispatchEvent(new Event('timeupdate'));

await interstitialAdManager.addAdUrlInterstitial('test:/vmap');
await shaka.test.Util.shortDelay();

expect(onEventSpy).not.toHaveBeenCalled();
expect(onEventSpy).toHaveBeenCalledTimes(2);
const eventValue1 = {
type: 'ad-cue-points-changed',
cuepoints: [
{
start: 0,
end: null,
},
],
};
expect(onEventSpy).toHaveBeenCalledWith(
jasmine.objectContaining(eventValue1));
const eventValue2 = {
type: 'ad-started',
};
expect(onEventSpy).toHaveBeenCalledWith(
jasmine.objectContaining(eventValue2));
});

it('dispatch skip event correctly', async () => {
const metadata = {
startTime: 0,
endTime: null,
values: [
{
key: 'ID',
data: 'PREROLL',
},
{
key: 'CUE',
data: 'PRE',
},
{
key: 'X-ASSET-URI',
data: 'test.m3u8',
},
],
};
await interstitialAdManager.addMetadata(metadata);

video.dispatchEvent(new Event('timeupdate'));

await shaka.test.Util.shortDelay();

expect(onEventSpy).toHaveBeenCalledTimes(3);
const eventValue1 = {
type: 'ad-cue-points-changed',
cuepoints: [
{
start: 0,
end: null,
},
],
};
expect(onEventSpy).toHaveBeenCalledWith(
jasmine.objectContaining(eventValue1));
const eventValue2 = {
type: 'ad-started',
};
expect(onEventSpy).toHaveBeenCalledWith(
jasmine.objectContaining(eventValue2));
const eventValue3 = {
type: 'ad-skip-state-changed',
};
expect(onEventSpy).toHaveBeenCalledWith(
jasmine.objectContaining(eventValue3));
});

it('jumping a mid-roll with JUMP restriction is not allowed', async () => {
const metadata = {
startTime: 10,
endTime: null,
values: [
{
key: 'ID',
data: 'MIDROLL',
},
{
key: 'X-ASSET-URI',
data: 'test.m3u8',
},
{
key: 'X-RESTRICT',
data: 'SKIP,JUMP',
},
],
};
await interstitialAdManager.addMetadata(metadata);

video.currentTime = 0;
video.dispatchEvent(new Event('timeupdate'));

await shaka.test.Util.shortDelay();

expect(onEventSpy).toHaveBeenCalledTimes(1);
const eventValue1 = {
type: 'ad-cue-points-changed',
cuepoints: [
{
start: 10,
end: null,
},
],
};
expect(onEventSpy).toHaveBeenCalledWith(
jasmine.objectContaining(eventValue1));

video.currentTime = 20;
video.dispatchEvent(new Event('timeupdate'));

await shaka.test.Util.delay(0.25);

expect(onEventSpy).toHaveBeenCalledTimes(2);
const eventValue2 = {
type: 'ad-started',
};
expect(onEventSpy).toHaveBeenCalledWith(
jasmine.objectContaining(eventValue2));
});
});
Loading