Home | History | Annotate | Download | only in contacts
      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/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 TEST(ContactMapTest, Merge) {
     18   ContactMap map;
     19   EXPECT_TRUE(map.empty());
     20   EXPECT_EQ(0U, map.size());
     21 
     22   // Create a contact.
     23   const std::string kContactId1 = "contact_id_1";
     24   scoped_ptr<Contact> contact1(new Contact);
     25   InitContact(kContactId1, "1", false, contact1.get());
     26 
     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()));
     36 
     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());
     41 
     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()));
     51 
     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()));
     59 
     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());
     64 
     65   // Merge it into the map with DROP_DELETED_CONTACTS.  The contact shouldn't be
     66   // saved.
     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()));
     72 
     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
     75   // removed.
     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()));
     81 
     82   map.Clear();
     83   EXPECT_TRUE(map.empty());
     84   EXPECT_EQ(0U, map.size());
     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 }
    103 
    104 }  // namespace test
    105 }  // namespace contacts
    106