Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / search_engines / template_url_service_test_util.cc
blob4c739f424f15a8f3da7e87d66b92be55db58e13a
1 // Copyright (c) 2012 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_engines/template_url_service_test_util.h"
7 #include "base/message_loop/message_loop_proxy.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/history/history_service_factory.h"
10 #include "chrome/browser/search_engines/chrome_template_url_service_client.h"
11 #include "chrome/test/base/testing_pref_service_syncable.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "components/search_engines/default_search_pref_test_util.h"
14 #include "components/search_engines/keyword_table.h"
15 #include "components/search_engines/keyword_web_data_service.h"
16 #include "components/search_engines/template_url_service.h"
17 #include "components/search_engines/testing_search_terms_data.h"
18 #include "components/webdata/common/web_database_service.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 namespace {
23 class TestingTemplateURLServiceClient : public ChromeTemplateURLServiceClient {
24 public:
25 TestingTemplateURLServiceClient(HistoryService* history_service,
26 base::string16* search_term)
27 : ChromeTemplateURLServiceClient(history_service),
28 search_term_(search_term) {}
30 virtual void SetKeywordSearchTermsForURL(
31 const GURL& url,
32 TemplateURLID id,
33 const base::string16& term) override {
34 *search_term_ = term;
37 private:
38 base::string16* search_term_;
40 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLServiceClient);
43 } // namespace
45 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
46 : changed_count_(0),
47 search_terms_data_(NULL) {
48 // Make unique temp directory.
49 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
50 profile_.reset(new TestingProfile(temp_dir_.path()));
52 scoped_refptr<WebDatabaseService> web_database_service =
53 new WebDatabaseService(temp_dir_.path().AppendASCII("webdata"),
54 base::MessageLoopProxy::current(),
55 base::MessageLoopProxy::current());
56 web_database_service->AddTable(
57 scoped_ptr<WebDatabaseTable>(new KeywordTable()));
58 web_database_service->LoadDatabase();
60 web_data_service_ = new KeywordWebDataService(
61 web_database_service.get(), base::MessageLoopProxy::current(),
62 KeywordWebDataService::ProfileErrorCallback());
63 web_data_service_->Init();
65 ResetModel(false);
68 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
69 ClearModel();
70 profile_.reset();
72 // Flush the message loop to make application verifiers happy.
73 base::RunLoop().RunUntilIdle();
76 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() {
77 changed_count_++;
80 int TemplateURLServiceTestUtil::GetObserverCount() {
81 return changed_count_;
84 void TemplateURLServiceTestUtil::ResetObserverCount() {
85 changed_count_ = 0;
88 void TemplateURLServiceTestUtil::VerifyLoad() {
89 ASSERT_FALSE(model()->loaded());
90 model()->Load();
91 base::RunLoop().RunUntilIdle();
92 EXPECT_EQ(1, GetObserverCount());
93 ResetObserverCount();
96 void TemplateURLServiceTestUtil::ChangeModelToLoadState() {
97 model()->ChangeToLoadedState();
98 // Initialize the web data service so that the database gets updated with
99 // any changes made.
101 model()->web_data_service_ = web_data_service_;
102 base::RunLoop().RunUntilIdle();
105 void TemplateURLServiceTestUtil::ClearModel() {
106 model_->Shutdown();
107 model_.reset();
108 search_terms_data_ = NULL;
111 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) {
112 if (model_)
113 ClearModel();
114 search_terms_data_ = new TestingSearchTermsData("http://www.google.com/");
115 model_.reset(new TemplateURLService(
116 profile()->GetPrefs(), scoped_ptr<SearchTermsData>(search_terms_data_),
117 web_data_service_.get(),
118 scoped_ptr<TemplateURLServiceClient>(
119 new TestingTemplateURLServiceClient(
120 HistoryServiceFactory::GetForProfileIfExists(
121 profile(), Profile::EXPLICIT_ACCESS),
122 &search_term_)),
123 NULL, NULL, base::Closure()));
124 model()->AddObserver(this);
125 changed_count_ = 0;
126 if (verify_load)
127 VerifyLoad();
130 base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() {
131 base::string16 search_term;
132 search_term.swap(search_term_);
133 return search_term;
136 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) {
137 DCHECK(base_url.is_valid());
138 search_terms_data_->set_google_base_url(base_url.spec());
139 model_->GoogleBaseURLChanged();
142 void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences(
143 bool enabled,
144 const std::string& name,
145 const std::string& keyword,
146 const std::string& search_url,
147 const std::string& suggest_url,
148 const std::string& icon_url,
149 const std::string& encodings,
150 const std::string& alternate_url,
151 const std::string& search_terms_replacement_key) {
152 DefaultSearchPrefTestUtil::SetManagedPref(
153 profile()->GetTestingPrefService(),
154 enabled, name, keyword, search_url, suggest_url, icon_url, encodings,
155 alternate_url, search_terms_replacement_key);
158 void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() {
159 DefaultSearchPrefTestUtil::RemoveManagedPref(
160 profile()->GetTestingPrefService());