OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "storage/browser/fileapi/file_system_usage_cache.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <limits> | |
10 | |
11 #include "base/files/file_util.h" | |
12 #include "base/files/scoped_temp_dir.h" | |
13 #include "base/macros.h" | |
14 #include "base/message_loop/message_loop.h" | |
15 #include "base/threading/thread_task_runner_handle.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 using storage::FileSystemUsageCache; | |
19 | |
20 namespace content { | |
21 | |
22 class FileSystemUsageCacheTest : public testing::Test { | |
23 public: | |
24 FileSystemUsageCacheTest() | |
25 : usage_cache_(base::ThreadTaskRunnerHandle::Get().get()) {} | |
26 | |
27 void SetUp() override { ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); } | |
28 | |
29 protected: | |
30 base::FilePath GetUsageFilePath() { | |
31 return data_dir_.GetPath().Append(FileSystemUsageCache::kUsageFileName); | |
32 } | |
33 | |
34 FileSystemUsageCache* usage_cache() { | |
35 return &usage_cache_; | |
36 } | |
37 | |
38 private: | |
39 base::MessageLoop message_loop_; | |
40 base::ScopedTempDir data_dir_; | |
41 FileSystemUsageCache usage_cache_; | |
42 | |
43 DISALLOW_COPY_AND_ASSIGN(FileSystemUsageCacheTest); | |
44 }; | |
45 | |
46 TEST_F(FileSystemUsageCacheTest, CreateTest) { | |
47 base::FilePath usage_file_path = GetUsageFilePath(); | |
48 EXPECT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 0)); | |
49 } | |
50 | |
51 TEST_F(FileSystemUsageCacheTest, SetSizeTest) { | |
52 static const int64_t size = 240122; | |
53 base::FilePath usage_file_path = GetUsageFilePath(); | |
54 int64_t usage = 0; | |
55 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); | |
56 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | |
57 EXPECT_EQ(size, usage); | |
58 } | |
59 | |
60 TEST_F(FileSystemUsageCacheTest, SetLargeSizeTest) { | |
61 static const int64_t size = std::numeric_limits<int64_t>::max(); | |
62 base::FilePath usage_file_path = GetUsageFilePath(); | |
63 int64_t usage = 0; | |
64 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); | |
65 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | |
66 EXPECT_EQ(size, usage); | |
67 } | |
68 | |
69 TEST_F(FileSystemUsageCacheTest, IncAndGetSizeTest) { | |
70 base::FilePath usage_file_path = GetUsageFilePath(); | |
71 uint32_t dirty = 0; | |
72 int64_t usage = 0; | |
73 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 98214)); | |
74 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); | |
75 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty)); | |
76 EXPECT_EQ(1u, dirty); | |
77 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | |
78 EXPECT_EQ(98214, usage); | |
79 } | |
80 | |
81 TEST_F(FileSystemUsageCacheTest, DecAndGetSizeTest) { | |
82 static const int64_t size = 71839; | |
83 base::FilePath usage_file_path = GetUsageFilePath(); | |
84 int64_t usage = 0; | |
85 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); | |
86 // DecrementDirty for dirty = 0 is invalid. It returns false. | |
87 ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path)); | |
88 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | |
89 EXPECT_EQ(size, usage); | |
90 } | |
91 | |
92 TEST_F(FileSystemUsageCacheTest, IncDecAndGetSizeTest) { | |
93 static const int64_t size = 198491; | |
94 base::FilePath usage_file_path = GetUsageFilePath(); | |
95 int64_t usage = 0; | |
96 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); | |
97 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); | |
98 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path)); | |
99 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | |
100 EXPECT_EQ(size, usage); | |
101 } | |
102 | |
103 TEST_F(FileSystemUsageCacheTest, DecIncAndGetSizeTest) { | |
104 base::FilePath usage_file_path = GetUsageFilePath(); | |
105 uint32_t dirty = 0; | |
106 int64_t usage = 0; | |
107 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 854238)); | |
108 // DecrementDirty for dirty = 0 is invalid. It returns false. | |
109 ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path)); | |
110 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); | |
111 // It tests DecrementDirty (which returns false) has no effect, i.e | |
112 // does not make dirty = -1 after DecrementDirty. | |
113 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty)); | |
114 EXPECT_EQ(1u, dirty); | |
115 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | |
116 EXPECT_EQ(854238, usage); | |
117 } | |
118 | |
119 TEST_F(FileSystemUsageCacheTest, ManyIncsSameDecsAndGetSizeTest) { | |
120 static const int64_t size = 82412; | |
121 base::FilePath usage_file_path = GetUsageFilePath(); | |
122 int64_t usage = 0; | |
123 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); | |
124 for (int i = 0; i < 20; i++) | |
125 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); | |
126 for (int i = 0; i < 20; i++) | |
127 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path)); | |
128 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | |
129 EXPECT_EQ(size, usage); | |
130 } | |
131 | |
132 TEST_F(FileSystemUsageCacheTest, ManyIncsLessDecsAndGetSizeTest) { | |
133 uint32_t dirty = 0; | |
134 int64_t usage = 0; | |
135 base::FilePath usage_file_path = GetUsageFilePath(); | |
136 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 19319)); | |
137 for (int i = 0; i < 20; i++) | |
138 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); | |
139 for (int i = 0; i < 19; i++) | |
140 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path)); | |
141 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty)); | |
142 EXPECT_EQ(1u, dirty); | |
143 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | |
144 EXPECT_EQ(19319, usage); | |
145 } | |
146 | |
147 TEST_F(FileSystemUsageCacheTest, GetSizeWithoutCacheFileTest) { | |
148 int64_t usage = 0; | |
149 base::FilePath usage_file_path = GetUsageFilePath(); | |
150 EXPECT_FALSE(usage_cache()->GetUsage(usage_file_path, &usage)); | |
151 } | |
152 | |
153 TEST_F(FileSystemUsageCacheTest, IncrementDirtyWithoutCacheFileTest) { | |
154 base::FilePath usage_file_path = GetUsageFilePath(); | |
155 EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path)); | |
156 } | |
157 | |
158 TEST_F(FileSystemUsageCacheTest, DecrementDirtyWithoutCacheFileTest) { | |
159 base::FilePath usage_file_path = GetUsageFilePath(); | |
160 EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path)); | |
161 } | |
162 | |
163 } // namespace content | |
OLD | NEW |