| 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 "chrome/browser/webshare/share_service_impl.h" | 10 #include "chrome/browser/webshare/share_service_impl.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 static ShareServiceTestImpl* Create( | 25 static ShareServiceTestImpl* Create( |
| 26 blink::mojom::ShareServiceRequest request) { | 26 blink::mojom::ShareServiceRequest request) { |
| 27 std::unique_ptr<ShareServiceTestImpl> share_service_helper = | 27 std::unique_ptr<ShareServiceTestImpl> share_service_helper = |
| 28 base::MakeUnique<ShareServiceTestImpl>(); | 28 base::MakeUnique<ShareServiceTestImpl>(); |
| 29 ShareServiceTestImpl* share_service_helper_raw = share_service_helper.get(); | 29 ShareServiceTestImpl* share_service_helper_raw = share_service_helper.get(); |
| 30 mojo::MakeStrongBinding(std::move(share_service_helper), | 30 mojo::MakeStrongBinding(std::move(share_service_helper), |
| 31 std::move(request)); | 31 std::move(request)); |
| 32 return share_service_helper_raw; | 32 return share_service_helper_raw; |
| 33 } | 33 } |
| 34 | 34 |
| 35 void set_picker_result(SharePickerResult result) { |
| 36 picker_result_ = result; |
| 37 } |
| 38 |
| 35 const std::string& GetLastUsedTargetURL() { return last_used_target_url_; } | 39 const std::string& GetLastUsedTargetURL() { return last_used_target_url_; } |
| 36 | 40 |
| 37 private: | 41 private: |
| 42 SharePickerResult picker_result_ = SharePickerResult::SHARE; |
| 38 std::string last_used_target_url_; | 43 std::string last_used_target_url_; |
| 39 | 44 |
| 45 void ShowPickerDialog( |
| 46 const std::vector<base::string16>& targets, |
| 47 const base::Callback<void(SharePickerResult)>& callback) override { |
| 48 callback.Run(picker_result_); |
| 49 } |
| 50 |
| 40 void OpenTargetURL(const GURL& target_url) override { | 51 void OpenTargetURL(const GURL& target_url) override { |
| 41 last_used_target_url_ = target_url.spec(); | 52 last_used_target_url_ = target_url.spec(); |
| 42 } | 53 } |
| 43 }; | 54 }; |
| 44 | 55 |
| 45 class ShareServiceImplUnittest : public ChromeRenderViewHostTestHarness { | 56 class ShareServiceImplUnittest : public ChromeRenderViewHostTestHarness { |
| 46 public: | 57 public: |
| 47 ShareServiceImplUnittest() = default; | 58 ShareServiceImplUnittest() = default; |
| 48 ~ShareServiceImplUnittest() override = default; | 59 ~ShareServiceImplUnittest() override = default; |
| 49 | 60 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 expected_url, base::Optional<std::string>()); | 99 expected_url, base::Optional<std::string>()); |
| 89 | 100 |
| 90 base::RunLoop run_loop; | 101 base::RunLoop run_loop; |
| 91 on_callback_ = run_loop.QuitClosure(); | 102 on_callback_ = run_loop.QuitClosure(); |
| 92 | 103 |
| 93 share_service_->Share(kTitle, kText, url, callback); | 104 share_service_->Share(kTitle, kText, url, callback); |
| 94 | 105 |
| 95 run_loop.Run(); | 106 run_loop.Run(); |
| 96 } | 107 } |
| 97 | 108 |
| 109 // Tests the result of cancelling the share in the picker UI. |
| 110 TEST_F(ShareServiceImplUnittest, ShareCancel) { |
| 111 // Ask that the dialog be cancelled. |
| 112 share_service_helper_->set_picker_result(SharePickerResult::CANCEL); |
| 113 |
| 114 // Expect an error message in response. |
| 115 base::Callback<void(const base::Optional<std::string>&)> callback = |
| 116 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this), |
| 117 std::string(), |
| 118 base::Optional<std::string>("Share was cancelled")); |
| 119 |
| 120 base::RunLoop run_loop; |
| 121 on_callback_ = run_loop.QuitClosure(); |
| 122 |
| 123 const GURL url(kUrlSpec); |
| 124 share_service_->Share(kTitle, kText, url, callback); |
| 125 |
| 126 run_loop.Run(); |
| 127 } |
| 128 |
| 98 // Replace various numbers of placeholders in various orders. Placeholders are | 129 // Replace various numbers of placeholders in various orders. Placeholders are |
| 99 // adjacent to eachother; there are no padding characters. | 130 // adjacent to eachother; there are no padding characters. |
| 100 TEST_F(ShareServiceImplUnittest, ReplacePlaceholders) { | 131 TEST_F(ShareServiceImplUnittest, ReplacePlaceholders) { |
| 101 const GURL url(kUrlSpec); | 132 const GURL url(kUrlSpec); |
| 102 std::string url_template_filled; | 133 std::string url_template_filled; |
| 103 bool succeeded; | 134 bool succeeded; |
| 104 | 135 |
| 105 // No placeholders | 136 // No placeholders |
| 106 std::string url_template = "blank"; | 137 std::string url_template = "blank"; |
| 107 succeeded = ShareServiceImpl::ReplacePlaceholders(url_template, kTitle, kText, | 138 succeeded = ShareServiceImpl::ReplacePlaceholders(url_template, kTitle, kText, |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 EXPECT_TRUE(succeeded); | 322 EXPECT_TRUE(succeeded); |
| 292 EXPECT_EQ("%C3%A9", url_template_filled); | 323 EXPECT_EQ("%C3%A9", url_template_filled); |
| 293 | 324 |
| 294 // U+1F4A9 | 325 // U+1F4A9 |
| 295 url_template = "{title}"; | 326 url_template = "{title}"; |
| 296 succeeded = ShareServiceImpl::ReplacePlaceholders( | 327 succeeded = ShareServiceImpl::ReplacePlaceholders( |
| 297 url_template, "\xf0\x9f\x92\xa9", kText, url, &url_template_filled); | 328 url_template, "\xf0\x9f\x92\xa9", kText, url, &url_template_filled); |
| 298 EXPECT_TRUE(succeeded); | 329 EXPECT_TRUE(succeeded); |
| 299 EXPECT_EQ("%F0%9F%92%A9", url_template_filled); | 330 EXPECT_EQ("%F0%9F%92%A9", url_template_filled); |
| 300 } | 331 } |
| OLD | NEW |