OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "apps/saved_files_service.h" | 7 #include "apps/saved_files_service.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/test/values_test_util.h" | 10 #include "base/test/values_test_util.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 // Check that a registered file entry has the correct value. | 64 // Check that a registered file entry has the correct value. |
65 void CheckEntrySequenceNumber(int id, int sequence_number) { | 65 void CheckEntrySequenceNumber(int id, int sequence_number) { |
66 std::string id_string = GenerateId(id); | 66 std::string id_string = GenerateId(id); |
67 SCOPED_TRACE(id_string); | 67 SCOPED_TRACE(id_string); |
68 EXPECT_TRUE(service_->IsRegistered(extension_->id(), id_string)); | 68 EXPECT_TRUE(service_->IsRegistered(extension_->id(), id_string)); |
69 const SavedFileEntry* entry = | 69 const SavedFileEntry* entry = |
70 service_->GetFileEntry(extension_->id(), id_string); | 70 service_->GetFileEntry(extension_->id(), id_string); |
71 ASSERT_TRUE(entry); | 71 ASSERT_TRUE(entry); |
72 EXPECT_EQ(id_string, entry->id); | 72 EXPECT_EQ(id_string, entry->id); |
73 EXPECT_EQ(path_, entry->path); | 73 EXPECT_EQ(path_, entry->path); |
| 74 EXPECT_TRUE(entry->is_directory); |
74 EXPECT_EQ(sequence_number, entry->sequence_number); | 75 EXPECT_EQ(sequence_number, entry->sequence_number); |
75 } | 76 } |
76 | 77 |
77 // Check that a range of registered file entries have the correct values. | 78 // Check that a range of registered file entries have the correct values. |
78 void CheckRangeEnqueuedInOrder(int start, int end) { | 79 void CheckRangeEnqueuedInOrder(int start, int end) { |
79 SavedFileEntry entry; | 80 SavedFileEntry entry; |
80 for (int i = start; i < end; i++) { | 81 for (int i = start; i < end; i++) { |
81 CheckEntrySequenceNumber(i, i + 1); | 82 CheckEntrySequenceNumber(i, i + 1); |
82 } | 83 } |
83 } | 84 } |
84 | 85 |
85 extensions::TestExtensionEnvironment env_; | 86 extensions::TestExtensionEnvironment env_; |
86 const extensions::Extension* extension_; | 87 const extensions::Extension* extension_; |
87 SavedFilesService* service_; | 88 SavedFilesService* service_; |
88 base::FilePath path_; | 89 base::FilePath path_; |
89 }; | 90 }; |
90 | 91 |
91 TEST_F(SavedFilesServiceUnitTest, RetainTwoFilesTest) { | 92 TEST_F(SavedFilesServiceUnitTest, RetainTwoFilesTest) { |
92 service_->RegisterFileEntry(extension_->id(), GenerateId(1), path_); | 93 service_->RegisterFileEntry(extension_->id(), GenerateId(1), path_, true); |
93 service_->RegisterFileEntry(extension_->id(), GenerateId(2), path_); | 94 service_->RegisterFileEntry(extension_->id(), GenerateId(2), path_, true); |
94 service_->RegisterFileEntry(extension_->id(), GenerateId(3), path_); | 95 service_->RegisterFileEntry(extension_->id(), GenerateId(3), path_, true); |
95 | 96 |
96 // Test that no entry has a sequence number. | 97 // Test that no entry has a sequence number. |
97 TRACE_CALL(CheckEntrySequenceNumber(1, 0)); | 98 TRACE_CALL(CheckEntrySequenceNumber(1, 0)); |
98 TRACE_CALL(CheckEntrySequenceNumber(2, 0)); | 99 TRACE_CALL(CheckEntrySequenceNumber(2, 0)); |
99 TRACE_CALL(CheckEntrySequenceNumber(3, 0)); | 100 TRACE_CALL(CheckEntrySequenceNumber(3, 0)); |
100 | 101 |
101 // Test that only entry #1 has a sequence number. | 102 // Test that only entry #1 has a sequence number. |
102 service_->EnqueueFileEntry(extension_->id(), GenerateId(1)); | 103 service_->EnqueueFileEntry(extension_->id(), GenerateId(1)); |
103 TRACE_CALL(CheckEntrySequenceNumber(1, 1)); | 104 TRACE_CALL(CheckEntrySequenceNumber(1, 1)); |
104 TRACE_CALL(CheckEntrySequenceNumber(2, 0)); | 105 TRACE_CALL(CheckEntrySequenceNumber(2, 0)); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 service_->Clear(extension_->id()); | 145 service_->Clear(extension_->id()); |
145 TRACE_CALL(CheckEntrySequenceNumber(1, 3)); | 146 TRACE_CALL(CheckEntrySequenceNumber(1, 3)); |
146 TRACE_CALL(CheckEntrySequenceNumber(2, 2)); | 147 TRACE_CALL(CheckEntrySequenceNumber(2, 2)); |
147 EXPECT_FALSE(service_->IsRegistered(extension_->id(), GenerateId(3))); | 148 EXPECT_FALSE(service_->IsRegistered(extension_->id(), GenerateId(3))); |
148 } | 149 } |
149 | 150 |
150 TEST_F(SavedFilesServiceUnitTest, NoRetainEntriesPermissionTest) { | 151 TEST_F(SavedFilesServiceUnitTest, NoRetainEntriesPermissionTest) { |
151 extension_ = env_.MakeExtension(*base::test::ParseJson( | 152 extension_ = env_.MakeExtension(*base::test::ParseJson( |
152 "{\"app\": {\"background\": {\"scripts\": [\"background.js\"]}}," | 153 "{\"app\": {\"background\": {\"scripts\": [\"background.js\"]}}," |
153 "\"permissions\": [\"fileSystem\"]}")); | 154 "\"permissions\": [\"fileSystem\"]}")); |
154 service_->RegisterFileEntry(extension_->id(), GenerateId(1), path_); | 155 service_->RegisterFileEntry(extension_->id(), GenerateId(1), path_, true); |
155 TRACE_CALL(CheckEntrySequenceNumber(1, 0)); | 156 TRACE_CALL(CheckEntrySequenceNumber(1, 0)); |
156 SavedFileEntry entry; | 157 SavedFileEntry entry; |
157 service_->EnqueueFileEntry(extension_->id(), GenerateId(1)); | 158 service_->EnqueueFileEntry(extension_->id(), GenerateId(1)); |
158 TRACE_CALL(CheckEntrySequenceNumber(1, 1)); | 159 TRACE_CALL(CheckEntrySequenceNumber(1, 1)); |
159 EXPECT_FALSE(service_->IsRegistered(extension_->id(), "another id")); | 160 EXPECT_FALSE(service_->IsRegistered(extension_->id(), "another id")); |
160 EXPECT_FALSE(service_->GetFileEntry(extension_->id(), "another id")); | 161 EXPECT_FALSE(service_->GetFileEntry(extension_->id(), "another id")); |
161 | 162 |
162 // ClearQueueIfNoRetainPermission should clear the queue, since the app does | 163 // ClearQueueIfNoRetainPermission should clear the queue, since the app does |
163 // not have the "retainEntries" permission. | 164 // not have the "retainEntries" permission. |
164 service_->ClearQueueIfNoRetainPermission(extension_); | 165 service_->ClearQueueIfNoRetainPermission(extension_); |
165 std::vector<SavedFileEntry> entries = | 166 std::vector<SavedFileEntry> entries = |
166 service_->GetAllFileEntries(extension_->id()); | 167 service_->GetAllFileEntries(extension_->id()); |
167 EXPECT_TRUE(entries.empty()); | 168 EXPECT_TRUE(entries.empty()); |
168 } | 169 } |
169 | 170 |
170 TEST_F(SavedFilesServiceUnitTest, EvictionTest) { | 171 TEST_F(SavedFilesServiceUnitTest, EvictionTest) { |
171 SavedFilesService::SetLruSizeForTest(10); | 172 SavedFilesService::SetLruSizeForTest(10); |
172 for (int i = 0; i < 10; i++) { | 173 for (int i = 0; i < 10; i++) { |
173 service_->RegisterFileEntry(extension_->id(), GenerateId(i), path_); | 174 service_->RegisterFileEntry(extension_->id(), GenerateId(i), path_, true); |
174 service_->EnqueueFileEntry(extension_->id(), GenerateId(i)); | 175 service_->EnqueueFileEntry(extension_->id(), GenerateId(i)); |
175 } | 176 } |
176 service_->RegisterFileEntry(extension_->id(), GenerateId(10), path_); | 177 service_->RegisterFileEntry(extension_->id(), GenerateId(10), path_, true); |
177 | 178 |
178 // Expect that entries 0 to 9 are in the queue, but 10 is not. | 179 // Expect that entries 0 to 9 are in the queue, but 10 is not. |
179 TRACE_CALL(CheckRangeEnqueuedInOrder(0, 10)); | 180 TRACE_CALL(CheckRangeEnqueuedInOrder(0, 10)); |
180 TRACE_CALL(CheckEntrySequenceNumber(10, 0)); | 181 TRACE_CALL(CheckEntrySequenceNumber(10, 0)); |
181 service_->EnqueueFileEntry(extension_->id(), GenerateId(10)); | 182 service_->EnqueueFileEntry(extension_->id(), GenerateId(10)); |
182 | 183 |
183 // Expect that entries 1 to 10 are in the queue, but entry 0 is not. | 184 // Expect that entries 1 to 10 are in the queue, but entry 0 is not. |
184 TRACE_CALL(CheckEntrySequenceNumber(0, 0)); | 185 TRACE_CALL(CheckEntrySequenceNumber(0, 0)); |
185 TRACE_CALL(CheckRangeEnqueuedInOrder(1, 11)); | 186 TRACE_CALL(CheckRangeEnqueuedInOrder(1, 11)); |
186 | 187 |
(...skipping 14 matching lines...) Expand all Loading... |
201 service_->Clear(extension_->id()); | 202 service_->Clear(extension_->id()); |
202 TRACE_CALL(CheckEntrySequenceNumber(2, 12)); | 203 TRACE_CALL(CheckEntrySequenceNumber(2, 12)); |
203 TRACE_CALL(CheckRangeEnqueuedInOrder(1, 1)); | 204 TRACE_CALL(CheckRangeEnqueuedInOrder(1, 1)); |
204 TRACE_CALL(CheckRangeEnqueuedInOrder(3, 11)); | 205 TRACE_CALL(CheckRangeEnqueuedInOrder(3, 11)); |
205 } | 206 } |
206 | 207 |
207 TEST_F(SavedFilesServiceUnitTest, SequenceNumberCompactionTest) { | 208 TEST_F(SavedFilesServiceUnitTest, SequenceNumberCompactionTest) { |
208 SavedFilesService::SetMaxSequenceNumberForTest(8); | 209 SavedFilesService::SetMaxSequenceNumberForTest(8); |
209 SavedFilesService::SetLruSizeForTest(8); | 210 SavedFilesService::SetLruSizeForTest(8); |
210 for (int i = 0; i < 4; i++) { | 211 for (int i = 0; i < 4; i++) { |
211 service_->RegisterFileEntry(extension_->id(), GenerateId(i), path_); | 212 service_->RegisterFileEntry(extension_->id(), GenerateId(i), path_, true); |
212 service_->EnqueueFileEntry(extension_->id(), GenerateId(i)); | 213 service_->EnqueueFileEntry(extension_->id(), GenerateId(i)); |
213 } | 214 } |
214 service_->EnqueueFileEntry(extension_->id(), GenerateId(2)); | 215 service_->EnqueueFileEntry(extension_->id(), GenerateId(2)); |
215 service_->EnqueueFileEntry(extension_->id(), GenerateId(3)); | 216 service_->EnqueueFileEntry(extension_->id(), GenerateId(3)); |
216 service_->EnqueueFileEntry(extension_->id(), GenerateId(2)); | 217 service_->EnqueueFileEntry(extension_->id(), GenerateId(2)); |
217 | 218 |
218 // The sequence numbers should be sparse, as they have not gone over the | 219 // The sequence numbers should be sparse, as they have not gone over the |
219 // limit. | 220 // limit. |
220 TRACE_CALL(CheckEntrySequenceNumber(0, 1)); | 221 TRACE_CALL(CheckEntrySequenceNumber(0, 1)); |
221 TRACE_CALL(CheckEntrySequenceNumber(1, 2)); | 222 TRACE_CALL(CheckEntrySequenceNumber(1, 2)); |
222 TRACE_CALL(CheckEntrySequenceNumber(2, 7)); | 223 TRACE_CALL(CheckEntrySequenceNumber(2, 7)); |
223 TRACE_CALL(CheckEntrySequenceNumber(3, 6)); | 224 TRACE_CALL(CheckEntrySequenceNumber(3, 6)); |
224 service_->Clear(extension_->id()); | 225 service_->Clear(extension_->id()); |
225 TRACE_CALL(CheckEntrySequenceNumber(0, 1)); | 226 TRACE_CALL(CheckEntrySequenceNumber(0, 1)); |
226 TRACE_CALL(CheckEntrySequenceNumber(1, 2)); | 227 TRACE_CALL(CheckEntrySequenceNumber(1, 2)); |
227 TRACE_CALL(CheckEntrySequenceNumber(2, 7)); | 228 TRACE_CALL(CheckEntrySequenceNumber(2, 7)); |
228 TRACE_CALL(CheckEntrySequenceNumber(3, 6)); | 229 TRACE_CALL(CheckEntrySequenceNumber(3, 6)); |
229 | 230 |
230 // This should push the sequence number to the limit of 8, and trigger a | 231 // This should push the sequence number to the limit of 8, and trigger a |
231 // sequence number compaction. Expect that the sequence numbers are | 232 // sequence number compaction. Expect that the sequence numbers are |
232 // contiguous from 1 to 4. | 233 // contiguous from 1 to 4. |
233 service_->EnqueueFileEntry(extension_->id(), GenerateId(3)); | 234 service_->EnqueueFileEntry(extension_->id(), GenerateId(3)); |
234 TRACE_CALL(CheckRangeEnqueuedInOrder(0, 4)); | 235 TRACE_CALL(CheckRangeEnqueuedInOrder(0, 4)); |
235 service_->Clear(extension_->id()); | 236 service_->Clear(extension_->id()); |
236 TRACE_CALL(CheckRangeEnqueuedInOrder(0, 4)); | 237 TRACE_CALL(CheckRangeEnqueuedInOrder(0, 4)); |
237 } | 238 } |
238 #endif | 239 #endif |
OLD | NEW |