| 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 "chrome/browser/webshare/share_service_impl.h" | 11 #include "chrome/browser/webshare/share_service_impl.h" |
| 11 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 12 #include "mojo/public/cpp/bindings/interface_request.h" | 13 #include "mojo/public/cpp/bindings/interface_request.h" |
| 13 #include "mojo/public/cpp/bindings/strong_binding.h" | 14 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 constexpr char kTitle[] = "My title"; | 20 constexpr char kTitle[] = "My title"; |
| 20 constexpr char kText[] = "My text"; | 21 constexpr char kText[] = "My text"; |
| 21 constexpr char kUrlSpec[] = "https://www.google.com/"; | 22 constexpr char kUrlSpec[] = "https://www.google.com/"; |
| 22 | 23 |
| 23 class ShareServiceTestImpl : public ShareServiceImpl { | 24 class ShareServiceTestImpl : public ShareServiceImpl { |
| 24 public: | 25 public: |
| 25 static ShareServiceTestImpl* Create( | 26 static ShareServiceTestImpl* Create( |
| 26 blink::mojom::ShareServiceRequest request) { | 27 blink::mojom::ShareServiceRequest request) { |
| 27 std::unique_ptr<ShareServiceTestImpl> share_service_helper = | 28 std::unique_ptr<ShareServiceTestImpl> share_service_helper = |
| 28 base::MakeUnique<ShareServiceTestImpl>(); | 29 base::MakeUnique<ShareServiceTestImpl>(); |
| 29 ShareServiceTestImpl* share_service_helper_raw = share_service_helper.get(); | 30 ShareServiceTestImpl* share_service_helper_raw = share_service_helper.get(); |
| 30 mojo::MakeStrongBinding(std::move(share_service_helper), | 31 mojo::MakeStrongBinding(std::move(share_service_helper), |
| 31 std::move(request)); | 32 std::move(request)); |
| 32 return share_service_helper_raw; | 33 return share_service_helper_raw; |
| 33 } | 34 } |
| 34 | 35 |
| 35 void set_picker_result(SharePickerResult result) { | 36 void set_picker_result(base::Optional<base::string16> result) { |
| 36 picker_result_ = result; | 37 picker_result_ = result; |
| 37 } | 38 } |
| 38 | 39 |
| 39 const std::string& GetLastUsedTargetURL() { return last_used_target_url_; } | 40 const std::string& GetLastUsedTargetURL() { return last_used_target_url_; } |
| 40 | 41 |
| 41 private: | 42 private: |
| 42 SharePickerResult picker_result_ = SharePickerResult::SHARE; | 43 base::Optional<base::string16> picker_result_ = base::nullopt; |
| 43 std::string last_used_target_url_; | 44 std::string last_used_target_url_; |
| 44 | 45 |
| 45 void ShowPickerDialog( | 46 void ShowPickerDialog( |
| 46 const std::vector<base::string16>& targets, | 47 const std::vector<base::string16>& targets, |
| 47 const base::Callback<void(SharePickerResult)>& callback) override { | 48 const base::Callback<void(base::Optional<base::string16>)>& callback) |
| 49 override { |
| 48 callback.Run(picker_result_); | 50 callback.Run(picker_result_); |
| 49 } | 51 } |
| 50 | 52 |
| 51 void OpenTargetURL(const GURL& target_url) override { | 53 void OpenTargetURL(const GURL& target_url) override { |
| 52 last_used_target_url_ = target_url.spec(); | 54 last_used_target_url_ = target_url.spec(); |
| 53 } | 55 } |
| 54 }; | 56 }; |
| 55 | 57 |
| 56 class ShareServiceImplUnittest : public ChromeRenderViewHostTestHarness { | 58 class ShareServiceImplUnittest : public ChromeRenderViewHostTestHarness { |
| 57 public: | 59 public: |
| (...skipping 27 matching lines...) Expand all Loading... |
| 85 | 87 |
| 86 } // namespace | 88 } // namespace |
| 87 | 89 |
| 88 // Basic test to check the Share method calls the callback with the expected | 90 // Basic test to check the Share method calls the callback with the expected |
| 89 // parameters. | 91 // parameters. |
| 90 TEST_F(ShareServiceImplUnittest, ShareCallbackParams) { | 92 TEST_F(ShareServiceImplUnittest, ShareCallbackParams) { |
| 91 std::string expected_url = | 93 std::string expected_url = |
| 92 "https://wicg.github.io/web-share-target/demos/" | 94 "https://wicg.github.io/web-share-target/demos/" |
| 93 "sharetarget.html?title=My%20title&text=My%20text&url=https%3A%2F%2Fwww." | 95 "sharetarget.html?title=My%20title&text=My%20text&url=https%3A%2F%2Fwww." |
| 94 "google.com%2F"; | 96 "google.com%2F"; |
| 97 share_service_helper_->set_picker_result( |
| 98 base::ASCIIToUTF16("https://wicg.github.io/web-share-target/demos/")); |
| 95 | 99 |
| 96 const GURL url(kUrlSpec); | 100 const GURL url(kUrlSpec); |
| 97 base::Callback<void(const base::Optional<std::string>&)> callback = | 101 base::Callback<void(const base::Optional<std::string>&)> callback = |
| 98 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this), | 102 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this), |
| 99 expected_url, base::Optional<std::string>()); | 103 expected_url, base::Optional<std::string>()); |
| 100 | 104 |
| 101 base::RunLoop run_loop; | 105 base::RunLoop run_loop; |
| 102 on_callback_ = run_loop.QuitClosure(); | 106 on_callback_ = run_loop.QuitClosure(); |
| 103 | 107 |
| 104 share_service_->Share(kTitle, kText, url, callback); | 108 share_service_->Share(kTitle, kText, url, callback); |
| 105 | 109 |
| 106 run_loop.Run(); | 110 run_loop.Run(); |
| 107 } | 111 } |
| 108 | 112 |
| 109 // Tests the result of cancelling the share in the picker UI. | 113 // Tests the result of cancelling the share in the picker UI. |
| 110 TEST_F(ShareServiceImplUnittest, ShareCancel) { | 114 TEST_F(ShareServiceImplUnittest, ShareCancel) { |
| 111 // Ask that the dialog be cancelled. | 115 // Ask that the dialog be cancelled. |
| 112 share_service_helper_->set_picker_result(SharePickerResult::CANCEL); | 116 share_service_helper_->set_picker_result(base::nullopt); |
| 113 | 117 |
| 114 // Expect an error message in response. | 118 // Expect an error message in response. |
| 115 base::Callback<void(const base::Optional<std::string>&)> callback = | 119 base::Callback<void(const base::Optional<std::string>&)> callback = |
| 116 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this), | 120 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this), |
| 117 std::string(), | 121 std::string(), |
| 118 base::Optional<std::string>("Share was cancelled")); | 122 base::Optional<std::string>("Share was cancelled")); |
| 119 | 123 |
| 120 base::RunLoop run_loop; | 124 base::RunLoop run_loop; |
| 121 on_callback_ = run_loop.QuitClosure(); | 125 on_callback_ = run_loop.QuitClosure(); |
| 122 | 126 |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 EXPECT_TRUE(succeeded); | 326 EXPECT_TRUE(succeeded); |
| 323 EXPECT_EQ("%C3%A9", url_template_filled); | 327 EXPECT_EQ("%C3%A9", url_template_filled); |
| 324 | 328 |
| 325 // U+1F4A9 | 329 // U+1F4A9 |
| 326 url_template = "{title}"; | 330 url_template = "{title}"; |
| 327 succeeded = ShareServiceImpl::ReplacePlaceholders( | 331 succeeded = ShareServiceImpl::ReplacePlaceholders( |
| 328 url_template, "\xf0\x9f\x92\xa9", kText, url, &url_template_filled); | 332 url_template, "\xf0\x9f\x92\xa9", kText, url, &url_template_filled); |
| 329 EXPECT_TRUE(succeeded); | 333 EXPECT_TRUE(succeeded); |
| 330 EXPECT_EQ("%F0%9F%92%A9", url_template_filled); | 334 EXPECT_EQ("%F0%9F%92%A9", url_template_filled); |
| 331 } | 335 } |
| OLD | NEW |