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 <map> |
| 6 |
| 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/memory/scoped_callback_factory.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "base/message_loop_proxy.h" |
| 12 #include "chrome/test/testing_profile.h" |
| 13 #include "content/browser/in_process_webkit/indexed_db_context.h" |
| 14 #include "content/browser/in_process_webkit/indexed_db_quota_client.h" |
| 15 #include "content/browser/in_process_webkit/webkit_context.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "webkit/database/database_util.h" |
| 18 |
| 19 // Declared to shorten the line lengths. |
| 20 static const quota::StorageType kTemp = quota::kStorageTypeTemporary; |
| 21 static const quota::StorageType kPerm = quota::kStorageTypePersistent; |
| 22 |
| 23 using namespace webkit_database; |
| 24 |
| 25 // Base class for our test fixtures. |
| 26 class IndexedDBQuotaClientTest : public testing::Test { |
| 27 public: |
| 28 const GURL kOriginA; |
| 29 const GURL kOriginB; |
| 30 |
| 31 IndexedDBQuotaClientTest() |
| 32 : kOriginA("http://host"), |
| 33 kOriginB("http://host:8000"), |
| 34 usage_(0), |
| 35 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 36 message_loop_(MessageLoop::TYPE_IO), |
| 37 webkit_thread_(BrowserThread::WEBKIT, &message_loop_) { |
| 38 TestingProfile profile; |
| 39 idb_context_ = new IndexedDBContext(profile.GetWebKitContext(), NULL, |
| 40 NULL, NULL); |
| 41 } |
| 42 |
| 43 ~IndexedDBQuotaClientTest() { |
| 44 // IndexedDBContext needs to be destructed on BrowserThread::WEBKIT, which |
| 45 // is also a member variable of this class. Cause IndexedDBContext's |
| 46 // destruction now to ensure that it doesn't outlive BrowserThread::WEBKIT. |
| 47 idb_context_ = NULL; |
| 48 MessageLoop::current()->RunAllPending(); |
| 49 } |
| 50 |
| 51 int64 GetOriginUsage( |
| 52 quota::QuotaClient* client, |
| 53 const GURL& origin, |
| 54 quota::StorageType type) { |
| 55 usage_ = -1; |
| 56 client->GetOriginUsage(origin, type, |
| 57 callback_factory_.NewCallback( |
| 58 &IndexedDBQuotaClientTest::OnGetOriginUsageComplete)); |
| 59 MessageLoop::current()->RunAllPending(); |
| 60 EXPECT_GT(usage_, -1); |
| 61 return usage_; |
| 62 } |
| 63 |
| 64 IndexedDBContext* idb_context() { return idb_context_.get(); } |
| 65 |
| 66 void SetFileSizeTo(const FilePath& path, int size) { |
| 67 std::string junk(size, 'a'); |
| 68 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); |
| 69 } |
| 70 |
| 71 |
| 72 private: |
| 73 void OnGetOriginUsageComplete(int64 usage) { |
| 74 usage_ = usage; |
| 75 } |
| 76 |
| 77 int64 usage_; |
| 78 scoped_refptr<IndexedDBContext> idb_context_; |
| 79 base::ScopedCallbackFactory<IndexedDBQuotaClientTest> callback_factory_; |
| 80 MessageLoop message_loop_; |
| 81 BrowserThread webkit_thread_; |
| 82 }; |
| 83 |
| 84 |
| 85 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { |
| 86 IndexedDBQuotaClient client( |
| 87 base::MessageLoopProxy::CreateForCurrentThread(), |
| 88 idb_context()); |
| 89 |
| 90 ScopedTempDir temp_dir; |
| 91 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 92 FilePath indexeddb_dir = temp_dir.path().Append( |
| 93 IndexedDBContext::kIndexedDBDirectory); |
| 94 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); |
| 95 |
| 96 idb_context()->set_data_path(indexeddb_dir); |
| 97 FilePath file_path_origin_a = idb_context()->GetIndexedDBFilePath( |
| 98 DatabaseUtil::GetOriginIdentifier(kOriginA)); |
| 99 FilePath file_path_origin_b = idb_context()->GetIndexedDBFilePath( |
| 100 DatabaseUtil::GetOriginIdentifier(kOriginB)); |
| 101 |
| 102 SetFileSizeTo(file_path_origin_a, 6); |
| 103 SetFileSizeTo(file_path_origin_b, 3); |
| 104 EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp)); |
| 105 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); |
| 106 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); |
| 107 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); |
| 108 |
| 109 SetFileSizeTo(file_path_origin_a, 1000); |
| 110 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
| 111 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); |
| 112 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); |
| 113 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); |
| 114 } |
OLD | NEW |