Index: content/browser/in_process_webkit/indexed_db_quota_client_unittest.cc |
diff --git a/content/browser/in_process_webkit/indexed_db_quota_client_unittest.cc b/content/browser/in_process_webkit/indexed_db_quota_client_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f79cd9f173ed9e52b4230a31a0d37d8ee5217e8b |
--- /dev/null |
+++ b/content/browser/in_process_webkit/indexed_db_quota_client_unittest.cc |
@@ -0,0 +1,103 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <map> |
+ |
+#include "base/file_path.h" |
+#include "base/file_util.h" |
+#include "base/memory/scoped_callback_factory.h" |
+#include "base/message_loop.h" |
+#include "base/message_loop_proxy.h" |
+#include "base/utf_string_conversions.h" |
michaeln
2011/05/26 08:40:35
still needed?
dgrogan
2011/05/26 20:56:04
Nope, deleted.
|
+#include "chrome/test/testing_profile.h" |
+#include "content/browser/in_process_webkit/indexed_db_context.h" |
+#include "content/browser/in_process_webkit/indexed_db_quota_client.h" |
+#include "content/browser/in_process_webkit/webkit_context.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "webkit/database/database_tracker.h" |
michaeln
2011/05/26 08:40:35
needed or copy/paste artifact?
dgrogan
2011/05/26 20:56:04
Artifact, apparently; deleted.
|
+#include "webkit/database/database_util.h" |
+ |
+// Declared to shorten the line lengths. |
+static const quota::StorageType kTemp = quota::kStorageTypeTemporary; |
+static const quota::StorageType kPerm = quota::kStorageTypePersistent; |
+ |
+using namespace webkit_database; |
+ |
+// Base class for our test fixtures. |
+class IndexedDBQuotaClientTest : public testing::Test { |
+ public: |
+ const GURL kOriginA; |
+ const GURL kOriginB; |
+ |
+ IndexedDBQuotaClientTest() |
+ : kOriginA("http://host"), |
+ kOriginB("http://host:8000"), |
+ usage_(0), |
+ callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
+ message_loop_(MessageLoop::TYPE_IO) { |
+ TestingProfile profile; |
+ idb_context_ = new IndexedDBContext(profile.GetWebKitContext(), NULL, |
+ NULL, NULL); |
michaeln
2011/05/26 08:40:35
indent is off
dgrogan
2011/05/26 20:56:04
Done.
|
+ } |
+ |
+ int64 GetOriginUsage( |
+ quota::QuotaClient* client, |
+ const GURL& origin, |
+ quota::StorageType type) { |
+ usage_ = 0; |
michaeln
2011/05/26 08:40:35
maybe init to -1 to ensure something happened in t
dgrogan
2011/05/26 20:56:04
Done.
|
+ client->GetOriginUsage(origin, type, |
+ callback_factory_.NewCallback( |
+ &IndexedDBQuotaClientTest::OnGetOriginUsageComplete)); |
+ MessageLoop::current()->RunAllPending(); |
+ return usage_; |
+ } |
+ |
+ IndexedDBContext* idb_context() { return idb_context_.get(); } |
+ |
+ void SetFileSizeTo(const FilePath& path, int size) { |
+ std::string junk(size, 'a'); |
+ ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); |
+ } |
+ |
+ |
+ private: |
+ void OnGetOriginUsageComplete(int64 usage) { |
+ usage_ = usage; |
+ } |
+ |
+ int64 usage_; |
+ scoped_refptr<IndexedDBContext> idb_context_; |
+ base::ScopedCallbackFactory<IndexedDBQuotaClientTest> callback_factory_; |
+ MessageLoop message_loop_; |
+}; |
+ |
+ |
+TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { |
+ IndexedDBQuotaClient client( |
+ base::MessageLoopProxy::CreateForCurrentThread(), |
+ idb_context()); |
+ |
+ ScopedTempDir temp_dir; |
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
+ FilePath indexeddb_dir = temp_dir.path().Append( |
+ IndexedDBContext::kIndexedDBDirectory); |
+ ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); |
+ |
+ idb_context()->set_data_path(indexeddb_dir); |
+ FilePath file_path_origin_a = idb_context()->GetIndexedDBFilePath(DatabaseUtil::GetOriginIdentifier(GURL("http://host/"))); |
michaeln
2011/05/26 08:40:35
you can use kOriginA here and B below
dgrogan
2011/05/26 20:56:04
Done.
|
+ FilePath file_path_origin_b = idb_context()->GetIndexedDBFilePath(DatabaseUtil::GetOriginIdentifier(GURL("http://host:8000/"))); |
michaeln
2011/05/26 08:40:35
nit: line lengths
dgrogan
2011/05/26 20:56:04
Done.
|
+ |
+ SetFileSizeTo(file_path_origin_a, 6); |
+ SetFileSizeTo(file_path_origin_b, 3); |
+ EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp)); |
+ EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); |
+ EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); |
+ EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); |
+ |
+ SetFileSizeTo(file_path_origin_a, 1000); |
+ EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
+ EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); |
+ EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); |
+ EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); |
+} |