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.
6 #include "base/bind_helpers.h"
7 #include "base/file_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/browsing_data/browsing_data_database_helper.h"
10 #include "chrome/browser/browsing_data/browsing_data_helper_browsertest.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/browser/storage_partition.h"
16 #include "content/public/test/test_browser_thread.h"
18 using content::BrowserContext
;
19 using content::BrowserThread
;
22 typedef BrowsingDataHelperCallback
<BrowsingDataDatabaseHelper::DatabaseInfo
>
23 TestCompletionCallback
;
25 const char kTestIdentifier1
[] = "http_www.google.com_0";
27 const char kTestIdentifierExtension
[] =
28 "chrome-extension_behllobkkfkfnphdnhnkndlbkcpglgmj_0";
30 class BrowsingDataDatabaseHelperTest
: public InProcessBrowserTest
{
32 virtual void CreateDatabases() {
33 webkit_database::DatabaseTracker
* db_tracker
=
34 BrowserContext::GetDefaultStoragePartition(browser()->profile())->
36 base::string16 db_name
= base::ASCIIToUTF16("db");
37 base::string16 description
= base::ASCIIToUTF16("db_description");
39 db_tracker
->DatabaseOpened(kTestIdentifier1
, db_name
, description
,
41 db_tracker
->DatabaseClosed(kTestIdentifier1
, db_name
);
42 base::FilePath db_path1
=
43 db_tracker
->GetFullDBFilePath(kTestIdentifier1
, db_name
);
44 base::CreateDirectory(db_path1
.DirName());
45 ASSERT_EQ(0, file_util::WriteFile(db_path1
, NULL
, 0));
46 db_tracker
->DatabaseOpened(kTestIdentifierExtension
, db_name
, description
,
48 db_tracker
->DatabaseClosed(kTestIdentifierExtension
, db_name
);
49 base::FilePath db_path2
=
50 db_tracker
->GetFullDBFilePath(kTestIdentifierExtension
, db_name
);
51 base::CreateDirectory(db_path2
.DirName());
52 ASSERT_EQ(0, file_util::WriteFile(db_path2
, NULL
, 0));
53 std::vector
<webkit_database::OriginInfo
> origins
;
54 db_tracker
->GetAllOriginsInfo(&origins
);
55 ASSERT_EQ(2U, origins
.size());
59 // Called back by BrowsingDataDatabaseHelper on the UI thread once the database
60 // information has been retrieved.
61 class StopTestOnCallback
{
63 explicit StopTestOnCallback(
64 BrowsingDataDatabaseHelper
* database_helper
)
65 : database_helper_(database_helper
) {
66 DCHECK(database_helper_
);
69 void Callback(const std::list
<BrowsingDataDatabaseHelper::DatabaseInfo
>&
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
72 ASSERT_EQ(1UL, database_info_list
.size());
73 EXPECT_EQ(std::string(kTestIdentifier1
),
74 database_info_list
.begin()->identifier
.ToString());
75 base::MessageLoop::current()->Quit();
79 BrowsingDataDatabaseHelper
* database_helper_
;
82 // Flaky on Win/Mac/Linux: http://crbug.com/92460
83 IN_PROC_BROWSER_TEST_F(BrowsingDataDatabaseHelperTest
, DISABLED_FetchData
) {
85 scoped_refptr
<BrowsingDataDatabaseHelper
> database_helper(
86 new BrowsingDataDatabaseHelper(browser()->profile()));
87 StopTestOnCallback
stop_test_on_callback(database_helper
.get());
88 database_helper
->StartFetching(base::Bind(
89 &StopTestOnCallback::Callback
, base::Unretained(&stop_test_on_callback
)));
90 // Blocks until StopTestOnCallback::Callback is notified.
91 content::RunMessageLoop();
94 IN_PROC_BROWSER_TEST_F(BrowsingDataDatabaseHelperTest
, CannedAddDatabase
) {
95 const GURL
origin1("http://host1:1/");
96 const GURL
origin2("http://host2:1/");
97 const char origin_str1
[] = "http_host1_1";
98 const char origin_str2
[] = "http_host2_1";
99 const char db1
[] = "db1";
100 const char db2
[] = "db2";
101 const char db3
[] = "db3";
103 scoped_refptr
<CannedBrowsingDataDatabaseHelper
> helper(
104 new CannedBrowsingDataDatabaseHelper(browser()->profile()));
105 helper
->AddDatabase(origin1
, db1
, std::string());
106 helper
->AddDatabase(origin1
, db2
, std::string());
107 helper
->AddDatabase(origin2
, db3
, std::string());
109 TestCompletionCallback callback
;
110 helper
->StartFetching(
111 base::Bind(&TestCompletionCallback::callback
,
112 base::Unretained(&callback
)));
114 std::list
<BrowsingDataDatabaseHelper::DatabaseInfo
> result
=
117 ASSERT_EQ(3u, result
.size());
118 std::list
<BrowsingDataDatabaseHelper::DatabaseInfo
>::iterator info
=
120 EXPECT_EQ(origin_str1
, info
->identifier
.ToString());
121 EXPECT_STREQ(db1
, info
->database_name
.c_str());
123 EXPECT_EQ(origin_str1
, info
->identifier
.ToString());
124 EXPECT_STREQ(db2
, info
->database_name
.c_str());
126 EXPECT_EQ(origin_str2
, info
->identifier
.ToString());
127 EXPECT_STREQ(db3
, info
->database_name
.c_str());
130 IN_PROC_BROWSER_TEST_F(BrowsingDataDatabaseHelperTest
, CannedUnique
) {
131 const GURL
origin("http://host1:1/");
132 const char origin_str
[] = "http_host1_1";
133 const char db
[] = "db1";
135 scoped_refptr
<CannedBrowsingDataDatabaseHelper
> helper(
136 new CannedBrowsingDataDatabaseHelper(browser()->profile()));
137 helper
->AddDatabase(origin
, db
, std::string());
138 helper
->AddDatabase(origin
, db
, std::string());
140 TestCompletionCallback callback
;
141 helper
->StartFetching(
142 base::Bind(&TestCompletionCallback::callback
,
143 base::Unretained(&callback
)));
145 std::list
<BrowsingDataDatabaseHelper::DatabaseInfo
> result
=
148 ASSERT_EQ(1u, result
.size());
149 EXPECT_EQ(origin_str
, result
.begin()->identifier
.ToString());
150 EXPECT_STREQ(db
, result
.begin()->database_name
.c_str());