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_content_unittest_base.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <set> |
| 10 #include <utility> |
| 11 |
| 12 #include "base/files/file_path.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "base/stl_util.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/common/service_worker/service_worker_status_code.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/associated_interface_ptr.h" |
| 22 #include "mojo/public/cpp/bindings/interface_request.h" |
| 23 |
| 24 namespace content { |
| 25 |
| 26 namespace { |
| 27 |
| 28 void RegisterServiceWorkerCallback(bool* called, |
| 29 ServiceWorkerStatusCode status, |
| 30 const std::string& status_message, |
| 31 int64_t registration_id) { |
| 32 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
| 33 *called = true; |
| 34 } |
| 35 |
| 36 } // namespace |
| 37 |
| 38 PaymentAppContentUnitTestBase::PaymentAppContentUnitTestBase() |
| 39 : thread_bundle_( |
| 40 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), |
| 41 embedded_worker_helper_(new EmbeddedWorkerTestHelper(base::FilePath())), |
| 42 storage_partition_impl_( |
| 43 new StoragePartitionImpl(embedded_worker_helper_->browser_context(), |
| 44 base::FilePath(), |
| 45 nullptr)), |
| 46 payment_app_context_(new PaymentAppContextImpl()) { |
| 47 embedded_worker_helper_->context_wrapper()->set_storage_partition( |
| 48 storage_partition_impl_.get()); |
| 49 payment_app_context_->Init(embedded_worker_helper_->context_wrapper()); |
| 50 base::RunLoop().RunUntilIdle(); |
| 51 } |
| 52 |
| 53 PaymentAppContentUnitTestBase::~PaymentAppContentUnitTestBase() { |
| 54 payment_app_context_->Shutdown(); |
| 55 base::RunLoop().RunUntilIdle(); |
| 56 } |
| 57 |
| 58 PaymentAppManager* PaymentAppContentUnitTestBase::CreatePaymentAppManager( |
| 59 const GURL& scope_url, |
| 60 const GURL& sw_script_url) { |
| 61 // Register service worker for payment app manager. |
| 62 bool called = false; |
| 63 embedded_worker_helper_->context()->RegisterServiceWorker( |
| 64 scope_url, sw_script_url, nullptr, |
| 65 base::Bind(&RegisterServiceWorkerCallback, &called)); |
| 66 base::RunLoop().RunUntilIdle(); |
| 67 EXPECT_TRUE(called); |
| 68 |
| 69 // This function should eventually return created payment app manager |
| 70 // but there is no way to get last created payment app manager from |
| 71 // payment_app_context_->payment_app_managers_ because its type is std::map |
| 72 // and can not ensure its order. So, just make a set of existing payment app |
| 73 // managers before creating a new manager and then check what is a new thing. |
| 74 std::set<PaymentAppManager*> existing_managers; |
| 75 for (const auto& existing_manager : |
| 76 payment_app_context_->payment_app_managers_) { |
| 77 existing_managers.insert(existing_manager.first); |
| 78 } |
| 79 |
| 80 // Create a new payment app manager. |
| 81 payments::mojom::PaymentAppManagerPtr manager; |
| 82 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request = |
| 83 mojo::GetProxy(&manager); |
| 84 payment_app_managers_.push_back(std::move(manager)); |
| 85 payment_app_context_->CreatePaymentAppManager(std::move(request)); |
| 86 base::RunLoop().RunUntilIdle(); |
| 87 |
| 88 // Find a last registered payment app manager. |
| 89 for (const auto& candidate_manager : |
| 90 payment_app_context_->payment_app_managers_) { |
| 91 if (!base::ContainsKey(existing_managers, candidate_manager.first)) |
| 92 return candidate_manager.first; |
| 93 } |
| 94 |
| 95 NOTREACHED(); |
| 96 return nullptr; |
| 97 } |
| 98 |
| 99 void PaymentAppContentUnitTestBase::SetManifest( |
| 100 PaymentAppManager* manager, |
| 101 const std::string& scope, |
| 102 payments::mojom::PaymentAppManifestPtr manifest, |
| 103 const PaymentAppManager::SetManifestCallback& callback) { |
| 104 ASSERT_NE(nullptr, manager); |
| 105 manager->SetManifest(scope, std::move(manifest), callback); |
| 106 base::RunLoop().RunUntilIdle(); |
| 107 } |
| 108 |
| 109 void PaymentAppContentUnitTestBase::GetManifest( |
| 110 PaymentAppManager* manager, |
| 111 const std::string& scope, |
| 112 const PaymentAppManager::GetManifestCallback& callback) { |
| 113 ASSERT_NE(nullptr, manager); |
| 114 manager->GetManifest(scope, callback); |
| 115 base::RunLoop().RunUntilIdle(); |
| 116 } |
| 117 |
| 118 } // namespace content |
OLD | NEW |