Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: chrome/browser/chromeos/contacts/contact_map_unittest.cc

Issue 10905033: contacts: Add ContactMap class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: apply review feedback Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/chromeos/contacts/contact_map.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/time.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"
13
14 namespace contacts {
15 namespace test {
16
17 typedef testing::Test ContactMapTest;
tfarina 2012/09/09 19:04:29 this is not necessary if you use TEST() macro!
Daniel Erat 2012/09/10 15:01:44 Thanks, done.
18
19 TEST_F(ContactMapTest, Merge) {
20 ContactMap map;
21 EXPECT_TRUE(map.empty());
22 EXPECT_EQ(0U, map.size());
23 EXPECT_EQ(base::Time(), map.GetMaxUpdateTime());
24
25 // Create a contact.
26 const std::string kContactId1 = "contact_id_1";
27 scoped_ptr<Contact> contact1(new Contact);
28 InitContact(kContactId1, "1", false, contact1.get());
29 const base::Time kUpdateTime1 = base::Time::FromInternalValue(100);
30 contact1->set_update_time(kUpdateTime1.ToInternalValue());
31
32 // Merge it into the map and check that it's stored.
33 scoped_ptr<ScopedVector<Contact> > contacts_to_merge(
34 new ScopedVector<Contact>);
35 contacts_to_merge->push_back(new Contact(*contact1));
36 map.Merge(contacts_to_merge.Pass(), ContactMap::KEEP_DELETED_CONTACTS);
37 EXPECT_FALSE(map.empty());
38 EXPECT_EQ(1U, map.size());
39 ASSERT_TRUE(map.Find(kContactId1));
40 EXPECT_EQ(ContactMapToString(map), VarContactsToString(1, contact1.get()));
41 EXPECT_EQ(kUpdateTime1, map.GetMaxUpdateTime());
42
43 // Create a second, deleted contact with a newer update time.
44 const std::string kContactId2 = "contact_id_2";
45 scoped_ptr<Contact> contact2(new Contact);
46 InitContact(kContactId2, "2", true, contact2.get());
47 const base::Time kUpdateTime2 =
48 kUpdateTime1 + base::TimeDelta::FromSeconds(10);
49 contact2->set_update_time(kUpdateTime2.ToInternalValue());
50
51 // Merge it into the map. Since we request keeping deleted contacts, the
52 // contact should be saved.
53 contacts_to_merge.reset(new ScopedVector<Contact>);
54 contacts_to_merge->push_back(new Contact(*contact2));
55 map.Merge(contacts_to_merge.Pass(), ContactMap::KEEP_DELETED_CONTACTS);
56 EXPECT_EQ(2U, map.size());
57 ASSERT_TRUE(map.Find(kContactId2));
58 EXPECT_EQ(ContactMapToString(map),
59 VarContactsToString(2, contact1.get(), contact2.get()));
60 EXPECT_EQ(kUpdateTime2, map.GetMaxUpdateTime());
61
62 // Update the first contact's update time and merge it into the map.
63 const base::Time kNewUpdateTime1 =
64 kUpdateTime2 + base::TimeDelta::FromSeconds(20);
65 contact1->set_update_time(kNewUpdateTime1.ToInternalValue());
66 contacts_to_merge.reset(new ScopedVector<Contact>);
67 contacts_to_merge->push_back(new Contact(*contact1));
68 map.Merge(contacts_to_merge.Pass(), ContactMap::KEEP_DELETED_CONTACTS);
69 EXPECT_EQ(ContactMapToString(map),
70 VarContactsToString(2, contact1.get(), contact2.get()));
71 EXPECT_EQ(kNewUpdateTime1, map.GetMaxUpdateTime());
72
73 // Create another deleted contact.
74 const std::string kContactId3 = "contact_id_3";
75 scoped_ptr<Contact> contact3(new Contact);
76 InitContact(kContactId3, "3", true, contact3.get());
77 const base::Time kUpdateTime3 =
78 kNewUpdateTime1 + base::TimeDelta::FromSeconds(40);
79 contact3->set_update_time(kUpdateTime3.ToInternalValue());
80
81 // Merge it into the map with DROP_DELETED_CONTACTS. The contact shouldn't be
82 // saved.
83 contacts_to_merge.reset(new ScopedVector<Contact>);
84 contacts_to_merge->push_back(new Contact(*contact3));
85 map.Merge(contacts_to_merge.Pass(), ContactMap::DROP_DELETED_CONTACTS);
86 EXPECT_EQ(ContactMapToString(map),
87 VarContactsToString(2, contact1.get(), contact2.get()));
88 EXPECT_EQ(kNewUpdateTime1, map.GetMaxUpdateTime());
89
90 // Mark the first contact as being deleted and merge it with
91 // DROP_DELETED_CONTACTS. The previous version of the contact should also be
92 // removed.
93 contact1->set_deleted(true);
94 contacts_to_merge.reset(new ScopedVector<Contact>);
95 contacts_to_merge->push_back(new Contact(*contact1));
96 map.Merge(contacts_to_merge.Pass(), ContactMap::DROP_DELETED_CONTACTS);
97 EXPECT_EQ(ContactMapToString(map), VarContactsToString(1, contact2.get()));
98 EXPECT_EQ(kUpdateTime2, map.GetMaxUpdateTime());
99
100 map.Clear();
101 EXPECT_TRUE(map.empty());
102 EXPECT_EQ(0U, map.size());
103 EXPECT_EQ(base::Time(), map.GetMaxUpdateTime());
104 }
105
106 } // namespace test
107 } // namespace contacts
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698