Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / extensions / browser / value_store / value_store_frontend_unittest.cc
blobbe65b7bf8b061550271853013a4daa57f8bda2f8
1 // Copyright 2014 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/files/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 "content/public/test/test_browser_thread.h"
11 #include "extensions/browser/value_store/value_store_frontend.h"
12 #include "extensions/common/extension_paths.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using content::BrowserThread;
17 const char kDatabaseUMAClientName[] = "Test";
19 class ValueStoreFrontendTest : public testing::Test {
20 public:
21 ValueStoreFrontendTest()
22 : ui_thread_(BrowserThread::UI, base::MessageLoop::current()),
23 file_thread_(BrowserThread::FILE, base::MessageLoop::current()) {
26 void SetUp() override {
27 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
29 base::FilePath test_data_dir;
30 ASSERT_TRUE(PathService::Get(extensions::DIR_TEST_DATA, &test_data_dir));
31 base::FilePath src_db(test_data_dir.AppendASCII("value_store_db"));
32 db_path_ = temp_dir_.path().AppendASCII("temp_db");
33 base::CopyDirectory(src_db, db_path_, true);
35 ResetStorage();
38 void TearDown() override {
39 base::MessageLoop::current()->RunUntilIdle(); // wait for storage to delete
40 storage_.reset();
43 // Reset the value store, reloading the DB from disk.
44 void ResetStorage() {
45 storage_.reset(new ValueStoreFrontend(kDatabaseUMAClientName, db_path_));
48 bool Get(const std::string& key, scoped_ptr<base::Value>* output) {
49 storage_->Get(key, base::Bind(&ValueStoreFrontendTest::GetAndWait,
50 base::Unretained(this), output));
51 base::MessageLoop::current()->Run(); // wait for GetAndWait
52 return !!output->get();
55 protected:
56 void GetAndWait(scoped_ptr<base::Value>* output,
57 scoped_ptr<base::Value> result) {
58 *output = result.Pass();
59 base::MessageLoop::current()->Quit();
62 scoped_ptr<ValueStoreFrontend> storage_;
63 base::ScopedTempDir temp_dir_;
64 base::FilePath db_path_;
65 base::MessageLoop message_loop_;
66 content::TestBrowserThread ui_thread_;
67 content::TestBrowserThread file_thread_;
70 TEST_F(ValueStoreFrontendTest, GetExistingData) {
71 scoped_ptr<base::Value> value;
72 ASSERT_FALSE(Get("key0", &value));
74 // Test existing keys in the DB.
76 ASSERT_TRUE(Get("key1", &value));
77 std::string result;
78 ASSERT_TRUE(value->GetAsString(&result));
79 EXPECT_EQ("value1", result);
83 ASSERT_TRUE(Get("key2", &value));
84 int result;
85 ASSERT_TRUE(value->GetAsInteger(&result));
86 EXPECT_EQ(2, result);
90 TEST_F(ValueStoreFrontendTest, ChangesPersistAfterReload) {
91 storage_->Set("key0", scoped_ptr<base::Value>(new base::FundamentalValue(0)));
92 storage_->Set("key1", scoped_ptr<base::Value>(new base::StringValue("new1")));
93 storage_->Remove("key2");
95 // Reload the DB and test our changes.
96 ResetStorage();
98 scoped_ptr<base::Value> value;
100 ASSERT_TRUE(Get("key0", &value));
101 int result;
102 ASSERT_TRUE(value->GetAsInteger(&result));
103 EXPECT_EQ(0, result);
107 ASSERT_TRUE(Get("key1", &value));
108 std::string result;
109 ASSERT_TRUE(value->GetAsString(&result));
110 EXPECT_EQ("new1", result);
113 ASSERT_FALSE(Get("key2", &value));