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