Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/contacts/fake_contact_database.h" | 5 #include "chrome/browser/chromeos/contacts/fake_contact_database.h" |
| 6 | 6 |
| 7 #include "chrome/browser/chromeos/contacts/contact.pb.h" | 7 #include "chrome/browser/chromeos/contacts/contact.pb.h" |
| 8 #include "chrome/browser/chromeos/contacts/contact_test_util.h" | 8 #include "chrome/browser/chromeos/contacts/contact_test_util.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 | 10 |
| 11 using content::BrowserThread; | 11 using content::BrowserThread; |
| 12 | 12 |
| 13 namespace contacts { | 13 namespace contacts { |
| 14 | 14 |
| 15 FakeContactDatabase::FakeContactDatabase() | 15 FakeContactDatabase::FakeContactDatabase() |
| 16 : init_success_(true), | 16 : init_success_(true), |
| 17 save_success_(true), | 17 save_success_(true), |
| 18 load_success_(true) { | 18 load_success_(true), |
| 19 num_saved_contacts_(0) { | |
| 19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 20 } | 21 } |
| 21 | 22 |
| 22 void FakeContactDatabase::Init(const FilePath& database_dir, | 23 void FakeContactDatabase::Init(const FilePath& database_dir, |
| 23 InitCallback callback) { | 24 InitCallback callback) { |
| 24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 25 callback.Run(init_success_); | 26 callback.Run(init_success_); |
| 26 } | 27 } |
| 27 | 28 |
| 28 void FakeContactDatabase::SetContacts(const ContactPointers& contacts, | 29 void FakeContactDatabase::SetContacts(const ContactPointers& contacts, |
| 29 const UpdateMetadata& metadata) { | 30 const UpdateMetadata& metadata) { |
| 30 test::CopyContacts(contacts, &contacts_); | 31 test::CopyContacts(contacts, &contacts_); |
| 31 metadata_ = metadata; | 32 metadata_ = metadata; |
| 32 } | 33 } |
| 33 | 34 |
| 34 void FakeContactDatabase::DestroyOnUIThread() { | 35 void FakeContactDatabase::DestroyOnUIThread() { |
| 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 36 delete this; | 37 delete this; |
| 37 } | 38 } |
| 38 | 39 |
| 39 void FakeContactDatabase::SaveContacts(scoped_ptr<ContactPointers> contacts, | 40 void FakeContactDatabase::SaveContacts(scoped_ptr<ContactPointers> contacts, |
| 40 scoped_ptr<UpdateMetadata> metadata, | 41 scoped_ptr<UpdateMetadata> metadata, |
| 41 bool is_full_update, | 42 bool is_full_update, |
| 42 SaveCallback callback) { | 43 SaveCallback callback) { |
| 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 44 if (save_success_) { | 45 if (save_success_) { |
| 45 test::CopyContacts(*contacts, &contacts_); | 46 num_saved_contacts_ += contacts->size(); |
|
Daniel Erat
2012/08/03 20:00:23
This bug was hidden by the bug in GoogleContactSto
| |
| 47 if (is_full_update) | |
| 48 test::CopyContacts(*contacts, &contacts_); | |
| 49 else | |
| 50 MergeContacts(*contacts); | |
| 46 metadata_ = *metadata; | 51 metadata_ = *metadata; |
| 47 } | 52 } |
| 48 callback.Run(save_success_); | 53 callback.Run(save_success_); |
| 49 } | 54 } |
| 50 | 55 |
| 51 void FakeContactDatabase::LoadContacts(LoadCallback callback) { | 56 void FakeContactDatabase::LoadContacts(LoadCallback callback) { |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 53 scoped_ptr<ScopedVector<Contact> > contacts(new ScopedVector<Contact>()); | 58 scoped_ptr<ScopedVector<Contact> > contacts(new ScopedVector<Contact>()); |
| 54 scoped_ptr<UpdateMetadata> metadata(new UpdateMetadata); | 59 scoped_ptr<UpdateMetadata> metadata(new UpdateMetadata); |
| 55 if (load_success_) { | 60 if (load_success_) { |
| 56 test::CopyContacts(contacts_, contacts.get()); | 61 test::CopyContacts(contacts_, contacts.get()); |
| 57 *metadata = metadata_; | 62 *metadata = metadata_; |
| 58 } | 63 } |
| 59 callback.Run(load_success_, contacts.Pass(), metadata.Pass()); | 64 callback.Run(load_success_, contacts.Pass(), metadata.Pass()); |
| 60 } | 65 } |
| 61 | 66 |
| 62 FakeContactDatabase::~FakeContactDatabase() { | 67 FakeContactDatabase::~FakeContactDatabase() { |
| 63 } | 68 } |
| 64 | 69 |
| 70 void FakeContactDatabase::MergeContacts( | |
| 71 const ContactPointers& updated_contacts) { | |
| 72 for (ContactPointers::const_iterator updated_it = updated_contacts.begin(); | |
| 73 updated_it != updated_contacts.end(); ++updated_it) { | |
| 74 const Contact& updated_contact = **updated_it; | |
| 75 bool found = false; | |
| 76 for (ScopedVector<Contact>::const_iterator existing_it = contacts_.begin(); | |
|
Daniel Erat
2012/08/03 20:00:23
This is ugly and inefficient, but I'm not sure how
satorux1
2012/08/03 20:08:14
Shouldn't matter for tests
| |
| 77 existing_it != contacts_.end(); ++existing_it) { | |
| 78 Contact* existing_contact = *existing_it; | |
| 79 if (existing_contact->provider_id() == updated_contact.provider_id()) { | |
| 80 *existing_contact = updated_contact; | |
| 81 found = true; | |
| 82 break; | |
| 83 } | |
| 84 } | |
| 85 if (!found) | |
| 86 contacts_.push_back(new Contact(updated_contact)); | |
| 87 } | |
| 88 } | |
| 89 | |
| 65 } // namespace contacts | 90 } // namespace contacts |
| OLD | NEW |