summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/write_on_cache_file_unittest.cc
blob: 1bca4fc319f327b738845ca0990dfa10ca6c6e23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/drive/write_on_cache_file.h"

#include "base/bind.h"
#include "components/drive/dummy_file_system.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_utils.h"
#include "google_apis/drive/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace drive {

namespace {

const base::FilePath::CharType kDrivePath[] =
    FILE_PATH_LITERAL("drive/root/file.txt");
const base::FilePath::CharType kInvalidPath[] =
    FILE_PATH_LITERAL("drive/invalid/path");
const base::FilePath::CharType kLocalPath[] =
    FILE_PATH_LITERAL("/tmp/local.txt");

class TestFileSystem : public DummyFileSystem {
 public:
  TestFileSystem() : num_closed_(0) {
  }

  int num_closed() const { return num_closed_; }

  // Mimics OpenFile. It fails if the |file_path| points to a hosted document.
  void OpenFile(const base::FilePath& file_path,
                OpenMode open_mode,
                const std::string& mime_type,
                const OpenFileCallback& callback) override {
    EXPECT_EQ(OPEN_OR_CREATE_FILE, open_mode);

    // Emulate a case of opening a hosted document.
    if (file_path == base::FilePath(kInvalidPath)) {
      callback.Run(FILE_ERROR_INVALID_OPERATION, base::FilePath(),
                   base::Closure());
      return;
    }

    callback.Run(FILE_ERROR_OK, base::FilePath(kLocalPath),
                 base::Bind(&TestFileSystem::CloseFile,
                            base::Unretained(this)));
  }

 private:

  void CloseFile() {
    ++num_closed_;
  }

  int num_closed_;
};

}  // namespace

TEST(WriteOnCacheFileTest, PrepareFileForWritingSuccess) {
  content::TestBrowserThreadBundle thread_bundle;
  TestFileSystem test_file_system;

  FileError error = FILE_ERROR_FAILED;
  base::FilePath path;
  // The file should successfully be opened.
  WriteOnCacheFile(
      &test_file_system,
      base::FilePath(kDrivePath),
      std::string(),  // mime_type
      google_apis::test_util::CreateCopyResultCallback(&error, &path));
  content::RunAllBlockingPoolTasksUntilIdle();

  EXPECT_EQ(FILE_ERROR_OK, error);
  EXPECT_EQ(kLocalPath, path.value());

  // Make sure that the file is actually closed.
  EXPECT_EQ(1, test_file_system.num_closed());
}

TEST(WriteOnCacheFileTest, PrepareFileForWritingCreateFail) {
  content::TestBrowserThreadBundle thread_bundle;
  TestFileSystem test_file_system;

  FileError error = FILE_ERROR_FAILED;
  base::FilePath path;
  // Access to kInvalidPath should fail, and FileWriteHelper should not try to
  // open or close the file.
  WriteOnCacheFile(
      &test_file_system,
      base::FilePath(kInvalidPath),
      std::string(),  // mime_type
      google_apis::test_util::CreateCopyResultCallback(&error, &path));
  content::RunAllBlockingPoolTasksUntilIdle();

  EXPECT_EQ(FILE_ERROR_INVALID_OPERATION, error);
  EXPECT_TRUE(path.empty());
}

}   // namespace drive