Commit b226dfc3 authored by Yue Zhang's avatar Yue Zhang Committed by Chromium LUCI CQ

[ChromeCart] Enable dismissing the module

Bug: 1157892
Change-Id: I386fb6bc5377c0a32c504eac5d8f0f92497a2943
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2599446Reviewed-by: default avatarAlex Gough <ajgo@chromium.org>
Reviewed-by: default avatarTibor Goldschwendt <tiborg@chromium.org>
Commit-Queue: Yue Zhang <yuezhanggg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#840320}
parent fa18d177
......@@ -3,18 +3,20 @@
// found in the LICENSE file.
#include "chrome/browser/cart/cart_handler.h"
#include "chrome/browser/cart/cart_service.h"
#include "chrome/browser/cart/cart_service_factory.h"
#include "components/search/ntp_features.h"
CartHandler::CartHandler(
mojo::PendingReceiver<chrome_cart::mojom::CartHandler> handler,
Profile* profile)
: handler_(this, std::move(handler)) {}
: handler_(this, std::move(handler)), profile_(profile) {}
CartHandler::~CartHandler() = default;
void CartHandler::GetMerchantCarts(GetMerchantCartsCallback callback) {
std::vector<chrome_cart::mojom::MerchantCartPtr> carts;
// TODO(https://crbug.com/1157892): Replace this with a feature parameter for
// TODO(https://crbug.com/1157892): Replace this with a feature parameter for
// fake data when real data is available.
if (base::FeatureList::IsEnabled(ntp_features::kNtpChromeCartModule)) {
auto dummy_cart1 = chrome_cart::mojom::MerchantCart::New();
......@@ -44,3 +46,11 @@ void CartHandler::GetMerchantCarts(GetMerchantCartsCallback callback) {
}
std::move(callback).Run(std::move(carts));
}
void CartHandler::DismissCartModule() {
CartServiceFactory::GetForProfile(profile_)->Dismiss();
}
void CartHandler::RestoreCartModule() {
CartServiceFactory::GetForProfile(profile_)->Restore();
}
......@@ -20,9 +20,12 @@ class CartHandler : public chrome_cart::mojom::CartHandler {
// chrome_cart::mojom::CartHandler:
void GetMerchantCarts(GetMerchantCartsCallback callback) override;
void DismissCartModule() override;
void RestoreCartModule() override;
private:
mojo::Receiver<chrome_cart::mojom::CartHandler> handler_;
Profile* profile_;
};
#endif // CHROME_BROWSER_CART_CART_HANDLER_H_
......@@ -20,4 +20,8 @@ struct MerchantCart {
interface CartHandler {
// Returns the merchant carts in chrome cart.
GetMerchantCarts() => (array<MerchantCart> carts);
// Dismisses the cart module.
DismissCartModule();
// Restores the cart module.
RestoreCartModule();
};
\ No newline at end of file
......@@ -102,7 +102,10 @@
width: 102px;
}
</style>
<ntp-module-header chip-text="$i18n{modulesCartHeaderNew}"></ntp-module-header>
<ntp-module-header
chip-text="$i18n{modulesCartHeaderNew}"
show-dismiss-button on-dismiss-button-click="onDismissButtonClick_">
</ntp-module-header>
<div id="moduleContent">
<template id="cartItemRepeat" is="dom-repeat" items="[[cartItems]]">
<a class="cart-item" title="[[item.merchant]]"
......
......@@ -54,6 +54,24 @@ class ChromeCartModuleElement extends PolymerElement {
getImagesToShow_(imageUrls) {
return imageUrls.slice(0, 3);
}
/** @private */
onDismissButtonClick_() {
ChromeCartProxy.getInstance().handler.dismissCartModule();
this.dispatchEvent(new CustomEvent('dismiss-module', {
bubbles: true,
composed: true,
detail: {
message: 'Your carts',
restoreCallback: this.onRestore_.bind(this),
},
}));
}
/** @private */
onRestore_() {
ChromeCartProxy.getInstance().handler.restoreCartModule();
}
}
customElements.define(ChromeCartModuleElement.is, ChromeCartModuleElement);
......
......@@ -5,6 +5,7 @@
import {chromeCartDescriptor, ChromeCartProxy} from 'chrome://new-tab-page/new_tab_page.js';
import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js';
import {TestBrowserProxy} from 'chrome://test/test_browser_proxy.m.js';
import {eventToPromise} from 'chrome://test/test_util.m.js';
suite('NewTabPageModulesChromeCartModuleTest', () => {
/**
......@@ -153,4 +154,46 @@ suite('NewTabPageModulesChromeCartModuleTest', () => {
assertEquals(
loadTimeData.getString('modulesCartHeaderNew'), headerChip.innerText);
});
test('Backend is notified when module is dismissed or restored', async () => {
// Arrange.
const carts = [
{
merchant: 'Amazon',
cartUrl: {url: 'https://amazon.com'},
productImageUrls: [
{url: 'https://image1.com'}, {url: 'https://image2.com'},
{url: 'https://image3.com'}
],
},
];
testProxy.handler.setResultFor(
'getMerchantCarts', Promise.resolve({carts}));
// Arrange.
await chromeCartDescriptor.initialize();
const moduleElement = chromeCartDescriptor.element;
document.body.append(moduleElement);
moduleElement.$.cartItemRepeat.render();
// Act.
const waitForDismissEvent = eventToPromise('dismiss-module', moduleElement);
const dismissButton =
moduleElement.shadowRoot.querySelector('ntp-module-header')
.shadowRoot.querySelector('#dismissButton');
dismissButton.click();
const dismissEvent = await waitForDismissEvent;
const toastMessage = dismissEvent.detail.message;
const restoreCallback = dismissEvent.detail.restoreCallback;
// Assert.
assertEquals('Your carts', toastMessage);
assertEquals(1, testProxy.handler.getCallCount('dismissCartModule'));
// Act.
restoreCallback();
// Assert.
assertEquals(1, testProxy.handler.getCallCount('restoreCartModule'));
});
});
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