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