Chromium Code Reviews| 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 "base/utf_string_conversions.h" | |
| 13 #include "chrome/test/testing_profile.h" | |
| 14 #include "content/browser/in_process_webkit/indexed_db_context.h" | |
| 15 #include "content/browser/in_process_webkit/indexed_db_quota_client.h" | |
| 16 #include "content/browser/in_process_webkit/webkit_context.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 #include "webkit/database/database_tracker.h" | |
| 19 #include "webkit/database/database_util.h" | |
| 20 | |
| 21 // Declared to shorten the line lengths. | |
| 22 static const quota::StorageType kTemp = quota::kStorageTypeTemporary; | |
| 23 static const quota::StorageType kPerm = quota::kStorageTypePersistent; | |
| 24 | |
| 25 using namespace webkit_database; | |
| 26 | |
| 27 // Base class for our test fixtures. | |
| 28 class IndexedDBQuotaClientTest : public testing::Test { | |
| 29 public: | |
| 30 const GURL kOriginA; | |
| 31 const GURL kOriginB; | |
| 32 | |
| 33 IndexedDBQuotaClientTest() | |
| 34 : kOriginA("http://host"), | |
| 35 kOriginB("http://host:8000"), | |
| 36 usage_(0), | |
| 37 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
| 38 message_loop_(MessageLoop::TYPE_IO) { | |
| 39 TestingProfile profile; | |
| 40 idb_context_ = new IndexedDBContext(profile.GetWebKitContext(), NULL, | |
| 41 NULL, NULL); | |
| 42 } | |
| 43 | |
| 44 int64 GetOriginUsage( | |
| 45 quota::QuotaClient* client, | |
| 46 const GURL& origin, | |
| 47 quota::StorageType type) { | |
| 48 usage_ = 0; | |
| 49 client->GetOriginUsage(origin, type, | |
| 50 callback_factory_.NewCallback( | |
| 51 &IndexedDBQuotaClientTest::OnGetOriginUsageComplete)); | |
| 52 MessageLoop::current()->RunAllPending(); | |
| 53 return usage_; | |
| 54 } | |
| 55 | |
| 56 IndexedDBContext* idb_context() { return idb_context_.get(); } | |
| 57 | |
| 58 void SetFileSizeTo(const FilePath& path, int size) { | |
| 59 char junk[size]; | |
| 60 memset(junk, 'a', size); | |
| 61 ASSERT_EQ(size, file_util::WriteFile(path, junk, size)); | |
| 62 } | |
| 63 | |
| 64 | |
| 65 private: | |
| 66 void OnGetOriginUsageComplete(int64 usage) { | |
| 67 usage_ = usage; | |
| 68 } | |
| 69 | |
| 70 int64 usage_; | |
| 71 scoped_refptr<IndexedDBContext> idb_context_; | |
| 72 base::ScopedCallbackFactory<IndexedDBQuotaClientTest> callback_factory_; | |
| 73 MessageLoop message_loop_; | |
| 74 }; | |
| 75 | |
| 76 | |
| 77 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { | |
| 78 IndexedDBQuotaClient client( | |
| 79 base::MessageLoopProxy::CreateForCurrentThread(), | |
| 80 idb_context()); | |
| 81 | |
| 82 ScopedTempDir temp_dir; | |
| 83 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 84 FilePath indexeddb_dir = temp_dir.path().Append( | |
| 85 IndexedDBContext::kIndexedDBDirectory); | |
| 86 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); | |
| 87 | |
| 88 FilePath::StringType file_name_origin_a(FILE_PATH_LITERAL("http_host_0")); | |
| 89 file_name_origin_a.append(IndexedDBContext::kIndexedDBExtension); | |
| 90 FilePath::StringType file_name_origin_b(FILE_PATH_LITERAL("http_host_8000")); | |
| 91 file_name_origin_b.append(IndexedDBContext::kIndexedDBExtension); | |
| 92 FilePath file_path_origin_a = indexeddb_dir.Append(file_name_origin_a); | |
| 93 FilePath file_path_origin_b = indexeddb_dir.Append(file_name_origin_b); | |
|
michaeln
2011/05/26 03:48:41
could you use idb_context()->GetIndexedDBFilePath(
dgrogan
2011/05/26 05:41:19
Thanks for pointing that out. Switched.
| |
| 94 idb_context()->set_data_path(indexeddb_dir); | |
| 95 | |
| 96 SetFileSizeTo(file_path_origin_a, 6); | |
| 97 SetFileSizeTo(file_path_origin_b, 3); | |
| 98 EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp)); | |
| 99 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); | |
| 100 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); | |
| 101 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); | |
| 102 | |
| 103 SetFileSizeTo(file_path_origin_a, 1000); | |
| 104 EXPECT_EQ(1000, 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 } | |
| OLD | NEW |