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

Unified Diff: chrome/browser/history/in_memory_url_index_unittest.cc

Issue 9030031: Move InMemoryURLIndex Caching Operations to FILE Thread (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Syncing with hopes of pleasing trybot update Created 8 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/history/in_memory_url_index.cc ('k') | chrome/browser/history/url_index_private_data.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/history/in_memory_url_index_unittest.cc
===================================================================
--- chrome/browser/history/in_memory_url_index_unittest.cc (revision 126922)
+++ chrome/browser/history/in_memory_url_index_unittest.cc (working copy)
@@ -9,6 +9,7 @@
#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/path_service.h"
+#include "base/scoped_temp_dir.h"
#include "base/string_util.h"
#include "base/string16.h"
#include "base/utf_string_conversions.h"
@@ -25,6 +26,8 @@
#include "sql/transaction.h"
#include "testing/gtest/include/gtest/gtest.h"
+using content::BrowserThread;
+
// The test version of the history url database table ('url') is contained in
// a database file created from a text file('url_history_provider_test.db.txt').
// The only difference between this table and a live 'urls' table from a
@@ -38,6 +41,54 @@
namespace history {
+// -----------------------------------------------------------------------------
+
+// Observer class so the unit tests can wait while the cache is being saved.
+class CacheFileSaverObserver : public InMemoryURLIndex::SaveCacheObserver {
+ public:
+ explicit CacheFileSaverObserver(MessageLoop* loop);
+ virtual void OnCacheSaveFinished(bool succeeded) OVERRIDE;
+
+ MessageLoop* loop_;
+ bool succeeded_;
+ DISALLOW_COPY_AND_ASSIGN(CacheFileSaverObserver);
+};
+
+CacheFileSaverObserver::CacheFileSaverObserver(MessageLoop* loop)
+ : loop_(loop),
+ succeeded_(false) {
+ DCHECK(loop);
+}
+
+void CacheFileSaverObserver::OnCacheSaveFinished(bool succeeded) {
+ succeeded_ = succeeded;
+ loop_->Quit();
+}
+
+// Observer class so the unit tests can wait while the cache is being restored.
+class CacheFileReaderObserver : public InMemoryURLIndex::RestoreCacheObserver {
+ public:
+ explicit CacheFileReaderObserver(MessageLoop* loop);
+ virtual void OnCacheRestoreFinished(bool succeeded) OVERRIDE;
+
+ MessageLoop* loop_;
+ bool succeeded_;
+ DISALLOW_COPY_AND_ASSIGN(CacheFileReaderObserver);
+};
+
+CacheFileReaderObserver::CacheFileReaderObserver(MessageLoop* loop)
+ : loop_(loop),
+ succeeded_(false) {
+ DCHECK(loop);
+}
+
+void CacheFileReaderObserver::OnCacheRestoreFinished(bool succeeded) {
+ succeeded_ = succeeded;
+ loop_->Quit();
+}
+
+// -----------------------------------------------------------------------------
+
class InMemoryURLIndexTest : public testing::Test {
public:
InMemoryURLIndexTest();
@@ -73,9 +124,14 @@
// Pass-through functions to simplify our friendship with InMemoryURLIndex.
URLIndexPrivateData* GetPrivateData() const;
+ void ClearPrivateData();
+ void set_history_dir(const FilePath& dir_path);
bool GetCacheFilePath(FilePath* file_path) const;
- void ClearHistoryDir() const;
+ void PostRestoreFromCacheFileTask();
+ void PostSaveToCacheFileTask();
+ const std::set<std::string>& scheme_whitelist();
+
// Pass-through functions to simplify our friendship with URLIndexPrivateData.
bool UpdateURL(const URLRow& row);
bool DeleteURL(const GURL& url);
@@ -105,25 +161,42 @@
}
URLIndexPrivateData* InMemoryURLIndexTest::GetPrivateData() const {
- DCHECK(url_index_->private_data_.get());
- return url_index_->private_data_.get();
+ DCHECK(url_index_->private_data());
+ return url_index_->private_data();
}
+void InMemoryURLIndexTest::ClearPrivateData() {
+ return url_index_->ClearPrivateData();
+}
+
+void InMemoryURLIndexTest::set_history_dir(const FilePath& dir_path) {
+ return url_index_->set_history_dir(dir_path);
+}
+
bool InMemoryURLIndexTest::GetCacheFilePath(FilePath* file_path) const {
DCHECK(file_path);
return url_index_->GetCacheFilePath(file_path);
}
-void InMemoryURLIndexTest::ClearHistoryDir() const {
- url_index_->history_dir_.clear();
+void InMemoryURLIndexTest::PostRestoreFromCacheFileTask() {
+ url_index_->PostRestoreFromCacheFileTask();
}
+void InMemoryURLIndexTest::PostSaveToCacheFileTask() {
+ url_index_->PostSaveToCacheFileTask();
+}
+
+const std::set<std::string>& InMemoryURLIndexTest::scheme_whitelist() {
+ return url_index_->scheme_whitelist();
+}
+
bool InMemoryURLIndexTest::UpdateURL(const URLRow& row) {
- return url_index_->private_data_->UpdateURL(row);
+ return GetPrivateData()->UpdateURL(row, url_index_->languages_,
+ url_index_->scheme_whitelist_);
}
bool InMemoryURLIndexTest::DeleteURL(const GURL& url) {
- return url_index_->private_data_->DeleteURL(url);
+ return GetPrivateData()->DeleteURL(url);
}
void InMemoryURLIndexTest::SetUp() {
@@ -359,8 +432,7 @@
TEST_F(LimitedInMemoryURLIndexTest, Initialization) {
// Verify that the database contains the expected number of items, which
// is the pre-filtered count, i.e. all of the items.
- sql::Connection& db(GetDB());
- sql::Statement statement(db.GetUniqueStatement("SELECT * FROM urls;"));
+ sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;"));
ASSERT_TRUE(statement.is_valid());
uint64 row_count = 0;
while (statement.Step()) ++row_count;
@@ -369,7 +441,7 @@
new InMemoryURLIndex(&profile_, FilePath(), "en,ja,hi,zh"));
url_index_->Init();
url_index_->RebuildFromHistory(history_database_);
- URLIndexPrivateData& private_data(*(url_index_->private_data_));
+ URLIndexPrivateData& private_data(*GetPrivateData());
// history_info_map_ should have the same number of items as were filtered.
EXPECT_EQ(1U, private_data.history_info_map_.size());
@@ -838,73 +910,95 @@
};
URLIndexPrivateData& private_data(*GetPrivateData());
+ const std::set<std::string>& whitelist(scheme_whitelist());
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
GURL url(data[i].url_spec);
EXPECT_EQ(data[i].expected_is_whitelisted,
- private_data.URLSchemeIsWhitelisted(url));
+ private_data.URLSchemeIsWhitelisted(url, whitelist));
}
}
TEST_F(InMemoryURLIndexTest, CacheSaveRestore) {
- // Part 1: Save the cache to a protobuf, restore it, and compare the results.
- in_memory_url_index::InMemoryURLIndexCacheItem index_cache;
- URLIndexPrivateData& expected(*GetPrivateData());
+ ScopedTempDir temp_directory;
+ ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
+ set_history_dir(temp_directory.path());
- // Capture our private data so we can later compare for equality.
- URLIndexPrivateData actual(expected);
+ URLIndexPrivateData& private_data(*GetPrivateData());
- actual.SavePrivateData(&index_cache);
+ // Ensure that there is really something there to be saved.
+ EXPECT_FALSE(private_data.word_list_.empty());
+ // available_words_ will already be empty since we have freshly built the
+ // data set for this test.
+ EXPECT_TRUE(private_data.available_words_.empty());
+ EXPECT_FALSE(private_data.word_map_.empty());
+ EXPECT_FALSE(private_data.char_word_map_.empty());
+ EXPECT_FALSE(private_data.word_id_history_map_.empty());
+ EXPECT_FALSE(private_data.history_id_word_map_.empty());
+ EXPECT_FALSE(private_data.history_info_map_.empty());
- // Version check: Make sure this version actually has the word starts.
- EXPECT_TRUE(index_cache.has_word_starts_map());
+ // Capture the current private data for later comparison to restored data.
+ scoped_refptr<URLIndexPrivateData> old_data(private_data.Duplicate());
- // Save the size of the resulting cache for later versioning comparison.
- std::string data;
- EXPECT_TRUE(index_cache.SerializeToString(&data));
- size_t current_version_cache_size = data.size();
+ // Save then restore our private data.
+ CacheFileSaverObserver save_observer(&message_loop_);
+ url_index_->set_save_cache_observer(&save_observer);
+ PostSaveToCacheFileTask();
+ message_loop_.Run();
+ EXPECT_TRUE(save_observer.succeeded_);
- // Prove that there is really something there.
- ExpectPrivateDataNotEmpty(actual);
+ // Clear and then prove it's clear before restoring.
+ ClearPrivateData();
+ EXPECT_TRUE(private_data.word_list_.empty());
+ EXPECT_TRUE(private_data.available_words_.empty());
+ EXPECT_TRUE(private_data.word_map_.empty());
+ EXPECT_TRUE(private_data.char_word_map_.empty());
+ EXPECT_TRUE(private_data.word_id_history_map_.empty());
+ EXPECT_TRUE(private_data.history_id_word_map_.empty());
+ EXPECT_TRUE(private_data.history_info_map_.empty());
- // Clear and then prove it's clear.
- actual.Clear();
- ExpectPrivateDataEmpty(actual);
+ CacheFileReaderObserver read_observer(&message_loop_);
+ url_index_->set_restore_cache_observer(&read_observer);
+ PostRestoreFromCacheFileTask();
+ message_loop_.Run();
+ EXPECT_TRUE(read_observer.succeeded_);
- // Restore the cache.
- EXPECT_TRUE(actual.RestorePrivateData(index_cache));
- EXPECT_EQ(kCurrentCacheFileVersion, actual.restored_cache_version_);
+ URLIndexPrivateData& new_data(*GetPrivateData());
- // Compare the restored and expected for equality.
- ExpectPrivateDataEqual(expected, actual);
+ // Compare the captured and restored for equality.
+ EXPECT_EQ(old_data->word_list_.size(), new_data.word_list_.size());
+ EXPECT_EQ(old_data->word_map_.size(), new_data.word_map_.size());
+ EXPECT_EQ(old_data->char_word_map_.size(), new_data.char_word_map_.size());
+ EXPECT_EQ(old_data->word_id_history_map_.size(),
+ new_data.word_id_history_map_.size());
+ EXPECT_EQ(old_data->history_id_word_map_.size(),
+ new_data.history_id_word_map_.size());
+ EXPECT_EQ(old_data->history_info_map_.size(),
+ new_data.history_info_map_.size());
+ // WordList must be index-by-index equal.
+ size_t count = old_data->word_list_.size();
+ for (size_t i = 0; i < count; ++i)
+ EXPECT_EQ(old_data->word_list_[i], new_data.word_list_[i]);
- // Part 2: Save an older version of the cache, restore it, and verify that the
- // reversioned portions are as expected.
- URLIndexPrivateData older(expected);
- in_memory_url_index::InMemoryURLIndexCacheItem older_cache;
- older.set_saved_cache_version(0);
- older.SavePrivateData(&older_cache);
+ ExpectMapOfContainersIdentical(old_data->char_word_map_,
+ new_data.char_word_map_);
+ ExpectMapOfContainersIdentical(old_data->word_id_history_map_,
+ new_data.word_id_history_map_);
+ ExpectMapOfContainersIdentical(old_data->history_id_word_map_,
+ new_data.history_id_word_map_);
- // Version check: Make sure this version does not have the word starts.
- EXPECT_FALSE(older_cache.has_word_starts_map());
-
- // Since we shouldn't have saved the word starts information for the version
- // 0 save immediately above, the cache should be a bit smaller.
- std::string older_data;
- EXPECT_TRUE(older_cache.SerializeToString(&older_data));
- size_t old_version_file_size = older_data.size();
- EXPECT_LT(old_version_file_size, current_version_cache_size);
- EXPECT_NE(data, older_data);
-
- // Clear and then prove it's clear.
- older.Clear();
- ExpectPrivateDataEmpty(older);
-
- // Restore the cache.
- EXPECT_TRUE(older.RestorePrivateData(older_cache));
- EXPECT_EQ(0, older.restored_cache_version_);
-
- // Compare the restored and expected for equality.
- ExpectPrivateDataEqual(expected, older);
+ for (HistoryInfoMap::const_iterator expected =
+ old_data->history_info_map_.begin();
+ expected != old_data->history_info_map_.end(); ++expected) {
+ HistoryInfoMap::const_iterator actual =
+ new_data.history_info_map_.find(expected->first);
+ ASSERT_FALSE(new_data.history_info_map_.end() == actual);
+ const URLRow& expected_row(expected->second);
+ const URLRow& actual_row(actual->second);
+ EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count());
+ EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count());
+ EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit());
+ EXPECT_EQ(expected_row.url(), actual_row.url());
+ }
}
class InMemoryURLIndexCacheTest : public testing::Test {
@@ -914,6 +1008,10 @@
protected:
virtual void SetUp() OVERRIDE;
+ // Pass-through functions to simplify our friendship with InMemoryURLIndex.
+ void set_history_dir(const FilePath& dir_path);
+ bool GetCacheFilePath(FilePath* file_path) const;
+
ScopedTempDir temp_dir_;
scoped_ptr<InMemoryURLIndex> url_index_;
};
@@ -925,13 +1023,22 @@
new InMemoryURLIndex(NULL, path, "en,ja,hi,zh"));
}
+void InMemoryURLIndexCacheTest::set_history_dir(const FilePath& dir_path) {
+ return url_index_->set_history_dir(dir_path);
+}
+
+bool InMemoryURLIndexCacheTest::GetCacheFilePath(FilePath* file_path) const {
+ DCHECK(file_path);
+ return url_index_->GetCacheFilePath(file_path);
+}
+
TEST_F(InMemoryURLIndexCacheTest, CacheFilePath) {
FilePath expectedPath =
temp_dir_.path().Append(FILE_PATH_LITERAL("History Provider Cache"));
std::vector<FilePath::StringType> expected_parts;
expectedPath.GetComponents(&expected_parts);
FilePath full_file_path;
- ASSERT_TRUE(url_index_->GetCacheFilePath(&full_file_path));
+ ASSERT_TRUE(GetCacheFilePath(&full_file_path));
std::vector<FilePath::StringType> actual_parts;
full_file_path.GetComponents(&actual_parts);
ASSERT_EQ(expected_parts.size(), actual_parts.size());
@@ -939,7 +1046,7 @@
for (size_t i = 0; i < count; ++i)
EXPECT_EQ(expected_parts[i], actual_parts[i]);
// Must clear the history_dir_ to satisfy the dtor's DCHECK.
- url_index_->history_dir_.clear();
+ set_history_dir(FilePath());
}
} // namespace history
« no previous file with comments | « chrome/browser/history/in_memory_url_index.cc ('k') | chrome/browser/history/url_index_private_data.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698