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 29 matching lines...) Expand all Loading... | |
40 // dir1/file4 | 40 // dir1/file4 |
41 // dir1/file5 | 41 // dir1/file5 |
42 // dir2/file6 | 42 // dir2/file6 |
43 // dir2/file7 | 43 // dir2/file7 |
44 // dir2/file8 | 44 // dir2/file8 |
45 // dir1/dir3/file9 | 45 // dir1/dir3/file9 |
46 // dir1/dir3/file10 | 46 // 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, int sequence_id); | 50 GDataDirectory* AddDirectory(GDataDirectory* parent, |
51 GDataFile* AddFile(GDataDirectory* parent, int sequence_id); | 51 GDataRootDirectory* root, int sequence_id); |
52 GDataFile* AddFile(GDataDirectory* parent, | |
53 GDataRootDirectory* root, int sequence_id); | |
52 | 54 |
53 // Tests GDataDB::NewIterator and GDataDBIter::GetNext. | 55 // Tests GDataDB::NewIterator and GDataDBIter::GetNext. |
54 // Creates an iterator with start at |parent|, and iterates comparing with | 56 // Creates an iterator with start at |parent|, and iterates comparing with |
55 // expected |filenames|. | 57 // expected |filenames|. |
56 void TestIter(const std::string& parent, | 58 void TestIter(const std::string& parent, |
57 const char* file_paths[], | 59 const char* file_paths[], |
58 size_t file_paths_size); | 60 size_t file_paths_size); |
59 | 61 |
60 scoped_ptr<TestingProfile> profile_; | 62 scoped_ptr<TestingProfile> profile_; |
61 scoped_ptr<GDataDB> gdata_db_; | 63 scoped_ptr<GDataDB> gdata_db_; |
62 GDataRootDirectory root_; | 64 std::set<GDataEntry*> entry_set_; |
63 }; | 65 }; |
64 | 66 |
65 void GDataDBTest::SetUp() { | 67 void GDataDBTest::SetUp() { |
66 profile_.reset(new TestingProfile()); | 68 profile_.reset(new TestingProfile()); |
67 gdata_db_ = db_factory::CreateGDataDB( | 69 gdata_db_ = db_factory::CreateGDataDB( |
68 profile_->GetPath().Append("testdb")); | 70 profile_->GetPath().Append("testdb")); |
69 } | 71 } |
70 | 72 |
71 void GDataDBTest::TestGetNotFound(const GDataEntry& source) { | 73 void GDataDBTest::TestGetNotFound(const GDataEntry& source) { |
72 scoped_ptr<GDataEntry> entry; | 74 scoped_ptr<GDataEntry> entry; |
(...skipping 19 matching lines...) Expand all Loading... | |
92 status = gdata_db_->GetByResourceId(source.resource_id(), &entry); | 94 status = gdata_db_->GetByResourceId(source.resource_id(), &entry); |
93 EXPECT_EQ(GDataDB::DB_OK, status); | 95 EXPECT_EQ(GDataDB::DB_OK, status); |
94 ASSERT_TRUE(entry.get()); | 96 ASSERT_TRUE(entry.get()); |
95 EXPECT_EQ(source.file_name(), entry->file_name()); | 97 EXPECT_EQ(source.file_name(), entry->file_name()); |
96 EXPECT_EQ(source.resource_id(), entry->resource_id()); | 98 EXPECT_EQ(source.resource_id(), entry->resource_id()); |
97 EXPECT_EQ(source.content_url(), entry->content_url()); | 99 EXPECT_EQ(source.content_url(), entry->content_url()); |
98 } | 100 } |
99 | 101 |
100 void GDataDBTest::InitDB() { | 102 void GDataDBTest::InitDB() { |
101 int sequence_id = 1; | 103 int sequence_id = 1; |
102 GDataDirectory* dir1 = AddDirectory(NULL, sequence_id++); | 104 GDataRootDirectory root; |
103 GDataDirectory* dir2 = AddDirectory(NULL, sequence_id++); | 105 GDataDirectory* dir1 = AddDirectory(NULL, &root, sequence_id++); |
104 GDataDirectory* dir3 = AddDirectory(dir1, sequence_id++); | 106 GDataDirectory* dir2 = AddDirectory(NULL, &root, sequence_id++); |
107 GDataDirectory* dir3 = AddDirectory(dir1, &root, sequence_id++); | |
105 | 108 |
106 AddFile(dir1, sequence_id++); | 109 AddFile(dir1, &root, sequence_id++); |
107 AddFile(dir1, sequence_id++); | 110 AddFile(dir1, &root, sequence_id++); |
108 | 111 |
109 AddFile(dir2, sequence_id++); | 112 AddFile(dir2, &root, sequence_id++); |
110 AddFile(dir2, sequence_id++); | 113 AddFile(dir2, &root, sequence_id++); |
111 AddFile(dir2, sequence_id++); | 114 AddFile(dir2, &root, sequence_id++); |
112 | 115 |
113 AddFile(dir3, sequence_id++); | 116 AddFile(dir3, &root, sequence_id++); |
114 AddFile(dir3, sequence_id++); | 117 AddFile(dir3, &root, sequence_id++); |
118 | |
119 STLDeleteElements(&entry_set_); | |
satorux1
2012/04/25 17:47:23
Is this the right place to delete? Shouldn't we do
achuithb
2012/04/25 18:34:38
The root is already destroyed when we return from
| |
115 } | 120 } |
116 | 121 |
117 GDataDirectory* GDataDBTest::AddDirectory(GDataDirectory* parent, | 122 GDataDirectory* GDataDBTest::AddDirectory(GDataDirectory* parent, |
123 GDataRootDirectory* root, | |
118 int sequence_id) { | 124 int sequence_id) { |
119 GDataDirectory* dir = new GDataDirectory(parent ? parent : &root_, &root_); | 125 GDataDirectory* dir = new GDataDirectory(parent ? parent : root, root); |
120 const std::string dir_name = "dir" + base::IntToString(sequence_id); | 126 const std::string dir_name = "dir" + base::IntToString(sequence_id); |
121 const std::string resource_id = std::string("dir_resource_id:") + | 127 const std::string resource_id = std::string("dir_resource_id:") + |
122 dir_name; | 128 dir_name; |
123 dir->set_file_name(dir_name); | 129 dir->set_file_name(dir_name); |
124 dir->set_resource_id(resource_id); | 130 dir->set_resource_id(resource_id); |
125 GDataDB::Status status = gdata_db_->Put(*dir); | 131 GDataDB::Status status = gdata_db_->Put(*dir); |
126 EXPECT_EQ(GDataDB::DB_OK, status); | 132 EXPECT_EQ(GDataDB::DB_OK, status); |
127 DVLOG(1) << "AddDirectory " << dir->GetFilePath().value() | 133 DVLOG(1) << "AddDirectory " << dir->GetFilePath().value() |
128 << ", " << resource_id; | 134 << ", " << resource_id; |
135 entry_set_.insert(dir); | |
129 return dir; | 136 return dir; |
130 } | 137 } |
131 | 138 |
132 GDataFile* GDataDBTest::AddFile(GDataDirectory* parent, | 139 GDataFile* GDataDBTest::AddFile(GDataDirectory* parent, |
140 GDataRootDirectory* root, | |
133 int sequence_id) { | 141 int sequence_id) { |
134 GDataFile* file = new GDataFile(parent, &root_); | 142 GDataFile* file = new GDataFile(parent, root); |
135 const std::string file_name = "file" + base::IntToString(sequence_id); | 143 const std::string file_name = "file" + base::IntToString(sequence_id); |
136 const std::string resource_id = std::string("file_resource_id:") + | 144 const std::string resource_id = std::string("file_resource_id:") + |
137 file_name; | 145 file_name; |
138 file->set_file_name(file_name); | 146 file->set_file_name(file_name); |
139 file->set_resource_id(resource_id); | 147 file->set_resource_id(resource_id); |
140 GDataDB::Status status = gdata_db_->Put(*file); | 148 GDataDB::Status status = gdata_db_->Put(*file); |
141 EXPECT_EQ(GDataDB::DB_OK, status); | 149 EXPECT_EQ(GDataDB::DB_OK, status); |
142 DVLOG(1) << "AddFile " << file->GetFilePath().value() | 150 DVLOG(1) << "AddFile " << file->GetFilePath().value() |
143 << ", " << resource_id; | 151 << ", " << resource_id; |
152 entry_set_.insert(file); | |
144 return file; | 153 return file; |
145 } | 154 } |
146 | 155 |
147 void GDataDBTest::TestIter(const std::string& parent, | 156 void GDataDBTest::TestIter(const std::string& parent, |
148 const char* file_paths[], | 157 const char* file_paths[], |
149 size_t file_paths_size) { | 158 size_t file_paths_size) { |
150 scoped_ptr<GDataDBIter> iter = gdata_db_->CreateIterator( | 159 scoped_ptr<GDataDBIter> iter = gdata_db_->CreateIterator( |
151 FilePath::FromUTF8Unsafe(parent)); | 160 FilePath::FromUTF8Unsafe(parent)); |
152 for (size_t i = 0; ; ++i) { | 161 for (size_t i = 0; ; ++i) { |
153 scoped_ptr<GDataEntry> entry; | 162 scoped_ptr<GDataEntry> entry; |
154 std::string path; | 163 std::string path; |
155 if (!iter->GetNext(&path, &entry)) { | 164 if (!iter->GetNext(&path, &entry)) { |
156 EXPECT_EQ(i, file_paths_size); | 165 EXPECT_EQ(i, file_paths_size); |
157 break; | 166 break; |
158 } | 167 } |
159 ASSERT_LT(i, file_paths_size); | 168 ASSERT_LT(i, file_paths_size); |
160 // TODO(achuith): Also test entry->GetFilePath(). | 169 // TODO(achuith): Also test entry->GetFilePath(). |
161 EXPECT_EQ(FilePath(file_paths[i]).BaseName().value(), entry->file_name()); | 170 EXPECT_EQ(FilePath(file_paths[i]).BaseName().value(), entry->file_name()); |
162 EXPECT_EQ(file_paths[i], path); | 171 EXPECT_EQ(file_paths[i], path); |
163 DVLOG(1) << "Iter " << path; | 172 DVLOG(1) << "Iter " << path; |
164 } | 173 } |
165 } | 174 } |
166 | 175 |
167 } // namespace | 176 } // namespace |
168 | 177 |
169 TEST_F(GDataDBTest, PutTest) { | 178 TEST_F(GDataDBTest, PutTest) { |
170 GDataDirectory* dir = new GDataDirectory(&root_, &root_); | 179 GDataRootDirectory root; |
171 dir->set_file_name("dir"); | 180 GDataDirectory dir(&root, &root); |
172 dir->set_resource_id("dir_resource_id"); | 181 dir.set_file_name("dir"); |
173 dir->set_content_url(GURL("http://content/dir")); | 182 dir.set_resource_id("dir_resource_id"); |
174 dir->set_upload_url(GURL("http://upload/dir")); | 183 dir.set_content_url(GURL("http://content/dir")); |
184 dir.set_upload_url(GURL("http://upload/dir")); | |
175 | 185 |
176 TestGetNotFound(*dir); | 186 TestGetNotFound(dir); |
177 | 187 |
178 GDataDB::Status status = gdata_db_->Put(*dir); | 188 GDataDB::Status status = gdata_db_->Put(dir); |
179 EXPECT_EQ(GDataDB::DB_OK, status); | 189 EXPECT_EQ(GDataDB::DB_OK, status); |
180 | 190 |
181 TestGetFound(*dir); | 191 TestGetFound(dir); |
182 | 192 |
183 scoped_ptr<GDataEntry> entry; | 193 scoped_ptr<GDataEntry> entry; |
184 gdata_db_->GetByPath(dir->GetFilePath(), &entry); | 194 gdata_db_->GetByPath(dir.GetFilePath(), &entry); |
185 EXPECT_EQ(dir->upload_url(), entry->AsGDataDirectory()->upload_url()); | 195 EXPECT_EQ(dir.upload_url(), entry->AsGDataDirectory()->upload_url()); |
186 EXPECT_TRUE(entry->AsGDataDirectory()->file_info().is_directory); | 196 EXPECT_TRUE(entry->AsGDataDirectory()->file_info().is_directory); |
187 | 197 |
188 status = gdata_db_->DeleteByPath(dir->GetFilePath()); | 198 status = gdata_db_->DeleteByPath(dir.GetFilePath()); |
189 EXPECT_EQ(GDataDB::DB_OK, status); | 199 EXPECT_EQ(GDataDB::DB_OK, status); |
190 | 200 |
191 TestGetNotFound(*dir); | 201 TestGetNotFound(dir); |
192 | 202 |
193 GDataFile* file = new GDataFile(dir, &root_); | 203 GDataFile file(&dir, &root); |
194 file->set_file_name("file1"); | 204 file.set_file_name("file"); |
195 file->set_resource_id("file1_resource_id"); | 205 file.set_resource_id("file_resource_id"); |
196 file->set_content_url(GURL("http://content/dir1/file1")); | 206 file.set_content_url(GURL("http://content/dir/file")); |
197 file->set_file_md5("file1_md5"); | 207 file.set_file_md5("file_md5"); |
198 | 208 |
199 TestGetNotFound(*file); | 209 TestGetNotFound(file); |
200 | 210 |
201 status = gdata_db_->Put(*file); | 211 status = gdata_db_->Put(file); |
202 EXPECT_EQ(GDataDB::DB_OK, status); | 212 EXPECT_EQ(GDataDB::DB_OK, status); |
203 | 213 |
204 TestGetFound(*file); | 214 TestGetFound(file); |
205 | 215 |
206 gdata_db_->GetByPath(file->GetFilePath(), &entry); | 216 gdata_db_->GetByPath(file.GetFilePath(), &entry); |
207 EXPECT_EQ(file->file_md5(), entry->AsGDataFile()->file_md5()); | 217 EXPECT_EQ(file.file_md5(), entry->AsGDataFile()->file_md5()); |
208 EXPECT_FALSE(entry->AsGDataFile()->file_info().is_directory); | 218 EXPECT_FALSE(entry->AsGDataFile()->file_info().is_directory); |
209 | 219 |
210 status = gdata_db_->DeleteByPath(file->GetFilePath()); | 220 status = gdata_db_->DeleteByPath(file.GetFilePath()); |
211 EXPECT_EQ(GDataDB::DB_OK, status); | 221 EXPECT_EQ(GDataDB::DB_OK, status); |
212 | 222 |
213 TestGetNotFound(*file); | 223 TestGetNotFound(file); |
214 } | 224 } |
215 | 225 |
216 TEST_F(GDataDBTest, IterTest) { | 226 TEST_F(GDataDBTest, IterTest) { |
217 InitDB(); | 227 InitDB(); |
218 | 228 |
219 const char* dir1_children[] = { | 229 const char* dir1_children[] = { |
220 "dir1", | 230 "dir1", |
221 "dir1/dir3", | 231 "dir1/dir3", |
222 "dir1/dir3/file10", | 232 "dir1/dir3/file10", |
223 "dir1/dir3/file9", | 233 "dir1/dir3/file9", |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
257 "dir2/file6", | 267 "dir2/file6", |
258 "dir2/file7", | 268 "dir2/file7", |
259 "dir2/file8", | 269 "dir2/file8", |
260 }; | 270 }; |
261 TestIter("", all_entries, arraysize(all_entries)); | 271 TestIter("", all_entries, arraysize(all_entries)); |
262 | 272 |
263 TestIter("dir4", NULL, 0); | 273 TestIter("dir4", NULL, 0); |
264 } | 274 } |
265 | 275 |
266 } // namespace gdata | 276 } // namespace gdata |
OLD | NEW |