Ensure low-memory renderers retry failed loads correctly.
[chromium-blink-merge.git] / components / drive / file_system / get_file_for_saving_operation_unittest.cc
blob984d0b692d979b445bda1a386a7781e82271e460
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 "components/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 "components/drive/drive.pb.h"
13 #include "components/drive/file_change.h"
14 #include "components/drive/file_errors.h"
15 #include "components/drive/file_system/operation_test_base.h"
16 #include "components/drive/file_write_watcher.h"
17 #include "components/drive/job_scheduler.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/test/test_utils.h"
20 #include "google_apis/drive/test_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace drive {
24 namespace file_system {
26 namespace {
28 // If OnCacheFileUploadNeededByOperation is called, records the local ID and
29 // calls |quit_closure|.
30 class TestDelegate : public OperationDelegate {
31 public:
32 void set_quit_closure(const base::Closure& quit_closure) {
33 quit_closure_ = quit_closure;
36 const std::string& updated_local_id() const {
37 return updated_local_id_;
40 // OperationDelegate overrides.
41 void OnEntryUpdatedByOperation(const ClientContext& /* context */,
42 const std::string& local_id) override {
43 updated_local_id_ = local_id;
44 if (!quit_closure_.is_null())
45 quit_closure_.Run();
48 private:
49 std::string updated_local_id_;
50 base::Closure quit_closure_;
53 } // namespace
55 class GetFileForSavingOperationTest : public OperationTestBase {
56 protected:
57 // FileWriteWatcher requires TYPE_IO message loop to run.
58 GetFileForSavingOperationTest()
59 : OperationTestBase(content::TestBrowserThreadBundle::IO_MAINLOOP) {
62 void SetUp() override {
63 OperationTestBase::SetUp();
65 file_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread(
66 content::BrowserThread::FILE);
68 operation_.reset(new GetFileForSavingOperation(
69 logger(), blocking_task_runner(), file_task_runner_.get(), &delegate_,
70 scheduler(), metadata(), cache(), temp_dir()));
71 operation_->file_write_watcher_for_testing()->DisableDelayForTesting();
74 TestDelegate delegate_;
75 scoped_ptr<GetFileForSavingOperation> operation_;
76 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
79 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Exist) {
80 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
81 ResourceEntry src_entry;
82 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
84 // Run the operation.
85 FileError error = FILE_ERROR_FAILED;
86 scoped_ptr<ResourceEntry> entry;
87 base::FilePath local_path;
88 operation_->GetFileForSaving(
89 drive_path,
90 google_apis::test_util::CreateCopyResultCallback(
91 &error, &local_path, &entry));
92 content::RunAllBlockingPoolTasksUntilIdle();
94 // Checks that the file is retrieved.
95 EXPECT_EQ(FILE_ERROR_OK, error);
96 ASSERT_TRUE(entry);
97 EXPECT_EQ(src_entry.resource_id(), entry->resource_id());
99 // Checks that it presents in cache and marked dirty.
100 EXPECT_TRUE(entry->file_specific_info().cache_state().is_present());
101 EXPECT_TRUE(entry->file_specific_info().cache_state().is_dirty());
103 // Write something to the cache and checks that the event is reported.
105 base::RunLoop run_loop;
106 delegate_.set_quit_closure(run_loop.QuitClosure());
107 google_apis::test_util::WriteStringToFile(local_path, "hello");
108 run_loop.Run();
109 EXPECT_EQ(GetLocalId(drive_path), delegate_.updated_local_id());
113 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_NotExist) {
114 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/NotExist.txt"));
115 ResourceEntry src_entry;
116 ASSERT_EQ(FILE_ERROR_NOT_FOUND,
117 GetLocalResourceEntry(drive_path, &src_entry));
119 // Run the operation.
120 FileError error = FILE_ERROR_FAILED;
121 scoped_ptr<ResourceEntry> entry;
122 base::FilePath local_path;
123 operation_->GetFileForSaving(
124 drive_path,
125 google_apis::test_util::CreateCopyResultCallback(
126 &error, &local_path, &entry));
127 content::RunAllBlockingPoolTasksUntilIdle();
129 // Checks that the file is created and retrieved.
130 EXPECT_EQ(FILE_ERROR_OK, error);
131 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
132 int64 size = -1;
133 EXPECT_TRUE(base::GetFileSize(local_path, &size));
134 EXPECT_EQ(0, size);
137 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Directory) {
138 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/Directory 1"));
139 ResourceEntry src_entry;
140 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
141 ASSERT_TRUE(src_entry.file_info().is_directory());
143 // Run the operation.
144 FileError error = FILE_ERROR_FAILED;
145 scoped_ptr<ResourceEntry> entry;
146 base::FilePath local_path;
147 operation_->GetFileForSaving(
148 drive_path,
149 google_apis::test_util::CreateCopyResultCallback(
150 &error, &local_path, &entry));
151 content::RunAllBlockingPoolTasksUntilIdle();
153 // Checks that an error is returned.
154 EXPECT_EQ(FILE_ERROR_EXISTS, error);
157 } // namespace file_system
158 } // namespace drive