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 "storage/browser/fileapi/file_system_usage_cache.h"
7 #include "base/basictypes.h"
8 #include "base/files/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 void SetUp() override
{ ASSERT_TRUE(data_dir_
.CreateUniqueTempDir()); }
25 base::FilePath
GetUsageFilePath() {
26 return data_dir_
.path().Append(FileSystemUsageCache::kUsageFileName
);
29 FileSystemUsageCache
* usage_cache() {
34 base::MessageLoop message_loop_
;
35 base::ScopedTempDir data_dir_
;
36 FileSystemUsageCache usage_cache_
;
38 DISALLOW_COPY_AND_ASSIGN(FileSystemUsageCacheTest
);
41 TEST_F(FileSystemUsageCacheTest
, CreateTest
) {
42 base::FilePath usage_file_path
= GetUsageFilePath();
43 EXPECT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, 0));
46 TEST_F(FileSystemUsageCacheTest
, SetSizeTest
) {
47 static const int64 size
= 240122;
48 base::FilePath usage_file_path
= GetUsageFilePath();
50 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, size
));
51 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
52 EXPECT_EQ(size
, usage
);
55 TEST_F(FileSystemUsageCacheTest
, SetLargeSizeTest
) {
56 static const int64 size
= kint64max
;
57 base::FilePath usage_file_path
= GetUsageFilePath();
59 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, size
));
60 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
61 EXPECT_EQ(size
, usage
);
64 TEST_F(FileSystemUsageCacheTest
, IncAndGetSizeTest
) {
65 base::FilePath usage_file_path
= GetUsageFilePath();
68 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, 98214));
69 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path
));
70 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path
, &dirty
));
72 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
73 EXPECT_EQ(98214, usage
);
76 TEST_F(FileSystemUsageCacheTest
, DecAndGetSizeTest
) {
77 static const int64 size
= 71839;
78 base::FilePath usage_file_path
= GetUsageFilePath();
80 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, size
));
81 // DecrementDirty for dirty = 0 is invalid. It returns false.
82 ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path
));
83 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
84 EXPECT_EQ(size
, usage
);
87 TEST_F(FileSystemUsageCacheTest
, IncDecAndGetSizeTest
) {
88 static const int64 size
= 198491;
89 base::FilePath usage_file_path
= GetUsageFilePath();
91 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, size
));
92 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path
));
93 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path
));
94 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
95 EXPECT_EQ(size
, usage
);
98 TEST_F(FileSystemUsageCacheTest
, DecIncAndGetSizeTest
) {
99 base::FilePath usage_file_path
= GetUsageFilePath();
102 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, 854238));
103 // DecrementDirty for dirty = 0 is invalid. It returns false.
104 ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path
));
105 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path
));
106 // It tests DecrementDirty (which returns false) has no effect, i.e
107 // does not make dirty = -1 after DecrementDirty.
108 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path
, &dirty
));
109 EXPECT_EQ(1u, dirty
);
110 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
111 EXPECT_EQ(854238, usage
);
114 TEST_F(FileSystemUsageCacheTest
, ManyIncsSameDecsAndGetSizeTest
) {
115 static const int64 size
= 82412;
116 base::FilePath usage_file_path
= GetUsageFilePath();
118 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, size
));
119 for (int i
= 0; i
< 20; i
++)
120 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path
));
121 for (int i
= 0; i
< 20; i
++)
122 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path
));
123 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
124 EXPECT_EQ(size
, usage
);
127 TEST_F(FileSystemUsageCacheTest
, ManyIncsLessDecsAndGetSizeTest
) {
130 base::FilePath usage_file_path
= GetUsageFilePath();
131 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path
, 19319));
132 for (int i
= 0; i
< 20; i
++)
133 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path
));
134 for (int i
= 0; i
< 19; i
++)
135 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path
));
136 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path
, &dirty
));
137 EXPECT_EQ(1u, dirty
);
138 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path
, &usage
));
139 EXPECT_EQ(19319, usage
);
142 TEST_F(FileSystemUsageCacheTest
, GetSizeWithoutCacheFileTest
) {
144 base::FilePath usage_file_path
= GetUsageFilePath();
145 EXPECT_FALSE(usage_cache()->GetUsage(usage_file_path
, &usage
));
148 TEST_F(FileSystemUsageCacheTest
, IncrementDirtyWithoutCacheFileTest
) {
149 base::FilePath usage_file_path
= GetUsageFilePath();
150 EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path
));
153 TEST_F(FileSystemUsageCacheTest
, DecrementDirtyWithoutCacheFileTest
) {
154 base::FilePath usage_file_path
= GetUsageFilePath();
155 EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path
));
158 } // namespace content