| 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" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 void set_run_loop(base::RunLoop* run_loop) { | 78 void set_run_loop(base::RunLoop* run_loop) { |
| 79 quit_run_loop_ = run_loop->QuitClosure(); | 79 quit_run_loop_ = run_loop->QuitClosure(); |
| 80 } | 80 } |
| 81 | 81 |
| 82 const std::string& GetLastUsedTargetURL() { return last_used_target_url_; } | 82 const std::string& GetLastUsedTargetURL() { return last_used_target_url_; } |
| 83 | 83 |
| 84 const std::vector<std::pair<base::string16, GURL>>& GetTargetsInPicker() { | 84 const std::vector<std::pair<base::string16, GURL>>& GetTargetsInPicker() { |
| 85 return targets_in_picker_; | 85 return targets_in_picker_; |
| 86 } | 86 } |
| 87 | 87 |
| 88 const base::Callback<void(base::Optional<std::string>)>& picker_callback() { | 88 chrome::WebShareTargetPickerCallback picker_callback() { |
| 89 return picker_callback_; | 89 return std::move(picker_callback_); |
| 90 } | 90 } |
| 91 | 91 |
| 92 private: | 92 private: |
| 93 void ShowPickerDialog( | 93 void ShowPickerDialog( |
| 94 const std::vector<std::pair<base::string16, GURL>>& targets, | 94 const std::vector<std::pair<base::string16, GURL>>& targets, |
| 95 const base::Callback<void(base::Optional<std::string>)>& callback) | 95 chrome::WebShareTargetPickerCallback callback) override { |
| 96 override { | |
| 97 // Store the arguments passed to the picker dialog. | 96 // Store the arguments passed to the picker dialog. |
| 98 targets_in_picker_ = targets; | 97 targets_in_picker_ = targets; |
| 99 picker_callback_ = callback; | 98 picker_callback_ = std::move(callback); |
| 100 | 99 |
| 101 // Quit the test's run loop. It is the test's responsibility to call the | 100 // Quit the test's run loop. It is the test's responsibility to call the |
| 102 // callback, to simulate the user's choice. | 101 // callback, to simulate the user's choice. |
| 103 quit_run_loop_.Run(); | 102 quit_run_loop_.Run(); |
| 104 } | 103 } |
| 105 | 104 |
| 106 void OpenTargetURL(const GURL& target_url) override { | 105 void OpenTargetURL(const GURL& target_url) override { |
| 107 last_used_target_url_ = target_url.spec(); | 106 last_used_target_url_ = target_url.spec(); |
| 108 } | 107 } |
| 109 | 108 |
| 110 PrefService* GetPrefService() override { return pref_service_.get(); } | 109 PrefService* GetPrefService() override { return pref_service_.get(); } |
| 111 | 110 |
| 112 blink::mojom::EngagementLevel GetEngagementLevel(const GURL& url) override { | 111 blink::mojom::EngagementLevel GetEngagementLevel(const GURL& url) override { |
| 113 return engagement_map_[url.spec()]; | 112 return engagement_map_[url.spec()]; |
| 114 } | 113 } |
| 115 | 114 |
| 116 mojo::Binding<blink::mojom::ShareService> binding_; | 115 mojo::Binding<blink::mojom::ShareService> binding_; |
| 117 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 116 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 118 | 117 |
| 119 std::map<std::string, blink::mojom::EngagementLevel> engagement_map_; | 118 std::map<std::string, blink::mojom::EngagementLevel> engagement_map_; |
| 120 // Closure to quit the test's run loop. | 119 // Closure to quit the test's run loop. |
| 121 base::Closure quit_run_loop_; | 120 base::Closure quit_run_loop_; |
| 122 | 121 |
| 123 // The last URL passed to OpenTargetURL. | 122 // The last URL passed to OpenTargetURL. |
| 124 std::string last_used_target_url_; | 123 std::string last_used_target_url_; |
| 125 // The targets passed to ShowPickerDialog. | 124 // The targets passed to ShowPickerDialog. |
| 126 std::vector<std::pair<base::string16, GURL>> targets_in_picker_; | 125 std::vector<std::pair<base::string16, GURL>> targets_in_picker_; |
| 127 // The callback passed to ShowPickerDialog (which is supposed to be called | 126 // The callback passed to ShowPickerDialog (which is supposed to be called |
| 128 // with the user's chosen result, or nullopt if cancelled). | 127 // with the user's chosen result, or nullopt if cancelled). |
| 129 base::Callback<void(base::Optional<std::string>)> picker_callback_; | 128 chrome::WebShareTargetPickerCallback picker_callback_; |
| 130 }; | 129 }; |
| 131 | 130 |
| 132 class ShareServiceImplUnittest : public ChromeRenderViewHostTestHarness { | 131 class ShareServiceImplUnittest : public ChromeRenderViewHostTestHarness { |
| 133 public: | 132 public: |
| 134 ShareServiceImplUnittest() = default; | 133 ShareServiceImplUnittest() = default; |
| 135 ~ShareServiceImplUnittest() override = default; | 134 ~ShareServiceImplUnittest() override = default; |
| 136 | 135 |
| 137 void SetUp() override { | 136 void SetUp() override { |
| 138 ChromeRenderViewHostTestHarness::SetUp(); | 137 ChromeRenderViewHostTestHarness::SetUp(); |
| 139 | 138 |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 base::Callback<void(blink::mojom::ShareError)> callback = | 335 base::Callback<void(blink::mojom::ShareError)> callback = |
| 337 base::Bind([](blink::mojom::ShareError error) { FAIL(); }); | 336 base::Bind([](blink::mojom::ShareError error) { FAIL(); }); |
| 338 share_service()->Share(kTitle, kText, url, callback); | 337 share_service()->Share(kTitle, kText, url, callback); |
| 339 | 338 |
| 340 run_loop.Run(); | 339 run_loop.Run(); |
| 341 | 340 |
| 342 const std::vector<std::pair<base::string16, GURL>> kExpectedTargets{ | 341 const std::vector<std::pair<base::string16, GURL>> kExpectedTargets{ |
| 343 make_pair(base::UTF8ToUTF16(kTargetName), GURL(kManifestUrlLow))}; | 342 make_pair(base::UTF8ToUTF16(kTargetName), GURL(kManifestUrlLow))}; |
| 344 EXPECT_EQ(kExpectedTargets, share_service_helper()->GetTargetsInPicker()); | 343 EXPECT_EQ(kExpectedTargets, share_service_helper()->GetTargetsInPicker()); |
| 345 | 344 |
| 346 const base::Callback<void(base::Optional<std::string>)> picker_callback = | 345 chrome::WebShareTargetPickerCallback picker_callback = |
| 347 share_service_helper()->picker_callback(); | 346 share_service_helper()->picker_callback(); |
| 348 | 347 |
| 349 DeleteShareService(); | 348 DeleteShareService(); |
| 350 | 349 |
| 351 // Pick example-low.com. | 350 // Pick example-low.com. |
| 352 picker_callback.Run(base::Optional<std::string>(kManifestUrlLow)); | 351 std::move(picker_callback).Run(base::Optional<std::string>(kManifestUrlLow)); |
| 353 } | 352 } |
| 354 | 353 |
| 355 // Replace various numbers of placeholders in various orders. Placeholders are | 354 // Replace various numbers of placeholders in various orders. Placeholders are |
| 356 // adjacent to eachother; there are no padding characters. | 355 // adjacent to eachother; there are no padding characters. |
| 357 TEST_F(ShareServiceImplUnittest, ReplacePlaceholders) { | 356 TEST_F(ShareServiceImplUnittest, ReplacePlaceholders) { |
| 358 const GURL url(kUrlSpec); | 357 const GURL url(kUrlSpec); |
| 359 std::string url_template_filled; | 358 std::string url_template_filled; |
| 360 bool succeeded; | 359 bool succeeded; |
| 361 | 360 |
| 362 // No placeholders | 361 // No placeholders |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 EXPECT_TRUE(succeeded); | 547 EXPECT_TRUE(succeeded); |
| 549 EXPECT_EQ("%C3%A9", url_template_filled); | 548 EXPECT_EQ("%C3%A9", url_template_filled); |
| 550 | 549 |
| 551 // U+1F4A9 | 550 // U+1F4A9 |
| 552 url_template = "{title}"; | 551 url_template = "{title}"; |
| 553 succeeded = ShareServiceImpl::ReplacePlaceholders( | 552 succeeded = ShareServiceImpl::ReplacePlaceholders( |
| 554 url_template, "\xf0\x9f\x92\xa9", kText, url, &url_template_filled); | 553 url_template, "\xf0\x9f\x92\xa9", kText, url, &url_template_filled); |
| 555 EXPECT_TRUE(succeeded); | 554 EXPECT_TRUE(succeeded); |
| 556 EXPECT_EQ("%F0%9F%92%A9", url_template_filled); | 555 EXPECT_EQ("%F0%9F%92%A9", url_template_filled); |
| 557 } | 556 } |
| OLD | NEW |