Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(957)

Unified Diff: chrome/browser/webshare/share_service_impl_unittest.cc

Issue 2564483003: Default share to Share Target: Partial impl. of Web Share for Desktop. (Closed)
Patch Set: Reimplemented ReplacePlaceholders algorithm. Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/webshare/share_service_impl_unittest.cc
diff --git a/chrome/browser/webshare/share_service_impl_unittest.cc b/chrome/browser/webshare/share_service_impl_unittest.cc
index fe0e1c75e9ac1b5cb1708cdcc19d736677a47c0e..355c50205070e2b33d36813517fdfd28e41e2a8c 100644
--- a/chrome/browser/webshare/share_service_impl_unittest.cc
+++ b/chrome/browser/webshare/share_service_impl_unittest.cc
@@ -2,55 +2,254 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <memory>
+
#include "base/bind.h"
#include "base/callback.h"
#include "base/run_loop.h"
#include "chrome/browser/webshare/share_service_impl.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "mojo/public/cpp/bindings/interface_request.h"
+#include "mojo/public/cpp/bindings/strong_binding.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
Sam McNally 2017/01/04 09:15:46 namespace {
constantina 2017/01/05 00:34:38 Done.
-class ShareServiceTest : public ChromeRenderViewHostTestHarness {
+const char kTitle[] = "My title";
Sam McNally 2017/01/04 09:15:46 constexpr
constantina 2017/01/05 00:34:38 Done.
+const char kText[] = "My text";
+const char kUrlSpec[] = "https://www.google.com/";
+
+class ShareServiceTestImpl : public ShareServiceImpl {
+ public:
+ static ShareServiceTestImpl* Create(
+ blink::mojom::ShareServiceRequest request) {
+ std::unique_ptr<ShareServiceTestImpl> share_service_helper =
+ base::MakeUnique<ShareServiceTestImpl>();
+ ShareServiceTestImpl* share_service_helper_raw = share_service_helper.get();
+ mojo::MakeStrongBinding(std::move(share_service_helper),
+ std::move(request));
+ return share_service_helper_raw;
+ }
+
+ std::string GetLastUsedTargetURL() { return last_used_target_url_; }
Sam McNally 2017/01/04 09:15:46 const std::string& (or const GURL&)
constantina 2017/01/05 00:34:38 Done.
+
+ private:
+ std::string last_used_target_url_;
+
+ protected:
Sam McNally 2017/01/04 09:15:47 protected before private.
constantina 2017/01/05 00:34:38 Changed this to private.
+ void OpenTargetURL(const GURL& target_url) override {
+ last_used_target_url_ = target_url.spec();
+ }
+};
+
+class ShareServiceImplUnittest : public ChromeRenderViewHostTestHarness {
public:
- ShareServiceTest() = default;
- ~ShareServiceTest() override = default;
+ ShareServiceImplUnittest() = default;
+ ~ShareServiceImplUnittest() override = default;
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
- ShareServiceImpl::Create(mojo::GetProxy(&share_service_));
+ share_service_helper_ =
+ ShareServiceTestImpl::Create(mojo::GetProxy(&share_service_));
}
- void TearDown() override {
- ChromeRenderViewHostTestHarness::TearDown();
- }
+ void TearDown() override { ChromeRenderViewHostTestHarness::TearDown(); }
- void DidShare(const base::Optional<std::string>& expected,
- const base::Optional<std::string>& str) {
- EXPECT_EQ(expected, str);
+ void DidShare(const std::string expected_target_url,
Sam McNally 2017/01/04 09:15:47 &
constantina 2017/01/05 00:34:38 Done.
+ const base::Optional<std::string>& expected_param,
+ const base::Optional<std::string>& param) {
+ EXPECT_EQ(expected_param, param);
+ std::string target_url = share_service_helper_->GetLastUsedTargetURL();
+ EXPECT_EQ(expected_target_url, target_url);
if (!on_callback_.is_null())
on_callback_.Run();
}
blink::mojom::ShareServicePtr share_service_;
+ ShareServiceTestImpl* share_service_helper_;
base::Closure on_callback_;
};
// Basic test to check the Share method calls the callback with the expected
// parameters.
-TEST_F(ShareServiceTest, ShareCallbackParams) {
- const GURL url("https://www.google.com");
+TEST_F(ShareServiceImplUnittest, ShareCallbackParams) {
+ std::string expected_url =
+ "https://wicg.github.io/web-share-target/demos/"
+ "sharetarget.html?title=My%20title&text=My%20text&url=https%3A%2F%2Fwww."
+ "google.com%2F";
+
+ const GURL url(kUrlSpec);
+ base::Callback<void(const base::Optional<std::string>&)> callback =
+ base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this),
+ expected_url, base::Optional<std::string>());
base::RunLoop run_loop;
on_callback_ = run_loop.QuitClosure();
- base::Callback<void(const base::Optional<std::string>&)> callback =
- base::Bind(
- &ShareServiceTest::DidShare, base::Unretained(this),
- base::Optional<std::string>("Not implemented: navigator.share"));
- share_service_->Share("title", "text", url, callback);
+ share_service_->Share(kTitle, kText, url, callback);
run_loop.Run();
}
+
+// Replace various numbers of placeholders in various orders. Placeholders are
+// adjacent to eachother; there are no padding characters.
+TEST_F(ShareServiceImplUnittest, ReplacePlaceholders) {
+ const GURL url(kUrlSpec);
+ int error = 0;
+
+ // No placeholders
+ std::string url_template = "blank";
+ std::string url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, kTitle, kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ("blank", url_template_filled);
+
+ // Empty |url_template|
+ url_template = "";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, kTitle, kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ("", url_template_filled);
+
+ // One title placeholder.
+ url_template = "%{title}";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, kTitle, kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ("My%20title", url_template_filled);
+
+ // One text placeholder.
+ url_template = "%{text}";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, kTitle, kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ("My%20text", url_template_filled);
+
+ // One url placeholder.
+ url_template = "%{url}";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, kTitle, kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ("https%3A%2F%2Fwww.google.com%2F", url_template_filled);
+
+ // One of each placeholder, in title, text, url order.
+ url_template = "%{title}%{text}%{url}";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, kTitle, kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ("My%20titleMy%20texthttps%3A%2F%2Fwww.google.com%2F",
+ url_template_filled);
+
+ // One of each placeholder, in url, text, title order.
+ url_template = "%{url}%{text}%{title}";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, kTitle, kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ("https%3A%2F%2Fwww.google.com%2FMy%20textMy%20title",
+ url_template_filled);
+
+ // Two of each placeholder, some next to each other, others not.
+ url_template = "%{title}%{url}%{text}%{text}%{title}%{url}";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, kTitle, kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ(
+ "My%20titlehttps%3A%2F%2Fwww.google.com%2FMy%20textMy%20textMy%20"
+ "titlehttps%3A%2F%2Fwww.google.com%2F",
+ url_template_filled);
+
+ // Placeholders are in a query string, as values. The expected use case.
+ // Two of each placeholder, some next to each other, others not.
+ url_template =
+ "?title=%{title}&url=%{url}&text=%{text}&text=%{text}&"
+ "title=%{title}&url=%{url}";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, kTitle, kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ(
+ "?title=My%20title&url=https%3A%2F%2Fwww.google.com%2F&text=My%20text&"
+ "text=My%20text&title=My%20title&url=https%3A%2F%2Fwww.google.com%2F",
+ url_template_filled);
+
+ // TODO(constantina): come back to this and think about returned value
Sam McNally 2017/01/04 09:15:46 Longer term I expect we'd reject malformed templat
constantina 2017/01/05 00:34:38 Ah yes, this makes sense. I have recorded this, an
+ // Badly nested placeholders.
+ url_template = "%{title";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, kTitle, kText, url, &error);
+ EXPECT_EQ(1, error);
+ EXPECT_EQ(url_template, url_template_filled);
+
+ url_template = "%{title%{text}}";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, kTitle, kText, url, &error);
+ EXPECT_EQ(1, error);
+ EXPECT_EQ(url_template, url_template_filled);
+
+ // Empty placeholder
+ url_template = "%{}";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, kTitle, kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ("", url_template_filled);
+
+ // Unexpected placeholders.
+ url_template = "%{nonexistentplaceholder}";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, kTitle, kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ("", url_template_filled);
+
+ // |url_template| with % escapes.
+ url_template = "%20%{title}%20";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, kTitle, kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ("%20My%20title%20", url_template_filled);
+
+ // Share data that contains percent escapes.
+ url_template = "%{title}";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, "My%20title", kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ("My%2520title", url_template_filled);
+
+ // Share data that contains placeholders. These should not be replaced.
+ url_template = "%{title}";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, "%{title}", kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ("%25%7Btitle%7D", url_template_filled);
+
+ // All characters that shouldn't be escaped.
+ url_template = "%{title}";
+ url_template_filled =
+ ShareServiceImpl::ReplacePlaceholders(url_template,
+ "-_.!~*'()0123456789"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz",
+ kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ(
+ "-_.!~*'()0123456789"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz",
+ url_template_filled);
+
+ // All characters that should be escaped.
+ url_template = "%{title}";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, " \"#$%&+,/:;<=>?@[\\]^`{|}", kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ(
+ "%20%22%23%24%25%26%2B%2C%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E%60%7B%7C%"
+ "7D",
+ url_template_filled);
+
+ // Unicode chars
+ url_template = "%{title}";
+ url_template_filled = ShareServiceImpl::ReplacePlaceholders(
+ url_template, "☻", kText, url, &error);
+ EXPECT_EQ(0, error);
+ EXPECT_EQ("%E2%98%BB", url_template_filled);
+}

Powered by Google App Engine
This is Rietveld 408576698