OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_path.h" | 5 #include "base/file_path.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
9 #include "base/platform_file.h" | 9 #include "base/platform_file.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
11 #include "base/time.h" | 11 #include "base/time.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
14 #include "net/base/test_completion_callback.h" | 14 #include "net/base/test_completion_callback.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "webkit/database/database_tracker.h" | 16 #include "webkit/database/database_tracker.h" |
17 #include "webkit/database/database_util.h" | 17 #include "webkit/database/database_util.h" |
| 18 #include "webkit/quota/mock_special_storage_policy.h" |
18 #include "webkit/quota/quota_manager.h" | 19 #include "webkit/quota/quota_manager.h" |
19 #include "webkit/quota/special_storage_policy.h" | |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 const char kOrigin1Url[] = "http://origin1"; | 23 const char kOrigin1Url[] = "http://origin1"; |
24 const char kOrigin2Url[] = "http://protected_origin2"; | 24 const char kOrigin2Url[] = "http://protected_origin2"; |
25 | 25 |
26 class TestSpecialStoragePolicy : public quota::SpecialStoragePolicy { | |
27 public: | |
28 virtual bool IsStorageProtected(const GURL& origin) { | |
29 return origin == GURL(kOrigin2Url); | |
30 } | |
31 | |
32 virtual bool IsStorageUnlimited(const GURL& origin) { | |
33 return false; | |
34 } | |
35 | |
36 virtual bool IsFileHandler(const std::string& extension_id) { | |
37 return false; | |
38 } | |
39 }; | |
40 | |
41 class TestObserver : public webkit_database::DatabaseTracker::Observer { | 26 class TestObserver : public webkit_database::DatabaseTracker::Observer { |
42 public: | 27 public: |
43 TestObserver() : new_notification_received_(false) {} | 28 TestObserver() : new_notification_received_(false) {} |
44 virtual ~TestObserver() {} | 29 virtual ~TestObserver() {} |
45 virtual void OnDatabaseSizeChanged(const string16& origin_identifier, | 30 virtual void OnDatabaseSizeChanged(const string16& origin_identifier, |
46 const string16& database_name, | 31 const string16& database_name, |
47 int64 database_size) { | 32 int64 database_size) { |
48 new_notification_received_ = true; | 33 new_notification_received_ = true; |
49 origin_identifier_ = origin_identifier; | 34 origin_identifier_ = origin_identifier; |
50 database_name_ = database_name; | 35 database_name_ = database_name; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 // the FRIEND_TEST() macro, and we implement all tests we want to run as | 163 // the FRIEND_TEST() macro, and we implement all tests we want to run as |
179 // static methods of this class. Then we make our TEST() targets call these | 164 // static methods of this class. Then we make our TEST() targets call these |
180 // static functions. This allows us to run each test in normal mode and | 165 // static functions. This allows us to run each test in normal mode and |
181 // incognito mode without writing the same code twice. | 166 // incognito mode without writing the same code twice. |
182 class DatabaseTracker_TestHelper_Test { | 167 class DatabaseTracker_TestHelper_Test { |
183 public: | 168 public: |
184 static void TestDeleteOpenDatabase(bool incognito_mode) { | 169 static void TestDeleteOpenDatabase(bool incognito_mode) { |
185 // Initialize the tracker database. | 170 // Initialize the tracker database. |
186 ScopedTempDir temp_dir; | 171 ScopedTempDir temp_dir; |
187 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 172 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 173 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
| 174 new quota::MockSpecialStoragePolicy; |
| 175 special_storage_policy->AddProtected(GURL(kOrigin2Url)); |
188 scoped_refptr<DatabaseTracker> tracker( | 176 scoped_refptr<DatabaseTracker> tracker( |
189 new DatabaseTracker(temp_dir.path(), incognito_mode, false, | 177 new DatabaseTracker(temp_dir.path(), incognito_mode, false, |
190 new TestSpecialStoragePolicy, | 178 special_storage_policy, NULL, NULL)); |
191 NULL, NULL)); | |
192 | 179 |
193 // Create and open three databases. | 180 // Create and open three databases. |
194 int64 database_size = 0; | 181 int64 database_size = 0; |
195 const string16 kOrigin1 = | 182 const string16 kOrigin1 = |
196 DatabaseUtil::GetOriginIdentifier(GURL(kOrigin1Url)); | 183 DatabaseUtil::GetOriginIdentifier(GURL(kOrigin1Url)); |
197 const string16 kOrigin2 = | 184 const string16 kOrigin2 = |
198 DatabaseUtil::GetOriginIdentifier(GURL(kOrigin2Url)); | 185 DatabaseUtil::GetOriginIdentifier(GURL(kOrigin2Url)); |
199 const string16 kDB1 = ASCIIToUTF16("db1"); | 186 const string16 kDB1 = ASCIIToUTF16("db1"); |
200 const string16 kDB2 = ASCIIToUTF16("db2"); | 187 const string16 kDB2 = ASCIIToUTF16("db2"); |
201 const string16 kDB3 = ASCIIToUTF16("db3"); | 188 const string16 kDB3 = ASCIIToUTF16("db3"); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 file_util::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB3))); | 268 file_util::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB3))); |
282 | 269 |
283 tracker->DatabaseClosed(kOrigin2, kDB3); | 270 tracker->DatabaseClosed(kOrigin2, kDB3); |
284 tracker->RemoveObserver(&observer); | 271 tracker->RemoveObserver(&observer); |
285 } | 272 } |
286 | 273 |
287 static void TestDatabaseTracker(bool incognito_mode) { | 274 static void TestDatabaseTracker(bool incognito_mode) { |
288 // Initialize the tracker database. | 275 // Initialize the tracker database. |
289 ScopedTempDir temp_dir; | 276 ScopedTempDir temp_dir; |
290 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 277 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 278 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
| 279 new quota::MockSpecialStoragePolicy; |
| 280 special_storage_policy->AddProtected(GURL(kOrigin2Url)); |
291 scoped_refptr<DatabaseTracker> tracker( | 281 scoped_refptr<DatabaseTracker> tracker( |
292 new DatabaseTracker(temp_dir.path(), incognito_mode, false, | 282 new DatabaseTracker(temp_dir.path(), incognito_mode, false, |
293 new TestSpecialStoragePolicy, | 283 special_storage_policy, NULL, NULL)); |
294 NULL, NULL)); | |
295 | 284 |
296 // Add two observers. | 285 // Add two observers. |
297 TestObserver observer1; | 286 TestObserver observer1; |
298 TestObserver observer2; | 287 TestObserver observer2; |
299 tracker->AddObserver(&observer1); | 288 tracker->AddObserver(&observer1); |
300 tracker->AddObserver(&observer2); | 289 tracker->AddObserver(&observer2); |
301 | 290 |
302 // Open three new databases. | 291 // Open three new databases. |
303 int64 database_size = 0; | 292 int64 database_size = 0; |
304 const string16 kOrigin1 = | 293 const string16 kOrigin1 = |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 const string16 kDB1 = ASCIIToUTF16("db1"); | 517 const string16 kDB1 = ASCIIToUTF16("db1"); |
529 const string16 kDB2 = ASCIIToUTF16("db2"); | 518 const string16 kDB2 = ASCIIToUTF16("db2"); |
530 const string16 kDB3 = ASCIIToUTF16("db3"); | 519 const string16 kDB3 = ASCIIToUTF16("db3"); |
531 const string16 kDescription = ASCIIToUTF16("database_description"); | 520 const string16 kDescription = ASCIIToUTF16("database_description"); |
532 | 521 |
533 // Initialize the tracker database. | 522 // Initialize the tracker database. |
534 ScopedTempDir temp_dir; | 523 ScopedTempDir temp_dir; |
535 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 524 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
536 FilePath origin1_db_dir; | 525 FilePath origin1_db_dir; |
537 { | 526 { |
| 527 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
| 528 new quota::MockSpecialStoragePolicy; |
| 529 special_storage_policy->AddProtected(GURL(kOrigin2Url)); |
538 scoped_refptr<DatabaseTracker> tracker( | 530 scoped_refptr<DatabaseTracker> tracker( |
539 new DatabaseTracker( | 531 new DatabaseTracker( |
540 temp_dir.path(), false, true, | 532 temp_dir.path(), false, true, |
541 new TestSpecialStoragePolicy, | 533 special_storage_policy, NULL, |
542 NULL, | |
543 base::MessageLoopProxy::current())); | 534 base::MessageLoopProxy::current())); |
544 | 535 |
545 // Open three new databases. | 536 // Open three new databases. |
546 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0, | 537 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0, |
547 &database_size); | 538 &database_size); |
548 EXPECT_EQ(0, database_size); | 539 EXPECT_EQ(0, database_size); |
549 tracker->DatabaseOpened(kOrigin2, kDB2, kDescription, 0, | 540 tracker->DatabaseOpened(kOrigin2, kDB2, kDescription, 0, |
550 &database_size); | 541 &database_size); |
551 EXPECT_EQ(0, database_size); | 542 EXPECT_EQ(0, database_size); |
552 tracker->DatabaseOpened(kOrigin1, kDB3, kDescription, 0, | 543 tracker->DatabaseOpened(kOrigin1, kDB3, kDescription, 0, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 base::PLATFORM_FILE_SHARE_DELETE, | 580 base::PLATFORM_FILE_SHARE_DELETE, |
590 NULL, NULL); | 581 NULL, NULL); |
591 | 582 |
592 tracker->Shutdown(); | 583 tracker->Shutdown(); |
593 | 584 |
594 base::ClosePlatformFile(file_handle); | 585 base::ClosePlatformFile(file_handle); |
595 tracker->DatabaseClosed(kOrigin1, kDB3); | 586 tracker->DatabaseClosed(kOrigin1, kDB3); |
596 } | 587 } |
597 | 588 |
598 // At this point, the database tracker should be gone. Create a new one. | 589 // At this point, the database tracker should be gone. Create a new one. |
| 590 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
| 591 new quota::MockSpecialStoragePolicy; |
| 592 special_storage_policy->AddProtected(GURL(kOrigin2Url)); |
599 scoped_refptr<DatabaseTracker> tracker( | 593 scoped_refptr<DatabaseTracker> tracker( |
600 new DatabaseTracker(temp_dir.path(), false, false, | 594 new DatabaseTracker(temp_dir.path(), false, false, |
601 new TestSpecialStoragePolicy, | 595 special_storage_policy, NULL, NULL)); |
602 NULL, NULL)); | |
603 | 596 |
604 // Get all data for all origins. | 597 // Get all data for all origins. |
605 std::vector<OriginInfo> origins_info; | 598 std::vector<OriginInfo> origins_info; |
606 EXPECT_TRUE(tracker->GetAllOriginsInfo(&origins_info)); | 599 EXPECT_TRUE(tracker->GetAllOriginsInfo(&origins_info)); |
607 EXPECT_EQ(size_t(1), origins_info.size()); | 600 EXPECT_EQ(size_t(1), origins_info.size()); |
608 EXPECT_EQ(kOrigin2, origins_info[0].GetOrigin()); | 601 EXPECT_EQ(kOrigin2, origins_info[0].GetOrigin()); |
609 EXPECT_EQ(FilePath(), tracker->GetFullDBFilePath(kOrigin1, kDB1)); | 602 EXPECT_EQ(FilePath(), tracker->GetFullDBFilePath(kOrigin1, kDB1)); |
610 EXPECT_TRUE( | 603 EXPECT_TRUE( |
611 file_util::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB2))); | 604 file_util::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB2))); |
612 EXPECT_EQ(FilePath(), tracker->GetFullDBFilePath(kOrigin1, kDB3)); | 605 EXPECT_EQ(FilePath(), tracker->GetFullDBFilePath(kOrigin1, kDB3)); |
(...skipping 23 matching lines...) Expand all Loading... |
636 // There is no difference in behavior between incognito and not. | 629 // There is no difference in behavior between incognito and not. |
637 DatabaseTracker_TestHelper_Test::DatabaseTrackerQuotaIntegration(); | 630 DatabaseTracker_TestHelper_Test::DatabaseTrackerQuotaIntegration(); |
638 } | 631 } |
639 | 632 |
640 TEST(DatabaseTrackerTest, DatabaseTrackerClearLocalStateOnExit) { | 633 TEST(DatabaseTrackerTest, DatabaseTrackerClearLocalStateOnExit) { |
641 // Only works for regular mode. | 634 // Only works for regular mode. |
642 DatabaseTracker_TestHelper_Test::DatabaseTrackerClearLocalStateOnExit(); | 635 DatabaseTracker_TestHelper_Test::DatabaseTrackerClearLocalStateOnExit(); |
643 } | 636 } |
644 | 637 |
645 } // namespace webkit_database | 638 } // namespace webkit_database |
OLD | NEW |