1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
|
// Copyright (c) 2009 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 "base/file_util.h"
#include "base/path_service.h"
#include "base/stl_util-inl.h"
#include "base/string_util.h"
#include "base/time.h"
#include "base/values.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/webdata/web_database.h"
#include "chrome/common/chrome_paths.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/glue/form_field.h"
#include "webkit/glue/password_form.h"
using base::Time;
using base::TimeDelta;
using webkit_glue::FormField;
using webkit_glue::PasswordForm;
class WebDatabaseTest : public testing::Test {
protected:
virtual void SetUp() {
PathService::Get(chrome::DIR_TEST_DATA, &file_);
const std::string test_db = "TestWebDatabase" +
Int64ToString(base::Time::Now().ToInternalValue()) +
".db";
file_ = file_.AppendASCII(test_db);
file_util::Delete(file_, false);
}
virtual void TearDown() {
file_util::Delete(file_, false);
}
static int GetKeyCount(const DictionaryValue& d) {
DictionaryValue::key_iterator i(d.begin_keys());
DictionaryValue::key_iterator e(d.end_keys());
int r = 0;
while (i != e) {
++i;
++r;
}
return r;
}
static bool StringDictionaryValueEquals(const DictionaryValue& a,
const DictionaryValue& b) {
int a_count = 0;
int b_count = GetKeyCount(b);
DictionaryValue::key_iterator i(a.begin_keys());
DictionaryValue::key_iterator e(a.end_keys());
std::wstring av, bv;
while (i != e) {
if (!(a.GetString(*i, &av)) ||
!(b.GetString(*i, &bv)) ||
av != bv)
return false;
a_count++;
++i;
}
return (a_count == b_count);
}
static int64 GetID(const TemplateURL* url) {
return url->id();
}
static void SetID(int64 new_id, TemplateURL* url) {
url->set_id(new_id);
}
static void set_prepopulate_id(TemplateURL* url, int id) {
url->set_prepopulate_id(id);
}
FilePath file_;
};
TEST_F(WebDatabaseTest, Keywords) {
WebDatabase db;
EXPECT_TRUE(db.Init(file_));
TemplateURL template_url;
template_url.set_short_name(L"short_name");
template_url.set_keyword(L"keyword");
GURL favicon_url("http://favicon.url/");
GURL originating_url("http://google.com/");
template_url.SetFavIconURL(favicon_url);
template_url.SetURL(L"http://url/", 0, 0);
template_url.set_safe_for_autoreplace(true);
template_url.set_show_in_default_list(true);
template_url.set_originating_url(originating_url);
template_url.set_usage_count(32);
template_url.add_input_encoding("UTF-8");
set_prepopulate_id(&template_url, 10);
SetID(1, &template_url);
EXPECT_TRUE(db.AddKeyword(template_url));
std::vector<TemplateURL*> template_urls;
EXPECT_TRUE(db.GetKeywords(&template_urls));
EXPECT_EQ(1U, template_urls.size());
const TemplateURL* restored_url = template_urls.front();
EXPECT_EQ(template_url.short_name(), restored_url->short_name());
EXPECT_EQ(template_url.keyword(), restored_url->keyword());
EXPECT_FALSE(restored_url->autogenerate_keyword());
EXPECT_TRUE(favicon_url == restored_url->GetFavIconURL());
EXPECT_TRUE(restored_url->safe_for_autoreplace());
EXPECT_TRUE(restored_url->show_in_default_list());
EXPECT_EQ(GetID(&template_url), GetID(restored_url));
EXPECT_TRUE(originating_url == restored_url->originating_url());
EXPECT_EQ(32, restored_url->usage_count());
ASSERT_EQ(1U, restored_url->input_encodings().size());
EXPECT_EQ("UTF-8", restored_url->input_encodings()[0]);
EXPECT_EQ(10, restored_url->prepopulate_id());
EXPECT_TRUE(db.RemoveKeyword(restored_url->id()));
template_urls.clear();
EXPECT_TRUE(db.GetKeywords(&template_urls));
EXPECT_EQ(0U, template_urls.size());
delete restored_url;
}
TEST_F(WebDatabaseTest, KeywordMisc) {
WebDatabase db;
EXPECT_TRUE(db.Init(file_));
ASSERT_EQ(0, db.GetDefaulSearchProviderID());
ASSERT_EQ(0, db.GetBuitinKeywordVersion());
db.SetDefaultSearchProviderID(10);
db.SetBuitinKeywordVersion(11);
ASSERT_EQ(10, db.GetDefaulSearchProviderID());
ASSERT_EQ(11, db.GetBuitinKeywordVersion());
}
TEST_F(WebDatabaseTest, UpdateKeyword) {
WebDatabase db;
EXPECT_TRUE(db.Init(file_));
TemplateURL template_url;
template_url.set_short_name(L"short_name");
template_url.set_keyword(L"keyword");
GURL favicon_url("http://favicon.url/");
GURL originating_url("http://originating.url/");
template_url.SetFavIconURL(favicon_url);
template_url.SetURL(L"http://url/", 0, 0);
template_url.set_safe_for_autoreplace(true);
template_url.set_show_in_default_list(true);
template_url.SetSuggestionsURL(L"url2", 0, 0);
SetID(1, &template_url);
EXPECT_TRUE(db.AddKeyword(template_url));
GURL originating_url2("http://originating.url/");
template_url.set_originating_url(originating_url2);
template_url.set_autogenerate_keyword(true);
EXPECT_EQ(L"url", template_url.keyword());
template_url.add_input_encoding("Shift_JIS");
set_prepopulate_id(&template_url, 5);
EXPECT_TRUE(db.UpdateKeyword(template_url));
std::vector<TemplateURL*> template_urls;
EXPECT_TRUE(db.GetKeywords(&template_urls));
EXPECT_EQ(1U, template_urls.size());
const TemplateURL* restored_url = template_urls.front();
EXPECT_EQ(template_url.short_name(), restored_url->short_name());
EXPECT_EQ(template_url.keyword(), restored_url->keyword());
EXPECT_TRUE(restored_url->autogenerate_keyword());
EXPECT_TRUE(favicon_url == restored_url->GetFavIconURL());
EXPECT_TRUE(restored_url->safe_for_autoreplace());
EXPECT_TRUE(restored_url->show_in_default_list());
EXPECT_EQ(GetID(&template_url), GetID(restored_url));
EXPECT_TRUE(originating_url2 == restored_url->originating_url());
ASSERT_EQ(1U, restored_url->input_encodings().size());
ASSERT_EQ("Shift_JIS", restored_url->input_encodings()[0]);
EXPECT_EQ(template_url.suggestions_url()->url(),
restored_url->suggestions_url()->url());
EXPECT_EQ(template_url.id(), restored_url->id());
EXPECT_EQ(template_url.prepopulate_id(), restored_url->prepopulate_id());
delete restored_url;
}
TEST_F(WebDatabaseTest, KeywordWithNoFavicon) {
WebDatabase db;
EXPECT_TRUE(db.Init(file_));
TemplateURL template_url;
template_url.set_short_name(L"short_name");
template_url.set_keyword(L"keyword");
template_url.SetURL(L"http://url/", 0, 0);
template_url.set_safe_for_autoreplace(true);
SetID(-100, &template_url);
EXPECT_TRUE(db.AddKeyword(template_url));
std::vector<TemplateURL*> template_urls;
EXPECT_TRUE(db.GetKeywords(&template_urls));
EXPECT_EQ(1U, template_urls.size());
const TemplateURL* restored_url = template_urls.front();
EXPECT_EQ(template_url.short_name(), restored_url->short_name());
EXPECT_EQ(template_url.keyword(), restored_url->keyword());
EXPECT_TRUE(!restored_url->GetFavIconURL().is_valid());
EXPECT_TRUE(restored_url->safe_for_autoreplace());
EXPECT_EQ(GetID(&template_url), GetID(restored_url));
delete restored_url;
}
TEST_F(WebDatabaseTest, Logins) {
WebDatabase db;
EXPECT_TRUE(db.Init(file_));
std::vector<PasswordForm*> result;
// Verify the database is empty.
EXPECT_TRUE(db.GetAllLogins(&result, true));
EXPECT_EQ(0U, result.size());
// Example password form.
PasswordForm form;
form.origin = GURL("http://www.google.com/accounts/LoginAuth");
form.action = GURL("http://www.google.com/accounts/Login");
form.username_element = ASCIIToUTF16("Email");
form.username_value = ASCIIToUTF16("test@gmail.com");
form.password_element = ASCIIToUTF16("Passwd");
form.password_value = ASCIIToUTF16("test");
form.submit_element = ASCIIToUTF16("signIn");
form.signon_realm = "http://www.google.com/";
form.ssl_valid = false;
form.preferred = false;
form.scheme = PasswordForm::SCHEME_HTML;
// Add it and make sure it is there.
EXPECT_TRUE(db.AddLogin(form));
EXPECT_TRUE(db.GetAllLogins(&result, true));
EXPECT_EQ(1U, result.size());
delete result[0];
result.clear();
// Match against an exact copy.
EXPECT_TRUE(db.GetLogins(form, &result));
EXPECT_EQ(1U, result.size());
delete result[0];
result.clear();
// The example site changes...
PasswordForm form2(form);
form2.origin = GURL("http://www.google.com/new/accounts/LoginAuth");
form2.submit_element = ASCIIToUTF16("reallySignIn");
// Match against an inexact copy
EXPECT_TRUE(db.GetLogins(form2, &result));
EXPECT_EQ(1U, result.size());
delete result[0];
result.clear();
// Uh oh, the site changed origin & action URL's all at once!
PasswordForm form3(form2);
form3.action = GURL("http://www.google.com/new/accounts/Login");
// signon_realm is the same, should match.
EXPECT_TRUE(db.GetLogins(form3, &result));
EXPECT_EQ(1U, result.size());
delete result[0];
result.clear();
// Imagine the site moves to a secure server for login.
PasswordForm form4(form3);
form4.signon_realm = "https://www.google.com/";
form4.ssl_valid = true;
// We have only an http record, so no match for this.
EXPECT_TRUE(db.GetLogins(form4, &result));
EXPECT_EQ(0U, result.size());
// Let's imagine the user logs into the secure site.
EXPECT_TRUE(db.AddLogin(form4));
EXPECT_TRUE(db.GetAllLogins(&result, true));
EXPECT_EQ(2U, result.size());
delete result[0];
delete result[1];
result.clear();
// Now the match works
EXPECT_TRUE(db.GetLogins(form4, &result));
EXPECT_EQ(1U, result.size());
delete result[0];
result.clear();
// The user chose to forget the original but not the new.
EXPECT_TRUE(db.RemoveLogin(form));
EXPECT_TRUE(db.GetAllLogins(&result, true));
EXPECT_EQ(1U, result.size());
delete result[0];
result.clear();
// The old form wont match the new site (http vs https).
EXPECT_TRUE(db.GetLogins(form, &result));
EXPECT_EQ(0U, result.size());
// The user's request for the HTTPS site is intercepted
// by an attacker who presents an invalid SSL cert.
PasswordForm form5(form4);
form5.ssl_valid = 0;
// It will match in this case.
EXPECT_TRUE(db.GetLogins(form5, &result));
EXPECT_EQ(1U, result.size());
delete result[0];
result.clear();
// User changes his password.
PasswordForm form6(form5);
form6.password_value = ASCIIToUTF16("test6");
form6.preferred = true;
// We update, and check to make sure it matches the
// old form, and there is only one record.
EXPECT_TRUE(db.UpdateLogin(form6));
// matches
EXPECT_TRUE(db.GetLogins(form5, &result));
EXPECT_EQ(1U, result.size());
delete result[0];
result.clear();
// Only one record.
EXPECT_TRUE(db.GetAllLogins(&result, true));
EXPECT_EQ(1U, result.size());
// password element was updated.
EXPECT_EQ(form6.password_value, result[0]->password_value);
// Preferred login.
EXPECT_TRUE(form6.preferred);
delete result[0];
result.clear();
// Make sure everything can disappear.
EXPECT_TRUE(db.RemoveLogin(form4));
EXPECT_TRUE(db.GetAllLogins(&result, true));
EXPECT_EQ(0U, result.size());
}
TEST_F(WebDatabaseTest, Autofill) {
WebDatabase db;
EXPECT_TRUE(db.Init(file_));
Time t1 = Time::Now();
// Simulate the submission of a handful of entries in a field called "Name",
// some more often than others.
EXPECT_TRUE(db.AddFormFieldValue(
FormField(string16(),
ASCIIToUTF16("Name"),
string16(),
ASCIIToUTF16("Superman"))));
std::vector<string16> v;
for (int i = 0; i < 5; i++) {
EXPECT_TRUE(db.AddFormFieldValue(
FormField(string16(),
ASCIIToUTF16("Name"),
string16(),
ASCIIToUTF16("Clark Kent"))));
}
for (int i = 0; i < 3; i++) {
EXPECT_TRUE(db.AddFormFieldValue(
FormField(string16(),
ASCIIToUTF16("Name"),
string16(),
ASCIIToUTF16("Clark Sutter"))));
}
for (int i = 0; i < 2; i++) {
EXPECT_TRUE(db.AddFormFieldValue(
FormField(string16(),
ASCIIToUTF16("Favorite Color"),
string16(),
ASCIIToUTF16("Green"))));
}
int count = 0;
int64 pair_id = 0;
// We have added the name Clark Kent 5 times, so count should be 5 and pair_id
// should be somthing non-zero.
EXPECT_TRUE(db.GetIDAndCountOfFormElement(
FormField(string16(),
ASCIIToUTF16("Name"),
string16(),
ASCIIToUTF16("Clark Kent")),
&pair_id, &count));
EXPECT_EQ(5, count);
EXPECT_NE(0, pair_id);
// Storing in the data base should be case sensitive, so there should be no
// database entry for clark kent lowercase.
EXPECT_TRUE(db.GetIDAndCountOfFormElement(
FormField(string16(),
ASCIIToUTF16("Name"),
string16(),
ASCIIToUTF16("clark kent")),
&pair_id, &count));
EXPECT_EQ(0, count);
EXPECT_TRUE(db.GetIDAndCountOfFormElement(
FormField(string16(),
ASCIIToUTF16("Favorite Color"),
string16(),
ASCIIToUTF16("Green")),
&pair_id, &count));
EXPECT_EQ(2, count);
// This is meant to get a list of suggestions for Name. The empty prefix
// in the second argument means it should return all suggestions for a name
// no matter what they start with. The order that the names occur in the list
// should be decreasing order by count.
EXPECT_TRUE(db.GetFormValuesForElementName(
ASCIIToUTF16("Name"), string16(), &v, 6));
EXPECT_EQ(3U, v.size());
if (v.size() == 3) {
EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v[0]);
EXPECT_EQ(ASCIIToUTF16("Clark Sutter"), v[1]);
EXPECT_EQ(ASCIIToUTF16("Superman"), v[2]);
}
// If we query again limiting the list size to 1, we should only get the most
// frequent entry.
EXPECT_TRUE(db.GetFormValuesForElementName(
ASCIIToUTF16("Name"), string16(), &v, 1));
EXPECT_EQ(1U, v.size());
if (v.size() == 1) {
EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v[0]);
}
// Querying for suggestions given a prefix is case-insensitive, so the prefix
// "cLa" shoud get suggestions for both Clarks.
EXPECT_TRUE(db.GetFormValuesForElementName(
ASCIIToUTF16("Name"), ASCIIToUTF16("cLa"), &v, 6));
EXPECT_EQ(2U, v.size());
if (v.size() == 2) {
EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v[0]);
EXPECT_EQ(ASCIIToUTF16("Clark Sutter"), v[1]);
}
// Removing all elements since the beginning of this function should remove
// everything from the database.
EXPECT_TRUE(db.RemoveFormElementsAddedBetween(t1, Time()));
EXPECT_TRUE(db.GetIDAndCountOfFormElement(
FormField(string16(),
ASCIIToUTF16("Name"),
string16(),
ASCIIToUTF16("Clark Kent")),
&pair_id, &count));
EXPECT_EQ(0, count);
EXPECT_TRUE(
db.GetFormValuesForElementName(ASCIIToUTF16("Name"), string16(), &v, 6));
EXPECT_EQ(0U, v.size());
// Now add some values with empty strings.
const string16 kValue = ASCIIToUTF16(" toto ");
EXPECT_TRUE(db.AddFormFieldValue(FormField(string16(),
ASCIIToUTF16("blank"),
string16(),
string16())));
EXPECT_TRUE(db.AddFormFieldValue(FormField(string16(),
ASCIIToUTF16("blank"),
string16(),
ASCIIToUTF16(" "))));
EXPECT_TRUE(db.AddFormFieldValue(FormField(string16(),
ASCIIToUTF16("blank"),
string16(),
ASCIIToUTF16(" "))));
EXPECT_TRUE(db.AddFormFieldValue(FormField(string16(),
ASCIIToUTF16("blank"),
string16(),
kValue)));
// They should be stored normally as the DB layer does not check for empty
// values.
v.clear();
EXPECT_TRUE(db.GetFormValuesForElementName(
ASCIIToUTF16("blank"), string16(), &v, 10));
EXPECT_EQ(4U, v.size());
// Now we'll check that ClearAutofillEmptyValueElements() works as expected.
db.ClearAutofillEmptyValueElements();
v.clear();
EXPECT_TRUE(db.GetFormValuesForElementName(ASCIIToUTF16("blank"),
string16(), &v, 10));
ASSERT_EQ(1U, v.size());
EXPECT_EQ(kValue, v[0]);
}
static bool AddTimestampedLogin(WebDatabase* db, std::string url,
const std::string& unique_string,
const Time& time) {
// Example password form.
PasswordForm form;
form.origin = GURL(url + std::string("/LoginAuth"));
form.username_element = ASCIIToUTF16(unique_string);
form.username_value = ASCIIToUTF16(unique_string);
form.password_element = ASCIIToUTF16(unique_string);
form.submit_element = ASCIIToUTF16("signIn");
form.signon_realm = url;
form.date_created = time;
return db->AddLogin(form);
}
static void ClearResults(std::vector<PasswordForm*>* results) {
for (size_t i = 0; i < results->size(); ++i) {
delete (*results)[i];
}
results->clear();
}
TEST_F(WebDatabaseTest, ClearPrivateData_SavedPasswords) {
WebDatabase db;
EXPECT_TRUE(db.Init(file_));
std::vector<PasswordForm*> result;
// Verify the database is empty.
EXPECT_TRUE(db.GetAllLogins(&result, true));
EXPECT_EQ(0U, result.size());
Time now = Time::Now();
TimeDelta one_day = TimeDelta::FromDays(1);
// Create one with a 0 time.
EXPECT_TRUE(AddTimestampedLogin(&db, "1", "foo1", Time()));
// Create one for now and +/- 1 day.
EXPECT_TRUE(AddTimestampedLogin(&db, "2", "foo2", now - one_day));
EXPECT_TRUE(AddTimestampedLogin(&db, "3", "foo3", now));
EXPECT_TRUE(AddTimestampedLogin(&db, "4", "foo4", now + one_day));
// Verify inserts worked.
EXPECT_TRUE(db.GetAllLogins(&result, true));
EXPECT_EQ(4U, result.size());
ClearResults(&result);
// Delete everything from today's date and on.
db.RemoveLoginsCreatedBetween(now, Time());
// Should have deleted half of what we inserted.
EXPECT_TRUE(db.GetAllLogins(&result, true));
EXPECT_EQ(2U, result.size());
ClearResults(&result);
// Delete with 0 date (should delete all).
db.RemoveLoginsCreatedBetween(Time(), Time());
// Verify nothing is left.
EXPECT_TRUE(db.GetAllLogins(&result, true));
EXPECT_EQ(0U, result.size());
}
TEST_F(WebDatabaseTest, BlacklistedLogins) {
WebDatabase db;
EXPECT_TRUE(db.Init(file_));
std::vector<PasswordForm*> result;
// Verify the database is empty.
EXPECT_TRUE(db.GetAllLogins(&result, true));
ASSERT_EQ(0U, result.size());
// Save a form as blacklisted.
PasswordForm form;
form.origin = GURL("http://www.google.com/accounts/LoginAuth");
form.action = GURL("http://www.google.com/accounts/Login");
form.username_element = ASCIIToUTF16("Email");
form.password_element = ASCIIToUTF16("Passwd");
form.submit_element = ASCIIToUTF16("signIn");
form.signon_realm = "http://www.google.com/";
form.ssl_valid = false;
form.preferred = true;
form.blacklisted_by_user = true;
form.scheme = PasswordForm::SCHEME_HTML;
EXPECT_TRUE(db.AddLogin(form));
// Get all non-blacklisted logins (should be none).
EXPECT_TRUE(db.GetAllLogins(&result, false));
ASSERT_EQ(0U, result.size());
// GetLogins should give the blacklisted result.
EXPECT_TRUE(db.GetLogins(form, &result));
EXPECT_EQ(1U, result.size());
ClearResults(&result);
// So should GetAll including blacklisted.
EXPECT_TRUE(db.GetAllLogins(&result, true));
EXPECT_EQ(1U, result.size());
ClearResults(&result);
}
TEST_F(WebDatabaseTest, WebAppHasAllImages) {
WebDatabase db;
EXPECT_TRUE(db.Init(file_));
GURL url("http://google.com/");
// Initial value for unknown web app should be false.
EXPECT_FALSE(db.GetWebAppHasAllImages(url));
// Set the value and make sure it took.
EXPECT_TRUE(db.SetWebAppHasAllImages(url, true));
EXPECT_TRUE(db.GetWebAppHasAllImages(url));
// Remove the app and make sure value reverts to default.
EXPECT_TRUE(db.RemoveWebApp(url));
EXPECT_FALSE(db.GetWebAppHasAllImages(url));
}
TEST_F(WebDatabaseTest, WebAppImages) {
WebDatabase db;
ASSERT_TRUE(db.Init(file_));
GURL url("http://google.com/");
// Web app should initially have no images.
std::vector<SkBitmap> images;
ASSERT_TRUE(db.GetWebAppImages(url, &images));
ASSERT_EQ(0U, images.size());
// Add an image.
SkBitmap image;
image.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
image.allocPixels();
image.eraseColor(SK_ColorBLACK);
ASSERT_TRUE(db.SetWebAppImage(url, image));
// Make sure we get the image back.
ASSERT_TRUE(db.GetWebAppImages(url, &images));
ASSERT_EQ(1U, images.size());
ASSERT_EQ(16, images[0].width());
ASSERT_EQ(16, images[0].height());
// Add another 16x16 image and make sure it replaces the original.
image.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
image.allocPixels();
image.eraseColor(SK_ColorBLACK);
// Set some random pixels so that we can identify the image. We don't use
// transparent images because of pre-multiplication rounding errors.
SkColor test_pixel_1 = 0xffccbbaa;
SkColor test_pixel_2 = 0xffaabbaa;
SkColor test_pixel_3 = 0xff339966;
image.getAddr32(0, 1)[0] = test_pixel_1;
image.getAddr32(0, 1)[1] = test_pixel_2;
image.getAddr32(0, 1)[2] = test_pixel_3;
ASSERT_TRUE(db.SetWebAppImage(url, image));
images.clear();
ASSERT_TRUE(db.GetWebAppImages(url, &images));
ASSERT_EQ(1U, images.size());
ASSERT_EQ(16, images[0].width());
ASSERT_EQ(16, images[0].height());
images[0].lockPixels();
ASSERT_TRUE(images[0].getPixels() != NULL);
ASSERT_EQ(test_pixel_1, images[0].getAddr32(0, 1)[0]);
ASSERT_EQ(test_pixel_2, images[0].getAddr32(0, 1)[1]);
ASSERT_EQ(test_pixel_3, images[0].getAddr32(0, 1)[2]);
images[0].unlockPixels();
// Add another image at a bigger size.
image.setConfig(SkBitmap::kARGB_8888_Config, 32, 32);
image.allocPixels();
image.eraseColor(SK_ColorBLACK);
ASSERT_TRUE(db.SetWebAppImage(url, image));
// Make sure we get both images back.
images.clear();
ASSERT_TRUE(db.GetWebAppImages(url, &images));
ASSERT_EQ(2U, images.size());
if (images[0].width() == 16) {
ASSERT_EQ(16, images[0].width());
ASSERT_EQ(16, images[0].height());
ASSERT_EQ(32, images[1].width());
ASSERT_EQ(32, images[1].height());
} else {
ASSERT_EQ(32, images[0].width());
ASSERT_EQ(32, images[0].height());
ASSERT_EQ(16, images[1].width());
ASSERT_EQ(16, images[1].height());
}
}
|