summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/file_write_helper_unittest.cc
blob: 594c8c079447ec9003284c50e0ab139900cfd2e3 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2012 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/file_write_helper.h"

#include "base/bind.h"
#include "base/message_loop.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/chromeos/drive/drive_test_util.h"
#include "chrome/browser/chromeos/drive/mock_drive_file_system.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::StrictMock;
using ::testing::_;

namespace drive {

namespace {

ACTION_P(MockCreateFile, error) {
  DCHECK(!arg2.is_null());
  arg2.Run(error);
}

ACTION_P2(MockOpenFile, error, local_path) {
  DCHECK(!arg1.is_null());
  arg1.Run(error, local_path);
}

ACTION_P(MockCloseFile, error) {
  DCHECK(!arg1.is_null());
  arg1.Run(error);
}

void RecordOpenFileCallbackArguments(DriveFileError* error,
                                     base::FilePath* path,
                                     DriveFileError error_arg,
                                     const base::FilePath& path_arg) {
  base::ThreadRestrictions::AssertIOAllowed();
  *error = error_arg;
  *path = path_arg;
}

}  // namespace

class FileWriteHelperTest : public testing::Test {
 public:
  FileWriteHelperTest()
      : ui_thread_(content::BrowserThread::UI, &message_loop_),
        mock_file_system_(new StrictMock<MockDriveFileSystem>) {
  }

 protected:
  MessageLoopForUI message_loop_;
  content::TestBrowserThread ui_thread_;
  scoped_ptr< StrictMock<MockDriveFileSystem> > mock_file_system_;
};

TEST_F(FileWriteHelperTest, PrepareFileForWritingSuccess) {
  const base::FilePath kDrivePath("/drive/file.txt");
  const base::FilePath kLocalPath("/tmp/dummy.txt");

  EXPECT_CALL(*mock_file_system_, CreateFile(kDrivePath, false, _))
      .WillOnce(MockCreateFile(DRIVE_FILE_OK));
  EXPECT_CALL(*mock_file_system_, OpenFile(kDrivePath, _))
      .WillOnce(MockOpenFile(DRIVE_FILE_OK, kLocalPath));
  EXPECT_CALL(*mock_file_system_, CloseFile(kDrivePath, _))
      .WillOnce(MockCloseFile(DRIVE_FILE_OK));

  FileWriteHelper file_write_helper(mock_file_system_.get());
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  base::FilePath path;
  file_write_helper.PrepareWritableFileAndRun(
      kDrivePath, base::Bind(&RecordOpenFileCallbackArguments, &error, &path));
  google_apis::test_util::RunBlockingPoolTask();

  EXPECT_EQ(DRIVE_FILE_OK, error);
  EXPECT_EQ(kLocalPath, path);
}

TEST_F(FileWriteHelperTest, PrepareFileForWritingCreateFail) {
  const base::FilePath kDrivePath("/drive/file.txt");

  EXPECT_CALL(*mock_file_system_, CreateFile(kDrivePath, false, _))
      .WillOnce(MockCreateFile(DRIVE_FILE_ERROR_ACCESS_DENIED));
  EXPECT_CALL(*mock_file_system_, OpenFile(_, _)).Times(0);
  EXPECT_CALL(*mock_file_system_, CloseFile(_, _)).Times(0);

  FileWriteHelper file_write_helper(mock_file_system_.get());
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  base::FilePath path;
  file_write_helper.PrepareWritableFileAndRun(
      kDrivePath, base::Bind(&RecordOpenFileCallbackArguments, &error, &path));
  google_apis::test_util::RunBlockingPoolTask();

  EXPECT_EQ(DRIVE_FILE_ERROR_ACCESS_DENIED, error);
  EXPECT_EQ(base::FilePath(), path);
}

TEST_F(FileWriteHelperTest, PrepareFileForWritingOpenFail) {
  const base::FilePath kDrivePath("/drive/file.txt");

  EXPECT_CALL(*mock_file_system_, CreateFile(kDrivePath, false, _))
      .WillOnce(MockCreateFile(DRIVE_FILE_OK));
  EXPECT_CALL(*mock_file_system_, OpenFile(kDrivePath, _))
      .WillOnce(MockOpenFile(DRIVE_FILE_ERROR_IN_USE, base::FilePath()));
  EXPECT_CALL(*mock_file_system_, CloseFile(_, _)).Times(0);

  FileWriteHelper file_write_helper(mock_file_system_.get());
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  base::FilePath path;
  file_write_helper.PrepareWritableFileAndRun(
      kDrivePath, base::Bind(&RecordOpenFileCallbackArguments, &error, &path));
  google_apis::test_util::RunBlockingPoolTask();

  EXPECT_EQ(DRIVE_FILE_ERROR_IN_USE, error);
  EXPECT_EQ(base::FilePath(), path);
}

}   // namespace drive