| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 // Browser test for basic Chrome OS file manager functionality: | 5 // Browser test for basic Chrome OS file manager functionality: |
| 6 // - The file list is updated when a file is added externally to the Downloads | 6 // - The file list is updated when a file is added externally to the Downloads |
| 7 // folder. | 7 // folder. |
| 8 // - Selecting a file and copy-pasting it with the keyboard copies the file. | 8 // - Selecting a file and copy-pasting it with the keyboard copies the file. |
| 9 // - Selecting a file and pressing delete deletes it. | 9 // - Selecting a file and pressing delete deletes it. |
| 10 | 10 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 // Creates a file with the given |name|, |length|, and |modification_time|. | 88 // Creates a file with the given |name|, |length|, and |modification_time|. |
| 89 virtual void CreateTestFile(const std::string& name, | 89 virtual void CreateTestFile(const std::string& name, |
| 90 int64 length, | 90 int64 length, |
| 91 const std::string& modification_time) = 0; | 91 const std::string& modification_time) = 0; |
| 92 | 92 |
| 93 // Creates an empty directory with the given |name| and |modification_time|. | 93 // Creates an empty directory with the given |name| and |modification_time|. |
| 94 virtual void CreateTestDirectory( | 94 virtual void CreateTestDirectory( |
| 95 const std::string& name, | 95 const std::string& name, |
| 96 const std::string& modification_time) = 0; | 96 const std::string& modification_time) = 0; |
| 97 | 97 |
| 98 // Returns the path of the root directory. |
| 99 virtual base::FilePath GetRootPath() = 0; |
| 100 |
| 101 // Returns true if |file_path| exists. |
| 102 virtual bool PathExists(const base::FilePath& file_path) = 0; |
| 103 |
| 104 // Waits until a file with the given size is present at |path|. Returns |
| 105 // true on success. |
| 106 virtual bool WaitUntilFilePresentWithSize(const base::FilePath& file_path, |
| 107 int64 file_size) = 0; |
| 108 |
| 109 // Waits until a file is not present at |path|. Returns true on success. |
| 110 virtual bool WaitUntilFileNotPresent(const base::FilePath& file_path) |
| 111 = 0; |
| 112 |
| 98 // Runs the file display test, shared by sub classes. | 113 // Runs the file display test, shared by sub classes. |
| 99 void DoTestFileDisplay(); | 114 void DoTestFileDisplay(); |
| 100 }; | 115 }; |
| 101 | 116 |
| 102 void FileManagerBrowserTestBase::StartFileManager( | 117 void FileManagerBrowserTestBase::StartFileManager( |
| 103 const std::string& directory_path) { | 118 const std::string& directory_path) { |
| 104 std::string file_manager_url = | 119 std::string file_manager_url = |
| 105 (std::string("chrome-extension://") + | 120 (std::string("chrome-extension://") + |
| 106 kFileManagerExtensionId + | 121 kFileManagerExtensionId + |
| 107 "/main.html#" + | 122 "/main.html#" + |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 StartTest("file display"); | 157 StartTest("file display"); |
| 143 | 158 |
| 144 ExtensionTestMessageListener listener("initial check done", true); | 159 ExtensionTestMessageListener listener("initial check done", true); |
| 145 ASSERT_TRUE(listener.WaitUntilSatisfied()); | 160 ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 146 CreateTestFile("newly added file.mp3", 2000, "4 Sep 1998 00:00:00"); | 161 CreateTestFile("newly added file.mp3", 2000, "4 Sep 1998 00:00:00"); |
| 147 listener.Reply("file added"); | 162 listener.Reply("file added"); |
| 148 | 163 |
| 149 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | 164 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 150 } | 165 } |
| 151 | 166 |
| 167 // Monitors changes to a single file until the supplied condition callback |
| 168 // returns true. Usage: |
| 169 // TestFilePathWatcher watcher(path_to_file, MyConditionCallback); |
| 170 // watcher.StartAndWaitUntilReady(); |
| 171 // ... trigger filesystem modification ... |
| 172 // watcher.RunMessageLoopUntilConditionSatisfied(); |
| 173 class TestFilePathWatcher { |
| 174 public: |
| 175 typedef base::Callback<bool(const base::FilePath& file_path)> |
| 176 ConditionCallback; |
| 177 |
| 178 // Stores the supplied |path| and |condition| for later use (no side effects). |
| 179 TestFilePathWatcher(const base::FilePath& path, |
| 180 const ConditionCallback& condition); |
| 181 |
| 182 // Waits (running a message pump) until the callback returns true or |
| 183 // FilePathWatcher reports an error. Return true on success. |
| 184 bool RunMessageLoopUntilConditionSatisfied(); |
| 185 |
| 186 private: |
| 187 // Starts the FilePathWatcher to watch the target file. Also check if the |
| 188 // condition is already met. |
| 189 void StartWatching(); |
| 190 |
| 191 // FilePathWatcher callback (on the FILE thread). Posts Done() to the UI |
| 192 // thread when the condition is satisfied or there is an error. |
| 193 void FilePathWatcherCallback(const base::FilePath& path, bool error); |
| 194 |
| 195 const base::FilePath path_; |
| 196 ConditionCallback condition_; |
| 197 scoped_ptr<base::FilePathWatcher> watcher_; |
| 198 base::RunLoop run_loop_; |
| 199 base::Closure quit_closure_; |
| 200 bool failed_; |
| 201 }; |
| 202 |
| 203 TestFilePathWatcher::TestFilePathWatcher(const base::FilePath& path, |
| 204 const ConditionCallback& condition) |
| 205 : path_(path), |
| 206 condition_(condition), |
| 207 quit_closure_(run_loop_.QuitClosure()), |
| 208 failed_(false) { |
| 209 } |
| 210 |
| 211 void TestFilePathWatcher::StartWatching() { |
| 212 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 213 |
| 214 watcher_.reset(new base::FilePathWatcher); |
| 215 bool ok = watcher_->Watch( |
| 216 path_, false /*recursive*/, |
| 217 base::Bind(&TestFilePathWatcher::FilePathWatcherCallback, |
| 218 base::Unretained(this))); |
| 219 DCHECK(ok); |
| 220 |
| 221 // If the condition was already met before FilePathWatcher was launched, |
| 222 // FilePathWatcher won't be able to detect a change, so check the condition |
| 223 // here. |
| 224 if (condition_.Run(path_)) { |
| 225 watcher_.reset(); |
| 226 content::BrowserThread::PostTask(content::BrowserThread::UI, |
| 227 FROM_HERE, |
| 228 quit_closure_); |
| 229 return; |
| 230 } |
| 231 } |
| 232 |
| 233 void TestFilePathWatcher::FilePathWatcherCallback(const base::FilePath& path, |
| 234 bool failed) { |
| 235 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 236 DCHECK_EQ(path_.value(), path.value()); |
| 237 |
| 238 if (failed || condition_.Run(path)) { |
| 239 failed_ = failed; |
| 240 watcher_.reset(); |
| 241 content::BrowserThread::PostTask(content::BrowserThread::UI, |
| 242 FROM_HERE, |
| 243 quit_closure_); |
| 244 } |
| 245 } |
| 246 |
| 247 bool TestFilePathWatcher::RunMessageLoopUntilConditionSatisfied() { |
| 248 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 249 |
| 250 content::BrowserThread::PostTask( |
| 251 content::BrowserThread::FILE, |
| 252 FROM_HERE, |
| 253 base::Bind(&TestFilePathWatcher::StartWatching, |
| 254 base::Unretained(this))); |
| 255 |
| 256 // Wait until the condition is met. |
| 257 run_loop_.Run(); |
| 258 return !failed_; |
| 259 } |
| 260 |
| 261 // Returns true if a file with the given size is present at |path|. |
| 262 bool FilePresentWithSize(const int64 file_size, |
| 263 const base::FilePath& path) { |
| 264 int64 copy_size = 0; |
| 265 // If the file doesn't exist yet this will fail and we'll keep waiting. |
| 266 if (!file_util::GetFileSize(path, ©_size)) |
| 267 return false; |
| 268 return (copy_size == file_size); |
| 269 } |
| 270 |
| 271 // Returns true if a file is not present at |path|. |
| 272 bool FileNotPresent(const base::FilePath& path) { |
| 273 return !file_util::PathExists(path); |
| 274 }; |
| 275 |
| 152 // The boolean parameter, retrieved by GetParam(), is true if testing in the | 276 // The boolean parameter, retrieved by GetParam(), is true if testing in the |
| 153 // guest mode. See SetUpCommandLine() below for details. | 277 // guest mode. See SetUpCommandLine() below for details. |
| 154 class FileManagerBrowserLocalTest : public FileManagerBrowserTestBase, | 278 class FileManagerBrowserLocalTest : public FileManagerBrowserTestBase, |
| 155 public ::testing::WithParamInterface<bool> { | 279 public ::testing::WithParamInterface<bool> { |
| 156 public: | 280 public: |
| 157 virtual void SetUp() OVERRIDE { | 281 virtual void SetUp() OVERRIDE { |
| 158 extensions::ComponentLoader::EnableBackgroundExtensionsForTesting(); | 282 extensions::ComponentLoader::EnableBackgroundExtensionsForTesting(); |
| 159 | 283 |
| 160 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); | 284 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); |
| 161 downloads_path_ = tmp_dir_.path().Append("Downloads"); | 285 downloads_path_ = tmp_dir_.path().Append("Downloads"); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 176 } | 300 } |
| 177 | 301 |
| 178 protected: | 302 protected: |
| 179 // FileManagerBrowserTestBase overrides. | 303 // FileManagerBrowserTestBase overrides. |
| 180 virtual void CreateTestFile(const std::string& name, | 304 virtual void CreateTestFile(const std::string& name, |
| 181 int64 length, | 305 int64 length, |
| 182 const std::string& modification_time) OVERRIDE; | 306 const std::string& modification_time) OVERRIDE; |
| 183 virtual void CreateTestDirectory( | 307 virtual void CreateTestDirectory( |
| 184 const std::string& name, | 308 const std::string& name, |
| 185 const std::string& modification_time) OVERRIDE; | 309 const std::string& modification_time) OVERRIDE; |
| 310 virtual base::FilePath GetRootPath() OVERRIDE; |
| 311 virtual bool PathExists(const base::FilePath& file_path) OVERRIDE; |
| 312 virtual bool WaitUntilFilePresentWithSize(const base::FilePath& file_path, |
| 313 int64 file_size) OVERRIDE; |
| 314 virtual bool WaitUntilFileNotPresent(const base::FilePath& file_path) |
| 315 OVERRIDE; |
| 186 | 316 |
| 187 // Add a mount point to the fake Downloads directory. Should be called | 317 // Add a mount point to the fake Downloads directory. Should be called |
| 188 // before StartFileManager(). | 318 // before StartFileManager(). |
| 189 void AddMountPointToFakeDownloads(); | 319 void AddMountPointToFakeDownloads(); |
| 190 | 320 |
| 191 // Path to the fake Downloads directory used in the test. | 321 // Path to the fake Downloads directory used in the test. |
| 192 base::FilePath downloads_path_; | 322 base::FilePath downloads_path_; |
| 193 | 323 |
| 194 private: | 324 private: |
| 195 base::ScopedTempDir tmp_dir_; | 325 base::ScopedTempDir tmp_dir_; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 226 void FileManagerBrowserLocalTest::CreateTestDirectory( | 356 void FileManagerBrowserLocalTest::CreateTestDirectory( |
| 227 const std::string& name, | 357 const std::string& name, |
| 228 const std::string& modification_time) { | 358 const std::string& modification_time) { |
| 229 base::FilePath path = downloads_path_.AppendASCII(name); | 359 base::FilePath path = downloads_path_.AppendASCII(name); |
| 230 ASSERT_TRUE(file_util::CreateDirectory(path)); | 360 ASSERT_TRUE(file_util::CreateDirectory(path)); |
| 231 base::Time time; | 361 base::Time time; |
| 232 ASSERT_TRUE(base::Time::FromString(modification_time.c_str(), &time)); | 362 ASSERT_TRUE(base::Time::FromString(modification_time.c_str(), &time)); |
| 233 ASSERT_TRUE(file_util::SetLastModifiedTime(path, time)); | 363 ASSERT_TRUE(file_util::SetLastModifiedTime(path, time)); |
| 234 } | 364 } |
| 235 | 365 |
| 366 base::FilePath FileManagerBrowserLocalTest::GetRootPath() { |
| 367 return downloads_path_; |
| 368 } |
| 369 |
| 370 bool FileManagerBrowserLocalTest::PathExists(const base::FilePath& file_path) { |
| 371 return file_util::PathExists(file_path); |
| 372 } |
| 373 |
| 374 bool FileManagerBrowserLocalTest::WaitUntilFilePresentWithSize( |
| 375 const base::FilePath& file_path, |
| 376 int64 file_size) { |
| 377 TestFilePathWatcher watcher(file_path, base::Bind(FilePresentWithSize, |
| 378 file_size)); |
| 379 return watcher.RunMessageLoopUntilConditionSatisfied(); |
| 380 } |
| 381 |
| 382 bool FileManagerBrowserLocalTest::WaitUntilFileNotPresent( |
| 383 const base::FilePath& file_path) { |
| 384 TestFilePathWatcher watcher(file_path, base::Bind(FileNotPresent)); |
| 385 return watcher.RunMessageLoopUntilConditionSatisfied(); |
| 386 } |
| 387 |
| 236 void FileManagerBrowserLocalTest::AddMountPointToFakeDownloads() { | 388 void FileManagerBrowserLocalTest::AddMountPointToFakeDownloads() { |
| 237 // Install our fake Downloads mount point first. | 389 // Install our fake Downloads mount point first. |
| 238 fileapi::ExternalMountPoints* mount_points = | 390 fileapi::ExternalMountPoints* mount_points = |
| 239 content::BrowserContext::GetMountPoints(profile()); | 391 content::BrowserContext::GetMountPoints(profile()); |
| 240 ASSERT_TRUE(mount_points->RevokeFileSystem("Downloads")); | 392 ASSERT_TRUE(mount_points->RevokeFileSystem("Downloads")); |
| 241 ASSERT_TRUE(mount_points->RegisterFileSystem( | 393 ASSERT_TRUE(mount_points->RegisterFileSystem( |
| 242 "Downloads", fileapi::kFileSystemTypeNativeLocal, downloads_path_)); | 394 "Downloads", fileapi::kFileSystemTypeNativeLocal, downloads_path_)); |
| 243 } | 395 } |
| 244 | 396 |
| 245 class FileManagerBrowserDriveTest : public FileManagerBrowserTestBase { | 397 class FileManagerBrowserDriveTest : public FileManagerBrowserTestBase { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 262 } | 414 } |
| 263 | 415 |
| 264 protected: | 416 protected: |
| 265 // FileManagerBrowserTestBase overrides. | 417 // FileManagerBrowserTestBase overrides. |
| 266 virtual void CreateTestFile(const std::string& name, | 418 virtual void CreateTestFile(const std::string& name, |
| 267 int64 length, | 419 int64 length, |
| 268 const std::string& modification_time) OVERRIDE; | 420 const std::string& modification_time) OVERRIDE; |
| 269 virtual void CreateTestDirectory( | 421 virtual void CreateTestDirectory( |
| 270 const std::string& name, | 422 const std::string& name, |
| 271 const std::string& modification_time) OVERRIDE; | 423 const std::string& modification_time) OVERRIDE; |
| 424 virtual base::FilePath GetRootPath() OVERRIDE; |
| 425 virtual bool PathExists(const base::FilePath& file_path) OVERRIDE; |
| 426 virtual bool WaitUntilFilePresentWithSize(const base::FilePath& file_path, |
| 427 int64 file_size) OVERRIDE; |
| 428 virtual bool WaitUntilFileNotPresent(const base::FilePath& file_path) |
| 429 OVERRIDE; |
| 272 | 430 |
| 273 // Notifies DriveFileSystem that the contents in FakeDriveService are | 431 // Notifies DriveFileSystem that the contents in FakeDriveService are |
| 274 // changed, hence the new contents should be fetched. | 432 // changed, hence the new contents should be fetched. |
| 275 void CheckForUpdates(); | 433 void CheckForUpdates(); |
| 276 | 434 |
| 277 // DriveSystemService factory function for this test. | 435 // DriveSystemService factory function for this test. |
| 278 drive::DriveSystemService* CreateDriveSystemService(Profile* profile); | 436 drive::DriveSystemService* CreateDriveSystemService(Profile* profile); |
| 279 | 437 |
| 280 base::ScopedTempDir test_cache_root_; | 438 base::ScopedTempDir test_cache_root_; |
| 281 google_apis::FakeDriveService* fake_drive_service_; | 439 google_apis::FakeDriveService* fake_drive_service_; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 time, | 492 time, |
| 335 google_apis::test_util::CreateCopyResultCallback(&error, | 493 google_apis::test_util::CreateCopyResultCallback(&error, |
| 336 &resource_entry)); | 494 &resource_entry)); |
| 337 MessageLoop::current()->RunUntilIdle(); | 495 MessageLoop::current()->RunUntilIdle(); |
| 338 ASSERT_EQ(google_apis::HTTP_SUCCESS, error); | 496 ASSERT_EQ(google_apis::HTTP_SUCCESS, error); |
| 339 ASSERT_TRUE(resource_entry); | 497 ASSERT_TRUE(resource_entry); |
| 340 | 498 |
| 341 CheckForUpdates(); | 499 CheckForUpdates(); |
| 342 } | 500 } |
| 343 | 501 |
| 502 base::FilePath FileManagerBrowserDriveTest::GetRootPath() { |
| 503 return base::FilePath(drive::util::kDriveMyDriveRootPath); |
| 504 } |
| 505 |
| 506 bool FileManagerBrowserDriveTest::PathExists(const base::FilePath& file_path) { |
| 507 // TODO(satorux): Implement this. crbug.com/224534 |
| 508 return true; |
| 509 } |
| 510 |
| 511 bool FileManagerBrowserDriveTest::WaitUntilFilePresentWithSize( |
| 512 const base::FilePath& file_path, |
| 513 int64 file_size) { |
| 514 // TODO(satorux): Implement this. crbug.com/224534 |
| 515 return true; |
| 516 } |
| 517 |
| 518 bool FileManagerBrowserDriveTest::WaitUntilFileNotPresent( |
| 519 const base::FilePath& file_path) { |
| 520 // TODO(satorux): Implement this. crbug.com/224534 |
| 521 return true; |
| 522 } |
| 523 |
| 344 void FileManagerBrowserDriveTest::CheckForUpdates() { | 524 void FileManagerBrowserDriveTest::CheckForUpdates() { |
| 345 if (system_service_ && system_service_->file_system()) { | 525 if (system_service_ && system_service_->file_system()) { |
| 346 system_service_->file_system()->CheckForUpdates(); | 526 system_service_->file_system()->CheckForUpdates(); |
| 347 } | 527 } |
| 348 } | 528 } |
| 349 | 529 |
| 350 drive::DriveSystemService* | 530 drive::DriveSystemService* |
| 351 FileManagerBrowserDriveTest::CreateDriveSystemService(Profile* profile) { | 531 FileManagerBrowserDriveTest::CreateDriveSystemService(Profile* profile) { |
| 352 fake_drive_service_ = new google_apis::FakeDriveService; | 532 fake_drive_service_ = new google_apis::FakeDriveService; |
| 353 fake_drive_service_->LoadResourceListForWapi( | 533 fake_drive_service_->LoadResourceListForWapi( |
| 354 "chromeos/gdata/empty_feed.json"); | 534 "chromeos/gdata/empty_feed.json"); |
| 355 fake_drive_service_->LoadAccountMetadataForWapi( | 535 fake_drive_service_->LoadAccountMetadataForWapi( |
| 356 "chromeos/gdata/account_metadata.json"); | 536 "chromeos/gdata/account_metadata.json"); |
| 357 fake_drive_service_->LoadAppListForDriveApi("chromeos/drive/applist.json"); | 537 fake_drive_service_->LoadAppListForDriveApi("chromeos/drive/applist.json"); |
| 358 | 538 |
| 359 // Create test files and directories inside the fake drive service. | 539 // Create test files and directories inside the fake drive service. |
| 360 CreateTestFilesAndDirectories(); | 540 CreateTestFilesAndDirectories(); |
| 361 | 541 |
| 362 system_service_ = new drive::DriveSystemService(profile, | 542 system_service_ = new drive::DriveSystemService(profile, |
| 363 fake_drive_service_, | 543 fake_drive_service_, |
| 364 test_cache_root_.path(), | 544 test_cache_root_.path(), |
| 365 NULL); | 545 NULL); |
| 366 | 546 |
| 367 return system_service_; | 547 return system_service_; |
| 368 } | 548 } |
| 369 | 549 |
| 370 // Monitors changes to a single file until the supplied condition callback | |
| 371 // returns true. Usage: | |
| 372 // TestFilePathWatcher watcher(path_to_file, MyConditionCallback); | |
| 373 // watcher.StartAndWaitUntilReady(); | |
| 374 // ... trigger filesystem modification ... | |
| 375 // watcher.RunMessageLoopUntilConditionSatisfied(); | |
| 376 class TestFilePathWatcher { | |
| 377 public: | |
| 378 typedef base::Callback<bool(const base::FilePath& file_path)> | |
| 379 ConditionCallback; | |
| 380 | |
| 381 // Stores the supplied |path| and |condition| for later use (no side effects). | |
| 382 TestFilePathWatcher(const base::FilePath& path, | |
| 383 const ConditionCallback& condition); | |
| 384 | |
| 385 // Waits (running a message pump) until the callback returns true or | |
| 386 // FilePathWatcher reports an error. Return true on success. | |
| 387 bool RunMessageLoopUntilConditionSatisfied(); | |
| 388 | |
| 389 private: | |
| 390 // Starts the FilePathWatcher to watch the target file. Also check if the | |
| 391 // condition is already met. | |
| 392 void StartWatching(); | |
| 393 | |
| 394 // FilePathWatcher callback (on the FILE thread). Posts Done() to the UI | |
| 395 // thread when the condition is satisfied or there is an error. | |
| 396 void FilePathWatcherCallback(const base::FilePath& path, bool error); | |
| 397 | |
| 398 const base::FilePath path_; | |
| 399 ConditionCallback condition_; | |
| 400 scoped_ptr<base::FilePathWatcher> watcher_; | |
| 401 base::RunLoop run_loop_; | |
| 402 base::Closure quit_closure_; | |
| 403 bool failed_; | |
| 404 }; | |
| 405 | |
| 406 TestFilePathWatcher::TestFilePathWatcher(const base::FilePath& path, | |
| 407 const ConditionCallback& condition) | |
| 408 : path_(path), | |
| 409 condition_(condition), | |
| 410 quit_closure_(run_loop_.QuitClosure()), | |
| 411 failed_(false) { | |
| 412 } | |
| 413 | |
| 414 void TestFilePathWatcher::StartWatching() { | |
| 415 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
| 416 | |
| 417 watcher_.reset(new base::FilePathWatcher); | |
| 418 bool ok = watcher_->Watch( | |
| 419 path_, false /*recursive*/, | |
| 420 base::Bind(&TestFilePathWatcher::FilePathWatcherCallback, | |
| 421 base::Unretained(this))); | |
| 422 DCHECK(ok); | |
| 423 | |
| 424 // If the condition was already met before FilePathWatcher was launched, | |
| 425 // FilePathWatcher won't be able to detect a change, so check the condition | |
| 426 // here. | |
| 427 if (condition_.Run(path_)) { | |
| 428 watcher_.reset(); | |
| 429 content::BrowserThread::PostTask(content::BrowserThread::UI, | |
| 430 FROM_HERE, | |
| 431 quit_closure_); | |
| 432 return; | |
| 433 } | |
| 434 } | |
| 435 | |
| 436 void TestFilePathWatcher::FilePathWatcherCallback(const base::FilePath& path, | |
| 437 bool failed) { | |
| 438 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
| 439 DCHECK_EQ(path_.value(), path.value()); | |
| 440 | |
| 441 if (failed || condition_.Run(path)) { | |
| 442 failed_ = failed; | |
| 443 watcher_.reset(); | |
| 444 content::BrowserThread::PostTask(content::BrowserThread::UI, | |
| 445 FROM_HERE, | |
| 446 quit_closure_); | |
| 447 } | |
| 448 } | |
| 449 | |
| 450 bool TestFilePathWatcher::RunMessageLoopUntilConditionSatisfied() { | |
| 451 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 452 | |
| 453 content::BrowserThread::PostTask( | |
| 454 content::BrowserThread::FILE, | |
| 455 FROM_HERE, | |
| 456 base::Bind(&TestFilePathWatcher::StartWatching, | |
| 457 base::Unretained(this))); | |
| 458 | |
| 459 // Wait until the condition is met. | |
| 460 run_loop_.Run(); | |
| 461 return !failed_; | |
| 462 } | |
| 463 | |
| 464 // Returns true if a file with the given size is present at |path|. | |
| 465 bool FilePresentWithSize(const int64 file_size, | |
| 466 const base::FilePath& path) { | |
| 467 int64 copy_size = 0; | |
| 468 // If the file doesn't exist yet this will fail and we'll keep waiting. | |
| 469 if (!file_util::GetFileSize(path, ©_size)) | |
| 470 return false; | |
| 471 return (copy_size == file_size); | |
| 472 } | |
| 473 | |
| 474 // Returns true if a file is not present at |path|. | |
| 475 bool FileNotPresent(const base::FilePath& path) { | |
| 476 return !file_util::PathExists(path); | |
| 477 }; | |
| 478 | |
| 479 IN_PROC_BROWSER_TEST_P(FileManagerBrowserLocalTest, TestFileDisplay) { | 550 IN_PROC_BROWSER_TEST_P(FileManagerBrowserLocalTest, TestFileDisplay) { |
| 480 AddMountPointToFakeDownloads(); | 551 AddMountPointToFakeDownloads(); |
| 481 StartFileManager("/Downloads"); | 552 StartFileManager("/Downloads"); |
| 482 | 553 |
| 483 DoTestFileDisplay(); | 554 DoTestFileDisplay(); |
| 484 } | 555 } |
| 485 | 556 |
| 486 IN_PROC_BROWSER_TEST_P(FileManagerBrowserLocalTest, TestKeyboardCopy) { | 557 IN_PROC_BROWSER_TEST_P(FileManagerBrowserLocalTest, TestKeyboardCopy) { |
| 487 AddMountPointToFakeDownloads(); | 558 AddMountPointToFakeDownloads(); |
| 488 StartFileManager("/Downloads"); | 559 StartFileManager("/Downloads"); |
| 489 | 560 |
| 490 base::FilePath copy_path = | 561 base::FilePath copy_path = |
| 491 downloads_path_.AppendASCII(kKeyboardTestFileCopyName); | 562 GetRootPath().AppendASCII(kKeyboardTestFileCopyName); |
| 492 ASSERT_FALSE(file_util::PathExists(copy_path)); | 563 ASSERT_FALSE(PathExists(copy_path)); |
| 493 | 564 |
| 494 ResultCatcher catcher; | 565 ResultCatcher catcher; |
| 495 StartTest("keyboard copy"); | 566 StartTest("keyboard copy"); |
| 496 | 567 |
| 497 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | 568 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 498 | 569 |
| 499 TestFilePathWatcher watcher( | 570 ASSERT_TRUE(WaitUntilFilePresentWithSize(copy_path, kKeyboardTestFileSize)); |
| 500 copy_path, | |
| 501 base::Bind(FilePresentWithSize, kKeyboardTestFileSize)); | |
| 502 ASSERT_TRUE(watcher.RunMessageLoopUntilConditionSatisfied()); | |
| 503 | 571 |
| 504 // Check that it was a copy, not a move. | 572 // Check that it was a copy, not a move. |
| 505 base::FilePath source_path = | 573 base::FilePath source_path = |
| 506 downloads_path_.AppendASCII(kKeyboardTestFileName); | 574 GetRootPath().AppendASCII(kKeyboardTestFileName); |
| 507 ASSERT_TRUE(file_util::PathExists(source_path)); | 575 ASSERT_TRUE(PathExists(source_path)); |
| 508 } | 576 } |
| 509 | 577 |
| 510 IN_PROC_BROWSER_TEST_P(FileManagerBrowserLocalTest, TestKeyboardDelete) { | 578 IN_PROC_BROWSER_TEST_P(FileManagerBrowserLocalTest, TestKeyboardDelete) { |
| 511 AddMountPointToFakeDownloads(); | 579 AddMountPointToFakeDownloads(); |
| 512 StartFileManager("/Downloads"); | 580 StartFileManager("/Downloads"); |
| 513 | 581 |
| 514 base::FilePath delete_path = | 582 base::FilePath delete_path = |
| 515 downloads_path_.AppendASCII(kKeyboardTestFileName); | 583 GetRootPath().AppendASCII(kKeyboardTestFileName); |
| 516 ASSERT_TRUE(file_util::PathExists(delete_path)); | 584 ASSERT_TRUE(PathExists(delete_path)); |
| 517 | 585 |
| 518 ResultCatcher catcher; | 586 ResultCatcher catcher; |
| 519 StartTest("keyboard delete"); | 587 StartTest("keyboard delete"); |
| 520 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | 588 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 521 | 589 |
| 522 TestFilePathWatcher watcher(delete_path, | 590 ASSERT_TRUE(WaitUntilFileNotPresent(delete_path)); |
| 523 base::Bind(FileNotPresent)); | |
| 524 ASSERT_TRUE(watcher.RunMessageLoopUntilConditionSatisfied()); | |
| 525 } | 591 } |
| 526 | 592 |
| 527 IN_PROC_BROWSER_TEST_F(FileManagerBrowserDriveTest, TestFileDisplay) { | 593 IN_PROC_BROWSER_TEST_F(FileManagerBrowserDriveTest, TestFileDisplay) { |
| 528 StartFileManager("/drive/root"); | 594 StartFileManager("/drive/root"); |
| 529 | 595 |
| 530 DoTestFileDisplay(); | 596 DoTestFileDisplay(); |
| 531 } | 597 } |
| 532 | 598 |
| 533 } // namespace | 599 } // namespace |
| OLD | NEW |