Commit e87569a5 authored by Moe Ahmadi's avatar Moe Ahmadi Committed by Commit Bot

Allows checking whether a promo with a browser command should be shown

- Allows OneGoogle callouts to query the NTP whether or not to show a
  promo with a browser command.
- Hides the middle slot promos if any of the promo's browser commands
  cannot be shown.
- Gates the ESB promo on whether Safe Browsing prefs are managed or
  the user is already opted in.

More info on the API: go/promos-deeplinking-settings

Bug: 1143366
Change-Id: I33c5b36c69a42e6b0c562b8afa0ed8ab56801486
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2505739
Commit-Queue: Moe Ahmadi <mahmadi@chromium.org>
Commit-Queue: Esmael Elmoslimany <aee@chromium.org>
Auto-Submit: Moe Ahmadi <mahmadi@chromium.org>
Reviewed-by: default avatarEsmael Elmoslimany <aee@chromium.org>
Reviewed-by: default avatarAlex Gough <ajgo@chromium.org>
Reviewed-by: default avatarVarun Khaneja <vakh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#822523}
parent 37cc551c
...@@ -31,6 +31,9 @@ struct ClickInfo { ...@@ -31,6 +31,9 @@ struct ClickInfo {
// Browser-side handler for requests from JS. // Browser-side handler for requests from JS.
interface CommandHandler { interface CommandHandler {
// Returns whether or not a promo with the given command ID can be shown.
CanShowPromoWithCommand(Command command_id) => (bool can_show);
// Executes the command with the given ID and click information. Returns // Executes the command with the given ID and click information. Returns
// whether or not the command was executed. // whether or not the command was executed.
ExecuteCommand(Command command_id, ClickInfo click_info) ExecuteCommand(Command command_id, ClickInfo click_info)
......
...@@ -743,6 +743,34 @@ class AppElement extends PolymerElement { ...@@ -743,6 +743,34 @@ class AppElement extends PolymerElement {
} }
} }
/**
* Sends the command received from the given source and origin to the browser.
* Relays the browser response to whether or not a promo containing the given
* command can be shown back to the source promo frame. |commandSource| and
* |commandOrigin| are used only to send the response back to the source promo
* frame and should not be used for anything else.
* @param {Object} messageData Data received from the source promo frame.
* @param {Window} commandSource Source promo frame.
* @param {string} commandOrigin Origin of the source promo frame.
* @private
*/
canShowPromoWithBrowserCommand_(messageData, commandSource, commandOrigin) {
// Make sure we don't send unsupported commands to the browser.
/** @type {!promoBrowserCommand.mojom.Command} */
const commandId = Object.values(promoBrowserCommand.mojom.Command)
.includes(messageData.commandId) ?
messageData.commandId :
promoBrowserCommand.mojom.Command.kUnknownCommand;
PromoBrowserCommandProxy.getInstance()
.handler.canShowPromoWithCommand(commandId)
.then(({canShow}) => {
const response = {messageType: messageData.messageType};
response[messageData.commandId] = canShow;
commandSource.postMessage(response, commandOrigin);
});
}
/** /**
* Sends the command and the accompanying mouse click info received from the * Sends the command and the accompanying mouse click info received from the
* promo of the given source and origin to the browser. Relays the execution * promo of the given source and origin to the browser. Relays the execution
...@@ -815,6 +843,8 @@ class AppElement extends PolymerElement { ...@@ -815,6 +843,8 @@ class AppElement extends PolymerElement {
} else if (data.messageType === 'deactivate') { } else if (data.messageType === 'deactivate') {
this.$.oneGoogleBarOverlayBackdrop.toggleAttribute('show', false); this.$.oneGoogleBarOverlayBackdrop.toggleAttribute('show', false);
$$(this, '#oneGoogleBar').style.zIndex = '0'; $$(this, '#oneGoogleBar').style.zIndex = '0';
} else if (data.messageType === 'can-show-promo-with-browser-command') {
this.canShowPromoWithBrowserCommand_(data, event.source, event.origin);
} else if (data.messageType === 'execute-browser-command') { } else if (data.messageType === 'execute-browser-command') {
this.executePromoBrowserCommand_( this.executePromoBrowserCommand_(
/** @type {!CommandData} */ (data.data), event.source, event.origin); /** @type {!CommandData} */ (data.data), event.source, event.origin);
......
...@@ -28,6 +28,7 @@ class MiddleSlotPromoElement extends PolymerElement { ...@@ -28,6 +28,7 @@ class MiddleSlotPromoElement extends PolymerElement {
/** @type {boolean} */ /** @type {boolean} */
hidden: { hidden: {
type: Boolean, type: Boolean,
value: true,
reflectToAttribute: true, reflectToAttribute: true,
}, },
...@@ -37,6 +38,18 @@ class MiddleSlotPromoElement extends PolymerElement { ...@@ -37,6 +38,18 @@ class MiddleSlotPromoElement extends PolymerElement {
value: () => loadTimeData.getBoolean('modulesEnabled'), value: () => loadTimeData.getBoolean('modulesEnabled'),
reflectToAttribute: true, reflectToAttribute: true,
}, },
/**
* The list of browser commands to run, if any. Used to decide whether the
* promo can be shown.
* @type {!Array<number>}
* @private
*/
/** @private */
commandIds_: {
type: Object,
value: () => [],
},
}; };
} }
...@@ -80,9 +93,13 @@ class MiddleSlotPromoElement extends PolymerElement { ...@@ -80,9 +93,13 @@ class MiddleSlotPromoElement extends PolymerElement {
this.$.container.appendChild(el); this.$.container.appendChild(el);
} }
}); });
this.pageHandler_.onPromoRendered( this.maybeShowPromo_().then(canShow => {
BrowserProxy.getInstance().now(), promo.logUrl || null); if (canShow) {
this.hidden = false; this.pageHandler_.onPromoRendered(
BrowserProxy.getInstance().now(), promo.logUrl || null);
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}));
...@@ -100,17 +117,20 @@ class MiddleSlotPromoElement extends PolymerElement { ...@@ -100,17 +117,20 @@ class MiddleSlotPromoElement extends PolymerElement {
return null; return null;
} }
const el = /** @type {!HTMLAnchorElement} */ (document.createElement('a')); const el = /** @type {!HTMLAnchorElement} */ (document.createElement('a'));
let commandId = null;
if (!commandIdMatch) { if (!commandIdMatch) {
el.href = target.url; el.href = target.url;
} else {
commandId = +commandIdMatch[1];
// Make sure we don't send unsupported commands to the browser.
if (!Object.values(promoBrowserCommand.mojom.Command)
.includes(commandId)) {
commandId = promoBrowserCommand.mojom.Command.kUnknownCommand;
}
this.commandIds_.push(commandId);
} }
const onClick = event => { const onClick = event => {
if (commandIdMatch) { if (commandId !== null) {
let commandId = +commandIdMatch[1];
// Make sure we don't send unsupported commands to the browser.
if (!Object.values(promoBrowserCommand.mojom.Command)
.includes(commandId)) {
commandId = promoBrowserCommand.mojom.Command.kUnknownCommand;
}
PromoBrowserCommandProxy.getInstance().handler.executeCommand( PromoBrowserCommandProxy.getInstance().handler.executeCommand(
commandId, { commandId, {
middleButton: event.button === 1, middleButton: event.button === 1,
...@@ -128,6 +148,18 @@ class MiddleSlotPromoElement extends PolymerElement { ...@@ -128,6 +148,18 @@ class MiddleSlotPromoElement extends PolymerElement {
el.addEventListener('click', onClick); el.addEventListener('click', onClick);
return el; return el;
} }
/**
* @return {!Promise<boolean>} Whether or not the promo can be shown by
* checking all command IDs seen in the promo.
* @private
*/
async maybeShowPromo_() {
const {handler} = PromoBrowserCommandProxy.getInstance();
const promises = this.commandIds_.map(
commandId => handler.canShowPromoWithCommand(commandId));
return (await Promise.all(promises)).every(({canShow}) => canShow);
}
} }
customElements.define(MiddleSlotPromoElement.is, MiddleSlotPromoElement); customElements.define(MiddleSlotPromoElement.is, MiddleSlotPromoElement);
...@@ -1525,6 +1525,7 @@ static_library("ui") { ...@@ -1525,6 +1525,7 @@ static_library("ui") {
"//components/printing/browser", "//components/printing/browser",
"//components/profile_metrics", "//components/profile_metrics",
"//components/reading_list/features:flags", "//components/reading_list/features:flags",
"//components/safe_browsing/core/common:safe_browsing_policy_handler",
"//components/safety_check", "//components/safety_check",
"//components/search_provider_logos", "//components/search_provider_logos",
"//components/services/app_service/public/cpp:app_update", "//components/services/app_service/public/cpp:app_update",
......
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
#include "chrome/browser/ui/browser_navigator.h" #include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/chrome_pages.h" #include "chrome/browser/ui/chrome_pages.h"
#include "chrome/common/webui_url_constants.h" #include "chrome/common/webui_url_constants.h"
#include "components/safe_browsing/content/web_ui/safe_browsing_ui.h"
#include "components/safe_browsing/core/common/safe_browsing_policy_handler.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "ui/base/page_transition_types.h" #include "ui/base/page_transition_types.h"
#include "ui/base/window_open_disposition.h" #include "ui/base/window_open_disposition.h"
...@@ -39,6 +42,31 @@ PromoBrowserCommandHandler::PromoBrowserCommandHandler( ...@@ -39,6 +42,31 @@ PromoBrowserCommandHandler::PromoBrowserCommandHandler(
PromoBrowserCommandHandler::~PromoBrowserCommandHandler() = default; PromoBrowserCommandHandler::~PromoBrowserCommandHandler() = default;
void PromoBrowserCommandHandler::CanShowPromoWithCommand(
promo_browser_command::mojom::Command command_id,
CanShowPromoWithCommandCallback callback) {
bool can_show = false;
switch (static_cast<Command>(command_id)) {
case Command::kUnknownCommand:
// Nothing to do.
break;
case Command::kOpenSafetyCheck:
can_show = true;
break;
case Command::kOpenSafeBrowsingEnhancedProtectionSettings: {
bool managed = safe_browsing::SafeBrowsingPolicyHandler::
IsSafeBrowsingProtectionLevelSetByPolicy(profile_->GetPrefs());
bool already_enabled =
safe_browsing::IsEnhancedProtectionEnabled(*(profile_->GetPrefs()));
can_show = !managed && !already_enabled;
} break;
default:
NOTREACHED() << "Unspecified behavior for command " << command_id;
break;
}
std::move(callback).Run(can_show);
}
void PromoBrowserCommandHandler::ExecuteCommand( void PromoBrowserCommandHandler::ExecuteCommand(
Command command_id, Command command_id,
ClickInfoPtr click_info, ClickInfoPtr click_info,
......
...@@ -31,6 +31,9 @@ class PromoBrowserCommandHandler ...@@ -31,6 +31,9 @@ class PromoBrowserCommandHandler
~PromoBrowserCommandHandler() override; ~PromoBrowserCommandHandler() override;
// promo_browser_command::mojom::CommandHandler: // promo_browser_command::mojom::CommandHandler:
void CanShowPromoWithCommand(
promo_browser_command::mojom::Command command_id,
CanShowPromoWithCommandCallback callback) override;
void ExecuteCommand(promo_browser_command::mojom::Command command_id, void ExecuteCommand(promo_browser_command::mojom::Command command_id,
promo_browser_command::mojom::ClickInfoPtr click_info, promo_browser_command::mojom::ClickInfoPtr click_info,
ExecuteCommandCallback callback) override; ExecuteCommandCallback callback) override;
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
#include "chrome/browser/ui/webui/new_tab_page/promo_browser_command/promo_browser_command_handler.h" #include "chrome/browser/ui/webui/new_tab_page/promo_browser_command/promo_browser_command_handler.h"
#include "chrome/common/webui_url_constants.h" #include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h" #include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -72,6 +74,15 @@ class MockCommandUpdater : public CommandUpdaterImpl { ...@@ -72,6 +74,15 @@ class MockCommandUpdater : public CommandUpdaterImpl {
MOCK_CONST_METHOD1(SupportsCommand, bool(int id)); MOCK_CONST_METHOD1(SupportsCommand, bool(int id));
}; };
// Callback used for testing
// PromoBrowserCommandHandler::CanShowPromoWithCommand().
void CanShowPromoWithCommandCallback(base::OnceClosure quit_closure,
bool* expected_can_show,
bool can_show) {
*expected_can_show = can_show;
std::move(quit_closure).Run();
}
// Callback used for testing PromoBrowserCommandHandler::ExecuteCommand(). // Callback used for testing PromoBrowserCommandHandler::ExecuteCommand().
void ExecuteCommandCallback(base::OnceClosure quit_closure, void ExecuteCommandCallback(base::OnceClosure quit_closure,
bool* expected_command_executed, bool* expected_command_executed,
...@@ -104,6 +115,16 @@ class PromoBrowserCommandHandlerTest : public testing::Test { ...@@ -104,6 +115,16 @@ class PromoBrowserCommandHandlerTest : public testing::Test {
command_handler_->GetCommandUpdater()); command_handler_->GetCommandUpdater());
} }
bool CanShowPromoWithCommand(Command command_id) {
base::RunLoop run_loop;
bool can_show = false;
command_handler_->CanShowPromoWithCommand(
command_id, base::BindOnce(&CanShowPromoWithCommandCallback,
run_loop.QuitClosure(), &can_show));
run_loop.Run();
return can_show;
}
bool ExecuteCommand(Command command_id, ClickInfoPtr click_info) { bool ExecuteCommand(Command command_id, ClickInfoPtr click_info) {
base::RunLoop run_loop; base::RunLoop run_loop;
bool command_executed = false; bool command_executed = false;
...@@ -180,6 +201,10 @@ TEST_F(PromoBrowserCommandHandlerTest, DisableHandlingCommands) { ...@@ -180,6 +201,10 @@ TEST_F(PromoBrowserCommandHandlerTest, DisableHandlingCommands) {
PromoBrowserCommandHandler::kPromoBrowserCommandHistogramName, 0); PromoBrowserCommandHandler::kPromoBrowserCommandHistogramName, 0);
} }
TEST_F(PromoBrowserCommandHandlerTest, CanShowOpenSafetyCheckCommandPromo) {
EXPECT_TRUE(CanShowPromoWithCommand(Command::kOpenSafetyCheck));
}
TEST_F(PromoBrowserCommandHandlerTest, OpenSafetyCheckCommand) { TEST_F(PromoBrowserCommandHandlerTest, OpenSafetyCheckCommand) {
// The OpenSafetyCheck command opens a new settings window with the Safety // The OpenSafetyCheck command opens a new settings window with the Safety
// Check, and the correct disposition. // Check, and the correct disposition.
...@@ -193,6 +218,70 @@ TEST_F(PromoBrowserCommandHandlerTest, OpenSafetyCheckCommand) { ...@@ -193,6 +218,70 @@ TEST_F(PromoBrowserCommandHandlerTest, OpenSafetyCheckCommand) {
EXPECT_TRUE(ExecuteCommand(Command::kOpenSafetyCheck, std::move(info))); EXPECT_TRUE(ExecuteCommand(Command::kOpenSafetyCheck, std::move(info)));
} }
TEST_F(PromoBrowserCommandHandlerTest,
CanShowSafeBrowsingEnhancedProtectionCommandPromo_NoPolicies) {
EXPECT_TRUE(CanShowPromoWithCommand(
Command::kOpenSafeBrowsingEnhancedProtectionSettings));
}
TEST_F(
PromoBrowserCommandHandlerTest,
CanShowSafeBrowsingEnhancedProtectionCommandPromo_EnhancedProtectionEnabled) {
TestingProfile::Builder builder;
std::unique_ptr<TestingProfile> profile = builder.Build();
profile->GetTestingPrefService()->SetUserPref(
prefs::kSafeBrowsingEnhanced, std::make_unique<base::Value>(true));
command_handler_ = std::make_unique<MockCommandHandler>(profile.get());
EXPECT_FALSE(CanShowPromoWithCommand(
Command::kOpenSafeBrowsingEnhancedProtectionSettings));
}
TEST_F(
PromoBrowserCommandHandlerTest,
CanShowSafeBrowsingEnhancedProtectionCommandPromo_HasSafeBrowsingManaged_NoProtection) {
TestingProfile::Builder builder;
std::unique_ptr<TestingProfile> profile = builder.Build();
profile->GetTestingPrefService()->SetManagedPref(
prefs::kSafeBrowsingEnabled, std::make_unique<base::Value>(false));
profile->GetTestingPrefService()->SetManagedPref(
prefs::kSafeBrowsingEnhanced, std::make_unique<base::Value>(false));
command_handler_ = std::make_unique<MockCommandHandler>(profile.get());
EXPECT_FALSE(CanShowPromoWithCommand(
Command::kOpenSafeBrowsingEnhancedProtectionSettings));
}
TEST_F(
PromoBrowserCommandHandlerTest,
CanShowSafeBrowsingEnhancedProtectionCommandPromo_HasSafeBrowsingManaged_StandardProtection) {
TestingProfile::Builder builder;
std::unique_ptr<TestingProfile> profile = builder.Build();
profile->GetTestingPrefService()->SetManagedPref(
prefs::kSafeBrowsingEnabled, std::make_unique<base::Value>(true));
profile->GetTestingPrefService()->SetManagedPref(
prefs::kSafeBrowsingEnhanced, std::make_unique<base::Value>(false));
command_handler_ = std::make_unique<MockCommandHandler>(profile.get());
EXPECT_FALSE(CanShowPromoWithCommand(
Command::kOpenSafeBrowsingEnhancedProtectionSettings));
}
TEST_F(
PromoBrowserCommandHandlerTest,
CanShowSafeBrowsingEnhancedProtectionCommandPromo_HasSafeBrowsingManaged_EnhancedProtection) {
TestingProfile::Builder builder;
std::unique_ptr<TestingProfile> profile = builder.Build();
profile->GetTestingPrefService()->SetManagedPref(
prefs::kSafeBrowsingEnabled, std::make_unique<base::Value>(true));
profile->GetTestingPrefService()->SetManagedPref(
prefs::kSafeBrowsingEnhanced, std::make_unique<base::Value>(true));
command_handler_ = std::make_unique<MockCommandHandler>(profile.get());
EXPECT_FALSE(CanShowPromoWithCommand(
Command::kOpenSafeBrowsingEnhancedProtectionSettings));
}
TEST_F(PromoBrowserCommandHandlerTest, TEST_F(PromoBrowserCommandHandlerTest,
OpenSafeBrowsingEnhancedProtectionCommand) { OpenSafeBrowsingEnhancedProtectionCommand) {
// The kOpenSafeBrowsingEnhancedProtectionSettings command opens a new // The kOpenSafeBrowsingEnhancedProtectionSettings command opens a new
......
...@@ -398,6 +398,38 @@ suite('NewTabPageAppTest', () => { ...@@ -398,6 +398,38 @@ suite('NewTabPageAppTest', () => {
assertTrue(mostVisited.hasAttribute('use-title-pill')); assertTrue(mostVisited.hasAttribute('use-title-pill'));
}); });
test('can show promo with browser command', async () => {
const testProxy = PromoBrowserCommandProxy.getInstance();
testProxy.handler = TestBrowserProxy.fromClass(
promoBrowserCommand.mojom.CommandHandlerRemote);
testProxy.handler.setResultFor(
'canShowPromoWithCommand', Promise.resolve({canShow: true}));
const commandId = 123; // Unsupported command.
window.dispatchEvent(new MessageEvent('message', {
data: {
frameType: 'one-google-bar',
messageType: 'can-show-promo-with-browser-command',
commandId,
},
source: window,
origin: window.origin,
}));
// Make sure the command is sent to the browser.
const expectedCommandId =
await testProxy.handler.whenCalled('canShowPromoWithCommand');
// Unsupported commands get resolved to the default command before being
// sent to the browser.
assertEquals(
promoBrowserCommand.mojom.Command.kUnknownCommand, expectedCommandId);
// Make sure the promo frame gets notified whether the promo can be shown.
const {data} = await eventToPromise('message', window);
assertEquals('can-show-promo-with-browser-command', data.messageType);
assertTrue(data[commandId]);
});
test('executes promo browser command', async () => { test('executes promo browser command', async () => {
const testProxy = PromoBrowserCommandProxy.getInstance(); const testProxy = PromoBrowserCommandProxy.getInstance();
testProxy.handler = TestBrowserProxy.fromClass( testProxy.handler = TestBrowserProxy.fromClass(
......
...@@ -9,18 +9,18 @@ import {TestBrowserProxy} from 'chrome://test/test_browser_proxy.m.js'; ...@@ -9,18 +9,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', () => {
/**
* @implements {BrowserProxy}
* @extends {TestBrowserProxy}
*/
let testProxy;
setup(() => { setup(() => {
PolymerTest.clearBody(); PolymerTest.clearBody();
testProxy = createTestProxy(); BrowserProxy.instance_ = createTestProxy();
const promoBrowserCommandTestProxy = PromoBrowserCommandProxy.getInstance();
promoBrowserCommandTestProxy.handler = TestBrowserProxy.fromClass(
promoBrowserCommand.mojom.CommandHandlerRemote);
}); });
async function createMiddleSlotPromo() { async function createMiddleSlotPromo(canShowPromo = true) {
const testProxy = BrowserProxy.getInstance();
testProxy.handler.setResultFor('getPromo', Promise.resolve({ testProxy.handler.setResultFor('getPromo', Promise.resolve({
promo: { promo: {
middleSlotParts: [ middleSlotParts: [
...@@ -55,46 +55,62 @@ suite('NewTabPageMiddleSlotPromoTest', () => { ...@@ -55,46 +55,62 @@ suite('NewTabPageMiddleSlotPromoTest', () => {
], ],
}, },
})); }));
BrowserProxy.instance_ = testProxy;
const loaded = const promoBrowserCommandTestProxy = PromoBrowserCommandProxy.getInstance();
eventToPromise('ntp-middle-slot-promo-loaded', document.body); promoBrowserCommandTestProxy.handler.setResultFor(
'canShowPromoWithCommand', Promise.resolve({canShow: canShowPromo}));
const 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 eventToPromise('ntp-middle-slot-promo-loaded', document.body);
await promoBrowserCommandTestProxy.handler.whenCalled(
'canShowPromoWithCommand');
assertEquals(
2,
promoBrowserCommandTestProxy.handler.getCallCount(
'canShowPromoWithCommand'));
if (canShowPromo) {
await testProxy.handler.whenCalled('onPromoRendered');
} else {
assertEquals(0, testProxy.handler.getCallCount('onPromoRendered'));
}
return middleSlotPromo; return middleSlotPromo;
} }
test('render', async () => { [true, false].forEach(canShowPromo => {
const middleSlotPromo = await createMiddleSlotPromo(); test(`render ${canShowPromo}`, async () => {
const parts = middleSlotPromo.$.container.children; const middleSlotPromo = await createMiddleSlotPromo(canShowPromo);
assertEquals(6, parts.length); assertEquals(!canShowPromo, middleSlotPromo.hasAttribute('hidden'));
const [image, imageWithLink, imageWithCommand, text, link, command] = parts; const parts = middleSlotPromo.$.container.children;
assertEquals(6, parts.length);
const [image, imageWithLink, imageWithCommand, text, link, command] =
parts;
assertEquals('https://image', image.autoSrc); assertEquals('https://image', image.autoSrc);
assertEquals('https://link/', imageWithLink.href); assertEquals('https://link/', imageWithLink.href);
assertEquals('https://image', imageWithLink.children[0].autoSrc); assertEquals('https://image', imageWithLink.children[0].autoSrc);
assertEquals('', imageWithCommand.href); assertEquals('', imageWithCommand.href);
assertEquals('https://image', imageWithCommand.children[0].autoSrc); assertEquals('https://image', imageWithCommand.children[0].autoSrc);
assertEquals('text', text.innerText); assertEquals('text', text.innerText);
assertEquals('red', text.style.color); assertEquals('red', text.style.color);
assertEquals('https://link/', link.href); assertEquals('https://link/', link.href);
assertEquals('link', link.innerText); assertEquals('link', link.innerText);
assertEquals('green', link.style.color); assertEquals('green', link.style.color);
assertEquals('', command.href); assertEquals('', command.href);
assertEquals('command', command.text); assertEquals('command', command.text);
assertEquals('blue', command.style.color); assertEquals('blue', command.style.color);
});
}); });
test('clicking on command', async () => { test('clicking on command', async () => {
const middleSlotPromo = await createMiddleSlotPromo(); const middleSlotPromo = await createMiddleSlotPromo();
assertFalse(middleSlotPromo.hasAttribute('hidden'));
const testProxy = PromoBrowserCommandProxy.getInstance(); const testProxy = PromoBrowserCommandProxy.getInstance();
testProxy.handler = TestBrowserProxy.fromClass(
promoBrowserCommand.mojom.CommandHandlerRemote);
testProxy.handler.setResultFor('executeCommand', Promise.resolve()); testProxy.handler.setResultFor('executeCommand', Promise.resolve());
const imageWithCommand = middleSlotPromo.$.container.children[2]; const imageWithCommand = middleSlotPromo.$.container.children[2];
const command = middleSlotPromo.$.container.children[5]; const command = middleSlotPromo.$.container.children[5];
...@@ -121,11 +137,13 @@ suite('NewTabPageMiddleSlotPromoTest', () => { ...@@ -121,11 +137,13 @@ suite('NewTabPageMiddleSlotPromoTest', () => {
}); });
test('sends loaded event if no promo', async () => { test('sends loaded event if no promo', async () => {
const testProxy = BrowserProxy.getInstance();
testProxy.handler.setResultFor('getPromo', Promise.resolve({promo: null})); testProxy.handler.setResultFor('getPromo', Promise.resolve({promo: null}));
const loaded = const loaded =
eventToPromise('ntp-middle-slot-promo-loaded', document.body); eventToPromise('ntp-middle-slot-promo-loaded', document.body);
const 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;
assertTrue(middleSlotPromo.hasAttribute('hidden'));
}); });
}); });
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