test_value_store_factory.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2016 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/test_value_store_factory.h"
  5. #include "base/containers/contains.h"
  6. #include "base/memory/ptr_util.h"
  7. #include "components/value_store/leveldb_value_store.h"
  8. #include "components/value_store/testing_value_store.h"
  9. namespace {
  10. const char kUMAClientName[] = "Test";
  11. } // namespace
  12. namespace value_store {
  13. TestValueStoreFactory::TestValueStoreFactory() = default;
  14. TestValueStoreFactory::TestValueStoreFactory(const base::FilePath& db_path)
  15. : db_path_(db_path) {}
  16. TestValueStoreFactory::~TestValueStoreFactory() = default;
  17. std::unique_ptr<ValueStore> TestValueStoreFactory::CreateValueStore(
  18. const base::FilePath& directory,
  19. const std::string& uma_client_name) {
  20. std::unique_ptr<ValueStore> value_store(CreateStore());
  21. // This factory is purposely keeping the raw pointers to each ValueStore
  22. // created. Tests using TestValueStoreFactory must be careful to keep
  23. // those ValueStore's alive for the duration of their test.
  24. value_store_map_[directory] = value_store.get();
  25. return value_store;
  26. }
  27. ValueStore* TestValueStoreFactory::LastCreatedStore() const {
  28. return last_created_store_;
  29. }
  30. void TestValueStoreFactory::DeleteValueStore(const base::FilePath& directory) {
  31. value_store_map_.erase(directory);
  32. }
  33. bool TestValueStoreFactory::HasValueStore(const base::FilePath& directory) {
  34. return base::Contains(value_store_map_, directory);
  35. }
  36. ValueStore* TestValueStoreFactory::GetExisting(
  37. const base::FilePath& directory) const {
  38. auto it = value_store_map_.find(directory);
  39. DCHECK(it != value_store_map_.end());
  40. return it->second;
  41. }
  42. void TestValueStoreFactory::Reset() {
  43. last_created_store_ = nullptr;
  44. value_store_map_.clear();
  45. }
  46. std::unique_ptr<ValueStore> TestValueStoreFactory::CreateStore() {
  47. std::unique_ptr<ValueStore> store;
  48. if (db_path_.empty())
  49. store = std::make_unique<TestingValueStore>();
  50. else
  51. store = std::make_unique<LeveldbValueStore>(kUMAClientName, db_path_);
  52. last_created_store_ = store.get();
  53. return store;
  54. }
  55. } // namespace value_store