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