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 <map> | 5 #include <map> |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/memory/scoped_callback_factory.h" | 9 #include "base/memory/scoped_callback_factory.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
12 #include "chrome/test/testing_profile.h" | 12 #include "chrome/test/testing_profile.h" |
13 #include "content/browser/in_process_webkit/indexed_db_context.h" | 13 #include "content/browser/in_process_webkit/indexed_db_context.h" |
14 #include "content/browser/in_process_webkit/indexed_db_quota_client.h" | 14 #include "content/browser/in_process_webkit/indexed_db_quota_client.h" |
15 #include "content/browser/in_process_webkit/webkit_context.h" | 15 #include "content/browser/in_process_webkit/webkit_context.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "webkit/database/database_util.h" | 17 #include "webkit/database/database_util.h" |
18 | 18 |
19 // Declared to shorten the line lengths. | 19 // Declared to shorten the line lengths. |
20 static const quota::StorageType kTemp = quota::kStorageTypeTemporary; | 20 static const quota::StorageType kTemp = quota::kStorageTypeTemporary; |
21 static const quota::StorageType kPerm = quota::kStorageTypePersistent; | 21 static const quota::StorageType kPerm = quota::kStorageTypePersistent; |
22 | 22 |
23 using namespace webkit_database; | 23 using namespace webkit_database; |
24 | 24 |
25 // Base class for our test fixtures. | 25 // Base class for our test fixtures. |
26 class IndexedDBQuotaClientTest : public testing::Test { | 26 class IndexedDBQuotaClientTest : public testing::Test { |
27 public: | 27 public: |
28 const GURL kOriginA; | 28 const GURL kOriginA; |
29 const GURL kOriginB; | 29 const GURL kOriginB; |
| 30 const GURL kOriginOther; |
30 | 31 |
31 IndexedDBQuotaClientTest() | 32 IndexedDBQuotaClientTest() |
32 : kOriginA("http://host"), | 33 : kOriginA("http://host"), |
33 kOriginB("http://host:8000"), | 34 kOriginB("http://host:8000"), |
| 35 kOriginOther("http://other"), |
34 usage_(0), | 36 usage_(0), |
35 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 37 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
36 message_loop_(MessageLoop::TYPE_IO), | 38 message_loop_(MessageLoop::TYPE_IO), |
37 webkit_thread_(BrowserThread::WEBKIT, &message_loop_) { | 39 webkit_thread_(BrowserThread::WEBKIT, &message_loop_) { |
38 TestingProfile profile; | 40 TestingProfile profile; |
39 idb_context_ = profile.GetWebKitContext()->indexed_db_context(); | 41 idb_context_ = profile.GetWebKitContext()->indexed_db_context(); |
40 setup_temp_dir(); | 42 setup_temp_dir(); |
41 } | 43 } |
42 void setup_temp_dir() { | 44 void setup_temp_dir() { |
43 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 45 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
(...skipping 17 matching lines...) Expand all Loading... |
61 quota::StorageType type) { | 63 quota::StorageType type) { |
62 usage_ = -1; | 64 usage_ = -1; |
63 client->GetOriginUsage(origin, type, | 65 client->GetOriginUsage(origin, type, |
64 callback_factory_.NewCallback( | 66 callback_factory_.NewCallback( |
65 &IndexedDBQuotaClientTest::OnGetOriginUsageComplete)); | 67 &IndexedDBQuotaClientTest::OnGetOriginUsageComplete)); |
66 MessageLoop::current()->RunAllPending(); | 68 MessageLoop::current()->RunAllPending(); |
67 EXPECT_GT(usage_, -1); | 69 EXPECT_GT(usage_, -1); |
68 return usage_; | 70 return usage_; |
69 } | 71 } |
70 | 72 |
| 73 const std::set<GURL>& GetOriginsForType( |
| 74 quota::QuotaClient* client, |
| 75 quota::StorageType type) { |
| 76 origins_.clear(); |
| 77 client->GetOriginsForType(type, |
| 78 callback_factory_.NewCallback( |
| 79 &IndexedDBQuotaClientTest::OnGetOriginsComplete)); |
| 80 MessageLoop::current()->RunAllPending(); |
| 81 return origins_; |
| 82 } |
| 83 |
| 84 const std::set<GURL>& GetOriginsForHost( |
| 85 quota::QuotaClient* client, |
| 86 quota::StorageType type, |
| 87 const std::string& host) { |
| 88 origins_.clear(); |
| 89 client->GetOriginsForHost(type, host, |
| 90 callback_factory_.NewCallback( |
| 91 &IndexedDBQuotaClientTest::OnGetOriginsComplete)); |
| 92 MessageLoop::current()->RunAllPending(); |
| 93 return origins_; |
| 94 } |
| 95 |
71 IndexedDBContext* idb_context() { return idb_context_.get(); } | 96 IndexedDBContext* idb_context() { return idb_context_.get(); } |
72 | 97 |
73 void SetFileSizeTo(const FilePath& path, int size) { | 98 void SetFileSizeTo(const FilePath& path, int size) { |
74 std::string junk(size, 'a'); | 99 std::string junk(size, 'a'); |
75 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); | 100 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); |
76 } | 101 } |
77 | 102 |
78 void AddFakeIndexedDB(const GURL& origin, int size) { | 103 void AddFakeIndexedDB(const GURL& origin, int size) { |
79 FilePath file_path_origin = idb_context()->GetIndexedDBFilePath( | 104 FilePath file_path_origin = idb_context()->GetIndexedDBFilePath( |
80 DatabaseUtil::GetOriginIdentifier(origin)); | 105 DatabaseUtil::GetOriginIdentifier(origin)); |
81 if (!file_util::CreateDirectory(file_path_origin)) { | 106 if (!file_util::CreateDirectory(file_path_origin)) { |
82 LOG(ERROR) << "failed to file_util::CreateDirectory " | 107 LOG(ERROR) << "failed to file_util::CreateDirectory " |
83 << file_path_origin.value(); | 108 << file_path_origin.value(); |
84 } | 109 } |
85 file_path_origin = file_path_origin.Append(FILE_PATH_LITERAL("fake_file")); | 110 file_path_origin = file_path_origin.Append(FILE_PATH_LITERAL("fake_file")); |
86 SetFileSizeTo(file_path_origin, size); | 111 SetFileSizeTo(file_path_origin, size); |
87 } | 112 } |
88 | 113 |
89 private: | 114 private: |
90 void OnGetOriginUsageComplete(int64 usage) { | 115 void OnGetOriginUsageComplete(int64 usage) { |
91 usage_ = usage; | 116 usage_ = usage; |
92 } | 117 } |
93 | 118 |
| 119 void OnGetOriginsComplete(const std::set<GURL>& origins) { |
| 120 origins_ = origins; |
| 121 } |
| 122 |
94 ScopedTempDir temp_dir_; | 123 ScopedTempDir temp_dir_; |
95 int64 usage_; | 124 int64 usage_; |
| 125 std::set<GURL> origins_; |
96 scoped_refptr<IndexedDBContext> idb_context_; | 126 scoped_refptr<IndexedDBContext> idb_context_; |
97 base::ScopedCallbackFactory<IndexedDBQuotaClientTest> callback_factory_; | 127 base::ScopedCallbackFactory<IndexedDBQuotaClientTest> callback_factory_; |
98 MessageLoop message_loop_; | 128 MessageLoop message_loop_; |
99 BrowserThread webkit_thread_; | 129 BrowserThread webkit_thread_; |
100 }; | 130 }; |
101 | 131 |
102 | 132 |
103 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { | 133 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { |
104 IndexedDBQuotaClient client( | 134 IndexedDBQuotaClient client( |
105 base::MessageLoopProxy::CreateForCurrentThread(), | 135 base::MessageLoopProxy::CreateForCurrentThread(), |
106 idb_context()); | 136 idb_context()); |
107 | 137 |
108 AddFakeIndexedDB(kOriginA, 6); | 138 AddFakeIndexedDB(kOriginA, 6); |
109 AddFakeIndexedDB(kOriginB, 3); | 139 AddFakeIndexedDB(kOriginB, 3); |
110 EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp)); | 140 EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp)); |
111 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); | 141 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); |
112 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); | 142 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); |
113 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); | 143 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); |
114 | 144 |
115 AddFakeIndexedDB(kOriginA, 1000); | 145 AddFakeIndexedDB(kOriginA, 1000); |
116 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); | 146 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
117 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); | 147 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); |
118 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); | 148 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); |
119 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); | 149 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); |
120 } | 150 } |
| 151 |
| 152 TEST_F(IndexedDBQuotaClientTest, GetOriginsForHost) { |
| 153 IndexedDBQuotaClient client( |
| 154 base::MessageLoopProxy::CreateForCurrentThread(), |
| 155 idb_context()); |
| 156 |
| 157 EXPECT_EQ(kOriginA.host(), kOriginB.host()); |
| 158 EXPECT_NE(kOriginA.host(), kOriginOther.host()); |
| 159 |
| 160 std::set<GURL> origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
| 161 EXPECT_TRUE(origins.empty()); |
| 162 |
| 163 AddFakeIndexedDB(kOriginA, 1000); |
| 164 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
| 165 EXPECT_EQ(origins.size(), 1ul); |
| 166 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
| 167 |
| 168 AddFakeIndexedDB(kOriginB, 1000); |
| 169 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
| 170 EXPECT_EQ(origins.size(), 2ul); |
| 171 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
| 172 EXPECT_TRUE(origins.find(kOriginB) != origins.end()); |
| 173 |
| 174 EXPECT_TRUE(GetOriginsForHost(&client, kPerm, kOriginA.host()).empty()); |
| 175 EXPECT_TRUE(GetOriginsForHost(&client, kTemp, kOriginOther.host()).empty()); |
| 176 } |
| 177 |
| 178 TEST_F(IndexedDBQuotaClientTest, GetOriginsForType) { |
| 179 IndexedDBQuotaClient client( |
| 180 base::MessageLoopProxy::CreateForCurrentThread(), |
| 181 idb_context()); |
| 182 |
| 183 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty()); |
| 184 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); |
| 185 |
| 186 AddFakeIndexedDB(kOriginA, 1000); |
| 187 std::set<GURL> origins = GetOriginsForType(&client, kTemp); |
| 188 EXPECT_EQ(origins.size(), 1ul); |
| 189 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
| 190 |
| 191 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); |
| 192 } |
OLD | NEW |