Commit 51977cf1 authored by Sahel Sharify's avatar Sahel Sharify Committed by Commit Bot

[Payments] Automate PH uninstallation tests.

This cl adds platform browser tests for payment handler uninstallation.

Bug: 1068734
Change-Id: Ibc7fa4e28afec32ee21a2378c3899b4c61d2daa0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2341013Reviewed-by: default avatarDanyao Wang <danyao@chromium.org>
Commit-Queue: Sahel Sharify <sahel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#796413}
parent 6c87eb2b
......@@ -20,6 +20,7 @@ source_set("browsertests") {
"payment_handler_enforce_full_delegation_browsertest.cc",
"payment_handler_exploit_browsertest.cc",
"payment_handler_just_in_time_installation_browsertest.cc",
"payment_handler_uninstall_browsertest.cc",
"payment_request_app_store_billing_browsertest.cc",
"payment_request_can_make_payment_browsertest.cc",
"payment_request_can_make_payment_event_browsertest.cc",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/test/payments/payment_request_platform_browsertest_base.h"
#include "content/public/test/browser_test.h"
namespace payments {
namespace {
class PaymentHandlerUninstallTest
: public PaymentRequestPlatformBrowserTestBase {
protected:
PaymentHandlerUninstallTest() = default;
~PaymentHandlerUninstallTest() override = default;
void SetUpOnMainThread() override {
PaymentRequestPlatformBrowserTestBase::SetUpOnMainThread();
NavigateTo("/payment_handler.html");
}
};
IN_PROC_BROWSER_TEST_F(PaymentHandlerUninstallTest, URLBasedPaymentMethod) {
EXPECT_EQ("success", content::EvalJs(GetActiveWebContents(), "install()"));
// Launch the payment request and confirm checkout completion.
ResetEventWaiterForSingleEvent(TestEvent::kPaymentCompleted);
EXPECT_EQ("success", content::EvalJs(GetActiveWebContents(), "launch()"));
WaitForObservedEvent();
// Uninstall the payment app and verify that a new request.show() gets
// rejected after the app uninstallation.
EXPECT_EQ("success", content::EvalJs(GetActiveWebContents(), "uninstall()"));
ResetEventWaiterForSingleEvent(TestEvent::kNotSupportedError);
EXPECT_EQ("success", content::EvalJs(GetActiveWebContents(),
"launchWithoutWaitForResponse()"));
WaitForObservedEvent();
}
IN_PROC_BROWSER_TEST_F(PaymentHandlerUninstallTest, BasicCard) {
EXPECT_EQ("success",
content::EvalJs(GetActiveWebContents(), "install('basic-card')"));
// Launch the payment request and validate that one app is available.
ResetEventWaiterForSingleEvent(TestEvent::kAppListReady);
EXPECT_EQ("success", content::EvalJs(GetActiveWebContents(),
"launchWithoutWaitForResponse()"));
WaitForObservedEvent();
EXPECT_EQ(1u, test_controller()->app_descriptions().size());
EXPECT_EQ("success", content::EvalJs(GetActiveWebContents(), "abort()"));
// Uninstall the payment app and verify that there is no payment app
// available. A new request.show() will not get rejected though since the user
// will still have the option to add a credit card.
EXPECT_EQ("success", content::EvalJs(GetActiveWebContents(), "uninstall()"));
ResetEventWaiterForSingleEvent(TestEvent::kAppListReady);
EXPECT_EQ("success", content::EvalJs(GetActiveWebContents(),
"launchWithoutWaitForResponse()"));
WaitForObservedEvent();
EXPECT_EQ(0u, test_controller()->app_descriptions().size());
}
} // namespace
} // namespace payments
......@@ -4,19 +4,27 @@
* found in the LICENSE file.
*/
const methodName = window.location.origin + '/pay';
const swSrcUrl = 'payment_handler_sw.js';
const DEFAULT_METHOD_NAME = window.location.origin + '/pay';
const SW_SRC_URL = 'payment_handler_sw.js';
/** Installs the payment handler. */
async function install() { // eslint-disable-line no-unused-vars
let methodName = DEFAULT_METHOD_NAME;
var request;
/** Installs the payment handler.
* @param {string} method - The payment method that this service worker
* supports.
* @return {Promise<string>} - 'success' or error message on failure.
*/
async function install(method=DEFAULT_METHOD_NAME) { // eslint-disable-line no-unused-vars, max-len
try {
methodName = method;
let registration =
await navigator.serviceWorker.getRegistration(swSrcUrl);
await navigator.serviceWorker.getRegistration(SW_SRC_URL);
if (registration) {
return 'The payment handler is already installed.';
}
await navigator.serviceWorker.register(swSrcUrl);
await navigator.serviceWorker.register(SW_SRC_URL);
registration = await navigator.serviceWorker.ready;
if (!registration.paymentManager) {
return 'PaymentManager API not found.';
......@@ -24,7 +32,7 @@ async function install() { // eslint-disable-line no-unused-vars
await registration.paymentManager.instruments.set('instrument-id', {
name: 'Instrument Name',
method: methodName,
method,
});
return 'success';
} catch (e) {
......@@ -32,16 +40,34 @@ async function install() { // eslint-disable-line no-unused-vars
}
}
/**
* Uninstalls the payment handler.
* @return {Promise<string>} - 'success' or error message on failure.
*/
async function uninstall() { // eslint-disable-line no-unused-vars
try {
let registration =
await navigator.serviceWorker.getRegistration(SW_SRC_URL);
if (!registration) {
return 'The Payment handler has not been installed yet.';
}
await registration.unregister();
return 'success';
} catch (e) {
return e.toString();
}
}
/**
* Delegates handling of the provided options to the payment handler.
* @param {Array<string>} delegations The list of payment options to delegate.
* @return {string} The 'success' or error message.
* @return {Promise<string>} - 'success' or error message on failure.
*/
async function enableDelegations(delegations) { // eslint-disable-line no-unused-vars, max-len
try {
await navigator.serviceWorker.ready;
let registration =
await navigator.serviceWorker.getRegistration(swSrcUrl);
await navigator.serviceWorker.getRegistration(SW_SRC_URL);
if (!registration) {
return 'The payment handler is not installed.';
}
......@@ -61,6 +87,7 @@ async function enableDelegations(delegations) { // eslint-disable-line no-unused
/**
* Launches the payment handler.
* @return {Promise<string>} - 'success' or error message on failure.
*/
async function launch() { // eslint-disable-line no-unused-vars
try {
......@@ -81,7 +108,7 @@ async function launch() { // eslint-disable-line no-unused-vars
*/
function launchWithoutWaitForResponse() { // eslint-disable-line no-unused-vars
try {
const request = new PaymentRequest([{supportedMethods: methodName}], {
request = new PaymentRequest([{supportedMethods: methodName}], {
total: {label: 'Total', amount: {currency: 'USD', value: '0.01'}},
});
request.show();
......@@ -91,6 +118,19 @@ function launchWithoutWaitForResponse() { // eslint-disable-line no-unused-vars
}
}
/**
* Aborts the on-going payment request.
* @return {Promise<string>} - 'success' or error message on failure.
*/
async function abort() { // eslint-disable-line no-unused-vars
try {
await request.abort();
return 'success';
} catch (e) {
return e.toString();
}
}
var paymentOptions = null;
/**
......
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