1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/value_store/value_store_frontend.h"
#include <utility>
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "content/public/test/test_browser_thread.h"
#include "extensions/browser/value_store/test_value_store_factory.h"
#include "extensions/common/extension_paths.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
class ValueStoreFrontendTest : public testing::Test {
public:
ValueStoreFrontendTest()
: ui_thread_(BrowserThread::UI, base::MessageLoop::current()),
file_thread_(BrowserThread::FILE, base::MessageLoop::current()) {
}
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
base::FilePath test_data_dir;
ASSERT_TRUE(PathService::Get(extensions::DIR_TEST_DATA, &test_data_dir));
base::FilePath src_db(test_data_dir.AppendASCII("value_store_db"));
db_path_ = temp_dir_.path().AppendASCII("temp_db");
base::CopyDirectory(src_db, db_path_, true);
factory_ = new extensions::TestValueStoreFactory(db_path_);
ResetStorage();
}
void TearDown() override {
base::MessageLoop::current()->RunUntilIdle(); // wait for storage to delete
storage_.reset();
}
// Reset the value store, reloading the DB from disk.
void ResetStorage() {
storage_.reset(new ValueStoreFrontend(
factory_, ValueStoreFrontend::BackendType::RULES));
}
bool Get(const std::string& key, scoped_ptr<base::Value>* output) {
storage_->Get(key, base::Bind(&ValueStoreFrontendTest::GetAndWait,
base::Unretained(this), output));
base::MessageLoop::current()->Run(); // wait for GetAndWait
return !!output->get();
}
protected:
void GetAndWait(scoped_ptr<base::Value>* output,
scoped_ptr<base::Value> result) {
*output = std::move(result);
base::MessageLoop::current()->QuitWhenIdle();
}
scoped_refptr<extensions::TestValueStoreFactory> factory_;
scoped_ptr<ValueStoreFrontend> storage_;
base::ScopedTempDir temp_dir_;
base::FilePath db_path_;
base::MessageLoop message_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread file_thread_;
};
TEST_F(ValueStoreFrontendTest, GetExistingData) {
scoped_ptr<base::Value> value;
ASSERT_FALSE(Get("key0", &value));
// Test existing keys in the DB.
{
ASSERT_TRUE(Get("key1", &value));
std::string result;
ASSERT_TRUE(value->GetAsString(&result));
EXPECT_EQ("value1", result);
}
{
ASSERT_TRUE(Get("key2", &value));
int result;
ASSERT_TRUE(value->GetAsInteger(&result));
EXPECT_EQ(2, result);
}
}
TEST_F(ValueStoreFrontendTest, ChangesPersistAfterReload) {
storage_->Set("key0", scoped_ptr<base::Value>(new base::FundamentalValue(0)));
storage_->Set("key1", scoped_ptr<base::Value>(new base::StringValue("new1")));
storage_->Remove("key2");
// Reload the DB and test our changes.
ResetStorage();
scoped_ptr<base::Value> value;
{
ASSERT_TRUE(Get("key0", &value));
int result;
ASSERT_TRUE(value->GetAsInteger(&result));
EXPECT_EQ(0, result);
}
{
ASSERT_TRUE(Get("key1", &value));
std::string result;
ASSERT_TRUE(value->GetAsString(&result));
EXPECT_EQ("new1", result);
}
ASSERT_FALSE(Get("key2", &value));
}
|