OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <cstddef> | 5 #include <cstddef> |
6 #include <string> | 6 #include <string> |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "components/payments/mojom/payment_app.mojom.h" | 11 #include "components/payments/mojom/payment_app.mojom.h" |
12 #include "content/browser/payments/payment_app_content_unittest_base.h" | 12 #include "content/browser/payments/payment_app_content_unittest_base.h" |
13 #include "content/browser/payments/payment_app_provider_impl.h" | 13 #include "content/browser/payments/payment_app_provider_impl.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
16 | 16 |
17 using payments::mojom::PaymentAppManifestError; | |
18 using payments::mojom::PaymentAppManifestPtr; | |
19 | |
20 namespace content { | 17 namespace content { |
21 | 18 |
22 class PaymentManager; | 19 class PaymentManager; |
23 | 20 |
24 namespace { | 21 namespace { |
25 | 22 |
26 using ::payments::mojom::PaymentHandlerStatus; | 23 using ::payments::mojom::PaymentHandlerStatus; |
27 using ::payments::mojom::PaymentInstrument; | 24 using ::payments::mojom::PaymentInstrument; |
28 using ::payments::mojom::PaymentInstrumentPtr; | 25 using ::payments::mojom::PaymentInstrumentPtr; |
29 | 26 |
30 void SetManifestCallback(bool* called, | |
31 PaymentAppManifestError* out_error, | |
32 PaymentAppManifestError error) { | |
33 *called = true; | |
34 *out_error = error; | |
35 } | |
36 | |
37 void GetAllManifestsCallback(bool* called, | |
38 PaymentAppProvider::Manifests* out_manifests, | |
39 PaymentAppProvider::Manifests manifests) { | |
40 *called = true; | |
41 *out_manifests = std::move(manifests); | |
42 } | |
43 | |
44 void SetPaymentInstrumentCallback(PaymentHandlerStatus* out_status, | 27 void SetPaymentInstrumentCallback(PaymentHandlerStatus* out_status, |
45 PaymentHandlerStatus status) { | 28 PaymentHandlerStatus status) { |
46 *out_status = status; | 29 *out_status = status; |
47 } | 30 } |
48 | 31 |
49 void GetAllPaymentAppsCallback(PaymentAppProvider::PaymentApps* out_apps, | 32 void GetAllPaymentAppsCallback(PaymentAppProvider::PaymentApps* out_apps, |
50 PaymentAppProvider::PaymentApps apps) { | 33 PaymentAppProvider::PaymentApps apps) { |
51 *out_apps = std::move(apps); | 34 *out_apps = std::move(apps); |
52 } | 35 } |
53 | 36 |
54 void InvokePaymentAppCallback(bool* called, | 37 void InvokePaymentAppCallback(bool* called, |
55 payments::mojom::PaymentAppResponsePtr response) { | 38 payments::mojom::PaymentAppResponsePtr response) { |
56 *called = true; | 39 *called = true; |
57 } | 40 } |
58 | 41 |
59 } // namespace | 42 } // namespace |
60 | 43 |
61 class PaymentAppProviderTest : public PaymentAppContentUnitTestBase { | 44 class PaymentAppProviderTest : public PaymentAppContentUnitTestBase { |
62 public: | 45 public: |
63 PaymentAppProviderTest() {} | 46 PaymentAppProviderTest() {} |
64 ~PaymentAppProviderTest() override {} | 47 ~PaymentAppProviderTest() override {} |
65 | 48 |
66 void GetAllManifests(PaymentAppProvider::GetAllManifestsCallback callback) { | |
67 PaymentAppProviderImpl::GetInstance()->GetAllManifests(browser_context(), | |
68 callback); | |
69 base::RunLoop().RunUntilIdle(); | |
70 } | |
71 | |
72 void SetPaymentInstrument( | 49 void SetPaymentInstrument( |
73 PaymentManager* manager, | 50 PaymentManager* manager, |
74 const std::string& instrument_key, | 51 const std::string& instrument_key, |
75 PaymentInstrumentPtr instrument, | 52 PaymentInstrumentPtr instrument, |
76 PaymentManager::SetPaymentInstrumentCallback callback) { | 53 PaymentManager::SetPaymentInstrumentCallback callback) { |
77 ASSERT_NE(nullptr, manager); | 54 ASSERT_NE(nullptr, manager); |
78 manager->SetPaymentInstrument(instrument_key, std::move(instrument), | 55 manager->SetPaymentInstrument(instrument_key, std::move(instrument), |
79 std::move(callback)); | 56 std::move(callback)); |
80 base::RunLoop().RunUntilIdle(); | 57 base::RunLoop().RunUntilIdle(); |
81 } | 58 } |
82 | 59 |
83 void GetAllPaymentApps( | 60 void GetAllPaymentApps( |
84 PaymentAppProvider::GetAllPaymentAppsCallback callback) { | 61 PaymentAppProvider::GetAllPaymentAppsCallback callback) { |
85 PaymentAppProviderImpl::GetInstance()->GetAllPaymentApps( | 62 PaymentAppProviderImpl::GetInstance()->GetAllPaymentApps( |
86 browser_context(), std::move(callback)); | 63 browser_context(), std::move(callback)); |
87 base::RunLoop().RunUntilIdle(); | 64 base::RunLoop().RunUntilIdle(); |
88 } | 65 } |
89 | 66 |
90 void InvokePaymentApp(int64_t registration_id, | 67 void InvokePaymentApp(int64_t registration_id, |
91 payments::mojom::PaymentAppRequestPtr app_request, | 68 payments::mojom::PaymentAppRequestPtr app_request, |
92 PaymentAppProvider::InvokePaymentAppCallback callback) { | 69 PaymentAppProvider::InvokePaymentAppCallback callback) { |
93 PaymentAppProviderImpl::GetInstance()->InvokePaymentApp( | 70 PaymentAppProviderImpl::GetInstance()->InvokePaymentApp( |
94 browser_context(), registration_id, std::move(app_request), callback); | 71 browser_context(), registration_id, std::move(app_request), callback); |
95 base::RunLoop().RunUntilIdle(); | 72 base::RunLoop().RunUntilIdle(); |
96 } | 73 } |
97 | 74 |
98 void CreatePaymentApp(const GURL& scope_url, const GURL& sw_script_url) { | |
99 PaymentManager* manager = CreatePaymentManager(scope_url, sw_script_url); | |
100 | |
101 PaymentAppManifestError error = | |
102 PaymentAppManifestError::MANIFEST_STORAGE_OPERATION_FAILED; | |
103 bool called = false; | |
104 SetManifest(manager, CreatePaymentAppManifestForTest(scope_url.spec()), | |
105 base::Bind(&SetManifestCallback, &called, &error)); | |
106 ASSERT_TRUE(called); | |
107 | |
108 ASSERT_EQ(PaymentAppManifestError::NONE, error); | |
109 } | |
110 | |
111 private: | 75 private: |
112 DISALLOW_COPY_AND_ASSIGN(PaymentAppProviderTest); | 76 DISALLOW_COPY_AND_ASSIGN(PaymentAppProviderTest); |
113 }; | 77 }; |
114 | 78 |
115 TEST_F(PaymentAppProviderTest, GetAllManifestsTest) { | |
116 static const struct { | |
117 const char* scopeUrl; | |
118 const char* scriptUrl; | |
119 } kPaymentAppInfo[] = { | |
120 {"https://example.com/a", "https://example.com/a/script.js"}, | |
121 {"https://example.com/b", "https://example.com/b/script.js"}, | |
122 {"https://example.com/c", "https://example.com/c/script.js"}}; | |
123 | |
124 for (size_t i = 0; i < arraysize(kPaymentAppInfo); i++) { | |
125 CreatePaymentApp(GURL(kPaymentAppInfo[i].scopeUrl), | |
126 GURL(kPaymentAppInfo[i].scriptUrl)); | |
127 } | |
128 | |
129 PaymentAppProvider::Manifests manifests; | |
130 bool called = false; | |
131 GetAllManifests(base::Bind(&GetAllManifestsCallback, &called, &manifests)); | |
132 ASSERT_TRUE(called); | |
133 | |
134 ASSERT_EQ(3U, manifests.size()); | |
135 size_t i = 0; | |
136 for (const auto& manifest : manifests) { | |
137 EXPECT_EQ("payment-app-icon", manifest.second->icon.value()); | |
138 EXPECT_EQ(kPaymentAppInfo[i++].scopeUrl, manifest.second->name); | |
139 ASSERT_EQ(1U, manifest.second->options.size()); | |
140 EXPECT_EQ("payment-app-icon", manifest.second->options[0]->icon.value()); | |
141 EXPECT_EQ("Visa ****", manifest.second->options[0]->name); | |
142 EXPECT_EQ("payment-app-id", manifest.second->options[0]->id); | |
143 ASSERT_EQ(1U, manifest.second->options[0]->enabled_methods.size()); | |
144 EXPECT_EQ("visa", manifest.second->options[0]->enabled_methods[0]); | |
145 } | |
146 } | |
147 | |
148 TEST_F(PaymentAppProviderTest, InvokePaymentAppTest) { | 79 TEST_F(PaymentAppProviderTest, InvokePaymentAppTest) { |
149 PaymentManager* manager1 = CreatePaymentManager( | 80 PaymentManager* manager1 = CreatePaymentManager( |
150 GURL("https://hellopay.com/a"), GURL("https://hellopay.com/a/script.js")); | 81 GURL("https://hellopay.com/a"), GURL("https://hellopay.com/a/script.js")); |
151 PaymentManager* manager2 = CreatePaymentManager( | 82 PaymentManager* manager2 = CreatePaymentManager( |
152 GURL("https://bobpay.com/b"), GURL("https://bobpay.com/b/script.js")); | 83 GURL("https://bobpay.com/b"), GURL("https://bobpay.com/b/script.js")); |
153 | 84 |
154 PaymentHandlerStatus status; | 85 PaymentHandlerStatus status; |
155 SetPaymentInstrument(manager1, "test_key1", | 86 SetPaymentInstrument(manager1, "test_key1", |
156 payments::mojom::PaymentInstrument::New(), | 87 payments::mojom::PaymentInstrument::New(), |
157 base::Bind(&SetPaymentInstrumentCallback, &status)); | 88 base::Bind(&SetPaymentInstrumentCallback, &status)); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 | 129 |
199 PaymentAppProvider::PaymentApps apps; | 130 PaymentAppProvider::PaymentApps apps; |
200 GetAllPaymentApps(base::Bind(&GetAllPaymentAppsCallback, &apps)); | 131 GetAllPaymentApps(base::Bind(&GetAllPaymentAppsCallback, &apps)); |
201 | 132 |
202 ASSERT_EQ(2U, apps.size()); | 133 ASSERT_EQ(2U, apps.size()); |
203 ASSERT_EQ(1U, apps[GURL("https://hellopay.com/")].size()); | 134 ASSERT_EQ(1U, apps[GURL("https://hellopay.com/")].size()); |
204 ASSERT_EQ(2U, apps[GURL("https://bobpay.com/")].size()); | 135 ASSERT_EQ(2U, apps[GURL("https://bobpay.com/")].size()); |
205 } | 136 } |
206 | 137 |
207 } // namespace content | 138 } // namespace content |
OLD | NEW |