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_manager_stub.h"
6
7#include "base/logging.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/chromeos/contacts/contact.pb.h"
10#include "chrome/browser/chromeos/contacts/contact_manager_observer.h"
11#include "chrome/browser/chromeos/contacts/contact_test_util.h"
12#include "chrome/browser/profiles/profile.h"
13#include "content/public/browser/browser_thread.h"
14
15using content::BrowserThread;
16
17namespace contacts {
18
19ContactManagerStub::ContactManagerStub(Profile* profile)
20    : profile_(profile),
21      weak_ptr_factory_(this) {
22}
23
24ContactManagerStub::~ContactManagerStub() {}
25
26void ContactManagerStub::NotifyObserversAboutUpdatedContacts() {
27  FOR_EACH_OBSERVER(ContactManagerObserver,
28                    observers_,
29                    OnContactsUpdated(profile_));
30}
31
32void ContactManagerStub::SetContacts(const ContactPointers& contacts) {
33  test::CopyContacts(contacts, &contacts_);
34}
35
36base::WeakPtr<ContactManagerInterface> ContactManagerStub::GetWeakPtr() {
37  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
38  return weak_ptr_factory_.GetWeakPtr();
39}
40
41void ContactManagerStub::AddObserver(ContactManagerObserver* observer,
42                                     Profile* profile) {
43  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
44  CHECK(observer);
45  CHECK_EQ(profile, profile_);
46  CHECK(!observers_.HasObserver(observer));
47  observers_.AddObserver(observer);
48}
49
50void ContactManagerStub::RemoveObserver(ContactManagerObserver* observer,
51                                        Profile* profile) {
52  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
53  CHECK(observer);
54  CHECK_EQ(profile, profile_);
55  CHECK(observers_.HasObserver(observer));
56  observers_.RemoveObserver(observer);
57}
58
59scoped_ptr<ContactPointers> ContactManagerStub::GetAllContacts(
60    Profile* profile) {
61  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
62  CHECK_EQ(profile, profile_);
63  scoped_ptr<ContactPointers> contacts(new ContactPointers);
64  for (size_t i = 0; i < contacts_.size(); ++i)
65    contacts->push_back(contacts_[i]);
66  return contacts.Pass();
67}
68
69const Contact* ContactManagerStub::GetContactById(
70    Profile* profile,
71    const std::string& contact_id) {
72  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
73  CHECK_EQ(profile, profile_);
74  for (size_t i = 0; i < contacts_.size(); ++i) {
75    if (contacts_[i]->contact_id() == contact_id)
76      return contacts_[i];
77  }
78  return NULL;
79}
80
81}  // namespace contacts
82