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

🏗 Added simple Visual Diff Tests for amp-consent #20783

Merged
merged 5 commits into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions build-system/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ app.use('/api/echo/post', (req, res) => {
* @type {RegExp}
*/
const ORIGIN_REGEX = new RegExp('^http://localhost:8000|' +
'^http://.+\.localhost:8000|' +
'^https?://.+\.herokuapp\.com');

/**
Expand All @@ -216,6 +217,7 @@ const ORIGIN_REGEX = new RegExp('^http://localhost:8000|' +
* @type {RegExp}
*/
const SOURCE_ORIGIN_REGEX = new RegExp('^http://localhost:8000|' +
'^http://.+\.localhost:8000|' +
'^https?://.+\.herokuapp\.com');

app.use('/form/html/post', (req, res) => {
Expand Down
80 changes: 80 additions & 0 deletions examples/visual-tests/amp-consent/amp-consent.amp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!doctype html>
<html amp lang="en">
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<title>Hello, AMPs</title>
<link rel="canonical" href="http://example.ampproject.org/article-metadata.html">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">

<script async custom-element="amp-consent" src="https://cdn.ampproject.org/v0/amp-consent-0.1.js"></script>
<script async custom-element="amp-iframe" src="https://cdn.ampproject.org/v0/amp-iframe-0.1.js"></script>
<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>
</head>
<body>
<h1>amp-consent</h1>

<h3>Image that is blocked by '_till_responded' consent</h3>
<amp-img data-block-on-consent='_till_responded'
src="https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w300-h200-no-n"
width=30
height=20>
</amp-img>

<h3>Image that is blocked by '_till_accepted' consent</h3>
<amp-img data-block-on-consent='_till_accepted'
src="https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w300-h200-no-n"
width=30
height=20>
</amp-img>

<h3>Image that is blocked by '_auto_reject' consent</h3>
<amp-img data-block-on-consent='_auto_reject'
src="https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w300-h200-no-n"
width=30
height=20>
</amp-img>

<h3>Image that is blocked by 'default' consent</h3>
<amp-img
data-block-on-consent='default'
src="https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w300-h200-no-n"
width=30
height=20>
</amp-img>

<h3>Image that is blocked by default (not specified) consent</h3>
<amp-img
id="not-specified"
data-block-on-consent
src="https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w300-h200-no-n"
width=30
height=20>
</amp-img>

<h3>Image that is NOT blocked by consent</h3>
<amp-img
src="https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w300-h200-no-n"
width=30
height=20>
</amp-img>

<amp-consent layout="nodisplay" id="consent-element">
<script type="application/json">
{
"consents": {
"my-consent": {
"checkConsentHref": "/get-consent-v1/",
"promptUI": "consent-ui"
}
}
}
</script>
<div id="consent-ui">
<button id="accept" on="tap:consent-element.accept" role="button">Accept</button>
<button id="reject" on="tap:consent-element.reject" role="button">Reject</button>
<button id="dismiss" on="tap:consent-element.dismiss" role="button">Dismiss</button>
</div>
</amp-consent>
</body>
</html>
110 changes: 110 additions & 0 deletions examples/visual-tests/amp-consent/amp-consent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Copyright 2019 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.
*/
'use strict';

const {verifyCssElements} = require('../../../build-system/tasks/visual-diff/helpers');

const clearLocalStorage = async (page) => {
await page.evaluate(() => {
localStorage.clear();
});
await page.reload({waitUntil: 'networkidle0'});
}

module.exports = {
'accept consent': async (page, name) => {

await clearLocalStorage(page);

await page.tap('#consent-ui #accept');

await verifyCssElements(page, name,
/* forbiddenCss */ null,
/* loadingIncompleteCss */ null,
/* loadingCompleteCss */ [
'amp-img[data-block-on-consent="_till_responded"] img',
'amp-img[data-block-on-consent="_till_accepted"] img',
'amp-img[data-block-on-consent="default"] img',
'#not-specified img'
]);
},
'reject consent': async (page, name) => {

await clearLocalStorage(page);

await page.tap('#consent-ui #reject');

await page.evaluate(() => {
document.querySelector('#consent-ui #reject').click();
});

await verifyCssElements(page, name,
/* forbiddenCss */ null,
/* loadingIncompleteCss */ null,
/* loadingCompleteCss */ [
'amp-img[data-block-on-consent="_till_responded"] img'
]);
},
'should preserve accepted state': async (page, name) => {

await clearLocalStorage(page);

// Accept the consent
await page.tap('#consent-ui #accept');

await verifyCssElements(page, name,
/* forbiddenCss */ null,
/* loadingIncompleteCss */ null,
/* loadingCompleteCss */ [
'amp-img[data-block-on-consent="_till_responded"] img',
'amp-img[data-block-on-consent="_till_accepted"] img',
'amp-img[data-block-on-consent="default"] img',
'#not-specified img'
]);

// save the accepted local storage value
const localStorageJson = await page.evaluate(() => {
const json = {};
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
json[key] = localStorage.getItem(key);
}
return json;
});

// Refresh the page
await page.reload();

// Set the local storage back to the accepted value
await page.evaluate(json => {
localStorage.clear();

Object.keys(json).forEach(key => {
localStorage.setItem(key, json[key]);
});
}, localStorageJson);

await verifyCssElements(page, name,
/* forbiddenCss */ null,
/* loadingIncompleteCss */ null,
/* loadingCompleteCss */ [
'amp-img[data-block-on-consent="_till_responded"] img',
'amp-img[data-block-on-consent="_till_accepted"] img',
'amp-img[data-block-on-consent="default"] img',
'#not-specified img'
]);
}
};
5 changes: 5 additions & 0 deletions test/visual-diff/visual-tests
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@
"loading_incomplete_css": [".article-body"],
"loading_complete_css": [".login-section"]
},
{
"url": "examples/visual-tests/amp-consent/amp-consent.amp.html",
"name": "amp-consent",
"interactive_tests": "examples/visual-tests/amp-consent/amp-consent.js"
},
{
"url": "examples/visual-tests/font.amp/font.amp.html",
"name": "Fonts",
Expand Down