| 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "chrome/browser/value_store/caching_value_store.h" | 10 #include "chrome/browser/value_store/value_store_frontend.h" |
| 11 #include "chrome/common/chrome_paths.h" | 11 #include "chrome/common/chrome_paths.h" |
| 12 #include "content/public/test/test_browser_thread.h" | 12 #include "content/public/test/test_browser_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 using content::BrowserThread; | 15 using content::BrowserThread; |
| 16 | 16 |
| 17 // Test suite for CachingValueStore, using a test database with a few simple | 17 // Test suite for CachingValueStore, using a test database with a few simple |
| 18 // entries. | 18 // entries. |
| 19 class CachingValueStoreTest | 19 class ValueStoreFrontendTest : public testing::Test { |
| 20 : public testing::Test, | |
| 21 public CachingValueStore::Observer { | |
| 22 public: | 20 public: |
| 23 CachingValueStoreTest() | 21 ValueStoreFrontendTest() |
| 24 : ui_thread_(BrowserThread::UI, MessageLoop::current()), | 22 : ui_thread_(BrowserThread::UI, MessageLoop::current()), |
| 25 file_thread_(BrowserThread::FILE, MessageLoop::current()) { | 23 file_thread_(BrowserThread::FILE, MessageLoop::current()) { |
| 26 } | 24 } |
| 27 | 25 |
| 28 virtual void SetUp() { | 26 virtual void SetUp() { |
| 29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 27 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 30 | 28 |
| 31 FilePath test_data_dir; | 29 FilePath test_data_dir; |
| 32 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)); | 30 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)); |
| 33 FilePath src_db(test_data_dir.AppendASCII("value_store_db")); | 31 FilePath src_db(test_data_dir.AppendASCII("value_store_db")); |
| 34 db_path_ = temp_dir_.path().AppendASCII("temp_db"); | 32 db_path_ = temp_dir_.path().AppendASCII("temp_db"); |
| 35 file_util::CopyDirectory(src_db, db_path_, true); | 33 file_util::CopyDirectory(src_db, db_path_, true); |
| 36 | 34 |
| 37 ResetStorage(); | 35 ResetStorage(); |
| 38 } | 36 } |
| 39 | 37 |
| 40 virtual void TearDown() { | 38 virtual void TearDown() { |
| 41 MessageLoop::current()->RunAllPending(); // wait for storage to delete | 39 MessageLoop::current()->RunAllPending(); // wait for storage to delete |
| 42 storage_->RemoveObserver(this); | |
| 43 storage_.reset(); | 40 storage_.reset(); |
| 44 } | 41 } |
| 45 | 42 |
| 46 // CachingValueStore::Observer | |
| 47 virtual void OnInitializationComplete() { | |
| 48 MessageLoop::current()->Quit(); | |
| 49 } | |
| 50 | |
| 51 // Reset the value store, reloading the DB from disk. | 43 // Reset the value store, reloading the DB from disk. |
| 52 void ResetStorage() { | 44 void ResetStorage() { |
| 53 if (storage_.get()) | 45 storage_.reset(new ValueStoreFrontend(db_path_)); |
| 54 storage_->RemoveObserver(this); | 46 } |
| 55 storage_.reset(new CachingValueStore(db_path_)); | 47 |
| 56 storage_->AddObserver(this); | 48 bool Get(const std::string& key, scoped_ptr<base::Value>* output) { |
| 57 MessageLoop::current()->Run(); // wait for OnInitializationComplete | 49 storage_->Get(key, base::Bind(&ValueStoreFrontendTest::GetAndWait, |
| 50 base::Unretained(this), output)); |
| 51 MessageLoop::current()->Run(); // wait for GetAndWait |
| 52 return !!output->get(); |
| 58 } | 53 } |
| 59 | 54 |
| 60 protected: | 55 protected: |
| 61 scoped_ptr<CachingValueStore> storage_; | 56 void GetAndWait(scoped_ptr<base::Value>* output, |
| 57 scoped_ptr<base::Value> result) { |
| 58 *output = result.Pass(); |
| 59 MessageLoop::current()->Quit(); |
| 60 } |
| 61 |
| 62 scoped_ptr<ValueStoreFrontend> storage_; |
| 62 ScopedTempDir temp_dir_; | 63 ScopedTempDir temp_dir_; |
| 63 FilePath db_path_; | 64 FilePath db_path_; |
| 64 MessageLoop message_loop_; | 65 MessageLoop message_loop_; |
| 65 content::TestBrowserThread ui_thread_; | 66 content::TestBrowserThread ui_thread_; |
| 66 content::TestBrowserThread file_thread_; | 67 content::TestBrowserThread file_thread_; |
| 67 }; | 68 }; |
| 68 | 69 |
| 69 TEST_F(CachingValueStoreTest, GetExistingData) { | 70 TEST_F(ValueStoreFrontendTest, GetExistingData) { |
| 70 const base::Value* value = NULL; | 71 scoped_ptr<base::Value> value(NULL); |
| 71 ASSERT_FALSE(storage_->Get("key0", &value)); | 72 ASSERT_FALSE(Get("key0", &value)); |
| 72 | 73 |
| 73 // Test existing keys in the DB. | 74 // Test existing keys in the DB. |
| 74 { | 75 { |
| 75 ASSERT_TRUE(storage_->Get("key1", &value)); | 76 ASSERT_TRUE(Get("key1", &value)); |
| 76 std::string result; | 77 std::string result; |
| 77 ASSERT_TRUE(value->GetAsString(&result)); | 78 ASSERT_TRUE(value->GetAsString(&result)); |
| 78 EXPECT_EQ("value1", result); | 79 EXPECT_EQ("value1", result); |
| 79 } | 80 } |
| 80 | 81 |
| 81 { | 82 { |
| 82 ASSERT_TRUE(storage_->Get("key2", &value)); | 83 ASSERT_TRUE(Get("key2", &value)); |
| 83 int result; | 84 int result; |
| 84 ASSERT_TRUE(value->GetAsInteger(&result)); | 85 ASSERT_TRUE(value->GetAsInteger(&result)); |
| 85 EXPECT_EQ(2, result); | 86 EXPECT_EQ(2, result); |
| 86 } | 87 } |
| 87 } | 88 } |
| 88 | 89 |
| 89 TEST_F(CachingValueStoreTest, ChangesPersistAfterReload) { | 90 TEST_F(ValueStoreFrontendTest, ChangesPersistAfterReload) { |
| 90 storage_->Set("key0", base::Value::CreateIntegerValue(0)); | 91 storage_->Set("key0", |
| 91 storage_->Set("key1", base::Value::CreateStringValue("new1")); | 92 scoped_ptr<base::Value>(base::Value::CreateIntegerValue(0))); |
| 93 storage_->Set("key1", |
| 94 scoped_ptr<base::Value>(base::Value::CreateStringValue("new1"))); |
| 92 storage_->Remove("key2"); | 95 storage_->Remove("key2"); |
| 93 | 96 |
| 94 // Reload the DB and test our changes. | 97 // Reload the DB and test our changes. |
| 95 ResetStorage(); | 98 ResetStorage(); |
| 96 | 99 |
| 97 const base::Value* value = NULL; | 100 scoped_ptr<base::Value> value(NULL); |
| 98 { | 101 { |
| 99 ASSERT_TRUE(storage_->Get("key0", &value)); | 102 ASSERT_TRUE(Get("key0", &value)); |
| 100 int result; | 103 int result; |
| 101 ASSERT_TRUE(value->GetAsInteger(&result)); | 104 ASSERT_TRUE(value->GetAsInteger(&result)); |
| 102 EXPECT_EQ(0, result); | 105 EXPECT_EQ(0, result); |
| 103 } | 106 } |
| 104 | 107 |
| 105 { | 108 { |
| 106 ASSERT_TRUE(storage_->Get("key1", &value)); | 109 ASSERT_TRUE(Get("key1", &value)); |
| 107 std::string result; | 110 std::string result; |
| 108 ASSERT_TRUE(value->GetAsString(&result)); | 111 ASSERT_TRUE(value->GetAsString(&result)); |
| 109 EXPECT_EQ("new1", result); | 112 EXPECT_EQ("new1", result); |
| 110 } | 113 } |
| 111 | 114 |
| 112 ASSERT_FALSE(storage_->Get("key2", &value)); | 115 ASSERT_FALSE(Get("key2", &value)); |
| 113 } | 116 } |
| OLD | NEW |