Changed GenerateKeyword to always return keyword in lowercase

TemplateURL::GenerateKeyword returns keyword for search engine URL using
GURL::host() method. TemplateURLService component that stores
TemplateURLs assumes that keywords are always converted to lowercase.
GURL::host() can return string with uppercase characters for some exotic
URLs. For example for "http://embedded.<html>web" it will return
"embedded.%3Ehtml%3Eweb".
This could lead to problems when TemplateURLService tries to resolve
conflicts between autogenerated keywords.

BUG=709761
[email protected], [email protected]

Review-Url: https://codereview.chromium.org/2806593006
Cr-Commit-Position: refs/heads/master@{#465946}
diff --git a/components/search_engines/template_url_unittest.cc b/components/search_engines/template_url_unittest.cc
index 3177462..2b831485 100644
--- a/components/search_engines/template_url_unittest.cc
+++ b/components/search_engines/template_url_unittest.cc
@@ -6,6 +6,7 @@
 
 #include "base/base_paths.h"
 #include "base/command_line.h"
+#include "base/i18n/case_conversion.h"
 #include "base/macros.h"
 #include "base/strings/string_number_conversions.h"
 #include "base/strings/string_util.h"
@@ -22,6 +23,12 @@
 
 using base::ASCIIToUTF16;
 
+namespace {
+bool IsLowerCase(const base::string16& str) {
+  return str == base::i18n::ToLower(str);
+}
+}
+
 class TemplateURLTest : public testing::Test {
  public:
   TemplateURLTest() : search_terms_data_("http://www.google.com/") {}
@@ -1742,6 +1749,13 @@
   ASSERT_EQ(
       base::UTF8ToUTF16("\xd0\xb0\xd0\xb1\xd0\xb2"),
       TemplateURL::GenerateKeyword(GURL("http://xn--80acd")));
+
+  // Generated keywords must always be in lowercase, because TemplateURLs always
+  // converts keywords to lowercase in its constructor and TemplateURLService
+  // stores TemplateURLs in maps using keyword as key.
+  EXPECT_TRUE(IsLowerCase(TemplateURL::GenerateKeyword(GURL("http://BLAH/"))));
+  EXPECT_TRUE(IsLowerCase(
+      TemplateURL::GenerateKeyword(GURL("http://embeddedhtml.<head>/"))));
 }
 
 TEST_F(TemplateURLTest, GenerateSearchURL) {