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

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

Issue 2556433002: PaymentApp: Implement GetAllManifests() in PaymentAppContext. (Closed)
Patch Set: 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_content_unittest_base.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 6
24 namespace content { 7 namespace content {
25 namespace { 8 namespace {
26 9
27 const char kServiceWorkerPattern[] = "https://example.com/a"; 10 const char kServiceWorkerPattern[] = "https://example.com/a";
28 const char kServiceWorkerScript[] = "https://example.com/a/script.js"; 11 const char kServiceWorkerScript[] = "https://example.com/a/script.js";
29 12
30 void RegisterServiceWorkerCallback(bool* called,
31 int64_t* store_registration_id,
32 ServiceWorkerStatusCode status,
33 const std::string& status_message,
34 int64_t registration_id) {
35 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status);
36 *called = true;
37 *store_registration_id = registration_id;
38 }
39
40 void SetManifestCallback(payments::mojom::PaymentAppManifestError* out_error, 13 void SetManifestCallback(payments::mojom::PaymentAppManifestError* out_error,
41 payments::mojom::PaymentAppManifestError error) { 14 payments::mojom::PaymentAppManifestError error) {
42 *out_error = error; 15 *out_error = error;
43 } 16 }
44 17
45 void GetManifestCallback(payments::mojom::PaymentAppManifestPtr* out_manifest, 18 void GetManifestCallback(payments::mojom::PaymentAppManifestPtr* out_manifest,
46 payments::mojom::PaymentAppManifestError* out_error, 19 payments::mojom::PaymentAppManifestError* out_error,
47 payments::mojom::PaymentAppManifestPtr manifest, 20 payments::mojom::PaymentAppManifestPtr manifest,
48 payments::mojom::PaymentAppManifestError error) { 21 payments::mojom::PaymentAppManifestError error) {
49 *out_manifest = std::move(manifest); 22 *out_manifest = std::move(manifest);
50 *out_error = error; 23 *out_error = error;
51 } 24 }
52 25
53 } // namespace 26 } // namespace
54 27
55 class PaymentAppManagerTest : public testing::Test { 28 class PaymentAppManagerTest : public PaymentAppContentUnitTestBase {
56 public: 29 public:
57 PaymentAppManagerTest() 30 PaymentAppManagerTest() {
58 : thread_bundle_( 31 manager_ = CreatePaymentAppManager(GURL(kServiceWorkerPattern),
59 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)), 32 GURL(kServiceWorkerScript));
60 embedded_worker_helper_(new EmbeddedWorkerTestHelper(base::FilePath())),
61 storage_partition_impl_(new StoragePartitionImpl(
62 embedded_worker_helper_->browser_context(), base::FilePath(),
63 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
64 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr)) {
65 embedded_worker_helper_->context_wrapper()->set_storage_partition(
66 storage_partition_impl_.get());
67
68 payment_app_context_ = new PaymentAppContextImpl();
69 payment_app_context_->Init(embedded_worker_helper_->context_wrapper());
70
71 bool called = false;
72 embedded_worker_helper_->context()->RegisterServiceWorker(
73 GURL(kServiceWorkerPattern), GURL(kServiceWorkerScript), NULL,
74 base::Bind(&RegisterServiceWorkerCallback, &called,
75 &sw_registration_id_));
76 base::RunLoop().RunUntilIdle();
77 EXPECT_TRUE(called);
78
79 mojo::InterfaceRequest<payments::mojom::PaymentAppManager> request =
80 mojo::GetProxy(&service_);
81 payment_app_context_->CreateService(std::move(request));
82 base::RunLoop().RunUntilIdle();
83
84 manager_ = payment_app_context_->services_.begin()->first;
85 EXPECT_TRUE(manager_); 33 EXPECT_TRUE(manager_);
86 } 34 }
87 35
88 ~PaymentAppManagerTest() override { 36 PaymentAppManager* GetPaymentAppManager() const { return manager_; }
89 payment_app_context_->Shutdown();
90 base::RunLoop().RunUntilIdle();
91 }
92
93 void SetManifest(const std::string& scope,
94 payments::mojom::PaymentAppManifestPtr manifest,
95 const PaymentAppManager::SetManifestCallback& callback) {
96 manager_->SetManifest(scope, std::move(manifest), callback);
97 base::RunLoop().RunUntilIdle();
98 }
99
100 void GetManifest(const std::string& scope,
101 const PaymentAppManager::GetManifestCallback& callback) {
102 manager_->GetManifest(scope, callback);
103 base::RunLoop().RunUntilIdle();
104 }
105 37
106 private: 38 private:
107 std::unique_ptr<TestBrowserThreadBundle> thread_bundle_;
108 std::unique_ptr<EmbeddedWorkerTestHelper> embedded_worker_helper_;
109 std::unique_ptr<StoragePartitionImpl> storage_partition_impl_;
110 int64_t sw_registration_id_;
111 scoped_refptr<PaymentAppContextImpl> payment_app_context_;
112 payments::mojom::PaymentAppManagerPtr service_;
113
114 // Owned by payment_app_context_.
115 PaymentAppManager* manager_; 39 PaymentAppManager* manager_;
116 40
117 DISALLOW_COPY_AND_ASSIGN(PaymentAppManagerTest); 41 DISALLOW_COPY_AND_ASSIGN(PaymentAppManagerTest);
118 }; 42 };
119 43
120 TEST_F(PaymentAppManagerTest, SetAndGetManifest) { 44 TEST_F(PaymentAppManagerTest, SetAndGetManifest) {
121 payments::mojom::PaymentAppOptionPtr option = 45 payments::mojom::PaymentAppOptionPtr option =
122 payments::mojom::PaymentAppOption::New(); 46 payments::mojom::PaymentAppOption::New();
123 option->label = "Visa ****"; 47 option->label = "Visa ****";
124 option->id = "payment-app-id"; 48 option->id = "payment-app-id";
125 option->icon = std::string("payment-app-icon"); 49 option->icon = std::string("payment-app-icon");
126 option->enabled_methods.push_back("visa"); 50 option->enabled_methods.push_back("visa");
127 51
128 payments::mojom::PaymentAppManifestPtr manifest = 52 payments::mojom::PaymentAppManifestPtr manifest =
129 payments::mojom::PaymentAppManifest::New(); 53 payments::mojom::PaymentAppManifest::New();
130 manifest->icon = std::string("payment-app-icon"); 54 manifest->icon = std::string("payment-app-icon");
131 manifest->label = "Payment App"; 55 manifest->label = "Payment App";
132 manifest->options.push_back(std::move(option)); 56 manifest->options.push_back(std::move(option));
133 57
134 payments::mojom::PaymentAppManifestError error; 58 payments::mojom::PaymentAppManifestError error;
135 SetManifest(kServiceWorkerPattern, std::move(manifest), 59 DCHECK(GetPaymentAppManager());
136 base::Bind(&SetManifestCallback, &error)); 60 SetManifest(GetPaymentAppManager(), kServiceWorkerPattern,
61 std::move(manifest), base::Bind(&SetManifestCallback, &error));
137 62
138 ASSERT_EQ(error, payments::mojom::PaymentAppManifestError::NONE); 63 ASSERT_EQ(error, payments::mojom::PaymentAppManifestError::NONE);
139 64
140 payments::mojom::PaymentAppManifestPtr read_manifest; 65 payments::mojom::PaymentAppManifestPtr read_manifest;
141 payments::mojom::PaymentAppManifestError read_error; 66 payments::mojom::PaymentAppManifestError read_error;
142 GetManifest(kServiceWorkerPattern, 67 GetManifest(GetPaymentAppManager(), kServiceWorkerPattern,
143 base::Bind(&GetManifestCallback, &read_manifest, &read_error)); 68 base::Bind(&GetManifestCallback, &read_manifest, &read_error));
144 69
145 ASSERT_EQ(read_error, payments::mojom::PaymentAppManifestError::NONE); 70 ASSERT_EQ(read_error, payments::mojom::PaymentAppManifestError::NONE);
146 EXPECT_EQ(read_manifest->icon, std::string("payment-app-icon")); 71 EXPECT_EQ(read_manifest->icon, std::string("payment-app-icon"));
147 EXPECT_EQ(read_manifest->label, "Payment App"); 72 EXPECT_EQ(read_manifest->label, "Payment App");
148 ASSERT_EQ(read_manifest->options.size(), 1U); 73 ASSERT_EQ(read_manifest->options.size(), 1U);
149 EXPECT_EQ(read_manifest->options[0]->icon, std::string("payment-app-icon")); 74 EXPECT_EQ(read_manifest->options[0]->icon, std::string("payment-app-icon"));
please use gerrit instead 2016/12/12 20:17:40 Same question about std::string() wrappers in this
150 EXPECT_EQ(read_manifest->options[0]->label, "Visa ****"); 75 EXPECT_EQ(read_manifest->options[0]->label, "Visa ****");
151 EXPECT_EQ(read_manifest->options[0]->id, "payment-app-id"); 76 EXPECT_EQ(read_manifest->options[0]->id, "payment-app-id");
152 ASSERT_EQ(read_manifest->options[0]->enabled_methods.size(), 1U); 77 ASSERT_EQ(read_manifest->options[0]->enabled_methods.size(), 1U);
153 EXPECT_EQ(read_manifest->options[0]->enabled_methods[0], "visa"); 78 EXPECT_EQ(read_manifest->options[0]->enabled_methods[0], "visa");
154 } 79 }
155 80
156 TEST_F(PaymentAppManagerTest, GetManifestWithoutAssociatedServiceWorker) { 81 TEST_F(PaymentAppManagerTest, GetManifestWithoutAssociatedServiceWorker) {
157 payments::mojom::PaymentAppManifestPtr read_manifest; 82 payments::mojom::PaymentAppManifestPtr read_manifest;
158 payments::mojom::PaymentAppManifestError read_error; 83 payments::mojom::PaymentAppManifestError read_error;
159 GetManifest(kServiceWorkerPattern, 84 GetManifest(GetPaymentAppManager(), kServiceWorkerPattern,
160 base::Bind(&GetManifestCallback, &read_manifest, &read_error)); 85 base::Bind(&GetManifestCallback, &read_manifest, &read_error));
161 86
162 EXPECT_EQ(read_error, payments::mojom::PaymentAppManifestError:: 87 EXPECT_EQ(read_error, payments::mojom::PaymentAppManifestError::
163 MANIFEST_STORAGE_OPERATION_FAILED); 88 MANIFEST_STORAGE_OPERATION_FAILED);
164 } 89 }
165 90
166 } // namespace content 91 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698