| 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/contact_map.h" | 5 #include "chrome/browser/chromeos/contacts/contact_map.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "chrome/browser/chromeos/contacts/contact.pb.h" | 10 #include "chrome/browser/chromeos/contacts/contact.pb.h" |
| 11 #include "chrome/browser/chromeos/contacts/contact_test_util.h" | 11 #include "chrome/browser/chromeos/contacts/contact_test_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace contacts { | 14 namespace contacts { |
| 15 namespace test { | 15 namespace test { |
| 16 | 16 |
| 17 TEST(ContactMapTest, Merge) { | 17 TEST(ContactMapTest, Merge) { |
| 18 ContactMap map; | 18 ContactMap map; |
| 19 EXPECT_TRUE(map.empty()); | 19 EXPECT_TRUE(map.empty()); |
| 20 EXPECT_EQ(0U, map.size()); | 20 EXPECT_EQ(0U, map.size()); |
| 21 EXPECT_EQ(base::Time(), map.GetMaxUpdateTime()); | |
| 22 | 21 |
| 23 // Create a contact. | 22 // Create a contact. |
| 24 const std::string kContactId1 = "contact_id_1"; | 23 const std::string kContactId1 = "contact_id_1"; |
| 25 scoped_ptr<Contact> contact1(new Contact); | 24 scoped_ptr<Contact> contact1(new Contact); |
| 26 InitContact(kContactId1, "1", false, contact1.get()); | 25 InitContact(kContactId1, "1", false, contact1.get()); |
| 27 const base::Time kUpdateTime1 = base::Time::FromInternalValue(100); | |
| 28 contact1->set_update_time(kUpdateTime1.ToInternalValue()); | |
| 29 | 26 |
| 30 // Merge it into the map and check that it's stored. | 27 // Merge it into the map and check that it's stored. |
| 31 scoped_ptr<ScopedVector<Contact> > contacts_to_merge( | 28 scoped_ptr<ScopedVector<Contact> > contacts_to_merge( |
| 32 new ScopedVector<Contact>); | 29 new ScopedVector<Contact>); |
| 33 contacts_to_merge->push_back(new Contact(*contact1)); | 30 contacts_to_merge->push_back(new Contact(*contact1)); |
| 34 map.Merge(contacts_to_merge.Pass(), ContactMap::KEEP_DELETED_CONTACTS); | 31 map.Merge(contacts_to_merge.Pass(), ContactMap::KEEP_DELETED_CONTACTS); |
| 35 EXPECT_FALSE(map.empty()); | 32 EXPECT_FALSE(map.empty()); |
| 36 EXPECT_EQ(1U, map.size()); | 33 EXPECT_EQ(1U, map.size()); |
| 37 ASSERT_TRUE(map.Find(kContactId1)); | 34 ASSERT_TRUE(map.Find(kContactId1)); |
| 38 EXPECT_EQ(ContactMapToString(map), VarContactsToString(1, contact1.get())); | 35 EXPECT_EQ(ContactMapToString(map), VarContactsToString(1, contact1.get())); |
| 39 EXPECT_EQ(kUpdateTime1, map.GetMaxUpdateTime()); | |
| 40 | 36 |
| 41 // Create a second, deleted contact with a newer update time. | 37 // Create a second, deleted contact. |
| 42 const std::string kContactId2 = "contact_id_2"; | 38 const std::string kContactId2 = "contact_id_2"; |
| 43 scoped_ptr<Contact> contact2(new Contact); | 39 scoped_ptr<Contact> contact2(new Contact); |
| 44 InitContact(kContactId2, "2", true, contact2.get()); | 40 InitContact(kContactId2, "2", true, contact2.get()); |
| 45 const base::Time kUpdateTime2 = | |
| 46 kUpdateTime1 + base::TimeDelta::FromSeconds(10); | |
| 47 contact2->set_update_time(kUpdateTime2.ToInternalValue()); | |
| 48 | 41 |
| 49 // Merge it into the map. Since we request keeping deleted contacts, the | 42 // Merge it into the map. Since we request keeping deleted contacts, the |
| 50 // contact should be saved. | 43 // contact should be saved. |
| 51 contacts_to_merge.reset(new ScopedVector<Contact>); | 44 contacts_to_merge.reset(new ScopedVector<Contact>); |
| 52 contacts_to_merge->push_back(new Contact(*contact2)); | 45 contacts_to_merge->push_back(new Contact(*contact2)); |
| 53 map.Merge(contacts_to_merge.Pass(), ContactMap::KEEP_DELETED_CONTACTS); | 46 map.Merge(contacts_to_merge.Pass(), ContactMap::KEEP_DELETED_CONTACTS); |
| 54 EXPECT_EQ(2U, map.size()); | 47 EXPECT_EQ(2U, map.size()); |
| 55 ASSERT_TRUE(map.Find(kContactId2)); | 48 ASSERT_TRUE(map.Find(kContactId2)); |
| 56 EXPECT_EQ(ContactMapToString(map), | 49 EXPECT_EQ(ContactMapToString(map), |
| 57 VarContactsToString(2, contact1.get(), contact2.get())); | 50 VarContactsToString(2, contact1.get(), contact2.get())); |
| 58 EXPECT_EQ(kUpdateTime2, map.GetMaxUpdateTime()); | |
| 59 | 51 |
| 60 // Update the first contact's update time and merge it into the map. | 52 // Update the first contact's update time and merge it into the map. |
| 61 const base::Time kNewUpdateTime1 = | 53 contact1->set_update_time(contact1->update_time() + 20); |
| 62 kUpdateTime2 + base::TimeDelta::FromSeconds(20); | |
| 63 contact1->set_update_time(kNewUpdateTime1.ToInternalValue()); | |
| 64 contacts_to_merge.reset(new ScopedVector<Contact>); | 54 contacts_to_merge.reset(new ScopedVector<Contact>); |
| 65 contacts_to_merge->push_back(new Contact(*contact1)); | 55 contacts_to_merge->push_back(new Contact(*contact1)); |
| 66 map.Merge(contacts_to_merge.Pass(), ContactMap::KEEP_DELETED_CONTACTS); | 56 map.Merge(contacts_to_merge.Pass(), ContactMap::KEEP_DELETED_CONTACTS); |
| 67 EXPECT_EQ(ContactMapToString(map), | 57 EXPECT_EQ(ContactMapToString(map), |
| 68 VarContactsToString(2, contact1.get(), contact2.get())); | 58 VarContactsToString(2, contact1.get(), contact2.get())); |
| 69 EXPECT_EQ(kNewUpdateTime1, map.GetMaxUpdateTime()); | |
| 70 | 59 |
| 71 // Create another deleted contact. | 60 // Create another deleted contact. |
| 72 const std::string kContactId3 = "contact_id_3"; | 61 const std::string kContactId3 = "contact_id_3"; |
| 73 scoped_ptr<Contact> contact3(new Contact); | 62 scoped_ptr<Contact> contact3(new Contact); |
| 74 InitContact(kContactId3, "3", true, contact3.get()); | 63 InitContact(kContactId3, "3", true, contact3.get()); |
| 75 const base::Time kUpdateTime3 = | |
| 76 kNewUpdateTime1 + base::TimeDelta::FromSeconds(40); | |
| 77 contact3->set_update_time(kUpdateTime3.ToInternalValue()); | |
| 78 | 64 |
| 79 // Merge it into the map with DROP_DELETED_CONTACTS. The contact shouldn't be | 65 // Merge it into the map with DROP_DELETED_CONTACTS. The contact shouldn't be |
| 80 // saved. | 66 // saved. |
| 81 contacts_to_merge.reset(new ScopedVector<Contact>); | 67 contacts_to_merge.reset(new ScopedVector<Contact>); |
| 82 contacts_to_merge->push_back(new Contact(*contact3)); | 68 contacts_to_merge->push_back(new Contact(*contact3)); |
| 83 map.Merge(contacts_to_merge.Pass(), ContactMap::DROP_DELETED_CONTACTS); | 69 map.Merge(contacts_to_merge.Pass(), ContactMap::DROP_DELETED_CONTACTS); |
| 84 EXPECT_EQ(ContactMapToString(map), | 70 EXPECT_EQ(ContactMapToString(map), |
| 85 VarContactsToString(2, contact1.get(), contact2.get())); | 71 VarContactsToString(2, contact1.get(), contact2.get())); |
| 86 EXPECT_EQ(kNewUpdateTime1, map.GetMaxUpdateTime()); | |
| 87 | 72 |
| 88 // Mark the first contact as being deleted and merge it with | 73 // Mark the first contact as being deleted and merge it with |
| 89 // DROP_DELETED_CONTACTS. The previous version of the contact should also be | 74 // DROP_DELETED_CONTACTS. The previous version of the contact should also be |
| 90 // removed. | 75 // removed. |
| 91 contact1->set_deleted(true); | 76 contact1->set_deleted(true); |
| 92 contacts_to_merge.reset(new ScopedVector<Contact>); | 77 contacts_to_merge.reset(new ScopedVector<Contact>); |
| 93 contacts_to_merge->push_back(new Contact(*contact1)); | 78 contacts_to_merge->push_back(new Contact(*contact1)); |
| 94 map.Merge(contacts_to_merge.Pass(), ContactMap::DROP_DELETED_CONTACTS); | 79 map.Merge(contacts_to_merge.Pass(), ContactMap::DROP_DELETED_CONTACTS); |
| 95 EXPECT_EQ(ContactMapToString(map), VarContactsToString(1, contact2.get())); | 80 EXPECT_EQ(ContactMapToString(map), VarContactsToString(1, contact2.get())); |
| 96 EXPECT_EQ(kUpdateTime2, map.GetMaxUpdateTime()); | |
| 97 | 81 |
| 98 map.Clear(); | 82 map.Clear(); |
| 99 EXPECT_TRUE(map.empty()); | 83 EXPECT_TRUE(map.empty()); |
| 100 EXPECT_EQ(0U, map.size()); | 84 EXPECT_EQ(0U, map.size()); |
| 101 EXPECT_EQ(base::Time(), map.GetMaxUpdateTime()); | 85 } |
| 86 |
| 87 TEST(ContactMapTest, Erase) { |
| 88 ContactMap map; |
| 89 const std::string kContactId = "contact_id"; |
| 90 scoped_ptr<Contact> contact(new Contact); |
| 91 InitContact(kContactId, "1", false, contact.get()); |
| 92 |
| 93 scoped_ptr<ScopedVector<Contact> > contacts_to_merge( |
| 94 new ScopedVector<Contact>); |
| 95 contacts_to_merge->push_back(new Contact(*contact)); |
| 96 map.Merge(contacts_to_merge.Pass(), ContactMap::KEEP_DELETED_CONTACTS); |
| 97 EXPECT_TRUE(map.Find(kContactId)); |
| 98 |
| 99 map.Erase(kContactId); |
| 100 EXPECT_FALSE(map.Find(kContactId)); |
| 101 EXPECT_TRUE(map.empty()); |
| 102 } | 102 } |
| 103 | 103 |
| 104 } // namespace test | 104 } // namespace test |
| 105 } // namespace contacts | 105 } // namespace contacts |
| OLD | NEW |