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.
5 #include "chrome/browser/chromeos/drive/file_system/get_file_for_saving_operation.h"
7 #include "base/callback.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.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_change.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 "chrome/browser/chromeos/drive/job_scheduler.h"
18 #include "content/public/test/test_utils.h"
19 #include "google_apis/drive/test_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
23 namespace file_system
{
27 // If OnCacheFileUploadNeededByOperation is called, records the local ID and
28 // calls |quit_closure|.
29 class TestDelegate
: public OperationDelegate
{
31 void set_quit_closure(const base::Closure
& quit_closure
) {
32 quit_closure_
= quit_closure
;
35 const std::string
& updated_local_id() const {
36 return updated_local_id_
;
39 // OperationDelegate overrides.
40 void OnEntryUpdatedByOperation(const ClientContext
& /* context */,
41 const std::string
& local_id
) override
{
42 updated_local_id_
= local_id
;
43 if (!quit_closure_
.is_null())
48 std::string updated_local_id_
;
49 base::Closure quit_closure_
;
54 class GetFileForSavingOperationTest
: public OperationTestBase
{
56 // FileWriteWatcher requires TYPE_IO message loop to run.
57 GetFileForSavingOperationTest()
58 : OperationTestBase(content::TestBrowserThreadBundle::IO_MAINLOOP
) {
61 void SetUp() override
{
62 OperationTestBase::SetUp();
64 operation_
.reset(new GetFileForSavingOperation(
65 logger(), blocking_task_runner(), &delegate_
, scheduler(), metadata(),
66 cache(), temp_dir()));
67 operation_
->file_write_watcher_for_testing()->DisableDelayForTesting();
70 TestDelegate delegate_
;
71 scoped_ptr
<GetFileForSavingOperation
> operation_
;
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
));
80 FileError error
= FILE_ERROR_FAILED
;
81 scoped_ptr
<ResourceEntry
> entry
;
82 base::FilePath local_path
;
83 operation_
->GetFileForSaving(
85 google_apis::test_util::CreateCopyResultCallback(
86 &error
, &local_path
, &entry
));
87 content::RunAllBlockingPoolTasksUntilIdle();
89 // Checks that the file is retrieved.
90 EXPECT_EQ(FILE_ERROR_OK
, error
);
92 EXPECT_EQ(src_entry
.resource_id(), entry
->resource_id());
94 // Checks that it presents in cache and marked dirty.
95 EXPECT_TRUE(entry
->file_specific_info().cache_state().is_present());
96 EXPECT_TRUE(entry
->file_specific_info().cache_state().is_dirty());
98 // Write something to the cache and checks that the event is reported.
100 base::RunLoop run_loop
;
101 delegate_
.set_quit_closure(run_loop
.QuitClosure());
102 google_apis::test_util::WriteStringToFile(local_path
, "hello");
104 EXPECT_EQ(GetLocalId(drive_path
), delegate_
.updated_local_id());
108 TEST_F(GetFileForSavingOperationTest
, GetFileForSaving_NotExist
) {
109 base::FilePath
drive_path(FILE_PATH_LITERAL("drive/root/NotExist.txt"));
110 ResourceEntry src_entry
;
111 ASSERT_EQ(FILE_ERROR_NOT_FOUND
,
112 GetLocalResourceEntry(drive_path
, &src_entry
));
114 // Run the operation.
115 FileError error
= FILE_ERROR_FAILED
;
116 scoped_ptr
<ResourceEntry
> entry
;
117 base::FilePath local_path
;
118 operation_
->GetFileForSaving(
120 google_apis::test_util::CreateCopyResultCallback(
121 &error
, &local_path
, &entry
));
122 content::RunAllBlockingPoolTasksUntilIdle();
124 // Checks that the file is created and retrieved.
125 EXPECT_EQ(FILE_ERROR_OK
, error
);
126 EXPECT_EQ(FILE_ERROR_OK
, GetLocalResourceEntry(drive_path
, &src_entry
));
128 EXPECT_TRUE(base::GetFileSize(local_path
, &size
));
132 TEST_F(GetFileForSavingOperationTest
, GetFileForSaving_Directory
) {
133 base::FilePath
drive_path(FILE_PATH_LITERAL("drive/root/Directory 1"));
134 ResourceEntry src_entry
;
135 ASSERT_EQ(FILE_ERROR_OK
, GetLocalResourceEntry(drive_path
, &src_entry
));
136 ASSERT_TRUE(src_entry
.file_info().is_directory());
138 // Run the operation.
139 FileError error
= FILE_ERROR_FAILED
;
140 scoped_ptr
<ResourceEntry
> entry
;
141 base::FilePath local_path
;
142 operation_
->GetFileForSaving(
144 google_apis::test_util::CreateCopyResultCallback(
145 &error
, &local_path
, &entry
));
146 content::RunAllBlockingPoolTasksUntilIdle();
148 // Checks that an error is returned.
149 EXPECT_EQ(FILE_ERROR_EXISTS
, error
);
152 } // namespace file_system