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

Side by Side Diff: chrome/browser/chromeos/drive/drive_scheduler_unittest.cc

Issue 11819046: drive: Replace local FakeDriveService with google_apis::FakeDriveService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 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
« no previous file with comments | « no previous file | chrome/browser/chromeos/drive/stale_cache_files_remover_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "chrome/browser/chromeos/drive/drive_scheduler.h" 5 #include "chrome/browser/chromeos/drive/drive_scheduler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/threading/sequenced_worker_pool.h" 10 #include "base/threading/sequenced_worker_pool.h"
11 #include "chrome/browser/chromeos/drive/drive_test_util.h" 11 #include "chrome/browser/chromeos/drive/drive_test_util.h"
12 #include "chrome/browser/google_apis/dummy_drive_service.h" 12 #include "chrome/browser/google_apis/fake_drive_service.h"
13 #include "chrome/browser/google_apis/gdata_wapi_parser.h" 13 #include "chrome/browser/google_apis/gdata_wapi_parser.h"
14 #include "chrome/browser/prefs/pref_service.h" 14 #include "chrome/browser/prefs/pref_service.h"
15 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/testing_profile.h" 16 #include "chrome/test/base/testing_profile.h"
17 #include "content/public/test/test_browser_thread.h" 17 #include "content/public/test/test_browser_thread.h"
18 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 20
21 using ::testing::AnyNumber; 21 using ::testing::AnyNumber;
22 using ::testing::DoAll; 22 using ::testing::DoAll;
23 using ::testing::Eq; 23 using ::testing::Eq;
24 using ::testing::Return; 24 using ::testing::Return;
25 using ::testing::StrictMock; 25 using ::testing::StrictMock;
26 using ::testing::_; 26 using ::testing::_;
27 27
28 namespace drive { 28 namespace drive {
29 29
30 namespace { 30 namespace {
31 31
32 class FakeDriveService : public google_apis::DummyDriveService {
33 virtual void GetResourceList(
34 const GURL& feed_url,
35 int64 start_changestamp,
36 const std::string& search_query,
37 bool shared_with_me,
38 const std::string& directory_resource_id,
39 const google_apis::GetResourceListCallback& callback) OVERRIDE {
40 // TODO: Make this more flexible.
41 if (feed_url == GURL("http://example.com/gdata/root_feed.json")) {
42 // Make some sample data.
43 scoped_ptr<base::Value> feed_data = google_apis::test_util::LoadJSONFile(
44 "gdata/root_feed.json");
45 scoped_ptr<google_apis::ResourceList> resource_list =
46 google_apis::ResourceList::ExtractAndParse(*feed_data);
47 base::MessageLoopProxy::current()->PostTask(FROM_HERE,
48 base::Bind(callback,
49 google_apis::HTTP_SUCCESS,
50 base::Passed(&resource_list)));
51 } else {
52 scoped_ptr<google_apis::ResourceList> resource_list;
53 base::MessageLoopProxy::current()->PostTask(FROM_HERE,
54 base::Bind(callback,
55 google_apis::GDATA_PARSE_ERROR,
56 base::Passed(&resource_list)));
57 }
58 }
59
60 virtual void GetAccountMetadata(
61 const google_apis::GetAccountMetadataCallback& callback) OVERRIDE {
62 // The contents don't matter here, so just return empty metadata.
63 scoped_ptr<google_apis::AccountMetadataFeed> account_metadata(
64 new google_apis::AccountMetadataFeed);
65
66 base::MessageLoopProxy::current()->PostTask(FROM_HERE,
67 base::Bind(callback,
68 google_apis::HTTP_SUCCESS,
69 base::Passed(&account_metadata)));
70 }
71
72 virtual void GetApplicationInfo(
73 const google_apis::GetDataCallback& callback) OVERRIDE {
74 // The contents don't matter here, so just return an empty dictionary.
75 scoped_ptr<base::Value> data(new base::DictionaryValue);
76
77 base::MessageLoopProxy::current()->PostTask(FROM_HERE,
78 base::Bind(callback,
79 google_apis::HTTP_SUCCESS,
80 base::Passed(&data)));
81 }
82
83 virtual void DeleteResource(
84 const GURL& edit_url,
85 const google_apis::EntryActionCallback& callback) {
86 if (edit_url == GURL("/feeds/default/private/full/some_file"))
87 callback.Run(google_apis::HTTP_SUCCESS);
88 else
89 callback.Run(google_apis::HTTP_NOT_FOUND);
90 }
91
92 virtual void GetResourceEntry(
93 const std::string& resource_id,
94 const google_apis::GetResourceEntryCallback& callback) {
95 if (resource_id == "file:2_file_resource_id") {
96 scoped_ptr<Value> data = google_apis::test_util::LoadJSONFile(
97 "gdata/file_entry.json");
98
99 // Parsing ResourceEntry is cheap enough to do on UI thread.
100 scoped_ptr<google_apis::ResourceEntry> entry =
101 google_apis::ResourceEntry::ExtractAndParse(*data);
102 if (!entry) {
103 callback.Run(google_apis::GDATA_PARSE_ERROR,
104 scoped_ptr<google_apis::ResourceEntry>());
105 return;
106 }
107
108 callback.Run(google_apis::HTTP_SUCCESS, entry.Pass());
109 } else {
110 callback.Run(google_apis::HTTP_NOT_FOUND,
111 scoped_ptr<google_apis::ResourceEntry>());
112 }
113 }
114
115 virtual void CopyHostedDocument(
116 const std::string& resource_id,
117 const FilePath::StringType& new_name,
118 const google_apis::GetResourceEntryCallback& callback) {
119 if (resource_id == "file:2_file_resource_id") {
120 scoped_ptr<Value> data = google_apis::test_util::LoadJSONFile(
121 "gdata/file_entry.json");
122
123 // Parsing ResourceEntry is cheap enough to do on UI thread.
124 scoped_ptr<google_apis::ResourceEntry> entry =
125 google_apis::ResourceEntry::ExtractAndParse(*data);
126 if (!entry) {
127 callback.Run(google_apis::GDATA_PARSE_ERROR,
128 scoped_ptr<google_apis::ResourceEntry>());
129 return;
130 }
131
132 callback.Run(google_apis::HTTP_SUCCESS, entry.Pass());
133 } else {
134 callback.Run(google_apis::HTTP_NOT_FOUND,
135 scoped_ptr<google_apis::ResourceEntry>());
136 }
137 }
138
139 virtual void AddNewDirectory(
140 const GURL& parent_content_url,
141 const FilePath::StringType& directory_name,
142 const google_apis::GetResourceEntryCallback& callback) {
143 scoped_ptr<Value> data = google_apis::test_util::LoadJSONFile(
144 "gdata/directory_entry.json");
145
146 // Parsing ResourceEntry is cheap enough to do on UI thread.
147 scoped_ptr<google_apis::ResourceEntry> entry =
148 google_apis::ResourceEntry::ExtractAndParse(*data);
149 if (!entry) {
150 callback.Run(google_apis::GDATA_PARSE_ERROR,
151 scoped_ptr<google_apis::ResourceEntry>());
152 return;
153 }
154
155 callback.Run(google_apis::HTTP_SUCCESS, entry.Pass());
156 }
157
158 virtual void RenameResource(
159 const GURL& edit_url,
160 const FilePath::StringType& new_name,
161 const google_apis::EntryActionCallback& callback) {
162 if (edit_url == GURL("/feeds/default/private/full/some_file"))
163 callback.Run(google_apis::HTTP_SUCCESS);
164 else
165 callback.Run(google_apis::HTTP_NOT_FOUND);
166 }
167
168 virtual void AddResourceToDirectory(
169 const GURL& parent_content_url,
170 const GURL& edit_url,
171 const google_apis::EntryActionCallback& callback) {
172 if (parent_content_url ==
173 GURL("/feeds/default/private/full/some_directory") &&
174 edit_url == GURL("/feeds/default/private/full/some_file"))
175 callback.Run(google_apis::HTTP_SUCCESS);
176 else
177 callback.Run(google_apis::HTTP_NOT_FOUND);
178 }
179
180 virtual void RemoveResourceFromDirectory(
181 const GURL& parent_content_url,
182 const std::string& resource_id,
183 const google_apis::EntryActionCallback& callback) {
184 if (parent_content_url ==
185 GURL("/feeds/default/private/full/some_directory") &&
186 resource_id == "file:2_file_resource_id")
187 callback.Run(google_apis::HTTP_SUCCESS);
188 else
189 callback.Run(google_apis::HTTP_NOT_FOUND);
190 }
191
192 };
193
194 class MockNetworkChangeNotifier : public net::NetworkChangeNotifier { 32 class MockNetworkChangeNotifier : public net::NetworkChangeNotifier {
195 public: 33 public:
196 MOCK_CONST_METHOD0(GetCurrentConnectionType, 34 MOCK_CONST_METHOD0(GetCurrentConnectionType,
197 net::NetworkChangeNotifier::ConnectionType()); 35 net::NetworkChangeNotifier::ConnectionType());
198 }; 36 };
199 37
200 } // namespace 38 } // namespace
201 39
202 class DriveSchedulerTest : public testing::Test { 40 class DriveSchedulerTest : public testing::Test {
203 public: 41 public:
204 DriveSchedulerTest() 42 DriveSchedulerTest()
205 : ui_thread_(content::BrowserThread::UI, &message_loop_), 43 : ui_thread_(content::BrowserThread::UI, &message_loop_),
206 profile_(new TestingProfile) { 44 profile_(new TestingProfile) {
207 } 45 }
208 46
209 virtual void SetUp() OVERRIDE { 47 virtual void SetUp() OVERRIDE {
210 mock_network_change_notifier_.reset(new MockNetworkChangeNotifier); 48 mock_network_change_notifier_.reset(new MockNetworkChangeNotifier);
211 49
212 fake_drive_service_.reset(new FakeDriveService()); 50 fake_drive_service_.reset(new google_apis::FakeDriveService());
51 fake_drive_service_->LoadResourceListForWapi(
52 "gdata/root_feed.json");
53 fake_drive_service_->LoadAccountMetadataForWapi(
54 "gdata/account_metadata.json");
55 fake_drive_service_->LoadApplicationInfoForDriveApi(
56 "drive/applist.json");
57
213 scheduler_.reset(new DriveScheduler(profile_.get(), 58 scheduler_.reset(new DriveScheduler(profile_.get(),
214 fake_drive_service_.get())); 59 fake_drive_service_.get()));
215 60
216 scheduler_->Initialize(); 61 scheduler_->Initialize();
217 scheduler_->SetDisableThrottling(true); 62 scheduler_->SetDisableThrottling(true);
218 } 63 }
219 64
220 virtual void TearDown() OVERRIDE { 65 virtual void TearDown() OVERRIDE {
221 // The scheduler should be deleted before NetworkLibrary, as it 66 // The scheduler should be deleted before NetworkLibrary, as it
222 // registers itself as observer of NetworkLibrary. 67 // registers itself as observer of NetworkLibrary.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 void ConnectToNone() { 101 void ConnectToNone() {
257 ChangeConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE); 102 ChangeConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE);
258 } 103 }
259 104
260 protected: 105 protected:
261 MessageLoopForUI message_loop_; 106 MessageLoopForUI message_loop_;
262 content::TestBrowserThread ui_thread_; 107 content::TestBrowserThread ui_thread_;
263 scoped_ptr<TestingProfile> profile_; 108 scoped_ptr<TestingProfile> profile_;
264 scoped_ptr<DriveScheduler> scheduler_; 109 scoped_ptr<DriveScheduler> scheduler_;
265 scoped_ptr<MockNetworkChangeNotifier> mock_network_change_notifier_; 110 scoped_ptr<MockNetworkChangeNotifier> mock_network_change_notifier_;
266 scoped_ptr<FakeDriveService> fake_drive_service_; 111 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_;
267 }; 112 };
268 113
269 TEST_F(DriveSchedulerTest, GetApplicationInfo) { 114 TEST_F(DriveSchedulerTest, GetApplicationInfo) {
270 ConnectToWifi(); 115 ConnectToWifi();
271 116
272 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; 117 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
273 scoped_ptr<base::Value> value; 118 scoped_ptr<base::Value> value;
274 119
275 scheduler_->GetApplicationInfo( 120 scheduler_->GetApplicationInfo(
276 base::Bind(&google_apis::test_util::CopyResultsFromGetDataCallback, 121 base::Bind(&google_apis::test_util::CopyResultsFromGetDataCallback,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 ASSERT_EQ(google_apis::HTTP_SUCCESS, error); 184 ASSERT_EQ(google_apis::HTTP_SUCCESS, error);
340 ASSERT_TRUE(entry); 185 ASSERT_TRUE(entry);
341 } 186 }
342 187
343 TEST_F(DriveSchedulerTest, DeleteResource) { 188 TEST_F(DriveSchedulerTest, DeleteResource) {
344 ConnectToWifi(); 189 ConnectToWifi();
345 190
346 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; 191 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
347 192
348 scheduler_->DeleteResource( 193 scheduler_->DeleteResource(
349 GURL("/feeds/default/private/full/some_file"), 194 GURL("https://file1_link_self/file:2_file_resource_id"),
350 base::Bind( 195 base::Bind(
351 &google_apis::test_util::CopyResultsFromEntryActionCallback, 196 &google_apis::test_util::CopyResultsFromEntryActionCallback,
352 &error)); 197 &error));
353 google_apis::test_util::RunBlockingPoolTask(); 198 google_apis::test_util::RunBlockingPoolTask();
354 199
355 ASSERT_EQ(google_apis::HTTP_SUCCESS, error); 200 ASSERT_EQ(google_apis::HTTP_SUCCESS, error);
356 } 201 }
357 202
358 TEST_F(DriveSchedulerTest, CopyHostedDocument) { 203 TEST_F(DriveSchedulerTest, CopyHostedDocument) {
359 ConnectToWifi(); 204 ConnectToWifi();
360 205
361 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; 206 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
362 scoped_ptr<google_apis::ResourceEntry> entry; 207 scoped_ptr<google_apis::ResourceEntry> entry;
363 208
364 scheduler_->CopyHostedDocument( 209 scheduler_->CopyHostedDocument(
365 "file:2_file_resource_id", // resource ID 210 "document:5_document_resource_id", // resource ID
366 FILE_PATH_LITERAL("New Document"), // new name 211 FILE_PATH_LITERAL("New Document"), // new name
367 base::Bind( 212 base::Bind(
368 &google_apis::test_util::CopyResultsFromGetResourceEntryCallback, 213 &google_apis::test_util::CopyResultsFromGetResourceEntryCallback,
369 &error, 214 &error,
370 &entry)); 215 &entry));
371 google_apis::test_util::RunBlockingPoolTask(); 216 google_apis::test_util::RunBlockingPoolTask();
372 217
373 ASSERT_EQ(google_apis::HTTP_SUCCESS, error); 218 ASSERT_EQ(google_apis::HTTP_SUCCESS, error);
374 ASSERT_TRUE(entry); 219 ASSERT_TRUE(entry);
375 } 220 }
376 221
377 TEST_F(DriveSchedulerTest, RenameResource) { 222 TEST_F(DriveSchedulerTest, RenameResource) {
378 ConnectToWifi(); 223 ConnectToWifi();
379 224
380 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; 225 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
381 226
382 scheduler_->RenameResource( 227 scheduler_->RenameResource(
383 GURL("/feeds/default/private/full/some_file"), 228 GURL("https://file1_link_self/file:2_file_resource_id"),
384 FILE_PATH_LITERAL("New Name"), 229 FILE_PATH_LITERAL("New Name"),
385 base::Bind( 230 base::Bind(
386 &google_apis::test_util::CopyResultsFromEntryActionCallback, 231 &google_apis::test_util::CopyResultsFromEntryActionCallback,
387 &error)); 232 &error));
388 google_apis::test_util::RunBlockingPoolTask(); 233 google_apis::test_util::RunBlockingPoolTask();
389 234
390 ASSERT_EQ(google_apis::HTTP_SUCCESS, error); 235 ASSERT_EQ(google_apis::HTTP_SUCCESS, error);
391 } 236 }
392 237
393 TEST_F(DriveSchedulerTest, AddResourceToDirectory) { 238 TEST_F(DriveSchedulerTest, AddResourceToDirectory) {
394 ConnectToWifi(); 239 ConnectToWifi();
395 240
396 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; 241 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
397 242
398 scheduler_->AddResourceToDirectory( 243 scheduler_->AddResourceToDirectory(
399 GURL("/feeds/default/private/full/some_directory"), 244 GURL("https://dir_1_self_link/folder:1_folder_resource_id"),
400 GURL("/feeds/default/private/full/some_file"), 245 GURL("https://file1_link_self/file:2_file_resource_id"),
401 base::Bind( 246 base::Bind(
402 &google_apis::test_util::CopyResultsFromEntryActionCallback, 247 &google_apis::test_util::CopyResultsFromEntryActionCallback,
403 &error)); 248 &error));
404 google_apis::test_util::RunBlockingPoolTask(); 249 google_apis::test_util::RunBlockingPoolTask();
405 250
406 ASSERT_EQ(google_apis::HTTP_SUCCESS, error); 251 ASSERT_EQ(google_apis::HTTP_SUCCESS, error);
407 } 252 }
408 253
409 TEST_F(DriveSchedulerTest, RemoveResourceFromDirectory) { 254 TEST_F(DriveSchedulerTest, RemoveResourceFromDirectory) {
410 ConnectToWifi(); 255 ConnectToWifi();
411 256
412 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; 257 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
413 258
414 scheduler_->RemoveResourceFromDirectory( 259 scheduler_->RemoveResourceFromDirectory(
415 GURL("/feeds/default/private/full/some_directory"), 260 GURL("https://dir_1_self_link/folder:1_folder_resource_id"),
416 "file:2_file_resource_id", // resource ID 261 "file:subdirectory_file_1_id", // resource ID
417 base::Bind( 262 base::Bind(
418 &google_apis::test_util::CopyResultsFromEntryActionCallback, 263 &google_apis::test_util::CopyResultsFromEntryActionCallback,
419 &error)); 264 &error));
420 google_apis::test_util::RunBlockingPoolTask(); 265 google_apis::test_util::RunBlockingPoolTask();
421 266
422 ASSERT_EQ(google_apis::HTTP_SUCCESS, error); 267 ASSERT_EQ(google_apis::HTTP_SUCCESS, error);
423 } 268 }
424 269
425 TEST_F(DriveSchedulerTest, AddNewDirectory) { 270 TEST_F(DriveSchedulerTest, AddNewDirectory) {
426 ConnectToWifi(); 271 ConnectToWifi();
427 272
428 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; 273 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
429 scoped_ptr<google_apis::ResourceEntry> entry; 274 scoped_ptr<google_apis::ResourceEntry> entry;
430 275
431 scheduler_->AddNewDirectory( 276 scheduler_->AddNewDirectory(
432 GURL("/feeds/default/private/full/folder%3Aroot"), 277 GURL(), // Root directory.
433 FILE_PATH_LITERAL("New Directory"), 278 FILE_PATH_LITERAL("New Directory"),
434 base::Bind( 279 base::Bind(
435 &google_apis::test_util::CopyResultsFromGetResourceEntryCallback, 280 &google_apis::test_util::CopyResultsFromGetResourceEntryCallback,
436 &error, 281 &error,
437 &entry)); 282 &entry));
438 google_apis::test_util::RunBlockingPoolTask(); 283 google_apis::test_util::RunBlockingPoolTask();
439 284
440 ASSERT_EQ(google_apis::HTTP_SUCCESS, error); 285 ASSERT_EQ(google_apis::HTTP_SUCCESS, error);
441 ASSERT_TRUE(entry); 286 ASSERT_TRUE(entry);
442 } 287 }
443 288
444 } // namespace drive 289 } // namespace drive
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/chromeos/drive/stale_cache_files_remover_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698