Home | History | Annotate | Download | only in search_engines
      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
      3 // found in the LICENSE file.
      4 
      5 #include "base/file_util.h"
      6 #include "base/memory/scoped_temp_dir.h"
      7 #include "base/memory/scoped_vector.h"
      8 #include "base/utf_string_conversions.h"
      9 #include "chrome/browser/search_engines/search_engine_type.h"
     10 #include "chrome/browser/search_engines/search_terms_data.h"
     11 #include "chrome/browser/search_engines/template_url.h"
     12 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
     13 #include "chrome/browser/search_engines/template_url_model.h"
     14 #include "chrome/common/pref_names.h"
     15 #include "chrome/test/testing_pref_service.h"
     16 #include "chrome/test/testing_profile.h"
     17 #include "grit/theme_resources.h"
     18 #include "testing/gtest/include/gtest/gtest.h"
     19 
     20 typedef testing::Test TemplateURLPrepopulateDataTest;
     21 
     22 const int kCountryIds[] = {
     23     'A'<<8|'D', 'A'<<8|'E', 'A'<<8|'F', 'A'<<8|'G', 'A'<<8|'I',
     24     'A'<<8|'L', 'A'<<8|'M', 'A'<<8|'N', 'A'<<8|'O', 'A'<<8|'Q',
     25     'A'<<8|'R', 'A'<<8|'S', 'A'<<8|'T', 'A'<<8|'U', 'A'<<8|'W',
     26     'A'<<8|'X', 'A'<<8|'Z', 'B'<<8|'A', 'B'<<8|'B', 'B'<<8|'D',
     27     'B'<<8|'E', 'B'<<8|'F', 'B'<<8|'G', 'B'<<8|'H', 'B'<<8|'I',
     28     'B'<<8|'J', 'B'<<8|'M', 'B'<<8|'N', 'B'<<8|'O', 'B'<<8|'R',
     29     'B'<<8|'S', 'B'<<8|'T', 'B'<<8|'V', 'B'<<8|'W', 'B'<<8|'Y',
     30     'B'<<8|'Z', 'C'<<8|'A', 'C'<<8|'C', 'C'<<8|'D', 'C'<<8|'F',
     31     'C'<<8|'G', 'C'<<8|'H', 'C'<<8|'I', 'C'<<8|'K', 'C'<<8|'L',
     32     'C'<<8|'M', 'C'<<8|'N', 'C'<<8|'O', 'C'<<8|'R', 'C'<<8|'U',
     33     'C'<<8|'V', 'C'<<8|'X', 'C'<<8|'Y', 'C'<<8|'Z', 'D'<<8|'E',
     34     'D'<<8|'J', 'D'<<8|'K', 'D'<<8|'M', 'D'<<8|'O', 'D'<<8|'Z',
     35     'E'<<8|'C', 'E'<<8|'E', 'E'<<8|'G', 'E'<<8|'R', 'E'<<8|'S',
     36     'E'<<8|'T', 'F'<<8|'I', 'F'<<8|'J', 'F'<<8|'K', 'F'<<8|'M',
     37     'F'<<8|'O', 'F'<<8|'R', 'G'<<8|'A', 'G'<<8|'B', 'G'<<8|'D',
     38     'G'<<8|'E', 'G'<<8|'F', 'G'<<8|'G', 'G'<<8|'H', 'G'<<8|'I',
     39     'G'<<8|'L', 'G'<<8|'M', 'G'<<8|'N', 'G'<<8|'P', 'G'<<8|'Q',
     40     'G'<<8|'R', 'G'<<8|'S', 'G'<<8|'T', 'G'<<8|'U', 'G'<<8|'W',
     41     'G'<<8|'Y', 'H'<<8|'K', 'H'<<8|'M', 'H'<<8|'N', 'H'<<8|'R',
     42     'H'<<8|'T', 'H'<<8|'U', 'I'<<8|'D', 'I'<<8|'E', 'I'<<8|'L',
     43     'I'<<8|'M', 'I'<<8|'N', 'I'<<8|'O', 'I'<<8|'P', 'I'<<8|'Q',
     44     'I'<<8|'R', 'I'<<8|'S', 'I'<<8|'T', 'J'<<8|'E', 'J'<<8|'M',
     45     'J'<<8|'O', 'J'<<8|'P', 'K'<<8|'E', 'K'<<8|'G', 'K'<<8|'H',
     46     'K'<<8|'I', 'K'<<8|'M', 'K'<<8|'N', 'K'<<8|'P', 'K'<<8|'R',
     47     'K'<<8|'W', 'K'<<8|'Y', 'K'<<8|'Z', 'L'<<8|'A', 'L'<<8|'B',
     48     'L'<<8|'C', 'L'<<8|'I', 'L'<<8|'K', 'L'<<8|'R', 'L'<<8|'S',
     49     'L'<<8|'T', 'L'<<8|'U', 'L'<<8|'V', 'L'<<8|'Y', 'M'<<8|'A',
     50     'M'<<8|'C', 'M'<<8|'D', 'M'<<8|'E', 'M'<<8|'G', 'M'<<8|'H',
     51     'M'<<8|'K', 'M'<<8|'L', 'M'<<8|'M', 'M'<<8|'N', 'M'<<8|'O',
     52     'M'<<8|'P', 'M'<<8|'Q', 'M'<<8|'R', 'M'<<8|'S', 'M'<<8|'T',
     53     'M'<<8|'U', 'M'<<8|'V', 'M'<<8|'W', 'M'<<8|'X', 'M'<<8|'Y',
     54     'M'<<8|'Z', 'N'<<8|'A', 'N'<<8|'C', 'N'<<8|'E', 'N'<<8|'F',
     55     'N'<<8|'G', 'N'<<8|'I', 'N'<<8|'L', 'N'<<8|'O', 'N'<<8|'P',
     56     'N'<<8|'R', 'N'<<8|'U', 'N'<<8|'Z', 'O'<<8|'M', 'P'<<8|'A',
     57     'P'<<8|'E', 'P'<<8|'F', 'P'<<8|'G', 'P'<<8|'H', 'P'<<8|'K',
     58     'P'<<8|'L', 'P'<<8|'M', 'P'<<8|'N', 'P'<<8|'R', 'P'<<8|'S',
     59     'P'<<8|'T', 'P'<<8|'W', 'P'<<8|'Y', 'Q'<<8|'A', 'R'<<8|'E',
     60     'R'<<8|'O', 'R'<<8|'S', 'R'<<8|'U', 'R'<<8|'W', 'S'<<8|'A',
     61     'S'<<8|'B', 'S'<<8|'C', 'S'<<8|'D', 'S'<<8|'E', 'S'<<8|'G',
     62     'S'<<8|'H', 'S'<<8|'I', 'S'<<8|'J', 'S'<<8|'K', 'S'<<8|'L',
     63     'S'<<8|'M', 'S'<<8|'N', 'S'<<8|'O', 'S'<<8|'R', 'S'<<8|'T',
     64     'S'<<8|'V', 'S'<<8|'Y', 'S'<<8|'Z', 'T'<<8|'C', 'T'<<8|'D',
     65     'T'<<8|'F', 'T'<<8|'G', 'T'<<8|'H', 'T'<<8|'J', 'T'<<8|'K',
     66     'T'<<8|'L', 'T'<<8|'M', 'T'<<8|'N', 'T'<<8|'O', 'T'<<8|'R',
     67     'T'<<8|'T', 'T'<<8|'V', 'T'<<8|'W', 'T'<<8|'Z', 'U'<<8|'A',
     68     'U'<<8|'G', 'U'<<8|'M', 'U'<<8|'S', 'U'<<8|'Y', 'U'<<8|'Z',
     69     'V'<<8|'A', 'V'<<8|'C', 'V'<<8|'E', 'V'<<8|'G', 'V'<<8|'I',
     70     'V'<<8|'N', 'V'<<8|'U', 'W'<<8|'F', 'W'<<8|'S', 'Y'<<8|'E',
     71     'Y'<<8|'T', 'Z'<<8|'A', 'Z'<<8|'M', 'Z'<<8|'W', -1 };
     72 
     73 // Verifies the set of prepopulate data doesn't contain entries with duplicate
     74 // ids.
     75 TEST_F(TemplateURLPrepopulateDataTest, UniqueIDs) {
     76   TestingProfile profile;
     77   for (size_t i = 0; i < arraysize(kCountryIds); ++i) {
     78     profile.GetPrefs()->SetInteger(prefs::kCountryIDAtInstall, kCountryIds[i]);
     79     ScopedVector<TemplateURL> urls;
     80     size_t default_index;
     81     TemplateURLPrepopulateData::GetPrepopulatedEngines(
     82         profile.GetPrefs(), &(urls.get()), &default_index);
     83     std::set<int> unique_ids;
     84     for (size_t turl_i = 0; turl_i < urls.size(); ++turl_i) {
     85       ASSERT_TRUE(unique_ids.find(urls[turl_i]->prepopulate_id()) ==
     86                   unique_ids.end());
     87       unique_ids.insert(urls[turl_i]->prepopulate_id());
     88     }
     89   }
     90 }
     91 
     92 // Verifies that default search providers from the preferences file
     93 // override the built-in ones.
     94 TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) {
     95   TestingPrefService prefs;
     96   TemplateURLPrepopulateData::RegisterUserPrefs(&prefs);
     97   prefs.SetUserPref(prefs::kSearchProviderOverridesVersion,
     98                     Value::CreateIntegerValue(1));
     99   ListValue* overrides = new ListValue;
    100   DictionaryValue* entry = new DictionaryValue;
    101   entry->SetString("name", "foo");
    102   entry->SetString("keyword", "fook");
    103   entry->SetString("search_url", "http://foo.com/s?q={searchTerms}");
    104   entry->SetString("favicon_url", "http://foi.com/favicon.ico");
    105   entry->SetString("suggest_url", "");
    106   entry->SetString("instant_url", "");
    107   entry->SetString("encoding", "UTF-8");
    108   entry->SetInteger("search_engine_type", 1);
    109   entry->SetInteger("logo_id", 0);
    110   entry->SetInteger("id", 1001);
    111   overrides->Append(entry);
    112   prefs.SetUserPref(prefs::kSearchProviderOverrides, overrides);
    113 
    114   int version = TemplateURLPrepopulateData::GetDataVersion(&prefs);
    115   EXPECT_EQ(1, version);
    116 
    117   ScopedVector<TemplateURL> t_urls;
    118   size_t default_index;
    119   TemplateURLPrepopulateData::GetPrepopulatedEngines(
    120       &prefs, &(t_urls.get()), &default_index);
    121 
    122   ASSERT_EQ(1u, t_urls.size());
    123   EXPECT_EQ(ASCIIToUTF16("foo"), t_urls[0]->short_name());
    124   EXPECT_EQ(ASCIIToUTF16("fook"), t_urls[0]->keyword());
    125   EXPECT_EQ("foo.com", t_urls[0]->url()->GetHost());
    126   EXPECT_EQ("foi.com", t_urls[0]->GetFaviconURL().host());
    127   EXPECT_EQ(1u, t_urls[0]->input_encodings().size());
    128   EXPECT_EQ(1001, t_urls[0]->prepopulate_id());
    129   EXPECT_EQ(SEARCH_ENGINE_GOOGLE, t_urls[0]->search_engine_type());
    130   EXPECT_EQ(0, t_urls[0]->logo_id());
    131 }
    132 
    133 TEST_F(TemplateURLPrepopulateDataTest, SearchEngineFromOrigin) {
    134   UIThreadSearchTermsData search_terms_data;
    135   std::set<GURL> unique_engines;
    136 
    137   { // Scoping for the profile.
    138     TestingProfile profile;
    139     for (size_t i = 0; i < arraysize(kCountryIds); ++i) {
    140       profile.GetPrefs()->SetInteger(prefs::kCountryIDAtInstall,
    141                                      kCountryIds[i]);
    142       ScopedVector<TemplateURL> urls;
    143       size_t default_index;
    144       TemplateURLPrepopulateData::GetPrepopulatedEngines(
    145           profile.GetPrefs(), &(urls.get()), &default_index);
    146       for (size_t turl_i = 0; turl_i < urls.size(); ++turl_i) {
    147         GURL engine_url(urls[turl_i]->url()->url());
    148         if (!engine_url.is_valid()) {
    149           engine_url = TemplateURLModel::GenerateSearchURLUsingTermsData(
    150               urls[turl_i], search_terms_data);
    151         }
    152         GURL origin = engine_url.GetOrigin();
    153         unique_engines.insert(origin);
    154       }
    155     }
    156   }
    157 
    158   TestingProfile profile;
    159   for (std::set<GURL>::iterator it = unique_engines.begin();
    160        it != unique_engines.end(); ++it) {
    161     scoped_ptr<TemplateURL> found_url(
    162         TemplateURLPrepopulateData::GetEngineForOrigin(profile.GetPrefs(),
    163                                                        *it));
    164     EXPECT_EQ(
    165         TemplateURLModel::GenerateSearchURLUsingTermsData(
    166             found_url.get(), search_terms_data).GetOrigin(),
    167         it->GetOrigin());
    168   }
    169 
    170   GURL not_a_search_engine("http://example.com/");
    171   EXPECT_EQ(NULL, TemplateURLPrepopulateData::GetEngineForOrigin(
    172       profile.GetPrefs(),
    173       not_a_search_engine));
    174 }
    175 
    176 TEST_F(TemplateURLPrepopulateDataTest, GetSearchEngineLogo) {
    177   GURL bad_engine("http://example.com/");
    178   EXPECT_EQ(kNoSearchEngineLogo,
    179             TemplateURLPrepopulateData::GetSearchEngineLogo(bad_engine));
    180   GURL engine_with_logo("http://www.ask.com/");
    181   EXPECT_EQ(IDR_SEARCH_ENGINE_LOGO_ASK,
    182             TemplateURLPrepopulateData::GetSearchEngineLogo(engine_with_logo));
    183   GURL engine_no_logo("http://araby.com/");
    184   EXPECT_EQ(kNoSearchEngineLogo,
    185             TemplateURLPrepopulateData::GetSearchEngineLogo(engine_no_logo));
    186 
    187 }
    188