offline_page_metadata_store_test_util.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include "components/offline_pages/core/offline_page_metadata_store_test_util.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/callback_helpers.h"
  9. #include "base/logging.h"
  10. #include "base/memory/ptr_util.h"
  11. #include "base/run_loop.h"
  12. #include "base/test/bind.h"
  13. #include "base/threading/thread_task_runner_handle.h"
  14. #include "components/offline_pages/core/model/add_page_task.h"
  15. #include "components/offline_pages/core/model/get_pages_task.h"
  16. #include "components/offline_pages/core/offline_page_types.h"
  17. #include "sql/database.h"
  18. #include "sql/statement.h"
  19. #include "testing/gtest/include/gtest/gtest.h"
  20. namespace offline_pages {
  21. namespace {
  22. int64_t GetPageCountSync(sql::Database* db) {
  23. static const char kSql[] = "SELECT count(*) FROM offlinepages_v1";
  24. sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql));
  25. if (statement.Step()) {
  26. return statement.ColumnInt64(0);
  27. }
  28. return 0UL;
  29. }
  30. } // namespace
  31. OfflinePageMetadataStoreTestUtil::OfflinePageMetadataStoreTestUtil()
  32. : store_ptr_(nullptr) {}
  33. OfflinePageMetadataStoreTestUtil::~OfflinePageMetadataStoreTestUtil() {}
  34. void OfflinePageMetadataStoreTestUtil::BuildStore() {
  35. if (!temp_directory_.IsValid() && !temp_directory_.CreateUniqueTempDir()) {
  36. DVLOG(1) << "temp_directory_ not created";
  37. return;
  38. }
  39. store_ = std::make_unique<OfflinePageMetadataStore>(
  40. base::ThreadTaskRunnerHandle::Get(), temp_directory_.GetPath());
  41. store_ptr_ = store_.get();
  42. }
  43. void OfflinePageMetadataStoreTestUtil::BuildStoreInMemory() {
  44. store_ = std::make_unique<OfflinePageMetadataStore>(
  45. base::ThreadTaskRunnerHandle::Get());
  46. store_ptr_ = store_.get();
  47. }
  48. void OfflinePageMetadataStoreTestUtil::DeleteStore() {
  49. store_.reset();
  50. store_ptr_ = nullptr;
  51. }
  52. std::unique_ptr<OfflinePageMetadataStore>
  53. OfflinePageMetadataStoreTestUtil::ReleaseStore() {
  54. return std::move(store_);
  55. }
  56. void OfflinePageMetadataStoreTestUtil::InsertItem(const OfflinePageItem& page) {
  57. base::RunLoop run_loop;
  58. AddPageResult result;
  59. auto task = std::make_unique<AddPageTask>(
  60. store(), page, base::BindLambdaForTesting([&](AddPageResult cb_result) {
  61. result = cb_result;
  62. run_loop.Quit();
  63. }));
  64. task->Execute(base::DoNothing());
  65. run_loop.Run();
  66. EXPECT_EQ(AddPageResult::SUCCESS, result);
  67. }
  68. int64_t OfflinePageMetadataStoreTestUtil::GetPageCount() {
  69. base::RunLoop run_loop;
  70. int64_t count = 0;
  71. store()->Execute(
  72. base::BindOnce(&GetPageCountSync),
  73. base::BindOnce(base::BindLambdaForTesting([&](int64_t cb_count) {
  74. count = cb_count;
  75. run_loop.Quit();
  76. })),
  77. int64_t());
  78. run_loop.Run();
  79. return count;
  80. }
  81. std::unique_ptr<OfflinePageItem>
  82. OfflinePageMetadataStoreTestUtil::GetPageByOfflineId(int64_t offline_id) {
  83. base::RunLoop run_loop;
  84. PageCriteria criteria;
  85. criteria.offline_ids = std::vector<int64_t>{offline_id};
  86. OfflinePageItem* page = nullptr;
  87. auto task = std::make_unique<GetPagesTask>(
  88. store(), criteria,
  89. base::BindOnce(base::BindLambdaForTesting(
  90. [&](const std::vector<OfflinePageItem>& cb_pages) {
  91. if (!cb_pages.empty())
  92. page = new OfflinePageItem(cb_pages[0]);
  93. run_loop.Quit();
  94. })));
  95. task->Execute(base::DoNothing());
  96. run_loop.Run();
  97. return base::WrapUnique<OfflinePageItem>(page);
  98. }
  99. } // namespace offline_pages