| 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 kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; | |
| 30 | 29 |
| 31 void RegisterServiceWorkerCallback(bool* called, | 30 void RegisterServiceWorkerCallback(bool* called, |
| 32 int64_t* store_registration_id, | 31 int64_t* store_registration_id, |
| 33 ServiceWorkerStatusCode status, | 32 ServiceWorkerStatusCode status, |
| 34 const std::string& status_message, | 33 const std::string& status_message, |
| 35 int64_t registration_id) { | 34 int64_t registration_id) { |
| 36 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 35 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
| 37 *called = true; | 36 *called = true; |
| 38 *store_registration_id = registration_id; | 37 *store_registration_id = registration_id; |
| 39 } | 38 } |
| 40 | 39 |
| 41 void SetManifestCallback(payments::mojom::PaymentAppManifestError* out_error, | 40 void SetManifestCallback(payments::mojom::PaymentAppManifestError* out_error, |
| 42 payments::mojom::PaymentAppManifestError error) { | 41 payments::mojom::PaymentAppManifestError error) { |
| 43 *out_error = error; | 42 *out_error = error; |
| 44 } | 43 } |
| 45 | 44 |
| 46 void ReadManifestDataCallback(std::vector<std::string>* out_data, | 45 void GetManifestCallback(payments::mojom::PaymentAppManifestPtr* out_manifest, |
| 47 const std::vector<std::string>& data, | 46 payments::mojom::PaymentAppManifestError* out_error, |
| 48 ServiceWorkerStatusCode status) { | 47 payments::mojom::PaymentAppManifestPtr manifest, |
| 49 *out_data = data; | 48 payments::mojom::PaymentAppManifestError error) { |
| 49 *out_manifest = std::move(manifest); |
| 50 *out_error = error; |
| 50 } | 51 } |
| 51 | 52 |
| 52 } // namespace | 53 } // namespace |
| 53 | 54 |
| 54 class PaymentAppManagerTest : public testing::Test { | 55 class PaymentAppManagerTest : public testing::Test { |
| 55 public: | 56 public: |
| 56 PaymentAppManagerTest() | 57 PaymentAppManagerTest() |
| 57 : thread_bundle_( | 58 : thread_bundle_( |
| 58 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), | 59 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), |
| 59 embedded_worker_helper_(new EmbeddedWorkerTestHelper(base::FilePath())), | 60 embedded_worker_helper_(new EmbeddedWorkerTestHelper(base::FilePath())), |
| (...skipping 30 matching lines...) Expand all Loading... |
| 90 base::RunLoop().RunUntilIdle(); | 91 base::RunLoop().RunUntilIdle(); |
| 91 } | 92 } |
| 92 | 93 |
| 93 void SetManifest(const std::string& scope, | 94 void SetManifest(const std::string& scope, |
| 94 payments::mojom::PaymentAppManifestPtr manifest, | 95 payments::mojom::PaymentAppManifestPtr manifest, |
| 95 const PaymentAppManager::SetManifestCallback& callback) { | 96 const PaymentAppManager::SetManifestCallback& callback) { |
| 96 manager_->SetManifest(scope, std::move(manifest), callback); | 97 manager_->SetManifest(scope, std::move(manifest), callback); |
| 97 base::RunLoop().RunUntilIdle(); | 98 base::RunLoop().RunUntilIdle(); |
| 98 } | 99 } |
| 99 | 100 |
| 100 void ReadManifestData(std::vector<std::string>* out_data) { | 101 void GetManifest(const std::string& scope, |
| 101 embedded_worker_helper_->context_wrapper()->GetRegistrationUserData( | 102 const PaymentAppManager::GetManifestCallback& callback) { |
| 102 sw_registration_id_, {{kPaymentAppManifestDataKey}}, | 103 manager_->GetManifest(scope, callback); |
| 103 base::Bind(&ReadManifestDataCallback, out_data)); | |
| 104 base::RunLoop().RunUntilIdle(); | 104 base::RunLoop().RunUntilIdle(); |
| 105 } | 105 } |
| 106 | 106 |
| 107 private: | 107 private: |
| 108 std::unique_ptr<TestBrowserThreadBundle> thread_bundle_; | 108 std::unique_ptr<TestBrowserThreadBundle> thread_bundle_; |
| 109 std::unique_ptr<EmbeddedWorkerTestHelper> embedded_worker_helper_; | 109 std::unique_ptr<EmbeddedWorkerTestHelper> embedded_worker_helper_; |
| 110 std::unique_ptr<StoragePartitionImpl> storage_partition_impl_; | 110 std::unique_ptr<StoragePartitionImpl> storage_partition_impl_; |
| 111 int64_t sw_registration_id_; | 111 int64_t sw_registration_id_; |
| 112 scoped_refptr<PaymentAppContext> payment_app_context_; | 112 scoped_refptr<PaymentAppContext> payment_app_context_; |
| 113 payments::mojom::PaymentAppManagerPtr service_; | 113 payments::mojom::PaymentAppManagerPtr service_; |
| 114 | 114 |
| 115 // Owned by payment_app_context_. | 115 // Owned by payment_app_context_. |
| 116 PaymentAppManager* manager_; | 116 PaymentAppManager* manager_; |
| 117 | 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(PaymentAppManagerTest); | 118 DISALLOW_COPY_AND_ASSIGN(PaymentAppManagerTest); |
| 119 }; | 119 }; |
| 120 | 120 |
| 121 TEST_F(PaymentAppManagerTest, SetManifest) { | 121 TEST_F(PaymentAppManagerTest, SetAndGetManifest) { |
| 122 payments::mojom::PaymentAppOptionPtr option = | 122 payments::mojom::PaymentAppOptionPtr option = |
| 123 payments::mojom::PaymentAppOption::New(); | 123 payments::mojom::PaymentAppOption::New(); |
| 124 option->label = "Visa ****"; | 124 option->label = "Visa ****"; |
| 125 option->id = "payment-app-id"; | 125 option->id = "payment-app-id"; |
| 126 option->icon = std::string("payment-app-icon"); | 126 option->icon = std::string("payment-app-icon"); |
| 127 option->enabled_methods.push_back("visa"); | 127 option->enabled_methods.push_back("visa"); |
| 128 | 128 |
| 129 payments::mojom::PaymentAppManifestPtr manifest = | 129 payments::mojom::PaymentAppManifestPtr manifest = |
| 130 payments::mojom::PaymentAppManifest::New(); | 130 payments::mojom::PaymentAppManifest::New(); |
| 131 manifest->icon = std::string("payment-app-icon"); | 131 manifest->icon = std::string("payment-app-icon"); |
| 132 manifest->label = "Payment App"; | 132 manifest->label = "Payment App"; |
| 133 manifest->options.push_back(std::move(option)); | 133 manifest->options.push_back(std::move(option)); |
| 134 | 134 |
| 135 payments::mojom::PaymentAppManifestError error; | 135 payments::mojom::PaymentAppManifestError error; |
| 136 SetManifest(kServiceWorkerPattern, std::move(manifest), | 136 SetManifest(kServiceWorkerPattern, std::move(manifest), |
| 137 base::Bind(&SetManifestCallback, &error)); | 137 base::Bind(&SetManifestCallback, &error)); |
| 138 | 138 |
| 139 ASSERT_EQ(error, payments::mojom::PaymentAppManifestError::NONE); | 139 ASSERT_EQ(error, payments::mojom::PaymentAppManifestError::NONE); |
| 140 | 140 |
| 141 std::vector<std::string> data; | 141 payments::mojom::PaymentAppManifestPtr read_manifest; |
| 142 ReadManifestData(&data); | 142 payments::mojom::PaymentAppManifestError read_error; |
| 143 ASSERT_EQ(data.size(), 1UL); | 143 GetManifest(kServiceWorkerPattern, |
| 144 EXPECT_EQ( | 144 base::Bind(&GetManifestCallback, &read_manifest, &read_error)); |
| 145 "\n\vPayment App\x12\x10payment-app-icon\x1A" | 145 |
| 146 "3\n\tVisa ****\x12\x10payment-app-icon\x1A\xEpayment-app-id\"\x4visa", | 146 ASSERT_EQ(read_error, payments::mojom::PaymentAppManifestError::NONE); |
| 147 data[0]); | 147 EXPECT_EQ(read_manifest->icon, std::string("payment-app-icon")); |
| 148 EXPECT_EQ(read_manifest->label, "Payment App"); |
| 149 ASSERT_EQ(read_manifest->options.size(), 1U); |
| 150 EXPECT_EQ(read_manifest->options[0]->icon, std::string("payment-app-icon")); |
| 151 EXPECT_EQ(read_manifest->options[0]->label, "Visa ****"); |
| 152 EXPECT_EQ(read_manifest->options[0]->id, "payment-app-id"); |
| 153 ASSERT_EQ(read_manifest->options[0]->enabled_methods.size(), 1U); |
| 154 EXPECT_EQ(read_manifest->options[0]->enabled_methods[0], "visa"); |
| 155 } |
| 156 |
| 157 TEST_F(PaymentAppManagerTest, GetManifestWithoutAssociatedServiceWorker) { |
| 158 payments::mojom::PaymentAppManifestPtr read_manifest; |
| 159 payments::mojom::PaymentAppManifestError read_error; |
| 160 GetManifest(kServiceWorkerPattern, |
| 161 base::Bind(&GetManifestCallback, &read_manifest, &read_error)); |
| 162 |
| 163 EXPECT_EQ(read_error, payments::mojom::PaymentAppManifestError:: |
| 164 MANIFEST_STORAGE_OPERATION_FAILED); |
| 148 } | 165 } |
| 149 | 166 |
| 150 } // namespace content | 167 } // namespace content |
| OLD | NEW |