Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: chrome/browser/chromeos/gdata/stale_cache_files_remover_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698