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 #ifndef CHROME_BROWSER_CHROMEOS_CONTACTS_FAKE_CONTACT_STORE_H_ 6 #define CHROME_BROWSER_CHROMEOS_CONTACTS_FAKE_CONTACT_STORE_H_ 7 8 #include "chrome/browser/chromeos/contacts/contact_store.h" 9 10 #include <map> 11 #include <string> 12 #include <vector> 13 14 #include "base/basictypes.h" 15 #include "base/compiler_specific.h" 16 #include "base/observer_list.h" 17 #include "base/stl_util.h" 18 19 class Profile; 20 21 namespace contacts { 22 23 class Contact; 24 class FakeContactStoreFactory; 25 typedef std::vector<const Contact*> ContactPointers; 26 27 // A "fake" in-memory implementation of ContactStore used for testing. 28 class FakeContactStore : public ContactStore { 29 public: 30 explicit FakeContactStore(FakeContactStoreFactory* factory); 31 virtual ~FakeContactStore(); 32 33 // Makes an internal copy of |contacts| so they can be returned by 34 // AppendContacts() and GetContactById(). 35 void SetContacts(const ContactPointers& contacts); 36 37 // Invokes observers' OnContactsUpdated() methods. 38 void NotifyObserversAboutContactsUpdate(); 39 40 // ContactStore implementation: 41 virtual void Init() OVERRIDE; 42 virtual void AppendContacts(ContactPointers* contacts_out) OVERRIDE; 43 virtual const Contact* GetContactById(const std::string& contact_id) OVERRIDE; 44 virtual void AddObserver(ContactStoreObserver* observer) OVERRIDE; 45 virtual void RemoveObserver(ContactStoreObserver* observer) OVERRIDE; 46 47 private: 48 // Map from a contact's ID to the contact itself. 49 typedef std::map<std::string, Contact*> ContactMap; 50 51 // Factory that created this store. Not owned. 52 FakeContactStoreFactory* factory_; 53 54 ObserverList<ContactStoreObserver> observers_; 55 56 // Owns the pointed-to Contact values. 57 ContactMap contacts_; 58 59 // Deletes values in |contacts_|. 60 STLValueDeleter<ContactMap> contacts_deleter_; 61 62 DISALLOW_COPY_AND_ASSIGN(FakeContactStore); 63 }; 64 65 // ContactStoreFactory implementation that returns FakeContactStores. 66 class FakeContactStoreFactory : public ContactStoreFactory { 67 public: 68 FakeContactStoreFactory(); 69 virtual ~FakeContactStoreFactory(); 70 71 void set_permit_store_creation(bool permit) { 72 permit_store_creation_ = permit; 73 } 74 75 // Returns the FakeContactStore previously created for |profile|, or NULL if 76 // no store has been created for it. 77 FakeContactStore* GetContactStoreForProfile(Profile* profile); 78 79 // Removes |store| from |stores_| after being called by a FakeContactStore's 80 // d'tor. 81 void RemoveStore(FakeContactStore* store); 82 83 // ContactStoreFactory implementation: 84 virtual bool CanCreateContactStoreForProfile(Profile* profile) OVERRIDE; 85 virtual ContactStore* CreateContactStore(Profile* profile) OVERRIDE; 86 87 private: 88 typedef std::map<Profile*, FakeContactStore*> ProfileStoreMap; 89 90 // Live FakeContactStore objects that we've handed out. We don't retain 91 // ownership of these, but we hang on to the pointers so that tests can 92 // manipulate the stores while they're in use. 93 ProfileStoreMap stores_; 94 95 // Should CanCreateContactStoreForProfile() return true? 96 bool permit_store_creation_; 97 98 DISALLOW_COPY_AND_ASSIGN(FakeContactStoreFactory); 99 }; 100 101 } // namespace contacts 102 103 #endif // CHROME_BROWSER_CHROMEOS_CONTACTS_FAKE_CONTACT_STORE_H_ 104