offline_page_metadata_store_test_util.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2017 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. #ifndef COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_PAGE_METADATA_STORE_TEST_UTIL_H_
  5. #define COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_PAGE_METADATA_STORE_TEST_UTIL_H_
  6. #include <memory>
  7. #include "base/files/scoped_temp_dir.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/test/simple_test_clock.h"
  10. #include "base/test/test_mock_time_task_runner.h"
  11. #include "components/offline_pages/core/offline_page_metadata_store.h"
  12. namespace base {
  13. class ScopedTempDir;
  14. class SimpleTestClock;
  15. } // namespace base
  16. namespace offline_pages {
  17. // Encapsulates the OfflinePageMetadataStore and provides synchronous
  18. // operations on the store, for test writing convenience.
  19. class OfflinePageMetadataStoreTestUtil {
  20. public:
  21. OfflinePageMetadataStoreTestUtil();
  22. OfflinePageMetadataStoreTestUtil(const OfflinePageMetadataStoreTestUtil&) =
  23. delete;
  24. OfflinePageMetadataStoreTestUtil& operator=(
  25. const OfflinePageMetadataStoreTestUtil&) = delete;
  26. ~OfflinePageMetadataStoreTestUtil();
  27. // Builds a new store in a temporary directory.
  28. void BuildStore();
  29. // Builds the store in memory (no disk storage).
  30. void BuildStoreInMemory();
  31. // Releases the ownership of currently controlled store. But still keeps a raw
  32. // pointer to the previously owned store in |store_ptr|, until the next time
  33. // BuildStore*() is called.
  34. std::unique_ptr<OfflinePageMetadataStore> ReleaseStore();
  35. // Deletes the currently held store that was previously built.
  36. void DeleteStore();
  37. // Inserts an offline page item into the store.
  38. void InsertItem(const OfflinePageItem& page_to_insert);
  39. // Gets the total number of pages in the store.
  40. int64_t GetPageCount();
  41. // Gets offline page by offline_id.
  42. std::unique_ptr<OfflinePageItem> GetPageByOfflineId(int64_t offline_id);
  43. OfflinePageMetadataStore* store() { return store_ptr_; }
  44. base::SimpleTestClock* clock() { return &clock_; }
  45. private:
  46. base::ScopedTempDir temp_directory_;
  47. // TODO(romax): Refactor the test util along with the similar one used in
  48. // Prefetching, to remove the ownership to the store. And clean up related
  49. // usage of |store_ptr_|.
  50. std::unique_ptr<OfflinePageMetadataStore> store_;
  51. raw_ptr<OfflinePageMetadataStore> store_ptr_;
  52. base::SimpleTestClock clock_;
  53. };
  54. } // namespace offline_pages
  55. #endif // COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_PAGE_METADATA_STORE_TEST_UTIL_H_