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