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 "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" |
| 9 #include "base/json/json_reader.h" |
8 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
9 #include "chrome/browser/chromeos/drive/drive_test_util.h" | 11 #include "chrome/browser/chromeos/drive/drive_test_util.h" |
10 #include "chrome/browser/chromeos/drive/file_system/drive_operations.h" | 12 #include "chrome/browser/chromeos/drive/file_system/drive_operations.h" |
11 #include "chrome/browser/chromeos/drive/file_system/copy_operation.h" | 13 #include "chrome/browser/chromeos/drive/file_system/copy_operation.h" |
12 #include "chrome/browser/chromeos/drive/file_system/move_operation.h" | 14 #include "chrome/browser/chromeos/drive/file_system/move_operation.h" |
13 #include "chrome/browser/chromeos/drive/file_system/remove_operation.h" | 15 #include "chrome/browser/chromeos/drive/file_system/remove_operation.h" |
14 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
15 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
16 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
17 #include "content/public/test/test_browser_thread.h" | 19 #include "content/public/test/test_browser_thread.h" |
18 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
20 | 22 |
21 using ::testing::AnyNumber; | 23 using ::testing::AnyNumber; |
22 using ::testing::DoAll; | 24 using ::testing::DoAll; |
| 25 using ::testing::Eq; |
23 using ::testing::Return; | 26 using ::testing::Return; |
24 using ::testing::StrictMock; | 27 using ::testing::StrictMock; |
25 using ::testing::_; | 28 using ::testing::_; |
26 | 29 |
| 30 namespace google_apis { |
| 31 |
| 32 class FakeDriveService : public DriveServiceInterface { |
| 33 virtual void Initialize(Profile* profile) { |
| 34 } |
| 35 |
| 36 virtual void AddObserver(DriveServiceObserver* observer) { |
| 37 } |
| 38 |
| 39 virtual void RemoveObserver(DriveServiceObserver* observer) { |
| 40 } |
| 41 |
| 42 virtual bool CanStartOperation() const { |
| 43 return true; |
| 44 } |
| 45 |
| 46 virtual void CancelAll() { |
| 47 } |
| 48 |
| 49 virtual bool CancelForFilePath(const FilePath& file_path) { |
| 50 return true; |
| 51 } |
| 52 |
| 53 virtual OperationProgressStatusList GetProgressStatusList() const { |
| 54 return OperationProgressStatusList(); |
| 55 } |
| 56 |
| 57 virtual bool HasAccessToken() const { |
| 58 return true; |
| 59 } |
| 60 |
| 61 virtual bool HasRefreshToken() const { |
| 62 return true; |
| 63 } |
| 64 |
| 65 virtual void GetDocuments(const GURL& feed_url, |
| 66 int64 start_changestamp, |
| 67 const std::string& search_query, |
| 68 bool shared_with_me, |
| 69 const std::string& directory_resource_id, |
| 70 const GetDataCallback& callback) { |
| 71 // TODO: Make this more flexible. |
| 72 if (feed_url == GURL("http://example.com/gdata/root_feed.json")) { |
| 73 // Make some sample data. |
| 74 const FilePath feed_path = |
| 75 test_util::GetTestFilePath("gdata/root_feed.json"); |
| 76 std::string feed_contents; |
| 77 file_util::ReadFileToString(feed_path, &feed_contents); |
| 78 scoped_ptr<base::Value> feed_data( |
| 79 base::JSONReader::Read(feed_contents)); |
| 80 |
| 81 base::MessageLoopProxy::current()->PostTask(FROM_HERE, |
| 82 base::Bind(callback, |
| 83 HTTP_SUCCESS, |
| 84 base::Passed(&feed_data))); |
| 85 } else { |
| 86 scoped_ptr<base::Value> feed_data; |
| 87 base::MessageLoopProxy::current()->PostTask(FROM_HERE, |
| 88 base::Bind(callback, |
| 89 GDATA_PARSE_ERROR, |
| 90 base::Passed(&feed_data))); |
| 91 } |
| 92 } |
| 93 |
| 94 virtual void GetDocumentEntry(const std::string& resource_id, |
| 95 const GetDataCallback& callback) { |
| 96 } |
| 97 |
| 98 virtual void GetAccountMetadata(const GetDataCallback& callback) { |
| 99 } |
| 100 |
| 101 virtual void GetApplicationInfo(const GetDataCallback& callback) { |
| 102 } |
| 103 |
| 104 virtual void DeleteDocument(const GURL& document_url, |
| 105 const EntryActionCallback& callback) { |
| 106 } |
| 107 |
| 108 virtual void DownloadDocument(const FilePath& virtual_path, |
| 109 const FilePath& local_cache_path, |
| 110 const GURL& content_url, |
| 111 DocumentExportFormat format, |
| 112 const DownloadActionCallback& callback) { |
| 113 } |
| 114 |
| 115 virtual void CopyDocument(const std::string& resource_id, |
| 116 const FilePath::StringType& new_name, |
| 117 const GetDataCallback& callback) { |
| 118 } |
| 119 |
| 120 virtual void RenameResource(const GURL& resource_url, |
| 121 const FilePath::StringType& new_name, |
| 122 const EntryActionCallback& callback) { |
| 123 } |
| 124 |
| 125 virtual void AddResourceToDirectory(const GURL& parent_content_url, |
| 126 const GURL& resource_url, |
| 127 const EntryActionCallback& callback) { |
| 128 } |
| 129 |
| 130 virtual void RemoveResourceFromDirectory( |
| 131 const GURL& parent_content_url, |
| 132 const std::string& resource_id, |
| 133 const EntryActionCallback& callback) { |
| 134 } |
| 135 |
| 136 virtual void AddNewDirectory(const GURL& parent_content_url, |
| 137 const FilePath::StringType& directory_name, |
| 138 const GetDataCallback& callback) { |
| 139 } |
| 140 |
| 141 virtual void DownloadFile( |
| 142 const FilePath& virtual_path, |
| 143 const FilePath& local_cache_path, |
| 144 const GURL& content_url, |
| 145 const DownloadActionCallback& download_action_callback, |
| 146 const GetContentCallback& get_content_callback) { |
| 147 } |
| 148 |
| 149 virtual void InitiateUpload(const InitiateUploadParams& params, |
| 150 const InitiateUploadCallback& callback) { |
| 151 } |
| 152 |
| 153 virtual void ResumeUpload(const ResumeUploadParams& params, |
| 154 const ResumeUploadCallback& callback) { |
| 155 } |
| 156 |
| 157 virtual void AuthorizeApp(const GURL& resource_url, |
| 158 const std::string& app_id, |
| 159 const GetDataCallback& callback) { |
| 160 } |
| 161 |
| 162 }; |
| 163 |
| 164 } // namespace google_apis |
| 165 |
27 namespace drive { | 166 namespace drive { |
28 | 167 |
29 namespace { | 168 namespace { |
30 | 169 |
31 class MockNetworkChangeNotifier : public net::NetworkChangeNotifier { | 170 class MockNetworkChangeNotifier : public net::NetworkChangeNotifier { |
32 public: | 171 public: |
33 MOCK_CONST_METHOD0(GetCurrentConnectionType, | 172 MOCK_CONST_METHOD0(GetCurrentConnectionType, |
34 net::NetworkChangeNotifier::ConnectionType()); | 173 net::NetworkChangeNotifier::ConnectionType()); |
35 }; | 174 }; |
36 | 175 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 class DriveSchedulerTest : public testing::Test { | 232 class DriveSchedulerTest : public testing::Test { |
94 public: | 233 public: |
95 DriveSchedulerTest() | 234 DriveSchedulerTest() |
96 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 235 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
97 profile_(new TestingProfile) { | 236 profile_(new TestingProfile) { |
98 } | 237 } |
99 | 238 |
100 virtual void SetUp() OVERRIDE { | 239 virtual void SetUp() OVERRIDE { |
101 mock_network_change_notifier_.reset(new MockNetworkChangeNotifier); | 240 mock_network_change_notifier_.reset(new MockNetworkChangeNotifier); |
102 | 241 |
| 242 fake_drive_service_.reset(new google_apis::FakeDriveService()); |
103 mock_copy_operation_ = new StrictMock<MockCopyOperation>(); | 243 mock_copy_operation_ = new StrictMock<MockCopyOperation>(); |
104 mock_move_operation_ = new StrictMock<MockMoveOperation>(); | 244 mock_move_operation_ = new StrictMock<MockMoveOperation>(); |
105 mock_remove_operation_ = new StrictMock<MockRemoveOperation>(); | 245 mock_remove_operation_ = new StrictMock<MockRemoveOperation>(); |
106 drive_operations_.InitForTesting(mock_copy_operation_, | 246 drive_operations_.InitForTesting(mock_copy_operation_, |
107 mock_move_operation_, | 247 mock_move_operation_, |
108 mock_remove_operation_, | 248 mock_remove_operation_, |
109 NULL); | 249 NULL); |
110 scheduler_.reset(new DriveScheduler(profile_.get(), | 250 scheduler_.reset(new DriveScheduler(profile_.get(), |
| 251 fake_drive_service_.get(), |
111 &drive_operations_)); | 252 &drive_operations_)); |
112 | 253 |
113 scheduler_->Initialize(); | 254 scheduler_->Initialize(); |
114 scheduler_->SetDisableThrottling(true); | 255 scheduler_->SetDisableThrottling(true); |
115 } | 256 } |
116 | 257 |
117 virtual void TearDown() OVERRIDE { | 258 virtual void TearDown() OVERRIDE { |
118 // The scheduler should be deleted before NetworkLibrary, as it | 259 // The scheduler should be deleted before NetworkLibrary, as it |
119 // registers itself as observer of NetworkLibrary. | 260 // registers itself as observer of NetworkLibrary. |
120 scheduler_.reset(); | 261 scheduler_.reset(); |
121 google_apis::test_util::RunBlockingPoolTask(); | 262 google_apis::test_util::RunBlockingPoolTask(); |
| 263 fake_drive_service_.reset(); |
122 mock_network_change_notifier_.reset(); | 264 mock_network_change_notifier_.reset(); |
123 } | 265 } |
124 | 266 |
125 // Sets up MockNetworkChangeNotifier as if it's connected to a network with | 267 // Sets up MockNetworkChangeNotifier as if it's connected to a network with |
126 // the specified connection type. | 268 // the specified connection type. |
127 void ChangeConnectionType(net::NetworkChangeNotifier::ConnectionType type) { | 269 void ChangeConnectionType(net::NetworkChangeNotifier::ConnectionType type) { |
128 EXPECT_CALL(*mock_network_change_notifier_, GetCurrentConnectionType()) | 270 EXPECT_CALL(*mock_network_change_notifier_, GetCurrentConnectionType()) |
129 .WillRepeatedly(Return(type)); | 271 .WillRepeatedly(Return(type)); |
130 // Notify the sync client that the network is changed. This is done via | 272 // Notify the sync client that the network is changed. This is done via |
131 // NetworkChangeNotifier in production, but here, we simulate the behavior | 273 // NetworkChangeNotifier in production, but here, we simulate the behavior |
(...skipping 20 matching lines...) Expand all Loading... |
152 void ConnectToNone() { | 294 void ConnectToNone() { |
153 ChangeConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE); | 295 ChangeConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE); |
154 } | 296 } |
155 | 297 |
156 protected: | 298 protected: |
157 MessageLoopForUI message_loop_; | 299 MessageLoopForUI message_loop_; |
158 content::TestBrowserThread ui_thread_; | 300 content::TestBrowserThread ui_thread_; |
159 scoped_ptr<TestingProfile> profile_; | 301 scoped_ptr<TestingProfile> profile_; |
160 scoped_ptr<DriveScheduler> scheduler_; | 302 scoped_ptr<DriveScheduler> scheduler_; |
161 scoped_ptr<MockNetworkChangeNotifier> mock_network_change_notifier_; | 303 scoped_ptr<MockNetworkChangeNotifier> mock_network_change_notifier_; |
| 304 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; |
162 | 305 |
163 file_system::DriveOperations drive_operations_; | 306 file_system::DriveOperations drive_operations_; |
164 StrictMock<MockCopyOperation>* mock_copy_operation_; | 307 StrictMock<MockCopyOperation>* mock_copy_operation_; |
165 StrictMock<MockMoveOperation>* mock_move_operation_; | 308 StrictMock<MockMoveOperation>* mock_move_operation_; |
166 StrictMock<MockRemoveOperation>* mock_remove_operation_; | 309 StrictMock<MockRemoveOperation>* mock_remove_operation_; |
167 }; | 310 }; |
168 | 311 |
169 TEST_F(DriveSchedulerTest, CopyFile) { | 312 TEST_F(DriveSchedulerTest, CopyFile) { |
170 ConnectToWifi(); | 313 ConnectToWifi(); |
171 | 314 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 | 373 |
231 DriveFileError error(DRIVE_FILE_ERROR_FAILED); | 374 DriveFileError error(DRIVE_FILE_ERROR_FAILED); |
232 scheduler_->TransferRegularFile( | 375 scheduler_->TransferRegularFile( |
233 file_in_root, dest_file, | 376 file_in_root, dest_file, |
234 base::Bind(&test_util::CopyErrorCodeFromFileOperationCallback, &error)); | 377 base::Bind(&test_util::CopyErrorCodeFromFileOperationCallback, &error)); |
235 google_apis::test_util::RunBlockingPoolTask(); | 378 google_apis::test_util::RunBlockingPoolTask(); |
236 | 379 |
237 ASSERT_EQ(DRIVE_FILE_OK, error); | 380 ASSERT_EQ(DRIVE_FILE_OK, error); |
238 } | 381 } |
239 | 382 |
| 383 TEST_F(DriveSchedulerTest, GetDocuments) { |
| 384 ConnectToWifi(); |
| 385 |
| 386 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; |
| 387 scoped_ptr<base::Value> value; |
| 388 |
| 389 scheduler_->GetDocuments( |
| 390 GURL("http://example.com/gdata/root_feed.json"), |
| 391 0, |
| 392 std::string(), |
| 393 true, |
| 394 std::string(), |
| 395 base::Bind(&google_apis::test_util::CopyResultsFromGetDataCallback, |
| 396 &error, |
| 397 &value)); |
| 398 google_apis::test_util::RunBlockingPoolTask(); |
| 399 |
| 400 ASSERT_EQ(google_apis::HTTP_SUCCESS, error); |
| 401 ASSERT_TRUE(value); |
| 402 } |
| 403 |
240 TEST_F(DriveSchedulerTest, MoveFile) { | 404 TEST_F(DriveSchedulerTest, MoveFile) { |
241 ConnectToWifi(); | 405 ConnectToWifi(); |
242 | 406 |
243 FilePath file_in_root(FILE_PATH_LITERAL("drive/File 1.txt")); | 407 FilePath file_in_root(FILE_PATH_LITERAL("drive/File 1.txt")); |
244 FilePath dest_file(FILE_PATH_LITERAL("drive/File 1.txt")); | 408 FilePath dest_file(FILE_PATH_LITERAL("drive/File 1.txt")); |
245 EXPECT_CALL(*mock_move_operation_, Move(file_in_root, dest_file, _)) | 409 EXPECT_CALL(*mock_move_operation_, Move(file_in_root, dest_file, _)) |
246 .WillOnce(MockOperation(DRIVE_FILE_OK)); | 410 .WillOnce(MockOperation(DRIVE_FILE_OK)); |
247 | 411 |
248 DriveFileError error(DRIVE_FILE_ERROR_FAILED); | 412 DriveFileError error(DRIVE_FILE_ERROR_FAILED); |
249 scheduler_->Move( | 413 scheduler_->Move( |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 | 563 |
400 FilePath file_in_root(FILE_PATH_LITERAL("drive/File 1.txt")); | 564 FilePath file_in_root(FILE_PATH_LITERAL("drive/File 1.txt")); |
401 DriveFileError error; | 565 DriveFileError error; |
402 scheduler_->Remove( | 566 scheduler_->Remove( |
403 file_in_root, false, | 567 file_in_root, false, |
404 base::Bind(&test_util::CopyErrorCodeFromFileOperationCallback, &error)); | 568 base::Bind(&test_util::CopyErrorCodeFromFileOperationCallback, &error)); |
405 google_apis::test_util::RunBlockingPoolTask(); | 569 google_apis::test_util::RunBlockingPoolTask(); |
406 } | 570 } |
407 | 571 |
408 } // namespace drive | 572 } // namespace drive |
OLD | NEW |