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