Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: chrome/browser/chromeos/drive/file_system/get_file_for_saving_operation_unittest.cc

Issue 22335004: Add drive::FileSystem::GetFileByPathForSaving(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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_operatio n.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 "chrome/browser/chromeos/drive/drive.pb.h"
12 #include "chrome/browser/chromeos/drive/file_errors.h"
13 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
14 #include "chrome/browser/chromeos/drive/file_write_watcher.h"
15 #include "chrome/browser/google_apis/test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace drive {
19 namespace file_system {
20
21 namespace {
22
23 // If OnCacheFileUploadNeededByOperation is called, records the resource ID and
24 // calls |quit_closure|.
25 class TestObserver : public OperationObserver {
26 public:
27 void set_quit_closure(const base::Closure& quit_closure) {
28 quit_closure_ = quit_closure;
29 }
30
31 const std::string& observerd_resource_id() const {
32 return observed_resource_id_;
33 }
34
35 // OperationObserver overrides.
36 virtual void OnDirectoryChangedByOperation(
37 const base::FilePath& path) OVERRIDE {}
38
39 virtual void OnCacheFileUploadNeededByOperation(
40 const std::string& resource_id) OVERRIDE {
41 observed_resource_id_ = resource_id;
42 quit_closure_.Run();
43 }
44
45 private:
46 std::string observed_resource_id_;
47 base::Closure quit_closure_;
48 };
49
50 } // namespace
51
52 class GetFileForSavingOperationTest : public OperationTestBase {
53 protected:
54 // FileWriteWatcher requires TYPE_IO message loop to run.
55 GetFileForSavingOperationTest()
56 : OperationTestBase(content::TestBrowserThreadBundle::IO_MAINLOOP) {
57 }
58
59 virtual void SetUp() OVERRIDE {
60 OperationTestBase::SetUp();
61
62 operation.reset(new GetFileForSavingOperation(
63 blocking_task_runner(), &observer, scheduler(), metadata(), cache(),
64 temp_dir()));
65 operation->file_write_watcher_for_testing()->DisableDelayForTesting();
66 }
67
68 TestObserver observer;
hashimoto 2013/08/07 10:31:54 nit: '_' missing at the end of member variables' n
kinaba 2013/08/07 11:28:38 Done.
69 scoped_ptr<GetFileForSavingOperation> operation;
70 };
71
72 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Exist) {
73 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
74 ResourceEntry src_entry;
75 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
76
77 // Run the operation.
78 FileError error = FILE_ERROR_FAILED;
79 scoped_ptr<ResourceEntry> entry;
80 base::FilePath local_path;
81 operation->GetFileForSaving(
82 drive_path,
83 google_apis::test_util::CreateCopyResultCallback(
84 &error, &local_path, &entry));
85 test_util::RunBlockingPoolTask();
86
87 // Checks that the file is retrieved.
88 EXPECT_EQ(FILE_ERROR_OK, error);
hashimoto 2013/08/07 10:31:54 nit: ASSERT_TRUE(entry)?
kinaba 2013/08/07 11:28:38 Done.
89 EXPECT_EQ(src_entry.resource_id(), entry->resource_id());
90
91 // Checks that it presents in cache and marked dirty.
92 bool success = false;
93 FileCacheEntry cache_entry;
94 cache()->GetCacheEntryOnUIThread(
95 src_entry.resource_id(),
96 google_apis::test_util::CreateCopyResultCallback(&success, &cache_entry));
97 test_util::RunBlockingPoolTask();
98 EXPECT_TRUE(success);
99 EXPECT_TRUE(cache_entry.is_present());
100 EXPECT_TRUE(cache_entry.is_dirty());
101
102 // Write something to the cache and checks that the event is reported.
103 {
104 base::RunLoop run_loop;
105 observer.set_quit_closure(run_loop.QuitClosure());
106 google_apis::test_util::WriteStringToFile(local_path, "hello");
107 run_loop.Run();
hashimoto 2013/08/07 10:31:54 Don't we need to check the state of the observer?
kinaba 2013/08/07 11:28:38 We have to. Done.
108 }
109 }
110
111 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_NotExist) {
112 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/NotExist.txt"));
113 ResourceEntry src_entry;
114 ASSERT_EQ(FILE_ERROR_NOT_FOUND,
115 GetLocalResourceEntry(drive_path, &src_entry));
116
117 // Run the operation.
118 FileError error = FILE_ERROR_FAILED;
119 scoped_ptr<ResourceEntry> entry;
120 base::FilePath local_path;
121 operation->GetFileForSaving(
122 drive_path,
123 google_apis::test_util::CreateCopyResultCallback(
124 &error, &local_path, &entry));
125 test_util::RunBlockingPoolTask();
126
127 // Checks that the file is created and retrieved.
128 EXPECT_EQ(FILE_ERROR_OK, error);
129 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
130 int64 size = -1;
131 EXPECT_TRUE(file_util::GetFileSize(local_path, &size));
132 EXPECT_EQ(0, size);
133 }
134
135 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Directory) {
136 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/Directory 1"));
137 ResourceEntry src_entry;
138 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
139 ASSERT_TRUE(src_entry.file_info().is_directory());
140
141 // Run the operation.
142 FileError error = FILE_ERROR_FAILED;
143 scoped_ptr<ResourceEntry> entry;
144 base::FilePath local_path;
145 operation->GetFileForSaving(
146 drive_path,
147 google_apis::test_util::CreateCopyResultCallback(
148 &error, &local_path, &entry));
149 test_util::RunBlockingPoolTask();
150
151 // Checks that an error is returned.
152 EXPECT_EQ(FILE_ERROR_EXISTS, error);
153 }
154
155 } // namespace file_system
156 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698