Commit a9b484f3 authored by derat@chromium.org's avatar derat@chromium.org

contacts: Add Contact struct and test functions.

This adds a (currently-Chrome-OS-only) Contact struct,
loosely patterned on Google's GData Contact kind v2.0.

It also adds test functions related to this struct.

BUG=128805
TEST=tested in conjunction with later code


Review URL: https://chromiumcodereview.appspot.com/10803045

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@147960 0039d316-1c4b-4281-b951-d872f2087c98
parent 41538626
// 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.
#include "chrome/browser/chromeos/contacts/contact.h"
namespace contacts {
Contact::AddressType::AddressType() : relation(RELATION_OTHER) {}
Contact::EmailAddress::EmailAddress() : primary(false) {}
Contact::PhoneNumber::PhoneNumber() : primary(false) {}
Contact::PostalAddress::PostalAddress() : primary(false) {}
Contact::InstantMessagingAddress::InstantMessagingAddress()
: protocol(PROTOCOL_OTHER),
primary(false) {}
Contact::Contact() : deleted(false) {}
Contact::~Contact() {}
} // namespace contacts
// 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_CONTACT_H_
#define CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_H_
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/time.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace contacts {
// Struct representing a contact, roughly based on the GData Contact kind:
// https://developers.google.com/gdata/docs/2.0/elements#gdContactKind
// All strings are UTF-8.
struct Contact {
// Describes an address-like field's type.
struct AddressType {
enum Relation {
RELATION_HOME = 0,
RELATION_WORK = 1,
RELATION_MOBILE = 2,
RELATION_OTHER = 3,
};
AddressType();
Relation relation;
std::string label;
};
struct EmailAddress {
EmailAddress();
std::string address;
AddressType type;
bool primary;
};
struct PhoneNumber {
PhoneNumber();
std::string number;
AddressType type;
bool primary;
};
struct PostalAddress {
PostalAddress();
std::string address;
AddressType type;
bool primary;
};
struct InstantMessagingAddress {
// Taken from https://developers.google.com/gdata/docs/2.0/elements#gdIm.
enum Protocol {
PROTOCOL_AIM = 0,
PROTOCOL_MSN = 1,
PROTOCOL_YAHOO = 2,
PROTOCOL_SKYPE = 3,
PROTOCOL_QQ = 4,
PROTOCOL_GOOGLE_TALK = 5,
PROTOCOL_ICQ = 6,
PROTOCOL_JABBER = 7,
PROTOCOL_OTHER = 8,
};
InstantMessagingAddress();
std::string address;
Protocol protocol;
AddressType type;
bool primary;
};
Contact();
~Contact();
int64 serialized_update_time() const {
return update_time.ToInternalValue();
}
void set_serialized_update_time(int64 serialized) {
update_time = base::Time::FromInternalValue(serialized);
}
// NOTE: Any changes to the below fields must be reflected in
// contact_test_util.cc's CopyContact() function.
// Provider-assigned unique identifier.
std::string provider_id;
// Last time at which this contact was updated.
base::Time update_time;
// Has the contact been deleted?
bool deleted;
// Taken from https://developers.google.com/gdata/docs/2.0/elements#gdName.
std::string full_name;
std::string given_name;
std::string additional_name;
std::string family_name;
std::string name_prefix;
std::string name_suffix;
SkBitmap photo;
std::vector<EmailAddress> email_addresses;
std::vector<PhoneNumber> phone_numbers;
std::vector<PostalAddress> postal_addresses;
std::vector<InstantMessagingAddress> instant_messaging_addresses;
DISALLOW_COPY_AND_ASSIGN(Contact);
};
typedef std::vector<const Contact*> ContactPointers;
} // namespace contacts
#endif // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_H_
// 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.
#include "chrome/browser/chromeos/contacts/contact_test_util.h"
#include <algorithm>
#include <vector>
#include "base/bind.h"
#include "base/logging.h"
#include "base/string_number_conversions.h"
#include "base/string_util.h"
#include "base/time.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/size.h"
namespace contacts {
namespace test {
namespace {
// Invokes |stringify_callback| on each item in |items| and prepends |prefix|,
// and then sorts the resulting strings and joins them using |join_char|.
template<class T>
std::string StringifyField(
const std::vector<T>& items,
base::Callback<std::string(const T&)> stringify_callback,
const std::string& prefix,
char join_char) {
std::vector<std::string> strings;
for (size_t i = 0; i < items.size(); ++i)
strings.push_back(prefix + stringify_callback.Run(items[i]));
std::sort(strings.begin(), strings.end());
return JoinString(strings, join_char);
}
std::string EmailAddressToString(const Contact::EmailAddress& email) {
return email.address + "," +
base::IntToString(email.type.relation) + "," +
email.type.label + "," +
base::IntToString(email.primary);
}
std::string PhoneNumberToString(const Contact::PhoneNumber& phone) {
return phone.number + "," +
base::IntToString(phone.type.relation) + "," +
phone.type.label + "," +
base::IntToString(phone.primary);
}
std::string PostalAddressToString(const Contact::PostalAddress& postal) {
return postal.address + "," +
base::IntToString(postal.type.relation) + "," +
postal.type.label + "," +
base::IntToString(postal.primary);
}
std::string InstantMessagingAddressToString(
const Contact::InstantMessagingAddress& im) {
return im.address + "," +
base::IntToString(im.protocol) + "," +
base::IntToString(im.type.relation) + "," +
im.type.label + "," +
base::IntToString(im.primary);
}
} // namespace
std::string ContactToString(const Contact& contact) {
std::string result =
contact.provider_id + "," +
base::Int64ToString(contact.update_time.ToInternalValue()) + "," +
base::IntToString(contact.deleted) + "," +
contact.full_name + "," +
contact.given_name + "," +
contact.additional_name + "," +
contact.family_name + "," +
contact.name_prefix + "," +
contact.name_suffix + "," +
base::IntToString(contact.photo.width()) + "x" +
base::IntToString(contact.photo.height());
result += " " + StringifyField(contact.email_addresses,
base::Bind(EmailAddressToString),
"email=", ' ');
result += " " + StringifyField(contact.phone_numbers,
base::Bind(PhoneNumberToString),
"phone=", ' ');
result += " " + StringifyField(contact.postal_addresses,
base::Bind(PostalAddressToString),
"postal=", ' ');
result += " " + StringifyField(contact.instant_messaging_addresses,
base::Bind(InstantMessagingAddressToString),
"im=", ' ');
return result;
}
std::string ContactsToString(const ContactPointers& contacts) {
std::vector<std::string> contact_strings;
for (size_t i = 0; i < contacts.size(); ++i)
contact_strings.push_back(ContactToString(*contacts[i]));
std::sort(contact_strings.begin(), contact_strings.end());
return JoinString(contact_strings, '\n');
}
std::string ContactsToString(const ScopedVector<Contact>& contacts) {
ContactPointers pointers;
for (size_t i = 0; i < contacts.size(); ++i)
pointers.push_back(contacts[i]);
return ContactsToString(pointers);
}
std::string VarContactsToString(int num_contacts, ...) {
ContactPointers contacts;
va_list list;
va_start(list, num_contacts);
for (int i = 0; i < num_contacts; ++i)
contacts.push_back(va_arg(list, const Contact*));
va_end(list);
return ContactsToString(contacts);
}
void CopyContact(const Contact& source, Contact* dest) {
dest->provider_id = source.provider_id;
dest->update_time = source.update_time;
dest->deleted = source.deleted;
dest->full_name = source.full_name;
dest->given_name = source.given_name;
dest->additional_name = source.additional_name;
dest->family_name = source.family_name;
dest->name_prefix = source.name_prefix;
dest->name_suffix = source.name_suffix;
dest->photo = source.photo;
dest->email_addresses = source.email_addresses;
dest->phone_numbers = source.phone_numbers;
dest->postal_addresses = source.postal_addresses;
dest->instant_messaging_addresses = source.instant_messaging_addresses;
}
void CopyContacts(const ContactPointers& source,
ScopedVector<Contact>* dest) {
DCHECK(dest);
dest->clear();
for (size_t i = 0; i < source.size(); ++i) {
Contact* contact = new Contact;
CopyContact(*source[i], contact);
dest->push_back(contact);
}
}
void CopyContacts(const ScopedVector<Contact>& source,
ScopedVector<Contact>* dest) {
ContactPointers pointers;
for (size_t i = 0; i < source.size(); ++i)
pointers.push_back(source[i]);
CopyContacts(pointers, dest);
}
void InitContact(const std::string& provider_id,
const std::string& name_suffix,
bool deleted,
Contact* contact) {
DCHECK(contact);
contact->provider_id = provider_id;
contact->update_time = base::Time::Now();
contact->deleted = deleted;
contact->full_name = "full_name_" + name_suffix;
contact->given_name = "given_name_" + name_suffix;
contact->additional_name = "additional_name_" + name_suffix;
contact->family_name = "family_name_" + name_suffix;
contact->name_prefix = "name_prefix_" + name_suffix;
contact->name_suffix = "name_suffix_" + name_suffix;
contact->photo = SkBitmap();
contact->email_addresses.clear();
contact->phone_numbers.clear();
contact->postal_addresses.clear();
contact->instant_messaging_addresses.clear();
}
void AddEmailAddress(const std::string& address,
Contact::AddressType::Relation relation,
const std::string& label,
bool primary,
Contact* contact) {
DCHECK(contact);
Contact::EmailAddress email;
email.address = address;
email.type.relation = relation;
email.type.label = label;
email.primary = primary;
contact->email_addresses.push_back(email);
}
void AddPhoneNumber(const std::string& number,
Contact::AddressType::Relation relation,
const std::string& label,
bool primary,
Contact* contact) {
DCHECK(contact);
Contact::PhoneNumber phone;
phone.number = number;
phone.type.relation = relation;
phone.type.label = label;
phone.primary = primary;
contact->phone_numbers.push_back(phone);
}
void AddPostalAddress(const std::string& address,
Contact::AddressType::Relation relation,
const std::string& label,
bool primary,
Contact* contact) {
DCHECK(contact);
Contact::PostalAddress postal;
postal.address = address;
postal.type.relation = relation;
postal.type.label = label;
postal.primary = primary;
contact->postal_addresses.push_back(postal);
}
void AddInstantMessagingAddress(
const std::string& address,
Contact::InstantMessagingAddress::Protocol protocol,
Contact::AddressType::Relation relation,
const std::string& label,
bool primary,
Contact* contact) {
DCHECK(contact);
Contact::InstantMessagingAddress im;
im.address = address;
im.protocol = protocol;
im.type.relation = relation;
im.type.label = label;
im.primary = primary;
contact->instant_messaging_addresses.push_back(im);
}
void SetPhoto(const gfx::Size& size, Contact* contact) {
DCHECK(contact);
contact->photo.setConfig(
SkBitmap::kARGB_8888_Config, size.width(), size.height());
contact->photo.allocPixels();
}
} // namespace test
} // namespace contacts
// 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_CONTACT_TEST_UTIL_H_
#define CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_TEST_UTIL_H_
#include <string>
#include "base/memory/scoped_vector.h"
#include "chrome/browser/chromeos/contacts/contact.h"
#include "ui/gfx/size.h"
namespace contacts {
namespace test {
// Returns a string containing the information stored in |contact|. The same
// string will be returned for functionally-equivalent contacts (e.g. ones
// containing the same email addresses but in a different order).
std::string ContactToString(const Contact& contact);
// Runs ContactToString() on each entry in |contacts| and returns the results
// joined by newlines (in a consistent order).
std::string ContactsToString(const ContactPointers& contacts);
std::string ContactsToString(const ScopedVector<Contact>& contacts);
// Convenience wrapper for ContactsToString(). Takes |num_contacts|
// const Contact* arguments.
std::string VarContactsToString(int num_contacts, ...);
// Copies |source|'s data to |dest|.
void CopyContact(const Contact& source, Contact* dest);
// Saves copies of all contacts in |source| to |dest|.
void CopyContacts(const ContactPointers& source,
ScopedVector<Contact>* dest);
void CopyContacts(const ScopedVector<Contact>& source,
ScopedVector<Contact>* dest);
// Initializes |contact| with the passed-in data. The photo and all address
// fields are cleared. |provider_id| corresponds to Contact::provider_id,
// |deleted| to Contact::deleted, and a unique string should be passed to
// |name_suffix| to make the name-related fields be distinct from those in other
// contacts.
void InitContact(const std::string& provider_id,
const std::string& name_suffix,
bool deleted,
Contact* contact);
// Adds an email address to |contact|.
void AddEmailAddress(const std::string& address,
Contact::AddressType::Relation relation,
const std::string& label,
bool primary,
Contact* contact);
// Adds a phone number to |contact|.
void AddPhoneNumber(const std::string& number,
Contact::AddressType::Relation relation,
const std::string& label,
bool primary,
Contact* contact);
// Adds a postal address to |contact|.
void AddPostalAddress(const std::string& address,
Contact::AddressType::Relation relation,
const std::string& label,
bool primary,
Contact* contact);
// Adds an IM address to |contact|.
void AddInstantMessagingAddress(
const std::string& address,
Contact::InstantMessagingAddress::Protocol protocol,
Contact::AddressType::Relation relation,
const std::string& label,
bool primary,
Contact* contact);
// Initializes |contact|'s photo to a bitmap of the given size.
// ContactToString() includes the photo's dimensions in its output, so tests can
// call this method to set the photo to a given size and then check that the
// size matches later (e.g. after loading the contact from a server or from
// disk) to confirm that the photo was loaded correctly.
void SetPhoto(const gfx::Size& size, Contact* contact);
} // namespace test
} // namespace contacts
#endif // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_TEST_UTIL_H_
......@@ -449,6 +449,8 @@
'browser/chromeos/choose_mobile_network_dialog.h',
'browser/chromeos/chrome_browser_main_chromeos.cc',
'browser/chromeos/chrome_browser_main_chromeos.h',
'browser/chromeos/contacts/contact.cc',
'browser/chromeos/contacts/contact.h',
'browser/chromeos/cros/burn_library.cc',
'browser/chromeos/cros/burn_library.h',
'browser/chromeos/cros/cellular_data_plan.cc',
......
......@@ -1087,6 +1087,8 @@
'browser/chrome_page_zoom_unittest.cc',
'browser/chromeos/bluetooth/bluetooth_service_record_unittest.cc',
'browser/chromeos/bluetooth/bluetooth_utils_unittest.cc',
'browser/chromeos/contacts/contact_test_util.cc',
'browser/chromeos/contacts/contact_test_util.h',
'browser/chromeos/cros/cros_network_functions_unittest.cc',
'browser/chromeos/cros/network_constants.h',
'browser/chromeos/cros/network_library.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment