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> | 5 #include <memory> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/webshare/share_service_impl.h" | 11 #include "chrome/browser/webshare/share_service_impl.h" |
12 #include "chrome/common/pref_names.h" | |
12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
14 #include "components/prefs/pref_registry_simple.h" | |
15 #include "components/prefs/scoped_user_pref_update.h" | |
16 #include "components/prefs/testing_pref_service.h" | |
13 #include "mojo/public/cpp/bindings/interface_request.h" | 17 #include "mojo/public/cpp/bindings/interface_request.h" |
14 #include "mojo/public/cpp/bindings/strong_binding.h" | 18 #include "mojo/public/cpp/bindings/strong_binding.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "url/gurl.h" | 20 #include "url/gurl.h" |
17 | 21 |
18 namespace { | 22 namespace { |
19 | 23 |
20 constexpr char kTitle[] = "My title"; | 24 constexpr char kTitle[] = "My title"; |
21 constexpr char kText[] = "My text"; | 25 constexpr char kText[] = "My text"; |
22 constexpr char kUrlSpec[] = "https://www.google.com/"; | 26 constexpr char kUrlSpec[] = "https://www.google.com/"; |
23 | 27 |
24 class ShareServiceTestImpl : public ShareServiceImpl { | 28 class ShareServiceTestImpl : public ShareServiceImpl { |
25 public: | 29 public: |
26 static ShareServiceTestImpl* Create( | 30 static ShareServiceTestImpl* Create( |
Matt Giuca
2017/02/03 02:40:24
Just noticed this (not directly applicable to this
constantina
2017/02/08 23:25:16
I am not sure. I will have to ask Sam or someone e
Sam McNally
2017/02/08 23:34:46
A StrongBinding ties the lifetime of the object to
constantina
2017/02/09 05:40:10
Done.
| |
27 blink::mojom::ShareServiceRequest request) { | 31 blink::mojom::ShareServiceRequest request) { |
28 std::unique_ptr<ShareServiceTestImpl> share_service_helper = | 32 std::unique_ptr<ShareServiceTestImpl> share_service_helper = |
29 base::MakeUnique<ShareServiceTestImpl>(); | 33 base::MakeUnique<ShareServiceTestImpl>(); |
30 ShareServiceTestImpl* share_service_helper_raw = share_service_helper.get(); | 34 ShareServiceTestImpl* share_service_helper_raw = share_service_helper.get(); |
31 mojo::MakeStrongBinding(std::move(share_service_helper), | 35 mojo::MakeStrongBinding(std::move(share_service_helper), |
32 std::move(request)); | 36 std::move(request)); |
33 return share_service_helper_raw; | 37 return share_service_helper_raw; |
34 } | 38 } |
35 | 39 |
36 void set_picker_result(base::Optional<base::string16> result) { | 40 void set_picker_result(base::Optional<base::string16> result) { |
37 picker_result_ = result; | 41 picker_result_ = result; |
38 } | 42 } |
39 | 43 |
44 void SetUpPrefs() { | |
Matt Giuca
2017/02/03 02:40:24
This can just go in the constructor (since you cal
constantina
2017/02/08 23:25:17
Done.
| |
45 pref_service_.reset(new TestingPrefServiceSimple()); | |
46 pref_service_->registry()->RegisterDictionaryPref( | |
47 prefs::kWebShareVisitedTargets); | |
48 } | |
49 | |
50 void AddShareTarget(std::string manifest_url, std::string url_template) { | |
Matt Giuca
2017/02/03 02:40:24
const std::string&
constantina
2017/02/08 23:25:16
Done.
| |
51 DictionaryPrefUpdate update(GetPrefService(), | |
52 prefs::kWebShareVisitedTargets); | |
53 base::DictionaryValue* share_target_dict = update.Get(); | |
54 | |
55 std::unique_ptr<base::DictionaryValue> origin_dict( | |
56 new base::DictionaryValue); | |
57 | |
58 constexpr char kUrlTemplateKey[] = "url_template"; | |
59 origin_dict->SetStringWithoutPathExpansion(kUrlTemplateKey, url_template); | |
60 | |
61 share_target_dict->SetWithoutPathExpansion(manifest_url, | |
62 std::move(origin_dict)); | |
63 } | |
64 | |
65 void AddEngagementForTarget(std::string manifest_url, | |
Matt Giuca
2017/02/03 02:40:24
How about SetEngagementForTarget (rather than Add)
constantina
2017/02/08 23:25:16
Done.
| |
66 blink::mojom::EngagementLevel level) { | |
67 engagement_map[manifest_url] = level; | |
68 } | |
69 | |
70 void ClearEngagements() { engagement_map.clear(); } | |
Matt Giuca
2017/02/03 02:40:24
This isn't needed (since the whole ShareServiceTes
constantina
2017/02/08 23:25:16
Done.
| |
71 | |
40 const std::string& GetLastUsedTargetURL() { return last_used_target_url_; } | 72 const std::string& GetLastUsedTargetURL() { return last_used_target_url_; } |
41 | 73 |
74 const std::vector<base::string16>& GetTargetsInPicker() { | |
75 return targets_in_picker_; | |
76 } | |
77 | |
42 private: | 78 private: |
43 base::Optional<base::string16> picker_result_ = base::nullopt; | 79 base::Optional<base::string16> picker_result_ = base::nullopt; |
Matt Giuca
2017/02/03 02:40:24
base::nullopt not needed (it's the default) ... so
constantina
2017/02/08 23:25:16
Done.
| |
44 std::string last_used_target_url_; | 80 std::string last_used_target_url_; |
81 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | |
82 std::map<std::string, blink::mojom::EngagementLevel> engagement_map; | |
Matt Giuca
2017/02/03 02:40:24
engagement_map_
constantina
2017/02/08 23:25:17
Done.
| |
83 std::vector<base::string16> targets_in_picker_; | |
45 | 84 |
46 void ShowPickerDialog( | 85 void ShowPickerDialog( |
47 const std::vector<base::string16>& targets, | 86 const std::vector<base::string16>& targets, |
48 const base::Callback<void(base::Optional<base::string16>)>& callback) | 87 const base::Callback<void(base::Optional<base::string16>)>& callback) |
49 override { | 88 override { |
89 targets_in_picker_ = targets; | |
90 if (picker_result_ != base::nullopt) { | |
constantina
2017/02/08 23:25:16
Don't need this anymore; not testing ShareServiceI
| |
91 EXPECT_TRUE(find(targets.begin(), targets.end(), picker_result_) != | |
92 targets.end()); | |
93 } | |
50 callback.Run(picker_result_); | 94 callback.Run(picker_result_); |
51 } | 95 } |
52 | 96 |
53 void OpenTargetURL(const GURL& target_url) override { | 97 void OpenTargetURL(const GURL& target_url) override { |
54 last_used_target_url_ = target_url.spec(); | 98 last_used_target_url_ = target_url.spec(); |
55 } | 99 } |
100 | |
101 PrefService* GetPrefService() override { return pref_service_.get(); } | |
102 | |
103 blink::mojom::EngagementLevel GetEngagementLevel(const GURL& url) override { | |
104 return engagement_map[url.spec()]; | |
105 } | |
56 }; | 106 }; |
57 | 107 |
58 class ShareServiceImplUnittest : public ChromeRenderViewHostTestHarness { | 108 class ShareServiceImplUnittest : public ChromeRenderViewHostTestHarness { |
59 public: | 109 public: |
60 ShareServiceImplUnittest() = default; | 110 ShareServiceImplUnittest() = default; |
61 ~ShareServiceImplUnittest() override = default; | 111 ~ShareServiceImplUnittest() override = default; |
62 | 112 |
63 void SetUp() override { | 113 void SetUp() override { |
64 ChromeRenderViewHostTestHarness::SetUp(); | 114 ChromeRenderViewHostTestHarness::SetUp(); |
65 | 115 |
66 share_service_helper_ = | 116 share_service_helper_ = |
67 ShareServiceTestImpl::Create(mojo::MakeRequest(&share_service_)); | 117 ShareServiceTestImpl::Create(mojo::MakeRequest(&share_service_)); |
118 share_service_helper_->SetUpPrefs(); | |
68 } | 119 } |
69 | 120 |
70 void TearDown() override { ChromeRenderViewHostTestHarness::TearDown(); } | 121 void TearDown() override { |
122 ChromeRenderViewHostTestHarness::TearDown(); | |
Matt Giuca
2017/02/03 02:40:24
Is this override needed at all? (Before this CL) i
constantina
2017/02/08 23:25:17
I think we worked out when we first wrote the test
| |
123 share_service_helper_->ClearEngagements(); | |
Matt Giuca
2017/02/03 02:40:24
As stated above, this isn't needed because the ent
constantina
2017/02/08 23:25:16
Done.
| |
124 } | |
71 | 125 |
72 void DidShare(const std::string& expected_target_url, | 126 void DidShare(const std::string& expected_target_url, |
127 const std::vector<base::string16>& expected_targets_in_picker, | |
Matt Giuca
2017/02/03 02:40:24
Little nit pick: could this argument go first? Jus
constantina
2017/02/08 23:25:16
Done.
| |
73 const base::Optional<std::string>& expected_param, | 128 const base::Optional<std::string>& expected_param, |
74 const base::Optional<std::string>& param) { | 129 const base::Optional<std::string>& param) { |
75 EXPECT_EQ(expected_param, param); | 130 EXPECT_EQ(expected_param, param); |
76 std::string target_url = share_service_helper_->GetLastUsedTargetURL(); | 131 std::string target_url = share_service_helper_->GetLastUsedTargetURL(); |
77 EXPECT_EQ(expected_target_url, target_url); | 132 EXPECT_EQ(expected_target_url, target_url); |
133 std::vector<base::string16> targets_in_picker = | |
134 share_service_helper_->GetTargetsInPicker(); | |
135 EXPECT_EQ(expected_targets_in_picker, targets_in_picker); | |
Matt Giuca
2017/02/03 02:40:24
Good test that the targets actually show up in the
constantina
2017/02/08 23:25:17
:D
| |
78 | 136 |
79 if (!on_callback_.is_null()) | 137 if (!on_callback_.is_null()) |
80 on_callback_.Run(); | 138 on_callback_.Run(); |
81 } | 139 } |
82 | 140 |
83 blink::mojom::ShareServicePtr share_service_; | 141 blink::mojom::ShareServicePtr share_service_; |
84 ShareServiceTestImpl* share_service_helper_; | 142 ShareServiceTestImpl* share_service_helper_; |
85 base::Closure on_callback_; | 143 base::Closure on_callback_; |
86 }; | 144 }; |
87 | 145 |
88 } // namespace | 146 } // namespace |
89 | 147 |
90 // Basic test to check the Share method calls the callback with the expected | 148 // Basic test to check the Share method calls the callback with the expected |
91 // parameters. | 149 // parameters. |
92 TEST_F(ShareServiceImplUnittest, ShareCallbackParams) { | 150 TEST_F(ShareServiceImplUnittest, ShareCallbackParams) { |
151 std::string manifest_url = | |
152 "https://wicg.github.io/web-share-target/demos/manifest.json"; | |
Matt Giuca
2017/02/03 02:40:24
Now that we are (as of this CL) no longer hard-cod
constantina
2017/02/08 23:25:16
Done.
| |
153 std::string url_template = | |
154 "sharetarget.html?title={title}&text={text}&url={url}"; | |
93 std::string expected_url = | 155 std::string expected_url = |
94 "https://wicg.github.io/web-share-target/demos/" | 156 "https://wicg.github.io/web-share-target/demos/" |
95 "sharetarget.html?title=My%20title&text=My%20text&url=https%3A%2F%2Fwww." | 157 "sharetarget.html?title=My%20title&text=My%20text&url=https%3A%2F%2Fwww." |
96 "google.com%2F"; | 158 "google.com%2F"; |
97 share_service_helper_->set_picker_result( | |
98 base::ASCIIToUTF16("https://wicg.github.io/web-share-target/demos/")); | |
99 | 159 |
100 const GURL url(kUrlSpec); | 160 share_service_helper_->set_picker_result(base::ASCIIToUTF16(manifest_url)); |
161 share_service_helper_->AddShareTarget(manifest_url, url_template); | |
Matt Giuca
2017/02/03 02:40:24
Perhaps add a second target here (since none of th
constantina
2017/02/08 23:25:17
Done. Did a bit of a refactor.
| |
162 share_service_helper_->AddEngagementForTarget( | |
163 manifest_url, blink::mojom::EngagementLevel::LOW); | |
164 | |
165 std::vector<base::string16> expected_targets{base::UTF8ToUTF16(manifest_url)}; | |
101 base::Callback<void(const base::Optional<std::string>&)> callback = | 166 base::Callback<void(const base::Optional<std::string>&)> callback = |
102 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this), | 167 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this), |
103 expected_url, base::Optional<std::string>()); | 168 expected_url, expected_targets, base::Optional<std::string>()); |
104 | 169 |
105 base::RunLoop run_loop; | 170 base::RunLoop run_loop; |
106 on_callback_ = run_loop.QuitClosure(); | 171 on_callback_ = run_loop.QuitClosure(); |
107 | 172 |
173 const GURL url(kUrlSpec); | |
108 share_service_->Share(kTitle, kText, url, callback); | 174 share_service_->Share(kTitle, kText, url, callback); |
109 | 175 |
110 run_loop.Run(); | 176 run_loop.Run(); |
111 } | 177 } |
112 | 178 |
113 // Tests the result of cancelling the share in the picker UI. | 179 // Tests the result of cancelling the share in the picker UI. |
114 TEST_F(ShareServiceImplUnittest, ShareCancel) { | 180 TEST_F(ShareServiceImplUnittest, ShareCancel) { |
Matt Giuca
2017/02/03 02:40:24
Add a test where there are 2 sufficiently engaged
constantina
2017/02/08 23:25:16
Done.
| |
115 // Ask that the dialog be cancelled. | 181 // Ask that the dialog be cancelled. |
116 share_service_helper_->set_picker_result(base::nullopt); | 182 share_service_helper_->set_picker_result(base::nullopt); |
117 | 183 |
118 // Expect an error message in response. | 184 // Expect an error message in response. |
119 base::Callback<void(const base::Optional<std::string>&)> callback = | 185 base::Callback<void(const base::Optional<std::string>&)> callback = |
120 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this), | 186 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this), |
121 std::string(), | 187 std::string(), std::vector<base::string16>(), |
122 base::Optional<std::string>("Share was cancelled")); | 188 base::Optional<std::string>("Share was cancelled")); |
123 | 189 |
124 base::RunLoop run_loop; | 190 base::RunLoop run_loop; |
125 on_callback_ = run_loop.QuitClosure(); | 191 on_callback_ = run_loop.QuitClosure(); |
126 | 192 |
127 const GURL url(kUrlSpec); | 193 const GURL url(kUrlSpec); |
128 share_service_->Share(kTitle, kText, url, callback); | 194 share_service_->Share(kTitle, kText, url, callback); |
129 | 195 |
130 run_loop.Run(); | 196 run_loop.Run(); |
131 } | 197 } |
132 | 198 |
199 // Test to check that no targets were in picker if they didn't have enough | |
200 // engagement. User forced to cancel. | |
201 TEST_F(ShareServiceImplUnittest, ShareWithInsuffientlyEngagedTargets) { | |
Matt Giuca
2017/02/03 02:40:24
I don't think it's worth having this test. It esse
constantina
2017/02/08 23:25:17
Removed.
| |
202 std::string manifest_url = | |
203 "https://wicg.github.io/web-share-target/demos/manifest.json"; | |
204 std::string url_template = | |
205 "sharetarget.html?title={title}&text={text}&url={url}"; | |
206 | |
207 share_service_helper_->set_picker_result(base::nullopt); | |
208 share_service_helper_->AddShareTarget(manifest_url, url_template); | |
209 share_service_helper_->AddEngagementForTarget( | |
210 manifest_url, blink::mojom::EngagementLevel::MINIMAL); | |
211 | |
212 base::Callback<void(const base::Optional<std::string>&)> callback = | |
213 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this), | |
214 std::string(), std::vector<base::string16>(), | |
215 base::Optional<std::string>("Share was cancelled")); | |
216 | |
217 base::RunLoop run_loop; | |
218 on_callback_ = run_loop.QuitClosure(); | |
219 | |
220 const GURL url(kUrlSpec); | |
221 share_service_->Share(kTitle, kText, url, callback); | |
222 | |
223 run_loop.Run(); | |
224 } | |
225 | |
226 // Test to check that only targets with enough engagement were in picker. | |
227 TEST_F(ShareServiceImplUnittest, ShareWithSomeInsuffientlyEngagedTargets) { | |
Matt Giuca
2017/02/03 02:40:24
Insufficiently
constantina
2017/02/08 23:25:17
Done.
| |
228 std::string manifest_url_wicg = | |
229 "https://wicg.github.io/web-share-target/demos/manifest.json"; | |
230 std::string manifest_url_cpyro = | |
231 "https://cpyro.github.io/web-share-target/demos/manifest.json"; | |
232 std::string url_template = | |
233 "sharetarget.html?title={title}&text={text}&url={url}"; | |
234 std::string expected_url = | |
235 "https://cpyro.github.io/web-share-target/demos/" | |
236 "sharetarget.html?title=My%20title&text=My%20text&url=https%3A%2F%2Fwww." | |
237 "google.com%2F"; | |
238 | |
239 share_service_helper_->set_picker_result( | |
240 base::UTF8ToUTF16(manifest_url_cpyro)); | |
241 share_service_helper_->AddShareTarget(manifest_url_wicg, url_template); | |
242 share_service_helper_->AddShareTarget(manifest_url_cpyro, url_template); | |
243 share_service_helper_->AddEngagementForTarget( | |
244 manifest_url_wicg, blink::mojom::EngagementLevel::MINIMAL); | |
245 share_service_helper_->AddEngagementForTarget( | |
246 manifest_url_cpyro, blink::mojom::EngagementLevel::LOW); | |
247 | |
248 std::vector<base::string16> expected_targets{ | |
249 base::UTF8ToUTF16(manifest_url_cpyro)}; | |
250 base::Callback<void(const base::Optional<std::string>&)> callback = | |
251 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this), | |
252 expected_url, expected_targets, base::Optional<std::string>()); | |
253 | |
254 base::RunLoop run_loop; | |
255 on_callback_ = run_loop.QuitClosure(); | |
256 | |
257 const GURL url(kUrlSpec); | |
258 share_service_->Share(kTitle, kText, url, callback); | |
259 | |
260 run_loop.Run(); | |
261 } | |
262 | |
133 // Replace various numbers of placeholders in various orders. Placeholders are | 263 // Replace various numbers of placeholders in various orders. Placeholders are |
134 // adjacent to eachother; there are no padding characters. | 264 // adjacent to eachother; there are no padding characters. |
135 TEST_F(ShareServiceImplUnittest, ReplacePlaceholders) { | 265 TEST_F(ShareServiceImplUnittest, ReplacePlaceholders) { |
136 const GURL url(kUrlSpec); | 266 const GURL url(kUrlSpec); |
137 std::string url_template_filled; | 267 std::string url_template_filled; |
138 bool succeeded; | 268 bool succeeded; |
139 | 269 |
140 // No placeholders | 270 // No placeholders |
141 std::string url_template = "blank"; | 271 std::string url_template = "blank"; |
142 succeeded = ShareServiceImpl::ReplacePlaceholders(url_template, kTitle, kText, | 272 succeeded = ShareServiceImpl::ReplacePlaceholders(url_template, kTitle, kText, |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
326 EXPECT_TRUE(succeeded); | 456 EXPECT_TRUE(succeeded); |
327 EXPECT_EQ("%C3%A9", url_template_filled); | 457 EXPECT_EQ("%C3%A9", url_template_filled); |
328 | 458 |
329 // U+1F4A9 | 459 // U+1F4A9 |
330 url_template = "{title}"; | 460 url_template = "{title}"; |
331 succeeded = ShareServiceImpl::ReplacePlaceholders( | 461 succeeded = ShareServiceImpl::ReplacePlaceholders( |
332 url_template, "\xf0\x9f\x92\xa9", kText, url, &url_template_filled); | 462 url_template, "\xf0\x9f\x92\xa9", kText, url, &url_template_filled); |
333 EXPECT_TRUE(succeeded); | 463 EXPECT_TRUE(succeeded); |
334 EXPECT_EQ("%F0%9F%92%A9", url_template_filled); | 464 EXPECT_EQ("%F0%9F%92%A9", url_template_filled); |
335 } | 465 } |
OLD | NEW |