Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(731)

Side by Side Diff: chrome/browser/search_engines/template_url_service_test_util.cc

Issue 17127002: Correctly integrate StoragePartition into TestingProfile. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments. merge to ToT. Murder a DB thread and more TestBrowserThreads. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/search_engines/template_url_service_test_util.h" 5 #include "chrome/browser/search_engines/template_url_service_test_util.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop.h"
9 #include "base/path_service.h" 8 #include "base/path_service.h"
10 #include "base/synchronization/waitable_event.h" 9 #include "base/run_loop.h"
11 #include "base/threading/thread.h" 10 #include "base/threading/thread.h"
12 #include "chrome/browser/google/google_url_tracker.h" 11 #include "chrome/browser/google/google_url_tracker.h"
13 #include "chrome/browser/search_engines/search_terms_data.h" 12 #include "chrome/browser/search_engines/search_terms_data.h"
14 #include "chrome/browser/search_engines/template_url_service.h" 13 #include "chrome/browser/search_engines/template_url_service.h"
15 #include "chrome/browser/search_engines/template_url_service_factory.h" 14 #include "chrome/browser/search_engines/template_url_service_factory.h"
16 #include "chrome/browser/webdata/web_data_service_factory.h" 15 #include "chrome/browser/webdata/web_data_service_factory.h"
17 #include "chrome/common/chrome_notification_types.h" 16 #include "chrome/common/chrome_notification_types.h"
18 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
19 #include "chrome/test/automation/value_conversion_util.h" 18 #include "chrome/test/automation/value_conversion_util.h"
20 #include "chrome/test/base/testing_pref_service_syncable.h" 19 #include "chrome/test/base/testing_pref_service_syncable.h"
21 #include "chrome/test/base/testing_profile.h" 20 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/browser/notification_service.h" 21 #include "content/public/browser/notification_service.h"
23 #include "content/public/test/test_browser_thread.h"
24 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
25 23
26
27 #if defined(OS_CHROMEOS) 24 #if defined(OS_CHROMEOS)
28 #include "chrome/browser/google/google_util_chromeos.h" 25 #include "chrome/browser/google/google_util_chromeos.h"
29 #endif 26 #endif
30 27
31 using content::BrowserThread;
32
33 namespace {
34
35 // A callback used to coordinate when the database has finished processing
36 // requests. See note in BlockTillServiceProcessesRequests for details.
37 //
38 // Schedules a QuitClosure on the message loop it was created with.
39 void QuitCallback(base::MessageLoop* message_loop) {
40 message_loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
41 }
42
43 // Blocks the caller until thread has finished servicing all pending
44 // requests.
45 static void WaitForThreadToProcessRequests(BrowserThread::ID identifier) {
46 // Schedule a task on the thread that is processed after all
47 // pending requests on the thread.
48 BrowserThread::PostTask(
49 identifier,
50 FROM_HERE,
51 base::Bind(&QuitCallback, base::MessageLoop::current()));
52 base::MessageLoop::current()->Run();
53 }
54
55 } // namespace
56
57 // Trivial subclass of TemplateURLService that records the last invocation of 28 // Trivial subclass of TemplateURLService that records the last invocation of
58 // SetKeywordSearchTermsForURL. 29 // SetKeywordSearchTermsForURL.
59 class TestingTemplateURLService : public TemplateURLService { 30 class TestingTemplateURLService : public TemplateURLService {
60 public: 31 public:
61 static BrowserContextKeyedService* Build(content::BrowserContext* profile) { 32 static BrowserContextKeyedService* Build(content::BrowserContext* profile) {
62 return new TestingTemplateURLService(static_cast<Profile*>(profile)); 33 return new TestingTemplateURLService(static_cast<Profile*>(profile));
63 } 34 }
64 35
65 explicit TestingTemplateURLService(Profile* profile) 36 explicit TestingTemplateURLService(Profile* profile)
66 : TemplateURLService(profile) { 37 : TemplateURLService(profile) {
(...skipping 12 matching lines...) Expand all
79 search_term_ = term; 50 search_term_ = term;
80 } 51 }
81 52
82 private: 53 private:
83 string16 search_term_; 54 string16 search_term_;
84 55
85 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService); 56 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService);
86 }; 57 };
87 58
88 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil() 59 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
89 : ui_thread_(BrowserThread::UI, &message_loop_), 60 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
90 db_thread_(BrowserThread::DB),
91 io_thread_(BrowserThread::IO),
92 changed_count_(0) { 61 changed_count_(0) {
93 } 62 }
94 63
95 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { 64 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
96 } 65 }
97 66
98 void TemplateURLServiceTestUtil::SetUp() { 67 void TemplateURLServiceTestUtil::SetUp() {
99 // Make unique temp directory. 68 // Make unique temp directory.
100 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
101 profile_.reset(new TestingProfile(temp_dir_.path())); 70 profile_.reset(new TestingProfile(temp_dir_.path()));
102 db_thread_.Start();
103 profile_->CreateWebDataService(); 71 profile_->CreateWebDataService();
104 72
105 TemplateURLService* service = static_cast<TemplateURLService*>( 73 TemplateURLService* service = static_cast<TemplateURLService*>(
106 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 74 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
107 profile_.get(), TestingTemplateURLService::Build)); 75 profile_.get(), TestingTemplateURLService::Build));
108 service->AddObserver(this); 76 service->AddObserver(this);
109 77
110 #if defined(OS_CHROMEOS) 78 #if defined(OS_CHROMEOS)
111 google_util::chromeos::ClearBrandForCurrentSession(); 79 google_util::chromeos::ClearBrandForCurrentSession();
112 #endif 80 #endif
113 } 81 }
114 82
115 void TemplateURLServiceTestUtil::TearDown() { 83 void TemplateURLServiceTestUtil::TearDown() {
116 if (profile_.get()) { 84 profile_.reset();
117 // Clear the request context so it will get deleted. This should be done
118 // before shutting down the I/O thread to avoid memory leaks.
119 profile_->ResetRequestContext();
120 profile_.reset();
121 }
122
123 // Wait for the delete of the request context to happen.
124 if (io_thread_.IsRunning())
125 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests();
126
127 // The I/O thread must be shutdown before the DB thread.
128 io_thread_.Stop();
129
130 // Note that we must ensure the DB thread is stopped after WDS
131 // shutdown (so it can commit pending transactions) but before
132 // deleting the test profile directory, otherwise we may not be
133 // able to delete it due to an open transaction.
134 // Schedule another task on the DB thread to notify us that it's safe to
135 // carry on with the test.
136 base::WaitableEvent done(false, false);
137 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
138 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done)));
139 done.Wait();
140 base::MessageLoop::current()->PostTask(FROM_HERE,
141 base::MessageLoop::QuitClosure());
142 base::MessageLoop::current()->Run();
143 db_thread_.Stop();
144 85
145 UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); 86 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
146 87
147 // Flush the message loop to make application verifiers happy. 88 // Flush the message loop to make application verifiers happy.
148 message_loop_.RunUntilIdle(); 89 base::RunLoop().RunUntilIdle();
149 } 90 }
150 91
151 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() { 92 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() {
152 changed_count_++; 93 changed_count_++;
153 } 94 }
154 95
155 int TemplateURLServiceTestUtil::GetObserverCount() { 96 int TemplateURLServiceTestUtil::GetObserverCount() {
156 return changed_count_; 97 return changed_count_;
157 } 98 }
158 99
159 void TemplateURLServiceTestUtil::ResetObserverCount() { 100 void TemplateURLServiceTestUtil::ResetObserverCount() {
160 changed_count_ = 0; 101 changed_count_ = 0;
161 } 102 }
162 103
163 void TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests() {
164 WaitForThreadToProcessRequests(BrowserThread::DB);
165 }
166
167 void TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests() {
168 WaitForThreadToProcessRequests(BrowserThread::IO);
169 }
170
171 void TemplateURLServiceTestUtil::VerifyLoad() { 104 void TemplateURLServiceTestUtil::VerifyLoad() {
172 ASSERT_FALSE(model()->loaded()); 105 ASSERT_FALSE(model()->loaded());
173 model()->Load(); 106 model()->Load();
174 BlockTillServiceProcessesRequests(); 107 base::RunLoop().RunUntilIdle();
175 EXPECT_EQ(1, GetObserverCount()); 108 EXPECT_EQ(1, GetObserverCount());
176 ResetObserverCount(); 109 ResetObserverCount();
177 } 110 }
178 111
179 void TemplateURLServiceTestUtil::ChangeModelToLoadState() { 112 void TemplateURLServiceTestUtil::ChangeModelToLoadState() {
180 model()->ChangeToLoadedState(); 113 model()->ChangeToLoadedState();
181 // Initialize the web data service so that the database gets updated with 114 // Initialize the web data service so that the database gets updated with
182 // any changes made. 115 // any changes made.
183 116
184 model()->service_ = WebDataService::FromBrowserContext(profile_.get()); 117 model()->service_ = WebDataService::FromBrowserContext(profile_.get());
185 BlockTillServiceProcessesRequests(); 118 base::RunLoop().RunUntilIdle();
186 } 119 }
187 120
188 void TemplateURLServiceTestUtil::ClearModel() { 121 void TemplateURLServiceTestUtil::ClearModel() {
189 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( 122 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
190 profile_.get(), NULL); 123 profile_.get(), NULL);
191 } 124 }
192 125
193 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) { 126 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) {
194 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 127 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
195 profile_.get(), TestingTemplateURLService::Build); 128 profile_.get(), TestingTemplateURLService::Build);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 content::NotificationService::NoDetails()); 204 content::NotificationService::NoDetails());
272 } 205 }
273 206
274 TemplateURLService* TemplateURLServiceTestUtil::model() const { 207 TemplateURLService* TemplateURLServiceTestUtil::model() const {
275 return TemplateURLServiceFactory::GetForProfile(profile_.get()); 208 return TemplateURLServiceFactory::GetForProfile(profile_.get());
276 } 209 }
277 210
278 TestingProfile* TemplateURLServiceTestUtil::profile() const { 211 TestingProfile* TemplateURLServiceTestUtil::profile() const {
279 return profile_.get(); 212 return profile_.get();
280 } 213 }
281
282 void TemplateURLServiceTestUtil::StartIOThread() {
283 io_thread_.StartIOThread();
284 }
285
286 void TemplateURLServiceTestUtil::PumpLoop() {
287 message_loop_.RunUntilIdle();
288 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698