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_content_unittest_base.h" | 5 #include "content/browser/payments/payment_app_content_unittest_base.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.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/common/service_worker/service_worker_event_dispatcher.mojom.h" |
18 #include "content/common/service_worker/service_worker_status_code.h" | 19 #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_context.h" |
20 #include "content/public/test/test_browser_thread_bundle.h" | 21 #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/associated_interface_ptr.h" |
22 #include "mojo/public/cpp/bindings/interface_request.h" | 23 #include "mojo/public/cpp/bindings/interface_request.h" |
23 | 24 |
24 namespace content { | 25 namespace content { |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 void RegisterServiceWorkerCallback(bool* called, | 29 void RegisterServiceWorkerCallback(bool* called, |
29 ServiceWorkerStatusCode status, | 30 ServiceWorkerStatusCode status, |
30 const std::string& status_message, | 31 const std::string& status_message, |
31 int64_t registration_id) { | 32 int64_t registration_id) { |
32 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 33 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
33 *called = true; | 34 *called = true; |
34 } | 35 } |
35 | 36 |
36 void UnregisterServiceWorkerCallback(bool* called, | 37 void UnregisterServiceWorkerCallback(bool* called, |
37 ServiceWorkerStatusCode status) { | 38 ServiceWorkerStatusCode status) { |
38 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 39 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
39 *called = true; | 40 *called = true; |
40 } | 41 } |
41 | 42 |
42 } // namespace | 43 } // namespace |
43 | 44 |
| 45 class PaymentAppContentUnitTestBase::PaymentAppForWorkerTestHelper |
| 46 : public EmbeddedWorkerTestHelper { |
| 47 public: |
| 48 PaymentAppForWorkerTestHelper() |
| 49 : EmbeddedWorkerTestHelper(base::FilePath()), |
| 50 was_dispatched_(false), |
| 51 last_sw_registration_id_(kInvalidServiceWorkerRegistrationId) {} |
| 52 ~PaymentAppForWorkerTestHelper() override {} |
| 53 |
| 54 void OnStartWorker( |
| 55 int embedded_worker_id, |
| 56 int64_t service_worker_version_id, |
| 57 const GURL& scope, |
| 58 const GURL& script_url, |
| 59 bool pause_after_download, |
| 60 mojom::ServiceWorkerEventDispatcherRequest request) override { |
| 61 ServiceWorkerVersion* version = |
| 62 context()->GetLiveVersion(service_worker_version_id); |
| 63 version->SetMainScriptHttpResponseInfo( |
| 64 EmbeddedWorkerTestHelper::CreateHttpResponseInfo()); |
| 65 last_sw_registration_id_ = version->registration_id(); |
| 66 last_sw_scope_ = scope; |
| 67 EmbeddedWorkerTestHelper::OnStartWorker( |
| 68 embedded_worker_id, service_worker_version_id, scope, script_url, |
| 69 pause_after_download, std::move(request)); |
| 70 } |
| 71 |
| 72 void OnPaymentRequestEvent( |
| 73 payments::mojom::PaymentAppRequestDataPtr data, |
| 74 const mojom::ServiceWorkerEventDispatcher:: |
| 75 DispatchPaymentRequestEventCallback& callback) override { |
| 76 ASSERT_FALSE(was_dispatched_); |
| 77 EmbeddedWorkerTestHelper::OnPaymentRequestEvent(std::move(data), callback); |
| 78 was_dispatched_ = true; |
| 79 } |
| 80 |
| 81 bool was_dispatched_; |
| 82 int64_t last_sw_registration_id_; |
| 83 GURL last_sw_scope_; |
| 84 |
| 85 private: |
| 86 DISALLOW_COPY_AND_ASSIGN(PaymentAppForWorkerTestHelper); |
| 87 }; |
| 88 |
44 PaymentAppContentUnitTestBase::PaymentAppContentUnitTestBase() | 89 PaymentAppContentUnitTestBase::PaymentAppContentUnitTestBase() |
45 : thread_bundle_( | 90 : thread_bundle_( |
46 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), | 91 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), |
47 embedded_worker_helper_(new EmbeddedWorkerTestHelper(base::FilePath())) { | 92 worker_helper_(new PaymentAppForWorkerTestHelper()) { |
48 embedded_worker_helper_->context_wrapper()->set_storage_partition( | 93 worker_helper_->context_wrapper()->set_storage_partition(storage_partition()); |
49 storage_partition()); | 94 storage_partition()->service_worker_context_->Shutdown(); |
50 payment_app_context()->Init(embedded_worker_helper_->context_wrapper()); | 95 base::RunLoop().RunUntilIdle(); |
| 96 |
| 97 storage_partition()->service_worker_context_ = |
| 98 worker_helper_->context_wrapper(); |
| 99 payment_app_context()->Init(worker_helper_->context_wrapper()); |
51 base::RunLoop().RunUntilIdle(); | 100 base::RunLoop().RunUntilIdle(); |
52 } | 101 } |
53 | 102 |
54 PaymentAppContentUnitTestBase::~PaymentAppContentUnitTestBase() {} | 103 PaymentAppContentUnitTestBase::~PaymentAppContentUnitTestBase() {} |
55 | 104 |
56 BrowserContext* PaymentAppContentUnitTestBase::browser_context() { | 105 BrowserContext* PaymentAppContentUnitTestBase::browser_context() { |
57 DCHECK(embedded_worker_helper_); | 106 DCHECK(worker_helper_); |
58 return embedded_worker_helper_->browser_context(); | 107 return worker_helper_->browser_context(); |
59 } | 108 } |
60 | 109 |
61 PaymentAppManager* PaymentAppContentUnitTestBase::CreatePaymentAppManager( | 110 PaymentAppManager* PaymentAppContentUnitTestBase::CreatePaymentAppManager( |
62 const GURL& scope_url, | 111 const GURL& scope_url, |
63 const GURL& sw_script_url) { | 112 const GURL& sw_script_url) { |
64 // Register service worker for payment app manager. | 113 // Register service worker for payment app manager. |
65 bool called = false; | 114 bool called = false; |
66 embedded_worker_helper_->context()->RegisterServiceWorker( | 115 worker_helper_->context()->RegisterServiceWorker( |
67 scope_url, sw_script_url, nullptr, | 116 scope_url, sw_script_url, nullptr, |
68 base::Bind(&RegisterServiceWorkerCallback, &called)); | 117 base::Bind(&RegisterServiceWorkerCallback, &called)); |
69 base::RunLoop().RunUntilIdle(); | 118 base::RunLoop().RunUntilIdle(); |
70 EXPECT_TRUE(called); | 119 EXPECT_TRUE(called); |
71 | 120 |
72 // This function should eventually return created payment app manager | 121 // This function should eventually return created payment app manager |
73 // but there is no way to get last created payment app manager from | 122 // but there is no way to get last created payment app manager from |
74 // payment_app_context()->payment_app_managers_ because its type is std::map | 123 // payment_app_context()->payment_app_managers_ because its type is std::map |
75 // and can not ensure its order. So, just make a set of existing payment app | 124 // and can not ensure its order. So, just make a set of existing payment app |
76 // managers before creating a new manager and then check what is a new thing. | 125 // managers before creating a new manager and then check what is a new thing. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 manifest->name = name; | 184 manifest->name = name; |
136 manifest->options.push_back(std::move(option)); | 185 manifest->options.push_back(std::move(option)); |
137 | 186 |
138 return manifest; | 187 return manifest; |
139 } | 188 } |
140 | 189 |
141 void PaymentAppContentUnitTestBase::UnregisterServiceWorker( | 190 void PaymentAppContentUnitTestBase::UnregisterServiceWorker( |
142 const GURL& scope_url) { | 191 const GURL& scope_url) { |
143 // Unregister service worker. | 192 // Unregister service worker. |
144 bool called = false; | 193 bool called = false; |
145 embedded_worker_helper_->context()->UnregisterServiceWorker( | 194 worker_helper_->context()->UnregisterServiceWorker( |
146 scope_url, base::Bind(&UnregisterServiceWorkerCallback, &called)); | 195 scope_url, base::Bind(&UnregisterServiceWorkerCallback, &called)); |
147 base::RunLoop().RunUntilIdle(); | 196 base::RunLoop().RunUntilIdle(); |
148 EXPECT_TRUE(called); | 197 EXPECT_TRUE(called); |
149 } | 198 } |
150 | 199 |
| 200 void PaymentAppContentUnitTestBase::ResetPaymentAppInvoked() const { |
| 201 worker_helper_->was_dispatched_ = false; |
| 202 } |
| 203 |
| 204 bool PaymentAppContentUnitTestBase::payment_app_invoked() const { |
| 205 return worker_helper_->was_dispatched_; |
| 206 } |
| 207 |
| 208 int64_t PaymentAppContentUnitTestBase::last_sw_registration_id() const { |
| 209 return worker_helper_->last_sw_registration_id_; |
| 210 } |
| 211 |
| 212 const GURL& PaymentAppContentUnitTestBase::last_sw_scope_url() const { |
| 213 return worker_helper_->last_sw_scope_; |
| 214 } |
| 215 |
151 StoragePartitionImpl* PaymentAppContentUnitTestBase::storage_partition() { | 216 StoragePartitionImpl* PaymentAppContentUnitTestBase::storage_partition() { |
152 return static_cast<StoragePartitionImpl*>( | 217 return static_cast<StoragePartitionImpl*>( |
153 BrowserContext::GetDefaultStoragePartition(browser_context())); | 218 BrowserContext::GetDefaultStoragePartition(browser_context())); |
154 } | 219 } |
155 | 220 |
156 PaymentAppContextImpl* PaymentAppContentUnitTestBase::payment_app_context() { | 221 PaymentAppContextImpl* PaymentAppContentUnitTestBase::payment_app_context() { |
157 return storage_partition()->GetPaymentAppContext(); | 222 return storage_partition()->GetPaymentAppContext(); |
158 } | 223 } |
159 | 224 |
160 } // namespace content | 225 } // namespace content |
OLD | NEW |