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 <string> | |
6 #include <vector> | |
7 | |
8 #include "base/bind.h" | |
9 #include "base/file_path.h" | |
10 #include "base/file_util.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/message_loop.h" | |
13 #include "base/stringprintf.h" | |
14 #include "base/threading/sequenced_worker_pool.h" | |
15 #include "base/values.h" | |
16 #include "chrome/browser/chromeos/cros/cros_library.h" | |
17 #include "chrome/browser/chromeos/gdata/drive.pb.h" | |
18 #include "chrome/browser/chromeos/gdata/drive_api_parser.h" | |
19 #include "chrome/browser/chromeos/gdata/drive_file_system.h" | |
20 #include "chrome/browser/chromeos/gdata/drive_webapps_registry.h" | |
21 #include "chrome/browser/chromeos/gdata/gdata_test_util.h" | |
22 #include "chrome/browser/chromeos/gdata/gdata_uploader.h" | |
23 #include "chrome/browser/chromeos/gdata/gdata_util.h" | |
24 #include "chrome/browser/chromeos/gdata/mock_directory_change_observer.h" | |
25 #include "chrome/browser/chromeos/gdata/mock_drive_cache_observer.h" | |
26 #include "chrome/browser/chromeos/gdata/mock_drive_service.h" | |
27 #include "chrome/browser/chromeos/gdata/mock_drive_web_apps_registry.h" | |
28 #include "chrome/browser/chromeos/gdata/mock_free_disk_space_getter.h" | |
29 #include "chrome/browser/chromeos/gdata/mock_gdata_uploader.h" | |
30 #include "chrome/browser/chromeos/gdata/stale_cache_files_remover.h" | |
31 #include "chrome/test/base/testing_profile.h" | |
32 #include "content/public/browser/browser_thread.h" | |
33 #include "content/public/test/test_browser_thread.h" | |
34 #include "testing/gmock/include/gmock/gmock.h" | |
35 #include "testing/gtest/include/gtest/gtest.h" | |
36 | |
37 using ::testing::AtLeast; | |
38 using ::testing::Eq; | |
39 using ::testing::NotNull; | |
40 using ::testing::Return; | |
41 using ::testing::StrictMock; | |
42 using ::testing::_; | |
43 | |
44 namespace gdata { | |
45 namespace { | |
46 | |
47 const int64 kLotsOfSpace = kMinFreeSpace * 10; | |
48 | |
49 // Callback for DriveCache::StoreOnUIThread used in RemoveStaleCacheFiles test. | |
50 // Verifies that the result is not an error. | |
51 void VerifyCacheFileState(DriveFileError error, | |
52 const std::string& resource_id, | |
53 const std::string& md5) { | |
54 EXPECT_EQ(DRIVE_FILE_OK, error); | |
55 } | |
56 | |
57 } // namespace | |
58 | |
59 class StaleCacheFilesRemoverTest : public testing::Test { | |
60 protected: | |
61 StaleCacheFilesRemoverTest() | |
62 : ui_thread_(content::BrowserThread::UI, &message_loop_), | |
63 io_thread_(content::BrowserThread::IO), | |
64 cache_(NULL), | |
65 file_system_(NULL), | |
66 mock_drive_service_(NULL), | |
67 mock_webapps_registry_(NULL), | |
68 root_feed_changestamp_(0) { | |
69 } | |
70 | |
71 virtual void SetUp() OVERRIDE { | |
72 chromeos::CrosLibrary::Initialize(true /* use_stub */); | |
satorux1
2012/08/30 04:38:15
Why is this needed? please add a comment.
yoshiki
2012/08/30 15:23:45
I realize it is unnecessarily at this test. Delete
| |
73 io_thread_.StartIOThread(); | |
74 | |
75 profile_.reset(new TestingProfile); | |
76 | |
77 // Allocate and keep a pointer to the mock, and inject it into the | |
78 // DriveFileSystem object, which will own the mock object. | |
79 mock_drive_service_ = new StrictMock<MockDriveService>; | |
80 | |
81 EXPECT_CALL(*mock_drive_service_, Initialize(profile_.get())).Times(1); | |
82 | |
83 // Likewise, this will be owned by DriveFileSystem. | |
84 mock_free_disk_space_checker_ = new StrictMock<MockFreeDiskSpaceGetter>; | |
85 SetFreeDiskSpaceGetterForTesting(mock_free_disk_space_checker_); | |
86 | |
87 scoped_refptr<base::SequencedWorkerPool> pool = | |
88 content::BrowserThread::GetBlockingPool(); | |
89 blocking_task_runner_ = | |
90 pool->GetSequencedTaskRunner(pool->GetSequenceToken()); | |
91 | |
92 cache_ = DriveCache::CreateDriveCacheOnUIThread( | |
93 DriveCache::GetCacheRootPath(profile_.get()), blocking_task_runner_); | |
94 | |
95 mock_uploader_.reset(new StrictMock<MockGDataUploader>); | |
96 mock_webapps_registry_.reset(new StrictMock<MockDriveWebAppsRegistry>); | |
97 | |
98 ASSERT_FALSE(file_system_); | |
99 file_system_ = new DriveFileSystem(profile_.get(), | |
100 cache_, | |
101 mock_drive_service_, | |
102 mock_uploader_.get(), | |
103 mock_webapps_registry_.get(), | |
104 blocking_task_runner_); | |
105 | |
106 mock_cache_observer_.reset(new StrictMock<MockDriveCacheObserver>); | |
107 cache_->AddObserver(mock_cache_observer_.get()); | |
108 | |
109 mock_directory_observer_.reset(new StrictMock<MockDirectoryChangeObserver>); | |
110 file_system_->AddObserver(mock_directory_observer_.get()); | |
111 | |
112 file_system_->Initialize(); | |
113 cache_->RequestInitializeOnUIThreadForTesting(); | |
114 | |
115 stale_cache_files_remover_.reset(new StaleCacheFilesRemover(file_system_, | |
116 cache_)); | |
117 | |
118 test_util::RunBlockingPoolTask(); | |
119 } | |
120 | |
121 virtual void TearDown() OVERRIDE { | |
122 ASSERT_TRUE(file_system_); | |
123 stale_cache_files_remover_.reset(); | |
124 EXPECT_CALL(*mock_drive_service_, CancelAll()).Times(1); | |
125 delete file_system_; | |
126 file_system_ = NULL; | |
127 delete mock_drive_service_; | |
128 mock_drive_service_ = NULL; | |
129 SetFreeDiskSpaceGetterForTesting(NULL); | |
130 cache_->DestroyOnUIThread(); | |
131 // The cache destruction requires to post a task to the blocking pool. | |
132 test_util::RunBlockingPoolTask(); | |
133 | |
134 profile_.reset(NULL); | |
135 chromeos::CrosLibrary::Shutdown(); | |
136 } | |
137 | |
138 // Loads test json file as root ("/drive") element. | |
139 void LoadRootFeedDocument(const std::string& filename) { | |
140 test_util::LoadChangeFeed(filename, | |
141 file_system_, | |
142 0, | |
143 root_feed_changestamp_++); | |
144 } | |
145 | |
146 MessageLoopForUI message_loop_; | |
147 // The order of the test threads is important, do not change the order. | |
148 // See also content/browser/browser_thread_impl.cc. | |
149 content::TestBrowserThread ui_thread_; | |
150 content::TestBrowserThread io_thread_; | |
151 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | |
152 scoped_ptr<TestingProfile> profile_; | |
153 DriveCache* cache_; | |
154 scoped_ptr<StrictMock<MockGDataUploader> > mock_uploader_; | |
155 DriveFileSystem* file_system_; | |
156 StrictMock<MockDriveService>* mock_drive_service_; | |
157 scoped_ptr<StrictMock<MockDriveWebAppsRegistry> > mock_webapps_registry_; | |
158 StrictMock<MockFreeDiskSpaceGetter>* mock_free_disk_space_checker_; | |
159 scoped_ptr<StrictMock<MockDriveCacheObserver> > mock_cache_observer_; | |
160 scoped_ptr<StrictMock<MockDirectoryChangeObserver> > mock_directory_observer_; | |
161 scoped_ptr<StaleCacheFilesRemover> stale_cache_files_remover_; | |
162 | |
163 int root_feed_changestamp_; | |
164 }; | |
165 | |
166 TEST_F(StaleCacheFilesRemoverTest, RemoveStaleCacheFiles) { | |
167 FilePath dummy_file = test_util::GetTestFilePath("root_feed.json"); | |
168 std::string resource_id("pdf:1a2b3c"); | |
169 std::string md5("abcdef0123456789"); | |
170 | |
171 EXPECT_CALL(*mock_free_disk_space_checker_, AmountOfFreeDiskSpace()) | |
172 .Times(AtLeast(1)).WillRepeatedly(Return(kLotsOfSpace)); | |
173 | |
174 // Create a stale cache file. | |
175 cache_->StoreOnUIThread(resource_id, md5, dummy_file, | |
176 DriveCache::FILE_OPERATION_COPY, | |
177 base::Bind(&gdata::VerifyCacheFileState)); | |
178 test_util::RunBlockingPoolTask(); | |
179 | |
180 // Verify that the cache file exists. | |
181 FilePath path = cache_->GetCacheFilePath(resource_id, | |
182 md5, | |
183 DriveCache::CACHE_TYPE_TMP, | |
184 DriveCache::CACHED_FILE_FROM_SERVER); | |
185 EXPECT_TRUE(file_util::PathExists(path)); | |
186 | |
187 // Verify that the corresponding file entry doesn't exist. | |
188 EXPECT_CALL(*mock_drive_service_, GetAccountMetadata(_)).Times(1); | |
189 EXPECT_CALL(*mock_drive_service_, GetDocuments(Eq(GURL()), _, "", _, _)) | |
190 .Times(1); | |
191 EXPECT_CALL(*mock_webapps_registry_, UpdateFromFeed(_)).Times(1); | |
192 | |
193 DriveFileError error(DRIVE_FILE_OK); | |
194 FilePath unused; | |
195 scoped_ptr<DriveEntryProto> entry_proto; | |
196 file_system_->GetEntryInfoByResourceId( | |
197 resource_id, | |
198 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, | |
199 &error, | |
200 &unused, | |
201 &entry_proto)); | |
202 test_util::RunBlockingPoolTask(); | |
203 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); | |
204 | |
205 file_system_->GetEntryInfoByPath( | |
206 path, | |
207 base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback, | |
208 &error, | |
209 &entry_proto)); | |
210 test_util::RunBlockingPoolTask(); | |
211 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); | |
212 EXPECT_FALSE(entry_proto.get()); | |
213 | |
214 // Load a root feed. | |
215 LoadRootFeedDocument("root_feed.json"); | |
216 | |
217 // Wait for StaleCacheFilesRemover to finish cleaning up the stale file. | |
218 test_util::RunBlockingPoolTask(); | |
219 | |
220 // Verify that the cache file is deleted. | |
221 path = cache_->GetCacheFilePath(resource_id, | |
222 md5, | |
223 DriveCache::CACHE_TYPE_TMP, | |
224 DriveCache::CACHED_FILE_FROM_SERVER); | |
225 EXPECT_FALSE(file_util::PathExists(path)); | |
226 } | |
227 | |
228 } // namespace gdata | |
OLD | NEW |