summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/contacts/fake_contact_store.h
blob: 43004682941e8020513455060f302a77363b00ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_CONTACTS_FAKE_CONTACT_STORE_H_
#define CHROME_BROWSER_CHROMEOS_CONTACTS_FAKE_CONTACT_STORE_H_

#include "chrome/browser/chromeos/contacts/contact_store.h"

#include <map>
#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/observer_list.h"
#include "base/stl_util.h"

class Profile;

namespace contacts {

class Contact;
class FakeContactStoreFactory;
typedef std::vector<const Contact*> ContactPointers;

// A "fake" in-memory implementation of ContactStore used for testing.
class FakeContactStore : public ContactStore {
 public:
  explicit FakeContactStore(FakeContactStoreFactory* factory);
  virtual ~FakeContactStore();

  // Makes an internal copy of |contacts| so they can be returned by
  // AppendContacts() and GetContactById().
  void SetContacts(const ContactPointers& contacts);

  // Invokes observers' OnContactsUpdated() methods.
  void NotifyObserversAboutContactsUpdate();

  // ContactStore implementation:
  virtual void Init() OVERRIDE;
  virtual void AppendContacts(ContactPointers* contacts_out) OVERRIDE;
  virtual const Contact* GetContactById(const std::string& contact_id) OVERRIDE;
  virtual void AddObserver(ContactStoreObserver* observer) OVERRIDE;
  virtual void RemoveObserver(ContactStoreObserver* observer) OVERRIDE;

 private:
  // Map from a contact's ID to the contact itself.
  typedef std::map<std::string, Contact*> ContactMap;

  // Factory that created this store.  Not owned.
  FakeContactStoreFactory* factory_;

  ObserverList<ContactStoreObserver> observers_;

  // Owns the pointed-to Contact values.
  ContactMap contacts_;

  // Deletes values in |contacts_|.
  STLValueDeleter<ContactMap> contacts_deleter_;

  DISALLOW_COPY_AND_ASSIGN(FakeContactStore);
};

// ContactStoreFactory implementation that returns FakeContactStores.
class FakeContactStoreFactory : public ContactStoreFactory {
 public:
  FakeContactStoreFactory();
  virtual ~FakeContactStoreFactory();

  void set_permit_store_creation(bool permit) {
    permit_store_creation_ = permit;
  }

  // Returns the FakeContactStore previously created for |profile|, or NULL if
  // no store has been created for it.
  FakeContactStore* GetContactStoreForProfile(Profile* profile);

  // Removes |store| from |stores_| after being called by a FakeContactStore's
  // d'tor.
  void RemoveStore(FakeContactStore* store);

  // ContactStoreFactory implementation:
  virtual bool CanCreateContactStoreForProfile(Profile* profile) OVERRIDE;
  virtual ContactStore* CreateContactStore(Profile* profile) OVERRIDE;

 private:
  typedef std::map<Profile*, FakeContactStore*> ProfileStoreMap;

  // Live FakeContactStore objects that we've handed out.  We don't retain
  // ownership of these, but we hang on to the pointers so that tests can
  // manipulate the stores while they're in use.
  ProfileStoreMap stores_;

  // Should CanCreateContactStoreForProfile() return true?
  bool permit_store_creation_;

  DISALLOW_COPY_AND_ASSIGN(FakeContactStoreFactory);
};

}  // namespace contacts

#endif  // CHROME_BROWSER_CHROMEOS_CONTACTS_FAKE_CONTACT_STORE_H_