Chromium Code Reviews| Index: chrome/browser/chromeos/gdata/gdata_db_unittest.cc |
| =================================================================== |
| --- chrome/browser/chromeos/gdata/gdata_db_unittest.cc (revision 0) |
| +++ chrome/browser/chromeos/gdata/gdata_db_unittest.cc (revision 0) |
| @@ -0,0 +1,283 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
|
satorux1
2012/04/23 17:40:41
shouldn't this be gdata_leveldb_unittest?
achuithb
2012/04/24 08:09:36
No, it tests GDataDB. I'm not including gdata_leve
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/gdata/gdata_db.h" |
| + |
| +#include "base/string_number_conversions.h" |
| +#include "chrome/browser/chromeos/gdata/gdata_files.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "chrome/test/base/testing_profile.h" |
| + |
| +namespace gdata { |
| +namespace { |
| + |
| +class GDataDBTest : public testing::Test { |
| + public: |
| + GDataDBTest() : index_(0) { |
| + } |
| + |
| + virtual ~GDataDBTest() { |
| + } |
| + |
| + protected: |
| + // testing::Test implementation. |
| + virtual void SetUp() OVERRIDE; |
| + |
| + // Tests GDataDB::GetPath and GDataDB::ResourceId, ensuring that an entry |
| + // with path |file_name| or resource_id |resource_id| does not exist. |
| + void TestGetNotFound(const std::string& file_name, |
| + const std::string& resource_id); |
| + |
| + // Tests GDataDB::GetPath and GDataDB::ResourceId, ensuring that an entry |
| + // with path |file_name| and resource_id |resource_id| exists. Additionally |
| + // tests entry's content_url matches |content_url|. |
| + void TestGetFound(const std::string& file_name, |
| + const std::string& resource_id, |
| + const GURL& content_url); |
| + |
| + // Initialize the database with the following entries: |
| + // /gdata/dir1 |
| + // /gdata/dir2 |
| + // /gdata/dir1/dir3 |
| + // /gdata/dir1/file4 |
| + // /gdata/dir1/file5 |
| + // /gdata/dir2/file6 |
| + // /gdata/dir2/file7 |
| + // /gdata/dir2/file8 |
| + // /gdata/dir1/dir3/file9 |
| + // /gdata/dir1/dir3/file10 |
| + void InitDB(); |
| + |
| + // Helper functions to add a directory/file, incrementing index. |
| + void AddDir(const std::string& parent); |
| + void AddFile(const std::string& parent); |
| + |
| + // Tests GDataDB::NewIterator and GDataDBIter::GetNext. |
| + // Creates an iterator with start at |parent|, and iterates comparing with |
| + // expected |filenames|. |
| + void TestIter(const std::string& parent, |
| + const char* file_names[], |
| + uint32 file_names_size); |
| + |
| + int index_; |
| + scoped_ptr<TestingProfile> profile_; |
| + scoped_ptr<GDataDB> gdata_db_; |
| +}; |
| + |
| +void GDataDBTest::SetUp() { |
| + profile_.reset(new TestingProfile()); |
| + gdata_db_ = GDataDB::Create(profile_->GetPath().Append("testdb")); |
| +} |
| + |
| +void GDataDBTest::TestGetNotFound(const std::string& file_name, |
| + const std::string& resource_id) { |
| + scoped_ptr<GDataEntry> entry; |
| + GDataDB::Status status = gdata_db_->GetByPath(file_name, &entry); |
| + EXPECT_EQ(GDataDB::DB_NOT_FOUND, status); |
| + EXPECT_FALSE(entry.get()); |
| + |
| + status = gdata_db_->GetByResourceId(resource_id, &entry); |
| + EXPECT_EQ(GDataDB::DB_NOT_FOUND, status); |
| + EXPECT_FALSE(entry.get()); |
| +} |
| + |
| +void GDataDBTest::TestGetFound(const std::string& file_name, |
| + const std::string& resource_id, |
| + const GURL& content_url) { |
| + scoped_ptr<GDataEntry> entry; |
| + GDataDB::Status status = gdata_db_->GetByPath(file_name, &entry); |
| + EXPECT_EQ(GDataDB::DB_OK, status); |
| + ASSERT_TRUE(entry.get()); |
| + EXPECT_EQ(file_name, entry->file_name()); |
| + EXPECT_EQ(resource_id, entry->resource_id()); |
| + EXPECT_EQ(content_url, entry->content_url()); |
| + entry.reset(); |
| + |
| + status = gdata_db_->GetByResourceId(resource_id, &entry); |
| + EXPECT_EQ(GDataDB::DB_OK, status); |
| + ASSERT_TRUE(entry.get()); |
| + EXPECT_EQ(file_name, entry->file_name()); |
| + EXPECT_EQ(resource_id, entry->resource_id()); |
| + EXPECT_EQ(content_url, entry->content_url()); |
| +} |
| + |
| +void GDataDBTest::InitDB() { |
| + index_ = 1; |
| + AddDir(""); |
| + AddDir(""); |
| + AddDir("dir1"); |
| + |
| + AddFile("dir1"); |
| + AddFile("dir1"); |
| + |
| + AddFile("dir2"); |
| + AddFile("dir2"); |
| + AddFile("dir2"); |
| + |
| + AddFile("dir1/dir3"); |
| + AddFile("dir1/dir3"); |
| +} |
| + |
| +std::string AddTrailingSlash(const std::string& parent) { |
| + std::string parent_str = parent; |
| + if (!parent.empty() && *parent.rbegin() != '/') |
| + parent_str += '/'; |
| + return parent_str; |
| +} |
| + |
| +void GDataDBTest::AddDir(const std::string& parent) { |
| + std::string parent_str = AddTrailingSlash(parent); |
| + GDataDirectory dir(NULL, NULL); |
| + const std::string dir_name = std::string("/gdata/") |
| + + parent_str + "dir" + base::IntToString(index_++); |
| + const std::string resource_id = std::string("dir_resource_id:") |
| + + dir_name; |
| + dir.set_file_name(dir_name); |
| + dir.set_resource_id(resource_id); |
| + GDataDB::Status status = gdata_db_->Put(dir); |
| + EXPECT_EQ(GDataDB::DB_OK, status); |
| + DVLOG(1) << "AddDir " << dir_name << ", " << resource_id; |
| +} |
| + |
| +void GDataDBTest::AddFile(const std::string& parent) { |
| + std::string parent_str = AddTrailingSlash(parent); |
| + GDataFile file(NULL, NULL); |
| + const std::string file_name = std::string("/gdata/") |
| + + parent_str + "file" + base::IntToString(index_++); |
| + const std::string resource_id = std::string("file_resource_id:") |
| + + file_name; |
| + file.set_file_name(file_name); |
| + file.set_resource_id(resource_id); |
| + GDataDB::Status status = gdata_db_->Put(file); |
| + EXPECT_EQ(GDataDB::DB_OK, status); |
| + DVLOG(1) << "AddFile " << file_name << ", " << resource_id; |
| +} |
| + |
| +void GDataDBTest::TestIter(const std::string& parent, |
| + const char* file_names[], |
| + uint32 file_names_size) { |
| + scoped_ptr<GDataDBIter> iter = gdata_db_->NewIterator(parent); |
| + for (uint i = 0; true; ++i) { |
| + scoped_ptr<GDataEntry> entry = iter->GetNext(); |
| + if (!entry.get()) { |
| + EXPECT_EQ(i, file_names_size); |
| + break; |
| + } |
| + ASSERT_LT(i, file_names_size); |
| + EXPECT_EQ(file_names[i], entry->file_name()); |
| + DVLOG(1) << "Iter " << entry->file_name(); |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +TEST_F(GDataDBTest, FileTest) { |
| + GDataFile file1(NULL, NULL); |
| + const std::string file_name1("/gdata/dir1/file1"); |
| + const std::string resource_id1("abcd"); |
| + const GURL content_url1("http://content/dir1/file1"); |
| + const std::string file_md51("ijkl"); |
| + file1.set_file_name(file_name1); |
| + file1.set_resource_id(resource_id1); |
| + file1.set_content_url(content_url1); |
| + file1.set_file_md5(file_md51); |
| + |
| + TestGetNotFound(file_name1, resource_id1); |
| + |
| + GDataDB::Status status = gdata_db_->Put(file1); |
| + EXPECT_EQ(GDataDB::DB_OK, status); |
| + |
| + TestGetFound(file_name1, resource_id1, content_url1); |
| + |
| + scoped_ptr<GDataEntry> entry; |
| + gdata_db_->GetByPath(file_name1, &entry); |
| + EXPECT_EQ(file_md51, entry->AsGDataFile()->file_md5()); |
| + EXPECT_FALSE(entry->AsGDataFile()->file_info().is_directory); |
| + |
| + status = gdata_db_->DeleteByPath(file_name1); |
| + EXPECT_EQ(GDataDB::DB_OK, status); |
| + |
| + TestGetNotFound(file_name1, resource_id1); |
| +} |
| + |
| +TEST_F(GDataDBTest, DirTest) { |
| + GDataDirectory dir1(NULL, NULL); |
| + const std::string dir_name1("/gdata/dir1"); |
| + const std::string dir_resource_id1("efgh"); |
| + const GURL dir_content_url1("http://content/dir1"); |
| + const GURL upload_url1("http://upload/dir1"); |
| + dir1.set_file_name(dir_name1); |
| + dir1.set_resource_id(dir_resource_id1); |
| + dir1.set_content_url(dir_content_url1); |
| + dir1.set_upload_url(upload_url1); |
| + |
| + TestGetNotFound(dir_name1, dir_resource_id1); |
| + |
| + GDataDB::Status status = gdata_db_->Put(dir1); |
| + EXPECT_EQ(GDataDB::DB_OK, status); |
| + |
| + TestGetFound(dir_name1, dir_resource_id1, dir_content_url1); |
| + |
| + scoped_ptr<GDataEntry> entry; |
| + gdata_db_->GetByPath(dir_name1, &entry); |
| + EXPECT_EQ(upload_url1, entry->AsGDataDirectory()->upload_url()); |
| + EXPECT_TRUE(entry->AsGDataDirectory()->file_info().is_directory); |
| + |
| + status = gdata_db_->DeleteByPath(dir_name1); |
| + EXPECT_EQ(GDataDB::DB_OK, status); |
| + |
| + TestGetNotFound(dir_name1, dir_resource_id1); |
| +} |
| + |
| +TEST_F(GDataDBTest, IterTest) { |
| + InitDB(); |
| + |
| + const char* dir1_children[] = { |
| + "/gdata/dir1", |
| + "/gdata/dir1/dir3", |
| + "/gdata/dir1/dir3/file10", |
| + "/gdata/dir1/dir3/file9", |
| + "/gdata/dir1/file4", |
| + "/gdata/dir1/file5", |
| + }; |
| + TestIter("/gdata/dir1", dir1_children, arraysize(dir1_children)); |
| + |
| + const char* dir2_children[] = { |
| + "/gdata/dir2", |
| + "/gdata/dir2/file6", |
| + "/gdata/dir2/file7", |
| + "/gdata/dir2/file8", |
| + }; |
| + TestIter("/gdata/dir2", dir2_children, arraysize(dir2_children)); |
| + |
| + const char* dir3_children[] = { |
| + "/gdata/dir1/dir3", |
| + "/gdata/dir1/dir3/file10", |
| + "/gdata/dir1/dir3/file9", |
| + }; |
| + TestIter("/gdata/dir1/dir3", dir3_children, arraysize(dir3_children)); |
| + |
| + const char* file10[] = { |
| + "/gdata/dir1/dir3/file10", |
| + }; |
| + TestIter(file10[0], file10, arraysize(file10)); |
| + |
| + const char* all_entries[] = { |
| + "/gdata/dir1", |
| + "/gdata/dir1/dir3", |
| + "/gdata/dir1/dir3/file10", |
| + "/gdata/dir1/dir3/file9", |
| + "/gdata/dir1/file4", |
| + "/gdata/dir1/file5", |
| + "/gdata/dir2", |
| + "/gdata/dir2/file6", |
| + "/gdata/dir2/file7", |
| + "/gdata/dir2/file8", |
| + }; |
| + TestIter("/", all_entries, arraysize(all_entries)); |
| + |
| + TestIter("/gdata/dir4", NULL, 0); |
| +} |
| + |
| +} // namespace gdata |
| Property changes on: chrome/browser/chromeos/gdata/gdata_db_unittest.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |