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.
5 #include "base/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/path_service.h"
10 #include "chrome/browser/value_store/value_store_frontend.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"
15 using content::BrowserThread
;
17 class ValueStoreFrontendTest
: public testing::Test
{
19 ValueStoreFrontendTest()
20 : ui_thread_(BrowserThread::UI
, base::MessageLoop::current()),
21 file_thread_(BrowserThread::FILE, base::MessageLoop::current()) {
24 virtual void SetUp() {
25 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
27 base::FilePath test_data_dir
;
28 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
));
29 base::FilePath
src_db(test_data_dir
.AppendASCII("value_store_db"));
30 db_path_
= temp_dir_
.path().AppendASCII("temp_db");
31 base::CopyDirectory(src_db
, db_path_
, true);
36 virtual void TearDown() {
37 base::MessageLoop::current()->RunUntilIdle(); // wait for storage to delete
41 // Reset the value store, reloading the DB from disk.
43 storage_
.reset(new ValueStoreFrontend(db_path_
));
46 bool Get(const std::string
& key
, scoped_ptr
<base::Value
>* output
) {
47 storage_
->Get(key
, base::Bind(&ValueStoreFrontendTest::GetAndWait
,
48 base::Unretained(this), output
));
49 base::MessageLoop::current()->Run(); // wait for GetAndWait
50 return !!output
->get();
54 void GetAndWait(scoped_ptr
<base::Value
>* output
,
55 scoped_ptr
<base::Value
> result
) {
56 *output
= result
.Pass();
57 base::MessageLoop::current()->Quit();
60 scoped_ptr
<ValueStoreFrontend
> storage_
;
61 base::ScopedTempDir temp_dir_
;
62 base::FilePath db_path_
;
63 base::MessageLoop message_loop_
;
64 content::TestBrowserThread ui_thread_
;
65 content::TestBrowserThread file_thread_
;
68 TEST_F(ValueStoreFrontendTest
, GetExistingData
) {
69 scoped_ptr
<base::Value
> value
;
70 ASSERT_FALSE(Get("key0", &value
));
72 // Test existing keys in the DB.
74 ASSERT_TRUE(Get("key1", &value
));
76 ASSERT_TRUE(value
->GetAsString(&result
));
77 EXPECT_EQ("value1", result
);
81 ASSERT_TRUE(Get("key2", &value
));
83 ASSERT_TRUE(value
->GetAsInteger(&result
));
88 TEST_F(ValueStoreFrontendTest
, ChangesPersistAfterReload
) {
90 scoped_ptr
<base::Value
>(base::Value::CreateIntegerValue(0)));
92 scoped_ptr
<base::Value
>(base::Value::CreateStringValue("new1")));
93 storage_
->Remove("key2");
95 // Reload the DB and test our changes.
98 scoped_ptr
<base::Value
> value
;
100 ASSERT_TRUE(Get("key0", &value
));
102 ASSERT_TRUE(value
->GetAsInteger(&result
));
103 EXPECT_EQ(0, result
);
107 ASSERT_TRUE(Get("key1", &value
));
109 ASSERT_TRUE(value
->GetAsString(&result
));
110 EXPECT_EQ("new1", result
);
113 ASSERT_FALSE(Get("key2", &value
));