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_raw_untrusted_photo()) { |
| 85 // Testing code just uses PNG for now. If that changes, use ImageDecoder |
| 86 // here instead. |
| 87 CHECK(gfx::PNGCodec::Decode(reinterpret_cast<const unsigned char*>( |
| 88 contact.raw_untrusted_photo().data()), |
| 89 contact.raw_untrusted_photo().size(), |
| 90 &bitmap)); |
| 91 } |
| 92 result += "," + base::IntToString(bitmap.width()) + "x" + |
| 93 base::IntToString(bitmap.height()); |
| 94 |
| 95 result += " " + StringifyField(contact.email_addresses(), |
85 base::Bind(EmailAddressToString), | 96 base::Bind(EmailAddressToString), |
86 "email=", ' '); | 97 "email=", ' '); |
87 result += " " + StringifyField(contact.phone_numbers, | 98 result += " " + StringifyField(contact.phone_numbers(), |
88 base::Bind(PhoneNumberToString), | 99 base::Bind(PhoneNumberToString), |
89 "phone=", ' '); | 100 "phone=", ' '); |
90 result += " " + StringifyField(contact.postal_addresses, | 101 result += " " + StringifyField(contact.postal_addresses(), |
91 base::Bind(PostalAddressToString), | 102 base::Bind(PostalAddressToString), |
92 "postal=", ' '); | 103 "postal=", ' '); |
93 result += " " + StringifyField(contact.instant_messaging_addresses, | 104 result += " " + StringifyField(contact.instant_messaging_addresses(), |
94 base::Bind(InstantMessagingAddressToString), | 105 base::Bind(InstantMessagingAddressToString), |
95 "im=", ' '); | 106 "im=", ' '); |
96 | 107 |
97 return result; | 108 return result; |
98 } | 109 } |
99 | 110 |
100 std::string ContactsToString(const ContactPointers& contacts) { | 111 std::string ContactsToString(const ContactPointers& contacts) { |
101 std::vector<std::string> contact_strings; | 112 std::vector<std::string> contact_strings; |
102 for (size_t i = 0; i < contacts.size(); ++i) | 113 for (size_t i = 0; i < contacts.size(); ++i) |
103 contact_strings.push_back(ContactToString(*contacts[i])); | 114 contact_strings.push_back(ContactToString(*contacts[i])); |
(...skipping 11 matching lines...) Expand all Loading... |
115 std::string VarContactsToString(int num_contacts, ...) { | 126 std::string VarContactsToString(int num_contacts, ...) { |
116 ContactPointers contacts; | 127 ContactPointers contacts; |
117 va_list list; | 128 va_list list; |
118 va_start(list, num_contacts); | 129 va_start(list, num_contacts); |
119 for (int i = 0; i < num_contacts; ++i) | 130 for (int i = 0; i < num_contacts; ++i) |
120 contacts.push_back(va_arg(list, const Contact*)); | 131 contacts.push_back(va_arg(list, const Contact*)); |
121 va_end(list); | 132 va_end(list); |
122 return ContactsToString(contacts); | 133 return ContactsToString(contacts); |
123 } | 134 } |
124 | 135 |
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, | 136 void CopyContacts(const ContactPointers& source, |
143 ScopedVector<Contact>* dest) { | 137 ScopedVector<Contact>* dest) { |
144 DCHECK(dest); | 138 DCHECK(dest); |
145 dest->clear(); | 139 dest->clear(); |
146 for (size_t i = 0; i < source.size(); ++i) { | 140 for (size_t i = 0; i < source.size(); ++i) { |
147 Contact* contact = new Contact; | 141 Contact* contact = new Contact; |
148 CopyContact(*source[i], contact); | 142 *contact = *source[i]; |
149 dest->push_back(contact); | 143 dest->push_back(contact); |
150 } | 144 } |
151 } | 145 } |
152 | 146 |
153 void CopyContacts(const ScopedVector<Contact>& source, | 147 void CopyContacts(const ScopedVector<Contact>& source, |
154 ScopedVector<Contact>* dest) { | 148 ScopedVector<Contact>* dest) { |
155 ContactPointers pointers; | 149 ContactPointers pointers; |
156 for (size_t i = 0; i < source.size(); ++i) | 150 for (size_t i = 0; i < source.size(); ++i) |
157 pointers.push_back(source[i]); | 151 pointers.push_back(source[i]); |
158 CopyContacts(pointers, dest); | 152 CopyContacts(pointers, dest); |
159 } | 153 } |
160 | 154 |
161 void InitContact(const std::string& provider_id, | 155 void InitContact(const std::string& provider_id, |
162 const std::string& name_suffix, | 156 const std::string& name_suffix, |
163 bool deleted, | 157 bool deleted, |
164 Contact* contact) { | 158 Contact* contact) { |
165 DCHECK(contact); | 159 DCHECK(contact); |
166 contact->provider_id = provider_id; | 160 contact->Clear(); |
167 contact->update_time = base::Time::Now(); | 161 contact->set_provider_id(provider_id); |
168 contact->deleted = deleted; | 162 contact->set_update_time(base::Time::Now().ToInternalValue()); |
169 contact->full_name = "full_name_" + name_suffix; | 163 contact->set_deleted(deleted); |
170 contact->given_name = "given_name_" + name_suffix; | 164 contact->set_full_name("full_name_" + name_suffix); |
171 contact->additional_name = "additional_name_" + name_suffix; | 165 contact->set_given_name("given_name_" + name_suffix); |
172 contact->family_name = "family_name_" + name_suffix; | 166 contact->set_additional_name("additional_name_" + name_suffix); |
173 contact->name_prefix = "name_prefix_" + name_suffix; | 167 contact->set_family_name("family_name_" + name_suffix); |
174 contact->name_suffix = "name_suffix_" + name_suffix; | 168 contact->set_name_prefix("name_prefix_" + name_suffix); |
175 contact->photo = SkBitmap(); | 169 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 } | 170 } |
181 | 171 |
182 void AddEmailAddress(const std::string& address, | 172 void AddEmailAddress(const std::string& address, |
183 Contact::AddressType::Relation relation, | 173 Contact_AddressType_Relation relation, |
184 const std::string& label, | 174 const std::string& label, |
185 bool primary, | 175 bool primary, |
186 Contact* contact) { | 176 Contact* contact) { |
187 DCHECK(contact); | 177 DCHECK(contact); |
188 Contact::EmailAddress email; | 178 Contact::EmailAddress* email = contact->add_email_addresses(); |
189 email.address = address; | 179 email->set_address(address); |
190 email.type.relation = relation; | 180 email->mutable_type()->set_relation(relation); |
191 email.type.label = label; | 181 email->mutable_type()->set_label(label); |
192 email.primary = primary; | 182 email->set_primary(primary); |
193 contact->email_addresses.push_back(email); | |
194 } | 183 } |
195 | 184 |
196 void AddPhoneNumber(const std::string& number, | 185 void AddPhoneNumber(const std::string& number, |
197 Contact::AddressType::Relation relation, | 186 Contact_AddressType_Relation relation, |
198 const std::string& label, | 187 const std::string& label, |
199 bool primary, | 188 bool primary, |
200 Contact* contact) { | 189 Contact* contact) { |
201 DCHECK(contact); | 190 DCHECK(contact); |
202 Contact::PhoneNumber phone; | 191 Contact::PhoneNumber* phone = contact->add_phone_numbers(); |
203 phone.number = number; | 192 phone->set_number(number); |
204 phone.type.relation = relation; | 193 phone->mutable_type()->set_relation(relation); |
205 phone.type.label = label; | 194 phone->mutable_type()->set_label(label); |
206 phone.primary = primary; | 195 phone->set_primary(primary); |
207 contact->phone_numbers.push_back(phone); | |
208 } | 196 } |
209 | 197 |
210 void AddPostalAddress(const std::string& address, | 198 void AddPostalAddress(const std::string& address, |
211 Contact::AddressType::Relation relation, | 199 Contact_AddressType_Relation relation, |
212 const std::string& label, | 200 const std::string& label, |
213 bool primary, | 201 bool primary, |
214 Contact* contact) { | 202 Contact* contact) { |
215 DCHECK(contact); | 203 DCHECK(contact); |
216 Contact::PostalAddress postal; | 204 Contact::PostalAddress* postal = contact->add_postal_addresses(); |
217 postal.address = address; | 205 postal->set_address(address); |
218 postal.type.relation = relation; | 206 postal->mutable_type()->set_relation(relation); |
219 postal.type.label = label; | 207 postal->mutable_type()->set_label(label); |
220 postal.primary = primary; | 208 postal->set_primary(primary); |
221 contact->postal_addresses.push_back(postal); | |
222 } | 209 } |
223 | 210 |
224 void AddInstantMessagingAddress( | 211 void AddInstantMessagingAddress( |
225 const std::string& address, | 212 const std::string& address, |
226 Contact::InstantMessagingAddress::Protocol protocol, | 213 Contact_InstantMessagingAddress_Protocol protocol, |
227 Contact::AddressType::Relation relation, | 214 Contact_AddressType_Relation relation, |
228 const std::string& label, | 215 const std::string& label, |
229 bool primary, | 216 bool primary, |
230 Contact* contact) { | 217 Contact* contact) { |
231 DCHECK(contact); | 218 DCHECK(contact); |
232 Contact::InstantMessagingAddress im; | 219 Contact::InstantMessagingAddress* im = |
233 im.address = address; | 220 contact->add_instant_messaging_addresses(); |
234 im.protocol = protocol; | 221 im->set_address(address); |
235 im.type.relation = relation; | 222 im->set_protocol(protocol); |
236 im.type.label = label; | 223 im->mutable_type()->set_relation(relation); |
237 im.primary = primary; | 224 im->mutable_type()->set_label(label); |
238 contact->instant_messaging_addresses.push_back(im); | 225 im->set_primary(primary); |
239 } | 226 } |
240 | 227 |
241 void SetPhoto(const gfx::Size& size, Contact* contact) { | 228 void SetPhoto(const gfx::Size& size, Contact* contact) { |
242 DCHECK(contact); | 229 DCHECK(contact); |
243 contact->photo.setConfig( | 230 if (size.IsEmpty()) { |
244 SkBitmap::kARGB_8888_Config, size.width(), size.height()); | 231 contact->clear_raw_untrusted_photo(); |
245 contact->photo.allocPixels(); | 232 return; |
| 233 } |
| 234 SkBitmap bitmap; |
| 235 bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height()); |
| 236 bitmap.allocPixels(); |
| 237 std::vector<unsigned char> png_photo; |
| 238 CHECK(gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_photo)); |
| 239 contact->set_raw_untrusted_photo(&png_photo[0], png_photo.size()); |
246 } | 240 } |
247 | 241 |
248 } // namespace test | 242 } // namespace test |
249 } // namespace contacts | 243 } // namespace contacts |
OLD | NEW |