value_store_frontend_unittest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.
  4. #include "components/value_store/value_store_frontend.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/files/file_util.h"
  9. #include "base/files/scoped_temp_dir.h"
  10. #include "base/path_service.h"
  11. #include "base/test/task_environment.h"
  12. #include "base/threading/thread_task_runner_handle.h"
  13. #include "components/value_store/test_value_store_factory.h"
  14. #include "components/value_store/value_store_task_runner.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace value_store {
  17. class ValueStoreFrontendTest : public testing::Test {
  18. public:
  19. ValueStoreFrontendTest() {}
  20. void SetUp() override {
  21. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  22. base::FilePath test_data_dir;
  23. ASSERT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir));
  24. base::FilePath src_db(
  25. test_data_dir.AppendASCII("components/test/data/value_store"));
  26. db_path_ = temp_dir_.GetPath().AppendASCII("temp_db");
  27. base::CopyDirectory(src_db, db_path_, true);
  28. factory_ = new TestValueStoreFactory(db_path_);
  29. ResetStorage();
  30. }
  31. void TearDown() override {
  32. RunUntilIdle();
  33. storage_.reset();
  34. }
  35. // Reset the value store, reloading the DB from disk.
  36. void ResetStorage() {
  37. storage_ = std::make_unique<ValueStoreFrontend>(
  38. factory_, base::FilePath(FILE_PATH_LITERAL("Test dir")),
  39. "test_uma_name", base::ThreadTaskRunnerHandle::Get(),
  40. value_store::GetValueStoreTaskRunner());
  41. }
  42. bool Get(const std::string& key, std::unique_ptr<base::Value>* output) {
  43. storage_->Get(key, base::BindOnce(&ValueStoreFrontendTest::GetAndWait,
  44. base::Unretained(this), output));
  45. RunUntilIdle();
  46. return !!output->get();
  47. }
  48. protected:
  49. void RunUntilIdle() { task_environment_.RunUntilIdle(); }
  50. void GetAndWait(std::unique_ptr<base::Value>* output,
  51. std::unique_ptr<base::Value> result) {
  52. *output = std::move(result);
  53. }
  54. base::test::TaskEnvironment task_environment_;
  55. scoped_refptr<TestValueStoreFactory> factory_;
  56. std::unique_ptr<ValueStoreFrontend> storage_;
  57. base::ScopedTempDir temp_dir_;
  58. base::FilePath db_path_;
  59. };
  60. TEST_F(ValueStoreFrontendTest, GetExistingData) {
  61. std::unique_ptr<base::Value> value;
  62. ASSERT_FALSE(Get("key0", &value));
  63. // Test existing keys in the DB.
  64. {
  65. ASSERT_TRUE(Get("key1", &value));
  66. ASSERT_TRUE(value->is_string());
  67. EXPECT_EQ("value1", value->GetString());
  68. }
  69. {
  70. ASSERT_TRUE(Get("key2", &value));
  71. ASSERT_TRUE(value->is_int());
  72. EXPECT_EQ(2, value->GetInt());
  73. }
  74. }
  75. TEST_F(ValueStoreFrontendTest, ChangesPersistAfterReload) {
  76. storage_->Set("key0", std::make_unique<base::Value>(0));
  77. storage_->Set("key1", std::make_unique<base::Value>("new1"));
  78. storage_->Remove("key2");
  79. // Reload the DB and test our changes.
  80. ResetStorage();
  81. std::unique_ptr<base::Value> value;
  82. {
  83. ASSERT_TRUE(Get("key0", &value));
  84. ASSERT_TRUE(value->is_int());
  85. EXPECT_EQ(0, value->GetInt());
  86. }
  87. {
  88. ASSERT_TRUE(Get("key1", &value));
  89. ASSERT_TRUE(value->is_string());
  90. EXPECT_EQ("new1", value->GetString());
  91. }
  92. ASSERT_FALSE(Get("key2", &value));
  93. }
  94. } // namespace value_store