Chromium Code Reviews| 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( |
| 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() { | |
| 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) { | |
| 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, | |
| 66 blink::mojom::EngagementLevel level) { | |
| 67 engagement_map[manifest_url] = level; | |
| 68 } | |
| 69 | |
| 70 void ClearEngagements() { engagement_map.clear(); } | |
| 71 | |
| 40 const std::string& GetLastUsedTargetURL() { return last_used_target_url_; } | 72 const std::string& GetLastUsedTargetURL() { return last_used_target_url_; } |
| 41 | 73 |
| 42 private: | 74 private: |
| 43 base::Optional<base::string16> picker_result_ = base::nullopt; | 75 base::Optional<base::string16> picker_result_ = base::nullopt; |
| 44 std::string last_used_target_url_; | 76 std::string last_used_target_url_; |
| 77 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | |
| 78 std::map<std::string, blink::mojom::EngagementLevel> engagement_map; | |
| 45 | 79 |
| 46 void ShowPickerDialog( | 80 void ShowPickerDialog( |
| 47 const std::vector<base::string16>& targets, | 81 const std::vector<base::string16>& targets, |
| 48 const base::Callback<void(base::Optional<base::string16>)>& callback) | 82 const base::Callback<void(base::Optional<base::string16>)>& callback) |
| 49 override { | 83 override { |
| 84 // TODO check targets were what was expected. | |
|
Matt Giuca
2017/02/01 07:30:20
Should this be done?
constantina
2017/02/02 00:43:48
Yep, will do in next patch.
| |
| 50 callback.Run(picker_result_); | 85 callback.Run(picker_result_); |
| 51 } | 86 } |
| 52 | 87 |
| 53 void OpenTargetURL(const GURL& target_url) override { | 88 void OpenTargetURL(const GURL& target_url) override { |
| 54 last_used_target_url_ = target_url.spec(); | 89 last_used_target_url_ = target_url.spec(); |
| 55 } | 90 } |
| 91 | |
| 92 PrefService* GetPrefService() override { return pref_service_.get(); } | |
| 93 | |
| 94 blink::mojom::EngagementLevel GetEngagementLevel(GURL url) override { | |
| 95 return engagement_map[url.spec()]; | |
| 96 } | |
| 56 }; | 97 }; |
| 57 | 98 |
| 58 class ShareServiceImplUnittest : public ChromeRenderViewHostTestHarness { | 99 class ShareServiceImplUnittest : public ChromeRenderViewHostTestHarness { |
| 59 public: | 100 public: |
| 60 ShareServiceImplUnittest() = default; | 101 ShareServiceImplUnittest() = default; |
| 61 ~ShareServiceImplUnittest() override = default; | 102 ~ShareServiceImplUnittest() override = default; |
| 62 | 103 |
| 63 void SetUp() override { | 104 void SetUp() override { |
| 64 ChromeRenderViewHostTestHarness::SetUp(); | 105 ChromeRenderViewHostTestHarness::SetUp(); |
| 65 | 106 |
| 66 share_service_helper_ = | 107 share_service_helper_ = |
| 67 ShareServiceTestImpl::Create(mojo::MakeRequest(&share_service_)); | 108 ShareServiceTestImpl::Create(mojo::MakeRequest(&share_service_)); |
| 109 share_service_helper_->SetUpPrefs(); | |
| 68 } | 110 } |
| 69 | 111 |
| 70 void TearDown() override { ChromeRenderViewHostTestHarness::TearDown(); } | 112 void TearDown() override { |
| 113 ChromeRenderViewHostTestHarness::TearDown(); | |
| 114 share_service_helper_->ClearEngagements(); | |
| 115 } | |
| 71 | 116 |
| 72 void DidShare(const std::string& expected_target_url, | 117 void DidShare(const std::string& expected_target_url, |
| 73 const base::Optional<std::string>& expected_param, | 118 const base::Optional<std::string>& expected_param, |
| 74 const base::Optional<std::string>& param) { | 119 const base::Optional<std::string>& param) { |
| 75 EXPECT_EQ(expected_param, param); | 120 EXPECT_EQ(expected_param, param); |
| 76 std::string target_url = share_service_helper_->GetLastUsedTargetURL(); | 121 std::string target_url = share_service_helper_->GetLastUsedTargetURL(); |
| 77 EXPECT_EQ(expected_target_url, target_url); | 122 EXPECT_EQ(expected_target_url, target_url); |
| 78 | 123 |
| 79 if (!on_callback_.is_null()) | 124 if (!on_callback_.is_null()) |
| 80 on_callback_.Run(); | 125 on_callback_.Run(); |
| 81 } | 126 } |
| 82 | 127 |
| 83 blink::mojom::ShareServicePtr share_service_; | 128 blink::mojom::ShareServicePtr share_service_; |
| 84 ShareServiceTestImpl* share_service_helper_; | 129 ShareServiceTestImpl* share_service_helper_; |
| 85 base::Closure on_callback_; | 130 base::Closure on_callback_; |
| 86 }; | 131 }; |
| 87 | 132 |
| 88 } // namespace | 133 } // namespace |
| 89 | 134 |
| 90 // Basic test to check the Share method calls the callback with the expected | 135 // Basic test to check the Share method calls the callback with the expected |
| 91 // parameters. | 136 // parameters. |
| 92 TEST_F(ShareServiceImplUnittest, ShareCallbackParams) { | 137 TEST_F(ShareServiceImplUnittest, ShareCallbackParams) { |
| 138 std::string base_url = "https://wicg.github.io/web-share-target/demos/"; | |
| 139 std::string url_template = | |
| 140 "sharetarget.html?title={title}&text={text}&url={url}"; | |
| 93 std::string expected_url = | 141 std::string expected_url = |
| 94 "https://wicg.github.io/web-share-target/demos/" | 142 "https://wicg.github.io/web-share-target/demos/" |
| 95 "sharetarget.html?title=My%20title&text=My%20text&url=https%3A%2F%2Fwww." | 143 "sharetarget.html?title=My%20title&text=My%20text&url=https%3A%2F%2Fwww." |
| 96 "google.com%2F"; | 144 "google.com%2F"; |
| 97 share_service_helper_->set_picker_result( | 145 share_service_helper_->set_picker_result(base::ASCIIToUTF16(base_url)); |
| 98 base::ASCIIToUTF16("https://wicg.github.io/web-share-target/demos/")); | 146 share_service_helper_->AddShareTarget(base_url, url_template); |
| 147 share_service_helper_->AddEngagementForTarget( | |
| 148 base_url, blink::mojom::EngagementLevel::LOW); | |
| 99 | 149 |
| 100 const GURL url(kUrlSpec); | |
| 101 base::Callback<void(const base::Optional<std::string>&)> callback = | 150 base::Callback<void(const base::Optional<std::string>&)> callback = |
| 102 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this), | 151 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this), |
| 103 expected_url, base::Optional<std::string>()); | 152 expected_url, base::Optional<std::string>()); |
| 104 | 153 |
| 105 base::RunLoop run_loop; | 154 base::RunLoop run_loop; |
| 106 on_callback_ = run_loop.QuitClosure(); | 155 on_callback_ = run_loop.QuitClosure(); |
| 107 | 156 |
| 157 const GURL url(kUrlSpec); | |
| 108 share_service_->Share(kTitle, kText, url, callback); | 158 share_service_->Share(kTitle, kText, url, callback); |
| 109 | 159 |
| 110 run_loop.Run(); | 160 run_loop.Run(); |
| 111 } | 161 } |
| 112 | 162 |
| 113 // Tests the result of cancelling the share in the picker UI. | 163 // Tests the result of cancelling the share in the picker UI. |
| 114 TEST_F(ShareServiceImplUnittest, ShareCancel) { | 164 TEST_F(ShareServiceImplUnittest, ShareCancel) { |
| 115 // Ask that the dialog be cancelled. | 165 // Ask that the dialog be cancelled. |
| 116 share_service_helper_->set_picker_result(base::nullopt); | 166 share_service_helper_->set_picker_result(base::nullopt); |
| 117 | 167 |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 326 EXPECT_TRUE(succeeded); | 376 EXPECT_TRUE(succeeded); |
| 327 EXPECT_EQ("%C3%A9", url_template_filled); | 377 EXPECT_EQ("%C3%A9", url_template_filled); |
| 328 | 378 |
| 329 // U+1F4A9 | 379 // U+1F4A9 |
| 330 url_template = "{title}"; | 380 url_template = "{title}"; |
| 331 succeeded = ShareServiceImpl::ReplacePlaceholders( | 381 succeeded = ShareServiceImpl::ReplacePlaceholders( |
| 332 url_template, "\xf0\x9f\x92\xa9", kText, url, &url_template_filled); | 382 url_template, "\xf0\x9f\x92\xa9", kText, url, &url_template_filled); |
| 333 EXPECT_TRUE(succeeded); | 383 EXPECT_TRUE(succeeded); |
| 334 EXPECT_EQ("%F0%9F%92%A9", url_template_filled); | 384 EXPECT_EQ("%F0%9F%92%A9", url_template_filled); |
| 335 } | 385 } |
| OLD | NEW |