OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 #include "webkit/fileapi/file_system_context.h" | 6 #include "webkit/fileapi/file_system_context.h" |
7 #include "webkit/fileapi/file_system_task_runners.h" | 7 #include "webkit/fileapi/file_system_task_runners.h" |
8 #include "webkit/fileapi/file_system_types.h" | 8 #include "webkit/fileapi/file_system_types.h" |
9 #include "webkit/fileapi/isolated_context.h" | 9 #include "webkit/fileapi/isolated_context.h" |
10 #include "webkit/fileapi/local_file_system_operation.h" | 10 #include "webkit/fileapi/local_file_system_operation.h" |
11 #include "webkit/fileapi/syncable/canned_syncable_file_system.h" | 11 #include "webkit/fileapi/syncable/canned_syncable_file_system.h" |
12 #include "webkit/fileapi/syncable/local_file_change_tracker.h" | 12 #include "webkit/fileapi/syncable/local_file_change_tracker.h" |
13 #include "webkit/fileapi/syncable/local_file_sync_context.h" | 13 #include "webkit/fileapi/syncable/local_file_sync_context.h" |
14 #include "webkit/fileapi/syncable/syncable_file_system_util.h" | 14 #include "webkit/fileapi/syncable/syncable_file_system_util.h" |
15 #include "webkit/quota/quota_manager.h" | 15 #include "webkit/quota/quota_manager.h" |
16 #include "webkit/quota/quota_types.h" | 16 #include "webkit/quota/quota_types.h" |
17 | 17 |
18 using base::PlatformFileError; | 18 using base::PlatformFileError; |
19 using quota::QuotaManager; | 19 using quota::QuotaManager; |
20 using quota::QuotaStatusCode; | 20 using quota::QuotaStatusCode; |
21 | 21 |
22 namespace fileapi { | 22 namespace fileapi { |
23 | 23 |
24 class SyncableFileSystemTest : public testing::Test { | 24 class SyncableFileSystemTest : public testing::Test { |
25 public: | 25 public: |
26 SyncableFileSystemTest() | 26 SyncableFileSystemTest() |
27 : file_system_(GURL("http://example.com/"), "test", | 27 : file_system_(GURL("http://example.com/"), "test", |
28 base::MessageLoopProxy::current()), | 28 base::MessageLoopProxy::current()), |
29 quota_status_(quota::kQuotaStatusUnknown), | |
30 usage_(-1), | |
31 quota_(-1), | |
32 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} | 29 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} |
33 | 30 |
34 void SetUp() { | 31 void SetUp() { |
35 file_system_.SetUp(); | 32 file_system_.SetUp(); |
36 | 33 |
37 sync_context_ = new LocalFileSyncContext(base::MessageLoopProxy::current(), | 34 sync_context_ = new LocalFileSyncContext(base::MessageLoopProxy::current(), |
38 base::MessageLoopProxy::current()); | 35 base::MessageLoopProxy::current()); |
39 ASSERT_EQ(SYNC_STATUS_OK, | 36 ASSERT_EQ(SYNC_STATUS_OK, |
40 file_system_.MaybeInitializeFileSystemContext(sync_context_)); | 37 file_system_.MaybeInitializeFileSystemContext(sync_context_)); |
41 } | 38 } |
42 | 39 |
43 void TearDown() { | 40 void TearDown() { |
44 if (sync_context_) | 41 if (sync_context_) |
45 sync_context_->ShutdownOnUIThread(); | 42 sync_context_->ShutdownOnUIThread(); |
46 sync_context_ = NULL; | 43 sync_context_ = NULL; |
47 | 44 |
48 file_system_.TearDown(); | 45 file_system_.TearDown(); |
49 | 46 |
50 // Make sure we don't leave the external filesystem. | 47 // Make sure we don't leave the external filesystem. |
51 // (CannedSyncableFileSystem::TearDown does not do this as there may be | 48 // (CannedSyncableFileSystem::TearDown does not do this as there may be |
52 // multiple syncable file systems registered for the name) | 49 // multiple syncable file systems registered for the name) |
53 RevokeSyncableFileSystem("test"); | 50 RevokeSyncableFileSystem("test"); |
54 } | 51 } |
55 | 52 |
56 void DidGetUsageAndQuota(QuotaStatusCode status, int64 usage, int64 quota) { | |
57 quota_status_ = status; | |
58 usage_ = usage; | |
59 quota_ = quota; | |
60 } | |
61 | |
62 protected: | 53 protected: |
63 void GetUsageAndQuota(int64* usage, int64* quota) { | |
64 quota_status_ = quota::kQuotaStatusUnknown; | |
65 file_system_.quota_manager()->GetUsageAndQuota( | |
66 file_system_.origin(), | |
67 file_system_.storage_type(), | |
68 base::Bind(&SyncableFileSystemTest::DidGetUsageAndQuota, | |
69 weak_factory_.GetWeakPtr())); | |
70 MessageLoop::current()->RunAllPending(); | |
71 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_); | |
72 if (usage) | |
73 *usage = usage_; | |
74 if (quota) | |
75 *quota = quota_; | |
76 } | |
77 | |
78 void VerifyAndClearChange(const FileSystemURL& url, | 54 void VerifyAndClearChange(const FileSystemURL& url, |
79 const FileChange& expected_change) { | 55 const FileChange& expected_change) { |
80 SCOPED_TRACE(testing::Message() << url.DebugString() << | 56 SCOPED_TRACE(testing::Message() << url.DebugString() << |
81 " expecting:" << expected_change.DebugString()); | 57 " expecting:" << expected_change.DebugString()); |
82 // Get the changes for URL and verify. | 58 // Get the changes for URL and verify. |
83 FileChangeList changes; | 59 FileChangeList changes; |
84 change_tracker()->GetChangesForURL(url, &changes); | 60 change_tracker()->GetChangesForURL(url, &changes); |
85 ASSERT_EQ(1U, changes.size()); | 61 ASSERT_EQ(1U, changes.size()); |
86 SCOPED_TRACE(testing::Message() << url.DebugString() << | 62 SCOPED_TRACE(testing::Message() << url.DebugString() << |
87 " actual:" << changes.DebugString()); | 63 " actual:" << changes.DebugString()); |
(...skipping 14 matching lines...) Expand all Loading... |
102 LocalFileChangeTracker* change_tracker() { | 78 LocalFileChangeTracker* change_tracker() { |
103 return file_system_context()->change_tracker(); | 79 return file_system_context()->change_tracker(); |
104 } | 80 } |
105 | 81 |
106 ScopedTempDir data_dir_; | 82 ScopedTempDir data_dir_; |
107 MessageLoop message_loop_; | 83 MessageLoop message_loop_; |
108 | 84 |
109 CannedSyncableFileSystem file_system_; | 85 CannedSyncableFileSystem file_system_; |
110 scoped_refptr<LocalFileSyncContext> sync_context_; | 86 scoped_refptr<LocalFileSyncContext> sync_context_; |
111 | 87 |
112 QuotaStatusCode quota_status_; | |
113 int64 usage_; | |
114 int64 quota_; | |
115 base::WeakPtrFactory<SyncableFileSystemTest> weak_factory_; | 88 base::WeakPtrFactory<SyncableFileSystemTest> weak_factory_; |
116 | 89 |
117 DISALLOW_COPY_AND_ASSIGN(SyncableFileSystemTest); | 90 DISALLOW_COPY_AND_ASSIGN(SyncableFileSystemTest); |
118 }; | 91 }; |
119 | 92 |
120 // Brief combined testing. Just see if all the sandbox feature works. | 93 // Brief combined testing. Just see if all the sandbox feature works. |
121 TEST_F(SyncableFileSystemTest, SyncableLocalSandboxCombined) { | 94 TEST_F(SyncableFileSystemTest, SyncableLocalSandboxCombined) { |
122 // Opens a syncable file system. | 95 // Opens a syncable file system. |
123 EXPECT_EQ(base::PLATFORM_FILE_OK, | 96 EXPECT_EQ(base::PLATFORM_FILE_OK, |
124 file_system_.OpenFileSystem()); | 97 file_system_.OpenFileSystem()); |
125 | 98 |
126 // Do some operations. | 99 // Do some operations. |
127 EXPECT_EQ(base::PLATFORM_FILE_OK, | 100 EXPECT_EQ(base::PLATFORM_FILE_OK, |
128 file_system_.CreateDirectory(URL("dir"))); | 101 file_system_.CreateDirectory(URL("dir"))); |
129 EXPECT_EQ(base::PLATFORM_FILE_OK, | 102 EXPECT_EQ(base::PLATFORM_FILE_OK, |
130 file_system_.CreateFile(URL("dir/foo"))); | 103 file_system_.CreateFile(URL("dir/foo"))); |
131 | 104 |
132 const int64 kOriginalQuota = QuotaManager::kSyncableStorageDefaultHostQuota; | 105 const int64 kOriginalQuota = QuotaManager::kSyncableStorageDefaultHostQuota; |
133 | 106 |
134 const int64 kQuota = 12345 * 1024; | 107 const int64 kQuota = 12345 * 1024; |
135 QuotaManager::kSyncableStorageDefaultHostQuota = kQuota; | 108 QuotaManager::kSyncableStorageDefaultHostQuota = kQuota; |
136 int64 usage, quota; | 109 int64 usage, quota; |
137 GetUsageAndQuota(&usage, "a); | 110 EXPECT_EQ(quota::kQuotaStatusOk, |
| 111 file_system_.GetUsageAndQuota(&usage, "a)); |
138 | 112 |
139 // Returned quota must be what we overrode. Usage must be greater than 0 | 113 // Returned quota must be what we overrode. Usage must be greater than 0 |
140 // as creating a file or directory consumes some space. | 114 // as creating a file or directory consumes some space. |
141 EXPECT_EQ(kQuota, quota); | 115 EXPECT_EQ(kQuota, quota); |
142 EXPECT_GT(usage, 0); | 116 EXPECT_GT(usage, 0); |
143 | 117 |
144 // Truncate to extend an existing file and see if the usage reflects it. | 118 // Truncate to extend an existing file and see if the usage reflects it. |
145 const int64 kFileSizeToExtend = 333; | 119 const int64 kFileSizeToExtend = 333; |
146 EXPECT_EQ(base::PLATFORM_FILE_OK, | 120 EXPECT_EQ(base::PLATFORM_FILE_OK, |
147 file_system_.CreateFile(URL("dir/foo"))); | 121 file_system_.CreateFile(URL("dir/foo"))); |
148 | 122 |
149 EXPECT_EQ(base::PLATFORM_FILE_OK, | 123 EXPECT_EQ(base::PLATFORM_FILE_OK, |
150 file_system_.TruncateFile(URL("dir/foo"), kFileSizeToExtend)); | 124 file_system_.TruncateFile(URL("dir/foo"), kFileSizeToExtend)); |
151 | 125 |
152 int64 new_usage; | 126 int64 new_usage; |
153 GetUsageAndQuota(&new_usage, NULL); | 127 EXPECT_EQ(quota::kQuotaStatusOk, |
| 128 file_system_.GetUsageAndQuota(&new_usage, "a)); |
154 EXPECT_EQ(kFileSizeToExtend, new_usage - usage); | 129 EXPECT_EQ(kFileSizeToExtend, new_usage - usage); |
155 | 130 |
156 // Shrink the quota to the current usage, try to extend the file further | 131 // Shrink the quota to the current usage, try to extend the file further |
157 // and see if it fails. | 132 // and see if it fails. |
158 QuotaManager::kSyncableStorageDefaultHostQuota = new_usage; | 133 QuotaManager::kSyncableStorageDefaultHostQuota = new_usage; |
159 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, | 134 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
160 file_system_.TruncateFile(URL("dir/foo"), kFileSizeToExtend + 1)); | 135 file_system_.TruncateFile(URL("dir/foo"), kFileSizeToExtend + 1)); |
161 | 136 |
162 usage = new_usage; | 137 usage = new_usage; |
163 GetUsageAndQuota(&new_usage, NULL); | 138 EXPECT_EQ(quota::kQuotaStatusOk, |
| 139 file_system_.GetUsageAndQuota(&new_usage, "a)); |
164 EXPECT_EQ(usage, new_usage); | 140 EXPECT_EQ(usage, new_usage); |
165 | 141 |
166 // Deletes the file system. | 142 // Deletes the file system. |
167 EXPECT_EQ(base::PLATFORM_FILE_OK, | 143 EXPECT_EQ(base::PLATFORM_FILE_OK, |
168 file_system_.DeleteFileSystem()); | 144 file_system_.DeleteFileSystem()); |
169 | 145 |
170 // Now the usage must be zero. | 146 // Now the usage must be zero. |
171 GetUsageAndQuota(&usage, NULL); | 147 EXPECT_EQ(quota::kQuotaStatusOk, |
| 148 file_system_.GetUsageAndQuota(&usage, "a)); |
172 EXPECT_EQ(0, usage); | 149 EXPECT_EQ(0, usage); |
173 | 150 |
174 // Restore the system default quota. | 151 // Restore the system default quota. |
175 QuotaManager::kSyncableStorageDefaultHostQuota = kOriginalQuota; | 152 QuotaManager::kSyncableStorageDefaultHostQuota = kOriginalQuota; |
176 } | 153 } |
177 | 154 |
178 // Combined testing with LocalFileChangeTracker. | 155 // Combined testing with LocalFileChangeTracker. |
179 TEST_F(SyncableFileSystemTest, ChangeTrackerSimple) { | 156 TEST_F(SyncableFileSystemTest, ChangeTrackerSimple) { |
180 EXPECT_EQ(base::PLATFORM_FILE_OK, | 157 EXPECT_EQ(base::PLATFORM_FILE_OK, |
181 file_system_.OpenFileSystem()); | 158 file_system_.OpenFileSystem()); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 FileChange::FILE_TYPE_DIRECTORY)); | 225 FileChange::FILE_TYPE_DIRECTORY)); |
249 VerifyAndClearChange(URL(kPath1), | 226 VerifyAndClearChange(URL(kPath1), |
250 FileChange(FileChange::FILE_CHANGE_DELETE, | 227 FileChange(FileChange::FILE_CHANGE_DELETE, |
251 FileChange::FILE_TYPE_DIRECTORY)); | 228 FileChange::FILE_TYPE_DIRECTORY)); |
252 VerifyAndClearChange(URL(kPath2), | 229 VerifyAndClearChange(URL(kPath2), |
253 FileChange(FileChange::FILE_CHANGE_DELETE, | 230 FileChange(FileChange::FILE_CHANGE_DELETE, |
254 FileChange::FILE_TYPE_FILE)); | 231 FileChange::FILE_TYPE_FILE)); |
255 } | 232 } |
256 | 233 |
257 } // namespace fileapi | 234 } // namespace fileapi |
OLD | NEW |