OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "base/bind.h" | |
6 #include "base/bind_helpers.h" | |
7 #include "base/file_util.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "chrome/browser/browsing_data_database_helper.h" | |
10 #include "chrome/browser/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/test/test_browser_thread.h" | |
16 | |
17 using content::BrowserContext; | |
18 using content::BrowserThread; | |
19 | |
20 namespace { | |
21 typedef BrowsingDataHelperCallback<BrowsingDataDatabaseHelper::DatabaseInfo> | |
22 TestCompletionCallback; | |
23 | |
24 const char kTestIdentifier1[] = "http_www.google.com_0"; | |
25 | |
26 const char kTestIdentifierExtension[] = | |
27 "chrome-extension_behllobkkfkfnphdnhnkndlbkcpglgmj_0"; | |
28 | |
29 class BrowsingDataDatabaseHelperTest : public InProcessBrowserTest { | |
30 public: | |
31 virtual void CreateDatabases() { | |
32 webkit_database::DatabaseTracker* db_tracker = | |
33 BrowserContext::GetDatabaseTracker(browser()->profile()); | |
34 string16 db_name = ASCIIToUTF16("db"); | |
35 string16 description = ASCIIToUTF16("db_description"); | |
36 int64 size; | |
37 string16 identifier1(UTF8ToUTF16(kTestIdentifier1)); | |
38 db_tracker->DatabaseOpened(identifier1, db_name, description, 1, &size); | |
39 db_tracker->DatabaseClosed(identifier1, db_name); | |
40 FilePath db_path1 = db_tracker->GetFullDBFilePath(identifier1, db_name); | |
41 file_util::CreateDirectory(db_path1.DirName()); | |
42 ASSERT_EQ(0, file_util::WriteFile(db_path1, NULL, 0)); | |
43 string16 identifierExtension(UTF8ToUTF16(kTestIdentifierExtension)); | |
44 db_tracker->DatabaseOpened(identifierExtension, db_name, description, 1, | |
45 &size); | |
46 db_tracker->DatabaseClosed(identifierExtension, db_name); | |
47 FilePath db_path2 = | |
48 db_tracker->GetFullDBFilePath(identifierExtension, db_name); | |
49 file_util::CreateDirectory(db_path2.DirName()); | |
50 ASSERT_EQ(0, file_util::WriteFile(db_path2, NULL, 0)); | |
51 std::vector<webkit_database::OriginInfo> origins; | |
52 db_tracker->GetAllOriginsInfo(&origins); | |
53 ASSERT_EQ(2U, origins.size()); | |
54 } | |
55 }; | |
56 | |
57 // Called back by BrowsingDataDatabaseHelper on the UI thread once the database | |
58 // information has been retrieved. | |
59 class StopTestOnCallback { | |
60 public: | |
61 explicit StopTestOnCallback( | |
62 BrowsingDataDatabaseHelper* database_helper) | |
63 : database_helper_(database_helper) { | |
64 DCHECK(database_helper_); | |
65 } | |
66 | |
67 void Callback(const std::list<BrowsingDataDatabaseHelper::DatabaseInfo>& | |
68 database_info_list) { | |
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
70 ASSERT_EQ(1UL, database_info_list.size()); | |
71 EXPECT_EQ(std::string(kTestIdentifier1), | |
72 database_info_list.begin()->origin_identifier); | |
73 MessageLoop::current()->Quit(); | |
74 } | |
75 | |
76 private: | |
77 BrowsingDataDatabaseHelper* database_helper_; | |
78 }; | |
79 | |
80 // Flaky on Win/Mac/Linux: http://crbug.com/92460 | |
81 IN_PROC_BROWSER_TEST_F(BrowsingDataDatabaseHelperTest, DISABLED_FetchData) { | |
82 CreateDatabases(); | |
83 scoped_refptr<BrowsingDataDatabaseHelper> database_helper( | |
84 new BrowsingDataDatabaseHelper(browser()->profile())); | |
85 StopTestOnCallback stop_test_on_callback(database_helper); | |
86 database_helper->StartFetching( | |
87 base::Bind(&StopTestOnCallback::Callback, | |
88 base::Unretained(&stop_test_on_callback))); | |
89 // Blocks until StopTestOnCallback::Callback is notified. | |
90 ui_test_utils::RunMessageLoop(); | |
91 } | |
92 | |
93 IN_PROC_BROWSER_TEST_F(BrowsingDataDatabaseHelperTest, CannedAddDatabase) { | |
94 const GURL origin1("http://host1:1/"); | |
95 const GURL origin2("http://host2:1/"); | |
96 const char origin_str1[] = "http_host1_1"; | |
97 const char origin_str2[] = "http_host2_1"; | |
98 const char db1[] = "db1"; | |
99 const char db2[] = "db2"; | |
100 const char db3[] = "db3"; | |
101 | |
102 scoped_refptr<CannedBrowsingDataDatabaseHelper> helper( | |
103 new CannedBrowsingDataDatabaseHelper(browser()->profile())); | |
104 helper->AddDatabase(origin1, db1, ""); | |
105 helper->AddDatabase(origin1, db2, ""); | |
106 helper->AddDatabase(origin2, db3, ""); | |
107 | |
108 TestCompletionCallback callback; | |
109 helper->StartFetching( | |
110 base::Bind(&TestCompletionCallback::callback, | |
111 base::Unretained(&callback))); | |
112 | |
113 std::list<BrowsingDataDatabaseHelper::DatabaseInfo> result = | |
114 callback.result(); | |
115 | |
116 ASSERT_EQ(3u, result.size()); | |
117 std::list<BrowsingDataDatabaseHelper::DatabaseInfo>::iterator info = | |
118 result.begin(); | |
119 EXPECT_STREQ(origin_str1, info->origin_identifier.c_str()); | |
120 EXPECT_STREQ(db1, info->database_name.c_str()); | |
121 info++; | |
122 EXPECT_STREQ(origin_str1, info->origin_identifier.c_str()); | |
123 EXPECT_STREQ(db2, info->database_name.c_str()); | |
124 info++; | |
125 EXPECT_STREQ(origin_str2, info->origin_identifier.c_str()); | |
126 EXPECT_STREQ(db3, info->database_name.c_str()); | |
127 } | |
128 | |
129 IN_PROC_BROWSER_TEST_F(BrowsingDataDatabaseHelperTest, CannedUnique) { | |
130 const GURL origin("http://host1:1/"); | |
131 const char origin_str[] = "http_host1_1"; | |
132 const char db[] = "db1"; | |
133 | |
134 scoped_refptr<CannedBrowsingDataDatabaseHelper> helper( | |
135 new CannedBrowsingDataDatabaseHelper(browser()->profile())); | |
136 helper->AddDatabase(origin, db, ""); | |
137 helper->AddDatabase(origin, db, ""); | |
138 | |
139 TestCompletionCallback callback; | |
140 helper->StartFetching( | |
141 base::Bind(&TestCompletionCallback::callback, | |
142 base::Unretained(&callback))); | |
143 | |
144 std::list<BrowsingDataDatabaseHelper::DatabaseInfo> result = | |
145 callback.result(); | |
146 | |
147 ASSERT_EQ(1u, result.size()); | |
148 EXPECT_STREQ(origin_str, result.begin()->origin_identifier.c_str()); | |
149 EXPECT_STREQ(db, result.begin()->database_name.c_str()); | |
150 } | |
151 } // namespace | |
OLD | NEW |