1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <algorithm>
#include "apps/saved_files_service.h"
#include "base/files/file_path.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/values_test_util.h"
#include "base/values.h"
#include "chrome/browser/extensions/test_extension_environment.h"
#include "chrome/test/base/testing_profile.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension.h"
#include "testing/gtest/include/gtest/gtest.h"
#define TRACE_CALL(expression) \
do { \
SCOPED_TRACE(#expression); \
expression; \
} while (0)
using apps::SavedFileEntry;
using apps::SavedFilesService;
namespace {
std::string GenerateId(int i) {
return base::IntToString(i) + ":filename.ext";
}
} // namespace
class SavedFilesServiceUnitTest : public testing::Test {
protected:
void SetUp() override {
testing::Test::SetUp();
extension_ = env_.MakeExtension(*base::test::ParseJson(
"{"
" \"app\": {"
" \"background\": {"
" \"scripts\": [\"background.js\"]"
" }"
" },"
" \"permissions\": ["
" {\"fileSystem\": [\"retainEntries\"]}"
" ]"
"}"));
service_ = SavedFilesService::Get(env_.profile());
path_ = base::FilePath(FILE_PATH_LITERAL("filename.ext"));
}
void TearDown() override {
SavedFilesService::ClearMaxSequenceNumberForTest();
SavedFilesService::ClearLruSizeForTest();
testing::Test::TearDown();
}
// Check that a registered file entry has the correct value.
void CheckEntrySequenceNumber(int id, int sequence_number) {
std::string id_string = GenerateId(id);
SCOPED_TRACE(id_string);
EXPECT_TRUE(service_->IsRegistered(extension_->id(), id_string));
const SavedFileEntry* entry =
service_->GetFileEntry(extension_->id(), id_string);
ASSERT_TRUE(entry);
EXPECT_EQ(id_string, entry->id);
EXPECT_EQ(path_, entry->path);
EXPECT_TRUE(entry->is_directory);
EXPECT_EQ(sequence_number, entry->sequence_number);
}
// Check that a range of registered file entries have the correct values.
void CheckRangeEnqueuedInOrder(int start, int end) {
SavedFileEntry entry;
for (int i = start; i < end; i++) {
CheckEntrySequenceNumber(i, i + 1);
}
}
extensions::TestExtensionEnvironment env_;
const extensions::Extension* extension_;
SavedFilesService* service_;
base::FilePath path_;
};
TEST_F(SavedFilesServiceUnitTest, RetainTwoFilesTest) {
service_->RegisterFileEntry(extension_->id(), GenerateId(1), path_, true);
service_->RegisterFileEntry(extension_->id(), GenerateId(2), path_, true);
service_->RegisterFileEntry(extension_->id(), GenerateId(3), path_, true);
// Test that no entry has a sequence number.
TRACE_CALL(CheckEntrySequenceNumber(1, 0));
TRACE_CALL(CheckEntrySequenceNumber(2, 0));
TRACE_CALL(CheckEntrySequenceNumber(3, 0));
// Test that only entry #1 has a sequence number.
service_->EnqueueFileEntry(extension_->id(), GenerateId(1));
TRACE_CALL(CheckEntrySequenceNumber(1, 1));
TRACE_CALL(CheckEntrySequenceNumber(2, 0));
// Test that entry #1 has not changed sequence number because it is the most
// recently enqueued entry.
service_->EnqueueFileEntry(extension_->id(), GenerateId(1));
TRACE_CALL(CheckEntrySequenceNumber(1, 1));
TRACE_CALL(CheckEntrySequenceNumber(2, 0));
// Test that entry #1 is unchanged and entry #2 has been assigned the next
// sequence number.
service_->EnqueueFileEntry(extension_->id(), GenerateId(2));
TRACE_CALL(CheckEntrySequenceNumber(1, 1));
TRACE_CALL(CheckEntrySequenceNumber(2, 2));
// Test that both entries #1 and #2 are unchanged because #2 is the most
// recently enqueued entry.
service_->EnqueueFileEntry(extension_->id(), GenerateId(2));
TRACE_CALL(CheckEntrySequenceNumber(1, 1));
TRACE_CALL(CheckEntrySequenceNumber(2, 2));
// Test that entry #1 has been assigned the next sequence number.
service_->EnqueueFileEntry(extension_->id(), GenerateId(1));
TRACE_CALL(CheckEntrySequenceNumber(1, 3));
TRACE_CALL(CheckEntrySequenceNumber(2, 2));
TRACE_CALL(CheckEntrySequenceNumber(3, 0));
EXPECT_FALSE(service_->IsRegistered(extension_->id(), "another id"));
SavedFileEntry entry;
EXPECT_FALSE(service_->GetFileEntry(extension_->id(), "another id"));
// ClearQueueIfNoRetainPermission should be a no-op because the app has the
// fileSystem.retainEntries permission.
service_->ClearQueueIfNoRetainPermission(extension_);
TRACE_CALL(CheckEntrySequenceNumber(1, 3));
TRACE_CALL(CheckEntrySequenceNumber(2, 2));
TRACE_CALL(CheckEntrySequenceNumber(3, 0));
// Test that after a clear, retained file entries are unchanged, but file
// entries that have been registered but not retained are no longer
// registered.
service_->Clear(extension_->id());
TRACE_CALL(CheckEntrySequenceNumber(1, 3));
TRACE_CALL(CheckEntrySequenceNumber(2, 2));
EXPECT_FALSE(service_->IsRegistered(extension_->id(), GenerateId(3)));
}
TEST_F(SavedFilesServiceUnitTest, NoRetainEntriesPermissionTest) {
extension_ = env_.MakeExtension(*base::test::ParseJson(
"{\"app\": {\"background\": {\"scripts\": [\"background.js\"]}},"
"\"permissions\": [\"fileSystem\"]}"));
service_->RegisterFileEntry(extension_->id(), GenerateId(1), path_, true);
TRACE_CALL(CheckEntrySequenceNumber(1, 0));
SavedFileEntry entry;
service_->EnqueueFileEntry(extension_->id(), GenerateId(1));
TRACE_CALL(CheckEntrySequenceNumber(1, 1));
EXPECT_FALSE(service_->IsRegistered(extension_->id(), "another id"));
EXPECT_FALSE(service_->GetFileEntry(extension_->id(), "another id"));
// ClearQueueIfNoRetainPermission should clear the queue, since the app does
// not have the "retainEntries" permission.
service_->ClearQueueIfNoRetainPermission(extension_);
std::vector<SavedFileEntry> entries =
service_->GetAllFileEntries(extension_->id());
EXPECT_TRUE(entries.empty());
}
TEST_F(SavedFilesServiceUnitTest, EvictionTest) {
SavedFilesService::SetLruSizeForTest(10);
for (int i = 0; i < 10; i++) {
service_->RegisterFileEntry(extension_->id(), GenerateId(i), path_, true);
service_->EnqueueFileEntry(extension_->id(), GenerateId(i));
}
service_->RegisterFileEntry(extension_->id(), GenerateId(10), path_, true);
// Expect that entries 0 to 9 are in the queue, but 10 is not.
TRACE_CALL(CheckRangeEnqueuedInOrder(0, 10));
TRACE_CALL(CheckEntrySequenceNumber(10, 0));
service_->EnqueueFileEntry(extension_->id(), GenerateId(10));
// Expect that entries 1 to 10 are in the queue, but entry 0 is not.
TRACE_CALL(CheckEntrySequenceNumber(0, 0));
TRACE_CALL(CheckRangeEnqueuedInOrder(1, 11));
// Check that retained entries are unchanged after a clear.
service_->Clear(extension_->id());
SavedFileEntry entry;
EXPECT_FALSE(service_->GetFileEntry(extension_->id(), GenerateId(0)));
TRACE_CALL(CheckRangeEnqueuedInOrder(1, 11));
// Expect that entry 2 is now at the back of the queue, and no further entries
// have been evicted.
service_->EnqueueFileEntry(extension_->id(), GenerateId(2));
TRACE_CALL(CheckEntrySequenceNumber(2, 12));
TRACE_CALL(CheckRangeEnqueuedInOrder(1, 1));
TRACE_CALL(CheckRangeEnqueuedInOrder(3, 11));
// Check that retained entries are unchanged after a clear.
service_->Clear(extension_->id());
TRACE_CALL(CheckEntrySequenceNumber(2, 12));
TRACE_CALL(CheckRangeEnqueuedInOrder(1, 1));
TRACE_CALL(CheckRangeEnqueuedInOrder(3, 11));
}
TEST_F(SavedFilesServiceUnitTest, SequenceNumberCompactionTest) {
SavedFilesService::SetMaxSequenceNumberForTest(8);
SavedFilesService::SetLruSizeForTest(8);
for (int i = 0; i < 4; i++) {
service_->RegisterFileEntry(extension_->id(), GenerateId(i), path_, true);
service_->EnqueueFileEntry(extension_->id(), GenerateId(i));
}
service_->EnqueueFileEntry(extension_->id(), GenerateId(2));
service_->EnqueueFileEntry(extension_->id(), GenerateId(3));
service_->EnqueueFileEntry(extension_->id(), GenerateId(2));
// The sequence numbers should be sparse, as they have not gone over the
// limit.
TRACE_CALL(CheckEntrySequenceNumber(0, 1));
TRACE_CALL(CheckEntrySequenceNumber(1, 2));
TRACE_CALL(CheckEntrySequenceNumber(2, 7));
TRACE_CALL(CheckEntrySequenceNumber(3, 6));
service_->Clear(extension_->id());
TRACE_CALL(CheckEntrySequenceNumber(0, 1));
TRACE_CALL(CheckEntrySequenceNumber(1, 2));
TRACE_CALL(CheckEntrySequenceNumber(2, 7));
TRACE_CALL(CheckEntrySequenceNumber(3, 6));
// This should push the sequence number to the limit of 8, and trigger a
// sequence number compaction. Expect that the sequence numbers are
// contiguous from 1 to 4.
service_->EnqueueFileEntry(extension_->id(), GenerateId(3));
TRACE_CALL(CheckRangeEnqueuedInOrder(0, 4));
service_->Clear(extension_->id());
TRACE_CALL(CheckRangeEnqueuedInOrder(0, 4));
}
|