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