Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_database_helper_browsertest.cc
blobcc56690181a693028767322a140e91095398ecf6
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.
5 #include "base/bind.h"
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;
21 namespace {
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 {
31 public:
32 virtual void CreateDatabases() {
33 webkit_database::DatabaseTracker* db_tracker =
34 BrowserContext::GetDefaultStoragePartition(browser()->profile())->
35 GetDatabaseTracker();
36 base::string16 db_name = base::ASCIIToUTF16("db");
37 base::string16 description = base::ASCIIToUTF16("db_description");
38 int64 size;
39 db_tracker->DatabaseOpened(kTestIdentifier1, db_name, description,
40 1, &size);
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,
47 1, &size);
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 {
62 public:
63 explicit StopTestOnCallback(
64 BrowsingDataDatabaseHelper* database_helper)
65 : database_helper_(database_helper) {
66 DCHECK(database_helper_);
69 void Callback(const std::list<BrowsingDataDatabaseHelper::DatabaseInfo>&
70 database_info_list) {
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();
78 private:
79 BrowsingDataDatabaseHelper* database_helper_;
82 // Flaky on Win/Mac/Linux: http://crbug.com/92460
83 IN_PROC_BROWSER_TEST_F(BrowsingDataDatabaseHelperTest, DISABLED_FetchData) {
84 CreateDatabases();
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 =
115 callback.result();
117 ASSERT_EQ(3u, result.size());
118 std::list<BrowsingDataDatabaseHelper::DatabaseInfo>::iterator info =
119 result.begin();
120 EXPECT_EQ(origin_str1, info->identifier.ToString());
121 EXPECT_STREQ(db1, info->database_name.c_str());
122 info++;
123 EXPECT_EQ(origin_str1, info->identifier.ToString());
124 EXPECT_STREQ(db2, info->database_name.c_str());
125 info++;
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 =
146 callback.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());
152 } // namespace