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