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

Side by Side Diff: chrome/browser/webshare/share_service_impl_unittest.cc

Issue 2564483003: Default share to Share Target: Partial impl. of Web Share for Desktop. (Closed)
Patch Set: Updated documentation Created 3 years, 11 months 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 <memory>
6
5 #include "base/bind.h" 7 #include "base/bind.h"
6 #include "base/callback.h" 8 #include "base/callback.h"
7 #include "base/run_loop.h" 9 #include "base/run_loop.h"
8 #include "chrome/browser/webshare/share_service_impl.h" 10 #include "chrome/browser/webshare/share_service_impl.h"
9 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 11 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
10 #include "mojo/public/cpp/bindings/interface_request.h" 12 #include "mojo/public/cpp/bindings/interface_request.h"
13 #include "mojo/public/cpp/bindings/strong_binding.h"
11 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
12 #include "url/gurl.h" 15 #include "url/gurl.h"
13 16
14 class ShareServiceTest : public ChromeRenderViewHostTestHarness { 17 namespace {
18
19 constexpr char kTitle[] = "My title";
20 constexpr char kText[] = "My text";
21 constexpr char kUrlSpec[] = "https://www.google.com/";
22
23 class ShareServiceTestImpl : public ShareServiceImpl {
15 public: 24 public:
16 ShareServiceTest() = default; 25 static ShareServiceTestImpl* Create(
17 ~ShareServiceTest() override = default; 26 blink::mojom::ShareServiceRequest request) {
27 std::unique_ptr<ShareServiceTestImpl> share_service_helper =
28 base::MakeUnique<ShareServiceTestImpl>();
29 ShareServiceTestImpl* share_service_helper_raw = share_service_helper.get();
30 mojo::MakeStrongBinding(std::move(share_service_helper),
31 std::move(request));
32 return share_service_helper_raw;
33 }
34
35 const std::string& GetLastUsedTargetURL() { return last_used_target_url_; }
36
37 private:
38 std::string last_used_target_url_;
39
40 void OpenTargetURL(const GURL& target_url) override {
41 last_used_target_url_ = target_url.spec();
42 }
43 };
44
45 class ShareServiceImplUnittest : public ChromeRenderViewHostTestHarness {
46 public:
47 ShareServiceImplUnittest() = default;
48 ~ShareServiceImplUnittest() override = default;
18 49
19 void SetUp() override { 50 void SetUp() override {
20 ChromeRenderViewHostTestHarness::SetUp(); 51 ChromeRenderViewHostTestHarness::SetUp();
21 52
22 ShareServiceImpl::Create(mojo::GetProxy(&share_service_)); 53 share_service_helper_ =
23 } 54 ShareServiceTestImpl::Create(mojo::GetProxy(&share_service_));
24 55 }
25 void TearDown() override { 56
26 ChromeRenderViewHostTestHarness::TearDown(); 57 void TearDown() override { ChromeRenderViewHostTestHarness::TearDown(); }
27 } 58
28 59 void DidShare(const std::string& expected_target_url,
29 void DidShare(const base::Optional<std::string>& expected, 60 const base::Optional<std::string>& expected_param,
30 const base::Optional<std::string>& str) { 61 const base::Optional<std::string>& param) {
31 EXPECT_EQ(expected, str); 62 EXPECT_EQ(expected_param, param);
63 std::string target_url = share_service_helper_->GetLastUsedTargetURL();
64 EXPECT_EQ(expected_target_url, target_url);
32 65
33 if (!on_callback_.is_null()) 66 if (!on_callback_.is_null())
34 on_callback_.Run(); 67 on_callback_.Run();
35 } 68 }
36 69
37 blink::mojom::ShareServicePtr share_service_; 70 blink::mojom::ShareServicePtr share_service_;
71 ShareServiceTestImpl* share_service_helper_;
38 base::Closure on_callback_; 72 base::Closure on_callback_;
39 }; 73 };
40 74
75 } // namespace
76
41 // Basic test to check the Share method calls the callback with the expected 77 // Basic test to check the Share method calls the callback with the expected
42 // parameters. 78 // parameters.
43 TEST_F(ShareServiceTest, ShareCallbackParams) { 79 TEST_F(ShareServiceImplUnittest, ShareCallbackParams) {
44 const GURL url("https://www.google.com"); 80 std::string expected_url =
81 "https://wicg.github.io/web-share-target/demos/"
82 "sharetarget.html?title=My%20title&text=My%20text&url=https%3A%2F%2Fwww."
83 "google.com%2F";
84
85 const GURL url(kUrlSpec);
Matt Giuca 2017/01/05 03:09:26 Can this be constexpr? (Not sure... I'm just picki
Sam McNally 2017/01/05 04:08:37 No. An object can only be constexpr if the class h
constantina 2017/01/09 02:12:43 Acknowledged.
86 base::Callback<void(const base::Optional<std::string>&)> callback =
87 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this),
88 expected_url, base::Optional<std::string>());
45 89
46 base::RunLoop run_loop; 90 base::RunLoop run_loop;
47 on_callback_ = run_loop.QuitClosure(); 91 on_callback_ = run_loop.QuitClosure();
48 92
49 base::Callback<void(const base::Optional<std::string>&)> callback = 93 share_service_->Share(kTitle, kText, url, callback);
50 base::Bind(
51 &ShareServiceTest::DidShare, base::Unretained(this),
52 base::Optional<std::string>("Not implemented: navigator.share"));
53 share_service_->Share("title", "text", url, callback);
54 94
55 run_loop.Run(); 95 run_loop.Run();
56 } 96 }
97
98 // Replace various numbers of placeholders in various orders. Placeholders are
99 // adjacent to eachother; there are no padding characters.
100 TEST_F(ShareServiceImplUnittest, ReplacePlaceholders) {
Matt Giuca 2017/01/05 03:09:26 Excellent test, very thorough.
constantina 2017/01/09 02:12:43 :)
101 const GURL url(kUrlSpec);
Matt Giuca 2017/01/05 03:09:26 As above: constexpr?
constantina 2017/01/09 02:12:43 Acknowledged.
102 std::string url_template_filled;
103 int error;
104
105 // No placeholders
106 std::string url_template = "blank";
107 error = ShareServiceImpl::ReplacePlaceholders(
108 url_template, kTitle, kText, url, &url_template_filled);
109 EXPECT_EQ(0, error);
110 EXPECT_EQ("blank", url_template_filled);
111
112 // Empty |url_template|
113 url_template = "";
114 error = ShareServiceImpl::ReplacePlaceholders(
115 url_template, kTitle, kText, url, &url_template_filled);
116 EXPECT_EQ(0, error);
117 EXPECT_EQ("", url_template_filled);
118
119 // One title placeholder.
120 url_template = "%{title}";
121 error = ShareServiceImpl::ReplacePlaceholders(
122 url_template, kTitle, kText, url, &url_template_filled);
123 EXPECT_EQ(0, error);
124 EXPECT_EQ("My%20title", url_template_filled);
125
126 // One text placeholder.
127 url_template = "%{text}";
128 error = ShareServiceImpl::ReplacePlaceholders(
129 url_template, kTitle, kText, url, &url_template_filled);
130 EXPECT_EQ(0, error);
131 EXPECT_EQ("My%20text", url_template_filled);
132
133 // One url placeholder.
134 url_template = "%{url}";
135 error = ShareServiceImpl::ReplacePlaceholders(
136 url_template, kTitle, kText, url, &url_template_filled);
137 EXPECT_EQ(0, error);
138 EXPECT_EQ("https%3A%2F%2Fwww.google.com%2F", url_template_filled);
139
140 // One of each placeholder, in title, text, url order.
141 url_template = "%{title}%{text}%{url}";
142 error = ShareServiceImpl::ReplacePlaceholders(
143 url_template, kTitle, kText, url, &url_template_filled);
144 EXPECT_EQ(0, error);
145 EXPECT_EQ("My%20titleMy%20texthttps%3A%2F%2Fwww.google.com%2F",
146 url_template_filled);
147
148 // One of each placeholder, in url, text, title order.
149 url_template = "%{url}%{text}%{title}";
150 error = ShareServiceImpl::ReplacePlaceholders(
151 url_template, kTitle, kText, url, &url_template_filled);
152 EXPECT_EQ(0, error);
153 EXPECT_EQ("https%3A%2F%2Fwww.google.com%2FMy%20textMy%20title",
154 url_template_filled);
155
156 // Two of each placeholder, some next to each other, others not.
157 url_template = "%{title}%{url}%{text}%{text}%{title}%{url}";
158 error = ShareServiceImpl::ReplacePlaceholders(
159 url_template, kTitle, kText, url, &url_template_filled);
160 EXPECT_EQ(0, error);
161 EXPECT_EQ(
162 "My%20titlehttps%3A%2F%2Fwww.google.com%2FMy%20textMy%20textMy%20"
163 "titlehttps%3A%2F%2Fwww.google.com%2F",
164 url_template_filled);
165
166 // Placeholders are in a query string, as values. The expected use case.
167 // Two of each placeholder, some next to each other, others not.
168 url_template =
169 "?title=%{title}&url=%{url}&text=%{text}&text=%{text}&"
170 "title=%{title}&url=%{url}";
171 error = ShareServiceImpl::ReplacePlaceholders(
172 url_template, kTitle, kText, url, &url_template_filled);
173 EXPECT_EQ(0, error);
174 EXPECT_EQ(
175 "?title=My%20title&url=https%3A%2F%2Fwww.google.com%2F&text=My%20text&"
176 "text=My%20text&title=My%20title&url=https%3A%2F%2Fwww.google.com%2F",
177 url_template_filled);
178
179 // TODO(constantina): come back to this and think about returned value
Matt Giuca 2017/01/05 03:09:26 Do this before submitting. (Note: If you have a T
constantina 2017/01/09 02:12:43 Done.
180 // Badly nested placeholders.
Matt Giuca 2017/01/05 03:09:26 Add a test for a placeholder with other characters
constantina 2017/01/09 02:12:43 Done.
181 url_template = "%{title";
182 error = ShareServiceImpl::ReplacePlaceholders(
183 url_template, kTitle, kText, url, &url_template_filled);
184 EXPECT_EQ(1, error);
185 EXPECT_EQ(url_template, url_template_filled);
186
187 url_template = "%{title%{text}}";
188 error = ShareServiceImpl::ReplacePlaceholders(
189 url_template, kTitle, kText, url, &url_template_filled);
190 EXPECT_EQ(1, error);
191 EXPECT_EQ(url_template, url_template_filled);
192
193 // Empty placeholder
194 url_template = "%{}";
195 error = ShareServiceImpl::ReplacePlaceholders(
196 url_template, kTitle, kText, url, &url_template_filled);
197 EXPECT_EQ(0, error);
198 EXPECT_EQ("", url_template_filled);
199
200 // Unexpected placeholders.
201 url_template = "%{nonexistentplaceholder}";
202 error = ShareServiceImpl::ReplacePlaceholders(
203 url_template, kTitle, kText, url, &url_template_filled);
204 EXPECT_EQ(0, error);
205 EXPECT_EQ("", url_template_filled);
206
207 // |url_template| with % escapes.
208 url_template = "%20%{title}%20";
209 error = ShareServiceImpl::ReplacePlaceholders(
210 url_template, kTitle, kText, url, &url_template_filled);
211 EXPECT_EQ(0, error);
212 EXPECT_EQ("%20My%20title%20", url_template_filled);
213
214 // Share data that contains percent escapes.
215 url_template = "%{title}";
216 error = ShareServiceImpl::ReplacePlaceholders(
217 url_template, "My%20title", kText, url, &url_template_filled);
218 EXPECT_EQ(0, error);
219 EXPECT_EQ("My%2520title", url_template_filled);
220
221 // Share data that contains placeholders. These should not be replaced.
222 url_template = "%{title}";
223 error = ShareServiceImpl::ReplacePlaceholders(
224 url_template, "%{title}", kText, url, &url_template_filled);
225 EXPECT_EQ(0, error);
226 EXPECT_EQ("%25%7Btitle%7D", url_template_filled);
227
228 // All characters that shouldn't be escaped.
229 url_template = "%{title}";
230 error =
231 ShareServiceImpl::ReplacePlaceholders(url_template,
232 "-_.!~*'()0123456789"
233 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
234 "abcdefghijklmnopqrstuvwxyz",
235 kText, url, &url_template_filled);
236 EXPECT_EQ(0, error);
237 EXPECT_EQ(
238 "-_.!~*'()0123456789"
239 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
240 "abcdefghijklmnopqrstuvwxyz",
241 url_template_filled);
242
243 // All characters that should be escaped.
244 url_template = "%{title}";
245 error = ShareServiceImpl::ReplacePlaceholders(
246 url_template, " \"#$%&+,/:;<=>?@[\\]^`{|}", kText, url,
247 &url_template_filled);
248 EXPECT_EQ(0, error);
249 EXPECT_EQ(
250 "%20%22%23%24%25%26%2B%2C%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E%60%7B%7C%"
251 "7D",
252 url_template_filled);
253
254 // Unicode chars
Matt Giuca 2017/01/05 03:09:26 Nit: "."
constantina 2017/01/09 02:12:44 Done.
255 url_template = "%{title}";
256 error = ShareServiceImpl::ReplacePlaceholders(url_template, "☻", kText, url,
Matt Giuca 2017/01/05 03:09:26 "\xe2\x98\xbb" instead of "☻". Also test a Latin-
constantina 2017/01/09 02:12:43 Done.
Matt Giuca 2017/01/09 03:56:54 Just to make it clear, add a comment explaining wh
257 &url_template_filled);
258 EXPECT_EQ(0, error);
259 EXPECT_EQ("%E2%98%BB", url_template_filled);
260 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698