OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/payments/payment_app_manager.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 |
| 24 namespace content { |
| 25 |
| 26 namespace { |
| 27 |
| 28 const char kServiceWorkerPattern[] = "https://example.com/a"; |
| 29 const char kServiceWorkerScript[] = "https://example.com/a/script.js"; |
| 30 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; |
| 31 |
| 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(payments::mojom::PaymentAppManifestError* out_error, |
| 43 payments::mojom::PaymentAppManifestError error) { |
| 44 *out_error = error; |
| 45 } |
| 46 |
| 47 void ReadManifestDataCallback(std::vector<std::string>* out_data, |
| 48 const std::vector<std::string>& data, |
| 49 ServiceWorkerStatusCode status) { |
| 50 *out_data = data; |
| 51 } |
| 52 |
| 53 } // namespace |
| 54 |
| 55 class PaymentAppManagerTest : public testing::Test { |
| 56 public: |
| 57 PaymentAppManagerTest() |
| 58 : thread_bundle_( |
| 59 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)) { |
| 60 } |
| 61 |
| 62 void SetUp() override { |
| 63 CreateTestHelper(); |
| 64 CreateStoragePartition(); |
| 65 CreatePaymentAppContext(); |
| 66 CreateServiceWorkerRegistration(); |
| 67 CreatePaymentAppManager(); |
| 68 } |
| 69 |
| 70 void TearDown() override { |
| 71 payment_app_context_->Shutdown(); |
| 72 base::RunLoop().RunUntilIdle(); |
| 73 payment_app_context_ = nullptr; |
| 74 } |
| 75 |
| 76 void SetManifest(const std::string& scope, |
| 77 payments::mojom::PaymentAppManifestPtr manifest, |
| 78 const PaymentAppManager::SetManifestCallback& callback) { |
| 79 manager_->SetManifest(scope, std::move(manifest), callback); |
| 80 base::RunLoop().RunUntilIdle(); |
| 81 } |
| 82 |
| 83 void ReadManifestData(std::vector<std::string>* out_data) { |
| 84 embedded_worker_helper_->context_wrapper()->GetRegistrationUserData( |
| 85 sw_registration_id_, {{kPaymentAppManifestDataKey}}, |
| 86 base::Bind(&ReadManifestDataCallback, out_data)); |
| 87 base::RunLoop().RunUntilIdle(); |
| 88 } |
| 89 |
| 90 private: |
| 91 void CreateTestHelper() { |
| 92 embedded_worker_helper_.reset( |
| 93 new EmbeddedWorkerTestHelper(base::FilePath())); |
| 94 } |
| 95 |
| 96 void CreateStoragePartition() { |
| 97 storage_partition_impl_.reset(new StoragePartitionImpl( |
| 98 embedded_worker_helper_->browser_context(), base::FilePath(), nullptr, |
| 99 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, |
| 100 nullptr, nullptr, nullptr, nullptr, nullptr)); |
| 101 embedded_worker_helper_->context_wrapper()->set_storage_partition( |
| 102 storage_partition_impl_.get()); |
| 103 } |
| 104 |
| 105 void CreatePaymentAppContext() { |
| 106 payment_app_context_ = |
| 107 new PaymentAppContext(embedded_worker_helper_->context_wrapper()); |
| 108 } |
| 109 |
| 110 void CreateServiceWorkerRegistration() { |
| 111 bool called = false; |
| 112 embedded_worker_helper_->context()->RegisterServiceWorker( |
| 113 GURL(kServiceWorkerPattern), GURL(kServiceWorkerScript), NULL, |
| 114 base::Bind(&RegisterServiceWorkerCallback, &called, |
| 115 &sw_registration_id_)); |
| 116 base::RunLoop().RunUntilIdle(); |
| 117 EXPECT_TRUE(called); |
| 118 } |
| 119 |
| 120 void CreatePaymentAppManager() { |
| 121 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request = |
| 122 mojo::GetProxy(&service_); |
| 123 payment_app_context_->CreateService(std::move(request)); |
| 124 base::RunLoop().RunUntilIdle(); |
| 125 manager_ = payment_app_context_->services_.begin()->first; |
| 126 EXPECT_TRUE(manager_); |
| 127 } |
| 128 |
| 129 std::unique_ptr<TestBrowserThreadBundle> thread_bundle_; |
| 130 std::unique_ptr<EmbeddedWorkerTestHelper> embedded_worker_helper_; |
| 131 std::unique_ptr<StoragePartitionImpl> storage_partition_impl_; |
| 132 int64_t sw_registration_id_; |
| 133 scoped_refptr<PaymentAppContext> payment_app_context_; |
| 134 payments::mojom::PaymentAppManagerPtr service_; |
| 135 PaymentAppManager* manager_; |
| 136 }; |
| 137 |
| 138 TEST_F(PaymentAppManagerTest, SetManifest) { |
| 139 payments::mojom::PaymentAppOptionPtr option = |
| 140 payments::mojom::PaymentAppOption::New(); |
| 141 option->label = "Visa ****"; |
| 142 option->id = "payment-app-id"; |
| 143 option->icon = std::string("payment-app-icon"); |
| 144 option->enabled_methods.push_back("visa"); |
| 145 |
| 146 payments::mojom::PaymentAppManifestPtr manifest = |
| 147 payments::mojom::PaymentAppManifest::New(); |
| 148 manifest->icon = std::string("payment-app-icon"); |
| 149 manifest->label = "Payment App"; |
| 150 manifest->options.push_back(std::move(option)); |
| 151 |
| 152 payments::mojom::PaymentAppManifestError error; |
| 153 SetManifest(kServiceWorkerPattern, std::move(manifest), |
| 154 base::Bind(&SetManifestCallback, &error)); |
| 155 |
| 156 EXPECT_EQ(error, payments::mojom::PaymentAppManifestError::NONE); |
| 157 |
| 158 std::vector<std::string> data; |
| 159 ReadManifestData(&data); |
| 160 EXPECT_EQ(data.size(), 1UL); |
| 161 EXPECT_EQ( |
| 162 "\n\vPayment App\x12\x10payment-app-icon\x1A" |
| 163 "3\n\tVisa ****\x12\x10payment-app-icon\x1A\xEpayment-app-id\"\x4visa", |
| 164 data[0]); |
| 165 } |
| 166 |
| 167 } // namespace content |
OLD | NEW |