Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: content/browser/in_process_webkit/indexed_db_quota_client_unittest.cc

Issue 7053030: Initial IndexedDBQuotaClient implementation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: custom struct for refptr to use on Destruct Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 TestingProfile profile;
38 idb_context_ = new IndexedDBContext(profile.GetWebKitContext(), NULL,
39 NULL, NULL);
40 }
41
42 int64 GetOriginUsage(
43 quota::QuotaClient* client,
44 const GURL& origin,
45 quota::StorageType type) {
46 usage_ = -1;
47 client->GetOriginUsage(origin, type,
48 callback_factory_.NewCallback(
49 &IndexedDBQuotaClientTest::OnGetOriginUsageComplete));
50 MessageLoop::current()->RunAllPending();
51 EXPECT_GT(usage_, -1);
52 return usage_;
53 }
54
55 IndexedDBContext* idb_context() { return idb_context_.get(); }
56
57 void SetFileSizeTo(const FilePath& path, int size) {
58 std::string junk(size, 'a');
59 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size));
60 }
61
62
63 private:
64 void OnGetOriginUsageComplete(int64 usage) {
65 usage_ = usage;
66 }
67
68 int64 usage_;
69 scoped_refptr<IndexedDBContext> idb_context_;
70 base::ScopedCallbackFactory<IndexedDBQuotaClientTest> callback_factory_;
71 MessageLoop message_loop_;
72 };
73
74
75 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) {
76 IndexedDBQuotaClient client(
77 base::MessageLoopProxy::CreateForCurrentThread(),
78 idb_context());
79
80 ScopedTempDir temp_dir;
81 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
82 FilePath indexeddb_dir = temp_dir.path().Append(
83 IndexedDBContext::kIndexedDBDirectory);
84 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir));
85
86 idb_context()->set_data_path(indexeddb_dir);
87 FilePath file_path_origin_a = idb_context()->GetIndexedDBFilePath(
88 DatabaseUtil::GetOriginIdentifier(kOriginA));
89 FilePath file_path_origin_b = idb_context()->GetIndexedDBFilePath(
90 DatabaseUtil::GetOriginIdentifier(kOriginB));
91
92 SetFileSizeTo(file_path_origin_a, 6);
93 SetFileSizeTo(file_path_origin_b, 3);
94 EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp));
95 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm));
96 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp));
97 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm));
98
99 SetFileSizeTo(file_path_origin_a, 1000);
100 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp));
101 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm));
102 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp));
103 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm));
104 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698