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/contact_test_util.h" | 5 #include "chrome/browser/chromeos/contacts/contact_test_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "ui/gfx/codec/png_codec.h" |
16 #include "ui/gfx/size.h" | 17 #include "ui/gfx/size.h" |
17 | 18 |
18 namespace contacts { | 19 namespace contacts { |
19 namespace test { | 20 namespace test { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 // Invokes |stringify_callback| on each item in |items| and prepends |prefix|, | 24 // 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 // and then sorts the resulting strings and joins them using |join_char|. |
25 template<class T> | 26 template<class T> |
26 std::string StringifyField( | 27 std::string StringifyField( |
27 const std::vector<T>& items, | 28 const ::google::protobuf::RepeatedPtrField<T>& items, |
28 base::Callback<std::string(const T&)> stringify_callback, | 29 base::Callback<std::string(const T&)> stringify_callback, |
29 const std::string& prefix, | 30 const std::string& prefix, |
30 char join_char) { | 31 char join_char) { |
31 std::vector<std::string> strings; | 32 std::vector<std::string> strings; |
32 for (size_t i = 0; i < items.size(); ++i) | 33 for (int i = 0; i < items.size(); ++i) |
33 strings.push_back(prefix + stringify_callback.Run(items[i])); | 34 strings.push_back(prefix + stringify_callback.Run(items.Get(i))); |
34 std::sort(strings.begin(), strings.end()); | 35 std::sort(strings.begin(), strings.end()); |
35 return JoinString(strings, join_char); | 36 return JoinString(strings, join_char); |
36 } | 37 } |
37 | 38 |
38 std::string EmailAddressToString(const Contact::EmailAddress& email) { | 39 std::string EmailAddressToString(const Contact_EmailAddress& email) { |
39 return email.address + "," + | 40 return email.address() + "," + |
40 base::IntToString(email.type.relation) + "," + | 41 base::IntToString(email.type().relation()) + "," + |
41 email.type.label + "," + | 42 email.type().label() + "," + |
42 base::IntToString(email.primary); | 43 base::IntToString(email.primary()); |
43 } | 44 } |
44 | 45 |
45 std::string PhoneNumberToString(const Contact::PhoneNumber& phone) { | 46 std::string PhoneNumberToString(const Contact_PhoneNumber& phone) { |
46 return phone.number + "," + | 47 return phone.number() + "," + |
47 base::IntToString(phone.type.relation) + "," + | 48 base::IntToString(phone.type().relation()) + "," + |
48 phone.type.label + "," + | 49 phone.type().label() + "," + |
49 base::IntToString(phone.primary); | 50 base::IntToString(phone.primary()); |
50 } | 51 } |
51 | 52 |
52 std::string PostalAddressToString(const Contact::PostalAddress& postal) { | 53 std::string PostalAddressToString(const Contact_PostalAddress& postal) { |
53 return postal.address + "," + | 54 return postal.address() + "," + |
54 base::IntToString(postal.type.relation) + "," + | 55 base::IntToString(postal.type().relation()) + "," + |
55 postal.type.label + "," + | 56 postal.type().label() + "," + |
56 base::IntToString(postal.primary); | 57 base::IntToString(postal.primary()); |
57 } | 58 } |
58 | 59 |
59 std::string InstantMessagingAddressToString( | 60 std::string InstantMessagingAddressToString( |
60 const Contact::InstantMessagingAddress& im) { | 61 const Contact_InstantMessagingAddress& im) { |
61 return im.address + "," + | 62 return im.address() + "," + |
62 base::IntToString(im.protocol) + "," + | 63 base::IntToString(im.protocol()) + "," + |
63 base::IntToString(im.type.relation) + "," + | 64 base::IntToString(im.type().relation()) + "," + |
64 im.type.label + "," + | 65 im.type().label() + "," + |
65 base::IntToString(im.primary); | 66 base::IntToString(im.primary()); |
66 } | 67 } |
67 | 68 |
68 } // namespace | 69 } // namespace |
69 | 70 |
70 std::string ContactToString(const Contact& contact) { | 71 std::string ContactToString(const Contact& contact) { |
71 std::string result = | 72 std::string result = |
72 contact.provider_id + "," + | 73 contact.provider_id() + "," + |
73 base::Int64ToString(contact.update_time.ToInternalValue()) + "," + | 74 base::Int64ToString(contact.update_time()) + "," + |
74 base::IntToString(contact.deleted) + "," + | 75 base::IntToString(contact.deleted()) + "," + |
75 contact.full_name + "," + | 76 contact.full_name() + "," + |
76 contact.given_name + "," + | 77 contact.given_name() + "," + |
77 contact.additional_name + "," + | 78 contact.additional_name() + "," + |
78 contact.family_name + "," + | 79 contact.family_name() + "," + |
79 contact.name_prefix + "," + | 80 contact.name_prefix() + "," + |
80 contact.name_suffix + "," + | 81 contact.name_suffix(); |
81 base::IntToString(contact.photo.width()) + "x" + | |
82 base::IntToString(contact.photo.height()); | |
83 | 82 |
84 result += " " + StringifyField(contact.email_addresses, | 83 SkBitmap bitmap; |
| 84 if (contact.has_png_photo()) { |
| 85 CHECK(gfx::PNGCodec::Decode(reinterpret_cast<const unsigned char*>( |
| 86 contact.png_photo().data()), |
| 87 contact.png_photo().size(), |
| 88 &bitmap)); |
| 89 } |
| 90 result += "," + base::IntToString(bitmap.width()) + "x" + |
| 91 base::IntToString(bitmap.height()); |
| 92 |
| 93 result += " " + StringifyField(contact.email_addresses(), |
85 base::Bind(EmailAddressToString), | 94 base::Bind(EmailAddressToString), |
86 "email=", ' '); | 95 "email=", ' '); |
87 result += " " + StringifyField(contact.phone_numbers, | 96 result += " " + StringifyField(contact.phone_numbers(), |
88 base::Bind(PhoneNumberToString), | 97 base::Bind(PhoneNumberToString), |
89 "phone=", ' '); | 98 "phone=", ' '); |
90 result += " " + StringifyField(contact.postal_addresses, | 99 result += " " + StringifyField(contact.postal_addresses(), |
91 base::Bind(PostalAddressToString), | 100 base::Bind(PostalAddressToString), |
92 "postal=", ' '); | 101 "postal=", ' '); |
93 result += " " + StringifyField(contact.instant_messaging_addresses, | 102 result += " " + StringifyField(contact.instant_messaging_addresses(), |
94 base::Bind(InstantMessagingAddressToString), | 103 base::Bind(InstantMessagingAddressToString), |
95 "im=", ' '); | 104 "im=", ' '); |
96 | 105 |
97 return result; | 106 return result; |
98 } | 107 } |
99 | 108 |
100 std::string ContactsToString(const ContactPointers& contacts) { | 109 std::string ContactsToString(const ContactPointers& contacts) { |
101 std::vector<std::string> contact_strings; | 110 std::vector<std::string> contact_strings; |
102 for (size_t i = 0; i < contacts.size(); ++i) | 111 for (size_t i = 0; i < contacts.size(); ++i) |
103 contact_strings.push_back(ContactToString(*contacts[i])); | 112 contact_strings.push_back(ContactToString(*contacts[i])); |
(...skipping 11 matching lines...) Expand all Loading... |
115 std::string VarContactsToString(int num_contacts, ...) { | 124 std::string VarContactsToString(int num_contacts, ...) { |
116 ContactPointers contacts; | 125 ContactPointers contacts; |
117 va_list list; | 126 va_list list; |
118 va_start(list, num_contacts); | 127 va_start(list, num_contacts); |
119 for (int i = 0; i < num_contacts; ++i) | 128 for (int i = 0; i < num_contacts; ++i) |
120 contacts.push_back(va_arg(list, const Contact*)); | 129 contacts.push_back(va_arg(list, const Contact*)); |
121 va_end(list); | 130 va_end(list); |
122 return ContactsToString(contacts); | 131 return ContactsToString(contacts); |
123 } | 132 } |
124 | 133 |
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, | 134 void CopyContacts(const ContactPointers& source, |
143 ScopedVector<Contact>* dest) { | 135 ScopedVector<Contact>* dest) { |
144 DCHECK(dest); | 136 DCHECK(dest); |
145 dest->clear(); | 137 dest->clear(); |
146 for (size_t i = 0; i < source.size(); ++i) { | 138 for (size_t i = 0; i < source.size(); ++i) { |
147 Contact* contact = new Contact; | 139 Contact* contact = new Contact; |
148 CopyContact(*source[i], contact); | 140 *contact = *source[i]; |
149 dest->push_back(contact); | 141 dest->push_back(contact); |
150 } | 142 } |
151 } | 143 } |
152 | 144 |
153 void CopyContacts(const ScopedVector<Contact>& source, | 145 void CopyContacts(const ScopedVector<Contact>& source, |
154 ScopedVector<Contact>* dest) { | 146 ScopedVector<Contact>* dest) { |
155 ContactPointers pointers; | 147 ContactPointers pointers; |
156 for (size_t i = 0; i < source.size(); ++i) | 148 for (size_t i = 0; i < source.size(); ++i) |
157 pointers.push_back(source[i]); | 149 pointers.push_back(source[i]); |
158 CopyContacts(pointers, dest); | 150 CopyContacts(pointers, dest); |
159 } | 151 } |
160 | 152 |
161 void InitContact(const std::string& provider_id, | 153 void InitContact(const std::string& provider_id, |
162 const std::string& name_suffix, | 154 const std::string& name_suffix, |
163 bool deleted, | 155 bool deleted, |
164 Contact* contact) { | 156 Contact* contact) { |
165 DCHECK(contact); | 157 DCHECK(contact); |
166 contact->provider_id = provider_id; | 158 contact->Clear(); |
167 contact->update_time = base::Time::Now(); | 159 contact->set_provider_id(provider_id); |
168 contact->deleted = deleted; | 160 contact->set_update_time(base::Time::Now().ToInternalValue()); |
169 contact->full_name = "full_name_" + name_suffix; | 161 contact->set_deleted(deleted); |
170 contact->given_name = "given_name_" + name_suffix; | 162 contact->set_full_name("full_name_" + name_suffix); |
171 contact->additional_name = "additional_name_" + name_suffix; | 163 contact->set_given_name("given_name_" + name_suffix); |
172 contact->family_name = "family_name_" + name_suffix; | 164 contact->set_additional_name("additional_name_" + name_suffix); |
173 contact->name_prefix = "name_prefix_" + name_suffix; | 165 contact->set_family_name("family_name_" + name_suffix); |
174 contact->name_suffix = "name_suffix_" + name_suffix; | 166 contact->set_name_prefix("name_prefix_" + name_suffix); |
175 contact->photo = SkBitmap(); | 167 contact->set_name_suffix("name_suffix_" + name_suffix); |
176 contact->email_addresses.clear(); | |
177 contact->phone_numbers.clear(); | |
178 contact->postal_addresses.clear(); | |
179 contact->instant_messaging_addresses.clear(); | |
180 } | 168 } |
181 | 169 |
182 void AddEmailAddress(const std::string& address, | 170 void AddEmailAddress(const std::string& address, |
183 Contact::AddressType::Relation relation, | 171 Contact_AddressType_Relation relation, |
184 const std::string& label, | 172 const std::string& label, |
185 bool primary, | 173 bool primary, |
186 Contact* contact) { | 174 Contact* contact) { |
187 DCHECK(contact); | 175 DCHECK(contact); |
188 Contact::EmailAddress email; | 176 Contact::EmailAddress* email = contact->add_email_addresses(); |
189 email.address = address; | 177 email->set_address(address); |
190 email.type.relation = relation; | 178 email->mutable_type()->set_relation(relation); |
191 email.type.label = label; | 179 email->mutable_type()->set_label(label); |
192 email.primary = primary; | 180 email->set_primary(primary); |
193 contact->email_addresses.push_back(email); | |
194 } | 181 } |
195 | 182 |
196 void AddPhoneNumber(const std::string& number, | 183 void AddPhoneNumber(const std::string& number, |
197 Contact::AddressType::Relation relation, | 184 Contact_AddressType_Relation relation, |
198 const std::string& label, | 185 const std::string& label, |
199 bool primary, | 186 bool primary, |
200 Contact* contact) { | 187 Contact* contact) { |
201 DCHECK(contact); | 188 DCHECK(contact); |
202 Contact::PhoneNumber phone; | 189 Contact::PhoneNumber* phone = contact->add_phone_numbers(); |
203 phone.number = number; | 190 phone->set_number(number); |
204 phone.type.relation = relation; | 191 phone->mutable_type()->set_relation(relation); |
205 phone.type.label = label; | 192 phone->mutable_type()->set_label(label); |
206 phone.primary = primary; | 193 phone->set_primary(primary); |
207 contact->phone_numbers.push_back(phone); | |
208 } | 194 } |
209 | 195 |
210 void AddPostalAddress(const std::string& address, | 196 void AddPostalAddress(const std::string& address, |
211 Contact::AddressType::Relation relation, | 197 Contact_AddressType_Relation relation, |
212 const std::string& label, | 198 const std::string& label, |
213 bool primary, | 199 bool primary, |
214 Contact* contact) { | 200 Contact* contact) { |
215 DCHECK(contact); | 201 DCHECK(contact); |
216 Contact::PostalAddress postal; | 202 Contact::PostalAddress* postal = contact->add_postal_addresses(); |
217 postal.address = address; | 203 postal->set_address(address); |
218 postal.type.relation = relation; | 204 postal->mutable_type()->set_relation(relation); |
219 postal.type.label = label; | 205 postal->mutable_type()->set_label(label); |
220 postal.primary = primary; | 206 postal->set_primary(primary); |
221 contact->postal_addresses.push_back(postal); | |
222 } | 207 } |
223 | 208 |
224 void AddInstantMessagingAddress( | 209 void AddInstantMessagingAddress( |
225 const std::string& address, | 210 const std::string& address, |
226 Contact::InstantMessagingAddress::Protocol protocol, | 211 Contact_InstantMessagingAddress_Protocol protocol, |
227 Contact::AddressType::Relation relation, | 212 Contact_AddressType_Relation relation, |
228 const std::string& label, | 213 const std::string& label, |
229 bool primary, | 214 bool primary, |
230 Contact* contact) { | 215 Contact* contact) { |
231 DCHECK(contact); | 216 DCHECK(contact); |
232 Contact::InstantMessagingAddress im; | 217 Contact::InstantMessagingAddress* im = |
233 im.address = address; | 218 contact->add_instant_messaging_addresses(); |
234 im.protocol = protocol; | 219 im->set_address(address); |
235 im.type.relation = relation; | 220 im->set_protocol(protocol); |
236 im.type.label = label; | 221 im->mutable_type()->set_relation(relation); |
237 im.primary = primary; | 222 im->mutable_type()->set_label(label); |
238 contact->instant_messaging_addresses.push_back(im); | 223 im->set_primary(primary); |
239 } | 224 } |
240 | 225 |
241 void SetPhoto(const gfx::Size& size, Contact* contact) { | 226 void SetPhoto(const gfx::Size& size, Contact* contact) { |
242 DCHECK(contact); | 227 DCHECK(contact); |
243 contact->photo.setConfig( | 228 if (size.IsEmpty()) { |
244 SkBitmap::kARGB_8888_Config, size.width(), size.height()); | 229 contact->clear_png_photo(); |
245 contact->photo.allocPixels(); | 230 return; |
| 231 } |
| 232 SkBitmap bitmap; |
| 233 bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height()); |
| 234 bitmap.allocPixels(); |
| 235 std::vector<unsigned char> png_photo; |
| 236 CHECK(gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_photo)); |
| 237 contact->set_png_photo(&png_photo[0], png_photo.size()); |
246 } | 238 } |
247 | 239 |
248 } // namespace test | 240 } // namespace test |
249 } // namespace contacts | 241 } // namespace contacts |
OLD | NEW |