OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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/memory/scoped_ptr.h" | |
6 #include "base/run_loop.h" | |
7 #include "chrome/browser/history/web_history_service.h" | |
sky
2014/11/19 01:02:41
nit: this should be your first include (unittests
rpetterson
2014/11/19 02:04:56
Done.
| |
8 #include "chrome/browser/history/web_history_service_factory.h" | |
9 #include "chrome/browser/sync/profile_sync_service.h" | |
10 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
11 #include "chrome/browser/sync/profile_sync_service_mock.h" | |
12 #include "chrome/test/base/testing_profile.h" | |
13 #include "content/public/test/test_browser_thread_bundle.h" | |
14 #include "net/http/http_status_code.h" | |
15 #include "testing/gmock/include/gmock/gmock.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 using history::WebHistoryService; | |
19 using ::testing::Return; | |
20 | |
21 namespace { | |
22 | |
23 // A testing web history service that does extra checks and creates a | |
24 // TestRequest instead of a normal request. | |
25 class TestingWebHistoryService : public WebHistoryService { | |
26 public: | |
27 explicit TestingWebHistoryService(Profile* profile) | |
28 : WebHistoryService(profile), | |
29 expected_url_(GURL()), | |
30 expected_audio_history_value_(false), | |
31 current_expected_post_data_("") { | |
32 } | |
33 ~TestingWebHistoryService() override {} | |
34 | |
35 WebHistoryService::Request* CreateRequest( | |
36 const GURL& url, const CompletionCallback& callback) override; | |
37 | |
38 const std::string& GetExpectedPostData(WebHistoryService::Request* request); | |
39 | |
40 std::string GetExpectedAudioHistoryValue(); | |
41 | |
42 void SetAudioHistoryCallback(bool success, bool new_enabled_value); | |
43 | |
44 void GetAudioHistoryCallback(bool success, bool new_enabled_value); | |
45 | |
46 void MultipleRequestsCallback(bool success, bool new_enabled_value); | |
47 | |
48 std::set<Request*>& pending_audio_history_requests() { | |
49 return pending_audio_history_requests_; | |
50 } | |
51 | |
52 void SetExpectedURL(const GURL& expected_url) { | |
53 expected_url_ = expected_url; | |
54 } | |
55 | |
56 void SetExpectedAudioHistoryValue(bool expected_value) { | |
57 expected_audio_history_value_ = expected_value; | |
58 } | |
59 | |
60 void SetExpectedPostData(const std::string& expected_data) { | |
61 current_expected_post_data_= expected_data; | |
62 } | |
63 | |
64 void EnsureNoPendingRequestsRemain() { | |
65 EXPECT_EQ(0u, pending_audio_history_requests_.size()); | |
66 } | |
67 | |
68 private: | |
69 GURL expected_url_; | |
70 bool expected_audio_history_value_; | |
71 std::string current_expected_post_data_; | |
72 std::map<Request*, std::string> expected_post_data_; | |
73 | |
74 DISALLOW_COPY_AND_ASSIGN(TestingWebHistoryService); | |
75 }; | |
76 | |
77 // A testing request class that allows expected values to be filled in. | |
78 class TestRequest : public WebHistoryService::Request { | |
79 public: | |
80 TestRequest(Profile* profile, | |
81 const GURL& url, | |
82 const WebHistoryService::CompletionCallback& callback, | |
83 int response_code, | |
84 const std::string& response_body) | |
85 : profile_(profile), | |
86 web_history_service_(nullptr), | |
87 url_(url), | |
88 callback_(callback), | |
89 response_code_(response_code), | |
90 response_body_(response_body), | |
91 post_data_(""), | |
92 is_pending_(false) { | |
93 } | |
94 | |
95 TestRequest(Profile* profile, | |
96 const GURL& url, | |
97 const WebHistoryService::CompletionCallback& callback, | |
98 TestingWebHistoryService* web_history_service) | |
99 : profile_(profile), | |
100 web_history_service_(web_history_service), | |
101 url_(url), | |
102 callback_(callback), | |
103 response_code_(net::HTTP_OK), | |
104 response_body_(""), | |
105 post_data_(""), | |
106 is_pending_(false) { | |
107 response_body_ = std::string("{\"history_recording_enabled\":") + | |
108 web_history_service->GetExpectedAudioHistoryValue() + | |
109 ("}"); | |
110 } | |
111 | |
112 ~TestRequest() override {} | |
113 | |
114 // history::Request overrides | |
115 bool is_pending() override { return is_pending_; } | |
116 int response_code() override { return response_code_; } | |
117 const std::string& response_body() override { return response_body_; } | |
118 void set_post_data(const std::string& post_data) override { | |
119 post_data_ = post_data; | |
120 } | |
121 | |
122 const std::string& post_data() { return post_data_; } | |
123 | |
124 void Start() override { | |
125 is_pending_ = true; | |
126 base::MessageLoop::current()->PostTask( | |
127 FROM_HERE, | |
128 base::Bind(&TestRequest::MimicReturnFromFetch, base::Unretained(this))); | |
129 } | |
130 | |
131 void MimicReturnFromFetch() { | |
132 // Mimic a successful fetch and return. We don't actually send out a request | |
133 // in unittests. | |
134 EXPECT_EQ(web_history_service_->GetExpectedPostData(this), post_data_); | |
135 callback_.Run(this, true); | |
136 } | |
137 | |
138 private: | |
139 Profile* profile_; | |
140 TestingWebHistoryService* web_history_service_; | |
141 GURL url_; | |
142 WebHistoryService::CompletionCallback callback_; | |
143 int response_code_; | |
144 std::string response_body_; | |
145 std::string post_data_; | |
146 bool is_pending_; | |
147 | |
148 DISALLOW_COPY_AND_ASSIGN(TestRequest); | |
149 }; | |
150 | |
151 WebHistoryService::Request* TestingWebHistoryService::CreateRequest( | |
152 const GURL& url, const CompletionCallback& callback) { | |
153 EXPECT_EQ(expected_url_, url); | |
154 WebHistoryService::Request* request = | |
155 new TestRequest(profile_, url, callback, this); | |
156 expected_post_data_[request] = current_expected_post_data_; | |
157 return request; | |
158 } | |
159 | |
160 void TestingWebHistoryService::SetAudioHistoryCallback( | |
161 bool success, bool new_enabled_value) { | |
162 EXPECT_TRUE(success); | |
163 // |new_enabled_value| should be equal to whatever the audio history value | |
164 // was just set to. | |
165 EXPECT_EQ(expected_audio_history_value_, new_enabled_value); | |
166 EXPECT_EQ(1u, pending_audio_history_requests_.size()); | |
167 } | |
168 | |
169 void TestingWebHistoryService::GetAudioHistoryCallback( | |
170 bool success, bool new_enabled_value) { | |
171 EXPECT_TRUE(success); | |
172 EXPECT_EQ(expected_audio_history_value_, new_enabled_value); | |
173 EXPECT_EQ(1u, pending_audio_history_requests_.size()); | |
174 } | |
175 | |
176 void TestingWebHistoryService::MultipleRequestsCallback( | |
177 bool success, bool new_enabled_value) { | |
178 EXPECT_TRUE(success); | |
179 EXPECT_EQ(expected_audio_history_value_, new_enabled_value); | |
180 } | |
181 | |
182 const std::string& TestingWebHistoryService::GetExpectedPostData( | |
183 Request* request) { | |
184 return expected_post_data_[request]; | |
185 } | |
186 | |
187 std::string TestingWebHistoryService::GetExpectedAudioHistoryValue() { | |
188 if (expected_audio_history_value_) | |
189 return "true"; | |
190 return "false"; | |
191 } | |
192 | |
193 static KeyedService* BuildWebHistoryService(content::BrowserContext* profile) { | |
194 return new TestingWebHistoryService(static_cast<Profile*>(profile)); | |
195 } | |
196 | |
197 } // namespace | |
198 | |
199 // A test class used for testing the WebHistoryService class. | |
200 // In order for WebHistoryService to be valid, we must have a valid | |
201 // ProfileSyncService. Using the ProfileSyncServiceMock class allows to | |
202 // assign specific return values as needed to make sure the web history | |
203 // service is available. | |
204 class WebHistoryServiceTest : public testing::Test { | |
205 public: | |
206 WebHistoryServiceTest() | |
207 : thread_bundle_(content::TestBrowserThreadBundle::REAL_DB_THREAD) {} | |
208 virtual ~WebHistoryServiceTest() {} | |
209 | |
210 void SetUp() override { | |
211 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
212 &profile_, &ProfileSyncServiceMock::BuildMockProfileSyncService); | |
213 // Use SetTestingFactoryAndUse to force creation and initialization. | |
214 WebHistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
215 &profile_, &BuildWebHistoryService); | |
216 | |
217 ProfileSyncServiceMock* sync_service = static_cast<ProfileSyncServiceMock*>( | |
218 ProfileSyncServiceFactory::GetInstance()->GetForProfile(&profile_)); | |
219 EXPECT_CALL(*sync_service, | |
220 SyncActive()).WillRepeatedly(Return(true)); | |
221 syncer::ModelTypeSet result; | |
222 result.Put(syncer::HISTORY_DELETE_DIRECTIVES); | |
223 EXPECT_CALL(*sync_service, | |
224 GetActiveDataTypes()).WillRepeatedly(Return(result)); | |
225 } | |
226 void TearDown() override { | |
227 base::RunLoop run_loop; | |
228 base::MessageLoop::current()->PostTask(FROM_HERE, run_loop.QuitClosure()); | |
229 run_loop.Run(); | |
230 } | |
231 Profile* profile() { return &profile_; } | |
232 | |
233 ProfileSyncServiceMock* mock_sync_service() { | |
234 return static_cast<ProfileSyncServiceMock*>( | |
235 ProfileSyncServiceFactory::GetInstance()->GetForProfile(&profile_)); | |
236 } | |
237 | |
238 private: | |
239 | |
240 content::TestBrowserThreadBundle thread_bundle_; | |
241 TestingProfile profile_; | |
242 | |
243 DISALLOW_COPY_AND_ASSIGN(WebHistoryServiceTest); | |
244 }; | |
245 | |
246 TEST_F(WebHistoryServiceTest, GetAudioHistoryEnabled) { | |
247 TestingWebHistoryService* web_history_service = | |
248 static_cast<TestingWebHistoryService*>( | |
249 WebHistoryServiceFactory::GetForProfile(profile())); | |
250 EXPECT_TRUE(web_history_service); | |
251 | |
252 web_history_service->SetExpectedURL( | |
253 GURL("https://history.google.com/history/api/lookup?client=audio")); | |
254 web_history_service->SetExpectedAudioHistoryValue(true); | |
255 web_history_service->GetAudioHistoryEnabled( | |
256 base::Bind(&TestingWebHistoryService::GetAudioHistoryCallback, | |
257 base::Unretained(web_history_service))); | |
258 base::MessageLoop::current()->PostTask( | |
259 FROM_HERE, | |
260 base::Bind(&TestingWebHistoryService::EnsureNoPendingRequestsRemain, | |
261 base::Unretained(web_history_service))); | |
262 } | |
263 | |
264 TEST_F(WebHistoryServiceTest, SetAudioHistoryEnabledTrue) { | |
265 TestingWebHistoryService* web_history_service = | |
266 static_cast<TestingWebHistoryService*>( | |
267 WebHistoryServiceFactory::GetForProfile(profile())); | |
268 EXPECT_TRUE(web_history_service); | |
269 | |
270 web_history_service->SetExpectedURL( | |
271 GURL("https://history.google.com/history/api/change")); | |
272 web_history_service->SetExpectedAudioHistoryValue(true); | |
273 web_history_service->SetExpectedPostData( | |
274 "{\"client\":\"audio\",\"enable_history_recording\":true}"); | |
275 web_history_service->SetAudioHistoryEnabled( | |
276 true, | |
277 base::Bind(&TestingWebHistoryService::SetAudioHistoryCallback, | |
278 base::Unretained(web_history_service))); | |
279 base::MessageLoop::current()->PostTask( | |
280 FROM_HERE, | |
281 base::Bind(&TestingWebHistoryService::EnsureNoPendingRequestsRemain, | |
282 base::Unretained(web_history_service))); | |
283 } | |
284 | |
285 TEST_F(WebHistoryServiceTest, SetAudioHistoryEnabledFalse) { | |
286 TestingWebHistoryService* web_history_service = | |
287 static_cast<TestingWebHistoryService*>( | |
288 WebHistoryServiceFactory::GetForProfile(profile())); | |
289 EXPECT_TRUE(web_history_service); | |
290 | |
291 web_history_service->SetExpectedURL( | |
292 GURL("https://history.google.com/history/api/change")); | |
293 web_history_service->SetExpectedAudioHistoryValue(false); | |
294 web_history_service->SetExpectedPostData( | |
295 "{\"client\":\"audio\",\"enable_history_recording\":false}"); | |
296 web_history_service->SetAudioHistoryEnabled( | |
297 false, | |
298 base::Bind(&TestingWebHistoryService::SetAudioHistoryCallback, | |
299 base::Unretained(web_history_service))); | |
300 base::MessageLoop::current()->PostTask( | |
301 FROM_HERE, | |
302 base::Bind(&TestingWebHistoryService::EnsureNoPendingRequestsRemain, | |
303 base::Unretained(web_history_service))); | |
304 } | |
305 | |
306 TEST_F(WebHistoryServiceTest, MultipleRequests) { | |
307 TestingWebHistoryService* web_history_service = | |
308 static_cast<TestingWebHistoryService*>( | |
309 WebHistoryServiceFactory::GetForProfile(profile())); | |
310 EXPECT_TRUE(web_history_service); | |
311 | |
312 web_history_service->SetExpectedURL( | |
313 GURL("https://history.google.com/history/api/change")); | |
314 web_history_service->SetExpectedAudioHistoryValue(false); | |
315 web_history_service->SetExpectedPostData( | |
316 "{\"client\":\"audio\",\"enable_history_recording\":false}"); | |
317 web_history_service->SetAudioHistoryEnabled( | |
318 false, | |
319 base::Bind(&TestingWebHistoryService::MultipleRequestsCallback, | |
320 base::Unretained(web_history_service))); | |
321 | |
322 web_history_service->SetExpectedURL( | |
323 GURL("https://history.google.com/history/api/lookup?client=audio")); | |
324 web_history_service->SetExpectedPostData(""); | |
325 web_history_service->GetAudioHistoryEnabled( | |
326 base::Bind(&TestingWebHistoryService::MultipleRequestsCallback, | |
327 base::Unretained(web_history_service))); | |
328 | |
329 // Check that both requests are no longer pending. | |
330 base::MessageLoop::current()->PostTask( | |
331 FROM_HERE, | |
332 base::Bind(&TestingWebHistoryService::EnsureNoPendingRequestsRemain, | |
333 base::Unretained(web_history_service))); | |
334 } | |
335 | |
336 TEST_F(WebHistoryServiceTest, VerifyReadResponse) { | |
337 // Test that properly formatted response with good response code returns true | |
338 // as expected. | |
339 WebHistoryService::CompletionCallback completion_callback; | |
340 WebHistoryService::Request* request = new TestRequest( | |
341 profile(), | |
342 GURL("http://history.google.com/"), | |
343 completion_callback, | |
344 net::HTTP_OK, /* response code */ | |
345 "{\n" /* response body */ | |
346 " \"history_recording_enabled\": true\n" | |
347 "}"); | |
348 scoped_ptr<base::DictionaryValue> response_value; | |
349 // ReadResponse deletes the request | |
350 response_value = WebHistoryService::ReadResponse(request); | |
351 bool enabled_value = false; | |
352 response_value->GetBoolean("history_recording_enabled", &enabled_value); | |
353 EXPECT_TRUE(enabled_value); | |
354 | |
355 // Test that properly formatted response with good response code returns false | |
356 // as expected. | |
357 WebHistoryService::Request* request2 = new TestRequest( | |
358 profile(), | |
359 GURL("http://history.google.com/"), | |
360 completion_callback, | |
361 net::HTTP_OK, | |
362 "{\n" | |
363 " \"history_recording_enabled\": false\n" | |
364 "}"); | |
365 scoped_ptr<base::DictionaryValue> response_value2; | |
366 // ReadResponse deletes the request | |
367 response_value2 = WebHistoryService::ReadResponse(request2); | |
368 enabled_value = true; | |
369 response_value2->GetBoolean("history_recording_enabled", &enabled_value); | |
370 EXPECT_FALSE(enabled_value); | |
371 | |
372 // Test that a bad response code returns false. | |
373 WebHistoryService::Request* request3 = new TestRequest( | |
374 profile(), | |
375 GURL("http://history.google.com/"), | |
376 completion_callback, | |
377 net::HTTP_UNAUTHORIZED, | |
378 "{\n" | |
379 " \"history_recording_enabled\": true\n" | |
380 "}"); | |
381 scoped_ptr<base::DictionaryValue> response_value3; | |
382 // ReadResponse deletes the request | |
383 response_value3 = WebHistoryService::ReadResponse(request3); | |
384 EXPECT_FALSE(response_value3); | |
385 | |
386 // Test that improperly formatted response returns false. | |
387 // Note: we expect to see a warning when running this test similar to | |
388 // "Non-JSON response received from history server". | |
389 // This test tests how that situation is handled. | |
390 WebHistoryService::Request* request4 = new TestRequest( | |
391 profile(), | |
392 GURL("http://history.google.com/"), | |
393 completion_callback, | |
394 net::HTTP_OK, | |
395 "{\n" | |
396 " \"history_recording_enabled\": not true\n" | |
397 "}"); | |
398 scoped_ptr<base::DictionaryValue> response_value4; | |
399 // ReadResponse deletes the request | |
400 response_value4 = WebHistoryService::ReadResponse(request4); | |
401 EXPECT_FALSE(response_value4); | |
402 | |
403 // Test that improperly formatted response returns false. | |
404 WebHistoryService::Request* request5 = new TestRequest( | |
405 profile(), | |
406 GURL("http://history.google.com/"), | |
407 completion_callback, | |
408 net::HTTP_OK, | |
409 "{\n" | |
410 " \"history_recording\": true\n" | |
411 "}"); | |
412 scoped_ptr<base::DictionaryValue> response_value5; | |
413 // ReadResponse deletes the request | |
414 response_value5 = WebHistoryService::ReadResponse(request5); | |
415 enabled_value = true; | |
416 EXPECT_FALSE(response_value5->GetBoolean("history_recording_enabled", | |
417 &enabled_value)); | |
418 EXPECT_TRUE(enabled_value); | |
419 } | |
OLD | NEW |