OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "webkit/browser/fileapi/file_system_usage_cache.h" | |
6 | |
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" | |
12 | |
13 namespace fileapi { | |
14 | |
15 class FileSystemUsageCacheTest : public testing::Test { | |
16 public: | |
17 FileSystemUsageCacheTest() | |
18 : usage_cache_(base::MessageLoopProxy::current().get()) {} | |
19 | |
20 virtual void SetUp() { | |
21 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
22 } | |
23 | |
24 protected: | |
25 base::FilePath GetUsageFilePath() { | |
26 return data_dir_.path().Append(FileSystemUsageCache::kUsageFileName); | |
27 } | |
28 | |
29 FileSystemUsageCache* usage_cache() { | |
30 return &usage_cache_; | |
31 } | |
32 | |
33 private: | |
34 base::MessageLoop message_loop_; | |
35 base::ScopedTempDir data_dir_; | |
36 FileSystemUsageCache usage_cache_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(FileSystemUsageCacheTest); | |
39 }; | |
40 | |
41 TEST_F(FileSystemUsageCacheTest, CreateTest) { | |
42 base::FilePath usage_file_path = GetUsageFilePath(); | |
43 EXPECT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 0)); | |
44 } | |
45 | |
46 TEST_F(FileSystemUsageCacheTest, SetSizeTest) { | |
47 static const int64 size = 240122; | |
48 base::FilePath usage_file_path = GetUsageFilePath(); | |
49 int64 usage = 0; | |
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); | |
53 } | |
54 | |
55 TEST_F(FileSystemUsageCacheTest, SetLargeSizeTest) { | |
56 static const int64 size = kint64max; | |
57 base::FilePath usage_file_path = GetUsageFilePath(); | |
58 int64 usage = 0; | |
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); | |
62 } | |
63 | |
64 TEST_F(FileSystemUsageCacheTest, IncAndGetSizeTest) { | |
65 base::FilePath usage_file_path = GetUsageFilePath(); | |
66 uint32 dirty = 0; | |
67 int64 usage = 0; | |
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)); | |
71 EXPECT_EQ(1u, dirty); | |
72 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | |
73 EXPECT_EQ(98214, usage); | |
74 } | |
75 | |
76 TEST_F(FileSystemUsageCacheTest, DecAndGetSizeTest) { | |
77 static const int64 size = 71839; | |
78 base::FilePath usage_file_path = GetUsageFilePath(); | |
79 int64 usage = 0; | |
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); | |
85 } | |
86 | |
87 TEST_F(FileSystemUsageCacheTest, IncDecAndGetSizeTest) { | |
88 static const int64 size = 198491; | |
89 base::FilePath usage_file_path = GetUsageFilePath(); | |
90 int64 usage = 0; | |
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); | |
96 } | |
97 | |
98 TEST_F(FileSystemUsageCacheTest, DecIncAndGetSizeTest) { | |
99 base::FilePath usage_file_path = GetUsageFilePath(); | |
100 uint32 dirty = 0; | |
101 int64 usage = 0; | |
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); | |
112 } | |
113 | |
114 TEST_F(FileSystemUsageCacheTest, ManyIncsSameDecsAndGetSizeTest) { | |
115 static const int64 size = 82412; | |
116 base::FilePath usage_file_path = GetUsageFilePath(); | |
117 int64 usage = 0; | |
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); | |
125 } | |
126 | |
127 TEST_F(FileSystemUsageCacheTest, ManyIncsLessDecsAndGetSizeTest) { | |
128 uint32 dirty = 0; | |
129 int64 usage = 0; | |
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); | |
140 } | |
141 | |
142 TEST_F(FileSystemUsageCacheTest, GetSizeWithoutCacheFileTest) { | |
143 int64 usage = 0; | |
144 base::FilePath usage_file_path = GetUsageFilePath(); | |
145 EXPECT_FALSE(usage_cache()->GetUsage(usage_file_path, &usage)); | |
146 } | |
147 | |
148 TEST_F(FileSystemUsageCacheTest, IncrementDirtyWithoutCacheFileTest) { | |
149 base::FilePath usage_file_path = GetUsageFilePath(); | |
150 EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path)); | |
151 } | |
152 | |
153 TEST_F(FileSystemUsageCacheTest, DecrementDirtyWithoutCacheFileTest) { | |
154 base::FilePath usage_file_path = GetUsageFilePath(); | |
155 EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path)); | |
156 } | |
157 | |
158 } // namespace fileapi | |
OLD | NEW |