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