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/job_scheduler.h" | 5 #include "chrome/browser/chromeos/drive/job_scheduler.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 } | 79 } |
80 | 80 |
81 virtual void OnJobDone(const JobInfo& info, FileError error) OVERRIDE { | 81 virtual void OnJobDone(const JobInfo& info, FileError error) OVERRIDE { |
82 events.push_back(EventLog(DONE, info)); | 82 events.push_back(EventLog(DONE, info)); |
83 } | 83 } |
84 | 84 |
85 private: | 85 private: |
86 std::vector<EventLog> events; | 86 std::vector<EventLog> events; |
87 }; | 87 }; |
88 | 88 |
| 89 // Fake drive service extended for testing cancellation. |
| 90 // When upload_new_file_cancelable is set, this Drive service starts |
| 91 // returning a closure to cancel from InitiateUploadNewFile(). The task will |
| 92 // finish only when the cancel closure is called. |
| 93 class CancelTestableFakeDriveService : public FakeDriveService { |
| 94 public: |
| 95 CancelTestableFakeDriveService() |
| 96 : upload_new_file_cancelable_(false) { |
| 97 } |
| 98 |
| 99 void set_upload_new_file_cancelable(bool cancelable) { |
| 100 upload_new_file_cancelable_ = cancelable; |
| 101 } |
| 102 |
| 103 virtual google_apis::CancelCallback InitiateUploadNewFile( |
| 104 const std::string& content_type, |
| 105 int64 content_length, |
| 106 const std::string& parent_resource_id, |
| 107 const std::string& title, |
| 108 const google_apis::InitiateUploadCallback& callback) OVERRIDE { |
| 109 if (upload_new_file_cancelable_) |
| 110 return base::Bind(callback, google_apis::GDATA_CANCELLED, GURL()); |
| 111 |
| 112 return FakeDriveService::InitiateUploadNewFile(content_type, |
| 113 content_length, |
| 114 parent_resource_id, |
| 115 title, |
| 116 callback); |
| 117 } |
| 118 |
| 119 private: |
| 120 bool upload_new_file_cancelable_; |
| 121 }; |
| 122 |
89 } // namespace | 123 } // namespace |
90 | 124 |
91 class JobSchedulerTest : public testing::Test { | 125 class JobSchedulerTest : public testing::Test { |
92 public: | 126 public: |
93 JobSchedulerTest() | 127 JobSchedulerTest() |
94 : pref_service_(new TestingPrefServiceSimple) { | 128 : pref_service_(new TestingPrefServiceSimple) { |
95 test_util::RegisterDrivePrefs(pref_service_->registry()); | 129 test_util::RegisterDrivePrefs(pref_service_->registry()); |
96 } | 130 } |
97 | 131 |
98 virtual void SetUp() OVERRIDE { | 132 virtual void SetUp() OVERRIDE { |
99 fake_network_change_notifier_.reset( | 133 fake_network_change_notifier_.reset( |
100 new test_util::FakeNetworkChangeNotifier); | 134 new test_util::FakeNetworkChangeNotifier); |
101 | 135 |
102 fake_drive_service_.reset(new FakeDriveService()); | 136 fake_drive_service_.reset(new CancelTestableFakeDriveService); |
103 fake_drive_service_->LoadResourceListForWapi( | 137 fake_drive_service_->LoadResourceListForWapi( |
104 "gdata/root_feed.json"); | 138 "gdata/root_feed.json"); |
105 fake_drive_service_->LoadAccountMetadataForWapi( | 139 fake_drive_service_->LoadAccountMetadataForWapi( |
106 "gdata/account_metadata.json"); | 140 "gdata/account_metadata.json"); |
107 fake_drive_service_->LoadAppListForDriveApi( | 141 fake_drive_service_->LoadAppListForDriveApi( |
108 "drive/applist.json"); | 142 "drive/applist.json"); |
109 | 143 |
110 scheduler_.reset(new JobScheduler(pref_service_.get(), | 144 scheduler_.reset(new JobScheduler(pref_service_.get(), |
111 fake_drive_service_.get(), | 145 fake_drive_service_.get(), |
112 base::MessageLoopProxy::current().get())); | 146 base::MessageLoopProxy::current().get())); |
(...skipping 28 matching lines...) Expand all Loading... |
141 } | 175 } |
142 | 176 |
143 static int GetMetadataQueueMaxJobCount() { | 177 static int GetMetadataQueueMaxJobCount() { |
144 return JobScheduler::kMaxJobCount[JobScheduler::METADATA_QUEUE]; | 178 return JobScheduler::kMaxJobCount[JobScheduler::METADATA_QUEUE]; |
145 } | 179 } |
146 | 180 |
147 content::TestBrowserThreadBundle thread_bundle_; | 181 content::TestBrowserThreadBundle thread_bundle_; |
148 scoped_ptr<TestingPrefServiceSimple> pref_service_; | 182 scoped_ptr<TestingPrefServiceSimple> pref_service_; |
149 scoped_ptr<test_util::FakeNetworkChangeNotifier> | 183 scoped_ptr<test_util::FakeNetworkChangeNotifier> |
150 fake_network_change_notifier_; | 184 fake_network_change_notifier_; |
151 scoped_ptr<FakeDriveService> fake_drive_service_; | 185 scoped_ptr<CancelTestableFakeDriveService> fake_drive_service_; |
152 scoped_ptr<JobScheduler> scheduler_; | 186 scoped_ptr<JobScheduler> scheduler_; |
153 }; | 187 }; |
154 | 188 |
155 TEST_F(JobSchedulerTest, GetAboutResource) { | 189 TEST_F(JobSchedulerTest, GetAboutResource) { |
156 ConnectToWifi(); | 190 ConnectToWifi(); |
157 | 191 |
158 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; | 192 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; |
159 scoped_ptr<google_apis::AboutResource> about_resource; | 193 scoped_ptr<google_apis::AboutResource> about_resource; |
160 scheduler_->GetAboutResource( | 194 scheduler_->GetAboutResource( |
161 google_apis::test_util::CreateCopyResultCallback( | 195 google_apis::test_util::CreateCopyResultCallback( |
(...skipping 819 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
981 google_apis::test_util::CreateCopyResultCallback(&error2, &entry)); | 1015 google_apis::test_util::CreateCopyResultCallback(&error2, &entry)); |
982 | 1016 |
983 // Cancel the first one. | 1017 // Cancel the first one. |
984 scheduler_->CancelJob(first_job_id); | 1018 scheduler_->CancelJob(first_job_id); |
985 | 1019 |
986 // Only the first job should be cancelled. | 1020 // Only the first job should be cancelled. |
987 ConnectToWifi(); | 1021 ConnectToWifi(); |
988 base::RunLoop().RunUntilIdle(); | 1022 base::RunLoop().RunUntilIdle(); |
989 EXPECT_EQ(google_apis::GDATA_CANCELLED, error1); | 1023 EXPECT_EQ(google_apis::GDATA_CANCELLED, error1); |
990 EXPECT_EQ(google_apis::HTTP_SUCCESS, error2); | 1024 EXPECT_EQ(google_apis::HTTP_SUCCESS, error2); |
| 1025 EXPECT_TRUE(scheduler_->GetJobInfoList().empty()); |
991 } | 1026 } |
992 | 1027 |
993 TEST_F(JobSchedulerTest, CancelRunningJob) { | 1028 TEST_F(JobSchedulerTest, CancelRunningJob) { |
994 // TODO(kinaba): test for canceling jobs running in the Drive service. | 1029 ConnectToWifi(); |
995 // We need to slightly extend FakeDriveService to support CancelCallback. | 1030 |
| 1031 base::ScopedTempDir temp_dir; |
| 1032 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1033 base::FilePath upload_path = temp_dir.path().AppendASCII("new_file.txt"); |
| 1034 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(upload_path, "Hello")); |
| 1035 |
| 1036 // Run as a cancelable task. |
| 1037 fake_drive_service_->set_upload_new_file_cancelable(true); |
| 1038 google_apis::GDataErrorCode error1 = google_apis::GDATA_OTHER_ERROR; |
| 1039 scoped_ptr<google_apis::ResourceEntry> entry; |
| 1040 scheduler_->UploadNewFile( |
| 1041 fake_drive_service_->GetRootResourceId(), |
| 1042 base::FilePath::FromUTF8Unsafe("dummy/path"), |
| 1043 upload_path, |
| 1044 "dummy title 1", |
| 1045 "text/plain", |
| 1046 ClientContext(USER_INITIATED), |
| 1047 google_apis::test_util::CreateCopyResultCallback(&error1, &entry)); |
| 1048 |
| 1049 const std::vector<JobInfo>& jobs = scheduler_->GetJobInfoList(); |
| 1050 ASSERT_EQ(1u, jobs.size()); |
| 1051 ASSERT_EQ(STATE_RUNNING, jobs[0].state); // It's running. |
| 1052 JobID first_job_id = jobs[0].job_id; |
| 1053 |
| 1054 // Start the second job normally. |
| 1055 fake_drive_service_->set_upload_new_file_cancelable(false); |
| 1056 google_apis::GDataErrorCode error2 = google_apis::GDATA_OTHER_ERROR; |
| 1057 scheduler_->UploadNewFile( |
| 1058 fake_drive_service_->GetRootResourceId(), |
| 1059 base::FilePath::FromUTF8Unsafe("dummy/path"), |
| 1060 upload_path, |
| 1061 "dummy title 2", |
| 1062 "text/plain", |
| 1063 ClientContext(USER_INITIATED), |
| 1064 google_apis::test_util::CreateCopyResultCallback(&error2, &entry)); |
| 1065 |
| 1066 // Cancel the first one. |
| 1067 scheduler_->CancelJob(first_job_id); |
| 1068 |
| 1069 // Only the first job should be cancelled. |
| 1070 base::RunLoop().RunUntilIdle(); |
| 1071 EXPECT_EQ(google_apis::GDATA_CANCELLED, error1); |
| 1072 EXPECT_EQ(google_apis::HTTP_SUCCESS, error2); |
| 1073 EXPECT_TRUE(scheduler_->GetJobInfoList().empty()); |
996 } | 1074 } |
997 | 1075 |
998 } // namespace drive | 1076 } // namespace drive |
OLD | NEW |