Skip to content

Commit

Permalink
More test coverage for ampcontext API (#10637)
Browse files Browse the repository at this point in the history
* Fix wrong context.location in ampcontext

* Test coverage for window.context.sourceUrl, window.context.container

* Test coverage for window.context.clientId

* more coverage

* more

* more

* address comment
  • Loading branch information
lannka committed Jul 26, 2017
1 parent 8de49a7 commit af46013
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
1 change: 1 addition & 0 deletions ads/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ let AdNetworkConfigDef;
export const adConfig = {
_ping_: {
renderStartImplemented: true,
clientIdScope: '_PING_',
},

a8: {
Expand Down
21 changes: 12 additions & 9 deletions test/fixtures/3p-ad.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<script async src="/dist/amp.js"></script>
<!-- use custom url to force same origin -->
<script async custom-element="amp-ad" src="/dist/v0/amp-ad-0.1.max.js"></script>
<!-- use custom url to force same origin -->
<meta name="amp-3p-iframe-src" content="http://localhost:9876/dist.3p/current/frame.max.html">
</head>
<body>
<div class='fill'></div>
<amp-ad width=300 height=250
type="_ping_"
data-id='0'
data-url='https://example.com/a?b=c&d=e'
data-valid='true'
data-custom-value=123
data-other_value="foo">
</amp-ad>
<!-- amp-lightbox is used for ad container testing, no script needed -->
<amp-lightbox>
<amp-ad width=300 height=250
type="_ping_"
data-id='0'
data-url='https://example.com/a?b=c&d=e'
data-valid='true'
data-custom-value=123
data-other_value="foo">
</amp-ad>
</amp-lightbox>
</body>
<div class='fill'></div>
</html>
52 changes: 33 additions & 19 deletions test/integration/test-amp-ad-3p.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
toggleExperiment,
resetExperimentTogglesForTesting,
} from '../../src/experiments';
import {layoutRectLtwh} from '../../src/layout-rect';

// TODO(@alanorozco): Inline this once 3p-use-ampcontext experiment is removed
function createIframeWithApis(fixture) {
Expand All @@ -47,18 +48,10 @@ function createIframeWithApis(fixture) {
};
});
}).then(context => {
// test iframe is created with correct context info.
expect(context.hidden).to.be.false;
// In some browsers the referrer is empty. But in Chrome it works, so
// we always check there.
if (context.referrer !== '' || platform.isChrome()) {
expect(context.referrer).to.contain(
'http://localhost:' + location.port);
}

expect(context.canonicalUrl).to.equal(
'https://www.example.com/doubleclick.html');
expect(context.clientId).to.be.defined;
expect(context.clientId).to.match(/amp-[a-zA-Z0-9\-_.]{22,24}/);
expect(context.container).to.equal('AMP-LIGHTBOX');
expect(context.data).to.deep.equal({
width: 300,
height: 250,
Expand All @@ -76,14 +69,22 @@ function createIframeWithApis(fixture) {
expect(context.data).to.equal(
iframe.contentWindow.networkIntegrationDataParamForTesting);

expect(context.pageViewId).to.be.greaterThan(0);
expect(context.startTime).to.be.a('number');
expect(context.container).to.be.defined;
expect(context.initialIntersection).to.be.defined;
// check for rootBounds as native IO doesn't support it with CORS
expect(context.initialLayoutRect).to.be.defined;
expect(context.initialLayoutRect.top).to.be.defined;
expect(context.initialIntersection.rootBounds).to.be.defined;
expect(context.hidden).to.be.false;
expect(context.initialLayoutRect).to.deep.equal({
height: 250,
left: 0,
top: platform.isIos() ? 1001 : 1000, // the iOS 1px trick
width: 300,
});
const initialIntersection = context.initialIntersection;
expect(initialIntersection.rootBounds).to.deep
.equal(layoutRectLtwh(0, 0, 500, 3000));
expect(initialIntersection.boundingClientRect).to.deep
.equal(layoutRectLtwh(0, platform.isIos() ? 1001 : 1000, 300, 250));
expect(initialIntersection.intersectionRect).to.deep
.equal(layoutRectLtwh(0, platform.isIos() ? 1001 : 1000, 300, 250));
expect(initialIntersection.intersectionRatio).to.equal(1);
expect(initialIntersection.time).to.be.a('number');
expect(context.isMaster).to.be.defined;
expect(context.computeInMasterFrame).to.be.defined;
expect(context.location).to.deep.equal({
Expand All @@ -97,7 +98,20 @@ function createIframeWithApis(fixture) {
protocol: 'http:',
search: '',
});
expect(context.sourceUrl).to.be.a('string');
expect(context.pageViewId).to.be.greaterThan(0);
// In some browsers the referrer is empty. But in Chrome it works, so
// we always check there.
if (context.referrer !== '' || platform.isChrome()) {
expect(context.referrer).to.contain(
'http://localhost:' + location.port);
}
expect(context.startTime).to.be.a('number');
// Edge has different opinion about window.location in srcdoc iframe.
// Nevertheless this only happens in test. In real world AMP will not
// in srcdoc iframe.
expect(context.sourceUrl).to.equal(platform.isEdge()
? 'http://localhost:9876/context.html'
: 'about:srcdoc');
}).then(() => {
// test iframe will send out render-start to amp-ad
return poll('render-start message received', () => {
Expand Down

0 comments on commit af46013

Please sign in to comment.