OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/gdata/gdata_db.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_db.h" |
6 | 6 |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "chrome/browser/chromeos/gdata/gdata_db_factory.h" | 8 #include "chrome/browser/chromeos/gdata/gdata_db_factory.h" |
9 #include "chrome/browser/chromeos/gdata/gdata_files.h" | 9 #include "chrome/browser/chromeos/gdata/gdata_files.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 // Tests GDataDB::GetPath and GDataDB::ResourceId, ensuring that an entry | 28 // Tests GDataDB::GetPath and GDataDB::ResourceId, ensuring that an entry |
29 // matching |source| does not exist. | 29 // matching |source| does not exist. |
30 void TestGetNotFound(const GDataEntry& source); | 30 void TestGetNotFound(const GDataEntry& source); |
31 | 31 |
32 // Tests GDataDB::GetPath and GDataDB::ResourceId, ensuring that an entry | 32 // Tests GDataDB::GetPath and GDataDB::ResourceId, ensuring that an entry |
33 // matching |source| exists. | 33 // matching |source| exists. |
34 void TestGetFound(const GDataEntry& source); | 34 void TestGetFound(const GDataEntry& source); |
35 | 35 |
36 // Initialize the database with the following entries: | 36 // Initialize the database with the following entries: |
37 // dir1 | 37 // gdata/dir1 |
38 // dir2 | 38 // gdata/dir2 |
39 // dir1/dir3 | 39 // gdata/dir1/dir3 |
40 // dir1/file4 | 40 // gdata/dir1/file4 |
41 // dir1/file5 | 41 // gdata/dir1/file5 |
42 // dir2/file6 | 42 // gdata/dir2/file6 |
43 // dir2/file7 | 43 // gdata/dir2/file7 |
44 // dir2/file8 | 44 // gdata/dir2/file8 |
45 // dir1/dir3/file9 | 45 // gdata/dir1/dir3/file9 |
46 // dir1/dir3/file10 | 46 // gdata/dir1/dir3/file10 |
47 void InitDB(); | 47 void InitDB(); |
48 | 48 |
49 // Helper functions to add a directory/file, incrementing index. | 49 // Helper functions to add a directory/file, incrementing index. |
50 GDataDirectory* AddDirectory(GDataDirectory* parent, | 50 GDataDirectory* AddDirectory(GDataDirectory* parent, |
51 GDataRootDirectory* root, int sequence_id); | 51 GDataRootDirectory* root, int sequence_id); |
52 GDataFile* AddFile(GDataDirectory* parent, | 52 GDataFile* AddFile(GDataDirectory* parent, |
53 GDataRootDirectory* root, int sequence_id); | 53 GDataRootDirectory* root, int sequence_id); |
54 | 54 |
55 // Tests GDataDB::NewIterator and GDataDBIter::GetNext. | 55 // Tests GDataDB::NewIterator and GDataDBIter::GetNext. |
56 // Creates an iterator with start at |parent|, and iterates comparing with | 56 // Creates an iterator with start at |parent|, and iterates comparing with |
(...skipping 22 matching lines...) Expand all Loading... |
79 status = gdata_db_->GetByResourceId(source.resource_id(), &entry); | 79 status = gdata_db_->GetByResourceId(source.resource_id(), &entry); |
80 EXPECT_EQ(GDataDB::DB_KEY_NOT_FOUND, status); | 80 EXPECT_EQ(GDataDB::DB_KEY_NOT_FOUND, status); |
81 EXPECT_FALSE(entry.get()); | 81 EXPECT_FALSE(entry.get()); |
82 } | 82 } |
83 | 83 |
84 void GDataDBTest::TestGetFound(const GDataEntry& source) { | 84 void GDataDBTest::TestGetFound(const GDataEntry& source) { |
85 scoped_ptr<GDataEntry> entry; | 85 scoped_ptr<GDataEntry> entry; |
86 GDataDB::Status status = gdata_db_->GetByPath(source.GetFilePath(), &entry); | 86 GDataDB::Status status = gdata_db_->GetByPath(source.GetFilePath(), &entry); |
87 EXPECT_EQ(GDataDB::DB_OK, status); | 87 EXPECT_EQ(GDataDB::DB_OK, status); |
88 ASSERT_TRUE(entry.get()); | 88 ASSERT_TRUE(entry.get()); |
89 EXPECT_EQ(source.file_name(), entry->file_name()); | 89 EXPECT_EQ(source.title(), entry->title()); |
90 EXPECT_EQ(source.resource_id(), entry->resource_id()); | 90 EXPECT_EQ(source.resource_id(), entry->resource_id()); |
91 EXPECT_EQ(source.content_url(), entry->content_url()); | 91 EXPECT_EQ(source.content_url(), entry->content_url()); |
92 entry.reset(); | 92 entry.reset(); |
93 | 93 |
94 status = gdata_db_->GetByResourceId(source.resource_id(), &entry); | 94 status = gdata_db_->GetByResourceId(source.resource_id(), &entry); |
95 EXPECT_EQ(GDataDB::DB_OK, status); | 95 EXPECT_EQ(GDataDB::DB_OK, status); |
96 ASSERT_TRUE(entry.get()); | 96 ASSERT_TRUE(entry.get()); |
97 EXPECT_EQ(source.file_name(), entry->file_name()); | 97 EXPECT_EQ(source.title(), entry->title()); |
98 EXPECT_EQ(source.resource_id(), entry->resource_id()); | 98 EXPECT_EQ(source.resource_id(), entry->resource_id()); |
99 EXPECT_EQ(source.content_url(), entry->content_url()); | 99 EXPECT_EQ(source.content_url(), entry->content_url()); |
100 } | 100 } |
101 | 101 |
102 void GDataDBTest::InitDB() { | 102 void GDataDBTest::InitDB() { |
103 int sequence_id = 1; | 103 int sequence_id = 1; |
104 GDataRootDirectory root; | 104 GDataRootDirectory root; |
105 GDataDirectory* dir1 = AddDirectory(NULL, &root, sequence_id++); | 105 GDataDirectory* dir1 = AddDirectory(NULL, &root, sequence_id++); |
106 GDataDirectory* dir2 = AddDirectory(NULL, &root, sequence_id++); | 106 GDataDirectory* dir2 = AddDirectory(NULL, &root, sequence_id++); |
107 GDataDirectory* dir3 = AddDirectory(dir1, &root, sequence_id++); | 107 GDataDirectory* dir3 = AddDirectory(dir1, &root, sequence_id++); |
(...skipping 11 matching lines...) Expand all Loading... |
119 STLDeleteElements(&entry_set_); | 119 STLDeleteElements(&entry_set_); |
120 } | 120 } |
121 | 121 |
122 GDataDirectory* GDataDBTest::AddDirectory(GDataDirectory* parent, | 122 GDataDirectory* GDataDBTest::AddDirectory(GDataDirectory* parent, |
123 GDataRootDirectory* root, | 123 GDataRootDirectory* root, |
124 int sequence_id) { | 124 int sequence_id) { |
125 GDataDirectory* dir = new GDataDirectory(parent ? parent : root, root); | 125 GDataDirectory* dir = new GDataDirectory(parent ? parent : root, root); |
126 const std::string dir_name = "dir" + base::IntToString(sequence_id); | 126 const std::string dir_name = "dir" + base::IntToString(sequence_id); |
127 const std::string resource_id = std::string("dir_resource_id:") + | 127 const std::string resource_id = std::string("dir_resource_id:") + |
128 dir_name; | 128 dir_name; |
| 129 dir->set_title(dir_name); |
129 dir->set_file_name(dir_name); | 130 dir->set_file_name(dir_name); |
130 dir->set_resource_id(resource_id); | 131 dir->set_resource_id(resource_id); |
131 GDataDB::Status status = gdata_db_->Put(*dir); | 132 GDataDB::Status status = gdata_db_->Put(*dir); |
132 EXPECT_EQ(GDataDB::DB_OK, status); | 133 EXPECT_EQ(GDataDB::DB_OK, status); |
133 DVLOG(1) << "AddDirectory " << dir->GetFilePath().value() | 134 DVLOG(1) << "AddDirectory " << dir->GetFilePath().value() |
134 << ", " << resource_id; | 135 << ", " << resource_id; |
135 entry_set_.insert(dir); | 136 entry_set_.insert(dir); |
136 return dir; | 137 return dir; |
137 } | 138 } |
138 | 139 |
139 GDataFile* GDataDBTest::AddFile(GDataDirectory* parent, | 140 GDataFile* GDataDBTest::AddFile(GDataDirectory* parent, |
140 GDataRootDirectory* root, | 141 GDataRootDirectory* root, |
141 int sequence_id) { | 142 int sequence_id) { |
142 GDataFile* file = new GDataFile(parent, root); | 143 GDataFile* file = new GDataFile(parent, root); |
143 const std::string file_name = "file" + base::IntToString(sequence_id); | 144 const std::string title = "file" + base::IntToString(sequence_id); |
144 const std::string resource_id = std::string("file_resource_id:") + | 145 const std::string resource_id = std::string("file_resource_id:") + |
145 file_name; | 146 title; |
146 file->set_file_name(file_name); | 147 file->set_title(title); |
| 148 file->set_file_name(title); |
147 file->set_resource_id(resource_id); | 149 file->set_resource_id(resource_id); |
148 GDataDB::Status status = gdata_db_->Put(*file); | 150 GDataDB::Status status = gdata_db_->Put(*file); |
149 EXPECT_EQ(GDataDB::DB_OK, status); | 151 EXPECT_EQ(GDataDB::DB_OK, status); |
150 DVLOG(1) << "AddFile " << file->GetFilePath().value() | 152 DVLOG(1) << "AddFile " << file->GetFilePath().value() |
151 << ", " << resource_id; | 153 << ", " << resource_id; |
152 entry_set_.insert(file); | 154 entry_set_.insert(file); |
153 return file; | 155 return file; |
154 } | 156 } |
155 | 157 |
156 void GDataDBTest::TestIter(const std::string& parent, | 158 void GDataDBTest::TestIter(const std::string& parent, |
(...skipping 14 matching lines...) Expand all Loading... |
171 EXPECT_EQ(file_paths[i], path); | 173 EXPECT_EQ(file_paths[i], path); |
172 DVLOG(1) << "Iter " << path; | 174 DVLOG(1) << "Iter " << path; |
173 } | 175 } |
174 } | 176 } |
175 | 177 |
176 } // namespace | 178 } // namespace |
177 | 179 |
178 TEST_F(GDataDBTest, PutTest) { | 180 TEST_F(GDataDBTest, PutTest) { |
179 GDataRootDirectory root; | 181 GDataRootDirectory root; |
180 GDataDirectory dir(&root, &root); | 182 GDataDirectory dir(&root, &root); |
| 183 dir.set_title("dir"); |
181 dir.set_file_name("dir"); | 184 dir.set_file_name("dir"); |
182 dir.set_resource_id("dir_resource_id"); | 185 dir.set_resource_id("dir_resource_id"); |
183 dir.set_content_url(GURL("http://content/dir")); | 186 dir.set_content_url(GURL("http://content/dir")); |
184 dir.set_upload_url(GURL("http://upload/dir")); | 187 dir.set_upload_url(GURL("http://upload/dir")); |
185 | 188 |
186 TestGetNotFound(dir); | 189 TestGetNotFound(dir); |
187 | 190 |
188 GDataDB::Status status = gdata_db_->Put(dir); | 191 GDataDB::Status status = gdata_db_->Put(dir); |
189 EXPECT_EQ(GDataDB::DB_OK, status); | 192 EXPECT_EQ(GDataDB::DB_OK, status); |
190 | 193 |
191 TestGetFound(dir); | 194 TestGetFound(dir); |
192 | 195 |
193 scoped_ptr<GDataEntry> entry; | 196 scoped_ptr<GDataEntry> entry; |
194 gdata_db_->GetByPath(dir.GetFilePath(), &entry); | 197 gdata_db_->GetByPath(dir.GetFilePath(), &entry); |
195 EXPECT_EQ(dir.upload_url(), entry->AsGDataDirectory()->upload_url()); | 198 EXPECT_EQ(dir.upload_url(), entry->AsGDataDirectory()->upload_url()); |
196 EXPECT_TRUE(entry->AsGDataDirectory()->file_info().is_directory); | 199 EXPECT_TRUE(entry->AsGDataDirectory()->file_info().is_directory); |
197 | 200 |
198 status = gdata_db_->DeleteByPath(dir.GetFilePath()); | 201 status = gdata_db_->DeleteByPath(dir.GetFilePath()); |
199 EXPECT_EQ(GDataDB::DB_OK, status); | 202 EXPECT_EQ(GDataDB::DB_OK, status); |
200 | 203 |
201 TestGetNotFound(dir); | 204 TestGetNotFound(dir); |
202 | 205 |
203 GDataFile file(&dir, &root); | 206 GDataFile file(&dir, &root); |
| 207 file.set_title("file"); |
204 file.set_file_name("file"); | 208 file.set_file_name("file"); |
205 file.set_resource_id("file_resource_id"); | 209 file.set_resource_id("file_resource_id"); |
206 file.set_content_url(GURL("http://content/dir/file")); | 210 file.set_content_url(GURL("http://content/dir/file")); |
207 file.set_file_md5("file_md5"); | 211 file.set_file_md5("file_md5"); |
208 | 212 |
209 TestGetNotFound(file); | 213 TestGetNotFound(file); |
210 | 214 |
211 status = gdata_db_->Put(file); | 215 status = gdata_db_->Put(file); |
212 EXPECT_EQ(GDataDB::DB_OK, status); | 216 EXPECT_EQ(GDataDB::DB_OK, status); |
213 | 217 |
214 TestGetFound(file); | 218 TestGetFound(file); |
215 | 219 |
216 gdata_db_->GetByPath(file.GetFilePath(), &entry); | 220 gdata_db_->GetByPath(file.GetFilePath(), &entry); |
217 EXPECT_EQ(file.file_md5(), entry->AsGDataFile()->file_md5()); | 221 EXPECT_EQ(file.file_md5(), entry->AsGDataFile()->file_md5()); |
218 EXPECT_FALSE(entry->AsGDataFile()->file_info().is_directory); | 222 EXPECT_FALSE(entry->AsGDataFile()->file_info().is_directory); |
219 | 223 |
220 status = gdata_db_->DeleteByPath(file.GetFilePath()); | 224 status = gdata_db_->DeleteByPath(file.GetFilePath()); |
221 EXPECT_EQ(GDataDB::DB_OK, status); | 225 EXPECT_EQ(GDataDB::DB_OK, status); |
222 | 226 |
223 TestGetNotFound(file); | 227 TestGetNotFound(file); |
224 } | 228 } |
225 | 229 |
226 TEST_F(GDataDBTest, IterTest) { | 230 TEST_F(GDataDBTest, IterTest) { |
227 InitDB(); | 231 InitDB(); |
228 | 232 |
229 const char* dir1_children[] = { | 233 const char* dir1_children[] = { |
230 "dir1", | 234 "gdata/dir1", |
231 "dir1/dir3", | 235 "gdata/dir1/dir3", |
232 "dir1/dir3/file10", | 236 "gdata/dir1/dir3/file10", |
233 "dir1/dir3/file9", | 237 "gdata/dir1/dir3/file9", |
234 "dir1/file4", | 238 "gdata/dir1/file4", |
235 "dir1/file5", | 239 "gdata/dir1/file5", |
236 }; | 240 }; |
237 TestIter("dir1", dir1_children, arraysize(dir1_children)); | 241 TestIter("gdata/dir1", dir1_children, arraysize(dir1_children)); |
238 | 242 |
239 const char* dir2_children[] = { | 243 const char* dir2_children[] = { |
240 "dir2", | 244 "gdata/dir2", |
241 "dir2/file6", | 245 "gdata/dir2/file6", |
242 "dir2/file7", | 246 "gdata/dir2/file7", |
243 "dir2/file8", | 247 "gdata/dir2/file8", |
244 }; | 248 }; |
245 TestIter("dir2", dir2_children, arraysize(dir2_children)); | 249 TestIter("gdata/dir2", dir2_children, arraysize(dir2_children)); |
246 | 250 |
247 const char* dir3_children[] = { | 251 const char* dir3_children[] = { |
248 "dir1/dir3", | 252 "gdata/dir1/dir3", |
249 "dir1/dir3/file10", | 253 "gdata/dir1/dir3/file10", |
250 "dir1/dir3/file9", | 254 "gdata/dir1/dir3/file9", |
251 }; | 255 }; |
252 TestIter("dir1/dir3", dir3_children, arraysize(dir3_children)); | 256 TestIter("gdata/dir1/dir3", dir3_children, arraysize(dir3_children)); |
253 | 257 |
254 const char* file10[] = { | 258 const char* file10[] = { |
255 "dir1/dir3/file10", | 259 "gdata/dir1/dir3/file10", |
256 }; | 260 }; |
257 TestIter(file10[0], file10, arraysize(file10)); | 261 TestIter(file10[0], file10, arraysize(file10)); |
258 | 262 |
259 const char* all_entries[] = { | 263 const char* all_entries[] = { |
260 "dir1", | 264 "gdata/dir1", |
261 "dir1/dir3", | 265 "gdata/dir1/dir3", |
262 "dir1/dir3/file10", | 266 "gdata/dir1/dir3/file10", |
263 "dir1/dir3/file9", | 267 "gdata/dir1/dir3/file9", |
264 "dir1/file4", | 268 "gdata/dir1/file4", |
265 "dir1/file5", | 269 "gdata/dir1/file5", |
266 "dir2", | 270 "gdata/dir2", |
267 "dir2/file6", | 271 "gdata/dir2/file6", |
268 "dir2/file7", | 272 "gdata/dir2/file7", |
269 "dir2/file8", | 273 "gdata/dir2/file8", |
270 }; | 274 }; |
271 TestIter("", all_entries, arraysize(all_entries)); | 275 TestIter("", all_entries, arraysize(all_entries)); |
272 | 276 |
273 TestIter("dir4", NULL, 0); | 277 TestIter("dir4", NULL, 0); |
274 } | 278 } |
275 | 279 |
276 } // namespace gdata | 280 } // namespace gdata |
OLD | NEW |