-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
A4A integration tests #5812
Merged
Merged
A4A integration tests #5812
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
31a42ab
First draft of full integration tests for A4A.
e916def
First draft of full integration tests for A4A.
9c5d96b
Got tests of exception conditions working. Refactored some common st…
9a08b60
Refactored XDom and friendly iframe tests to helpers.
af29b01
Added tests.
0de8f8e
Resolved merge conflicts.
481fe00
Centralized visibility check and updated visibility test.
3051838
Moved all A4A tests to using central visibility test.
c6d3e55
Lint fixes.
4fd797c
Changed custom expectations to local functions.
1848de6
Replaced stringToArrayBuffer with utf8Encode.
59e9634
Fixed visibility assertion.
0c72a3c
Lint fixes.
487d149
Added a comment (partly in order to get Travis to re-run).
fbda8c8
Changes in response to reviews.
b384663
Rebased and fixed small rebase error.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
/** | ||
* 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 { | ||
MockA4AImpl, | ||
SIGNATURE_HEADER, | ||
TEST_URL, | ||
} from './utils'; | ||
import {Xhr} from '../../../../src/service/xhr-impl'; | ||
import {createIframePromise} from '../../../../testing/iframe'; | ||
import { | ||
data as validCSSAmp, | ||
} from './testdata/valid_css_at_rules_amp.reserialized'; | ||
import {installDocService} from '../../../../src/service/ampdoc-impl'; | ||
import {adConfig} from '../../../../ads/_config'; | ||
import {a4aRegistry} from '../../../../ads/_a4a-config'; | ||
import { | ||
resetScheduledElementForTesting, | ||
upgradeOrRegisterElement, | ||
} from '../../../../src/custom-element'; | ||
import {utf8Encode} from '../../../../src/utils/bytes'; | ||
import * as sinon from 'sinon'; | ||
|
||
// Integration tests for A4A. These stub out accesses to the outside world | ||
// (e.g., XHR requests and interfaces to ad network-specific code), but | ||
// otherwise test the complete A4A flow, without making assumptions about | ||
// the structure of that flow. | ||
|
||
function expectRenderedInFriendlyIframe(element, srcdoc) { | ||
expect(element, 'ad element').to.be.ok; | ||
const child = element.querySelector('iframe[srcdoc]'); | ||
expect(child, 'iframe child').to.be.ok; | ||
expect(child.getAttribute('srcdoc')).to.contain.string(srcdoc); | ||
const childBody = child.contentDocument.body; | ||
expect(childBody, 'body of iframe doc').to.be.ok; | ||
expect(element, 'ad tag').to.be.visible; | ||
expect(child, 'iframe child').to.be.visible; | ||
expect(childBody, 'ad creative content body').to.be.visible; | ||
} | ||
|
||
function expectRenderedInXDomainIframe(element, src) { | ||
expect(element, 'ad element').to.be.ok; | ||
expect(element.querySelector('iframe[srcdoc]'), | ||
'does not have a friendly iframe child').to.not.be.ok; | ||
const child = element.querySelector('iframe[src]'); | ||
expect(child, 'iframe child').to.be.ok; | ||
expect(child.getAttribute('src')).to.contain.string(src); | ||
expect(element, 'ad tag').to.be.visible; | ||
expect(child, 'iframe child').to.be.visible; | ||
} | ||
|
||
describe('integration test: a4a', () => { | ||
let sandbox; | ||
let xhrMock; | ||
let fixture; | ||
let mockResponse; | ||
let a4aElement; | ||
beforeEach(() => { | ||
sandbox = sinon.sandbox.create(); | ||
xhrMock = sandbox.stub(Xhr.prototype, 'fetch'); | ||
mockResponse = { | ||
arrayBuffer: function() { | ||
return utf8Encode(validCSSAmp.reserialized); | ||
}, | ||
bodyUsed: false, | ||
headers: new Headers(), | ||
}; | ||
mockResponse.headers.append(SIGNATURE_HEADER, validCSSAmp.signature); | ||
xhrMock.withArgs(TEST_URL, { | ||
mode: 'cors', | ||
method: 'GET', | ||
credentials: 'include', | ||
requireAmpResponseSourceOrigin: true, | ||
}).onFirstCall().returns(Promise.resolve(mockResponse)); | ||
adConfig['mock'] = {}; | ||
a4aRegistry['mock'] = () => {return true;}; | ||
return createIframePromise().then(f => { | ||
fixture = f; | ||
installDocService(fixture.win, /* isSingleDoc */ true); | ||
upgradeOrRegisterElement(fixture.win, 'amp-a4a', MockA4AImpl); | ||
const doc = fixture.doc; | ||
a4aElement = doc.createElement('amp-a4a'); | ||
a4aElement.setAttribute('width', 200); | ||
a4aElement.setAttribute('height', 50); | ||
a4aElement.setAttribute('type', 'mock'); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
resetScheduledElementForTesting(window, 'amp-a4a'); | ||
delete adConfig['mock']; | ||
delete a4aRegistry['mock']; | ||
}); | ||
|
||
it('should render a single AMP ad in a friendly iframe', () => { | ||
return fixture.addElement(a4aElement).then(unusedElement => { | ||
expectRenderedInFriendlyIframe(a4aElement, 'Hello, world.'); | ||
}); | ||
}); | ||
|
||
it('should fall back to 3p when no signature is present', () => { | ||
mockResponse.headers.delete(SIGNATURE_HEADER); | ||
return fixture.addElement(a4aElement).then(unusedElement => { | ||
expectRenderedInXDomainIframe(a4aElement, TEST_URL); | ||
}); | ||
}); | ||
|
||
it('should fall back to 3p when the XHR fails', () => { | ||
xhrMock.resetBehavior(); | ||
xhrMock.throws(new Error('Testing network error')); | ||
// TODO(tdrl) Currently layoutCallback rejects, even though something *is* | ||
// rendered. This should be fixed in a refactor, and we should change this | ||
// .catch to a .then. | ||
return fixture.addElement(a4aElement).catch(error => { | ||
expect(error.message).to.contain.string('Testing network error'); | ||
expect(error.message).to.contain.string('amp-a4a:'); | ||
expectRenderedInXDomainIframe(a4aElement, TEST_URL); | ||
}); | ||
}); | ||
|
||
it('should fall back to 3p when extractCreative throws', () => { | ||
sandbox.stub(MockA4AImpl.prototype, 'extractCreativeAndSignature').throws( | ||
new Error('Testing extractCreativeAndSignature error')); | ||
// TODO(tdrl) Currently layoutCallback rejects, even though something *is* | ||
// rendered. This should be fixed in a refactor, and we should change this | ||
// .catch to a .then. | ||
return fixture.addElement(a4aElement).catch(error => { | ||
expect(error.message).to.contain.string( | ||
'Testing extractCreativeAndSignature error'); | ||
expect(error.message).to.contain.string('amp-a4a:'); | ||
expectRenderedInXDomainIframe(a4aElement, TEST_URL); | ||
}); | ||
}); | ||
|
||
it('should fall back to 3p when extractCreative returns empty sig', () => { | ||
const extractCreativeAndSignatureStub = | ||
sandbox.stub(MockA4AImpl.prototype, 'extractCreativeAndSignature'); | ||
extractCreativeAndSignatureStub.onFirstCall().returns({ | ||
creative: utf8Encode(validCSSAmp.reserialized), | ||
signature: null, | ||
}); | ||
return fixture.addElement(a4aElement).then(unusedElement => { | ||
expect(extractCreativeAndSignatureStub).to.be.calledOnce; | ||
expectRenderedInXDomainIframe(a4aElement, TEST_URL); | ||
}); | ||
}); | ||
|
||
it('should fall back to 3p when extractCreative returns empty creative', | ||
() => { | ||
sandbox.stub(MockA4AImpl.prototype, 'extractCreativeAndSignature') | ||
.onFirstCall().returns({ | ||
creative: null, | ||
signature: validCSSAmp.signature, | ||
}) | ||
.onSecondCall().throws(new Error( | ||
'Testing extractCreativeAndSignature should not occur error')); | ||
// TODO(tdrl) Currently layoutCallback rejects, even though something | ||
// *is* rendered. This should be fixed in a refactor, and we should | ||
// change this .catch to a .then. | ||
return fixture.addElement(a4aElement).catch(error => { | ||
expect(error.message).to.contain.string('Key failed to validate'); | ||
expect(error.message).to.contain.string('amp-a4a:'); | ||
expectRenderedInXDomainIframe(a4aElement, TEST_URL); | ||
}); | ||
}); | ||
|
||
// TODO(@ampproject/a4a): Need a test that double-checks that thrown errors | ||
// are propagated out and printed to console and/or sent upstream to error | ||
// logging systems. This is a bit tricky, because it's handled by the AMP | ||
// runtime and can't be done within the context of a | ||
// fixture.addElement().then() or .catch(). This should be integrated into | ||
// all tests, so that we know precisely when errors are being reported and | ||
// to whom. | ||
it('should propagate errors out and report them to upstream error log'); | ||
}); | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pls take a look at
describes.js
. It provides iframe isolation, should save a lot boilerplate code here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nifty. I wasn't aware of those -- thanks for pointing them out!
In this specific case, though, I'm afraid I don't see how to apply them to save a lot of boilerplate. I looked at doing:
for the setup. But then I still have to do a bunch of test-specific setup: mocking the XHR, setting up my expected response, building the amp-a4a element, calling
upgradeOrRegisterElement
to bind the mock impl toamp-a4a
, etc. That's the majority of the setup space, so I don't save a lot. Plus, I lose the niceaddElement
method thatcreateIframePromise
provides, so I have to do some lifecycle stuff myself in the individual tests.Unless I'm misunderstanding how to use those test fixtures? If so, I'm afraid you'll have to spell out a more explicit example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine to leave it as is. In general,
addElement
is OK for a "integration" test like this one, but we'd like to avoid for unit test (no need to initialize the AMP runtime).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, good to know. Yeah, for these tests, we really do want the AMP runtime in action. We really want to test that the interaction between A4A and AMP is correct / as expected. But it's a good thing to know for other, more unit-level tests. Thanks for the pointer.