Commit 10ced8a9 authored by Esmael El-Moslimany's avatar Esmael El-Moslimany Committed by Commit Bot

Reland "WebUI Modules: shopping tasks, hide elements that do not fit"

Fix flaky test and reland CL.

Bug: 1134274
Change-Id: I58452d5264e36eb838e4e1d413c917496f4b964a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2444374
Auto-Submit: Esmael Elmoslimany <aee@chromium.org>
Commit-Queue: Tibor Goldschwendt <tiborg@chromium.org>
Reviewed-by: default avatarTibor Goldschwendt <tiborg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813208}
parent 8758810d
......@@ -133,7 +133,7 @@
</style>
<div id="products">
<template is="dom-repeat" id="productsRepeat"
items="[[shoppingTask.products]]">
items="[[shoppingTask.products]]" on-dom-change="onDomChange_">
<a class="product" href="[[item.targetUrl.url]]" on-click="onClick_"
on-auxclick="onClick_">
<div class="image">
......@@ -147,7 +147,7 @@
</div>
<div id="relatedSearches">
<template is="dom-repeat" id="relatedSearchesRepeat"
items="[[shoppingTask.relatedSearches]]">
items="[[shoppingTask.relatedSearches]]" on-dom-change="onDomChange_">
<a class="pill" href="[[item.targetUrl.url]]" on-click="onClick_"
on-auxclick="onClick_">
<div class="loupe"></div>
......
......@@ -34,6 +34,13 @@ class ShoppingTasksModuleElement extends PolymerElement {
};
}
/** @override */
ready() {
super.ready();
/** @type {IntersectionObserver} */
this.intersectionObserver_ = null;
}
/** @private */
onClick_() {
this.dispatchEvent(new Event('usage', {bubbles: true, composed: true}));
......@@ -43,6 +50,23 @@ class ShoppingTasksModuleElement extends PolymerElement {
onCloseClick_() {
this.showInfoDialog = false;
}
/** @private */
onDomChange_() {
if (!this.intersectionObserver_) {
this.intersectionObserver_ = new IntersectionObserver(entries => {
entries.forEach(({intersectionRatio, target}) => {
target.style.visibility =
intersectionRatio < 1 ? 'hidden' : 'visible';
});
this.dispatchEvent(new Event('visibility-update'));
}, {root: this, threshold: 1});
} else {
this.intersectionObserver_.disconnect();
}
this.shadowRoot.querySelectorAll('.product, .pill')
.forEach(el => this.intersectionObserver_.observe(el));
}
}
customElements.define(
......
......@@ -4,6 +4,7 @@
import {shoppingTasksDescriptor, ShoppingTasksHandlerProxy} from 'chrome://new-tab-page/new_tab_page.js';
import {TestBrowserProxy} from 'chrome://test/test_browser_proxy.m.js';
import {eventToPromise} from 'chrome://test/test_util.m.js';
suite('NewTabPageModulesShoppingTasksModuleTest', () => {
/**
......@@ -102,4 +103,45 @@ suite('NewTabPageModulesShoppingTasksModuleTest', () => {
assertEquals('https://blub.com/', pills[1].href);
assertEquals('blub', pills[1].querySelector('.search-text').innerText);
});
test('products and pills are hidden when cutoff', async () => {
const repeat = (n, fn) => Array(n).fill(0).map(fn);
testProxy.handler.setResultFor('getPrimaryShoppingTask', Promise.resolve({
shoppingTask: {
title: 'Hello world',
products: repeat(20, () => ({
name: 'foo',
imageUrl: {url: 'https://foo.com/img.png'},
price: '1 gazillion dollars',
info: 'foo info',
targetUrl: {url: 'https://foo.com'},
})),
relatedSearches: repeat(20, () => ({
text: 'baz',
targetUrl: {url: 'https://baz.com'},
})),
}
}));
await shoppingTasksDescriptor.initialize();
const moduleElement = shoppingTasksDescriptor.element;
document.body.append(moduleElement);
moduleElement.$.productsRepeat.render();
moduleElement.$.relatedSearchesRepeat.render();
const getElements = () => Array.from(
moduleElement.shadowRoot.querySelectorAll('.product, .pill'));
assertEquals(40, getElements().length);
const hiddenCount = () =>
getElements().filter(el => el.style.visibility === 'hidden').length;
const checkHidden = async (width, count) => {
const waitForVisibilityUpdate =
eventToPromise('visibility-update', moduleElement);
moduleElement.style.width = width;
await waitForVisibilityUpdate;
assertEquals(count, hiddenCount());
};
await checkHidden('500px', 31);
await checkHidden('300px', 35);
await checkHidden('700px', 26);
await checkHidden('500px', 31);
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment