Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dvoytenko committed Mar 5, 2020
1 parent df0a14d commit 79ffbb6
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 0 deletions.
133 changes: 133 additions & 0 deletions extensions/amp-story/1.0/test/test-amp-story-grid-layer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
* Copyright 2018 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 {Action, AmpStoryStoreService} from '../amp-story-store-service';
import {AmpDocSingle} from '../../../../src/service/ampdoc-impl';
import {AmpStoryGridLayer} from '../amp-story-grid-layer';
import {AmpStoryPage} from '../amp-story-page';
import {LocalizationService} from '../../../../src/service/localization';
import {MediaType} from '../media-pool';
import {Services} from '../../../../src/services';
import {registerServiceBuilder} from '../../../../src/service';

describes.realWin('amp-story-grid-layer', {amp: true}, env => {
let win;
let element;
let gridLayerEl;
let page;
let grid;
let storeService;

beforeEach(() => {
win = env.win;

env.sandbox
.stub(Services, 'vsyncFor')
.callsFake(() => ({mutate: task => task()}));

const mediaPoolRoot = {
getElement: () => win.document.createElement('div'),
getMaxMediaElementCounts: () => ({
[MediaType.VIDEO]: 8,
[MediaType.AUDIO]: 8,
}),
};

storeService = new AmpStoryStoreService(win);
registerServiceBuilder(win, 'story-store', function() {
return storeService;
});

const localizationService = new LocalizationService(win);
registerServiceBuilder(win, 'localization', function() {
return localizationService;
});

registerServiceBuilder(win, 'performance', function() {
return {
isPerformanceTrackingOn: () => false,
};
});

const story = win.document.createElement('amp-story');
story.getImpl = () => Promise.resolve(mediaPoolRoot);

element = win.document.createElement('amp-story-page');
gridLayerEl = win.document.createElement('amp-story-grid-layer');
element.getAmpDoc = () => new AmpDocSingle(win);
element.appendChild(gridLayerEl);
story.appendChild(element);
win.document.body.appendChild(story);

page = new AmpStoryPage(element);
env.sandbox.stub(page, 'mutateElement').callsFake(fn => fn());

grid = new AmpStoryGridLayer(gridLayerEl);
});

afterEach(() => {
element.remove();
});

async function buildGridLayer() {
page.buildCallback();
await page.layoutCallback();
grid.buildCallback();
await grid.layoutCallback();
}

it('should set the vertical aspect-ratio', async () => {
gridLayerEl.setAttribute('aspect-ratio', '9:16');
await buildGridLayer();

storeService.dispatch(Action.SET_PAGE_SIZE, {width: 1000, height: 1000});

expect(gridLayerEl).to.have.class('i-amphtml-story-grid-template-aspect');
expect(
parseInt(
gridLayerEl.style.getPropertyValue('--i-amphtml-story-layer-width'),
10
)
).to.equal(562);
expect(
parseInt(
gridLayerEl.style.getPropertyValue('--i-amphtml-story-layer-height'),
10
)
).to.equal(1000);
});

it('should set the horizontal aspect-ratio', async () => {
gridLayerEl.setAttribute('aspect-ratio', '16:9');
await buildGridLayer();

storeService.dispatch(Action.SET_PAGE_SIZE, {width: 1000, height: 1000});

expect(gridLayerEl).to.have.class('i-amphtml-story-grid-template-aspect');
expect(
parseInt(
gridLayerEl.style.getPropertyValue('--i-amphtml-story-layer-width'),
10
)
).to.equal(1000);
expect(
parseInt(
gridLayerEl.style.getPropertyValue('--i-amphtml-story-layer-height'),
10
)
).to.equal(562);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!--
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.
-->
<!--
Test Description:
Tests for the live story functionality inside amp-story.
-->
<!doctype html>
<html amp lang="en">
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-story"
src="https://cdn.ampproject.org/v0/amp-story-1.0.js"></script>
<title>amp-story-grid-layer</title>
<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>
<link rel="canonical" href="index.html">
</head>

<body>
<amp-story standalone>
<amp-story-page>
<amp-story-grid-layer aspect-ratio="a:b" template="vertical">
</amp-story-grid-layer>
</amp-story-page>
</amp-story>
</body>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FAIL
| <!--
| 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.
| -->
| <!--
| Test Description:
| Tests for the live story functionality inside amp-story.
| -->
| <!doctype html>
| <html amp lang="en">
| <head>
| <meta charset="utf-8">
| <script async src="https://cdn.ampproject.org/v0.js"></script>
| <script async custom-element="amp-story"
| src="https://cdn.ampproject.org/v0/amp-story-1.0.js"></script>
| <title>amp-story-grid-layer</title>
| <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>
| <link rel="canonical" href="index.html">
| </head>
|
| <body>
| <amp-story standalone>
| <amp-story-page>
| <amp-story-grid-layer aspect-ratio="a:b" template="vertical">
>> ^~~~~~~~~
amp-story/1.0/test/validator-amp-story-grid-layer-error.html:35:6 The attribute 'aspec-ratio' in tag 'amp-story-grid-layer' is set to the invalid value 'a:b'. (see https://amp.dev/documentation/components/amp-story)
| </amp-story-grid-layer>
| </amp-story-page>
| </amp-story>
| </body>
|
| </html>
40 changes: 40 additions & 0 deletions extensions/amp-story/1.0/test/validator-amp-story-grid-layer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!--
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.
-->
<!--
Test Description:
Tests for the live story functionality inside amp-story.
-->
<!doctype html>
<html amp lang="en">
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-story"
src="https://cdn.ampproject.org/v0/amp-story-1.0.js"></script>
<title>amp-story-grid-layer</title>
<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>
<link rel="canonical" href="index.html">
</head>

<body>
<amp-story standalone>
<amp-story-page>
<amp-story-grid-layer aspect-ratio="9:16" template="vertical">
</amp-story-grid-layer>
</amp-story-page>
</amp-story>
</body>

</html>
41 changes: 41 additions & 0 deletions extensions/amp-story/1.0/test/validator-amp-story-grid-layer.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
PASS
| <!--
| 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.
| -->
| <!--
| Test Description:
| Tests for the live story functionality inside amp-story.
| -->
| <!doctype html>
| <html amp lang="en">
| <head>
| <meta charset="utf-8">
| <script async src="https://cdn.ampproject.org/v0.js"></script>
| <script async custom-element="amp-story"
| src="https://cdn.ampproject.org/v0/amp-story-1.0.js"></script>
| <title>amp-story-grid-layer</title>
| <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>
| <link rel="canonical" href="index.html">
| </head>
|
| <body>
| <amp-story standalone>
| <amp-story-page>
| <amp-story-grid-layer aspect-ratio="9:16" template="vertical">
| </amp-story-grid-layer>
| </amp-story-page>
| </amp-story>
| </body>
|
| </html>
15 changes: 15 additions & 0 deletions extensions/amp-story/amp-story-grid-layer.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ Example:
</amp-story-grid-layer>
```

### aspect-ratio [optional]

The value specifies an aspect ratio in the "horizontal:vertical" format, where both "horizontal" and "vertical" are integer numbers. If this attribute is specified, the layout of the grid layer is set to conform to the specified proportions. The font size, in this case, is automatically set to the 1/10th of the resulting height to enable proportional content scaling.

Example:

```html
<amp-story-grid-layer aspect-ratio="9:16" template="vertical">
<div style="width: 10%; height: 10%; font-size: 2em;">
This block will be in 9:16 aspect ratio and font size will be set at the
20% of the layer's height.
</div>
</amp-story-grid-layer>
```

## Templates

The following are available templates to specify for the layout of the grid layer.
Expand Down
4 changes: 4 additions & 0 deletions extensions/amp-story/validator-amp-story.protoascii
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ tags: { # <amp-story-grid-layer>
value: "landscape-half-left"
value: "landscape-half-right"
}
attrs: {
name: "aspect-ratio"
value_regex: "\\d+:\\d+"
}
descendant_tag_list: "amp-story-grid-layer-allowed-descendants"
reference_points: {
tag_spec_name: "AMP-STORY-GRID-LAYER default"
Expand Down

0 comments on commit 79ffbb6

Please sign in to comment.