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