get_file_for_saving_operation_unittest.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1// Copyright 2013 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_system/get_file_for_saving_operation.h"
6
7#include "base/callback.h"
8#include "base/file_util.h"
9#include "base/files/file_path.h"
10#include "base/run_loop.h"
11#include "base/task_runner_util.h"
12#include "chrome/browser/chromeos/drive/drive.pb.h"
13#include "chrome/browser/chromeos/drive/file_errors.h"
14#include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
15#include "chrome/browser/chromeos/drive/file_write_watcher.h"
16#include "google_apis/drive/test_util.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19namespace drive {
20namespace file_system {
21
22namespace {
23
24// If OnCacheFileUploadNeededByOperation is called, records the local ID and
25// calls |quit_closure|.
26class TestObserver : public OperationObserver {
27 public:
28  void set_quit_closure(const base::Closure& quit_closure) {
29    quit_closure_ = quit_closure;
30  }
31
32  const std::string& observerd_local_id() const {
33    return observed_local_id_;
34  }
35
36  // OperationObserver overrides.
37  virtual void OnDirectoryChangedByOperation(
38      const base::FilePath& path) OVERRIDE {}
39
40  virtual void OnEntryUpdatedByOperation(const std::string& local_id) OVERRIDE {
41    observed_local_id_ = local_id;
42    if (!quit_closure_.is_null())
43      quit_closure_.Run();
44  }
45
46 private:
47  std::string observed_local_id_;
48  base::Closure quit_closure_;
49};
50
51}  // namespace
52
53class GetFileForSavingOperationTest : public OperationTestBase {
54 protected:
55  // FileWriteWatcher requires TYPE_IO message loop to run.
56  GetFileForSavingOperationTest()
57      : OperationTestBase(content::TestBrowserThreadBundle::IO_MAINLOOP) {
58  }
59
60  virtual void SetUp() OVERRIDE {
61    OperationTestBase::SetUp();
62
63    operation_.reset(new GetFileForSavingOperation(
64        logger(), blocking_task_runner(), &observer_, scheduler(), metadata(),
65        cache(), temp_dir()));
66    operation_->file_write_watcher_for_testing()->DisableDelayForTesting();
67  }
68
69  TestObserver observer_;
70  scoped_ptr<GetFileForSavingOperation> operation_;
71};
72
73TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Exist) {
74  base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
75  ResourceEntry src_entry;
76  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
77
78  // Run the operation.
79  FileError error = FILE_ERROR_FAILED;
80  scoped_ptr<ResourceEntry> entry;
81  base::FilePath local_path;
82  operation_->GetFileForSaving(
83      drive_path,
84      google_apis::test_util::CreateCopyResultCallback(
85          &error, &local_path, &entry));
86  test_util::RunBlockingPoolTask();
87
88  // Checks that the file is retrieved.
89  EXPECT_EQ(FILE_ERROR_OK, error);
90  ASSERT_TRUE(entry);
91  EXPECT_EQ(src_entry.resource_id(), entry->resource_id());
92
93  // Checks that it presents in cache and marked dirty.
94  EXPECT_TRUE(entry->file_specific_info().cache_state().is_present());
95  EXPECT_TRUE(entry->file_specific_info().cache_state().is_dirty());
96
97  // Write something to the cache and checks that the event is reported.
98  {
99    base::RunLoop run_loop;
100    observer_.set_quit_closure(run_loop.QuitClosure());
101    google_apis::test_util::WriteStringToFile(local_path, "hello");
102    run_loop.Run();
103    EXPECT_EQ(GetLocalId(drive_path), observer_.observerd_local_id());
104  }
105}
106
107TEST_F(GetFileForSavingOperationTest, GetFileForSaving_NotExist) {
108  base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/NotExist.txt"));
109  ResourceEntry src_entry;
110  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
111            GetLocalResourceEntry(drive_path, &src_entry));
112
113  // Run the operation.
114  FileError error = FILE_ERROR_FAILED;
115  scoped_ptr<ResourceEntry> entry;
116  base::FilePath local_path;
117  operation_->GetFileForSaving(
118      drive_path,
119      google_apis::test_util::CreateCopyResultCallback(
120          &error, &local_path, &entry));
121  test_util::RunBlockingPoolTask();
122
123  // Checks that the file is created and retrieved.
124  EXPECT_EQ(FILE_ERROR_OK, error);
125  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
126  int64 size = -1;
127  EXPECT_TRUE(base::GetFileSize(local_path, &size));
128  EXPECT_EQ(0, size);
129}
130
131TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Directory) {
132  base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/Directory 1"));
133  ResourceEntry src_entry;
134  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
135  ASSERT_TRUE(src_entry.file_info().is_directory());
136
137  // Run the operation.
138  FileError error = FILE_ERROR_FAILED;
139  scoped_ptr<ResourceEntry> entry;
140  base::FilePath local_path;
141  operation_->GetFileForSaving(
142      drive_path,
143      google_apis::test_util::CreateCopyResultCallback(
144          &error, &local_path, &entry));
145  test_util::RunBlockingPoolTask();
146
147  // Checks that an error is returned.
148  EXPECT_EQ(FILE_ERROR_EXISTS, error);
149}
150
151}  // namespace file_system
152}  // namespace drive
153