- add sources.
[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_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 "chrome/browser/google_apis/test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace drive {
20 namespace file_system {
21
22 namespace {
23
24 // If OnCacheFileUploadNeededByOperation is called, records the local ID and
25 // calls |quit_closure|.
26 class 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 OnCacheFileUploadNeededByOperation(
41       const std::string& local_id) OVERRIDE {
42     observed_local_id_ = local_id;
43     quit_closure_.Run();
44   }
45
46  private:
47   std::string observed_local_id_;
48   base::Closure quit_closure_;
49 };
50
51 }  // namespace
52
53 class 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         blocking_task_runner(), &observer_, scheduler(), metadata(), cache(),
65         temp_dir()));
66     operation_->file_write_watcher_for_testing()->DisableDelayForTesting();
67   }
68
69   TestObserver observer_;
70   scoped_ptr<GetFileForSavingOperation> operation_;
71 };
72
73 TEST_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   bool success = false;
95   FileCacheEntry cache_entry;
96   base::PostTaskAndReplyWithResult(
97       blocking_task_runner(),
98       FROM_HERE,
99       base::Bind(&internal::FileCache::GetCacheEntry,
100                  base::Unretained(cache()),
101                  GetLocalId(drive_path),
102                  &cache_entry),
103       google_apis::test_util::CreateCopyResultCallback(&success));
104   test_util::RunBlockingPoolTask();
105   EXPECT_TRUE(success);
106   EXPECT_TRUE(cache_entry.is_present());
107   EXPECT_TRUE(cache_entry.is_dirty());
108
109   // Write something to the cache and checks that the event is reported.
110   {
111     base::RunLoop run_loop;
112     observer_.set_quit_closure(run_loop.QuitClosure());
113     google_apis::test_util::WriteStringToFile(local_path, "hello");
114     run_loop.Run();
115     EXPECT_EQ(GetLocalId(drive_path), observer_.observerd_local_id());
116   }
117 }
118
119 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_NotExist) {
120   base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/NotExist.txt"));
121   ResourceEntry src_entry;
122   ASSERT_EQ(FILE_ERROR_NOT_FOUND,
123             GetLocalResourceEntry(drive_path, &src_entry));
124
125   // Run the operation.
126   FileError error = FILE_ERROR_FAILED;
127   scoped_ptr<ResourceEntry> entry;
128   base::FilePath local_path;
129   operation_->GetFileForSaving(
130       drive_path,
131       google_apis::test_util::CreateCopyResultCallback(
132           &error, &local_path, &entry));
133   test_util::RunBlockingPoolTask();
134
135   // Checks that the file is created and retrieved.
136   EXPECT_EQ(FILE_ERROR_OK, error);
137   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
138   int64 size = -1;
139   EXPECT_TRUE(file_util::GetFileSize(local_path, &size));
140   EXPECT_EQ(0, size);
141 }
142
143 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Directory) {
144   base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/Directory 1"));
145   ResourceEntry src_entry;
146   ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
147   ASSERT_TRUE(src_entry.file_info().is_directory());
148
149   // Run the operation.
150   FileError error = FILE_ERROR_FAILED;
151   scoped_ptr<ResourceEntry> entry;
152   base::FilePath local_path;
153   operation_->GetFileForSaving(
154       drive_path,
155       google_apis::test_util::CreateCopyResultCallback(
156           &error, &local_path, &entry));
157   test_util::RunBlockingPoolTask();
158
159   // Checks that an error is returned.
160   EXPECT_EQ(FILE_ERROR_EXISTS, error);
161 }
162
163 }  // namespace file_system
164 }  // namespace drive