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 "content/browser/payments/payment_app_manager.h" | 5 #include "content/browser/payments/payment_app_manager.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "components/payments/payment_app.mojom.h" | 14 #include "components/payments/payment_app.mojom.h" |
15 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 15 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
16 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 16 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
17 #include "content/browser/storage_partition_impl.h" | 17 #include "content/browser/storage_partition_impl.h" |
18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/test/test_browser_context.h" | 19 #include "content/public/test/test_browser_context.h" |
20 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
21 #include "mojo/public/cpp/bindings/interface_ptr.h" | 21 #include "mojo/public/cpp/bindings/interface_ptr.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 namespace content { | 24 namespace content { |
25 namespace { | 25 namespace { |
26 | 26 |
27 const char kServiceWorkerPattern[] = "https://example.com/a"; | 27 const char kServiceWorkerPattern[] = "https://example.com/a"; |
28 const char kServiceWorkerScript[] = "https://example.com/a/script.js"; | 28 const char kServiceWorkerScript[] = "https://example.com/a/script.js"; |
| 29 const char kUnregisteredServiceWorkerPattern[] = |
| 30 "https://example.com/unregistered"; |
29 | 31 |
30 void RegisterServiceWorkerCallback(bool* called, | 32 void RegisterServiceWorkerCallback(bool* called, |
31 int64_t* store_registration_id, | 33 int64_t* store_registration_id, |
32 ServiceWorkerStatusCode status, | 34 ServiceWorkerStatusCode status, |
33 const std::string& status_message, | 35 const std::string& status_message, |
34 int64_t registration_id) { | 36 int64_t registration_id) { |
35 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 37 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
36 *called = true; | 38 *called = true; |
37 *store_registration_id = registration_id; | 39 *store_registration_id = registration_id; |
38 } | 40 } |
39 | 41 |
40 void SetManifestCallback(payments::mojom::PaymentAppManifestError* out_error, | 42 void SetManifestCallback(bool* called, |
| 43 payments::mojom::PaymentAppManifestError* out_error, |
41 payments::mojom::PaymentAppManifestError error) { | 44 payments::mojom::PaymentAppManifestError error) { |
| 45 *called = true; |
42 *out_error = error; | 46 *out_error = error; |
43 } | 47 } |
44 | 48 |
45 void GetManifestCallback(payments::mojom::PaymentAppManifestPtr* out_manifest, | 49 void GetManifestCallback(bool* called, |
| 50 payments::mojom::PaymentAppManifestPtr* out_manifest, |
46 payments::mojom::PaymentAppManifestError* out_error, | 51 payments::mojom::PaymentAppManifestError* out_error, |
47 payments::mojom::PaymentAppManifestPtr manifest, | 52 payments::mojom::PaymentAppManifestPtr manifest, |
48 payments::mojom::PaymentAppManifestError error) { | 53 payments::mojom::PaymentAppManifestError error) { |
| 54 *called = true; |
49 *out_manifest = std::move(manifest); | 55 *out_manifest = std::move(manifest); |
50 *out_error = error; | 56 *out_error = error; |
51 } | 57 } |
52 | 58 |
| 59 payments::mojom::PaymentAppManifestPtr CreatePaymentAppManifestForTest() { |
| 60 payments::mojom::PaymentAppOptionPtr option = |
| 61 payments::mojom::PaymentAppOption::New(); |
| 62 option->label = "Visa ****"; |
| 63 option->id = "payment-app-id"; |
| 64 option->icon = std::string("payment-app-icon"); |
| 65 option->enabled_methods.push_back("visa"); |
| 66 |
| 67 payments::mojom::PaymentAppManifestPtr manifest = |
| 68 payments::mojom::PaymentAppManifest::New(); |
| 69 manifest->icon = std::string("payment-app-icon"); |
| 70 manifest->label = "Payment App"; |
| 71 manifest->options.push_back(std::move(option)); |
| 72 |
| 73 return manifest; |
| 74 } |
| 75 |
53 } // namespace | 76 } // namespace |
54 | 77 |
55 class PaymentAppManagerTest : public testing::Test { | 78 class PaymentAppManagerTest : public testing::Test { |
56 public: | 79 public: |
57 PaymentAppManagerTest() | 80 PaymentAppManagerTest() |
58 : thread_bundle_( | 81 : thread_bundle_( |
59 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), | 82 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), |
60 embedded_worker_helper_(new EmbeddedWorkerTestHelper(base::FilePath())), | 83 embedded_worker_helper_(new EmbeddedWorkerTestHelper(base::FilePath())), |
61 storage_partition_impl_(new StoragePartitionImpl( | 84 storage_partition_impl_(new StoragePartitionImpl( |
62 embedded_worker_helper_->browser_context(), | 85 embedded_worker_helper_->browser_context(), |
63 base::FilePath(), nullptr)) { | 86 base::FilePath(), nullptr)), |
64 | 87 sw_registration_id_(0) { |
65 embedded_worker_helper_->context_wrapper()->set_storage_partition( | 88 embedded_worker_helper_->context_wrapper()->set_storage_partition( |
66 storage_partition_impl_.get()); | 89 storage_partition_impl_.get()); |
67 | 90 |
68 payment_app_context_ = | 91 payment_app_context_ = new PaymentAppContextImpl(); |
69 new PaymentAppContextImpl(embedded_worker_helper_->context_wrapper()); | 92 payment_app_context_->Init(embedded_worker_helper_->context_wrapper()); |
70 | 93 |
71 bool called = false; | 94 bool called = false; |
72 embedded_worker_helper_->context()->RegisterServiceWorker( | 95 embedded_worker_helper_->context()->RegisterServiceWorker( |
73 GURL(kServiceWorkerPattern), GURL(kServiceWorkerScript), NULL, | 96 GURL(kServiceWorkerPattern), GURL(kServiceWorkerScript), NULL, |
74 base::Bind(&RegisterServiceWorkerCallback, &called, | 97 base::Bind(&RegisterServiceWorkerCallback, &called, |
75 &sw_registration_id_)); | 98 &sw_registration_id_)); |
76 base::RunLoop().RunUntilIdle(); | 99 base::RunLoop().RunUntilIdle(); |
77 EXPECT_TRUE(called); | 100 EXPECT_TRUE(called); |
78 | 101 |
79 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request = | 102 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request = |
80 mojo::GetProxy(&service_); | 103 mojo::GetProxy(&service_); |
81 payment_app_context_->CreateService(std::move(request)); | 104 payment_app_context_->CreatePaymentAppManager(std::move(request)); |
82 base::RunLoop().RunUntilIdle(); | 105 base::RunLoop().RunUntilIdle(); |
83 | 106 |
84 manager_ = payment_app_context_->services_.begin()->first; | 107 manager_ = payment_app_context_->payment_app_managers_.begin()->first; |
85 EXPECT_TRUE(manager_); | 108 EXPECT_NE(nullptr, manager_); |
86 } | 109 } |
87 | 110 |
88 ~PaymentAppManagerTest() override { | 111 ~PaymentAppManagerTest() override { |
89 payment_app_context_->Shutdown(); | 112 payment_app_context_->Shutdown(); |
90 base::RunLoop().RunUntilIdle(); | 113 base::RunLoop().RunUntilIdle(); |
91 } | 114 } |
92 | 115 |
93 void SetManifest(const std::string& scope, | 116 void SetManifest(const std::string& scope, |
94 payments::mojom::PaymentAppManifestPtr manifest, | 117 payments::mojom::PaymentAppManifestPtr manifest, |
95 const PaymentAppManager::SetManifestCallback& callback) { | 118 const PaymentAppManager::SetManifestCallback& callback) { |
(...skipping 15 matching lines...) Expand all Loading... |
111 scoped_refptr<PaymentAppContextImpl> payment_app_context_; | 134 scoped_refptr<PaymentAppContextImpl> payment_app_context_; |
112 payments::mojom::PaymentAppManagerPtr service_; | 135 payments::mojom::PaymentAppManagerPtr service_; |
113 | 136 |
114 // Owned by payment_app_context_. | 137 // Owned by payment_app_context_. |
115 PaymentAppManager* manager_; | 138 PaymentAppManager* manager_; |
116 | 139 |
117 DISALLOW_COPY_AND_ASSIGN(PaymentAppManagerTest); | 140 DISALLOW_COPY_AND_ASSIGN(PaymentAppManagerTest); |
118 }; | 141 }; |
119 | 142 |
120 TEST_F(PaymentAppManagerTest, SetAndGetManifest) { | 143 TEST_F(PaymentAppManagerTest, SetAndGetManifest) { |
121 payments::mojom::PaymentAppOptionPtr option = | 144 bool called = false; |
122 payments::mojom::PaymentAppOption::New(); | 145 payments::mojom::PaymentAppManifestError error = payments::mojom:: |
123 option->label = "Visa ****"; | 146 PaymentAppManifestError::MANIFEST_STORAGE_OPERATION_FAILED; |
124 option->id = "payment-app-id"; | 147 SetManifest(kServiceWorkerPattern, CreatePaymentAppManifestForTest(), |
125 option->icon = std::string("payment-app-icon"); | 148 base::Bind(&SetManifestCallback, &called, &error)); |
126 option->enabled_methods.push_back("visa"); | |
127 | 149 |
128 payments::mojom::PaymentAppManifestPtr manifest = | 150 ASSERT_TRUE(called); |
129 payments::mojom::PaymentAppManifest::New(); | |
130 manifest->icon = std::string("payment-app-icon"); | |
131 manifest->label = "Payment App"; | |
132 manifest->options.push_back(std::move(option)); | |
133 | |
134 payments::mojom::PaymentAppManifestError error; | |
135 SetManifest(kServiceWorkerPattern, std::move(manifest), | |
136 base::Bind(&SetManifestCallback, &error)); | |
137 | |
138 ASSERT_EQ(error, payments::mojom::PaymentAppManifestError::NONE); | 151 ASSERT_EQ(error, payments::mojom::PaymentAppManifestError::NONE); |
139 | 152 |
| 153 called = false; |
140 payments::mojom::PaymentAppManifestPtr read_manifest; | 154 payments::mojom::PaymentAppManifestPtr read_manifest; |
141 payments::mojom::PaymentAppManifestError read_error; | 155 payments::mojom::PaymentAppManifestError read_error = payments::mojom:: |
142 GetManifest(kServiceWorkerPattern, | 156 PaymentAppManifestError::MANIFEST_STORAGE_OPERATION_FAILED; |
143 base::Bind(&GetManifestCallback, &read_manifest, &read_error)); | 157 GetManifest(kServiceWorkerPattern, base::Bind(&GetManifestCallback, &called, |
| 158 &read_manifest, &read_error)); |
144 | 159 |
| 160 ASSERT_TRUE(called); |
145 ASSERT_EQ(read_error, payments::mojom::PaymentAppManifestError::NONE); | 161 ASSERT_EQ(read_error, payments::mojom::PaymentAppManifestError::NONE); |
146 EXPECT_EQ(read_manifest->icon, std::string("payment-app-icon")); | 162 EXPECT_EQ(read_manifest->icon, std::string("payment-app-icon")); |
147 EXPECT_EQ(read_manifest->label, "Payment App"); | 163 EXPECT_EQ(read_manifest->label, "Payment App"); |
148 ASSERT_EQ(read_manifest->options.size(), 1U); | 164 ASSERT_EQ(read_manifest->options.size(), 1U); |
149 EXPECT_EQ(read_manifest->options[0]->icon, std::string("payment-app-icon")); | 165 EXPECT_EQ(read_manifest->options[0]->icon, std::string("payment-app-icon")); |
150 EXPECT_EQ(read_manifest->options[0]->label, "Visa ****"); | 166 EXPECT_EQ(read_manifest->options[0]->label, "Visa ****"); |
151 EXPECT_EQ(read_manifest->options[0]->id, "payment-app-id"); | 167 EXPECT_EQ(read_manifest->options[0]->id, "payment-app-id"); |
152 ASSERT_EQ(read_manifest->options[0]->enabled_methods.size(), 1U); | 168 ASSERT_EQ(read_manifest->options[0]->enabled_methods.size(), 1U); |
153 EXPECT_EQ(read_manifest->options[0]->enabled_methods[0], "visa"); | 169 EXPECT_EQ(read_manifest->options[0]->enabled_methods[0], "visa"); |
154 } | 170 } |
155 | 171 |
| 172 TEST_F(PaymentAppManagerTest, SetManifestWithoutAssociatedServiceWorker) { |
| 173 bool called = false; |
| 174 payments::mojom::PaymentAppManifestError error = |
| 175 payments::mojom::PaymentAppManifestError::NONE; |
| 176 SetManifest(kUnregisteredServiceWorkerPattern, |
| 177 CreatePaymentAppManifestForTest(), |
| 178 base::Bind(&SetManifestCallback, &called, &error)); |
| 179 |
| 180 ASSERT_TRUE(called); |
| 181 EXPECT_EQ(error, payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); |
| 182 } |
| 183 |
156 TEST_F(PaymentAppManagerTest, GetManifestWithoutAssociatedServiceWorker) { | 184 TEST_F(PaymentAppManagerTest, GetManifestWithoutAssociatedServiceWorker) { |
| 185 bool called = false; |
157 payments::mojom::PaymentAppManifestPtr read_manifest; | 186 payments::mojom::PaymentAppManifestPtr read_manifest; |
158 payments::mojom::PaymentAppManifestError read_error; | 187 payments::mojom::PaymentAppManifestError read_error = |
159 GetManifest(kServiceWorkerPattern, | 188 payments::mojom::PaymentAppManifestError::NONE; |
160 base::Bind(&GetManifestCallback, &read_manifest, &read_error)); | 189 GetManifest( |
| 190 kUnregisteredServiceWorkerPattern, |
| 191 base::Bind(&GetManifestCallback, &called, &read_manifest, &read_error)); |
161 | 192 |
| 193 ASSERT_TRUE(called); |
| 194 EXPECT_EQ(read_error, |
| 195 payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); |
| 196 } |
| 197 |
| 198 TEST_F(PaymentAppManagerTest, GetManifestWithNoSavedManifest) { |
| 199 bool called = false; |
| 200 payments::mojom::PaymentAppManifestPtr read_manifest; |
| 201 payments::mojom::PaymentAppManifestError read_error = |
| 202 payments::mojom::PaymentAppManifestError::NONE; |
| 203 GetManifest(kServiceWorkerPattern, base::Bind(&GetManifestCallback, &called, |
| 204 &read_manifest, &read_error)); |
| 205 |
| 206 ASSERT_TRUE(called); |
162 EXPECT_EQ(read_error, payments::mojom::PaymentAppManifestError:: | 207 EXPECT_EQ(read_error, payments::mojom::PaymentAppManifestError:: |
163 MANIFEST_STORAGE_OPERATION_FAILED); | 208 MANIFEST_STORAGE_OPERATION_FAILED); |
164 } | 209 } |
165 | 210 |
166 } // namespace content | 211 } // namespace content |
OLD | NEW |