Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / search / instant_unittest_base.cc
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.
4
5 #include "chrome/browser/search/instant_unittest_base.h"
6 #include <string>
7
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/google/google_url_tracker.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/search_terms_data.h"
16 #include "chrome/browser/search_engines/template_url.h"
17 #include "chrome/browser/search_engines/template_url_service.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/browser_with_test_window_test.h"
21 #include "chrome/test/base/testing_pref_service_syncable.h"
22 #include "chrome/test/base/ui_test_utils.h"
23 #include "components/variations/entropy_provider.h"
24 #include "content/public/browser/notification_details.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/notification_source.h"
27
28 InstantUnitTestBase::InstantUnitTestBase() {
29   field_trial_list_.reset(new base::FieldTrialList(
30       new metrics::SHA1EntropyProvider("42")));
31 }
32
33 InstantUnitTestBase::~InstantUnitTestBase() {
34 }
35
36 void InstantUnitTestBase::SetUp() {
37   chrome::EnableQueryExtractionForTesting();
38   SetUpHelper();
39 }
40
41 void InstantUnitTestBase::TearDown() {
42   UIThreadSearchTermsData::SetGoogleBaseURL("");
43   BrowserWithTestWindowTest::TearDown();
44 }
45
46 #if !defined(OS_IOS) && !defined(OS_ANDROID)
47 void InstantUnitTestBase::SetUpWithoutQueryExtraction() {
48   SetUpHelper();
49 }
50 #endif
51
52 void InstantUnitTestBase::SetUserSelectedDefaultSearchProvider(
53     const std::string& base_url) {
54   TemplateURLData data;
55   data.SetKeyword(base::UTF8ToUTF16(base_url));
56   data.SetURL(base_url + "url?bar={searchTerms}");
57   data.instant_url = base_url +
58       "instant?{google:omniboxStartMarginParameter}{google:forceInstantResults}"
59       "foo=foo#foo=foo&strk";
60   data.new_tab_url = base_url + "newtab";
61   data.alternate_urls.push_back(base_url + "alt#quux={searchTerms}");
62   data.search_terms_replacement_key = "strk";
63
64   TemplateURL* template_url = new TemplateURL(profile(), data);
65   // Takes ownership of |template_url|.
66   template_url_service_->Add(template_url);
67   template_url_service_->SetUserSelectedDefaultSearchProvider(template_url);
68 }
69
70 void InstantUnitTestBase::NotifyGoogleBaseURLUpdate(
71     const std::string& new_google_base_url) {
72   // GoogleURLTracker is not created in tests.
73   // (See GoogleURLTrackerFactory::ServiceIsNULLWhileTesting())
74   // For determining google:baseURL for NTP, the following is used:
75   // UIThreadSearchTermsData::GoogleBaseURLValue()
76   // For simulating test behavior, this is overridden below.
77   UIThreadSearchTermsData::SetGoogleBaseURL(new_google_base_url);
78   GoogleURLTracker::UpdatedDetails details(GURL("https://www.google.com/"),
79                                            GURL(new_google_base_url));
80   content::NotificationService::current()->Notify(
81       chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
82       content::Source<Profile>(profile()->GetOriginalProfile()),
83       content::Details<GoogleURLTracker::UpdatedDetails>(&details));
84 }
85
86 bool InstantUnitTestBase::IsInstantServiceObserver(
87     InstantServiceObserver* observer) {
88   return instant_service_->observers_.HasObserver(observer);
89 }
90
91 TestingProfile* InstantUnitTestBase::CreateProfile() {
92   TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile();
93   TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
94       profile, &TemplateURLServiceFactory::BuildInstanceFor);
95   return profile;
96 }
97
98 void InstantUnitTestBase::SetUpHelper() {
99   BrowserWithTestWindowTest::SetUp();
100
101   template_url_service_ = TemplateURLServiceFactory::GetForProfile(profile());
102   ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service_);
103
104   UIThreadSearchTermsData::SetGoogleBaseURL("https://www.google.com/");
105   TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService();
106   pref_service->SetUserPref(prefs::kLastPromptedGoogleURL,
107                             new base::StringValue("https://www.google.com/"));
108   SetUserSelectedDefaultSearchProvider("{google:baseURL}");
109   instant_service_ = InstantServiceFactory::GetForProfile(profile());
110 }