Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: content/browser/payments/payment_app_manager_unittest.cc

Issue 2572183002: PaymentApp: Introduce PaymentAppDatabase class. (Closed)
Patch Set: PaymentApp: Introduce PaymentAppDatabase class. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_manager.h" 5 #include "content/browser/payments/payment_app_manager.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "components/payments/payment_app.mojom.h" 14 #include "components/payments/payment_app.mojom.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/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/test/test_browser_context.h" 19 #include "content/public/test/test_browser_context.h"
20 #include "content/public/test/test_browser_thread_bundle.h" 20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "mojo/public/cpp/bindings/interface_ptr.h" 21 #include "mojo/public/cpp/bindings/interface_ptr.h"
22 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
23 23
24 namespace content { 24 namespace content {
25 namespace { 25 namespace {
26 26
27 const char kServiceWorkerPattern[] = "https://example.com/a"; 27 const char kServiceWorkerPattern[] = "https://example.com/a";
28 const char kServiceWorkerScript[] = "https://example.com/a/script.js"; 28 const char kServiceWorkerScript[] = "https://example.com/a/script.js";
29 const char kUnregisteredServiceWorkerPattern[] =
30 "https://example.com/unregistered";
29 31
30 void RegisterServiceWorkerCallback(bool* called, 32 void RegisterServiceWorkerCallback(bool* called,
31 int64_t* store_registration_id, 33 int64_t* store_registration_id,
32 ServiceWorkerStatusCode status, 34 ServiceWorkerStatusCode status,
33 const std::string& status_message, 35 const std::string& status_message,
34 int64_t registration_id) { 36 int64_t registration_id) {
35 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); 37 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status);
36 *called = true; 38 *called = true;
37 *store_registration_id = registration_id; 39 *store_registration_id = registration_id;
38 } 40 }
39 41
40 void SetManifestCallback(payments::mojom::PaymentAppManifestError* out_error, 42 void SetManifestCallback(bool* called,
43 payments::mojom::PaymentAppManifestError* out_error,
41 payments::mojom::PaymentAppManifestError error) { 44 payments::mojom::PaymentAppManifestError error) {
45 *called = true;
42 *out_error = error; 46 *out_error = error;
43 } 47 }
44 48
45 void GetManifestCallback(payments::mojom::PaymentAppManifestPtr* out_manifest, 49 void GetManifestCallback(bool* called,
50 payments::mojom::PaymentAppManifestPtr* out_manifest,
46 payments::mojom::PaymentAppManifestError* out_error, 51 payments::mojom::PaymentAppManifestError* out_error,
47 payments::mojom::PaymentAppManifestPtr manifest, 52 payments::mojom::PaymentAppManifestPtr manifest,
48 payments::mojom::PaymentAppManifestError error) { 53 payments::mojom::PaymentAppManifestError error) {
54 *called = true;
49 *out_manifest = std::move(manifest); 55 *out_manifest = std::move(manifest);
50 *out_error = error; 56 *out_error = error;
51 } 57 }
52 58
59 payments::mojom::PaymentAppManifestPtr CreatePaymentAppManifestForTest() {
60 payments::mojom::PaymentAppOptionPtr option =
61 payments::mojom::PaymentAppOption::New();
62 option->label = "Visa ****";
63 option->id = "payment-app-id";
64 option->icon = std::string("payment-app-icon");
65 option->enabled_methods.push_back("visa");
66
67 payments::mojom::PaymentAppManifestPtr manifest =
68 payments::mojom::PaymentAppManifest::New();
69 manifest->icon = std::string("payment-app-icon");
70 manifest->label = "Payment App";
71 manifest->options.push_back(std::move(option));
72
73 return manifest;
74 }
75
53 } // namespace 76 } // namespace
54 77
55 class PaymentAppManagerTest : public testing::Test { 78 class PaymentAppManagerTest : public testing::Test {
56 public: 79 public:
57 PaymentAppManagerTest() 80 PaymentAppManagerTest()
58 : thread_bundle_( 81 : thread_bundle_(
59 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), 82 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)),
60 embedded_worker_helper_(new EmbeddedWorkerTestHelper(base::FilePath())), 83 embedded_worker_helper_(new EmbeddedWorkerTestHelper(base::FilePath())),
61 storage_partition_impl_(new StoragePartitionImpl( 84 storage_partition_impl_(
62 embedded_worker_helper_->browser_context(), base::FilePath(), 85 new StoragePartitionImpl(embedded_worker_helper_->browser_context(),
63 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, 86 base::FilePath(),
64 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr)) { 87 nullptr,
65 88 nullptr,
89 nullptr,
90 nullptr,
91 nullptr,
92 nullptr,
93 nullptr,
94 nullptr,
95 nullptr,
96 nullptr,
97 nullptr,
98 nullptr,
99 nullptr,
100 nullptr)),
please use gerrit instead 2016/12/15 19:24:31 Oh boy! Is this clang-format?
zino 2016/12/15 23:33:57 Yes :( But, fortuneately the problem was solved af
please use gerrit instead 2016/12/16 14:25:56 Much better.
101 sw_registration_id_(0) {
66 embedded_worker_helper_->context_wrapper()->set_storage_partition( 102 embedded_worker_helper_->context_wrapper()->set_storage_partition(
67 storage_partition_impl_.get()); 103 storage_partition_impl_.get());
68 104
69 payment_app_context_ = 105 payment_app_context_ = new PaymentAppContextImpl();
70 new PaymentAppContextImpl(embedded_worker_helper_->context_wrapper()); 106 payment_app_context_->Init(embedded_worker_helper_->context_wrapper());
71 107
72 bool called = false; 108 bool called = false;
73 embedded_worker_helper_->context()->RegisterServiceWorker( 109 embedded_worker_helper_->context()->RegisterServiceWorker(
74 GURL(kServiceWorkerPattern), GURL(kServiceWorkerScript), NULL, 110 GURL(kServiceWorkerPattern), GURL(kServiceWorkerScript), NULL,
75 base::Bind(&RegisterServiceWorkerCallback, &called, 111 base::Bind(&RegisterServiceWorkerCallback, &called,
76 &sw_registration_id_)); 112 &sw_registration_id_));
77 base::RunLoop().RunUntilIdle(); 113 base::RunLoop().RunUntilIdle();
78 EXPECT_TRUE(called); 114 EXPECT_TRUE(called);
79 115
80 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request = 116 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request =
81 mojo::GetProxy(&service_); 117 mojo::GetProxy(&service_);
82 payment_app_context_->CreateService(std::move(request)); 118 payment_app_context_->CreatePaymentAppManager(std::move(request));
83 base::RunLoop().RunUntilIdle(); 119 base::RunLoop().RunUntilIdle();
84 120
85 manager_ = payment_app_context_->services_.begin()->first; 121 manager_ = payment_app_context_->payment_app_managers_.begin()->first;
86 EXPECT_TRUE(manager_); 122 EXPECT_NE(nullptr, manager_);
87 } 123 }
88 124
89 ~PaymentAppManagerTest() override { 125 ~PaymentAppManagerTest() override {
90 payment_app_context_->Shutdown(); 126 payment_app_context_->Shutdown();
91 base::RunLoop().RunUntilIdle(); 127 base::RunLoop().RunUntilIdle();
92 } 128 }
93 129
94 void SetManifest(const std::string& scope, 130 void SetManifest(const std::string& scope,
95 payments::mojom::PaymentAppManifestPtr manifest, 131 payments::mojom::PaymentAppManifestPtr manifest,
96 const PaymentAppManager::SetManifestCallback& callback) { 132 const PaymentAppManager::SetManifestCallback& callback) {
(...skipping 15 matching lines...) Expand all
112 scoped_refptr<PaymentAppContextImpl> payment_app_context_; 148 scoped_refptr<PaymentAppContextImpl> payment_app_context_;
113 payments::mojom::PaymentAppManagerPtr service_; 149 payments::mojom::PaymentAppManagerPtr service_;
114 150
115 // Owned by payment_app_context_. 151 // Owned by payment_app_context_.
116 PaymentAppManager* manager_; 152 PaymentAppManager* manager_;
117 153
118 DISALLOW_COPY_AND_ASSIGN(PaymentAppManagerTest); 154 DISALLOW_COPY_AND_ASSIGN(PaymentAppManagerTest);
119 }; 155 };
120 156
121 TEST_F(PaymentAppManagerTest, SetAndGetManifest) { 157 TEST_F(PaymentAppManagerTest, SetAndGetManifest) {
122 payments::mojom::PaymentAppOptionPtr option = 158 bool called = false;
123 payments::mojom::PaymentAppOption::New(); 159 payments::mojom::PaymentAppManifestError error = payments::mojom::
124 option->label = "Visa ****"; 160 PaymentAppManifestError::MANIFEST_STORAGE_OPERATION_FAILED;
125 option->id = "payment-app-id"; 161 SetManifest(kServiceWorkerPattern, CreatePaymentAppManifestForTest(),
126 option->icon = std::string("payment-app-icon"); 162 base::Bind(&SetManifestCallback, &called, &error));
127 option->enabled_methods.push_back("visa");
128 163
129 payments::mojom::PaymentAppManifestPtr manifest = 164 ASSERT_TRUE(called);
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); 165 ASSERT_EQ(error, payments::mojom::PaymentAppManifestError::NONE);
140 166
167 called = false;
141 payments::mojom::PaymentAppManifestPtr read_manifest; 168 payments::mojom::PaymentAppManifestPtr read_manifest;
142 payments::mojom::PaymentAppManifestError read_error; 169 payments::mojom::PaymentAppManifestError read_error = payments::mojom::
143 GetManifest(kServiceWorkerPattern, 170 PaymentAppManifestError::MANIFEST_STORAGE_OPERATION_FAILED;
144 base::Bind(&GetManifestCallback, &read_manifest, &read_error)); 171 GetManifest(kServiceWorkerPattern, base::Bind(&GetManifestCallback, &called,
172 &read_manifest, &read_error));
145 173
174 ASSERT_TRUE(called);
146 ASSERT_EQ(read_error, payments::mojom::PaymentAppManifestError::NONE); 175 ASSERT_EQ(read_error, payments::mojom::PaymentAppManifestError::NONE);
147 EXPECT_EQ(read_manifest->icon, std::string("payment-app-icon")); 176 EXPECT_EQ(read_manifest->icon, std::string("payment-app-icon"));
148 EXPECT_EQ(read_manifest->label, "Payment App"); 177 EXPECT_EQ(read_manifest->label, "Payment App");
149 ASSERT_EQ(read_manifest->options.size(), 1U); 178 ASSERT_EQ(read_manifest->options.size(), 1U);
150 EXPECT_EQ(read_manifest->options[0]->icon, std::string("payment-app-icon")); 179 EXPECT_EQ(read_manifest->options[0]->icon, std::string("payment-app-icon"));
151 EXPECT_EQ(read_manifest->options[0]->label, "Visa ****"); 180 EXPECT_EQ(read_manifest->options[0]->label, "Visa ****");
152 EXPECT_EQ(read_manifest->options[0]->id, "payment-app-id"); 181 EXPECT_EQ(read_manifest->options[0]->id, "payment-app-id");
153 ASSERT_EQ(read_manifest->options[0]->enabled_methods.size(), 1U); 182 ASSERT_EQ(read_manifest->options[0]->enabled_methods.size(), 1U);
154 EXPECT_EQ(read_manifest->options[0]->enabled_methods[0], "visa"); 183 EXPECT_EQ(read_manifest->options[0]->enabled_methods[0], "visa");
155 } 184 }
156 185
186 TEST_F(PaymentAppManagerTest, SetManifestWithoutAssociatedServiceWorker) {
187 bool called = false;
188 payments::mojom::PaymentAppManifestError error =
189 payments::mojom::PaymentAppManifestError::NONE;
190 SetManifest(kUnregisteredServiceWorkerPattern,
191 CreatePaymentAppManifestForTest(),
192 base::Bind(&SetManifestCallback, &called, &error));
193
194 ASSERT_TRUE(called);
195 EXPECT_EQ(error, payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER);
196 }
197
157 TEST_F(PaymentAppManagerTest, GetManifestWithoutAssociatedServiceWorker) { 198 TEST_F(PaymentAppManagerTest, GetManifestWithoutAssociatedServiceWorker) {
199 bool called = false;
158 payments::mojom::PaymentAppManifestPtr read_manifest; 200 payments::mojom::PaymentAppManifestPtr read_manifest;
159 payments::mojom::PaymentAppManifestError read_error; 201 payments::mojom::PaymentAppManifestError read_error =
160 GetManifest(kServiceWorkerPattern, 202 payments::mojom::PaymentAppManifestError::NONE;
161 base::Bind(&GetManifestCallback, &read_manifest, &read_error)); 203 GetManifest(
204 kUnregisteredServiceWorkerPattern,
205 base::Bind(&GetManifestCallback, &called, &read_manifest, &read_error));
162 206
207 ASSERT_TRUE(called);
208 EXPECT_EQ(read_error,
209 payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER);
210 }
211
212 TEST_F(PaymentAppManagerTest, GetManifestWithNoSavedManifest) {
213 bool called = false;
214 payments::mojom::PaymentAppManifestPtr read_manifest;
215 payments::mojom::PaymentAppManifestError read_error =
216 payments::mojom::PaymentAppManifestError::NONE;
217 GetManifest(kServiceWorkerPattern, base::Bind(&GetManifestCallback, &called,
218 &read_manifest, &read_error));
219
220 ASSERT_TRUE(called);
163 EXPECT_EQ(read_error, payments::mojom::PaymentAppManifestError:: 221 EXPECT_EQ(read_error, payments::mojom::PaymentAppManifestError::
164 MANIFEST_STORAGE_OPERATION_FAILED); 222 MANIFEST_STORAGE_OPERATION_FAILED);
165 } 223 }
166 224
167 } // namespace content 225 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698