| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "storage/browser/fileapi/file_system_usage_cache.h" | 5 #include "storage/browser/fileapi/file_system_usage_cache.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include <stdint.h> |
| 8 |
| 9 #include <limits> |
| 10 |
| 8 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 11 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 16 |
| 14 using storage::FileSystemUsageCache; | 17 using storage::FileSystemUsageCache; |
| 15 | 18 |
| 16 namespace content { | 19 namespace content { |
| 17 | 20 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 38 | 41 |
| 39 DISALLOW_COPY_AND_ASSIGN(FileSystemUsageCacheTest); | 42 DISALLOW_COPY_AND_ASSIGN(FileSystemUsageCacheTest); |
| 40 }; | 43 }; |
| 41 | 44 |
| 42 TEST_F(FileSystemUsageCacheTest, CreateTest) { | 45 TEST_F(FileSystemUsageCacheTest, CreateTest) { |
| 43 base::FilePath usage_file_path = GetUsageFilePath(); | 46 base::FilePath usage_file_path = GetUsageFilePath(); |
| 44 EXPECT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 0)); | 47 EXPECT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 0)); |
| 45 } | 48 } |
| 46 | 49 |
| 47 TEST_F(FileSystemUsageCacheTest, SetSizeTest) { | 50 TEST_F(FileSystemUsageCacheTest, SetSizeTest) { |
| 48 static const int64 size = 240122; | 51 static const int64_t size = 240122; |
| 49 base::FilePath usage_file_path = GetUsageFilePath(); | 52 base::FilePath usage_file_path = GetUsageFilePath(); |
| 50 int64 usage = 0; | 53 int64_t usage = 0; |
| 51 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); | 54 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); |
| 52 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | 55 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); |
| 53 EXPECT_EQ(size, usage); | 56 EXPECT_EQ(size, usage); |
| 54 } | 57 } |
| 55 | 58 |
| 56 TEST_F(FileSystemUsageCacheTest, SetLargeSizeTest) { | 59 TEST_F(FileSystemUsageCacheTest, SetLargeSizeTest) { |
| 57 static const int64 size = kint64max; | 60 static const int64_t size = std::numeric_limits<int64_t>::max(); |
| 58 base::FilePath usage_file_path = GetUsageFilePath(); | 61 base::FilePath usage_file_path = GetUsageFilePath(); |
| 59 int64 usage = 0; | 62 int64_t usage = 0; |
| 60 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); | 63 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); |
| 61 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | 64 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); |
| 62 EXPECT_EQ(size, usage); | 65 EXPECT_EQ(size, usage); |
| 63 } | 66 } |
| 64 | 67 |
| 65 TEST_F(FileSystemUsageCacheTest, IncAndGetSizeTest) { | 68 TEST_F(FileSystemUsageCacheTest, IncAndGetSizeTest) { |
| 66 base::FilePath usage_file_path = GetUsageFilePath(); | 69 base::FilePath usage_file_path = GetUsageFilePath(); |
| 67 uint32 dirty = 0; | 70 uint32_t dirty = 0; |
| 68 int64 usage = 0; | 71 int64_t usage = 0; |
| 69 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 98214)); | 72 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 98214)); |
| 70 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); | 73 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); |
| 71 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty)); | 74 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty)); |
| 72 EXPECT_EQ(1u, dirty); | 75 EXPECT_EQ(1u, dirty); |
| 73 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | 76 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); |
| 74 EXPECT_EQ(98214, usage); | 77 EXPECT_EQ(98214, usage); |
| 75 } | 78 } |
| 76 | 79 |
| 77 TEST_F(FileSystemUsageCacheTest, DecAndGetSizeTest) { | 80 TEST_F(FileSystemUsageCacheTest, DecAndGetSizeTest) { |
| 78 static const int64 size = 71839; | 81 static const int64_t size = 71839; |
| 79 base::FilePath usage_file_path = GetUsageFilePath(); | 82 base::FilePath usage_file_path = GetUsageFilePath(); |
| 80 int64 usage = 0; | 83 int64_t usage = 0; |
| 81 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); | 84 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); |
| 82 // DecrementDirty for dirty = 0 is invalid. It returns false. | 85 // DecrementDirty for dirty = 0 is invalid. It returns false. |
| 83 ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path)); | 86 ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path)); |
| 84 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | 87 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); |
| 85 EXPECT_EQ(size, usage); | 88 EXPECT_EQ(size, usage); |
| 86 } | 89 } |
| 87 | 90 |
| 88 TEST_F(FileSystemUsageCacheTest, IncDecAndGetSizeTest) { | 91 TEST_F(FileSystemUsageCacheTest, IncDecAndGetSizeTest) { |
| 89 static const int64 size = 198491; | 92 static const int64_t size = 198491; |
| 90 base::FilePath usage_file_path = GetUsageFilePath(); | 93 base::FilePath usage_file_path = GetUsageFilePath(); |
| 91 int64 usage = 0; | 94 int64_t usage = 0; |
| 92 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); | 95 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); |
| 93 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); | 96 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); |
| 94 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path)); | 97 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path)); |
| 95 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | 98 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); |
| 96 EXPECT_EQ(size, usage); | 99 EXPECT_EQ(size, usage); |
| 97 } | 100 } |
| 98 | 101 |
| 99 TEST_F(FileSystemUsageCacheTest, DecIncAndGetSizeTest) { | 102 TEST_F(FileSystemUsageCacheTest, DecIncAndGetSizeTest) { |
| 100 base::FilePath usage_file_path = GetUsageFilePath(); | 103 base::FilePath usage_file_path = GetUsageFilePath(); |
| 101 uint32 dirty = 0; | 104 uint32_t dirty = 0; |
| 102 int64 usage = 0; | 105 int64_t usage = 0; |
| 103 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 854238)); | 106 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 854238)); |
| 104 // DecrementDirty for dirty = 0 is invalid. It returns false. | 107 // DecrementDirty for dirty = 0 is invalid. It returns false. |
| 105 ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path)); | 108 ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path)); |
| 106 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); | 109 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); |
| 107 // It tests DecrementDirty (which returns false) has no effect, i.e | 110 // It tests DecrementDirty (which returns false) has no effect, i.e |
| 108 // does not make dirty = -1 after DecrementDirty. | 111 // does not make dirty = -1 after DecrementDirty. |
| 109 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty)); | 112 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty)); |
| 110 EXPECT_EQ(1u, dirty); | 113 EXPECT_EQ(1u, dirty); |
| 111 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | 114 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); |
| 112 EXPECT_EQ(854238, usage); | 115 EXPECT_EQ(854238, usage); |
| 113 } | 116 } |
| 114 | 117 |
| 115 TEST_F(FileSystemUsageCacheTest, ManyIncsSameDecsAndGetSizeTest) { | 118 TEST_F(FileSystemUsageCacheTest, ManyIncsSameDecsAndGetSizeTest) { |
| 116 static const int64 size = 82412; | 119 static const int64_t size = 82412; |
| 117 base::FilePath usage_file_path = GetUsageFilePath(); | 120 base::FilePath usage_file_path = GetUsageFilePath(); |
| 118 int64 usage = 0; | 121 int64_t usage = 0; |
| 119 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); | 122 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); |
| 120 for (int i = 0; i < 20; i++) | 123 for (int i = 0; i < 20; i++) |
| 121 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); | 124 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); |
| 122 for (int i = 0; i < 20; i++) | 125 for (int i = 0; i < 20; i++) |
| 123 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path)); | 126 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path)); |
| 124 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | 127 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); |
| 125 EXPECT_EQ(size, usage); | 128 EXPECT_EQ(size, usage); |
| 126 } | 129 } |
| 127 | 130 |
| 128 TEST_F(FileSystemUsageCacheTest, ManyIncsLessDecsAndGetSizeTest) { | 131 TEST_F(FileSystemUsageCacheTest, ManyIncsLessDecsAndGetSizeTest) { |
| 129 uint32 dirty = 0; | 132 uint32_t dirty = 0; |
| 130 int64 usage = 0; | 133 int64_t usage = 0; |
| 131 base::FilePath usage_file_path = GetUsageFilePath(); | 134 base::FilePath usage_file_path = GetUsageFilePath(); |
| 132 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 19319)); | 135 ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 19319)); |
| 133 for (int i = 0; i < 20; i++) | 136 for (int i = 0; i < 20; i++) |
| 134 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); | 137 ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); |
| 135 for (int i = 0; i < 19; i++) | 138 for (int i = 0; i < 19; i++) |
| 136 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path)); | 139 ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path)); |
| 137 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty)); | 140 EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty)); |
| 138 EXPECT_EQ(1u, dirty); | 141 EXPECT_EQ(1u, dirty); |
| 139 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); | 142 EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); |
| 140 EXPECT_EQ(19319, usage); | 143 EXPECT_EQ(19319, usage); |
| 141 } | 144 } |
| 142 | 145 |
| 143 TEST_F(FileSystemUsageCacheTest, GetSizeWithoutCacheFileTest) { | 146 TEST_F(FileSystemUsageCacheTest, GetSizeWithoutCacheFileTest) { |
| 144 int64 usage = 0; | 147 int64_t usage = 0; |
| 145 base::FilePath usage_file_path = GetUsageFilePath(); | 148 base::FilePath usage_file_path = GetUsageFilePath(); |
| 146 EXPECT_FALSE(usage_cache()->GetUsage(usage_file_path, &usage)); | 149 EXPECT_FALSE(usage_cache()->GetUsage(usage_file_path, &usage)); |
| 147 } | 150 } |
| 148 | 151 |
| 149 TEST_F(FileSystemUsageCacheTest, IncrementDirtyWithoutCacheFileTest) { | 152 TEST_F(FileSystemUsageCacheTest, IncrementDirtyWithoutCacheFileTest) { |
| 150 base::FilePath usage_file_path = GetUsageFilePath(); | 153 base::FilePath usage_file_path = GetUsageFilePath(); |
| 151 EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path)); | 154 EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path)); |
| 152 } | 155 } |
| 153 | 156 |
| 154 TEST_F(FileSystemUsageCacheTest, DecrementDirtyWithoutCacheFileTest) { | 157 TEST_F(FileSystemUsageCacheTest, DecrementDirtyWithoutCacheFileTest) { |
| 155 base::FilePath usage_file_path = GetUsageFilePath(); | 158 base::FilePath usage_file_path = GetUsageFilePath(); |
| 156 EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path)); | 159 EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path)); |
| 157 } | 160 } |
| 158 | 161 |
| 159 } // namespace content | 162 } // namespace content |
| OLD | NEW |