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