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 struct PaymentAppContentUnitTestBase::PaymentAppForWorkerTestHelper | |
zino
2017/01/11 20:06:43
@rouslan,
Should this struct be located to below
please use gerrit instead
2017/01/12 19:04:00
It feels strange to derive a struct from a class.
please use gerrit instead
2017/01/12 19:04:00
Good question. Let's follow the rule of thumb that
zino
2017/01/13 16:41:10
Done.
zino
2017/01/16 15:01:09
I tried it but I couldn't because it is used in Pa
| |
46 : public EmbeddedWorkerTestHelper { | |
47 PaymentAppForWorkerTestHelper() | |
48 : EmbeddedWorkerTestHelper(base::FilePath()) {} | |
please use gerrit instead
2017/01/12 19:04:00
Need to initialize was_dispatched and last_sw_regi
please use gerrit instead
2017/01/12 19:04:00
Need to override the parent destructor.
zino
2017/01/13 16:41:10
Done.
| |
49 | |
50 void OnStartWorker(int embedded_worker_id, | |
51 int64_t service_worker_version_id, | |
52 const GURL& scope, | |
53 const GURL& script_url, | |
54 bool pause_after_download) override { | |
55 ServiceWorkerVersion* version = | |
56 context()->GetLiveVersion(service_worker_version_id); | |
57 version->SetMainScriptHttpResponseInfo( | |
58 EmbeddedWorkerTestHelper::CreateHttpResponseInfo()); | |
59 last_sw_registration_id = version->registration_id(); | |
60 last_sw_scope = scope; | |
61 EmbeddedWorkerTestHelper::OnStartWorker(embedded_worker_id, | |
62 service_worker_version_id, scope, | |
63 script_url, pause_after_download); | |
64 } | |
65 | |
66 void OnPaymentRequestEvent( | |
67 payments::mojom::PaymentAppRequestDataPtr data, | |
68 const mojom::ServiceWorkerEventDispatcher:: | |
69 DispatchPaymentRequestEventCallback& callback) override { | |
70 ASSERT_FALSE(was_dispatched); | |
71 EmbeddedWorkerTestHelper::OnPaymentRequestEvent(std::move(data), callback); | |
72 was_dispatched = true; | |
73 } | |
74 | |
75 bool was_dispatched; | |
76 int64_t last_sw_registration_id; | |
77 GURL last_sw_scope; | |
please use gerrit instead
2017/01/12 19:04:00
private:
DISALLOW_COPY_AND_ASSIGN(PaymentAppForWor
zino
2017/01/13 16:41:10
Done.
| |
78 }; | |
79 | |
44 PaymentAppContentUnitTestBase::PaymentAppContentUnitTestBase() | 80 PaymentAppContentUnitTestBase::PaymentAppContentUnitTestBase() |
45 : thread_bundle_( | 81 : thread_bundle_( |
46 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), | 82 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), |
47 embedded_worker_helper_(new EmbeddedWorkerTestHelper(base::FilePath())) { | 83 worker_helper_(new PaymentAppForWorkerTestHelper()) { |
48 embedded_worker_helper_->context_wrapper()->set_storage_partition( | 84 worker_helper_->context_wrapper()->set_storage_partition(storage_partition()); |
49 storage_partition()); | 85 storage_partition()->service_worker_context_->Shutdown(); |
50 payment_app_context()->Init(embedded_worker_helper_->context_wrapper()); | 86 base::RunLoop().RunUntilIdle(); |
87 | |
88 storage_partition()->service_worker_context_ = | |
89 worker_helper_->context_wrapper(); | |
90 payment_app_context()->Init(worker_helper_->context_wrapper()); | |
51 base::RunLoop().RunUntilIdle(); | 91 base::RunLoop().RunUntilIdle(); |
52 } | 92 } |
53 | 93 |
54 PaymentAppContentUnitTestBase::~PaymentAppContentUnitTestBase() {} | 94 PaymentAppContentUnitTestBase::~PaymentAppContentUnitTestBase() {} |
55 | 95 |
56 BrowserContext* PaymentAppContentUnitTestBase::browser_context() { | 96 BrowserContext* PaymentAppContentUnitTestBase::browser_context() { |
57 DCHECK(embedded_worker_helper_); | 97 DCHECK(worker_helper_); |
58 return embedded_worker_helper_->browser_context(); | 98 return worker_helper_->browser_context(); |
59 } | 99 } |
60 | 100 |
61 PaymentAppManager* PaymentAppContentUnitTestBase::CreatePaymentAppManager( | 101 PaymentAppManager* PaymentAppContentUnitTestBase::CreatePaymentAppManager( |
62 const GURL& scope_url, | 102 const GURL& scope_url, |
63 const GURL& sw_script_url) { | 103 const GURL& sw_script_url) { |
64 // Register service worker for payment app manager. | 104 // Register service worker for payment app manager. |
65 bool called = false; | 105 bool called = false; |
66 embedded_worker_helper_->context()->RegisterServiceWorker( | 106 worker_helper_->context()->RegisterServiceWorker( |
67 scope_url, sw_script_url, nullptr, | 107 scope_url, sw_script_url, nullptr, |
68 base::Bind(&RegisterServiceWorkerCallback, &called)); | 108 base::Bind(&RegisterServiceWorkerCallback, &called)); |
69 base::RunLoop().RunUntilIdle(); | 109 base::RunLoop().RunUntilIdle(); |
70 EXPECT_TRUE(called); | 110 EXPECT_TRUE(called); |
71 | 111 |
72 // This function should eventually return created payment app manager | 112 // This function should eventually return created payment app manager |
73 // but there is no way to get last created payment app manager from | 113 // 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 | 114 // 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 | 115 // 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. | 116 // 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; | 175 manifest->name = name; |
136 manifest->options.push_back(std::move(option)); | 176 manifest->options.push_back(std::move(option)); |
137 | 177 |
138 return manifest; | 178 return manifest; |
139 } | 179 } |
140 | 180 |
141 void PaymentAppContentUnitTestBase::UnregisterServiceWorker( | 181 void PaymentAppContentUnitTestBase::UnregisterServiceWorker( |
142 const GURL& scope_url) { | 182 const GURL& scope_url) { |
143 // Unregister service worker. | 183 // Unregister service worker. |
144 bool called = false; | 184 bool called = false; |
145 embedded_worker_helper_->context()->UnregisterServiceWorker( | 185 worker_helper_->context()->UnregisterServiceWorker( |
146 scope_url, base::Bind(&UnregisterServiceWorkerCallback, &called)); | 186 scope_url, base::Bind(&UnregisterServiceWorkerCallback, &called)); |
147 base::RunLoop().RunUntilIdle(); | 187 base::RunLoop().RunUntilIdle(); |
148 EXPECT_TRUE(called); | 188 EXPECT_TRUE(called); |
149 } | 189 } |
150 | 190 |
191 void PaymentAppContentUnitTestBase::init_payment_app_invoked() const { | |
192 worker_helper_->was_dispatched = false; | |
please use gerrit instead
2017/01/12 19:04:00
Initialization should happen in the constructor of
zino
2017/01/13 16:41:10
Done.
| |
193 } | |
194 | |
195 bool PaymentAppContentUnitTestBase::payment_app_invoked() const { | |
196 return worker_helper_->was_dispatched; | |
197 } | |
198 | |
199 int64_t PaymentAppContentUnitTestBase::last_sw_registration_id() const { | |
200 return worker_helper_->last_sw_registration_id; | |
201 } | |
202 | |
203 const GURL& PaymentAppContentUnitTestBase::last_sw_scope_url() const { | |
204 return worker_helper_->last_sw_scope; | |
205 } | |
206 | |
151 StoragePartitionImpl* PaymentAppContentUnitTestBase::storage_partition() { | 207 StoragePartitionImpl* PaymentAppContentUnitTestBase::storage_partition() { |
152 return static_cast<StoragePartitionImpl*>( | 208 return static_cast<StoragePartitionImpl*>( |
153 BrowserContext::GetDefaultStoragePartition(browser_context())); | 209 BrowserContext::GetDefaultStoragePartition(browser_context())); |
154 } | 210 } |
155 | 211 |
156 PaymentAppContextImpl* PaymentAppContentUnitTestBase::payment_app_context() { | 212 PaymentAppContextImpl* PaymentAppContentUnitTestBase::payment_app_context() { |
157 return storage_partition()->GetPaymentAppContext(); | 213 return storage_partition()->GetPaymentAppContext(); |
158 } | 214 } |
159 | 215 |
160 } // namespace content | 216 } // namespace content |
OLD | NEW |