-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some changes to help building ads. (#2908)
- Add a startTime to the context given to ads. - Move getAdCid to a separate function, rather than a method of AmpAd. - Split out ad-cid tests from amp-ad tests. - Fix a typo.
- Loading branch information
1 parent
21c1272
commit 74b9daf
Showing
10 changed files
with
313 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright 2016 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {cidForOrNull} from './cid'; | ||
import {clientIdScope} from '../ads/_config'; | ||
import {userNotificationManagerFor} from './user-notification'; | ||
|
||
|
||
/** | ||
* @param {BaseElement} adElement | ||
* @return {!Promise<string|undefined>} A promise for a CID or undefined if | ||
* - the ad network does not request one or | ||
* - `amp-analytics` which provides the CID service was not installed. | ||
*/ | ||
export function getAdCid(adElement) { | ||
const scope = clientIdScope[adElement.element.getAttribute('type')]; | ||
const consentId = adElement.element.getAttribute( | ||
'data-consent-notification-id'); | ||
if (!(scope || consentId)) { | ||
return Promise.resolve(); | ||
} | ||
return cidForOrNull(adElement.getWin()).then(cidService => { | ||
if (!cidService) { | ||
return; | ||
} | ||
let consent = Promise.resolve(); | ||
if (consentId) { | ||
consent = userNotificationManagerFor(adElement.getWin()).then(service => { | ||
return service.get(consentId); | ||
}); | ||
if (!scope && consentId) { | ||
return consent; | ||
} | ||
} | ||
return cidService.get(scope, consent); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/** | ||
* Copyright 2016 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {clientIdScope} from '../../ads/_config'; | ||
import {createAdPromise} from '../../testing/ad-iframe'; | ||
import {installAd} from '../../builtins/amp-ad'; | ||
import {installEmbed} from '../../builtins/amp-embed'; | ||
import {installCidService} from '../../src/service/cid-impl'; | ||
import { | ||
installUserNotificationManager, | ||
} from '../../extensions/amp-user-notification/0.1/amp-user-notification'; | ||
import {setCookie} from '../../src/cookies'; | ||
import * as sinon from 'sinon'; | ||
|
||
|
||
describe('ad-cid', tests('amp-ad', installAd)); | ||
describe('ad-cid-embed', tests('amp-embed', win => { | ||
installAd(win); | ||
installEmbed(win); | ||
})); | ||
|
||
function tests(name, installer) { | ||
function getAd(attributes, canonical, opt_handleElement, | ||
opt_beforeLayoutCallback) { | ||
return createAdPromise(name, installer, attributes, canonical, | ||
opt_handleElement, opt_beforeLayoutCallback); | ||
} | ||
|
||
return () => { | ||
describe('cid-ad support', () => { | ||
const cidScope = 'cid-in-ads-test'; | ||
let sandbox; | ||
|
||
beforeEach(() => { | ||
sandbox = sinon.sandbox.create(); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
delete clientIdScope['with_cid']; | ||
setCookie(window, cidScope, '', new Date().getTime() - 5000); | ||
}); | ||
|
||
it('provides cid to ad', () => { | ||
clientIdScope['with_cid'] = cidScope; | ||
return getAd({ | ||
width: 300, | ||
height: 250, | ||
type: 'with_cid', | ||
src: 'testsrc', | ||
}, 'https://schema.org', function(ad) { | ||
const win = ad.ownerDocument.defaultView; | ||
setCookie(window, cidScope, 'sentinel123', | ||
new Date().getTime() + 5000); | ||
installCidService(win); | ||
return ad; | ||
}).then(ad => { | ||
expect(ad.getAttribute('ampcid')).to.equal('sentinel123'); | ||
}); | ||
}); | ||
|
||
it('waits for consent', () => { | ||
clientIdScope['with_cid'] = cidScope; | ||
return getAd({ | ||
width: 300, | ||
height: 250, | ||
type: 'with_cid', | ||
src: 'testsrc', | ||
'data-consent-notification-id': 'uid', | ||
}, 'https://schema.org', function(ad) { | ||
const win = ad.ownerDocument.defaultView; | ||
const cidService = installCidService(win); | ||
const uidService = installUserNotificationManager(win); | ||
sandbox.stub(uidService, 'get', id => { | ||
expect(id).to.equal('uid'); | ||
return Promise.resolve('consent'); | ||
}); | ||
sandbox.stub(cidService, 'get', (scope, consent) => { | ||
expect(scope).to.equal(cidScope); | ||
return consent.then(val => { | ||
return val + '-cid'; | ||
}); | ||
}); | ||
return ad; | ||
}).then(ad => { | ||
expect(ad.getAttribute('ampcid')).to.equal('consent-cid'); | ||
}); | ||
}); | ||
|
||
it('waits for consent w/o cidScope', () => { | ||
return getAd({ | ||
width: 300, | ||
height: 250, | ||
type: 'with_cid', | ||
src: 'testsrc', | ||
'data-consent-notification-id': 'uid', | ||
}, 'https://schema.org', function(ad) { | ||
const win = ad.ownerDocument.defaultView; | ||
const cidService = installCidService(win); | ||
const uidService = installUserNotificationManager(win); | ||
sandbox.stub(uidService, 'get', id => { | ||
expect(id).to.equal('uid'); | ||
return Promise.resolve('consent'); | ||
}); | ||
sandbox.stub(cidService, 'get', (scope, consent) => { | ||
expect(scope).to.equal(cidScope); | ||
return consent.then(val => { | ||
return val + '-cid'; | ||
}); | ||
}); | ||
return ad; | ||
}).then(ad => { | ||
expect(ad.getAttribute('ampcid')).to.equal('consent'); | ||
}); | ||
}); | ||
|
||
it('provide null if notification and cid is not provided', () => { | ||
let uidSpy = null; | ||
return getAd({ | ||
width: 300, | ||
height: 250, | ||
type: 'with_cid', | ||
src: 'testsrc', | ||
}, 'https://schema.org', function(ad) { | ||
const win = ad.ownerDocument.defaultView; | ||
const cidService = installCidService(win); | ||
const uidService = installUserNotificationManager(win); | ||
uidSpy = sandbox.spy(uidService, 'get'); | ||
sandbox.stub(cidService, 'get', (scope, consent) => { | ||
expect(scope).to.equal(cidScope); | ||
return consent.then(val => { | ||
return val + '-cid'; | ||
}); | ||
}); | ||
return ad; | ||
}).then(ad => { | ||
expect(uidSpy.callCount).to.equal(0); | ||
expect(ad.getAttribute('ampcid')).to.be.null; | ||
}); | ||
}); | ||
|
||
it('provides null if cid service not available', () => { | ||
clientIdScope['with_cid'] = cidScope; | ||
return getAd({ | ||
width: 300, | ||
height: 250, | ||
type: 'with_cid', | ||
src: 'testsrc', | ||
}, 'https://schema.org', function(ad) { | ||
setCookie(window, cidScope, 'XXX', | ||
new Date().getTime() + 5000); | ||
return ad; | ||
}).then(ad => { | ||
expect(ad.getAttribute('ampcid')).to.be.null; | ||
}); | ||
}); | ||
}); | ||
}; | ||
} |
Oops, something went wrong.