| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
| 7 #include "chrome/browser/autocomplete/autocomplete_match.h" | 7 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 8 #include "chrome/browser/autocomplete/keyword_provider.h" | 8 #include "chrome/browser/autocomplete/keyword_provider.h" |
| 9 #include "chrome/browser/search_engines/template_url.h" | 9 #include "chrome/browser/search_engines/template_url.h" |
| 10 #include "chrome/browser/search_engines/template_url_model.h" | 10 #include "chrome/browser/search_engines/template_url_model.h" |
| 11 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 class KeywordProviderTest : public testing::Test { | 14 class KeywordProviderTest : public testing::Test { |
| 15 protected: | 15 protected: |
| 16 template<class ResultType> | 16 template<class ResultType> |
| 17 struct test_data { | 17 struct test_data { |
| 18 const std::wstring input; | 18 const string16 input; |
| 19 const size_t num_results; | 19 const size_t num_results; |
| 20 const ResultType output[3]; | 20 const ResultType output[3]; |
| 21 }; | 21 }; |
| 22 | 22 |
| 23 KeywordProviderTest() : kw_provider_(NULL) { } | 23 KeywordProviderTest() : kw_provider_(NULL) { } |
| 24 virtual ~KeywordProviderTest() { } | 24 virtual ~KeywordProviderTest() { } |
| 25 | 25 |
| 26 virtual void SetUp(); | 26 virtual void SetUp(); |
| 27 virtual void TearDown(); | 27 virtual void TearDown(); |
| 28 | 28 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 57 kw_provider_ = NULL; | 57 kw_provider_ = NULL; |
| 58 } | 58 } |
| 59 | 59 |
| 60 template<class ResultType> | 60 template<class ResultType> |
| 61 void KeywordProviderTest::RunTest( | 61 void KeywordProviderTest::RunTest( |
| 62 test_data<ResultType>* keyword_cases, | 62 test_data<ResultType>* keyword_cases, |
| 63 int num_cases, | 63 int num_cases, |
| 64 ResultType AutocompleteMatch::* member) { | 64 ResultType AutocompleteMatch::* member) { |
| 65 ACMatches matches; | 65 ACMatches matches; |
| 66 for (int i = 0; i < num_cases; ++i) { | 66 for (int i = 0; i < num_cases; ++i) { |
| 67 AutocompleteInput input(keyword_cases[i].input, std::wstring(), true, | 67 AutocompleteInput input(keyword_cases[i].input, string16(), true, |
| 68 false, true, false); | 68 false, true, false); |
| 69 kw_provider_->Start(input, false); | 69 kw_provider_->Start(input, false); |
| 70 EXPECT_TRUE(kw_provider_->done()); | 70 EXPECT_TRUE(kw_provider_->done()); |
| 71 matches = kw_provider_->matches(); | 71 matches = kw_provider_->matches(); |
| 72 EXPECT_EQ(keyword_cases[i].num_results, matches.size()) << | 72 EXPECT_EQ(keyword_cases[i].num_results, matches.size()) << |
| 73 L"Input was: " + keyword_cases[i].input; | 73 ASCIIToUTF16("Input was: ") + keyword_cases[i].input; |
| 74 if (matches.size() == keyword_cases[i].num_results) { | 74 if (matches.size() == keyword_cases[i].num_results) { |
| 75 for (size_t j = 0; j < keyword_cases[i].num_results; ++j) { | 75 for (size_t j = 0; j < keyword_cases[i].num_results; ++j) { |
| 76 EXPECT_EQ(keyword_cases[i].output[j], matches[j].*member); | 76 EXPECT_EQ(keyword_cases[i].output[j], matches[j].*member); |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 | 81 |
| 82 TEST_F(KeywordProviderTest, Edit) { | 82 TEST_F(KeywordProviderTest, Edit) { |
| 83 test_data<std::wstring> edit_cases[] = { | 83 test_data<string16> edit_cases[] = { |
| 84 // Searching for a nonexistent prefix should give nothing. | 84 // Searching for a nonexistent prefix should give nothing. |
| 85 {L"Not Found", 0, {}}, | 85 {ASCIIToUTF16("Not Found"), 0, {}}, |
| 86 {L"aaaaaNot Found", 0, {}}, | 86 {ASCIIToUTF16("aaaaaNot Found"), 0, {}}, |
| 87 | 87 |
| 88 // Check that tokenization only collapses whitespace between first tokens, | 88 // Check that tokenization only collapses whitespace between first tokens, |
| 89 // no-query-input cases have a space appended, and action is not escaped. | 89 // no-query-input cases have a space appended, and action is not escaped. |
| 90 {L"z foo", 1, {L"z foo"}}, | 90 {ASCIIToUTF16("z foo"), 1, {ASCIIToUTF16("z foo")}}, |
| 91 {L"z", 1, {L"z "}}, | 91 {ASCIIToUTF16("z"), 1, {ASCIIToUTF16("z ")}}, |
| 92 {L"z \t", 1, {L"z "}}, | 92 {ASCIIToUTF16("z \t"), 1, {ASCIIToUTF16("z ")}}, |
| 93 {L"z a b c++", 1, {L"z a b c++"}}, | 93 {ASCIIToUTF16("z a b c++"), 1, {ASCIIToUTF16("z a b c++")}}, |
| 94 | 94 |
| 95 // Matches should be limited to three, and sorted in quality order, not | 95 // Matches should be limited to three, and sorted in quality order, not |
| 96 // alphabetical. | 96 // alphabetical. |
| 97 {L"aaa", 2, {L"aaaa ", L"aaaaa "}}, | 97 {ASCIIToUTF16("aaa"), 2, {ASCIIToUTF16("aaaa "), |
| 98 {L"a 1 2 3", 3, {L"aa 1 2 3", L"ab 1 2 3", L"aaaa 1 2 3"}}, | 98 ASCIIToUTF16("aaaaa ")}}, |
| 99 {L"www.a", 3, {L"aa ", L"ab ", L"aaaa "}}, | 99 {ASCIIToUTF16("a 1 2 3"), 3, {ASCIIToUTF16("aa 1 2 3"), |
| 100 ASCIIToUTF16("ab 1 2 3"), |
| 101 ASCIIToUTF16("aaaa 1 2 3")}}, |
| 102 {ASCIIToUTF16("www.a"), 3, {ASCIIToUTF16("aa "), |
| 103 ASCIIToUTF16("ab "), |
| 104 ASCIIToUTF16("aaaa ")}}, |
| 100 // Exact matches should prevent returning inexact matches. | 105 // Exact matches should prevent returning inexact matches. |
| 101 {L"aaaa foo", 1, {L"aaaa foo"}}, | 106 {ASCIIToUTF16("aaaa foo"), 1, {ASCIIToUTF16("aaaa foo")}}, |
| 102 {L"www.aaaa foo", 1, {L"aaaa foo"}}, | 107 {ASCIIToUTF16("www.aaaa foo"), 1, {ASCIIToUTF16("aaaa foo")}}, |
| 103 | 108 |
| 104 // Clean up keyword input properly. "http" and "https" are the only | 109 // Clean up keyword input properly. "http" and "https" are the only |
| 105 // allowed schemes. | 110 // allowed schemes. |
| 106 {L"www", 1, {L"www "}}, | 111 {ASCIIToUTF16("www"), 1, {ASCIIToUTF16("www ")}}, |
| 107 {L"www.", 0, {}}, | 112 {ASCIIToUTF16("www."), 0, {}}, |
| 108 {L"www.w w", 2, {L"www w", L"weasel w"}}, | 113 {ASCIIToUTF16("www.w w"), 2, {ASCIIToUTF16("www w"), |
| 109 {L"http://www", 1, {L"www "}}, | 114 ASCIIToUTF16("weasel w")}}, |
| 110 {L"http://www.", 0, {}}, | 115 {ASCIIToUTF16("http://www"), 1, {ASCIIToUTF16("www ")}}, |
| 111 {L"ftp: blah", 0, {}}, | 116 {ASCIIToUTF16("http://www."), 0, {}}, |
| 112 {L"mailto:z", 0, {}}, | 117 {ASCIIToUTF16("ftp: blah"), 0, {}}, |
| 113 {L"ftp://z", 0, {}}, | 118 {ASCIIToUTF16("mailto:z"), 0, {}}, |
| 114 {L"https://z", 1, {L"z "}}, | 119 {ASCIIToUTF16("ftp://z"), 0, {}}, |
| 120 {ASCIIToUTF16("https://z"), 1, {ASCIIToUTF16("z ")}}, |
| 115 }; | 121 }; |
| 116 | 122 |
| 117 RunTest<std::wstring>(edit_cases, arraysize(edit_cases), | 123 RunTest<string16>(edit_cases, arraysize(edit_cases), |
| 118 &AutocompleteMatch::fill_into_edit); | 124 &AutocompleteMatch::fill_into_edit); |
| 119 } | 125 } |
| 120 | 126 |
| 121 TEST_F(KeywordProviderTest, URL) { | 127 TEST_F(KeywordProviderTest, URL) { |
| 122 test_data<GURL> url_cases[] = { | 128 test_data<GURL> url_cases[] = { |
| 123 // No query input -> empty destination URL. | 129 // No query input -> empty destination URL. |
| 124 {L"z", 1, {GURL()}}, | 130 {ASCIIToUTF16("z"), 1, {GURL()}}, |
| 125 {L"z \t", 1, {GURL()}}, | 131 {ASCIIToUTF16("z \t"), 1, {GURL()}}, |
| 126 | 132 |
| 127 // Check that tokenization only collapses whitespace between first tokens | 133 // Check that tokenization only collapses whitespace between first tokens |
| 128 // and query input, but not rest of URL, is escaped. | 134 // and query input, but not rest of URL, is escaped. |
| 129 {L"z a b c++", 1, {GURL("a+++b+++c%2B%2B=z")}}, | 135 {ASCIIToUTF16("z a b c++"), 1, {GURL("a+++b+++c%2B%2B=z")}}, |
| 130 {L"www.www www", 1, {GURL(" +%2B?=wwwfoo ")}}, | 136 {ASCIIToUTF16("www.www www"), 1, {GURL(" +%2B?=wwwfoo ")}}, |
| 131 | 137 |
| 132 // Substitution should work with various locations of the "%s". | 138 // Substitution should work with various locations of the "%s". |
| 133 {L"aaa 1a2b", 2, {GURL("http://aaaa/?aaaa=1&b=1a2b&c"), | 139 {ASCIIToUTF16("aaa 1a2b"), 2, {GURL("http://aaaa/?aaaa=1&b=1a2b&c"), |
| 134 GURL("1a2b")}}, | 140 GURL("1a2b")}}, |
| 135 {L"a 1 2 3", 3, {GURL("aa.com?foo=1+2+3"), GURL("bogus URL 1+2+3"), | 141 {ASCIIToUTF16("a 1 2 3"), 3, {GURL("aa.com?foo=1+2+3"), |
| 136 GURL("http://aaaa/?aaaa=1&b=1+2+3&c")}}, | 142 GURL("bogus URL 1+2+3"), |
| 137 {L"www.w w", 2, {GURL(" +%2B?=wfoo "), GURL("weaselwweasel")}}, | 143 GURL("http://aaaa/?aaaa=1&b=1+2+3&c")}}, |
| 144 {ASCIIToUTF16("www.w w"), 2, {GURL(" +%2B?=wfoo "), |
| 145 GURL("weaselwweasel")}}, |
| 138 }; | 146 }; |
| 139 | 147 |
| 140 RunTest<GURL>(url_cases, arraysize(url_cases), | 148 RunTest<GURL>(url_cases, arraysize(url_cases), |
| 141 &AutocompleteMatch::destination_url); | 149 &AutocompleteMatch::destination_url); |
| 142 } | 150 } |
| 143 | 151 |
| 144 TEST_F(KeywordProviderTest, Contents) { | 152 TEST_F(KeywordProviderTest, Contents) { |
| 145 test_data<std::wstring> contents_cases[] = { | 153 test_data<string16> contents_cases[] = { |
| 146 // No query input -> substitute "<enter query>" into contents. | 154 // No query input -> substitute "<enter query>" into contents. |
| 147 {L"z", 1, {L"Search z for <enter query>"}}, | 155 {ASCIIToUTF16("z"), 1, |
| 148 {L"z \t", 1, {L"Search z for <enter query>"}}, | 156 {ASCIIToUTF16("Search z for <enter query>")}}, |
| 157 {ASCIIToUTF16("z \t"), 1, |
| 158 {ASCIIToUTF16("Search z for <enter query>")}}, |
| 149 | 159 |
| 150 // Check that tokenization only collapses whitespace between first tokens | 160 // Check that tokenization only collapses whitespace between first tokens |
| 151 // and contents are not escaped or unescaped. | 161 // and contents are not escaped or unescaped. |
| 152 {L"z a b c++", 1, {L"Search z for a b c++"}}, | 162 {ASCIIToUTF16("z a b c++"), 1, |
| 153 {L"www.www www", 1, {L"Search www for www"}}, | 163 {ASCIIToUTF16("Search z for a b c++")}}, |
| 164 {ASCIIToUTF16("www.www www"), 1, {ASCIIToUTF16("Search www for www")}}, |
| 154 | 165 |
| 155 // Substitution should work with various locations of the "%s". | 166 // Substitution should work with various locations of the "%s". |
| 156 {L"aaa", 2, {L"Search aaaa for <enter query>", | 167 {ASCIIToUTF16("aaa"), 2, |
| 157 L"Search aaaaa for <enter query>"}}, | 168 {ASCIIToUTF16("Search aaaa for <enter query>"), |
| 158 {L"a 1 2 3", 3, {L"Search aa for 1 2 3", L"Search ab for 1 2 3", | 169 ASCIIToUTF16("Search aaaaa for <enter query>")}}, |
| 159 L"Search aaaa for 1 2 3"}}, | 170 {ASCIIToUTF16("a 1 2 3"), 3, {ASCIIToUTF16("Search aa for 1 2 3"), |
| 160 {L"www.w w", 2, {L"Search www for w", L"Search weasel for w"}}, | 171 ASCIIToUTF16("Search ab for 1 2 3"), |
| 172 ASCIIToUTF16("Search aaaa for 1 2 3")}}, |
| 173 {ASCIIToUTF16("www.w w"), 2, {ASCIIToUTF16("Search www for w"), |
| 174 ASCIIToUTF16("Search weasel for w")}}, |
| 161 }; | 175 }; |
| 162 | 176 |
| 163 RunTest<std::wstring>(contents_cases, arraysize(contents_cases), | 177 RunTest<string16>(contents_cases, arraysize(contents_cases), |
| 164 &AutocompleteMatch::contents); | 178 &AutocompleteMatch::contents); |
| 165 } | 179 } |
| 166 | 180 |
| 167 TEST_F(KeywordProviderTest, Description) { | 181 TEST_F(KeywordProviderTest, Description) { |
| 168 test_data<std::wstring> description_cases[] = { | 182 test_data<string16> description_cases[] = { |
| 169 // Whole keyword should be returned for both exact and inexact matches. | 183 // Whole keyword should be returned for both exact and inexact matches. |
| 170 {L"z foo", 1, {L"(Keyword: z)"}}, | 184 {ASCIIToUTF16("z foo"), 1, {ASCIIToUTF16("(Keyword: z)")}}, |
| 171 {L"a foo", 3, {L"(Keyword: aa)", L"(Keyword: ab)", | 185 {ASCIIToUTF16("a foo"), 3, {ASCIIToUTF16("(Keyword: aa)"), |
| 172 L"(Keyword: aaaa)"}}, | 186 ASCIIToUTF16("(Keyword: ab)"), |
| 173 {L"ftp://www.www w", 0, {}}, | 187 ASCIIToUTF16("(Keyword: aaaa)")}}, |
| 174 {L"http://www.ab w", 1, {L"(Keyword: ab)"}}, | 188 {ASCIIToUTF16("ftp://www.www w"), 0, {}}, |
| 189 {ASCIIToUTF16("http://www.ab w"), 1, {ASCIIToUTF16("(Keyword: ab)")}}, |
| 175 | 190 |
| 176 // Keyword should be returned regardless of query input. | 191 // Keyword should be returned regardless of query input. |
| 177 {L"z", 1, {L"(Keyword: z)"}}, | 192 {ASCIIToUTF16("z"), 1, {ASCIIToUTF16("(Keyword: z)")}}, |
| 178 {L"z \t", 1, {L"(Keyword: z)"}}, | 193 {ASCIIToUTF16("z \t"), 1, {ASCIIToUTF16("(Keyword: z)")}}, |
| 179 {L"z a b c++", 1, {L"(Keyword: z)"}}, | 194 {ASCIIToUTF16("z a b c++"), 1, {ASCIIToUTF16("(Keyword: z)")}}, |
| 180 }; | 195 }; |
| 181 | 196 |
| 182 RunTest<std::wstring>(description_cases, arraysize(description_cases), | 197 RunTest<string16>(description_cases, arraysize(description_cases), |
| 183 &AutocompleteMatch::description); | 198 &AutocompleteMatch::description); |
| 184 } | 199 } |
| 185 | 200 |
| 186 TEST_F(KeywordProviderTest, AddKeyword) { | 201 TEST_F(KeywordProviderTest, AddKeyword) { |
| 187 TemplateURL* template_url = new TemplateURL(); | 202 TemplateURL* template_url = new TemplateURL(); |
| 188 string16 keyword(ASCIIToUTF16("foo")); | 203 string16 keyword(ASCIIToUTF16("foo")); |
| 189 std::string url("http://www.google.com/foo?q={searchTerms}"); | 204 std::string url("http://www.google.com/foo?q={searchTerms}"); |
| 190 template_url->SetURL(url, 0, 0); | 205 template_url->SetURL(url, 0, 0); |
| 191 template_url->set_keyword(keyword); | 206 template_url->set_keyword(keyword); |
| 192 template_url->set_short_name(ASCIIToUTF16("Test")); | 207 template_url->set_short_name(ASCIIToUTF16("Test")); |
| 193 model_->Add(template_url); | 208 model_->Add(template_url); |
| 194 ASSERT_TRUE(template_url == model_->GetTemplateURLForKeyword(keyword)); | 209 ASSERT_TRUE(template_url == model_->GetTemplateURLForKeyword(keyword)); |
| 195 } | 210 } |
| 196 | 211 |
| 197 TEST_F(KeywordProviderTest, RemoveKeyword) { | 212 TEST_F(KeywordProviderTest, RemoveKeyword) { |
| 198 string16 url(ASCIIToUTF16("http://aaaa/?aaaa=1&b={searchTerms}&c")); | 213 string16 url(ASCIIToUTF16("http://aaaa/?aaaa=1&b={searchTerms}&c")); |
| 199 model_->Remove(model_->GetTemplateURLForKeyword(ASCIIToUTF16("aaaa"))); | 214 model_->Remove(model_->GetTemplateURLForKeyword(ASCIIToUTF16("aaaa"))); |
| 200 ASSERT_TRUE(model_->GetTemplateURLForKeyword(ASCIIToUTF16("aaaa")) == NULL); | 215 ASSERT_TRUE(model_->GetTemplateURLForKeyword(ASCIIToUTF16("aaaa")) == NULL); |
| 201 } | 216 } |
| OLD | NEW |