Commit a269cdb0 authored by Tibor Goldschwendt's avatar Tibor Goldschwendt Committed by Commit Bot

[ntp][modules] Fix promo and module coordination

* Show modules even if there is no promo. Modules wait until the promo
  has sent a loaded event. Before, this event was only sent if there was
  a promo. This CL changes it so that the event is sent even if there is
  no promo.

* Only log NewTabPage.Modules.ShownTime after modules are allowed to be
  visible. This could be after they are added to the DOM in case the
  promo takes longer to load.

Bug: 1134269
Change-Id: I5975524ba3c783de66dda2b98d347442f6b73a96
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2443693
Commit-Queue: Tibor Goldschwendt <tiborg@chromium.org>
Reviewed-by: default avatarMoe Ahmadi <mahmadi@chromium.org>
Auto-Submit: Tibor Goldschwendt <tiborg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813285}
parent 55b23a64
...@@ -110,7 +110,7 @@ ...@@ -110,7 +110,7 @@
:host(:not([promo-and-modules-loaded_])) ntp-middle-slot-promo, :host(:not([promo-and-modules-loaded_])) ntp-middle-slot-promo,
:host(:not([promo-and-modules-loaded_])) ntp-module-wrapper { :host(:not([promo-and-modules-loaded_])) ntp-module-wrapper {
visibility: hidden; display: none;
} }
#customizeButtonSpacer { #customizeButtonSpacer {
...@@ -290,7 +290,7 @@ ...@@ -290,7 +290,7 @@
on-ntp-middle-slot-promo-loaded="onMiddleSlotPromoLoaded_" hidden> on-ntp-middle-slot-promo-loaded="onMiddleSlotPromoLoaded_" hidden>
</ntp-middle-slot-promo> </ntp-middle-slot-promo>
<template is="dom-repeat" items="[[moduleDescriptors_]]" id="modules" <template is="dom-repeat" items="[[moduleDescriptors_]]" id="modules"
on-dom-change="onModulesRendered_"> on-dom-change="onModulesLoaded_">
<ntp-module-wrapper descriptor="[[item]]" <ntp-module-wrapper descriptor="[[item]]"
on-dismiss-module="onDismissModule_"> on-dismiss-module="onDismissModule_">
</ntp-module-wrapper> </ntp-module-wrapper>
......
...@@ -216,6 +216,7 @@ class AppElement extends PolymerElement { ...@@ -216,6 +216,7 @@ class AppElement extends PolymerElement {
computed: `computePromoAndModulesLoaded_(middleSlotPromoLoaded_, computed: `computePromoAndModulesLoaded_(middleSlotPromoLoaded_,
modulesLoaded_)`, modulesLoaded_)`,
reflectToAttribute: true, reflectToAttribute: true,
observer: 'onPromoAndModulesLoadedChange_',
}, },
/** /**
...@@ -507,7 +508,6 @@ class AppElement extends PolymerElement { ...@@ -507,7 +508,6 @@ class AppElement extends PolymerElement {
} }
this.moduleDescriptors_ = this.moduleDescriptors_ =
await ModuleRegistry.getInstance().initializeModules(); await ModuleRegistry.getInstance().initializeModules();
this.modulesLoaded_ = true;
} }
/** @private */ /** @private */
...@@ -592,6 +592,11 @@ class AppElement extends PolymerElement { ...@@ -592,6 +592,11 @@ class AppElement extends PolymerElement {
this.updateBackgroundImagePath_(); this.updateBackgroundImagePath_();
} }
/** @private */
onPromoAndModulesLoadedChange_() {
this.pageHandler_.onModulesRendered(BrowserProxy.getInstance().now());
}
/** /**
* Set the #backgroundImage |path| only when different and non-empty. Reset * Set the #backgroundImage |path| only when different and non-empty. Reset
* the customize dialog background selection if the dialog is closed. * the customize dialog background selection if the dialog is closed.
...@@ -808,8 +813,8 @@ class AppElement extends PolymerElement { ...@@ -808,8 +813,8 @@ class AppElement extends PolymerElement {
} }
/** @private */ /** @private */
onModulesRendered_() { onModulesLoaded_() {
this.pageHandler_.onModulesRendered(BrowserProxy.getInstance().now()); this.modulesLoaded_ = true;
} }
/** /**
......
...@@ -50,41 +50,40 @@ class MiddleSlotPromoElement extends PolymerElement { ...@@ -50,41 +50,40 @@ class MiddleSlotPromoElement extends PolymerElement {
connectedCallback() { connectedCallback() {
super.connectedCallback(); super.connectedCallback();
this.pageHandler_.getPromo().then(({promo}) => { this.pageHandler_.getPromo().then(({promo}) => {
if (!promo) { if (promo) {
return; promo.middleSlotParts.forEach(({image, link, text}) => {
} let el;
promo.middleSlotParts.forEach(({image, link, text}) => { if (image) {
let el; el = new ImgElement();
if (image) { el.autoSrc = image.imageUrl.url;
el = new ImgElement(); if (image.target) {
el.autoSrc = image.imageUrl.url; const anchor = this.createAnchor_(image.target);
if (image.target) { if (anchor) {
const anchor = this.createAnchor_(image.target); anchor.appendChild(el);
if (anchor) { el = anchor;
anchor.appendChild(el); }
el = anchor;
} }
el.classList.add('image');
} else if (link) {
el = this.createAnchor_(link.url);
} else if (text) {
el = document.createElement('span');
} }
el.classList.add('image'); const linkOrText = link || text;
} else if (link) { if (el && linkOrText) {
el = this.createAnchor_(link.url); el.innerText = linkOrText.text;
} else if (text) { if (linkOrText.color) {
el = document.createElement('span'); el.style.color = linkOrText.color;
} }
const linkOrText = link || text;
if (el && linkOrText) {
el.innerText = linkOrText.text;
if (linkOrText.color) {
el.style.color = linkOrText.color;
} }
} if (el) {
if (el) { this.$.container.appendChild(el);
this.$.container.appendChild(el); }
} });
}); this.pageHandler_.onPromoRendered(
this.pageHandler_.onPromoRendered( BrowserProxy.getInstance().now(), promo.logUrl || null);
BrowserProxy.getInstance().now(), promo.logUrl || null); this.hidden = false;
this.hidden = false; }
this.dispatchEvent(new Event( this.dispatchEvent(new Event(
'ntp-middle-slot-promo-loaded', {bubbles: true, composed: true})); 'ntp-middle-slot-promo-loaded', {bubbles: true, composed: true}));
}); });
......
...@@ -8,18 +8,18 @@ import {TestBrowserProxy} from 'chrome://test/test_browser_proxy.m.js'; ...@@ -8,18 +8,18 @@ import {TestBrowserProxy} from 'chrome://test/test_browser_proxy.m.js';
import {eventToPromise, flushTasks} from 'chrome://test/test_util.m.js'; import {eventToPromise, flushTasks} from 'chrome://test/test_util.m.js';
suite('NewTabPageMiddleSlotPromoTest', () => { suite('NewTabPageMiddleSlotPromoTest', () => {
/** @type {!MiddleSlotPromoElement} */
let middleSlotPromo;
/** /**
* @implements {BrowserProxy} * @implements {BrowserProxy}
* @extends {TestBrowserProxy} * @extends {TestBrowserProxy}
*/ */
let testProxy; let testProxy;
setup(async () => { setup(() => {
PolymerTest.clearBody(); PolymerTest.clearBody();
testProxy = createTestProxy(); testProxy = createTestProxy();
});
async function createMiddleSlotPromo() {
testProxy.handler.setResultFor('getPromo', Promise.resolve({ testProxy.handler.setResultFor('getPromo', Promise.resolve({
promo: { promo: {
middleSlotParts: [ middleSlotParts: [
...@@ -57,12 +57,14 @@ suite('NewTabPageMiddleSlotPromoTest', () => { ...@@ -57,12 +57,14 @@ suite('NewTabPageMiddleSlotPromoTest', () => {
BrowserProxy.instance_ = testProxy; BrowserProxy.instance_ = testProxy;
const loaded = const loaded =
eventToPromise('ntp-middle-slot-promo-loaded', document.body); eventToPromise('ntp-middle-slot-promo-loaded', document.body);
middleSlotPromo = document.createElement('ntp-middle-slot-promo'); const middleSlotPromo = document.createElement('ntp-middle-slot-promo');
document.body.appendChild(middleSlotPromo); document.body.appendChild(middleSlotPromo);
await loaded; await loaded;
}); return middleSlotPromo;
}
test('render', () => { test('render', async () => {
const middleSlotPromo = await createMiddleSlotPromo();
const parts = middleSlotPromo.$.container.children; const parts = middleSlotPromo.$.container.children;
assertEquals(6, parts.length); assertEquals(6, parts.length);
const [image, imageWithLink, imageWithCommand, text, link, command] = parts; const [image, imageWithLink, imageWithCommand, text, link, command] = parts;
...@@ -88,6 +90,7 @@ suite('NewTabPageMiddleSlotPromoTest', () => { ...@@ -88,6 +90,7 @@ suite('NewTabPageMiddleSlotPromoTest', () => {
}); });
test('clicking on command', async () => { test('clicking on command', async () => {
const middleSlotPromo = await createMiddleSlotPromo();
const testProxy = PromoBrowserCommandProxy.getInstance(); const testProxy = PromoBrowserCommandProxy.getInstance();
testProxy.handler = TestBrowserProxy.fromClass( testProxy.handler = TestBrowserProxy.fromClass(
promoBrowserCommand.mojom.CommandHandlerRemote); promoBrowserCommand.mojom.CommandHandlerRemote);
...@@ -115,4 +118,13 @@ suite('NewTabPageMiddleSlotPromoTest', () => { ...@@ -115,4 +118,13 @@ suite('NewTabPageMiddleSlotPromoTest', () => {
expectedClickInfo); expectedClickInfo);
})); }));
}); });
test('sends loaded event if no promo', async () => {
testProxy.handler.setResultFor('getPromo', Promise.resolve({promo: null}));
const loaded =
eventToPromise('ntp-middle-slot-promo-loaded', document.body);
const middleSlotPromo = document.createElement('ntp-middle-slot-promo');
document.body.appendChild(middleSlotPromo);
await loaded;
});
}); });
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