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> | |
| 6 | |
| 5 #include "base/bind.h" | 7 #include "base/bind.h" |
| 6 #include "base/callback.h" | 8 #include "base/callback.h" |
| 7 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 8 #include "chrome/browser/webshare/share_service_impl.h" | 10 #include "chrome/browser/webshare/share_service_impl.h" |
| 9 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 11 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 10 #include "mojo/public/cpp/bindings/interface_request.h" | 12 #include "mojo/public/cpp/bindings/interface_request.h" |
| 13 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 13 | 16 |
| 14 class ShareServiceTest : public ChromeRenderViewHostTestHarness { | 17 class ShareServiceImplTestHelper : public ShareServiceImpl { |
|
Matt Giuca
2016/12/21 07:13:49
"TestHelper" implies that it is a collection of he
constantina
2017/01/04 07:01:45
Done.
| |
| 15 public: | 18 public: |
| 16 ShareServiceTest() = default; | 19 static ShareServiceImplTestHelper* Create( |
| 17 ~ShareServiceTest() override = default; | 20 blink::mojom::ShareServiceRequest request) { |
| 21 std::unique_ptr<ShareServiceImplTestHelper> share_service_helper = | |
| 22 base::MakeUnique<ShareServiceImplTestHelper>(); | |
| 23 ShareServiceImplTestHelper* share_service_helper_raw = | |
| 24 share_service_helper.get(); | |
| 25 mojo::MakeStrongBinding(std::move(share_service_helper), | |
| 26 std::move(request)); | |
| 27 return share_service_helper_raw; | |
| 28 } | |
| 29 | |
| 30 void openTargetURL(GURL target_url) override { | |
|
Matt Giuca
2016/12/21 07:13:49
const GURL&
Get into the habit of always using "c
Matt Giuca
2016/12/21 07:13:50
Nit: Names should start with a capital letter.
constantina
2017/01/04 07:01:45
Done.
constantina
2017/01/04 07:01:45
Done.
| |
| 31 last_used_target_url_ = target_url.spec(); | |
| 32 } | |
| 33 | |
| 34 std::string last_used_target_url_; | |
|
Matt Giuca
2016/12/21 07:13:49
Even in a test, you shouldn't have public fields.
constantina
2017/01/04 07:01:45
Done.
| |
| 35 }; | |
| 36 | |
| 37 class ShareServiceImplUnittest : public ChromeRenderViewHostTestHarness { | |
| 38 public: | |
| 39 ShareServiceImplUnittest() = default; | |
| 40 ~ShareServiceImplUnittest() override = default; | |
| 18 | 41 |
| 19 void SetUp() override { | 42 void SetUp() override { |
| 20 ChromeRenderViewHostTestHarness::SetUp(); | 43 ChromeRenderViewHostTestHarness::SetUp(); |
| 21 | 44 |
| 22 ShareServiceImpl::Create(mojo::GetProxy(&share_service_)); | 45 share_service_helper_ = |
| 46 ShareServiceImplTestHelper::Create(mojo::GetProxy(&share_service_)); | |
| 23 } | 47 } |
| 24 | 48 |
| 25 void TearDown() override { | 49 void TearDown() override { ChromeRenderViewHostTestHarness::TearDown(); } |
| 26 ChromeRenderViewHostTestHarness::TearDown(); | |
| 27 } | |
| 28 | 50 |
| 29 void DidShare(const base::Optional<std::string>& expected, | 51 void DidShare(const std::string expected_target_url, |
| 30 const base::Optional<std::string>& str) { | 52 const base::Optional<std::string>& expected_param, |
| 31 EXPECT_EQ(expected, str); | 53 const base::Optional<std::string>& param) { |
| 54 EXPECT_EQ(expected_param, param); | |
| 55 std::string target_url = share_service_helper_->last_used_target_url_; | |
| 56 EXPECT_EQ(expected_target_url, target_url); | |
| 32 | 57 |
| 33 if (!on_callback_.is_null()) | 58 if (!on_callback_.is_null()) |
| 34 on_callback_.Run(); | 59 on_callback_.Run(); |
| 35 } | 60 } |
| 36 | 61 |
| 37 blink::mojom::ShareServicePtr share_service_; | 62 blink::mojom::ShareServicePtr share_service_; |
| 63 ShareServiceImplTestHelper* share_service_helper_; | |
| 38 base::Closure on_callback_; | 64 base::Closure on_callback_; |
| 39 }; | 65 }; |
| 40 | 66 |
| 41 // Basic test to check the Share method calls the callback with the expected | 67 // Basic test to check the Share method calls the callback with the expected |
| 42 // parameters. | 68 // parameters. |
| 43 TEST_F(ShareServiceTest, ShareCallbackParams) { | 69 TEST_F(ShareServiceImplUnittest, ShareCallbackParams) { |
| 70 std::string target_url = | |
|
Matt Giuca
2016/12/21 07:13:50
nit: name this expected_url.
constantina
2017/01/04 07:01:45
Done.
| |
| 71 "https://wicg.github.io/web-share-target/demos/" | |
| 72 "sharetarget.html?title=title&text=text&url=https%3A%2F%2Fwww.google.com%" | |
| 73 "2F"; | |
| 74 | |
| 44 const GURL url("https://www.google.com"); | 75 const GURL url("https://www.google.com"); |
| 76 base::Callback<void(const base::Optional<std::string>&)> callback = | |
| 77 base::Bind(&ShareServiceImplUnittest::DidShare, base::Unretained(this), | |
| 78 target_url, base::Optional<std::string>()); | |
| 45 | 79 |
| 46 base::RunLoop run_loop; | 80 base::RunLoop run_loop; |
| 47 on_callback_ = run_loop.QuitClosure(); | 81 on_callback_ = run_loop.QuitClosure(); |
| 48 | 82 |
| 49 base::Callback<void(const base::Optional<std::string>&)> callback = | |
| 50 base::Bind( | |
| 51 &ShareServiceTest::DidShare, base::Unretained(this), | |
| 52 base::Optional<std::string>("Not implemented: navigator.share")); | |
| 53 share_service_->Share("title", "text", url, callback); | 83 share_service_->Share("title", "text", url, callback); |
|
Matt Giuca
2016/12/21 07:13:49
Make title and text constants too.
In fact, these
constantina
2017/01/04 07:01:45
Done.
| |
| 54 | 84 |
| 55 run_loop.Run(); | 85 run_loop.Run(); |
| 56 } | 86 } |
| 87 | |
| 88 // Replace various numbers of placeholders in various orders. Placeholders are | |
| 89 // adjacent to eachother; there are no padding characters. | |
| 90 TEST_F(ShareServiceImplUnittest, ReplacePlaceholders) { | |
| 91 const std::string title = "My title"; | |
|
Matt Giuca
2016/12/21 07:13:50
As above, use const char kTitle[] = "My title", et
constantina
2017/01/04 07:01:45
Done.
| |
| 92 const std::string text = "My text"; | |
| 93 const GURL url("https://www.google.com/"); | |
| 94 | |
| 95 // One title placeholder | |
|
Matt Giuca
2016/12/21 07:13:50
Nit: Full stops at the end.
constantina
2017/01/04 07:01:45
Done.
| |
| 96 std::string url_template = "%{title}"; | |
| 97 std::string url_template_filled = | |
| 98 ShareServiceImpl::replacePlaceholders(url_template, title, text, url); | |
| 99 EXPECT_EQ("My%20title", url_template_filled); | |
| 100 | |
| 101 // One text placeholder | |
| 102 url_template = "%{text}"; | |
| 103 url_template_filled = | |
| 104 ShareServiceImpl::replacePlaceholders(url_template, title, text, url); | |
| 105 EXPECT_EQ("My%20text", url_template_filled); | |
| 106 | |
| 107 // One url placeholder | |
| 108 url_template = "%{url}"; | |
| 109 url_template_filled = | |
| 110 ShareServiceImpl::replacePlaceholders(url_template, title, text, url); | |
| 111 EXPECT_EQ("https%3A%2F%2Fwww.google.com%2F", url_template_filled); | |
| 112 | |
| 113 // One of each placeholder, in title, text, url order | |
| 114 url_template = "%{title}%{text}%{url}"; | |
| 115 url_template_filled = | |
| 116 ShareServiceImpl::replacePlaceholders(url_template, title, text, url); | |
| 117 EXPECT_EQ("My%20titleMy%20texthttps%3A%2F%2Fwww.google.com%2F", | |
| 118 url_template_filled); | |
| 119 | |
| 120 // One of each placeholder, in url, text, title order | |
| 121 url_template = "%{url}%{text}%{title}"; | |
| 122 url_template_filled = | |
| 123 ShareServiceImpl::replacePlaceholders(url_template, title, text, url); | |
| 124 EXPECT_EQ("https%3A%2F%2Fwww.google.com%2FMy%20textMy%20title", | |
| 125 url_template_filled); | |
| 126 | |
| 127 // Two of each placeholder, some next to each other, others not | |
| 128 url_template = "%{title}%{url}%{text}%{text}%{title}%{url}"; | |
| 129 url_template_filled = | |
| 130 ShareServiceImpl::replacePlaceholders(url_template, title, text, url); | |
| 131 EXPECT_EQ( | |
| 132 "My%20titlehttps%3A%2F%2Fwww.google.com%2FMy%20textMy%20textMy%20" | |
| 133 "titlehttps%3A%2F%2Fwww.google.com%2F", | |
| 134 url_template_filled); | |
| 135 } | |
|
Matt Giuca
2016/12/21 07:13:49
Some other things to test:
- Badly nested placehol
constantina
2017/01/04 07:01:45
Done.
| |
| 136 | |
| 137 // Replace various numbers of placeholders in various orders, placed into a | |
| 138 // query string, as a value. The expected use case. | |
| 139 TEST_F(ShareServiceImplUnittest, ReplacePlaceholdersInQuery) { | |
|
Matt Giuca
2016/12/21 07:13:49
I think this could be merged into the previous tes
constantina
2017/01/04 07:01:45
Not quite sure what you meant, so I took the last
| |
| 140 const std::string title = "My title"; | |
| 141 const std::string text = "My text"; | |
| 142 const GURL url("https://www.google.com/"); | |
| 143 | |
| 144 // One title placeholder | |
| 145 std::string url_template = "?key=%{title}"; | |
| 146 std::string url_template_filled = | |
| 147 ShareServiceImpl::replacePlaceholders(url_template, title, text, url); | |
| 148 EXPECT_EQ("?key=My%20title", url_template_filled); | |
| 149 | |
| 150 // One text placeholder | |
| 151 url_template = "?key=%{text}"; | |
| 152 url_template_filled = | |
| 153 ShareServiceImpl::replacePlaceholders(url_template, title, text, url); | |
| 154 EXPECT_EQ("?key=My%20text", url_template_filled); | |
| 155 | |
| 156 // One url placeholder | |
| 157 url_template = "?key=%{url}"; | |
| 158 url_template_filled = | |
| 159 ShareServiceImpl::replacePlaceholders(url_template, title, text, url); | |
| 160 EXPECT_EQ("?key=https%3A%2F%2Fwww.google.com%2F", url_template_filled); | |
| 161 | |
| 162 // One of each placeholder, in title, text, url order | |
| 163 url_template = "?title=%{title}&text=%{text}&url=%{url}"; | |
| 164 url_template_filled = | |
| 165 ShareServiceImpl::replacePlaceholders(url_template, title, text, url); | |
| 166 EXPECT_EQ( | |
| 167 "?title=My%20title&text=My%20text&url=https%3A%2F%2Fwww.google.com%2F", | |
| 168 url_template_filled); | |
| 169 | |
| 170 // One of each placeholder, in url, text, title order | |
| 171 url_template = "?url=%{url}&text=%{text}&title=%{title}"; | |
| 172 url_template_filled = | |
| 173 ShareServiceImpl::replacePlaceholders(url_template, title, text, url); | |
| 174 EXPECT_EQ( | |
| 175 "?url=https%3A%2F%2Fwww.google.com%2F&text=My%20text&title=My%20title", | |
| 176 url_template_filled); | |
| 177 | |
| 178 // Two of each placeholder, some next to each other, others not | |
| 179 url_template = | |
| 180 "?title=%{title}&url=%{url}&text=%{text}&text=%{text}&" | |
| 181 "title=%{title}&url=%{url}"; | |
| 182 url_template_filled = | |
| 183 ShareServiceImpl::replacePlaceholders(url_template, title, text, url); | |
| 184 EXPECT_EQ( | |
| 185 "?title=My%20title&url=https%3A%2F%2Fwww.google.com%2F&text=My%20text&" | |
| 186 "text=My%20text&title=My%20title&url=https%3A%2F%2Fwww.google.com%2F", | |
| 187 url_template_filled); | |
| 188 } | |
| OLD | NEW |