1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/payments/content/service_worker_payment_app_finder.h"
6 
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "third_party/blink/public/mojom/payments/payment_request.mojom.h"
9 
10 namespace payments {
11 
12 class ServiceWorkerPaymentAppFinderTest : public testing::Test {
13  protected:
RemoveAppsWithoutMatchingMethodData(const std::vector<mojom::PaymentMethodDataPtr> & requested_method_data,content::InstalledPaymentAppsFinder::PaymentApps * apps)14   void RemoveAppsWithoutMatchingMethodData(
15       const std::vector<mojom::PaymentMethodDataPtr>& requested_method_data,
16       content::InstalledPaymentAppsFinder::PaymentApps* apps) {
17     ServiceWorkerPaymentAppFinder::RemoveAppsWithoutMatchingMethodData(
18         requested_method_data, apps);
19   }
20 };
21 
TEST_F(ServiceWorkerPaymentAppFinderTest,RemoveAppsWithoutMatchingMethodData_NoApps)22 TEST_F(ServiceWorkerPaymentAppFinderTest,
23        RemoveAppsWithoutMatchingMethodData_NoApps) {
24   std::vector<mojom::PaymentMethodDataPtr> requested_methods;
25   requested_methods.emplace_back(mojom::PaymentMethodData::New());
26   requested_methods.back()->supported_method = "method";
27   content::InstalledPaymentAppsFinder::PaymentApps no_apps;
28 
29   RemoveAppsWithoutMatchingMethodData(requested_methods, &no_apps);
30 
31   EXPECT_TRUE(no_apps.empty());
32 }
33 
TEST_F(ServiceWorkerPaymentAppFinderTest,RemoveAppsWithoutMatchingMethodData_NoMethods)34 TEST_F(ServiceWorkerPaymentAppFinderTest,
35        RemoveAppsWithoutMatchingMethodData_NoMethods) {
36   std::vector<mojom::PaymentMethodDataPtr> no_requested_methods;
37   content::InstalledPaymentAppsFinder::PaymentApps apps;
38   apps[0] = std::make_unique<content::StoredPaymentApp>();
39   apps[0]->enabled_methods = {"method1", "method2"};
40 
41   RemoveAppsWithoutMatchingMethodData(no_requested_methods, &apps);
42 
43   EXPECT_TRUE(apps.empty());
44 }
45 
TEST_F(ServiceWorkerPaymentAppFinderTest,RemoveAppsWithoutMatchingMethodData_IntersectionOfMethods)46 TEST_F(ServiceWorkerPaymentAppFinderTest,
47        RemoveAppsWithoutMatchingMethodData_IntersectionOfMethods) {
48   std::vector<mojom::PaymentMethodDataPtr> requested_methods;
49   requested_methods.emplace_back(mojom::PaymentMethodData::New());
50   requested_methods.back()->supported_method = "method1";
51   requested_methods.emplace_back(mojom::PaymentMethodData::New());
52   requested_methods.back()->supported_method = "method2";
53   requested_methods.emplace_back(mojom::PaymentMethodData::New());
54   requested_methods.back()->supported_method = "method3";
55   content::InstalledPaymentAppsFinder::PaymentApps apps;
56   apps[0] = std::make_unique<content::StoredPaymentApp>();
57   apps[0]->enabled_methods = {"method2"};
58   apps[1] = std::make_unique<content::StoredPaymentApp>();
59   apps[1]->enabled_methods = {"method3"};
60   apps[2] = std::make_unique<content::StoredPaymentApp>();
61   apps[2]->enabled_methods = {"method4"};
62 
63   RemoveAppsWithoutMatchingMethodData(requested_methods, &apps);
64 
65   EXPECT_EQ(2U, apps.size());
66   ASSERT_NE(apps.end(), apps.find(0));
67   EXPECT_EQ(std::vector<std::string>{"method2"},
68             apps.find(0)->second->enabled_methods);
69   ASSERT_NE(apps.end(), apps.find(1));
70   EXPECT_EQ(std::vector<std::string>{"method3"},
71             apps.find(1)->second->enabled_methods);
72 }
73 
TEST_F(ServiceWorkerPaymentAppFinderTest,RemoveAppsWithoutMatchingMethodData_NoCapabilitiesNetworksOrTypes)74 TEST_F(ServiceWorkerPaymentAppFinderTest,
75        RemoveAppsWithoutMatchingMethodData_NoCapabilitiesNetworksOrTypes) {
76   std::vector<mojom::PaymentMethodDataPtr> requested_methods;
77   requested_methods.emplace_back(mojom::PaymentMethodData::New());
78   requested_methods.back()->supported_method = "basic-card";
79   content::InstalledPaymentAppsFinder::PaymentApps apps;
80   apps[0] = std::make_unique<content::StoredPaymentApp>();
81   apps[0]->enabled_methods = {"basic-card"};
82 
83   RemoveAppsWithoutMatchingMethodData(requested_methods, &apps);
84 
85   EXPECT_EQ(1U, apps.size());
86   ASSERT_NE(apps.end(), apps.find(0));
87   EXPECT_EQ(std::vector<std::string>{"basic-card"},
88             apps.find(0)->second->enabled_methods);
89 }
90 
TEST_F(ServiceWorkerPaymentAppFinderTest,RemoveAppsWithoutMatchingMethodData_NoNetworkCapabilities)91 TEST_F(ServiceWorkerPaymentAppFinderTest,
92        RemoveAppsWithoutMatchingMethodData_NoNetworkCapabilities) {
93   std::vector<mojom::PaymentMethodDataPtr> requested_methods;
94   requested_methods.emplace_back(mojom::PaymentMethodData::New());
95   requested_methods.back()->supported_method = "basic-card";
96   requested_methods.back()->supported_networks = {
97       mojom::BasicCardNetwork::AMEX};
98   content::InstalledPaymentAppsFinder::PaymentApps apps;
99   apps[0] = std::make_unique<content::StoredPaymentApp>();
100   apps[0]->enabled_methods = {"basic-card"};
101 
102   RemoveAppsWithoutMatchingMethodData(requested_methods, &apps);
103 
104   EXPECT_TRUE(apps.empty());
105 }
106 
TEST_F(ServiceWorkerPaymentAppFinderTest,RemoveAppsWithoutMatchingMethodData_NoMatchingNetworkCapabilities)107 TEST_F(ServiceWorkerPaymentAppFinderTest,
108        RemoveAppsWithoutMatchingMethodData_NoMatchingNetworkCapabilities) {
109   std::vector<mojom::PaymentMethodDataPtr> requested_methods;
110   requested_methods.emplace_back(mojom::PaymentMethodData::New());
111   requested_methods.back()->supported_method = "basic-card";
112   requested_methods.back()->supported_networks = {
113       mojom::BasicCardNetwork::AMEX};
114   content::InstalledPaymentAppsFinder::PaymentApps apps;
115   apps[0] = std::make_unique<content::StoredPaymentApp>();
116   apps[0]->enabled_methods = {"basic-card"};
117   apps[0]->capabilities.emplace_back();
118   apps[0]->capabilities.back().supported_card_networks = {
119       static_cast<int32_t>(mojom::BasicCardNetwork::VISA)};
120 
121   RemoveAppsWithoutMatchingMethodData(requested_methods, &apps);
122 
123   EXPECT_TRUE(apps.empty());
124 }
125 
TEST_F(ServiceWorkerPaymentAppFinderTest,RemoveAppsWithoutMatchingMethodData_NoRequestedNetwork)126 TEST_F(ServiceWorkerPaymentAppFinderTest,
127        RemoveAppsWithoutMatchingMethodData_NoRequestedNetwork) {
128   std::vector<mojom::PaymentMethodDataPtr> requested_methods;
129   requested_methods.emplace_back(mojom::PaymentMethodData::New());
130   requested_methods.back()->supported_method = "basic-card";
131   content::InstalledPaymentAppsFinder::PaymentApps apps;
132   apps[0] = std::make_unique<content::StoredPaymentApp>();
133   apps[0]->enabled_methods = {"basic-card"};
134   apps[0]->capabilities.emplace_back();
135   apps[0]->capabilities.back().supported_card_networks = {
136       static_cast<int32_t>(mojom::BasicCardNetwork::VISA)};
137 
138   RemoveAppsWithoutMatchingMethodData(requested_methods, &apps);
139 
140   EXPECT_EQ(1U, apps.size());
141   ASSERT_NE(apps.end(), apps.find(0));
142   const auto& actual = apps.find(0)->second;
143   EXPECT_EQ(std::vector<std::string>{"basic-card"}, actual->enabled_methods);
144   ASSERT_EQ(1U, actual->capabilities.size());
145   const auto& capability = actual->capabilities.back();
146   ASSERT_EQ(1U, actual->capabilities.back().supported_card_networks.size());
147   EXPECT_EQ(static_cast<int32_t>(mojom::BasicCardNetwork::VISA),
148             capability.supported_card_networks[0]);
149 }
150 
TEST_F(ServiceWorkerPaymentAppFinderTest,RemoveAppsWithoutMatchingMethodData_IntersectionOfNetworks)151 TEST_F(ServiceWorkerPaymentAppFinderTest,
152        RemoveAppsWithoutMatchingMethodData_IntersectionOfNetworks) {
153   std::vector<mojom::PaymentMethodDataPtr> requested_methods;
154   requested_methods.emplace_back(mojom::PaymentMethodData::New());
155   requested_methods.back()->supported_method = "basic-card";
156   requested_methods.back()->supported_networks = {
157       mojom::BasicCardNetwork::AMEX, mojom::BasicCardNetwork::DINERS};
158   content::InstalledPaymentAppsFinder::PaymentApps apps;
159   apps[0] = std::make_unique<content::StoredPaymentApp>();
160   apps[0]->enabled_methods = {"basic-card"};
161   apps[0]->capabilities.emplace_back();
162   apps[0]->capabilities.back().supported_card_networks = {
163       static_cast<int32_t>(mojom::BasicCardNetwork::DINERS),
164       static_cast<int32_t>(mojom::BasicCardNetwork::VISA)};
165 
166   RemoveAppsWithoutMatchingMethodData(requested_methods, &apps);
167 
168   EXPECT_EQ(1U, apps.size());
169   ASSERT_NE(apps.end(), apps.find(0));
170   const auto& actual = apps.find(0)->second;
171   EXPECT_EQ(std::vector<std::string>{"basic-card"}, actual->enabled_methods);
172   ASSERT_EQ(1U, actual->capabilities.size());
173   const auto& capability = actual->capabilities.back();
174   EXPECT_EQ((std::vector<int32_t>{
175                 static_cast<int32_t>(mojom::BasicCardNetwork::DINERS),
176                 static_cast<int32_t>(mojom::BasicCardNetwork::VISA)}),
177             capability.supported_card_networks);
178 }
179 
TEST_F(ServiceWorkerPaymentAppFinderTest,RemoveAppsWithoutMatchingMethodData_NonBasicCardIgnoresCapabilities)180 TEST_F(ServiceWorkerPaymentAppFinderTest,
181        RemoveAppsWithoutMatchingMethodData_NonBasicCardIgnoresCapabilities) {
182   std::vector<mojom::PaymentMethodDataPtr> requested_methods;
183   requested_methods.emplace_back(mojom::PaymentMethodData::New());
184   requested_methods.back()->supported_method = "unknown-method";
185   content::InstalledPaymentAppsFinder::PaymentApps apps;
186   apps[0] = std::make_unique<content::StoredPaymentApp>();
187   apps[0]->enabled_methods = {"unknown-method"};
188   apps[0]->capabilities.emplace_back();
189 
190   RemoveAppsWithoutMatchingMethodData(requested_methods, &apps);
191 
192   EXPECT_EQ(1U, apps.size());
193   ASSERT_NE(apps.end(), apps.find(0));
194   EXPECT_EQ(std::vector<std::string>{"unknown-method"},
195             apps.find(0)->second->enabled_methods);
196 }
197 
198 }  // namespace payments
199