Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: chrome/browser/chromeos/contacts/contact_test_lib.cc

Issue 10803045: contacts: Add Contact struct and test functions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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_test_lib.h"
6
7 #include <algorithm>
8 #include <vector>
9
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/string_number_conversions.h"
13 #include "base/string_util.h"
14 #include "base/time.h"
15 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "ui/gfx/size.h"
17
18 namespace contacts {
19 namespace test {
20
21 namespace {
22
23 // Invokes |stringify_callback| on each item in |items| and prepends |prefix|,
24 // and then sorts the resulting strings and joins them using |join_char|.
25 template<class T>
26 std::string StringifyField(
27 const std::vector<T>& items,
28 base::Callback<std::string(const T&)> stringify_callback,
satorux1 2012/07/20 17:42:57 oh this is a cool use of callback!
29 const std::string& prefix,
30 char join_char) {
31 std::vector<std::string> strings;
32 for (size_t i = 0; i < items.size(); ++i)
33 strings.push_back(prefix + stringify_callback.Run(items[i]));
34 std::sort(strings.begin(), strings.end());
35 return JoinString(strings, join_char);
36 }
37
38 std::string EmailAddressToString(const Contact::EmailAddress& email) {
39 return email.address + "," +
40 base::IntToString(email.type.relation) + "," +
41 email.type.label + "," +
42 base::IntToString(email.primary);
43 }
44
45 std::string PhoneNumberToString(const Contact::PhoneNumber& phone) {
46 return phone.number + "," +
47 base::IntToString(phone.type.relation) + "," +
48 phone.type.label + "," +
49 base::IntToString(phone.primary);
50 }
51
52 std::string PostalAddressToString(const Contact::PostalAddress& postal) {
53 return postal.address + "," +
54 base::IntToString(postal.type.relation) + "," +
55 postal.type.label + "," +
56 base::IntToString(postal.primary);
57 }
58
59 std::string InstantMessagingAddressToString(
60 const Contact::InstantMessagingAddress& im) {
61 return im.address + "," +
62 base::IntToString(im.protocol) + "," +
63 base::IntToString(im.type.relation) + "," +
64 im.type.label + "," +
65 base::IntToString(im.primary);
66 }
67
68 } // namespace
69
70 std::string ContactToString(const Contact& contact) {
71 std::string result =
72 contact.provider_id + "," +
73 base::Int64ToString(contact.update_time.ToInternalValue()) + "," +
74 base::IntToString(contact.deleted) + "," +
75 contact.full_name + "," +
76 contact.given_name + "," +
77 contact.additional_name + "," +
78 contact.family_name + "," +
79 contact.name_prefix + "," +
80 contact.name_suffix + "," +
81 base::IntToString(contact.photo.width()) + "x" +
82 base::IntToString(contact.photo.height());
83
84 result += " " + StringifyField(contact.email_addresses,
85 base::Bind(EmailAddressToString),
86 "email=", ' ');
87 result += " " + StringifyField(contact.phone_numbers,
88 base::Bind(PhoneNumberToString),
89 "phone=", ' ');
90 result += " " + StringifyField(contact.postal_addresses,
91 base::Bind(PostalAddressToString),
92 "postal=", ' ');
93 result += " " + StringifyField(contact.instant_messaging_addresses,
94 base::Bind(InstantMessagingAddressToString),
95 "im=", ' ');
96
97 return result;
98 }
99
100 std::string ContactsToString(const ContactPointers& contacts) {
101 std::vector<std::string> contact_strings;
102 for (size_t i = 0; i < contacts.size(); ++i)
103 contact_strings.push_back(ContactToString(*contacts[i]));
104 std::sort(contact_strings.begin(), contact_strings.end());
105 return JoinString(contact_strings, '\n');
106 }
107
108 std::string ContactsToString(const ScopedVector<Contact>& contacts) {
109 ContactPointers pointers;
110 for (size_t i = 0; i < contacts.size(); ++i)
111 pointers.push_back(contacts[i]);
112 return ContactsToString(pointers);
113 }
114
115 std::string VarContactsToString(int num_contacts, ...) {
116 ContactPointers contacts;
117 va_list list;
118 va_start(list, num_contacts);
119 for (int i = 0; i < num_contacts; ++i)
120 contacts.push_back(va_arg(list, const Contact*));
121 va_end(list);
122 return ContactsToString(contacts);
123 }
124
125 void CopyContact(const Contact& source, Contact* dest) {
126 dest->provider_id = source.provider_id;
127 dest->update_time = source.update_time;
128 dest->deleted = source.deleted;
129 dest->full_name = source.full_name;
130 dest->given_name = source.given_name;
131 dest->additional_name = source.additional_name;
132 dest->family_name = source.family_name;
133 dest->name_prefix = source.name_prefix;
134 dest->name_suffix = source.name_suffix;
135 dest->photo = source.photo;
136 dest->email_addresses = source.email_addresses;
137 dest->phone_numbers = source.phone_numbers;
138 dest->postal_addresses = source.postal_addresses;
139 dest->instant_messaging_addresses = source.instant_messaging_addresses;
140 }
141
142 void CopyContacts(const ContactPointers& source,
143 ScopedVector<Contact>* dest) {
144 DCHECK(dest);
145 dest->clear();
146 for (size_t i = 0; i < source.size(); ++i) {
147 Contact* contact = new Contact;
148 CopyContact(*source[i], contact);
149 dest->push_back(contact);
150 }
151 }
152
153 void CopyContacts(const ScopedVector<Contact>& source,
154 ScopedVector<Contact>* dest) {
155 ContactPointers pointers;
156 for (size_t i = 0; i < source.size(); ++i)
157 pointers.push_back(source[i]);
158 CopyContacts(pointers, dest);
159 }
160
161 void InitContact(const std::string& provider_id,
162 const std::string& field_suffix,
163 bool deleted,
164 Contact* contact) {
165 DCHECK(contact);
166 contact->provider_id = provider_id;
167 contact->update_time = base::Time::Now();
168 contact->deleted = deleted;
169 contact->full_name = "full_name_" + field_suffix;
170 contact->given_name = "given_name_" + field_suffix;
171 contact->additional_name = "additional_name_" + field_suffix;
172 contact->family_name = "family_name_" + field_suffix;
173 contact->name_prefix = "name_prefix_" + field_suffix;
174 contact->name_suffix = "name_suffix_" + field_suffix;
175 contact->photo = SkBitmap();
176 contact->email_addresses.clear();
177 contact->phone_numbers.clear();
178 contact->postal_addresses.clear();
179 contact->instant_messaging_addresses.clear();
180 }
181
182 void AddEmailAddress(const std::string& address,
183 Contact::AddressType::Relation relation,
184 const std::string& label,
185 bool primary,
186 Contact* contact) {
187 DCHECK(contact);
188 Contact::EmailAddress email;
189 email.address = address;
190 email.type.relation = relation;
191 email.type.label = label;
192 email.primary = primary;
193 contact->email_addresses.push_back(email);
194 }
195
196 void AddPhoneNumber(const std::string& number,
197 Contact::AddressType::Relation relation,
198 const std::string& label,
199 bool primary,
200 Contact* contact) {
201 DCHECK(contact);
202 Contact::PhoneNumber phone;
203 phone.number = number;
204 phone.type.relation = relation;
205 phone.type.label = label;
206 phone.primary = primary;
207 contact->phone_numbers.push_back(phone);
208 }
209
210 void AddPostalAddress(const std::string& address,
211 Contact::AddressType::Relation relation,
212 const std::string& label,
213 bool primary,
214 Contact* contact) {
215 DCHECK(contact);
216 Contact::PostalAddress postal;
217 postal.address = address;
218 postal.type.relation = relation;
219 postal.type.label = label;
220 postal.primary = primary;
221 contact->postal_addresses.push_back(postal);
222 }
223
224 void AddInstantMessagingAddress(
225 const std::string& address,
226 Contact::InstantMessagingAddress::Protocol protocol,
227 Contact::AddressType::Relation relation,
228 const std::string& label,
229 bool primary,
230 Contact* contact) {
231 DCHECK(contact);
232 Contact::InstantMessagingAddress im;
233 im.address = address;
234 im.protocol = protocol;
235 im.type.relation = relation;
236 im.type.label = label;
237 im.primary = primary;
238 contact->instant_messaging_addresses.push_back(im);
239 }
240
241 void SetPhoto(const gfx::Size& size, Contact* contact) {
242 DCHECK(contact);
243 contact->photo.setConfig(
244 SkBitmap::kARGB_8888_Config, size.width(), size.height());
245 contact->photo.allocPixels();
246 }
247
248 } // namespace test
249 } // namespace contacts
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698