Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / search / instant_unittest_base.cc
blob93a8b24a2597a3bb6ea03e9204827657af22832e
1 // Copyright 2013 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.
5 #include "chrome/browser/search/instant_unittest_base.h"
7 #include <string>
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search/instant_service.h"
13 #include "chrome/browser/search/instant_service_factory.h"
14 #include "chrome/browser/search/search.h"
15 #include "chrome/browser/search_engines/template_url_service_factory.h"
16 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
17 #include "chrome/test/base/browser_with_test_window_test.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "components/google/core/browser/google_pref_names.h"
20 #include "components/google/core/browser/google_url_tracker.h"
21 #include "components/search/search.h"
22 #include "components/search_engines/template_url.h"
23 #include "components/search_engines/template_url_service.h"
24 #include "components/variations/entropy_provider.h"
26 InstantUnitTestBase::InstantUnitTestBase() {
27 field_trial_list_.reset(new base::FieldTrialList(
28 new metrics::SHA1EntropyProvider("42")));
31 InstantUnitTestBase::~InstantUnitTestBase() {
34 void InstantUnitTestBase::SetUp() {
35 search::EnableQueryExtractionForTesting();
36 SetUpHelper();
39 void InstantUnitTestBase::TearDown() {
40 UIThreadSearchTermsData::SetGoogleBaseURL("");
41 BrowserWithTestWindowTest::TearDown();
44 #if !defined(OS_IOS) && !defined(OS_ANDROID)
45 void InstantUnitTestBase::SetUpWithoutQueryExtraction() {
46 SetUpHelper();
48 #endif
50 void InstantUnitTestBase::SetUserSelectedDefaultSearchProvider(
51 const std::string& base_url) {
52 TemplateURLData data;
53 data.SetShortName(base::UTF8ToUTF16(base_url));
54 data.SetKeyword(base::UTF8ToUTF16(base_url));
55 data.SetURL(base_url + "url?bar={searchTerms}");
56 data.instant_url = base_url +
57 "instant?{google:omniboxStartMarginParameter}{google:forceInstantResults}"
58 "foo=foo#foo=foo&strk";
59 data.new_tab_url = base_url + "newtab";
60 data.alternate_urls.push_back(base_url + "alt#quux={searchTerms}");
61 data.search_terms_replacement_key = "strk";
63 TemplateURL* template_url = new TemplateURL(data);
64 // Takes ownership of |template_url|.
65 template_url_service_->Add(template_url);
66 template_url_service_->SetUserSelectedDefaultSearchProvider(template_url);
69 void InstantUnitTestBase::NotifyGoogleBaseURLUpdate(
70 const std::string& new_google_base_url) {
71 // GoogleURLTracker is not created in tests.
72 // (See GoogleURLTrackerFactory::ServiceIsNULLWhileTesting())
73 // For determining google:baseURL for NTP, the following is used:
74 // UIThreadSearchTermsData::GoogleBaseURLValue()
75 // For simulating test behavior, this is overridden below.
76 UIThreadSearchTermsData::SetGoogleBaseURL(new_google_base_url);
77 TemplateURLServiceFactory::GetForProfile(profile())->GoogleBaseURLChanged();
80 bool InstantUnitTestBase::IsInstantServiceObserver(
81 const InstantServiceObserver* observer) const {
82 return instant_service_->observers_.HasObserver(observer);
85 TestingProfile* InstantUnitTestBase::CreateProfile() {
86 TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile();
87 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
88 profile, &TemplateURLServiceFactory::BuildInstanceFor);
89 return profile;
92 void InstantUnitTestBase::SetUpHelper() {
93 BrowserWithTestWindowTest::SetUp();
95 template_url_service_ = TemplateURLServiceFactory::GetForProfile(profile());
96 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service_);
98 UIThreadSearchTermsData::SetGoogleBaseURL("https://www.google.com/");
99 SetUserSelectedDefaultSearchProvider("{google:baseURL}");
100 instant_service_ = InstantServiceFactory::GetForProfile(profile());