OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/drive/fake_drive_file_system.h" | 5 #include "chrome/browser/chromeos/drive/fake_file_system.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "chrome/browser/chromeos/drive/file_system_util.h" | 9 #include "chrome/browser/chromeos/drive/file_system_util.h" |
10 #include "chrome/browser/google_apis/fake_drive_service.h" | 10 #include "chrome/browser/google_apis/fake_drive_service.h" |
11 #include "chrome/browser/google_apis/test_util.h" | 11 #include "chrome/browser/google_apis/test_util.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "content/public/test/test_browser_thread.h" | 13 #include "content/public/test/test_browser_thread.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace drive { | 16 namespace drive { |
17 namespace test_util { | 17 namespace test_util { |
18 | 18 |
19 class FakeDriveFileSystemTest : public ::testing::Test { | 19 class FakeFileSystemTest : public ::testing::Test { |
20 protected: | 20 protected: |
21 FakeDriveFileSystemTest() | 21 FakeFileSystemTest() |
22 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | 22 : ui_thread_(content::BrowserThread::UI, &message_loop_) { |
23 } | 23 } |
24 | 24 |
25 virtual void SetUp() OVERRIDE { | 25 virtual void SetUp() OVERRIDE { |
26 // Initialize FakeDriveService. | 26 // Initialize FakeDriveService. |
27 fake_drive_service_.reset(new google_apis::FakeDriveService); | 27 fake_drive_service_.reset(new google_apis::FakeDriveService); |
28 fake_drive_service_->LoadResourceListForWapi( | 28 fake_drive_service_->LoadResourceListForWapi( |
29 "chromeos/gdata/root_feed.json"); | 29 "chromeos/gdata/root_feed.json"); |
30 fake_drive_service_->LoadAccountMetadataForWapi( | 30 fake_drive_service_->LoadAccountMetadataForWapi( |
31 "chromeos/gdata/account_metadata.json"); | 31 "chromeos/gdata/account_metadata.json"); |
32 fake_drive_service_->LoadAppListForDriveApi("chromeos/drive/applist.json"); | 32 fake_drive_service_->LoadAppListForDriveApi("chromeos/drive/applist.json"); |
33 | 33 |
34 // Create a testee instance. | 34 // Create a testee instance. |
35 fake_drive_file_system_.reset( | 35 fake_file_system_.reset( |
36 new FakeDriveFileSystem(fake_drive_service_.get())); | 36 new FakeFileSystem(fake_drive_service_.get())); |
37 ASSERT_TRUE(fake_drive_file_system_->InitializeForTesting()); | 37 ASSERT_TRUE(fake_file_system_->InitializeForTesting()); |
38 } | 38 } |
39 | 39 |
40 MessageLoopForUI message_loop_; | 40 MessageLoopForUI message_loop_; |
41 content::TestBrowserThread ui_thread_; | 41 content::TestBrowserThread ui_thread_; |
42 | 42 |
43 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; | 43 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; |
44 scoped_ptr<FakeDriveFileSystem> fake_drive_file_system_; | 44 scoped_ptr<FakeFileSystem> fake_file_system_; |
45 }; | 45 }; |
46 | 46 |
47 TEST_F(FakeDriveFileSystemTest, GetEntryInfoByResourceId) { | 47 TEST_F(FakeFileSystemTest, GetEntryInfoByResourceId) { |
48 FileError error = FILE_ERROR_FAILED; | 48 FileError error = FILE_ERROR_FAILED; |
49 scoped_ptr<DriveEntryProto> entry; | 49 scoped_ptr<DriveEntryProto> entry; |
50 base::FilePath file_path; | 50 base::FilePath file_path; |
51 | 51 |
52 fake_drive_file_system_->GetEntryInfoByResourceId( | 52 fake_file_system_->GetEntryInfoByResourceId( |
53 "folder:sub_dir_folder_resource_id", | 53 "folder:sub_dir_folder_resource_id", |
54 google_apis::test_util::CreateCopyResultCallback( | 54 google_apis::test_util::CreateCopyResultCallback( |
55 &error, &file_path, &entry)); | 55 &error, &file_path, &entry)); |
56 google_apis::test_util::RunBlockingPoolTask(); | 56 google_apis::test_util::RunBlockingPoolTask(); |
57 | 57 |
58 ASSERT_EQ(FILE_ERROR_OK, error); | 58 ASSERT_EQ(FILE_ERROR_OK, error); |
59 EXPECT_EQ( | 59 EXPECT_EQ( |
60 util::GetDriveMyDriveRootPath().AppendASCII( | 60 util::GetDriveMyDriveRootPath().AppendASCII( |
61 "Directory 1/Sub Directory Folder"), | 61 "Directory 1/Sub Directory Folder"), |
62 file_path); | 62 file_path); |
63 EXPECT_TRUE(entry); // Just make sure something is returned. | 63 EXPECT_TRUE(entry); // Just make sure something is returned. |
64 } | 64 } |
65 | 65 |
66 TEST_F(FakeDriveFileSystemTest, | 66 TEST_F(FakeFileSystemTest, |
67 GetEntryInfoByResourceId_PathCompatibleWithGetEntryInfoByPath) { | 67 GetEntryInfoByResourceId_PathCompatibleWithGetEntryInfoByPath) { |
68 const std::string document_resource_id = "document:5_document_resource_id"; | 68 const std::string document_resource_id = "document:5_document_resource_id"; |
69 | 69 |
70 FileError error = FILE_ERROR_FAILED; | 70 FileError error = FILE_ERROR_FAILED; |
71 scoped_ptr<DriveEntryProto> entry; | 71 scoped_ptr<DriveEntryProto> entry; |
72 base::FilePath file_path; | 72 base::FilePath file_path; |
73 | 73 |
74 // Get entry info by resource id. | 74 // Get entry info by resource id. |
75 fake_drive_file_system_->GetEntryInfoByResourceId( | 75 fake_file_system_->GetEntryInfoByResourceId( |
76 document_resource_id, | 76 document_resource_id, |
77 google_apis::test_util::CreateCopyResultCallback( | 77 google_apis::test_util::CreateCopyResultCallback( |
78 &error, &file_path, &entry)); | 78 &error, &file_path, &entry)); |
79 google_apis::test_util::RunBlockingPoolTask(); | 79 google_apis::test_util::RunBlockingPoolTask(); |
80 | 80 |
81 ASSERT_EQ(FILE_ERROR_OK, error); | 81 ASSERT_EQ(FILE_ERROR_OK, error); |
82 ASSERT_TRUE(entry); | 82 ASSERT_TRUE(entry); |
83 EXPECT_TRUE(entry->file_specific_info().is_hosted_document()); | 83 EXPECT_TRUE(entry->file_specific_info().is_hosted_document()); |
84 | 84 |
85 // Get entry info by path given by GetEntryInfoByResourceId. | 85 // Get entry info by path given by GetEntryInfoByResourceId. |
86 error = FILE_ERROR_FAILED; | 86 error = FILE_ERROR_FAILED; |
87 entry.reset(); | 87 entry.reset(); |
88 fake_drive_file_system_->GetEntryInfoByPath( | 88 fake_file_system_->GetEntryInfoByPath( |
89 file_path, | 89 file_path, |
90 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); | 90 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); |
91 google_apis::test_util::RunBlockingPoolTask(); | 91 google_apis::test_util::RunBlockingPoolTask(); |
92 | 92 |
93 ASSERT_EQ(FILE_ERROR_OK, error); | 93 ASSERT_EQ(FILE_ERROR_OK, error); |
94 ASSERT_TRUE(entry); | 94 ASSERT_TRUE(entry); |
95 EXPECT_EQ(document_resource_id, entry->resource_id()); | 95 EXPECT_EQ(document_resource_id, entry->resource_id()); |
96 } | 96 } |
97 | 97 |
98 TEST_F(FakeDriveFileSystemTest, GetFileContentByPath) { | 98 TEST_F(FakeFileSystemTest, GetFileContentByPath) { |
99 FileError initialize_error = FILE_ERROR_FAILED; | 99 FileError initialize_error = FILE_ERROR_FAILED; |
100 scoped_ptr<DriveEntryProto> entry_proto; | 100 scoped_ptr<DriveEntryProto> entry_proto; |
101 base::FilePath cache_file_path; | 101 base::FilePath cache_file_path; |
102 base::Closure cancel_download; | 102 base::Closure cancel_download; |
103 google_apis::test_util::TestGetContentCallback get_content_callback; | 103 google_apis::test_util::TestGetContentCallback get_content_callback; |
104 FileError completion_error = FILE_ERROR_FAILED; | 104 FileError completion_error = FILE_ERROR_FAILED; |
105 | 105 |
106 const base::FilePath kDriveFile = | 106 const base::FilePath kDriveFile = |
107 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 107 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
108 | 108 |
109 // For the first time, the file should be downloaded from the service. | 109 // For the first time, the file should be downloaded from the service. |
110 fake_drive_file_system_->GetFileContentByPath( | 110 fake_file_system_->GetFileContentByPath( |
111 kDriveFile, | 111 kDriveFile, |
112 google_apis::test_util::CreateCopyResultCallback( | 112 google_apis::test_util::CreateCopyResultCallback( |
113 &initialize_error, &entry_proto, &cache_file_path, &cancel_download), | 113 &initialize_error, &entry_proto, &cache_file_path, &cancel_download), |
114 get_content_callback.callback(), | 114 get_content_callback.callback(), |
115 google_apis::test_util::CreateCopyResultCallback(&completion_error)); | 115 google_apis::test_util::CreateCopyResultCallback(&completion_error)); |
116 google_apis::test_util::RunBlockingPoolTask(); | 116 google_apis::test_util::RunBlockingPoolTask(); |
117 | 117 |
118 EXPECT_EQ(FILE_ERROR_OK, initialize_error); | 118 EXPECT_EQ(FILE_ERROR_OK, initialize_error); |
119 EXPECT_TRUE(entry_proto); | 119 EXPECT_TRUE(entry_proto); |
120 | 120 |
121 // No cache file is available yet. | 121 // No cache file is available yet. |
122 EXPECT_TRUE(cache_file_path.empty()); | 122 EXPECT_TRUE(cache_file_path.empty()); |
123 | 123 |
124 // The download should be happened so the |get_content_callback| | 124 // The download should be happened so the |get_content_callback| |
125 // should have the actual data. | 125 // should have the actual data. |
126 std::string content = get_content_callback.GetConcatenatedData(); | 126 std::string content = get_content_callback.GetConcatenatedData(); |
127 EXPECT_EQ(10U, content.size()); | 127 EXPECT_EQ(10U, content.size()); |
128 EXPECT_EQ(FILE_ERROR_OK, completion_error); | 128 EXPECT_EQ(FILE_ERROR_OK, completion_error); |
129 | 129 |
130 initialize_error = FILE_ERROR_FAILED; | 130 initialize_error = FILE_ERROR_FAILED; |
131 entry_proto.reset(); | 131 entry_proto.reset(); |
132 get_content_callback.mutable_data()->clear(); | 132 get_content_callback.mutable_data()->clear(); |
133 completion_error = FILE_ERROR_FAILED; | 133 completion_error = FILE_ERROR_FAILED; |
134 | 134 |
135 // For the second time, the cache file should be found. | 135 // For the second time, the cache file should be found. |
136 fake_drive_file_system_->GetFileContentByPath( | 136 fake_file_system_->GetFileContentByPath( |
137 kDriveFile, | 137 kDriveFile, |
138 google_apis::test_util::CreateCopyResultCallback( | 138 google_apis::test_util::CreateCopyResultCallback( |
139 &initialize_error, &entry_proto, &cache_file_path, &cancel_download), | 139 &initialize_error, &entry_proto, &cache_file_path, &cancel_download), |
140 get_content_callback.callback(), | 140 get_content_callback.callback(), |
141 google_apis::test_util::CreateCopyResultCallback(&completion_error)); | 141 google_apis::test_util::CreateCopyResultCallback(&completion_error)); |
142 google_apis::test_util::RunBlockingPoolTask(); | 142 google_apis::test_util::RunBlockingPoolTask(); |
143 | 143 |
144 EXPECT_EQ(FILE_ERROR_OK, initialize_error); | 144 EXPECT_EQ(FILE_ERROR_OK, initialize_error); |
145 EXPECT_TRUE(entry_proto); | 145 EXPECT_TRUE(entry_proto); |
146 | 146 |
147 // Cache file should be available. | 147 // Cache file should be available. |
148 ASSERT_FALSE(cache_file_path.empty()); | 148 ASSERT_FALSE(cache_file_path.empty()); |
149 | 149 |
150 // There should be a cache file so no data should be downloaded. | 150 // There should be a cache file so no data should be downloaded. |
151 EXPECT_TRUE(get_content_callback.data().empty()); | 151 EXPECT_TRUE(get_content_callback.data().empty()); |
152 EXPECT_EQ(FILE_ERROR_OK, completion_error); | 152 EXPECT_EQ(FILE_ERROR_OK, completion_error); |
153 | 153 |
154 // Make sure the cached file's content. | 154 // Make sure the cached file's content. |
155 std::string cache_file_content; | 155 std::string cache_file_content; |
156 ASSERT_TRUE( | 156 ASSERT_TRUE( |
157 file_util::ReadFileToString(cache_file_path, &cache_file_content)); | 157 file_util::ReadFileToString(cache_file_path, &cache_file_content)); |
158 EXPECT_EQ(content, cache_file_content); | 158 EXPECT_EQ(content, cache_file_content); |
159 } | 159 } |
160 | 160 |
161 TEST_F(FakeDriveFileSystemTest, GetFileContentByPath_Directory) { | 161 TEST_F(FakeFileSystemTest, GetFileContentByPath_Directory) { |
162 FileError initialize_error = FILE_ERROR_FAILED; | 162 FileError initialize_error = FILE_ERROR_FAILED; |
163 scoped_ptr<DriveEntryProto> entry_proto; | 163 scoped_ptr<DriveEntryProto> entry_proto; |
164 base::FilePath cache_file_path; | 164 base::FilePath cache_file_path; |
165 google_apis::test_util::TestGetContentCallback get_content_callback; | 165 google_apis::test_util::TestGetContentCallback get_content_callback; |
166 FileError completion_error = FILE_ERROR_FAILED; | 166 FileError completion_error = FILE_ERROR_FAILED; |
167 base::Closure cancel_download; | 167 base::Closure cancel_download; |
168 | 168 |
169 fake_drive_file_system_->GetFileContentByPath( | 169 fake_file_system_->GetFileContentByPath( |
170 util::GetDriveMyDriveRootPath(), | 170 util::GetDriveMyDriveRootPath(), |
171 google_apis::test_util::CreateCopyResultCallback( | 171 google_apis::test_util::CreateCopyResultCallback( |
172 &initialize_error, &entry_proto, &cache_file_path, &cancel_download), | 172 &initialize_error, &entry_proto, &cache_file_path, &cancel_download), |
173 get_content_callback.callback(), | 173 get_content_callback.callback(), |
174 google_apis::test_util::CreateCopyResultCallback(&completion_error)); | 174 google_apis::test_util::CreateCopyResultCallback(&completion_error)); |
175 google_apis::test_util::RunBlockingPoolTask(); | 175 google_apis::test_util::RunBlockingPoolTask(); |
176 | 176 |
177 EXPECT_EQ(FILE_ERROR_NOT_A_FILE, completion_error); | 177 EXPECT_EQ(FILE_ERROR_NOT_A_FILE, completion_error); |
178 } | 178 } |
179 | 179 |
180 TEST_F(FakeDriveFileSystemTest, GetEntryInfoByPath) { | 180 TEST_F(FakeFileSystemTest, GetEntryInfoByPath) { |
181 FileError error = FILE_ERROR_FAILED; | 181 FileError error = FILE_ERROR_FAILED; |
182 scoped_ptr<DriveEntryProto> entry; | 182 scoped_ptr<DriveEntryProto> entry; |
183 fake_drive_file_system_->GetEntryInfoByPath( | 183 fake_file_system_->GetEntryInfoByPath( |
184 util::GetDriveMyDriveRootPath().AppendASCII( | 184 util::GetDriveMyDriveRootPath().AppendASCII( |
185 "Directory 1/Sub Directory Folder"), | 185 "Directory 1/Sub Directory Folder"), |
186 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); | 186 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); |
187 google_apis::test_util::RunBlockingPoolTask(); | 187 google_apis::test_util::RunBlockingPoolTask(); |
188 | 188 |
189 ASSERT_EQ(FILE_ERROR_OK, error); | 189 ASSERT_EQ(FILE_ERROR_OK, error); |
190 ASSERT_TRUE(entry); | 190 ASSERT_TRUE(entry); |
191 EXPECT_EQ("folder:sub_dir_folder_resource_id", entry->resource_id()); | 191 EXPECT_EQ("folder:sub_dir_folder_resource_id", entry->resource_id()); |
192 } | 192 } |
193 | 193 |
194 TEST_F(FakeDriveFileSystemTest, GetEntryInfoByPath_Root) { | 194 TEST_F(FakeFileSystemTest, GetEntryInfoByPath_Root) { |
195 FileError error = FILE_ERROR_FAILED; | 195 FileError error = FILE_ERROR_FAILED; |
196 scoped_ptr<DriveEntryProto> entry; | 196 scoped_ptr<DriveEntryProto> entry; |
197 fake_drive_file_system_->GetEntryInfoByPath( | 197 fake_file_system_->GetEntryInfoByPath( |
198 util::GetDriveMyDriveRootPath(), | 198 util::GetDriveMyDriveRootPath(), |
199 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); | 199 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); |
200 google_apis::test_util::RunBlockingPoolTask(); | 200 google_apis::test_util::RunBlockingPoolTask(); |
201 | 201 |
202 ASSERT_EQ(FILE_ERROR_OK, error); | 202 ASSERT_EQ(FILE_ERROR_OK, error); |
203 ASSERT_TRUE(entry); | 203 ASSERT_TRUE(entry); |
204 EXPECT_TRUE(entry->file_info().is_directory()); | 204 EXPECT_TRUE(entry->file_info().is_directory()); |
205 EXPECT_EQ(fake_drive_service_->GetRootResourceId(), entry->resource_id()); | 205 EXPECT_EQ(fake_drive_service_->GetRootResourceId(), entry->resource_id()); |
206 EXPECT_EQ(util::kDriveMyDriveRootDirName, entry->title()); | 206 EXPECT_EQ(util::kDriveMyDriveRootDirName, entry->title()); |
207 } | 207 } |
208 | 208 |
209 TEST_F(FakeDriveFileSystemTest, GetEntryInfoByPath_Invalid) { | 209 TEST_F(FakeFileSystemTest, GetEntryInfoByPath_Invalid) { |
210 FileError error = FILE_ERROR_FAILED; | 210 FileError error = FILE_ERROR_FAILED; |
211 scoped_ptr<DriveEntryProto> entry; | 211 scoped_ptr<DriveEntryProto> entry; |
212 fake_drive_file_system_->GetEntryInfoByPath( | 212 fake_file_system_->GetEntryInfoByPath( |
213 util::GetDriveMyDriveRootPath().AppendASCII("Invalid File Name"), | 213 util::GetDriveMyDriveRootPath().AppendASCII("Invalid File Name"), |
214 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); | 214 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); |
215 google_apis::test_util::RunBlockingPoolTask(); | 215 google_apis::test_util::RunBlockingPoolTask(); |
216 | 216 |
217 ASSERT_EQ(FILE_ERROR_NOT_FOUND, error); | 217 ASSERT_EQ(FILE_ERROR_NOT_FOUND, error); |
218 ASSERT_FALSE(entry); | 218 ASSERT_FALSE(entry); |
219 } | 219 } |
220 | 220 |
221 } // namespace test_util | 221 } // namespace test_util |
222 } // namespace drive | 222 } // namespace drive |
OLD | NEW |