summaryrefslogtreecommitdiff
path: root/chromium/components/offline_pages/core/offline_page_metadata_store_test_util.h
blob: 4bffad5a861c6ec1b5033817e8222931f43f6a21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_PAGE_METADATA_STORE_TEST_UTIL_H_
#define COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_PAGE_METADATA_STORE_TEST_UTIL_H_

#include <memory>
#include <vector>

#include "base/files/scoped_temp_dir.h"
#include "base/macros.h"
#include "base/test/simple_test_clock.h"
#include "base/test/test_mock_time_task_runner.h"
#include "components/offline_pages/core/offline_page_metadata_store.h"

namespace base {
class ScopedTempDir;
class SimpleTestClock;
}  // namespace base

namespace offline_pages {

// Encapsulates the OfflinePageMetadataStore and provides synchronous
// operations on the store, for test writing convenience.
class OfflinePageMetadataStoreTestUtil {
 public:
  explicit OfflinePageMetadataStoreTestUtil(
      scoped_refptr<base::TestMockTimeTaskRunner> task_runner);
  ~OfflinePageMetadataStoreTestUtil();

  // Builds a new store in a temporary directory.
  void BuildStore();
  // Builds the store in memory (no disk storage).
  void BuildStoreInMemory();
  // Releases the ownership of currently controlled store. But still keeps a raw
  // pointer to the previously owned store in |store_ptr|, until the next time
  // BuildStore*() is called.
  std::unique_ptr<OfflinePageMetadataStore> ReleaseStore();
  // Deletes the currently held store that was previously built.
  void DeleteStore();

  // Inserts an offline page item into the store.
  void InsertItem(const OfflinePageItem& page_to_insert);

  // Gets the total number of pages in the store.
  int64_t GetPageCount();

  // Gets offline page by offline_id.
  std::unique_ptr<OfflinePageItem> GetPageByOfflineId(int64_t offline_id);

  OfflinePageMetadataStore* store() { return store_ptr_; }

  base::SimpleTestClock* clock() { return &clock_; }

 private:
  void RunUntilIdle();

  scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
  base::ScopedTempDir temp_directory_;
  // TODO(romax): Refactor the test util along with the similar one used in
  // Prefetching, to remove the ownership to the store. And clean up related
  // usage of |store_ptr_|.
  std::unique_ptr<OfflinePageMetadataStore> store_;
  OfflinePageMetadataStore* store_ptr_;
  base::SimpleTestClock clock_;

  DISALLOW_COPY_AND_ASSIGN(OfflinePageMetadataStoreTestUtil);
};

}  // namespace offline_pages

#endif  // COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_PAGE_METADATA_STORE_TEST_UTIL_H_