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 #ifndef CHROME_BROWSER_CHROMEOS_CONTACTS_FAKE_CONTACT_STORE_H_
6 #define CHROME_BROWSER_CHROMEOS_CONTACTS_FAKE_CONTACT_STORE_H_
8 #include "chrome/browser/chromeos/contacts/contact_store.h"
14 #include "base/basictypes.h"
15 #include "base/compiler_specific.h"
16 #include "base/observer_list.h"
17 #include "base/stl_util.h"
24 class FakeContactStoreFactory;
25 typedef std::vector<const Contact*> ContactPointers;
27 // A "fake" in-memory implementation of ContactStore used for testing.
28 class FakeContactStore : public ContactStore {
30 explicit FakeContactStore(FakeContactStoreFactory* factory);
31 virtual ~FakeContactStore();
33 // Makes an internal copy of |contacts| so they can be returned by
34 // AppendContacts() and GetContactById().
35 void SetContacts(const ContactPointers& contacts);
37 // Invokes observers' OnContactsUpdated() methods.
38 void NotifyObserversAboutContactsUpdate();
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;
48 // Map from a contact's ID to the contact itself.
49 typedef std::map<std::string, Contact*> ContactMap;
51 // Factory that created this store. Not owned.
52 FakeContactStoreFactory* factory_;
54 ObserverList<ContactStoreObserver> observers_;
56 // Owns the pointed-to Contact values.
59 // Deletes values in |contacts_|.
60 STLValueDeleter<ContactMap> contacts_deleter_;
62 DISALLOW_COPY_AND_ASSIGN(FakeContactStore);
65 // ContactStoreFactory implementation that returns FakeContactStores.
66 class FakeContactStoreFactory : public ContactStoreFactory {
68 FakeContactStoreFactory();
69 virtual ~FakeContactStoreFactory();
71 void set_permit_store_creation(bool permit) {
72 permit_store_creation_ = permit;
75 // Returns the FakeContactStore previously created for |profile|, or NULL if
76 // no store has been created for it.
77 FakeContactStore* GetContactStoreForProfile(Profile* profile);
79 // Removes |store| from |stores_| after being called by a FakeContactStore's
81 void RemoveStore(FakeContactStore* store);
83 // ContactStoreFactory implementation:
84 virtual bool CanCreateContactStoreForProfile(Profile* profile) OVERRIDE;
85 virtual ContactStore* CreateContactStore(Profile* profile) OVERRIDE;
88 typedef std::map<Profile*, FakeContactStore*> ProfileStoreMap;
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_;
95 // Should CanCreateContactStoreForProfile() return true?
96 bool permit_store_creation_;
98 DISALLOW_COPY_AND_ASSIGN(FakeContactStoreFactory);
101 } // namespace contacts
103 #endif // CHROME_BROWSER_CHROMEOS_CONTACTS_FAKE_CONTACT_STORE_H_