1 // Copyright 2014 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.
5 #include "webkit/browser/fileapi/file_system_usage_cache.h"
7 #include "base/basictypes.h"
8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/message_loop/message_loop.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using storage::FileSystemUsageCache
;
17 class FileSystemUsageCacheTest
: public testing::Test
{
19 FileSystemUsageCacheTest()
20 : usage_cache_(base::MessageLoopProxy::current().get()) {}
22 virtual void SetUp() {
23 ASSERT_TRUE(data_dir_
.CreateUniqueTempDir());
27 base::FilePath
GetUsageFilePath() {
28 return data_dir_
.path().Append(FileSystemUsageCache::kUsageFileName
);
31 FileSystemUsageCache
* usage_cache() {
36 base::MessageLoop message_loop_
;
37 base::ScopedTempDir data_dir_
;
38 FileSystemUsageCache usage_cache_
;
40 DISALLOW_COPY_AND_ASSIGN(FileSystemUsageCacheTest
);
43 TEST_F(FileSystemUsageCacheTest
, CreateTest
) {
44 base::FilePath usage_file_path
= GetUsageFilePath();
45 EXPECT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, 0));
48 TEST_F(FileSystemUsageCacheTest
, SetSizeTest
) {
49 static const int64 size
= 240122;
50 base::FilePath usage_file_path
= GetUsageFilePath();
52 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, size
));
53 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
54 EXPECT_EQ(size
, usage
);
57 TEST_F(FileSystemUsageCacheTest
, SetLargeSizeTest
) {
58 static const int64 size
= kint64max
;
59 base::FilePath usage_file_path
= GetUsageFilePath();
61 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, size
));
62 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
63 EXPECT_EQ(size
, usage
);
66 TEST_F(FileSystemUsageCacheTest
, IncAndGetSizeTest
) {
67 base::FilePath usage_file_path
= GetUsageFilePath();
70 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, 98214));
71 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path
));
72 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path
, &dirty
));
74 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
75 EXPECT_EQ(98214, usage
);
78 TEST_F(FileSystemUsageCacheTest
, DecAndGetSizeTest
) {
79 static const int64 size
= 71839;
80 base::FilePath usage_file_path
= GetUsageFilePath();
82 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, size
));
83 // DecrementDirty for dirty = 0 is invalid. It returns false.
84 ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path
));
85 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
86 EXPECT_EQ(size
, usage
);
89 TEST_F(FileSystemUsageCacheTest
, IncDecAndGetSizeTest
) {
90 static const int64 size
= 198491;
91 base::FilePath usage_file_path
= GetUsageFilePath();
93 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, size
));
94 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path
));
95 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path
));
96 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
97 EXPECT_EQ(size
, usage
);
100 TEST_F(FileSystemUsageCacheTest
, DecIncAndGetSizeTest
) {
101 base::FilePath usage_file_path
= GetUsageFilePath();
104 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, 854238));
105 // DecrementDirty for dirty = 0 is invalid. It returns false.
106 ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path
));
107 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path
));
108 // It tests DecrementDirty (which returns false) has no effect, i.e
109 // does not make dirty = -1 after DecrementDirty.
110 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path
, &dirty
));
111 EXPECT_EQ(1u, dirty
);
112 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
113 EXPECT_EQ(854238, usage
);
116 TEST_F(FileSystemUsageCacheTest
, ManyIncsSameDecsAndGetSizeTest
) {
117 static const int64 size
= 82412;
118 base::FilePath usage_file_path
= GetUsageFilePath();
120 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, size
));
121 for (int i
= 0; i
< 20; i
++)
122 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path
));
123 for (int i
= 0; i
< 20; i
++)
124 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path
));
125 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
126 EXPECT_EQ(size
, usage
);
129 TEST_F(FileSystemUsageCacheTest
, ManyIncsLessDecsAndGetSizeTest
) {
132 base::FilePath usage_file_path
= GetUsageFilePath();
133 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, 19319));
134 for (int i
= 0; i
< 20; i
++)
135 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path
));
136 for (int i
= 0; i
< 19; i
++)
137 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path
));
138 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path
, &dirty
));
139 EXPECT_EQ(1u, dirty
);
140 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
141 EXPECT_EQ(19319, usage
);
144 TEST_F(FileSystemUsageCacheTest
, GetSizeWithoutCacheFileTest
) {
146 base::FilePath usage_file_path
= GetUsageFilePath();
147 EXPECT_FALSE(usage_cache()->GetUsage(usage_file_path
, &usage
));
150 TEST_F(FileSystemUsageCacheTest
, IncrementDirtyWithoutCacheFileTest
) {
151 base::FilePath usage_file_path
= GetUsageFilePath();
152 EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path
));
155 TEST_F(FileSystemUsageCacheTest
, DecrementDirtyWithoutCacheFileTest
) {
156 base::FilePath usage_file_path
= GetUsageFilePath();
157 EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path
));
160 } // namespace content