OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/history/web_history_service.h" | 5 #include "components/history/core/browser/web_history_service.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "chrome/browser/history/web_history_service_factory.h" | 9 #include "chrome/browser/history/web_history_service_factory.h" |
| 10 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 11 #include "chrome/browser/signin/signin_manager_factory.h" |
10 #include "chrome/browser/sync/profile_sync_service.h" | 12 #include "chrome/browser/sync/profile_sync_service.h" |
11 #include "chrome/browser/sync/profile_sync_service_factory.h" | 13 #include "chrome/browser/sync/profile_sync_service_factory.h" |
12 #include "chrome/browser/sync/profile_sync_service_mock.h" | 14 #include "chrome/browser/sync/profile_sync_service_mock.h" |
13 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 17 #include "components/signin/core/browser/signin_manager.h" |
14 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
15 #include "net/http/http_status_code.h" | 19 #include "net/http/http_status_code.h" |
| 20 #include "net/url_request/url_request_context_getter.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
18 | 23 |
19 using history::WebHistoryService; | 24 using history::WebHistoryService; |
20 using ::testing::Return; | 25 using ::testing::Return; |
21 | 26 |
22 namespace { | 27 namespace { |
23 | 28 |
24 // A testing web history service that does extra checks and creates a | 29 // A testing web history service that does extra checks and creates a |
25 // TestRequest instead of a normal request. | 30 // TestRequest instead of a normal request. |
26 class TestingWebHistoryService : public WebHistoryService { | 31 class TestingWebHistoryService : public WebHistoryService { |
27 public: | 32 public: |
28 explicit TestingWebHistoryService(Profile* profile) | 33 explicit TestingWebHistoryService( |
29 : WebHistoryService(profile), | 34 ProfileOAuth2TokenService* token_service, |
30 profile_(profile), | 35 SigninManagerBase* signin_manager, |
31 expected_url_(GURL()), | 36 const scoped_refptr<net::URLRequestContextGetter>& request_context) |
32 expected_audio_history_value_(false), | 37 : WebHistoryService(token_service, signin_manager, request_context), |
33 current_expected_post_data_("") { | 38 expected_url_(GURL()), |
34 } | 39 expected_audio_history_value_(false), |
| 40 current_expected_post_data_("") {} |
35 ~TestingWebHistoryService() override {} | 41 ~TestingWebHistoryService() override {} |
36 | 42 |
37 WebHistoryService::Request* CreateRequest( | 43 WebHistoryService::Request* CreateRequest( |
38 const GURL& url, const CompletionCallback& callback) override; | 44 const GURL& url, const CompletionCallback& callback) override; |
39 | 45 |
40 // This is sorta an override but override and static don't mix. | 46 // This is sorta an override but override and static don't mix. |
41 // This function just calls WebHistoryService::ReadResponse. | 47 // This function just calls WebHistoryService::ReadResponse. |
42 static scoped_ptr<base::DictionaryValue> ReadResponse( | 48 static scoped_ptr<base::DictionaryValue> ReadResponse( |
43 Request* request); | 49 Request* request); |
44 | 50 |
(...skipping 17 matching lines...) Expand all Loading... |
62 | 68 |
63 void SetExpectedPostData(const std::string& expected_data) { | 69 void SetExpectedPostData(const std::string& expected_data) { |
64 current_expected_post_data_= expected_data; | 70 current_expected_post_data_= expected_data; |
65 } | 71 } |
66 | 72 |
67 void EnsureNoPendingRequestsRemain() { | 73 void EnsureNoPendingRequestsRemain() { |
68 EXPECT_EQ(0u, GetNumberOfPendingAudioHistoryRequests()); | 74 EXPECT_EQ(0u, GetNumberOfPendingAudioHistoryRequests()); |
69 } | 75 } |
70 | 76 |
71 private: | 77 private: |
72 Profile* profile_; | |
73 GURL expected_url_; | 78 GURL expected_url_; |
74 bool expected_audio_history_value_; | 79 bool expected_audio_history_value_; |
75 std::string current_expected_post_data_; | 80 std::string current_expected_post_data_; |
76 std::map<Request*, std::string> expected_post_data_; | 81 std::map<Request*, std::string> expected_post_data_; |
77 | 82 |
78 DISALLOW_COPY_AND_ASSIGN(TestingWebHistoryService); | 83 DISALLOW_COPY_AND_ASSIGN(TestingWebHistoryService); |
79 }; | 84 }; |
80 | 85 |
81 // A testing request class that allows expected values to be filled in. | 86 // A testing request class that allows expected values to be filled in. |
82 class TestRequest : public WebHistoryService::Request { | 87 class TestRequest : public WebHistoryService::Request { |
83 public: | 88 public: |
84 TestRequest(Profile* profile, | 89 TestRequest(const GURL& url, |
85 const GURL& url, | |
86 const WebHistoryService::CompletionCallback& callback, | 90 const WebHistoryService::CompletionCallback& callback, |
87 int response_code, | 91 int response_code, |
88 const std::string& response_body) | 92 const std::string& response_body) |
89 : profile_(profile), | 93 : web_history_service_(nullptr), |
90 web_history_service_(nullptr), | |
91 url_(url), | 94 url_(url), |
92 callback_(callback), | 95 callback_(callback), |
93 response_code_(response_code), | 96 response_code_(response_code), |
94 response_body_(response_body), | 97 response_body_(response_body), |
95 post_data_(""), | 98 post_data_(""), |
96 is_pending_(false) { | 99 is_pending_(false) { |
97 } | 100 } |
98 | 101 |
99 TestRequest(Profile* profile, | 102 TestRequest(const GURL& url, |
100 const GURL& url, | |
101 const WebHistoryService::CompletionCallback& callback, | 103 const WebHistoryService::CompletionCallback& callback, |
102 TestingWebHistoryService* web_history_service) | 104 TestingWebHistoryService* web_history_service) |
103 : profile_(profile), | 105 : web_history_service_(web_history_service), |
104 web_history_service_(web_history_service), | |
105 url_(url), | 106 url_(url), |
106 callback_(callback), | 107 callback_(callback), |
107 response_code_(net::HTTP_OK), | 108 response_code_(net::HTTP_OK), |
108 response_body_(""), | 109 response_body_(""), |
109 post_data_(""), | 110 post_data_(""), |
110 is_pending_(false) { | 111 is_pending_(false) { |
111 response_body_ = std::string("{\"history_recording_enabled\":") + | 112 response_body_ = std::string("{\"history_recording_enabled\":") + |
112 web_history_service->GetExpectedAudioHistoryValue() + | 113 web_history_service->GetExpectedAudioHistoryValue() + |
113 ("}"); | 114 ("}"); |
114 } | 115 } |
(...skipping 16 matching lines...) Expand all Loading... |
131 } | 132 } |
132 | 133 |
133 void MimicReturnFromFetch() { | 134 void MimicReturnFromFetch() { |
134 // Mimic a successful fetch and return. We don't actually send out a request | 135 // Mimic a successful fetch and return. We don't actually send out a request |
135 // in unittests. | 136 // in unittests. |
136 EXPECT_EQ(web_history_service_->GetExpectedPostData(this), post_data_); | 137 EXPECT_EQ(web_history_service_->GetExpectedPostData(this), post_data_); |
137 callback_.Run(this, true); | 138 callback_.Run(this, true); |
138 } | 139 } |
139 | 140 |
140 private: | 141 private: |
141 Profile* profile_; | |
142 TestingWebHistoryService* web_history_service_; | 142 TestingWebHistoryService* web_history_service_; |
143 GURL url_; | 143 GURL url_; |
144 WebHistoryService::CompletionCallback callback_; | 144 WebHistoryService::CompletionCallback callback_; |
145 int response_code_; | 145 int response_code_; |
146 std::string response_body_; | 146 std::string response_body_; |
147 std::string post_data_; | 147 std::string post_data_; |
148 bool is_pending_; | 148 bool is_pending_; |
149 | 149 |
150 DISALLOW_COPY_AND_ASSIGN(TestRequest); | 150 DISALLOW_COPY_AND_ASSIGN(TestRequest); |
151 }; | 151 }; |
152 | 152 |
153 WebHistoryService::Request* TestingWebHistoryService::CreateRequest( | 153 WebHistoryService::Request* TestingWebHistoryService::CreateRequest( |
154 const GURL& url, const CompletionCallback& callback) { | 154 const GURL& url, const CompletionCallback& callback) { |
155 EXPECT_EQ(expected_url_, url); | 155 EXPECT_EQ(expected_url_, url); |
156 WebHistoryService::Request* request = | 156 WebHistoryService::Request* request = |
157 new TestRequest(profile_, url, callback, this); | 157 new TestRequest(url, callback, this); |
158 expected_post_data_[request] = current_expected_post_data_; | 158 expected_post_data_[request] = current_expected_post_data_; |
159 return request; | 159 return request; |
160 } | 160 } |
161 | 161 |
162 scoped_ptr<base::DictionaryValue> TestingWebHistoryService::ReadResponse( | 162 scoped_ptr<base::DictionaryValue> TestingWebHistoryService::ReadResponse( |
163 Request* request) { | 163 Request* request) { |
164 return WebHistoryService::ReadResponse(request); | 164 return WebHistoryService::ReadResponse(request); |
165 } | 165 } |
166 | 166 |
167 void TestingWebHistoryService::SetAudioHistoryCallback( | 167 void TestingWebHistoryService::SetAudioHistoryCallback( |
(...skipping 20 matching lines...) Expand all Loading... |
188 Request* request) { | 188 Request* request) { |
189 return expected_post_data_[request]; | 189 return expected_post_data_[request]; |
190 } | 190 } |
191 | 191 |
192 std::string TestingWebHistoryService::GetExpectedAudioHistoryValue() { | 192 std::string TestingWebHistoryService::GetExpectedAudioHistoryValue() { |
193 if (expected_audio_history_value_) | 193 if (expected_audio_history_value_) |
194 return "true"; | 194 return "true"; |
195 return "false"; | 195 return "false"; |
196 } | 196 } |
197 | 197 |
198 static KeyedService* BuildWebHistoryService(content::BrowserContext* profile) { | 198 static KeyedService* BuildWebHistoryService(content::BrowserContext* context) { |
199 return new TestingWebHistoryService(static_cast<Profile*>(profile)); | 199 Profile* profile = static_cast<Profile*>(context); |
| 200 return new TestingWebHistoryService( |
| 201 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), |
| 202 SigninManagerFactory::GetForProfile(profile), |
| 203 profile->GetRequestContext()); |
200 } | 204 } |
201 | 205 |
202 } // namespace | 206 } // namespace |
203 | 207 |
204 // A test class used for testing the WebHistoryService class. | 208 // A test class used for testing the WebHistoryService class. |
205 // In order for WebHistoryService to be valid, we must have a valid | 209 // In order for WebHistoryService to be valid, we must have a valid |
206 // ProfileSyncService. Using the ProfileSyncServiceMock class allows to | 210 // ProfileSyncService. Using the ProfileSyncServiceMock class allows to |
207 // assign specific return values as needed to make sure the web history | 211 // assign specific return values as needed to make sure the web history |
208 // service is available. | 212 // service is available. |
209 class WebHistoryServiceTest : public testing::Test { | 213 class WebHistoryServiceTest : public testing::Test { |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 base::Bind(&TestingWebHistoryService::EnsureNoPendingRequestsRemain, | 341 base::Bind(&TestingWebHistoryService::EnsureNoPendingRequestsRemain, |
338 base::Unretained(web_history_service))); | 342 base::Unretained(web_history_service))); |
339 } | 343 } |
340 | 344 |
341 TEST_F(WebHistoryServiceTest, VerifyReadResponse) { | 345 TEST_F(WebHistoryServiceTest, VerifyReadResponse) { |
342 // Test that properly formatted response with good response code returns true | 346 // Test that properly formatted response with good response code returns true |
343 // as expected. | 347 // as expected. |
344 WebHistoryService::CompletionCallback completion_callback; | 348 WebHistoryService::CompletionCallback completion_callback; |
345 scoped_ptr<WebHistoryService::Request> request( | 349 scoped_ptr<WebHistoryService::Request> request( |
346 new TestRequest( | 350 new TestRequest( |
347 profile(), | |
348 GURL("http://history.google.com/"), | 351 GURL("http://history.google.com/"), |
349 completion_callback, | 352 completion_callback, |
350 net::HTTP_OK, /* response code */ | 353 net::HTTP_OK, /* response code */ |
351 "{\n" /* response body */ | 354 "{\n" /* response body */ |
352 " \"history_recording_enabled\": true\n" | 355 " \"history_recording_enabled\": true\n" |
353 "}")); | 356 "}")); |
354 scoped_ptr<base::DictionaryValue> response_value; | 357 scoped_ptr<base::DictionaryValue> response_value; |
355 // ReadResponse deletes the request | 358 // ReadResponse deletes the request |
356 response_value = TestingWebHistoryService::ReadResponse(request.get()); | 359 response_value = TestingWebHistoryService::ReadResponse(request.get()); |
357 bool enabled_value = false; | 360 bool enabled_value = false; |
358 response_value->GetBoolean("history_recording_enabled", &enabled_value); | 361 response_value->GetBoolean("history_recording_enabled", &enabled_value); |
359 EXPECT_TRUE(enabled_value); | 362 EXPECT_TRUE(enabled_value); |
360 | 363 |
361 // Test that properly formatted response with good response code returns false | 364 // Test that properly formatted response with good response code returns false |
362 // as expected. | 365 // as expected. |
363 scoped_ptr<WebHistoryService::Request> request2( | 366 scoped_ptr<WebHistoryService::Request> request2( |
364 new TestRequest( | 367 new TestRequest( |
365 profile(), | |
366 GURL("http://history.google.com/"), | 368 GURL("http://history.google.com/"), |
367 completion_callback, | 369 completion_callback, |
368 net::HTTP_OK, | 370 net::HTTP_OK, |
369 "{\n" | 371 "{\n" |
370 " \"history_recording_enabled\": false\n" | 372 " \"history_recording_enabled\": false\n" |
371 "}")); | 373 "}")); |
372 scoped_ptr<base::DictionaryValue> response_value2; | 374 scoped_ptr<base::DictionaryValue> response_value2; |
373 // ReadResponse deletes the request | 375 // ReadResponse deletes the request |
374 response_value2 = TestingWebHistoryService::ReadResponse(request2.get()); | 376 response_value2 = TestingWebHistoryService::ReadResponse(request2.get()); |
375 enabled_value = true; | 377 enabled_value = true; |
376 response_value2->GetBoolean("history_recording_enabled", &enabled_value); | 378 response_value2->GetBoolean("history_recording_enabled", &enabled_value); |
377 EXPECT_FALSE(enabled_value); | 379 EXPECT_FALSE(enabled_value); |
378 | 380 |
379 // Test that a bad response code returns false. | 381 // Test that a bad response code returns false. |
380 scoped_ptr<WebHistoryService::Request> request3( | 382 scoped_ptr<WebHistoryService::Request> request3( |
381 new TestRequest( | 383 new TestRequest( |
382 profile(), | |
383 GURL("http://history.google.com/"), | 384 GURL("http://history.google.com/"), |
384 completion_callback, | 385 completion_callback, |
385 net::HTTP_UNAUTHORIZED, | 386 net::HTTP_UNAUTHORIZED, |
386 "{\n" | 387 "{\n" |
387 " \"history_recording_enabled\": true\n" | 388 " \"history_recording_enabled\": true\n" |
388 "}")); | 389 "}")); |
389 scoped_ptr<base::DictionaryValue> response_value3; | 390 scoped_ptr<base::DictionaryValue> response_value3; |
390 // ReadResponse deletes the request | 391 // ReadResponse deletes the request |
391 response_value3 = TestingWebHistoryService::ReadResponse(request3.get()); | 392 response_value3 = TestingWebHistoryService::ReadResponse(request3.get()); |
392 EXPECT_FALSE(response_value3); | 393 EXPECT_FALSE(response_value3); |
393 | 394 |
394 // Test that improperly formatted response returns false. | 395 // Test that improperly formatted response returns false. |
395 // Note: we expect to see a warning when running this test similar to | 396 // Note: we expect to see a warning when running this test similar to |
396 // "Non-JSON response received from history server". | 397 // "Non-JSON response received from history server". |
397 // This test tests how that situation is handled. | 398 // This test tests how that situation is handled. |
398 scoped_ptr<WebHistoryService::Request> request4( | 399 scoped_ptr<WebHistoryService::Request> request4( |
399 new TestRequest( | 400 new TestRequest( |
400 profile(), | |
401 GURL("http://history.google.com/"), | 401 GURL("http://history.google.com/"), |
402 completion_callback, | 402 completion_callback, |
403 net::HTTP_OK, | 403 net::HTTP_OK, |
404 "{\n" | 404 "{\n" |
405 " \"history_recording_enabled\": not true\n" | 405 " \"history_recording_enabled\": not true\n" |
406 "}")); | 406 "}")); |
407 scoped_ptr<base::DictionaryValue> response_value4; | 407 scoped_ptr<base::DictionaryValue> response_value4; |
408 // ReadResponse deletes the request | 408 // ReadResponse deletes the request |
409 response_value4 = TestingWebHistoryService::ReadResponse(request4.get()); | 409 response_value4 = TestingWebHistoryService::ReadResponse(request4.get()); |
410 EXPECT_FALSE(response_value4); | 410 EXPECT_FALSE(response_value4); |
411 | 411 |
412 // Test that improperly formatted response returns false. | 412 // Test that improperly formatted response returns false. |
413 scoped_ptr<WebHistoryService::Request> request5( | 413 scoped_ptr<WebHistoryService::Request> request5( |
414 new TestRequest( | 414 new TestRequest( |
415 profile(), | |
416 GURL("http://history.google.com/"), | 415 GURL("http://history.google.com/"), |
417 completion_callback, | 416 completion_callback, |
418 net::HTTP_OK, | 417 net::HTTP_OK, |
419 "{\n" | 418 "{\n" |
420 " \"history_recording\": true\n" | 419 " \"history_recording\": true\n" |
421 "}")); | 420 "}")); |
422 scoped_ptr<base::DictionaryValue> response_value5; | 421 scoped_ptr<base::DictionaryValue> response_value5; |
423 // ReadResponse deletes the request | 422 // ReadResponse deletes the request |
424 response_value5 = TestingWebHistoryService::ReadResponse(request5.get()); | 423 response_value5 = TestingWebHistoryService::ReadResponse(request5.get()); |
425 enabled_value = true; | 424 enabled_value = true; |
426 EXPECT_FALSE(response_value5->GetBoolean("history_recording_enabled", | 425 EXPECT_FALSE(response_value5->GetBoolean("history_recording_enabled", |
427 &enabled_value)); | 426 &enabled_value)); |
428 EXPECT_TRUE(enabled_value); | 427 EXPECT_TRUE(enabled_value); |
429 } | 428 } |
OLD | NEW |