Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / fileapi / file_system_usage_cache_unittest.cc
blobf67485c8c9026374140ddcca032d31466f89c1ba
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 "base/thread_task_runner_handle.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using storage::FileSystemUsageCache;
16 namespace content {
18 class FileSystemUsageCacheTest : public testing::Test {
19 public:
20 FileSystemUsageCacheTest()
21 : usage_cache_(base::ThreadTaskRunnerHandle::Get().get()) {}
23 void SetUp() override { ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); }
25 protected:
26 base::FilePath GetUsageFilePath() {
27 return data_dir_.path().Append(FileSystemUsageCache::kUsageFileName);
30 FileSystemUsageCache* usage_cache() {
31 return &usage_cache_;
34 private:
35 base::MessageLoop message_loop_;
36 base::ScopedTempDir data_dir_;
37 FileSystemUsageCache usage_cache_;
39 DISALLOW_COPY_AND_ASSIGN(FileSystemUsageCacheTest);
42 TEST_F(FileSystemUsageCacheTest, CreateTest) {
43 base::FilePath usage_file_path = GetUsageFilePath();
44 EXPECT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 0));
47 TEST_F(FileSystemUsageCacheTest, SetSizeTest) {
48 static const int64 size = 240122;
49 base::FilePath usage_file_path = GetUsageFilePath();
50 int64 usage = 0;
51 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size));
52 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
53 EXPECT_EQ(size, usage);
56 TEST_F(FileSystemUsageCacheTest, SetLargeSizeTest) {
57 static const int64 size = kint64max;
58 base::FilePath usage_file_path = GetUsageFilePath();
59 int64 usage = 0;
60 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size));
61 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
62 EXPECT_EQ(size, usage);
65 TEST_F(FileSystemUsageCacheTest, IncAndGetSizeTest) {
66 base::FilePath usage_file_path = GetUsageFilePath();
67 uint32 dirty = 0;
68 int64 usage = 0;
69 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 98214));
70 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path));
71 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty));
72 EXPECT_EQ(1u, dirty);
73 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
74 EXPECT_EQ(98214, usage);
77 TEST_F(FileSystemUsageCacheTest, DecAndGetSizeTest) {
78 static const int64 size = 71839;
79 base::FilePath usage_file_path = GetUsageFilePath();
80 int64 usage = 0;
81 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size));
82 // DecrementDirty for dirty = 0 is invalid. It returns false.
83 ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path));
84 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
85 EXPECT_EQ(size, usage);
88 TEST_F(FileSystemUsageCacheTest, IncDecAndGetSizeTest) {
89 static const int64 size = 198491;
90 base::FilePath usage_file_path = GetUsageFilePath();
91 int64 usage = 0;
92 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size));
93 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path));
94 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path));
95 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
96 EXPECT_EQ(size, usage);
99 TEST_F(FileSystemUsageCacheTest, DecIncAndGetSizeTest) {
100 base::FilePath usage_file_path = GetUsageFilePath();
101 uint32 dirty = 0;
102 int64 usage = 0;
103 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 854238));
104 // DecrementDirty for dirty = 0 is invalid. It returns false.
105 ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path));
106 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path));
107 // It tests DecrementDirty (which returns false) has no effect, i.e
108 // does not make dirty = -1 after DecrementDirty.
109 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty));
110 EXPECT_EQ(1u, dirty);
111 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
112 EXPECT_EQ(854238, usage);
115 TEST_F(FileSystemUsageCacheTest, ManyIncsSameDecsAndGetSizeTest) {
116 static const int64 size = 82412;
117 base::FilePath usage_file_path = GetUsageFilePath();
118 int64 usage = 0;
119 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size));
120 for (int i = 0; i < 20; i++)
121 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path));
122 for (int i = 0; i < 20; i++)
123 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path));
124 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
125 EXPECT_EQ(size, usage);
128 TEST_F(FileSystemUsageCacheTest, ManyIncsLessDecsAndGetSizeTest) {
129 uint32 dirty = 0;
130 int64 usage = 0;
131 base::FilePath usage_file_path = GetUsageFilePath();
132 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 19319));
133 for (int i = 0; i < 20; i++)
134 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path));
135 for (int i = 0; i < 19; i++)
136 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path));
137 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty));
138 EXPECT_EQ(1u, dirty);
139 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage));
140 EXPECT_EQ(19319, usage);
143 TEST_F(FileSystemUsageCacheTest, GetSizeWithoutCacheFileTest) {
144 int64 usage = 0;
145 base::FilePath usage_file_path = GetUsageFilePath();
146 EXPECT_FALSE(usage_cache()->GetUsage(usage_file_path, &usage));
149 TEST_F(FileSystemUsageCacheTest, IncrementDirtyWithoutCacheFileTest) {
150 base::FilePath usage_file_path = GetUsageFilePath();
151 EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path));
154 TEST_F(FileSystemUsageCacheTest, DecrementDirtyWithoutCacheFileTest) {
155 base::FilePath usage_file_path = GetUsageFilePath();
156 EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path));
159 } // namespace content