[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 5 | #include "base/base_paths.h" |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 6 | #include "base/string_util.h" |
[email protected] | 64048bd | 2010-03-08 23:28:58 | [diff] [blame] | 7 | #include "base/utf_string_conversions.h" |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 8 | #include "chrome/browser/browser_process.h" |
| 9 | #include "chrome/browser/rlz/rlz.h" |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 10 | #include "chrome/browser/search_engines/search_terms_data.h" |
[email protected] | d54e03a5 | 2009-01-16 00:31:04 | [diff] [blame] | 11 | #include "chrome/browser/search_engines/template_url.h" |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
[email protected] | 81d9b72d | 2012-03-26 22:29:17 | [diff] [blame] | 14 | #if defined(ENABLE_RLZ) |
| 15 | #include "chrome/browser/google/google_util.h" |
| 16 | #endif |
| 17 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 18 | // TestSearchTermsData -------------------------------------------------------- |
| 19 | |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 20 | // Simple implementation of SearchTermsData. |
| 21 | class TestSearchTermsData : public SearchTermsData { |
| 22 | public: |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 23 | explicit TestSearchTermsData(const std::string& google_base_url); |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 24 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 25 | virtual std::string GoogleBaseURLValue() const OVERRIDE; |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 26 | |
| 27 | private: |
| 28 | std::string google_base_url_; |
| 29 | |
| 30 | DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData); |
| 31 | }; |
| 32 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 33 | TestSearchTermsData::TestSearchTermsData(const std::string& google_base_url) |
| 34 | : google_base_url_(google_base_url) { |
| 35 | } |
| 36 | |
| 37 | std::string TestSearchTermsData::GoogleBaseURLValue() const { |
| 38 | return google_base_url_; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | // TemplateURLTest ------------------------------------------------------------ |
| 43 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 44 | class TemplateURLTest : public testing::Test { |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 45 | public: |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 46 | void CheckSuggestBaseURL(const std::string& base_url, |
| 47 | const std::string& base_suggest_url) const; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 48 | }; |
| 49 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 50 | void TemplateURLTest::CheckSuggestBaseURL( |
| 51 | const std::string& base_url, |
| 52 | const std::string& base_suggest_url) const { |
| 53 | TestSearchTermsData search_terms_data(base_url); |
| 54 | EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue()); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | // Actual tests --------------------------------------------------------------- |
| 59 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 60 | TEST_F(TemplateURLTest, Defaults) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 61 | TemplateURLData data; |
| 62 | EXPECT_FALSE(data.show_in_default_list); |
| 63 | EXPECT_FALSE(data.safe_for_autoreplace); |
| 64 | EXPECT_EQ(0, data.prepopulate_id); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | TEST_F(TemplateURLTest, TestValidWithComplete) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 68 | TemplateURLData data; |
| 69 | data.SetURL("{searchTerms}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 70 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 71 | EXPECT_TRUE(url.url_ref().IsValid()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | TEST_F(TemplateURLTest, URLRefTestSearchTerms) { |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 75 | struct SearchTermsCase { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 76 | const char* url; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 77 | const string16 terms; |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 78 | const std::string output; |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 79 | } search_term_cases[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 80 | { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 81 | "http://foosea%20rch/bar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 82 | { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 83 | "http://foosea%20rch/bar?boo=abc" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 84 | { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 85 | "http://foo/?boo=sea+rch%2Fbar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 86 | { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 87 | "http://en.wikipedia.org/wiki/%3F" } |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 88 | }; |
| 89 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { |
| 90 | const SearchTermsCase& value = search_term_cases[i]; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 91 | TemplateURLData data; |
| 92 | data.SetURL(value.url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 93 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 94 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 95 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame^] | 96 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 97 | TemplateURLRef::SearchTermsArgs(value.terms))); |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 98 | ASSERT_TRUE(result.is_valid()); |
| 99 | EXPECT_EQ(value.output, result.spec()); |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 100 | } |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | TEST_F(TemplateURLTest, URLRefTestCount) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 104 | TemplateURLData data; |
| 105 | data.SetURL("http://foo{searchTerms}{count?}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 106 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 107 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 108 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame^] | 109 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 110 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 111 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 112 | EXPECT_EQ("http://foox/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | TEST_F(TemplateURLTest, URLRefTestCount2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 116 | TemplateURLData data; |
| 117 | data.SetURL("http://foo{searchTerms}{count}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 118 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 119 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 120 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame^] | 121 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 122 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 123 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 124 | EXPECT_EQ("http://foox10/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | TEST_F(TemplateURLTest, URLRefTestIndices) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 128 | TemplateURLData data; |
| 129 | data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 130 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 131 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 132 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame^] | 133 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 134 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 135 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 136 | EXPECT_EQ("http://fooxxy/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | TEST_F(TemplateURLTest, URLRefTestIndices2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 140 | TemplateURLData data; |
| 141 | data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 142 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 143 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 144 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame^] | 145 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 146 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 147 | ASSERT_TRUE(result.is_valid()); |
[email protected] | 405aae2 | 2012-03-29 20:36:13 | [diff] [blame] | 148 | EXPECT_EQ("http://fooxx1y1/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | TEST_F(TemplateURLTest, URLRefTestEncoding) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 152 | TemplateURLData data; |
| 153 | data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 154 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 155 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 156 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame^] | 157 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 158 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 159 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 160 | EXPECT_EQ("http://fooxxutf-8ya/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | d88cb20 | 2011-08-17 20:03:01 | [diff] [blame] | 163 | // Test that setting the prepopulate ID from TemplateURL causes the stored |
| 164 | // TemplateURLRef to handle parsing the URL parameters differently. |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 165 | TEST_F(TemplateURLTest, SetPrepopulatedAndParse) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 166 | TemplateURLData data; |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 167 | data.SetURL("http://foo{fhqwhgads}bar"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 168 | TemplateURL url(NULL, data); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 169 | TemplateURLRef::Replacements replacements; |
| 170 | bool valid = false; |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 171 | EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL( |
| 172 | "http://foo{fhqwhgads}bar", &replacements, &valid)); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 173 | EXPECT_TRUE(replacements.empty()); |
| 174 | EXPECT_TRUE(valid); |
| 175 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 176 | data.prepopulate_id = 123; |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 177 | TemplateURL url2(NULL, data); |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 178 | EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar", |
| 179 | &replacements, &valid)); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 180 | EXPECT_TRUE(replacements.empty()); |
| 181 | EXPECT_TRUE(valid); |
| 182 | } |
| 183 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 184 | TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 185 | TemplateURLData data; |
| 186 | data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 187 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 188 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 189 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame^] | 190 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 191 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 192 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 193 | EXPECT_EQ("http://fooxutf-8axyb/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | TEST_F(TemplateURLTest, URLRefTestEncoding2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 197 | TemplateURLData data; |
| 198 | data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 199 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 200 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 201 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame^] | 202 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 203 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 204 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 205 | EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 206 | } |
| 207 | |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 208 | TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) { |
| 209 | struct SearchTermsCase { |
| 210 | const char* url; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 211 | const string16 terms; |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 212 | const char* output; |
| 213 | } search_term_cases[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 214 | { "{google:baseURL}{language}{searchTerms}", string16(), |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 215 | "http://example.com/e/en" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 216 | { "{google:baseSuggestURL}{searchTerms}", string16(), |
[email protected] | 014010e | 2011-10-01 04:12:44 | [diff] [blame] | 217 | "http://example.com/complete/" } |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | TestSearchTermsData search_terms_data("http://example.com/e/"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 221 | TemplateURLData data; |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 222 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { |
| 223 | const SearchTermsCase& value = search_term_cases[i]; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 224 | data.SetURL(value.url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 225 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 226 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 227 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame^] | 228 | GURL result(url.url_ref().ReplaceSearchTermsUsingTermsData( |
| 229 | TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data)); |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 230 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 231 | EXPECT_EQ(value.output, result.spec()); |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 235 | TEST_F(TemplateURLTest, URLRefTermToWide) { |
| 236 | struct ToWideCase { |
| 237 | const char* encoded_search_term; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 238 | const string16 expected_decoded_term; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 239 | } to_wide_cases[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 240 | {"hello+world", ASCIIToUTF16("hello world")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 241 | // Test some big-5 input. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 242 | {"%a7A%A6%6e+to+you", WideToUTF16(L"\x4f60\x597d to you")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 243 | // Test some UTF-8 input. We should fall back to this when the encoding |
| 244 | // doesn't look like big-5. We have a '5' in the middle, which is an invalid |
| 245 | // Big-5 trailing byte. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 246 | {"%e4%bd%a05%e5%a5%bd+to+you", WideToUTF16(L"\x4f60\x35\x597d to you")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 247 | // Undecodable input should stay escaped. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 248 | {"%91%01+abcd", WideToUTF16(L"%91%01 abcd")}, |
[email protected] | 7df4348 | 2009-07-31 19:37:44 | [diff] [blame] | 249 | // Make sure we convert %2B to +. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 250 | {"C%2B%2B", ASCIIToUTF16("C++")}, |
[email protected] | 7df4348 | 2009-07-31 19:37:44 | [diff] [blame] | 251 | // C%2B is escaped as C%252B, make sure we unescape it properly. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 252 | {"C%252B", ASCIIToUTF16("C%2B")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 253 | }; |
| 254 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 255 | // Set one input encoding: big-5. This is so we can test fallback to UTF-8. |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 256 | TemplateURLData data; |
| 257 | data.SetURL("http://foo?q={searchTerms}"); |
| 258 | data.input_encodings.push_back("big-5"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 259 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 260 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 261 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 262 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) { |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 263 | EXPECT_EQ(to_wide_cases[i].expected_decoded_term, |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 264 | url.url_ref().SearchTermToString16( |
| 265 | to_wide_cases[i].encoded_search_term)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 269 | TEST_F(TemplateURLTest, DisplayURLToURLRef) { |
| 270 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 271 | const std::string url; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 272 | const string16 expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 273 | } test_data[] = { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 274 | { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 275 | ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 276 | { "http://X", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 277 | ASCIIToUTF16("http://X") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 278 | { "http://foo{searchTerms", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 279 | ASCIIToUTF16("http://foo{searchTerms") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 280 | { "http://foo{searchTerms}{language}", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 281 | ASCIIToUTF16("http://foo%s{language}") }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 282 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 283 | TemplateURLData data; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 284 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 285 | data.SetURL(test_data[i].url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 286 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 287 | EXPECT_EQ(test_data[i].expected_result, url.url_ref().DisplayURL()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 288 | EXPECT_EQ(test_data[i].url, |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 289 | TemplateURLRef::DisplayURLToURLRef(url.url_ref().DisplayURL())); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | TEST_F(TemplateURLTest, ReplaceSearchTerms) { |
| 294 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 295 | const std::string url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 296 | const std::string expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 297 | } test_data[] = { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 298 | { "http://foo/{language}{searchTerms}{inputEncoding}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 299 | "http://foo/{language}XUTF-8" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 300 | { "http://foo/{language}{inputEncoding}{searchTerms}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 301 | "http://foo/{language}UTF-8X" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 302 | { "http://foo/{searchTerms}{language}{inputEncoding}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 303 | "http://foo/X{language}UTF-8" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 304 | { "http://foo/{searchTerms}{inputEncoding}{language}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 305 | "http://foo/XUTF-8{language}" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 306 | { "http://foo/{inputEncoding}{searchTerms}{language}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 307 | "http://foo/UTF-8X{language}" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 308 | { "http://foo/{inputEncoding}{language}{searchTerms}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 309 | "http://foo/UTF-8{language}X" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 310 | { "http://foo/{language}a{searchTerms}a{inputEncoding}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 311 | "http://foo/{language}aXaUTF-8a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 312 | { "http://foo/{language}a{inputEncoding}a{searchTerms}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 313 | "http://foo/{language}aUTF-8aXa" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 314 | { "http://foo/{searchTerms}a{language}a{inputEncoding}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 315 | "http://foo/Xa{language}aUTF-8a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 316 | { "http://foo/{searchTerms}a{inputEncoding}a{language}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 317 | "http://foo/XaUTF-8a{language}a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 318 | { "http://foo/{inputEncoding}a{searchTerms}a{language}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 319 | "http://foo/UTF-8aXa{language}a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 320 | { "http://foo/{inputEncoding}a{language}a{searchTerms}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 321 | "http://foo/UTF-8a{language}aXa" }, |
| 322 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 323 | TemplateURLData data; |
| 324 | data.input_encodings.push_back("UTF-8"); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 325 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 326 | data.SetURL(test_data[i].url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 327 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 328 | EXPECT_TRUE(url.url_ref().IsValid()); |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 329 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 330 | std::string expected_result = test_data[i].expected_result; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 331 | ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}", |
[email protected] | d70539de | 2009-06-24 22:17:06 | [diff] [blame] | 332 | g_browser_process->GetApplicationLocale()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame^] | 333 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 334 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 335 | ASSERT_TRUE(result.is_valid()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 336 | EXPECT_EQ(expected_result, result.spec()); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | |
| 341 | // Tests replacing search terms in various encodings and making sure the |
| 342 | // generated URL matches the expected value. |
| 343 | TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) { |
| 344 | struct TestData { |
| 345 | const std::string encoding; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 346 | const string16 search_term; |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 347 | const std::string url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 348 | const std::string expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 349 | } test_data[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 350 | { "BIG5", WideToUTF16(L"\x60BD"), |
| 351 | "http://foo/?{searchTerms}{inputEncoding}", |
[email protected] | 3c75f0d | 2010-03-02 05:51:17 | [diff] [blame] | 352 | "http://foo/?%B1~BIG5" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 353 | { "UTF-8", ASCIIToUTF16("blah"), |
| 354 | "http://foo/?{searchTerms}{inputEncoding}", |
[email protected] | 3c75f0d | 2010-03-02 05:51:17 | [diff] [blame] | 355 | "http://foo/?blahUTF-8" }, |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 356 | { "Shift_JIS", UTF8ToUTF16("\xe3\x81\x82"), |
| 357 | "http://foo/{searchTerms}/bar", |
| 358 | "http://foo/%82%A0/bar"}, |
| 359 | { "Shift_JIS", UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"), |
| 360 | "http://foo/{searchTerms}/bar", |
| 361 | "http://foo/%82%A0%20%82%A2/bar"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 362 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 363 | TemplateURLData data; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 364 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 365 | data.SetURL(test_data[i].url); |
| 366 | data.input_encodings.clear(); |
| 367 | data.input_encodings.push_back(test_data[i].encoding); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 368 | TemplateURL url(NULL, data); |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 369 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 370 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame^] | 371 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 372 | TemplateURLRef::SearchTermsArgs(test_data[i].search_term))); |
| 373 | ASSERT_TRUE(result.is_valid()); |
| 374 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // Tests replacing assisted query stats (AQS) in various scenarios. |
| 379 | TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) { |
| 380 | struct TestData { |
| 381 | const string16 search_term; |
| 382 | const std::string aqs; |
| 383 | const std::string base_url; |
| 384 | const std::string url; |
| 385 | const std::string expected_result; |
| 386 | } test_data[] = { |
| 387 | // No HTTPS, no AQS. |
| 388 | { ASCIIToUTF16("foo"), |
| 389 | "chrome.0.0l6", |
| 390 | "http://foo/", |
| 391 | "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", |
| 392 | "http://foo/?foo" }, |
| 393 | // HTTPS available, AQS should be replaced. |
| 394 | { ASCIIToUTF16("foo"), |
| 395 | "chrome.0.0l6", |
| 396 | "https://foo/", |
| 397 | "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", |
| 398 | "https://foo/?fooaqs=chrome.0.0l6&" }, |
| 399 | // HTTPS available, however AQS is empty. |
| 400 | { ASCIIToUTF16("foo"), |
| 401 | "", |
| 402 | "https://foo/", |
| 403 | "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", |
| 404 | "https://foo/?foo" }, |
| 405 | // No {google:baseURL} and protocol is HTTP, we must not substitute AQS. |
| 406 | { ASCIIToUTF16("foo"), |
| 407 | "chrome.0.0l6", |
| 408 | "", |
| 409 | "http://foo?{searchTerms}{google:assistedQueryStats}", |
| 410 | "http://foo/?foo" }, |
| 411 | // A non-Google search provider with HTTPS should allow AQS. |
| 412 | { ASCIIToUTF16("foo"), |
| 413 | "chrome.0.0l6", |
| 414 | "", |
| 415 | "https://foo?{searchTerms}{google:assistedQueryStats}", |
| 416 | "https://foo/?fooaqs=chrome.0.0l6&" }, |
| 417 | }; |
| 418 | TemplateURLData data; |
| 419 | data.input_encodings.push_back("UTF-8"); |
| 420 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 421 | data.SetURL(test_data[i].url); |
| 422 | TemplateURL url(NULL, data); |
| 423 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 424 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 425 | TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| 426 | search_terms_args.assisted_query_stats = test_data[i].aqs; |
| 427 | UIThreadSearchTermsData::SetGoogleBaseURL(test_data[i].base_url); |
| 428 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 429 | ASSERT_TRUE(result.is_valid()); |
| 430 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | TEST_F(TemplateURLTest, Suggestions) { |
| 435 | struct TestData { |
| 436 | const int accepted_suggestion; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 437 | const string16 original_query_for_suggestion; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 438 | const std::string expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 439 | } test_data[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 440 | { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16(), |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 441 | "http://bar/foo?q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 442 | { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"), |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 443 | "http://bar/foo?q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 444 | { TemplateURLRef::NO_SUGGESTION_CHOSEN, string16(), |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 445 | "http://bar/foo?aq=f&q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 446 | { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"), |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 447 | "http://bar/foo?aq=f&q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 448 | { 0, string16(), "http://bar/foo?aq=0&oq=&q=foobar" }, |
| 449 | { 1, ASCIIToUTF16("foo"), "http://bar/foo?aq=1&oq=foo&q=foobar" }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 450 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 451 | TemplateURLData data; |
| 452 | data.SetURL("http://bar/foo?{google:acceptedSuggestion}" |
| 453 | "{google:originalQueryForSuggestion}q={searchTerms}"); |
| 454 | data.input_encodings.push_back("UTF-8"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 455 | TemplateURL url(NULL, data); |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 456 | EXPECT_TRUE(url.url_ref().IsValid()); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 457 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 458 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame^] | 459 | TemplateURLRef::SearchTermsArgs search_terms_args( |
| 460 | ASCIIToUTF16("foobar")); |
| 461 | search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion; |
| 462 | search_terms_args.original_query = |
| 463 | test_data[i].original_query_for_suggestion; |
| 464 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 465 | ASSERT_TRUE(result.is_valid()); |
| 466 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
[email protected] | 81d9b72d | 2012-03-26 22:29:17 | [diff] [blame] | 470 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | 81f808de | 2009-09-25 01:36:34 | [diff] [blame] | 471 | TEST_F(TemplateURLTest, RLZ) { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 472 | string16 rlz_string; |
[email protected] | 81d9b72d | 2012-03-26 22:29:17 | [diff] [blame] | 473 | #if defined(ENABLE_RLZ) |
| 474 | std::string brand; |
| 475 | if (google_util::GetBrand(&brand) && !brand.empty() && |
| 476 | !google_util::IsOrganic(brand)) { |
| 477 | RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz_string); |
| 478 | } |
[email protected] | 58b6433 | 2010-08-13 16:09:39 | [diff] [blame] | 479 | #endif |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 480 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 481 | TemplateURLData data; |
| 482 | data.SetURL("http://bar/?{google:RLZ}{searchTerms}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 483 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 484 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 485 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame^] | 486 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 487 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 488 | ASSERT_TRUE(result.is_valid()); |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 489 | std::string expected_url = "http://bar/?"; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 490 | if (!rlz_string.empty()) |
[email protected] | 8ead4c02 | 2012-03-17 02:09:50 | [diff] [blame] | 491 | expected_url += "rlz=" + UTF16ToUTF8(rlz_string) + "&"; |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 492 | expected_url += "x"; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 493 | EXPECT_EQ(expected_url, result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 494 | } |
[email protected] | 81f808de | 2009-09-25 01:36:34 | [diff] [blame] | 495 | #endif |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 496 | |
| 497 | TEST_F(TemplateURLTest, HostAndSearchTermKey) { |
| 498 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 499 | const std::string url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 500 | const std::string host; |
| 501 | const std::string path; |
| 502 | const std::string search_term_key; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 503 | } test_data[] = { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 504 | { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 505 | |
| 506 | // No query key should result in empty values. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 507 | { "http://blah/{searchTerms}", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 508 | |
| 509 | // No term should result in empty values. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 510 | { "http://blah/", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 511 | |
| 512 | // Multiple terms should result in empty values. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 513 | { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 514 | |
| 515 | // Term in the host shouldn't match. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 516 | { "http://{searchTerms}", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 517 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 518 | { "http://blah/?q={searchTerms}", "blah", "/", "q"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 519 | |
| 520 | // Single term with extra chars in value should match. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 521 | { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 522 | }; |
| 523 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 524 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 525 | TemplateURLData data; |
| 526 | data.SetURL(test_data[i].url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 527 | TemplateURL url(NULL, data); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 528 | EXPECT_EQ(test_data[i].host, url.url_ref().GetHost()); |
| 529 | EXPECT_EQ(test_data[i].path, url.url_ref().GetPath()); |
| 530 | EXPECT_EQ(test_data[i].search_term_key, url.url_ref().GetSearchTermKey()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
| 534 | TEST_F(TemplateURLTest, GoogleBaseSuggestURL) { |
| 535 | static const struct { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 536 | const char* const base_url; |
| 537 | const char* const base_suggest_url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 538 | } data[] = { |
[email protected] | 014010e | 2011-10-01 04:12:44 | [diff] [blame] | 539 | { "http://google.com/", "http://google.com/complete/", }, |
| 540 | { "http://www.google.com/", "http://www.google.com/complete/", }, |
| 541 | { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", }, |
| 542 | { "http://www.google.com.by/", "http://www.google.com.by/complete/", }, |
| 543 | { "http://google.com/intl/xx/", "http://google.com/complete/", }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 544 | }; |
| 545 | |
[email protected] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 546 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 547 | CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url); |
| 548 | } |
| 549 | |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 550 | TEST_F(TemplateURLTest, ParseParameterKnown) { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 551 | std::string parsed_url("{searchTerms}"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 552 | TemplateURLData data; |
| 553 | data.SetURL(parsed_url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 554 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 555 | TemplateURLRef::Replacements replacements; |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 556 | EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements)); |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 557 | EXPECT_EQ(std::string(), parsed_url); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 558 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 559 | EXPECT_EQ(0U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 560 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 561 | } |
| 562 | |
| 563 | TEST_F(TemplateURLTest, ParseParameterUnknown) { |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 564 | std::string parsed_url("{fhqwhgads}abc"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 565 | TemplateURLData data; |
| 566 | data.SetURL(parsed_url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 567 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 568 | TemplateURLRef::Replacements replacements; |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 569 | |
| 570 | // By default, TemplateURLRef should not consider itself prepopulated. |
| 571 | // Therefore we should not replace the unknown parameter. |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 572 | EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 573 | EXPECT_EQ("{fhqwhgads}abc", parsed_url); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 574 | EXPECT_TRUE(replacements.empty()); |
| 575 | |
| 576 | // If the TemplateURLRef is prepopulated, we should remove unknown parameters. |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 577 | parsed_url = "{fhqwhgads}abc"; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 578 | data.prepopulate_id = 1; |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 579 | TemplateURL url2(NULL, data); |
[email protected] | 495c30b | 2012-05-15 18:48:15 | [diff] [blame] | 580 | EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 581 | EXPECT_EQ("abc", parsed_url); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 582 | EXPECT_TRUE(replacements.empty()); |
| 583 | } |
| 584 | |
| 585 | TEST_F(TemplateURLTest, ParseURLEmpty) { |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 586 | TemplateURL url(NULL, TemplateURLData()); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 587 | TemplateURLRef::Replacements replacements; |
| 588 | bool valid = false; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 589 | EXPECT_EQ(std::string(), |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 590 | url.url_ref().ParseURL(std::string(), &replacements, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 591 | EXPECT_TRUE(replacements.empty()); |
| 592 | EXPECT_TRUE(valid); |
| 593 | } |
| 594 | |
| 595 | TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 596 | TemplateURLData data; |
| 597 | data.SetURL("{"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 598 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 599 | TemplateURLRef::Replacements replacements; |
| 600 | bool valid = false; |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 601 | EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 602 | EXPECT_TRUE(replacements.empty()); |
| 603 | EXPECT_FALSE(valid); |
| 604 | } |
| 605 | |
| 606 | TEST_F(TemplateURLTest, ParseURLNoKnownParameters) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 607 | TemplateURLData data; |
| 608 | data.SetURL("{}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 609 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 610 | TemplateURLRef::Replacements replacements; |
| 611 | bool valid = false; |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 612 | EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 613 | EXPECT_TRUE(replacements.empty()); |
| 614 | EXPECT_TRUE(valid); |
| 615 | } |
| 616 | |
| 617 | TEST_F(TemplateURLTest, ParseURLTwoParameters) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 618 | TemplateURLData data; |
| 619 | data.SetURL("{}{{%s}}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 620 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 621 | TemplateURLRef::Replacements replacements; |
| 622 | bool valid = false; |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 623 | EXPECT_EQ("{}{}", |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 624 | url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 625 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 626 | EXPECT_EQ(3U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 627 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 628 | EXPECT_TRUE(valid); |
| 629 | } |
| 630 | |
| 631 | TEST_F(TemplateURLTest, ParseURLNestedParameter) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 632 | TemplateURLData data; |
| 633 | data.SetURL("{%s"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 634 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 635 | TemplateURLRef::Replacements replacements; |
| 636 | bool valid = false; |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 637 | EXPECT_EQ("{", |
| 638 | url.url_ref().ParseURL("{{searchTerms}", &replacements, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 639 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 640 | EXPECT_EQ(1U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 641 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 642 | EXPECT_TRUE(valid); |
| 643 | } |