1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <utility>
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "content/browser/payments/payment_app_content_unittest_base.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/payments/payment_app.mojom.h"
#include "url/gurl.h"
namespace content {
namespace {
using ::payments::mojom::PaymentHandlerStatus;
using ::payments::mojom::PaymentInstrument;
using ::payments::mojom::PaymentInstrumentPtr;
const char kServiceWorkerScope[] = "https://example.test/a/";
const char kServiceWorkerScript[] = "https://example.test/a/script.js";
const char kServiceWorkerScope2[] = "https://example.test/b/";
const char kServiceWorkerScript2[] = "https://example.test/b/script.js";
void DeletePaymentInstrumentCallback(PaymentHandlerStatus* out_status,
PaymentHandlerStatus status) {
*out_status = status;
}
void SetPaymentInstrumentCallback(PaymentHandlerStatus* out_status,
PaymentHandlerStatus status) {
*out_status = status;
}
void KeysOfPaymentInstrumentsCallback(std::vector<std::string>* out_keys,
PaymentHandlerStatus* out_status,
const std::vector<std::string>& keys,
PaymentHandlerStatus status) {
*out_keys = keys;
*out_status = status;
}
void HasPaymentInstrumentCallback(PaymentHandlerStatus* out_status,
PaymentHandlerStatus status) {
*out_status = status;
}
void GetPaymentInstrumentCallback(PaymentInstrumentPtr* out_instrument,
PaymentHandlerStatus* out_status,
PaymentInstrumentPtr instrument,
PaymentHandlerStatus status) {
*out_instrument = std::move(instrument);
*out_status = status;
}
void ClearPaymentInstrumentsCallback(PaymentHandlerStatus* out_status,
PaymentHandlerStatus status) {
*out_status = status;
}
} // namespace
class PaymentManagerTest : public PaymentAppContentUnitTestBase {
public:
PaymentManagerTest() {
manager_ = CreatePaymentManager(GURL(kServiceWorkerScope),
GURL(kServiceWorkerScript));
EXPECT_NE(nullptr, manager_);
}
PaymentManagerTest(const PaymentManagerTest&) = delete;
PaymentManagerTest& operator=(const PaymentManagerTest&) = delete;
PaymentManager* payment_manager() const { return manager_; }
void DeletePaymentInstrument(const std::string& instrument_key,
PaymentHandlerStatus* out_status) {
manager_->DeletePaymentInstrument(
instrument_key,
base::BindOnce(&DeletePaymentInstrumentCallback, out_status));
base::RunLoop().RunUntilIdle();
}
void SetPaymentInstrument(const std::string& instrument_key,
PaymentInstrumentPtr instrument,
PaymentHandlerStatus* out_status) {
manager_->SetPaymentInstrument(
instrument_key, std::move(instrument),
base::BindOnce(&SetPaymentInstrumentCallback, out_status));
base::RunLoop().RunUntilIdle();
}
void KeysOfPaymentInstruments(std::vector<std::string>* out_keys,
PaymentHandlerStatus* out_status) {
manager_->KeysOfPaymentInstruments(base::BindOnce(
&KeysOfPaymentInstrumentsCallback, out_keys, out_status));
base::RunLoop().RunUntilIdle();
}
void HasPaymentInstrument(const std::string& instrument_key,
PaymentHandlerStatus* out_status) {
manager_->HasPaymentInstrument(
instrument_key,
base::BindOnce(&HasPaymentInstrumentCallback, out_status));
base::RunLoop().RunUntilIdle();
}
void GetPaymentInstrument(const std::string& instrument_key,
PaymentInstrumentPtr* out_instrument,
PaymentHandlerStatus* out_status) {
manager_->GetPaymentInstrument(instrument_key,
base::BindOnce(&GetPaymentInstrumentCallback,
out_instrument, out_status));
base::RunLoop().RunUntilIdle();
}
void ClearPaymentInstruments(PaymentHandlerStatus* out_status) {
manager_->ClearPaymentInstruments(
base::BindOnce(&ClearPaymentInstrumentsCallback, out_status));
base::RunLoop().RunUntilIdle();
}
protected:
// Owned by payment_app_context_.
raw_ptr<PaymentManager> manager_;
};
TEST_F(PaymentManagerTest, GetUnstoredPaymentInstrument) {
PaymentHandlerStatus read_status = PaymentHandlerStatus::SUCCESS;
PaymentInstrumentPtr read_details;
GetPaymentInstrument("test_key", &read_details, &read_status);
ASSERT_EQ(PaymentHandlerStatus::NOT_FOUND, read_status);
}
TEST_F(PaymentManagerTest, DeletePaymentInstrument) {
PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
PaymentInstrumentPtr write_details = PaymentInstrument::New();
write_details->name = "Visa ending ****4756";
write_details->method = "visa";
SetPaymentInstrument("test_key", std::move(write_details), &write_status);
// Write the first instrument of a web payment app will return
// FETCH_PAYMENT_APP_INFO_FAILED since the web app's manifest is not
// available, but the write of the instrument is succeed, otherwise will
// return the other errors.
ASSERT_EQ(PaymentHandlerStatus::FETCH_PAYMENT_APP_INFO_FAILED, write_status);
PaymentHandlerStatus read_status = PaymentHandlerStatus::NOT_FOUND;
PaymentInstrumentPtr read_details;
GetPaymentInstrument("test_key", &read_details, &read_status);
ASSERT_EQ(PaymentHandlerStatus::SUCCESS, read_status);
PaymentHandlerStatus delete_status = PaymentHandlerStatus::NOT_FOUND;
DeletePaymentInstrument("test_key", &delete_status);
ASSERT_EQ(PaymentHandlerStatus::SUCCESS, delete_status);
read_status = PaymentHandlerStatus::NOT_FOUND;
GetPaymentInstrument("test_key", &read_details, &read_status);
ASSERT_EQ(PaymentHandlerStatus::NOT_FOUND, read_status);
}
TEST_F(PaymentManagerTest, HasPaymentInstrument) {
PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
PaymentInstrumentPtr write_details = PaymentInstrument::New();
write_details->name = "Visa ending ****4756";
write_details->method = "visa";
SetPaymentInstrument("test_key", std::move(write_details), &write_status);
// Write the first instrument of a web payment app will return
// FETCH_PAYMENT_APP_INFO_FAILED since the web app's manifest is not
// available, but the write of the instrument is succeed, otherwise will
// return the other errors.
ASSERT_EQ(PaymentHandlerStatus::FETCH_PAYMENT_APP_INFO_FAILED, write_status);
PaymentHandlerStatus has_status = PaymentHandlerStatus::NOT_FOUND;
HasPaymentInstrument("test_key", &has_status);
ASSERT_EQ(PaymentHandlerStatus::SUCCESS, has_status);
HasPaymentInstrument("unstored_test_key", &has_status);
ASSERT_EQ(PaymentHandlerStatus::NOT_FOUND, has_status);
}
TEST_F(PaymentManagerTest, KeysOfPaymentInstruments) {
PaymentHandlerStatus keys_status = PaymentHandlerStatus::NOT_FOUND;
std::vector<std::string> keys;
KeysOfPaymentInstruments(&keys, &keys_status);
ASSERT_EQ(PaymentHandlerStatus::SUCCESS, keys_status);
ASSERT_EQ(0U, keys.size());
{
PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
SetPaymentInstrument("test_key1", PaymentInstrument::New(), &write_status);
// Write the first instrument of a web payment app will return
// FETCH_PAYMENT_APP_INFO_FAILED since the web app's manifest is not
// available, but the write of the instrument is succeed, otherwise will
// return the other errors.
ASSERT_EQ(PaymentHandlerStatus::FETCH_PAYMENT_APP_INFO_FAILED,
write_status);
}
{
PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
SetPaymentInstrument("test_key3", PaymentInstrument::New(), &write_status);
ASSERT_EQ(PaymentHandlerStatus::SUCCESS, write_status);
}
{
PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
SetPaymentInstrument("test_key2", PaymentInstrument::New(), &write_status);
ASSERT_EQ(PaymentHandlerStatus::SUCCESS, write_status);
}
keys_status = PaymentHandlerStatus::NOT_FOUND;
KeysOfPaymentInstruments(&keys, &keys_status);
ASSERT_EQ(PaymentHandlerStatus::SUCCESS, keys_status);
ASSERT_EQ(3U, keys.size());
ASSERT_EQ("test_key1", keys[0]);
ASSERT_EQ("test_key3", keys[1]);
ASSERT_EQ("test_key2", keys[2]);
}
TEST_F(PaymentManagerTest, ClearPaymentInstruments) {
PaymentHandlerStatus status = PaymentHandlerStatus::NOT_FOUND;
std::vector<std::string> keys;
KeysOfPaymentInstruments(&keys, &status);
ASSERT_EQ(PaymentHandlerStatus::SUCCESS, status);
ASSERT_EQ(0U, keys.size());
{
PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
SetPaymentInstrument("test_key1", PaymentInstrument::New(), &write_status);
// Write the first instrument of a web payment app will return
// FETCH_PAYMENT_APP_INFO_FAILED since the web app's manifest is not
// available, but the write of the instrument is succeed, otherwise will
// return the other errors.
ASSERT_EQ(PaymentHandlerStatus::FETCH_PAYMENT_APP_INFO_FAILED,
write_status);
}
{
PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
SetPaymentInstrument("test_key3", PaymentInstrument::New(), &write_status);
ASSERT_EQ(PaymentHandlerStatus::SUCCESS, write_status);
}
{
PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
SetPaymentInstrument("test_key2", PaymentInstrument::New(), &write_status);
ASSERT_EQ(PaymentHandlerStatus::SUCCESS, write_status);
}
status = PaymentHandlerStatus::NOT_FOUND;
KeysOfPaymentInstruments(&keys, &status);
ASSERT_EQ(PaymentHandlerStatus::SUCCESS, status);
ASSERT_EQ(3U, keys.size());
status = PaymentHandlerStatus::NOT_FOUND;
ClearPaymentInstruments(&status);
ASSERT_EQ(PaymentHandlerStatus::SUCCESS, status);
status = PaymentHandlerStatus::NOT_FOUND;
KeysOfPaymentInstruments(&keys, &status);
ASSERT_EQ(PaymentHandlerStatus::SUCCESS, status);
ASSERT_EQ(0U, keys.size());
}
TEST_F(PaymentManagerTest, SetAndGetPaymentInstrument) {
PaymentHandlerStatus write_status = PaymentHandlerStatus::NOT_FOUND;
PaymentInstrumentPtr write_details = PaymentInstrument::New();
write_details->name = "ChromePay: chrome@chromepay.test";
write_details->method = "https://www.chromium.org";
SetPaymentInstrument("test_key", std::move(write_details), &write_status);
// Write the first instrument of a web payment app will return
// FETCH_PAYMENT_APP_INFO_FAILED since the web app's manifest is not
// available, but the write of the instrument is succeed, otherwise will
// return the other errors.
ASSERT_EQ(PaymentHandlerStatus::FETCH_PAYMENT_APP_INFO_FAILED, write_status);
PaymentHandlerStatus read_status = PaymentHandlerStatus::NOT_FOUND;
PaymentInstrumentPtr read_details;
GetPaymentInstrument("test_key", &read_details, &read_status);
ASSERT_EQ(PaymentHandlerStatus::SUCCESS, read_status);
EXPECT_EQ("ChromePay: chrome@chromepay.test", read_details->name);
EXPECT_EQ("https://www.chromium.org", read_details->method);
}
TEST_F(PaymentManagerTest, UninitializedPaymentManager) {
manager_ = CreateUninitializedPaymentManager(GURL(kServiceWorkerScope2),
GURL(kServiceWorkerScript2));
// Test that calling the payment manager does not crash, and instead
// disconnects due to the invalid state (Init not called).
PaymentHandlerStatus status = PaymentHandlerStatus::NOT_FOUND;
DeletePaymentInstrument("test_key", &status);
ASSERT_EQ(PaymentHandlerStatus::NOT_FOUND, status);
PaymentInstrumentPtr instrument;
GetPaymentInstrument("test_key", &instrument, &status);
ASSERT_EQ(PaymentHandlerStatus::NOT_FOUND, status);
std::vector<std::string> keys;
KeysOfPaymentInstruments(&keys, &status);
HasPaymentInstrument("test_key", &status);
ASSERT_EQ(PaymentHandlerStatus::NOT_FOUND, status);
SetPaymentInstrument("test_key", PaymentInstrument::New(), &status);
ASSERT_EQ(PaymentHandlerStatus::NOT_FOUND, status);
ClearPaymentInstruments(&status);
ASSERT_EQ(PaymentHandlerStatus::NOT_FOUND, status);
}
} // namespace content
|