| 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 "components/drive/job_scheduler.h" | 5 #include "components/drive/job_scheduler.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 7 #include <set> | 10 #include <set> |
| 8 | 11 |
| 9 #include "base/bind.h" | 12 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/prefs/testing_pref_service.h" | 15 #include "base/prefs/testing_pref_service.h" |
| 13 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 14 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 15 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 16 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 17 #include "base/thread_task_runner_handle.h" | 20 #include "base/thread_task_runner_handle.h" |
| 18 #include "components/drive/drive_pref_names.h" | 21 #include "components/drive/drive_pref_names.h" |
| 19 #include "components/drive/drive_test_util.h" | 22 #include "components/drive/drive_test_util.h" |
| 20 #include "components/drive/event_logger.h" | 23 #include "components/drive/event_logger.h" |
| 21 #include "components/drive/service/fake_drive_service.h" | 24 #include "components/drive/service/fake_drive_service.h" |
| 22 #include "components/drive/service/test_util.h" | 25 #include "components/drive/service/test_util.h" |
| 23 #include "content/public/test/test_browser_thread_bundle.h" | 26 #include "content/public/test/test_browser_thread_bundle.h" |
| 24 #include "google_apis/drive/drive_api_parser.h" | 27 #include "google_apis/drive/drive_api_parser.h" |
| 25 #include "google_apis/drive/test_util.h" | 28 #include "google_apis/drive/test_util.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 30 |
| 28 namespace drive { | 31 namespace drive { |
| 29 | 32 |
| 30 namespace { | 33 namespace { |
| 31 | 34 |
| 32 // Dummy value passed for the |expected_file_size| parameter of DownloadFile(). | 35 // Dummy value passed for the |expected_file_size| parameter of DownloadFile(). |
| 33 const int64 kDummyDownloadFileSize = 0; | 36 const int64_t kDummyDownloadFileSize = 0; |
| 34 | 37 |
| 35 void CopyTitleFromFileResourceCallback( | 38 void CopyTitleFromFileResourceCallback( |
| 36 std::vector<std::string>* title_list_out, | 39 std::vector<std::string>* title_list_out, |
| 37 google_apis::DriveApiErrorCode error_in, | 40 google_apis::DriveApiErrorCode error_in, |
| 38 scoped_ptr<google_apis::FileResource> entry_in) { | 41 scoped_ptr<google_apis::FileResource> entry_in) { |
| 39 title_list_out->push_back(entry_in->title()); | 42 title_list_out->push_back(entry_in->title()); |
| 40 } | 43 } |
| 41 | 44 |
| 42 class JobListLogger : public JobListObserver { | 45 class JobListLogger : public JobListObserver { |
| 43 public: | 46 public: |
| (...skipping 14 matching lines...) Expand all Loading... |
| 58 // Checks whether the specified type of event has occurred. | 61 // Checks whether the specified type of event has occurred. |
| 59 bool Has(EventType type, JobType job_type) { | 62 bool Has(EventType type, JobType job_type) { |
| 60 for (size_t i = 0; i < events.size(); ++i) { | 63 for (size_t i = 0; i < events.size(); ++i) { |
| 61 if (events[i].type == type && events[i].info.job_type == job_type) | 64 if (events[i].type == type && events[i].info.job_type == job_type) |
| 62 return true; | 65 return true; |
| 63 } | 66 } |
| 64 return false; | 67 return false; |
| 65 } | 68 } |
| 66 | 69 |
| 67 // Gets the progress event information of the specified type. | 70 // Gets the progress event information of the specified type. |
| 68 void GetProgressInfo(JobType job_type, std::vector<int64>* progress) { | 71 void GetProgressInfo(JobType job_type, std::vector<int64_t>* progress) { |
| 69 for (size_t i = 0; i < events.size(); ++i) { | 72 for (size_t i = 0; i < events.size(); ++i) { |
| 70 if (events[i].type == UPDATED && events[i].info.job_type == job_type) | 73 if (events[i].type == UPDATED && events[i].info.job_type == job_type) |
| 71 progress->push_back(events[i].info.num_completed_bytes); | 74 progress->push_back(events[i].info.num_completed_bytes); |
| 72 } | 75 } |
| 73 } | 76 } |
| 74 | 77 |
| 75 // JobListObserver overrides. | 78 // JobListObserver overrides. |
| 76 void OnJobAdded(const JobInfo& info) override { | 79 void OnJobAdded(const JobInfo& info) override { |
| 77 events.push_back(EventLog(ADDED, info)); | 80 events.push_back(EventLog(ADDED, info)); |
| 78 } | 81 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 98 CancelTestableFakeDriveService() | 101 CancelTestableFakeDriveService() |
| 99 : upload_new_file_cancelable_(false) { | 102 : upload_new_file_cancelable_(false) { |
| 100 } | 103 } |
| 101 | 104 |
| 102 void set_upload_new_file_cancelable(bool cancelable) { | 105 void set_upload_new_file_cancelable(bool cancelable) { |
| 103 upload_new_file_cancelable_ = cancelable; | 106 upload_new_file_cancelable_ = cancelable; |
| 104 } | 107 } |
| 105 | 108 |
| 106 google_apis::CancelCallback InitiateUploadNewFile( | 109 google_apis::CancelCallback InitiateUploadNewFile( |
| 107 const std::string& content_type, | 110 const std::string& content_type, |
| 108 int64 content_length, | 111 int64_t content_length, |
| 109 const std::string& parent_resource_id, | 112 const std::string& parent_resource_id, |
| 110 const std::string& title, | 113 const std::string& title, |
| 111 const UploadNewFileOptions& options, | 114 const UploadNewFileOptions& options, |
| 112 const google_apis::InitiateUploadCallback& callback) override { | 115 const google_apis::InitiateUploadCallback& callback) override { |
| 113 if (upload_new_file_cancelable_) | 116 if (upload_new_file_cancelable_) |
| 114 return base::Bind(callback, google_apis::DRIVE_CANCELLED, GURL()); | 117 return base::Bind(callback, google_apis::DRIVE_CANCELLED, GURL()); |
| 115 | 118 |
| 116 return FakeDriveService::InitiateUploadNewFile(content_type, | 119 return FakeDriveService::InitiateUploadNewFile(content_type, |
| 117 content_length, | 120 content_length, |
| 118 parent_resource_id, | 121 parent_resource_id, |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 google_apis::test_util::CreateCopyResultCallback(&error, &file_list)); | 262 google_apis::test_util::CreateCopyResultCallback(&error, &file_list)); |
| 260 base::RunLoop().RunUntilIdle(); | 263 base::RunLoop().RunUntilIdle(); |
| 261 | 264 |
| 262 ASSERT_EQ(google_apis::HTTP_SUCCESS, error); | 265 ASSERT_EQ(google_apis::HTTP_SUCCESS, error); |
| 263 ASSERT_TRUE(file_list); | 266 ASSERT_TRUE(file_list); |
| 264 } | 267 } |
| 265 | 268 |
| 266 TEST_F(JobSchedulerTest, GetChangeList) { | 269 TEST_F(JobSchedulerTest, GetChangeList) { |
| 267 ConnectToWifi(); | 270 ConnectToWifi(); |
| 268 | 271 |
| 269 int64 old_largest_change_id = | 272 int64_t old_largest_change_id = |
| 270 fake_drive_service_->about_resource().largest_change_id(); | 273 fake_drive_service_->about_resource().largest_change_id(); |
| 271 | 274 |
| 272 google_apis::DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR; | 275 google_apis::DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR; |
| 273 | 276 |
| 274 // Create a new directory. | 277 // Create a new directory. |
| 275 { | 278 { |
| 276 scoped_ptr<google_apis::FileResource> entry; | 279 scoped_ptr<google_apis::FileResource> entry; |
| 277 fake_drive_service_->AddNewDirectory( | 280 fake_drive_service_->AddNewDirectory( |
| 278 fake_drive_service_->GetRootResourceId(), "new directory", | 281 fake_drive_service_->GetRootResourceId(), "new directory", |
| 279 AddNewDirectoryOptions(), | 282 AddNewDirectoryOptions(), |
| (...skipping 629 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 909 scheduler_->DownloadFile( | 912 scheduler_->DownloadFile( |
| 910 base::FilePath::FromUTF8Unsafe("drive/whatever.txt"), // virtual path | 913 base::FilePath::FromUTF8Unsafe("drive/whatever.txt"), // virtual path |
| 911 kDummyDownloadFileSize, | 914 kDummyDownloadFileSize, |
| 912 temp_dir.path().AppendASCII("whatever.txt"), | 915 temp_dir.path().AppendASCII("whatever.txt"), |
| 913 "2_file_resource_id", | 916 "2_file_resource_id", |
| 914 ClientContext(BACKGROUND), | 917 ClientContext(BACKGROUND), |
| 915 google_apis::test_util::CreateCopyResultCallback(&error, &path), | 918 google_apis::test_util::CreateCopyResultCallback(&error, &path), |
| 916 google_apis::GetContentCallback()); | 919 google_apis::GetContentCallback()); |
| 917 base::RunLoop().RunUntilIdle(); | 920 base::RunLoop().RunUntilIdle(); |
| 918 | 921 |
| 919 std::vector<int64> download_progress; | 922 std::vector<int64_t> download_progress; |
| 920 logger.GetProgressInfo(TYPE_DOWNLOAD_FILE, &download_progress); | 923 logger.GetProgressInfo(TYPE_DOWNLOAD_FILE, &download_progress); |
| 921 ASSERT_TRUE(!download_progress.empty()); | 924 ASSERT_TRUE(!download_progress.empty()); |
| 922 EXPECT_TRUE(base::STLIsSorted(download_progress)); | 925 EXPECT_TRUE(base::STLIsSorted(download_progress)); |
| 923 EXPECT_GE(download_progress.front(), 0); | 926 EXPECT_GE(download_progress.front(), 0); |
| 924 EXPECT_LE(download_progress.back(), 26); | 927 EXPECT_LE(download_progress.back(), 26); |
| 925 | 928 |
| 926 // Upload job. | 929 // Upload job. |
| 927 path = temp_dir.path().AppendASCII("new_file.txt"); | 930 path = temp_dir.path().AppendASCII("new_file.txt"); |
| 928 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(path, "Hello")); | 931 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(path, "Hello")); |
| 929 google_apis::DriveApiErrorCode upload_error = | 932 google_apis::DriveApiErrorCode upload_error = |
| 930 google_apis::DRIVE_OTHER_ERROR; | 933 google_apis::DRIVE_OTHER_ERROR; |
| 931 scoped_ptr<google_apis::FileResource> entry; | 934 scoped_ptr<google_apis::FileResource> entry; |
| 932 | 935 |
| 933 scheduler_->UploadNewFile( | 936 scheduler_->UploadNewFile( |
| 934 fake_drive_service_->GetRootResourceId(), std::string("Hello").size(), | 937 fake_drive_service_->GetRootResourceId(), std::string("Hello").size(), |
| 935 base::FilePath::FromUTF8Unsafe("drive/new_file.txt"), path, "dummy title", | 938 base::FilePath::FromUTF8Unsafe("drive/new_file.txt"), path, "dummy title", |
| 936 "plain/plain", UploadNewFileOptions(), ClientContext(BACKGROUND), | 939 "plain/plain", UploadNewFileOptions(), ClientContext(BACKGROUND), |
| 937 google_apis::test_util::CreateCopyResultCallback(&upload_error, &entry)); | 940 google_apis::test_util::CreateCopyResultCallback(&upload_error, &entry)); |
| 938 base::RunLoop().RunUntilIdle(); | 941 base::RunLoop().RunUntilIdle(); |
| 939 | 942 |
| 940 std::vector<int64> upload_progress; | 943 std::vector<int64_t> upload_progress; |
| 941 logger.GetProgressInfo(TYPE_UPLOAD_NEW_FILE, &upload_progress); | 944 logger.GetProgressInfo(TYPE_UPLOAD_NEW_FILE, &upload_progress); |
| 942 ASSERT_TRUE(!upload_progress.empty()); | 945 ASSERT_TRUE(!upload_progress.empty()); |
| 943 EXPECT_TRUE(base::STLIsSorted(upload_progress)); | 946 EXPECT_TRUE(base::STLIsSorted(upload_progress)); |
| 944 EXPECT_GE(upload_progress.front(), 0); | 947 EXPECT_GE(upload_progress.front(), 0); |
| 945 EXPECT_LE(upload_progress.back(), 13); | 948 EXPECT_LE(upload_progress.back(), 13); |
| 946 } | 949 } |
| 947 | 950 |
| 948 TEST_F(JobSchedulerTest, CancelPendingJob) { | 951 TEST_F(JobSchedulerTest, CancelPendingJob) { |
| 949 base::ScopedTempDir temp_dir; | 952 base::ScopedTempDir temp_dir; |
| 950 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 953 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1029 scheduler_->CancelJob(first_job_id); | 1032 scheduler_->CancelJob(first_job_id); |
| 1030 | 1033 |
| 1031 // Only the first job should be cancelled. | 1034 // Only the first job should be cancelled. |
| 1032 base::RunLoop().RunUntilIdle(); | 1035 base::RunLoop().RunUntilIdle(); |
| 1033 EXPECT_EQ(google_apis::DRIVE_CANCELLED, error1); | 1036 EXPECT_EQ(google_apis::DRIVE_CANCELLED, error1); |
| 1034 EXPECT_EQ(google_apis::HTTP_SUCCESS, error2); | 1037 EXPECT_EQ(google_apis::HTTP_SUCCESS, error2); |
| 1035 EXPECT_TRUE(scheduler_->GetJobInfoList().empty()); | 1038 EXPECT_TRUE(scheduler_->GetJobInfoList().empty()); |
| 1036 } | 1039 } |
| 1037 | 1040 |
| 1038 } // namespace drive | 1041 } // namespace drive |
| OLD | NEW |