Home | History | Annotate | Download | only in drive
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/chromeos/drive/file_write_helper.h"
      6 
      7 #include "base/bind.h"
      8 #include "chrome/browser/chromeos/drive/dummy_file_system.h"
      9 #include "chrome/browser/chromeos/drive/test_util.h"
     10 #include "content/public/test/test_browser_thread_bundle.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 namespace drive {
     14 
     15 namespace {
     16 
     17 const base::FilePath::CharType kDrivePath[] =
     18     FILE_PATH_LITERAL("drive/root/file.txt");
     19 const base::FilePath::CharType kInvalidPath[] =
     20     FILE_PATH_LITERAL("drive/invalid/path");
     21 const base::FilePath::CharType kLocalPath[] =
     22     FILE_PATH_LITERAL("/tmp/local.txt");
     23 
     24 class TestFileSystem : public DummyFileSystem {
     25  public:
     26   TestFileSystem() : num_closed_(0) {
     27   }
     28 
     29   int num_closed() const { return num_closed_; }
     30 
     31   // Mimics OpenFile. It fails if the |file_path| points to a hosted document.
     32   virtual void OpenFile(const base::FilePath& file_path,
     33                         OpenMode open_mode,
     34                         const OpenFileCallback& callback) OVERRIDE {
     35     EXPECT_EQ(OPEN_OR_CREATE_FILE, open_mode);
     36 
     37     // Emulate a case of opening a hosted document.
     38     if (file_path == base::FilePath(kInvalidPath)) {
     39       callback.Run(FILE_ERROR_INVALID_OPERATION, base::FilePath(),
     40                    base::Closure());
     41       return;
     42     }
     43 
     44     callback.Run(FILE_ERROR_OK, base::FilePath(kLocalPath),
     45                  base::Bind(&TestFileSystem::CloseFile,
     46                             base::Unretained(this)));
     47   }
     48 
     49  private:
     50 
     51   void CloseFile() {
     52     ++num_closed_;
     53   }
     54 
     55   int num_closed_;
     56 };
     57 
     58 }  // namespace
     59 
     60 class FileWriteHelperTest : public testing::Test {
     61  public:
     62   FileWriteHelperTest()
     63       : test_file_system_(new TestFileSystem) {
     64   }
     65 
     66  protected:
     67   content::TestBrowserThreadBundle thread_bundle_;
     68   scoped_ptr<TestFileSystem> test_file_system_;
     69 };
     70 
     71 TEST_F(FileWriteHelperTest, PrepareFileForWritingSuccess) {
     72   FileWriteHelper file_write_helper(test_file_system_.get());
     73   FileError error = FILE_ERROR_FAILED;
     74   base::FilePath path;
     75   // The file should successfully be opened.
     76   file_write_helper.PrepareWritableFileAndRun(
     77       base::FilePath(kDrivePath),
     78       google_apis::test_util::CreateCopyResultCallback(&error, &path));
     79   test_util::RunBlockingPoolTask();
     80 
     81   EXPECT_EQ(FILE_ERROR_OK, error);
     82   EXPECT_EQ(kLocalPath, path.value());
     83 
     84   // Make sure that the file is actually closed.
     85   EXPECT_EQ(1, test_file_system_->num_closed());
     86 }
     87 
     88 TEST_F(FileWriteHelperTest, PrepareFileForWritingCreateFail) {
     89   FileWriteHelper file_write_helper(test_file_system_.get());
     90   FileError error = FILE_ERROR_FAILED;
     91   base::FilePath path;
     92   // Access to kInvalidPath should fail, and FileWriteHelper should not try to
     93   // open or close the file.
     94   file_write_helper.PrepareWritableFileAndRun(
     95       base::FilePath(kInvalidPath),
     96       google_apis::test_util::CreateCopyResultCallback(&error, &path));
     97   test_util::RunBlockingPoolTask();
     98 
     99   EXPECT_EQ(FILE_ERROR_INVALID_OPERATION, error);
    100   EXPECT_TRUE(path.empty());
    101 }
    102 
    103 }   // namespace drive
    104