PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/chrome/browser/autocomplete/keyword_provider_unittest.cc

https://bitbucket.org/kennethendfinger/chromium
C++ | 228 lines | 176 code | 30 blank | 22 comment | 6 complexity | b750ae1810d9b5208d8e06c5ada59b84 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1, CC-BY-SA-3.0, BSD-2-Clause, CC-BY-3.0, AGPL-1.0, Unlicense, 0BSD, MPL-2.0, LGPL-2.0, Apache-2.0, GPL-2.0, LGPL-3.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/message_loop.h"
  5. #include "base/utf_string_conversions.h"
  6. #include "chrome/browser/autocomplete/autocomplete_match.h"
  7. #include "chrome/browser/autocomplete/keyword_provider.h"
  8. #include "chrome/browser/search_engines/template_url.h"
  9. #include "chrome/browser/search_engines/template_url_service.h"
  10. #include "chrome/test/base/testing_browser_process.h"
  11. #include "googleurl/src/gurl.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. class KeywordProviderTest : public testing::Test {
  14. protected:
  15. template<class ResultType>
  16. struct test_data {
  17. const string16 input;
  18. const size_t num_results;
  19. const ResultType output[3];
  20. };
  21. KeywordProviderTest() : kw_provider_(NULL) { }
  22. virtual ~KeywordProviderTest() { }
  23. virtual void SetUp();
  24. virtual void TearDown();
  25. template<class ResultType>
  26. void RunTest(test_data<ResultType>* keyword_cases,
  27. int num_cases,
  28. ResultType AutocompleteMatch::* member);
  29. protected:
  30. scoped_refptr<KeywordProvider> kw_provider_;
  31. scoped_ptr<TemplateURLService> model_;
  32. };
  33. void KeywordProviderTest::SetUp() {
  34. static const TemplateURLService::Initializer kTestKeywordData[] = {
  35. { "aa", "aa.com?foo=%s", "aa" },
  36. { "aaaa", "http://aaaa/?aaaa=1&b=%s&c", "aaaa" },
  37. { "aaaaa", "%s", "aaaaa" },
  38. { "ab", "bogus URL %s", "ab" },
  39. { "weasel", "weasel%sweasel", "weasel" },
  40. { "www", " +%2B?=%sfoo ", "www" },
  41. { "z", "%s=z", "z" },
  42. };
  43. model_.reset(new TemplateURLService(kTestKeywordData,
  44. arraysize(kTestKeywordData)));
  45. kw_provider_ = new KeywordProvider(NULL, model_.get());
  46. }
  47. void KeywordProviderTest::TearDown() {
  48. model_.reset();
  49. kw_provider_ = NULL;
  50. }
  51. template<class ResultType>
  52. void KeywordProviderTest::RunTest(
  53. test_data<ResultType>* keyword_cases,
  54. int num_cases,
  55. ResultType AutocompleteMatch::* member) {
  56. ACMatches matches;
  57. for (int i = 0; i < num_cases; ++i) {
  58. AutocompleteInput input(keyword_cases[i].input, string16(), true,
  59. false, true, AutocompleteInput::ALL_MATCHES);
  60. kw_provider_->Start(input, false);
  61. EXPECT_TRUE(kw_provider_->done());
  62. matches = kw_provider_->matches();
  63. EXPECT_EQ(keyword_cases[i].num_results, matches.size()) <<
  64. ASCIIToUTF16("Input was: ") + keyword_cases[i].input;
  65. if (matches.size() == keyword_cases[i].num_results) {
  66. for (size_t j = 0; j < keyword_cases[i].num_results; ++j) {
  67. EXPECT_EQ(keyword_cases[i].output[j], matches[j].*member);
  68. }
  69. }
  70. }
  71. }
  72. // http://crbug.com/80612
  73. TEST_F(KeywordProviderTest, DISABLED_Edit) {
  74. test_data<string16> edit_cases[] = {
  75. // Searching for a nonexistent prefix should give nothing.
  76. {ASCIIToUTF16("Not Found"), 0, {}},
  77. {ASCIIToUTF16("aaaaaNot Found"), 0, {}},
  78. // Check that tokenization only collapses whitespace between first tokens,
  79. // no-query-input cases have a space appended, and action is not escaped.
  80. {ASCIIToUTF16("z foo"), 1, {ASCIIToUTF16("z foo")}},
  81. {ASCIIToUTF16("z"), 1, {ASCIIToUTF16("z ")}},
  82. {ASCIIToUTF16("z \t"), 1, {ASCIIToUTF16("z ")}},
  83. {ASCIIToUTF16("z a b c++"), 1, {ASCIIToUTF16("z a b c++")}},
  84. // Matches should be limited to three, and sorted in quality order, not
  85. // alphabetical.
  86. {ASCIIToUTF16("aaa"), 2, {ASCIIToUTF16("aaaa "),
  87. ASCIIToUTF16("aaaaa ")}},
  88. {ASCIIToUTF16("a 1 2 3"), 3, {ASCIIToUTF16("aa 1 2 3"),
  89. ASCIIToUTF16("ab 1 2 3"),
  90. ASCIIToUTF16("aaaa 1 2 3")}},
  91. {ASCIIToUTF16("www.a"), 3, {ASCIIToUTF16("aa "),
  92. ASCIIToUTF16("ab "),
  93. ASCIIToUTF16("aaaa ")}},
  94. // Exact matches should prevent returning inexact matches.
  95. {ASCIIToUTF16("aaaa foo"), 1, {ASCIIToUTF16("aaaa foo")}},
  96. {ASCIIToUTF16("www.aaaa foo"), 1, {ASCIIToUTF16("aaaa foo")}},
  97. // Clean up keyword input properly. "http" and "https" are the only
  98. // allowed schemes.
  99. {ASCIIToUTF16("www"), 1, {ASCIIToUTF16("www ")}},
  100. {ASCIIToUTF16("www."), 0, {}},
  101. {ASCIIToUTF16("www.w w"), 2, {ASCIIToUTF16("www w"),
  102. ASCIIToUTF16("weasel w")}},
  103. {ASCIIToUTF16("http://www"), 1, {ASCIIToUTF16("www ")}},
  104. {ASCIIToUTF16("http://www."), 0, {}},
  105. {ASCIIToUTF16("ftp: blah"), 0, {}},
  106. {ASCIIToUTF16("mailto:z"), 0, {}},
  107. {ASCIIToUTF16("ftp://z"), 0, {}},
  108. {ASCIIToUTF16("https://z"), 1, {ASCIIToUTF16("z ")}},
  109. };
  110. RunTest<string16>(edit_cases, arraysize(edit_cases),
  111. &AutocompleteMatch::fill_into_edit);
  112. }
  113. TEST_F(KeywordProviderTest, URL) {
  114. test_data<GURL> url_cases[] = {
  115. // No query input -> empty destination URL.
  116. {ASCIIToUTF16("z"), 1, {GURL()}},
  117. {ASCIIToUTF16("z \t"), 1, {GURL()}},
  118. // Check that tokenization only collapses whitespace between first tokens
  119. // and query input, but not rest of URL, is escaped.
  120. {ASCIIToUTF16("z a b c++"), 1, {GURL("a+++b+++c%2B%2B=z")}},
  121. {ASCIIToUTF16("www.www www"), 1, {GURL(" +%2B?=wwwfoo ")}},
  122. // Substitution should work with various locations of the "%s".
  123. {ASCIIToUTF16("aaa 1a2b"), 2, {GURL("http://aaaa/?aaaa=1&b=1a2b&c"),
  124. GURL("1a2b")}},
  125. {ASCIIToUTF16("a 1 2 3"), 3, {GURL("aa.com?foo=1+2+3"),
  126. GURL("bogus URL 1+2+3"),
  127. GURL("http://aaaa/?aaaa=1&b=1+2+3&c")}},
  128. {ASCIIToUTF16("www.w w"), 2, {GURL(" +%2B?=wfoo "),
  129. GURL("weaselwweasel")}},
  130. };
  131. RunTest<GURL>(url_cases, arraysize(url_cases),
  132. &AutocompleteMatch::destination_url);
  133. }
  134. TEST_F(KeywordProviderTest, Contents) {
  135. test_data<string16> contents_cases[] = {
  136. // No query input -> substitute "<enter query>" into contents.
  137. {ASCIIToUTF16("z"), 1,
  138. {ASCIIToUTF16("Search z for <enter query>")}},
  139. {ASCIIToUTF16("z \t"), 1,
  140. {ASCIIToUTF16("Search z for <enter query>")}},
  141. // Check that tokenization only collapses whitespace between first tokens
  142. // and contents are not escaped or unescaped.
  143. {ASCIIToUTF16("z a b c++"), 1,
  144. {ASCIIToUTF16("Search z for a b c++")}},
  145. {ASCIIToUTF16("www.www www"), 1, {ASCIIToUTF16("Search www for www")}},
  146. // Substitution should work with various locations of the "%s".
  147. {ASCIIToUTF16("aaa"), 2,
  148. {ASCIIToUTF16("Search aaaa for <enter query>"),
  149. ASCIIToUTF16("Search aaaaa for <enter query>")}},
  150. {ASCIIToUTF16("a 1 2 3"), 3, {ASCIIToUTF16("Search aa for 1 2 3"),
  151. ASCIIToUTF16("Search ab for 1 2 3"),
  152. ASCIIToUTF16("Search aaaa for 1 2 3")}},
  153. {ASCIIToUTF16("www.w w"), 2, {ASCIIToUTF16("Search www for w"),
  154. ASCIIToUTF16("Search weasel for w")}},
  155. };
  156. RunTest<string16>(contents_cases, arraysize(contents_cases),
  157. &AutocompleteMatch::contents);
  158. }
  159. TEST_F(KeywordProviderTest, DISABLED_Description) {
  160. test_data<string16> description_cases[] = {
  161. // Whole keyword should be returned for both exact and inexact matches.
  162. {ASCIIToUTF16("z foo"), 1, {ASCIIToUTF16("(Keyword: z)")}},
  163. {ASCIIToUTF16("a foo"), 3, {ASCIIToUTF16("(Keyword: aa)"),
  164. ASCIIToUTF16("(Keyword: ab)"),
  165. ASCIIToUTF16("(Keyword: aaaa)")}},
  166. {ASCIIToUTF16("ftp://www.www w"), 0, {}},
  167. {ASCIIToUTF16("http://www.ab w"), 1, {ASCIIToUTF16("(Keyword: ab)")}},
  168. // Keyword should be returned regardless of query input.
  169. {ASCIIToUTF16("z"), 1, {ASCIIToUTF16("(Keyword: z)")}},
  170. {ASCIIToUTF16("z \t"), 1, {ASCIIToUTF16("(Keyword: z)")}},
  171. {ASCIIToUTF16("z a b c++"), 1, {ASCIIToUTF16("(Keyword: z)")}},
  172. };
  173. RunTest<string16>(description_cases, arraysize(description_cases),
  174. &AutocompleteMatch::description);
  175. }
  176. TEST_F(KeywordProviderTest, AddKeyword) {
  177. TemplateURL* template_url = new TemplateURL();
  178. string16 keyword(ASCIIToUTF16("foo"));
  179. std::string url("http://www.google.com/foo?q={searchTerms}");
  180. template_url->SetURL(url, 0, 0);
  181. template_url->set_keyword(keyword);
  182. template_url->set_short_name(ASCIIToUTF16("Test"));
  183. model_->Add(template_url);
  184. ASSERT_TRUE(template_url == model_->GetTemplateURLForKeyword(keyword));
  185. }
  186. TEST_F(KeywordProviderTest, RemoveKeyword) {
  187. string16 url(ASCIIToUTF16("http://aaaa/?aaaa=1&b={searchTerms}&c"));
  188. model_->Remove(model_->GetTemplateURLForKeyword(ASCIIToUTF16("aaaa")));
  189. ASSERT_TRUE(model_->GetTemplateURLForKeyword(ASCIIToUTF16("aaaa")) == NULL);
  190. }
  191. TEST_F(KeywordProviderTest, GetKeywordForInput) {
  192. EXPECT_EQ(ASCIIToUTF16("aa"),
  193. kw_provider_->GetKeywordForText(ASCIIToUTF16("aa")));
  194. EXPECT_EQ(string16(),
  195. kw_provider_->GetKeywordForText(ASCIIToUTF16("aafoo")));
  196. EXPECT_EQ(string16(),
  197. kw_provider_->GetKeywordForText(ASCIIToUTF16("aa foo")));
  198. }