| Index: chrome/browser/chromeos/extensions/file_manager/file_manager_browsertest.cc
|
| diff --git a/chrome/browser/chromeos/extensions/file_manager/file_manager_browsertest.cc b/chrome/browser/chromeos/extensions/file_manager/file_manager_browsertest.cc
|
| index 827743231fd5fceb2ea06bdf2baa3177607c7175..aa70851aebf299e5fe67695993b219b7ec579e73 100644
|
| --- a/chrome/browser/chromeos/extensions/file_manager/file_manager_browsertest.cc
|
| +++ b/chrome/browser/chromeos/extensions/file_manager/file_manager_browsertest.cc
|
| @@ -95,6 +95,21 @@ class FileManagerBrowserTestBase : public ExtensionApiTest {
|
| const std::string& name,
|
| const std::string& modification_time) = 0;
|
|
|
| + // Returns the path of the root directory.
|
| + virtual base::FilePath GetRootPath() = 0;
|
| +
|
| + // Returns true if |file_path| exists.
|
| + virtual bool PathExists(const base::FilePath& file_path) = 0;
|
| +
|
| + // Waits until a file with the given size is present at |path|. Returns
|
| + // true on success.
|
| + virtual bool WaitUntilFilePresentWithSize(const base::FilePath& file_path,
|
| + int64 file_size) = 0;
|
| +
|
| + // Waits until a file is not present at |path|. Returns true on success.
|
| + virtual bool WaitUntilFileNotPresent(const base::FilePath& file_path)
|
| + = 0;
|
| +
|
| // Runs the file display test, shared by sub classes.
|
| void DoTestFileDisplay();
|
| };
|
| @@ -149,6 +164,115 @@ void FileManagerBrowserTestBase::DoTestFileDisplay() {
|
| ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
|
| }
|
|
|
| +// Monitors changes to a single file until the supplied condition callback
|
| +// returns true. Usage:
|
| +// TestFilePathWatcher watcher(path_to_file, MyConditionCallback);
|
| +// watcher.StartAndWaitUntilReady();
|
| +// ... trigger filesystem modification ...
|
| +// watcher.RunMessageLoopUntilConditionSatisfied();
|
| +class TestFilePathWatcher {
|
| + public:
|
| + typedef base::Callback<bool(const base::FilePath& file_path)>
|
| + ConditionCallback;
|
| +
|
| + // Stores the supplied |path| and |condition| for later use (no side effects).
|
| + TestFilePathWatcher(const base::FilePath& path,
|
| + const ConditionCallback& condition);
|
| +
|
| + // Waits (running a message pump) until the callback returns true or
|
| + // FilePathWatcher reports an error. Return true on success.
|
| + bool RunMessageLoopUntilConditionSatisfied();
|
| +
|
| + private:
|
| + // Starts the FilePathWatcher to watch the target file. Also check if the
|
| + // condition is already met.
|
| + void StartWatching();
|
| +
|
| + // FilePathWatcher callback (on the FILE thread). Posts Done() to the UI
|
| + // thread when the condition is satisfied or there is an error.
|
| + void FilePathWatcherCallback(const base::FilePath& path, bool error);
|
| +
|
| + const base::FilePath path_;
|
| + ConditionCallback condition_;
|
| + scoped_ptr<base::FilePathWatcher> watcher_;
|
| + base::RunLoop run_loop_;
|
| + base::Closure quit_closure_;
|
| + bool failed_;
|
| +};
|
| +
|
| +TestFilePathWatcher::TestFilePathWatcher(const base::FilePath& path,
|
| + const ConditionCallback& condition)
|
| + : path_(path),
|
| + condition_(condition),
|
| + quit_closure_(run_loop_.QuitClosure()),
|
| + failed_(false) {
|
| +}
|
| +
|
| +void TestFilePathWatcher::StartWatching() {
|
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
|
| +
|
| + watcher_.reset(new base::FilePathWatcher);
|
| + bool ok = watcher_->Watch(
|
| + path_, false /*recursive*/,
|
| + base::Bind(&TestFilePathWatcher::FilePathWatcherCallback,
|
| + base::Unretained(this)));
|
| + DCHECK(ok);
|
| +
|
| + // If the condition was already met before FilePathWatcher was launched,
|
| + // FilePathWatcher won't be able to detect a change, so check the condition
|
| + // here.
|
| + if (condition_.Run(path_)) {
|
| + watcher_.reset();
|
| + content::BrowserThread::PostTask(content::BrowserThread::UI,
|
| + FROM_HERE,
|
| + quit_closure_);
|
| + return;
|
| + }
|
| +}
|
| +
|
| +void TestFilePathWatcher::FilePathWatcherCallback(const base::FilePath& path,
|
| + bool failed) {
|
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
|
| + DCHECK_EQ(path_.value(), path.value());
|
| +
|
| + if (failed || condition_.Run(path)) {
|
| + failed_ = failed;
|
| + watcher_.reset();
|
| + content::BrowserThread::PostTask(content::BrowserThread::UI,
|
| + FROM_HERE,
|
| + quit_closure_);
|
| + }
|
| +}
|
| +
|
| +bool TestFilePathWatcher::RunMessageLoopUntilConditionSatisfied() {
|
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
| +
|
| + content::BrowserThread::PostTask(
|
| + content::BrowserThread::FILE,
|
| + FROM_HERE,
|
| + base::Bind(&TestFilePathWatcher::StartWatching,
|
| + base::Unretained(this)));
|
| +
|
| + // Wait until the condition is met.
|
| + run_loop_.Run();
|
| + return !failed_;
|
| +}
|
| +
|
| +// Returns true if a file with the given size is present at |path|.
|
| +bool FilePresentWithSize(const int64 file_size,
|
| + const base::FilePath& path) {
|
| + int64 copy_size = 0;
|
| + // If the file doesn't exist yet this will fail and we'll keep waiting.
|
| + if (!file_util::GetFileSize(path, ©_size))
|
| + return false;
|
| + return (copy_size == file_size);
|
| +}
|
| +
|
| +// Returns true if a file is not present at |path|.
|
| +bool FileNotPresent(const base::FilePath& path) {
|
| + return !file_util::PathExists(path);
|
| +};
|
| +
|
| // The boolean parameter, retrieved by GetParam(), is true if testing in the
|
| // guest mode. See SetUpCommandLine() below for details.
|
| class FileManagerBrowserLocalTest : public FileManagerBrowserTestBase,
|
| @@ -183,6 +307,12 @@ class FileManagerBrowserLocalTest : public FileManagerBrowserTestBase,
|
| virtual void CreateTestDirectory(
|
| const std::string& name,
|
| const std::string& modification_time) OVERRIDE;
|
| + virtual base::FilePath GetRootPath() OVERRIDE;
|
| + virtual bool PathExists(const base::FilePath& file_path) OVERRIDE;
|
| + virtual bool WaitUntilFilePresentWithSize(const base::FilePath& file_path,
|
| + int64 file_size) OVERRIDE;
|
| + virtual bool WaitUntilFileNotPresent(const base::FilePath& file_path)
|
| + OVERRIDE;
|
|
|
| // Add a mount point to the fake Downloads directory. Should be called
|
| // before StartFileManager().
|
| @@ -233,6 +363,28 @@ void FileManagerBrowserLocalTest::CreateTestDirectory(
|
| ASSERT_TRUE(file_util::SetLastModifiedTime(path, time));
|
| }
|
|
|
| +base::FilePath FileManagerBrowserLocalTest::GetRootPath() {
|
| + return downloads_path_;
|
| +}
|
| +
|
| +bool FileManagerBrowserLocalTest::PathExists(const base::FilePath& file_path) {
|
| + return file_util::PathExists(file_path);
|
| +}
|
| +
|
| +bool FileManagerBrowserLocalTest::WaitUntilFilePresentWithSize(
|
| + const base::FilePath& file_path,
|
| + int64 file_size) {
|
| + TestFilePathWatcher watcher(file_path, base::Bind(FilePresentWithSize,
|
| + file_size));
|
| + return watcher.RunMessageLoopUntilConditionSatisfied();
|
| +}
|
| +
|
| +bool FileManagerBrowserLocalTest::WaitUntilFileNotPresent(
|
| + const base::FilePath& file_path) {
|
| + TestFilePathWatcher watcher(file_path, base::Bind(FileNotPresent));
|
| + return watcher.RunMessageLoopUntilConditionSatisfied();
|
| +}
|
| +
|
| void FileManagerBrowserLocalTest::AddMountPointToFakeDownloads() {
|
| // Install our fake Downloads mount point first.
|
| fileapi::ExternalMountPoints* mount_points =
|
| @@ -269,6 +421,12 @@ class FileManagerBrowserDriveTest : public FileManagerBrowserTestBase {
|
| virtual void CreateTestDirectory(
|
| const std::string& name,
|
| const std::string& modification_time) OVERRIDE;
|
| + virtual base::FilePath GetRootPath() OVERRIDE;
|
| + virtual bool PathExists(const base::FilePath& file_path) OVERRIDE;
|
| + virtual bool WaitUntilFilePresentWithSize(const base::FilePath& file_path,
|
| + int64 file_size) OVERRIDE;
|
| + virtual bool WaitUntilFileNotPresent(const base::FilePath& file_path)
|
| + OVERRIDE;
|
|
|
| // Notifies DriveFileSystem that the contents in FakeDriveService are
|
| // changed, hence the new contents should be fetched.
|
| @@ -341,6 +499,28 @@ void FileManagerBrowserDriveTest::CreateTestDirectory(
|
| CheckForUpdates();
|
| }
|
|
|
| +base::FilePath FileManagerBrowserDriveTest::GetRootPath() {
|
| + return base::FilePath(drive::util::kDriveMyDriveRootPath);
|
| +}
|
| +
|
| +bool FileManagerBrowserDriveTest::PathExists(const base::FilePath& file_path) {
|
| + // TODO(satorux): Implement this. crbug.com/224534
|
| + return true;
|
| +}
|
| +
|
| +bool FileManagerBrowserDriveTest::WaitUntilFilePresentWithSize(
|
| + const base::FilePath& file_path,
|
| + int64 file_size) {
|
| + // TODO(satorux): Implement this. crbug.com/224534
|
| + return true;
|
| +}
|
| +
|
| +bool FileManagerBrowserDriveTest::WaitUntilFileNotPresent(
|
| + const base::FilePath& file_path) {
|
| + // TODO(satorux): Implement this. crbug.com/224534
|
| + return true;
|
| +}
|
| +
|
| void FileManagerBrowserDriveTest::CheckForUpdates() {
|
| if (system_service_ && system_service_->file_system()) {
|
| system_service_->file_system()->CheckForUpdates();
|
| @@ -367,115 +547,6 @@ FileManagerBrowserDriveTest::CreateDriveSystemService(Profile* profile) {
|
| return system_service_;
|
| }
|
|
|
| -// Monitors changes to a single file until the supplied condition callback
|
| -// returns true. Usage:
|
| -// TestFilePathWatcher watcher(path_to_file, MyConditionCallback);
|
| -// watcher.StartAndWaitUntilReady();
|
| -// ... trigger filesystem modification ...
|
| -// watcher.RunMessageLoopUntilConditionSatisfied();
|
| -class TestFilePathWatcher {
|
| - public:
|
| - typedef base::Callback<bool(const base::FilePath& file_path)>
|
| - ConditionCallback;
|
| -
|
| - // Stores the supplied |path| and |condition| for later use (no side effects).
|
| - TestFilePathWatcher(const base::FilePath& path,
|
| - const ConditionCallback& condition);
|
| -
|
| - // Waits (running a message pump) until the callback returns true or
|
| - // FilePathWatcher reports an error. Return true on success.
|
| - bool RunMessageLoopUntilConditionSatisfied();
|
| -
|
| - private:
|
| - // Starts the FilePathWatcher to watch the target file. Also check if the
|
| - // condition is already met.
|
| - void StartWatching();
|
| -
|
| - // FilePathWatcher callback (on the FILE thread). Posts Done() to the UI
|
| - // thread when the condition is satisfied or there is an error.
|
| - void FilePathWatcherCallback(const base::FilePath& path, bool error);
|
| -
|
| - const base::FilePath path_;
|
| - ConditionCallback condition_;
|
| - scoped_ptr<base::FilePathWatcher> watcher_;
|
| - base::RunLoop run_loop_;
|
| - base::Closure quit_closure_;
|
| - bool failed_;
|
| -};
|
| -
|
| -TestFilePathWatcher::TestFilePathWatcher(const base::FilePath& path,
|
| - const ConditionCallback& condition)
|
| - : path_(path),
|
| - condition_(condition),
|
| - quit_closure_(run_loop_.QuitClosure()),
|
| - failed_(false) {
|
| -}
|
| -
|
| -void TestFilePathWatcher::StartWatching() {
|
| - DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
|
| -
|
| - watcher_.reset(new base::FilePathWatcher);
|
| - bool ok = watcher_->Watch(
|
| - path_, false /*recursive*/,
|
| - base::Bind(&TestFilePathWatcher::FilePathWatcherCallback,
|
| - base::Unretained(this)));
|
| - DCHECK(ok);
|
| -
|
| - // If the condition was already met before FilePathWatcher was launched,
|
| - // FilePathWatcher won't be able to detect a change, so check the condition
|
| - // here.
|
| - if (condition_.Run(path_)) {
|
| - watcher_.reset();
|
| - content::BrowserThread::PostTask(content::BrowserThread::UI,
|
| - FROM_HERE,
|
| - quit_closure_);
|
| - return;
|
| - }
|
| -}
|
| -
|
| -void TestFilePathWatcher::FilePathWatcherCallback(const base::FilePath& path,
|
| - bool failed) {
|
| - DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
|
| - DCHECK_EQ(path_.value(), path.value());
|
| -
|
| - if (failed || condition_.Run(path)) {
|
| - failed_ = failed;
|
| - watcher_.reset();
|
| - content::BrowserThread::PostTask(content::BrowserThread::UI,
|
| - FROM_HERE,
|
| - quit_closure_);
|
| - }
|
| -}
|
| -
|
| -bool TestFilePathWatcher::RunMessageLoopUntilConditionSatisfied() {
|
| - DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
| -
|
| - content::BrowserThread::PostTask(
|
| - content::BrowserThread::FILE,
|
| - FROM_HERE,
|
| - base::Bind(&TestFilePathWatcher::StartWatching,
|
| - base::Unretained(this)));
|
| -
|
| - // Wait until the condition is met.
|
| - run_loop_.Run();
|
| - return !failed_;
|
| -}
|
| -
|
| -// Returns true if a file with the given size is present at |path|.
|
| -bool FilePresentWithSize(const int64 file_size,
|
| - const base::FilePath& path) {
|
| - int64 copy_size = 0;
|
| - // If the file doesn't exist yet this will fail and we'll keep waiting.
|
| - if (!file_util::GetFileSize(path, ©_size))
|
| - return false;
|
| - return (copy_size == file_size);
|
| -}
|
| -
|
| -// Returns true if a file is not present at |path|.
|
| -bool FileNotPresent(const base::FilePath& path) {
|
| - return !file_util::PathExists(path);
|
| -};
|
| -
|
| IN_PROC_BROWSER_TEST_P(FileManagerBrowserLocalTest, TestFileDisplay) {
|
| AddMountPointToFakeDownloads();
|
| StartFileManager("/Downloads");
|
| @@ -488,23 +559,20 @@ IN_PROC_BROWSER_TEST_P(FileManagerBrowserLocalTest, TestKeyboardCopy) {
|
| StartFileManager("/Downloads");
|
|
|
| base::FilePath copy_path =
|
| - downloads_path_.AppendASCII(kKeyboardTestFileCopyName);
|
| - ASSERT_FALSE(file_util::PathExists(copy_path));
|
| + GetRootPath().AppendASCII(kKeyboardTestFileCopyName);
|
| + ASSERT_FALSE(PathExists(copy_path));
|
|
|
| ResultCatcher catcher;
|
| StartTest("keyboard copy");
|
|
|
| ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
|
|
|
| - TestFilePathWatcher watcher(
|
| - copy_path,
|
| - base::Bind(FilePresentWithSize, kKeyboardTestFileSize));
|
| - ASSERT_TRUE(watcher.RunMessageLoopUntilConditionSatisfied());
|
| + ASSERT_TRUE(WaitUntilFilePresentWithSize(copy_path, kKeyboardTestFileSize));
|
|
|
| // Check that it was a copy, not a move.
|
| base::FilePath source_path =
|
| - downloads_path_.AppendASCII(kKeyboardTestFileName);
|
| - ASSERT_TRUE(file_util::PathExists(source_path));
|
| + GetRootPath().AppendASCII(kKeyboardTestFileName);
|
| + ASSERT_TRUE(PathExists(source_path));
|
| }
|
|
|
| IN_PROC_BROWSER_TEST_P(FileManagerBrowserLocalTest, TestKeyboardDelete) {
|
| @@ -512,16 +580,14 @@ IN_PROC_BROWSER_TEST_P(FileManagerBrowserLocalTest, TestKeyboardDelete) {
|
| StartFileManager("/Downloads");
|
|
|
| base::FilePath delete_path =
|
| - downloads_path_.AppendASCII(kKeyboardTestFileName);
|
| - ASSERT_TRUE(file_util::PathExists(delete_path));
|
| + GetRootPath().AppendASCII(kKeyboardTestFileName);
|
| + ASSERT_TRUE(PathExists(delete_path));
|
|
|
| ResultCatcher catcher;
|
| StartTest("keyboard delete");
|
| ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
|
|
|
| - TestFilePathWatcher watcher(delete_path,
|
| - base::Bind(FileNotPresent));
|
| - ASSERT_TRUE(watcher.RunMessageLoopUntilConditionSatisfied());
|
| + ASSERT_TRUE(WaitUntilFileNotPresent(delete_path));
|
| }
|
|
|
| IN_PROC_BROWSER_TEST_F(FileManagerBrowserDriveTest, TestFileDisplay) {
|
|
|