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

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

Issue 2497983002: PaymentApp: Implement PaymentAppManager.setManifest(). (Closed)
Patch Set: PaymentApp: Implement PaymentAppManager.setManifest(). Created 4 years, 1 month 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
(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
please use gerrit instead 2016/11/15 20:04:27 No newline.
zino 2016/11/16 18:12:58 Done.
26 namespace {
27
28 const char kServiceWorkerPattern[] = "https://example.com/a";
29 const char kServiceWorkerScript[] = "https://example.com/a/script.js";
30 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData";
31
32 void RegisterServiceWorkerCallback(bool* called,
33 int64_t* store_registration_id,
34 ServiceWorkerStatusCode status,
35 const std::string& status_message,
36 int64_t registration_id) {
37 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status);
38 *called = true;
39 *store_registration_id = registration_id;
40 }
41
42 void SetManifestCallback(payments::mojom::PaymentAppManifestError* out_error,
43 payments::mojom::PaymentAppManifestError error) {
44 *out_error = error;
45 }
46
47 void ReadManifestDataCallback(std::vector<std::string>* out_data,
48 const std::vector<std::string>& data,
49 ServiceWorkerStatusCode status) {
50 *out_data = data;
51 }
52
53 } // namespace
54
55 class PaymentAppManagerTest : public testing::Test {
56 public:
57 PaymentAppManagerTest()
58 : thread_bundle_(
59 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)) {
60 }
please use gerrit instead 2016/11/15 20:04:27 If you have a constructor, you should have a destr
zino 2016/11/16 18:12:59 Done.
61
62 void SetUp() override {
please use gerrit instead 2016/11/15 20:04:27 SetUp is usually not used. You can put all these f
zino 2016/11/16 18:12:58 Done.
63 CreateTestHelper();
64 CreateStoragePartition();
65 CreatePaymentAppContext();
66 CreateServiceWorkerRegistration();
67 CreatePaymentAppManager();
68 }
69
70 void TearDown() override {
please use gerrit instead 2016/11/15 20:04:27 TearDown is usually not used. You can put all thes
zino 2016/11/16 18:12:58 Done.
71 payment_app_context_->Shutdown();
72 base::RunLoop().RunUntilIdle();
73 payment_app_context_ = nullptr;
please use gerrit instead 2016/11/15 20:04:27 Clearing the scoped_refptr should not be necessary
zino 2016/11/16 18:12:58 Done.
74 }
75
76 void SetManifest(const std::string& scope,
77 payments::mojom::PaymentAppManifestPtr manifest,
78 const PaymentAppManager::SetManifestCallback& callback) {
79 manager_->SetManifest(scope, std::move(manifest), callback);
80 base::RunLoop().RunUntilIdle();
81 }
82
83 void ReadManifestData(std::vector<std::string>* out_data) {
84 embedded_worker_helper_->context_wrapper()->GetRegistrationUserData(
85 sw_registration_id_, {{kPaymentAppManifestDataKey}},
86 base::Bind(&ReadManifestDataCallback, out_data));
87 base::RunLoop().RunUntilIdle();
88 }
89
90 private:
91 void CreateTestHelper() {
92 embedded_worker_helper_.reset(
93 new EmbeddedWorkerTestHelper(base::FilePath()));
please use gerrit instead 2016/11/15 20:04:27 When a function is used only once, it's generally
zino 2016/11/16 18:12:59 Done.
94 }
95
96 void CreateStoragePartition() {
97 storage_partition_impl_.reset(new StoragePartitionImpl(
98 embedded_worker_helper_->browser_context(), base::FilePath(), nullptr,
99 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
100 nullptr, nullptr, nullptr, nullptr, nullptr));
101 embedded_worker_helper_->context_wrapper()->set_storage_partition(
102 storage_partition_impl_.get());
please use gerrit instead 2016/11/15 20:04:27 Also can be "inlined".
zino 2016/11/16 18:12:59 Done.
103 }
104
105 void CreatePaymentAppContext() {
106 payment_app_context_ =
107 new PaymentAppContext(embedded_worker_helper_->context_wrapper());
please use gerrit instead 2016/11/15 20:04:27 Also can be "inlined".
zino 2016/11/16 18:12:59 Done.
108 }
109
110 void CreateServiceWorkerRegistration() {
111 bool called = false;
112 embedded_worker_helper_->context()->RegisterServiceWorker(
113 GURL(kServiceWorkerPattern), GURL(kServiceWorkerScript), NULL,
114 base::Bind(&RegisterServiceWorkerCallback, &called,
115 &sw_registration_id_));
116 base::RunLoop().RunUntilIdle();
please use gerrit instead 2016/11/15 20:04:27 Is there no way to avoid running the loop?
zino 2016/11/16 18:12:59 The RegisterServiceWorker() will run asynchronousl
117 EXPECT_TRUE(called);
please use gerrit instead 2016/11/15 20:04:27 Also can be "inlined".
zino 2016/11/16 18:12:59 Done.
118 }
119
120 void CreatePaymentAppManager() {
121 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request =
122 mojo::GetProxy(&service_);
123 payment_app_context_->CreateService(std::move(request));
124 base::RunLoop().RunUntilIdle();
please use gerrit instead 2016/11/15 20:04:27 Is there no way to avoid running the loop?
zino 2016/11/16 18:12:59 ditto
125 manager_ = payment_app_context_->services_.begin()->first;
126 EXPECT_TRUE(manager_);
please use gerrit instead 2016/11/15 20:04:27 Also can be "inlined".
zino 2016/11/16 18:12:59 Done.
127 }
128
129 std::unique_ptr<TestBrowserThreadBundle> thread_bundle_;
130 std::unique_ptr<EmbeddedWorkerTestHelper> embedded_worker_helper_;
131 std::unique_ptr<StoragePartitionImpl> storage_partition_impl_;
132 int64_t sw_registration_id_;
133 scoped_refptr<PaymentAppContext> payment_app_context_;
134 payments::mojom::PaymentAppManagerPtr service_;
135 PaymentAppManager* manager_;
please use gerrit instead 2016/11/15 20:04:26 Add a note about ownership of this pointer. If you
zino 2016/11/16 18:12:58 This class doesn't have the ownership of this poin
136 };
please use gerrit instead 2016/11/15 20:04:26 DISALLOW_COPY_AND_ASSIGN is always a good idea.
zino 2016/11/16 18:12:58 Done.
137
138 TEST_F(PaymentAppManagerTest, SetManifest) {
139 payments::mojom::PaymentAppOptionPtr option =
140 payments::mojom::PaymentAppOption::New();
141 option->label = "Visa ****";
142 option->id = "payment-app-id";
143 option->icon = std::string("payment-app-icon");
144 option->enabled_methods.push_back("visa");
145
146 payments::mojom::PaymentAppManifestPtr manifest =
147 payments::mojom::PaymentAppManifest::New();
148 manifest->icon = std::string("payment-app-icon");
149 manifest->label = "Payment App";
150 manifest->options.push_back(std::move(option));
151
152 payments::mojom::PaymentAppManifestError error;
153 SetManifest(kServiceWorkerPattern, std::move(manifest),
154 base::Bind(&SetManifestCallback, &error));
155
156 EXPECT_EQ(error, payments::mojom::PaymentAppManifestError::NONE);
157
158 std::vector<std::string> data;
159 ReadManifestData(&data);
160 EXPECT_EQ(data.size(), 1UL);
please use gerrit instead 2016/11/15 20:04:27 ASSERT_EQ If the data size is not 1UL, you want t
zino 2016/11/16 18:12:58 Done.
161 EXPECT_EQ(
162 "\n\vPayment App\x12\x10payment-app-icon\x1A"
163 "3\n\tVisa ****\x12\x10payment-app-icon\x1A\xEpayment-app-id\"\x4visa",
164 data[0]);
165 }
166
167 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698