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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/contacts/contact_map.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/scoped_vector.h"
10 #include "chrome/browser/chromeos/contacts/contact.pb.h"
11 #include "chrome/browser/chromeos/contacts/contact_test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
17 TEST(ContactMapTest
, Merge
) {
19 EXPECT_TRUE(map
.empty());
20 EXPECT_EQ(0U, map
.size());
23 const std::string kContactId1
= "contact_id_1";
24 scoped_ptr
<Contact
> contact1(new Contact
);
25 InitContact(kContactId1
, "1", false, contact1
.get());
27 // Merge it into the map and check that it's stored.
28 scoped_ptr
<ScopedVector
<Contact
> > contacts_to_merge(
29 new ScopedVector
<Contact
>);
30 contacts_to_merge
->push_back(new Contact(*contact1
));
31 map
.Merge(contacts_to_merge
.Pass(), ContactMap::KEEP_DELETED_CONTACTS
);
32 EXPECT_FALSE(map
.empty());
33 EXPECT_EQ(1U, map
.size());
34 ASSERT_TRUE(map
.Find(kContactId1
));
35 EXPECT_EQ(ContactMapToString(map
), VarContactsToString(1, contact1
.get()));
37 // Create a second, deleted contact.
38 const std::string kContactId2
= "contact_id_2";
39 scoped_ptr
<Contact
> contact2(new Contact
);
40 InitContact(kContactId2
, "2", true, contact2
.get());
42 // Merge it into the map. Since we request keeping deleted contacts, the
43 // contact should be saved.
44 contacts_to_merge
.reset(new ScopedVector
<Contact
>);
45 contacts_to_merge
->push_back(new Contact(*contact2
));
46 map
.Merge(contacts_to_merge
.Pass(), ContactMap::KEEP_DELETED_CONTACTS
);
47 EXPECT_EQ(2U, map
.size());
48 ASSERT_TRUE(map
.Find(kContactId2
));
49 EXPECT_EQ(ContactMapToString(map
),
50 VarContactsToString(2, contact1
.get(), contact2
.get()));
52 // Update the first contact's update time and merge it into the map.
53 contact1
->set_update_time(contact1
->update_time() + 20);
54 contacts_to_merge
.reset(new ScopedVector
<Contact
>);
55 contacts_to_merge
->push_back(new Contact(*contact1
));
56 map
.Merge(contacts_to_merge
.Pass(), ContactMap::KEEP_DELETED_CONTACTS
);
57 EXPECT_EQ(ContactMapToString(map
),
58 VarContactsToString(2, contact1
.get(), contact2
.get()));
60 // Create another deleted contact.
61 const std::string kContactId3
= "contact_id_3";
62 scoped_ptr
<Contact
> contact3(new Contact
);
63 InitContact(kContactId3
, "3", true, contact3
.get());
65 // Merge it into the map with DROP_DELETED_CONTACTS. The contact shouldn't be
67 contacts_to_merge
.reset(new ScopedVector
<Contact
>);
68 contacts_to_merge
->push_back(new Contact(*contact3
));
69 map
.Merge(contacts_to_merge
.Pass(), ContactMap::DROP_DELETED_CONTACTS
);
70 EXPECT_EQ(ContactMapToString(map
),
71 VarContactsToString(2, contact1
.get(), contact2
.get()));
73 // Mark the first contact as being deleted and merge it with
74 // DROP_DELETED_CONTACTS. The previous version of the contact should also be
76 contact1
->set_deleted(true);
77 contacts_to_merge
.reset(new ScopedVector
<Contact
>);
78 contacts_to_merge
->push_back(new Contact(*contact1
));
79 map
.Merge(contacts_to_merge
.Pass(), ContactMap::DROP_DELETED_CONTACTS
);
80 EXPECT_EQ(ContactMapToString(map
), VarContactsToString(1, contact2
.get()));
83 EXPECT_TRUE(map
.empty());
84 EXPECT_EQ(0U, map
.size());
87 TEST(ContactMapTest
, Erase
) {
89 const std::string kContactId
= "contact_id";
90 scoped_ptr
<Contact
> contact(new Contact
);
91 InitContact(kContactId
, "1", false, contact
.get());
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
));
99 map
.Erase(kContactId
);
100 EXPECT_FALSE(map
.Find(kContactId
));
101 EXPECT_TRUE(map
.empty());
105 } // namespace contacts