| OLD | NEW |
| 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/chromeos/contacts/google_contact_store.h" | 5 #include "chrome/browser/chromeos/contacts/google_contact_store.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| (...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 test_api_->NotifyAboutNetworkStateChange(false); | 407 test_api_->NotifyAboutNetworkStateChange(false); |
| 408 test_api_->DoUpdate(); | 408 test_api_->DoUpdate(); |
| 409 EXPECT_EQ(0, gdata_service_->num_download_requests()); | 409 EXPECT_EQ(0, gdata_service_->num_download_requests()); |
| 410 | 410 |
| 411 // When we're back online, the update should happen. | 411 // When we're back online, the update should happen. |
| 412 gdata_service_->reset_stats(); | 412 gdata_service_->reset_stats(); |
| 413 test_api_->NotifyAboutNetworkStateChange(true); | 413 test_api_->NotifyAboutNetworkStateChange(true); |
| 414 EXPECT_EQ(1, gdata_service_->num_download_requests()); | 414 EXPECT_EQ(1, gdata_service_->num_download_requests()); |
| 415 } | 415 } |
| 416 | 416 |
| 417 TEST_F(GoogleContactStoreTest, DropDeletedContacts) { |
| 418 // Tell the GData service to return a single contact. |
| 419 scoped_ptr<Contact> contact1(new Contact); |
| 420 InitContact("contact1", "1", false, contact1.get()); |
| 421 ContactPointers gdata_contacts; |
| 422 gdata_contacts.push_back(contact1.get()); |
| 423 gdata_service_->SetContacts(gdata_contacts, base::Time()); |
| 424 |
| 425 // Check that the contact store loads it into memory and saves it to the |
| 426 // database. |
| 427 store_->Init(); |
| 428 EXPECT_EQ(0, gdata_service_->num_download_requests_with_wrong_timestamps()); |
| 429 EXPECT_EQ(base::Time::FromInternalValue(contact1->update_time()), |
| 430 test_api_->last_contact_update_time()); |
| 431 EXPECT_EQ(VarContactsToString(1, contact1.get()), |
| 432 ContactsToString(*test_api_->GetLoadedContacts())); |
| 433 EXPECT_EQ(VarContactsToString(1, contact1.get()), |
| 434 ContactMapToString(db_->contacts())); |
| 435 EXPECT_EQ(contact1->update_time(), |
| 436 db_->metadata().last_contact_update_time()); |
| 437 EXPECT_TRUE(test_api_->update_scheduled()); |
| 438 |
| 439 // Now tell the GData service to return a more-newly-updated, already deleted |
| 440 // contact. |
| 441 scoped_ptr<Contact> contact2(new Contact); |
| 442 InitContact("contact2", "2", true, contact2.get()); |
| 443 contact2->set_update_time( |
| 444 (base::Time::FromInternalValue(contact1->update_time()) + |
| 445 base::TimeDelta::FromSeconds(5)).ToInternalValue()); |
| 446 gdata_contacts.clear(); |
| 447 gdata_contacts.push_back(contact2.get()); |
| 448 gdata_service_->SetContacts( |
| 449 gdata_contacts, |
| 450 base::Time::FromInternalValue(contact1->update_time()) + |
| 451 base::TimeDelta::FromMilliseconds(1)); |
| 452 |
| 453 // The contact store should save the last update time from the deleted |
| 454 // contact, but the contact itself shouldn't be loaded into memory or written |
| 455 // to the database. |
| 456 test_api_->DoUpdate(); |
| 457 EXPECT_EQ(0, gdata_service_->num_download_requests_with_wrong_timestamps()); |
| 458 EXPECT_EQ(base::Time::FromInternalValue(contact2->update_time()), |
| 459 test_api_->last_contact_update_time()); |
| 460 EXPECT_EQ(VarContactsToString(1, contact1.get()), |
| 461 ContactsToString(*test_api_->GetLoadedContacts())); |
| 462 EXPECT_EQ(VarContactsToString(1, contact1.get()), |
| 463 ContactMapToString(db_->contacts())); |
| 464 EXPECT_EQ(contact2->update_time(), |
| 465 db_->metadata().last_contact_update_time()); |
| 466 |
| 467 // Tell the GData service to report the first contact as having been deleted. |
| 468 contact1->set_update_time( |
| 469 (base::Time::FromInternalValue(contact2->update_time()) + |
| 470 base::TimeDelta::FromSeconds(10)).ToInternalValue()); |
| 471 contact1->set_deleted(true); |
| 472 gdata_contacts.clear(); |
| 473 gdata_contacts.push_back(contact1.get()); |
| 474 gdata_service_->SetContacts( |
| 475 gdata_contacts, |
| 476 base::Time::FromInternalValue(contact2->update_time()) + |
| 477 base::TimeDelta::FromMilliseconds(1)); |
| 478 |
| 479 // The contact store should drop the first contact after another update. |
| 480 test_api_->DoUpdate(); |
| 481 EXPECT_EQ(0, gdata_service_->num_download_requests_with_wrong_timestamps()); |
| 482 EXPECT_EQ(base::Time::FromInternalValue(contact1->update_time()), |
| 483 test_api_->last_contact_update_time()); |
| 484 EXPECT_TRUE(test_api_->GetLoadedContacts()->empty()); |
| 485 EXPECT_TRUE(db_->contacts().empty()); |
| 486 EXPECT_EQ(contact1->update_time(), |
| 487 db_->metadata().last_contact_update_time()); |
| 488 } |
| 489 |
| 490 TEST_F(GoogleContactStoreTest, UseLastContactUpdateTimeFromMetadata) { |
| 491 base::Time::Exploded kInitTimeExploded = { 2012, 3, 0, 1, 16, 34, 56, 123 }; |
| 492 base::Time kInitTime = base::Time::FromUTCExploded(kInitTimeExploded); |
| 493 |
| 494 // Configure the metadata to say that a contact was updated one day before the |
| 495 // current time. We won't create a contact that actually contains this time, |
| 496 // though; this mimics the situation where the most-recently-updated contact |
| 497 // has been deleted and wasn't saved to the database. |
| 498 base::Time kDeletedContactUpdateTime = |
| 499 kInitTime - base::TimeDelta::FromDays(1); |
| 500 UpdateMetadata db_metadata; |
| 501 db_metadata.set_last_contact_update_time( |
| 502 kDeletedContactUpdateTime.ToInternalValue()); |
| 503 |
| 504 // Create a non-deleted contact with an update time one day prior to the |
| 505 // update time in the metadata. |
| 506 base::Time kNonDeletedContactUpdateTime = |
| 507 kDeletedContactUpdateTime - base::TimeDelta::FromDays(1); |
| 508 scoped_ptr<Contact> non_deleted_contact(new Contact); |
| 509 InitContact("contact", "1", false, non_deleted_contact.get()); |
| 510 non_deleted_contact->set_update_time( |
| 511 kNonDeletedContactUpdateTime.ToInternalValue()); |
| 512 |
| 513 // Save the contact to the database. |
| 514 ContactPointers db_contacts; |
| 515 db_contacts.push_back(non_deleted_contact.get()); |
| 516 db_->SetContacts(db_contacts, db_metadata); |
| 517 |
| 518 // Tell the GData service to expect the deleted contact's update time. |
| 519 ContactPointers gdata_contacts; |
| 520 gdata_contacts.push_back(non_deleted_contact.get()); |
| 521 gdata_service_->SetContacts( |
| 522 gdata_contacts, |
| 523 kDeletedContactUpdateTime + base::TimeDelta::FromMilliseconds(1)); |
| 524 |
| 525 test_api_->set_current_time(kInitTime); |
| 526 store_->Init(); |
| 527 EXPECT_EQ(0, gdata_service_->num_download_requests_with_wrong_timestamps()); |
| 528 ContactPointers loaded_contacts; |
| 529 store_->AppendContacts(&loaded_contacts); |
| 530 EXPECT_EQ(ContactsToString(gdata_contacts), |
| 531 ContactsToString(loaded_contacts)); |
| 532 EXPECT_TRUE(test_api_->update_scheduled()); |
| 533 |
| 534 } |
| 535 |
| 417 } // namespace test | 536 } // namespace test |
| 418 } // namespace contacts | 537 } // namespace contacts |
| OLD | NEW |