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 
5 #ifndef COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_MOCK_PREFETCH_ITEM_GENERATOR_H_
6 #define COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_MOCK_PREFETCH_ITEM_GENERATOR_H_
7 
8 #include <string>
9 
10 #include "components/offline_pages/core/prefetch/prefetch_item.h"
11 #include "components/offline_pages/core/prefetch/prefetch_types.h"
12 
13 namespace offline_pages {
14 
15 // Generator of PrefetchItem instances with all fields automatically
16 // pre-populated with values that are reasonable and unique (within the
17 // instance). To further customize returned items one can set custom prefixes or
18 // just change the actual values of returned instances.  When creating an item
19 // with a particular state, only the fields applicable to that state will be
20 // populated, and the rest will remain in their default state, even if prefixes
21 // are set for that data member.
22 class MockPrefetchItemGenerator {
23  public:
24   static const std::string kClientNamespace;
25   static const std::string kClientIdPrefix;
26   static const std::string kUrlPrefix;
27   static const std::string kFinalUrlPrefix;
28   static const std::string kOperationNamePrefix;
29   static const std::string kArchiveBodyNamePrefix;
30   static const std::string kTitlePrefix;
31   static const std::string kFilePathPrefix;
32 
33   MockPrefetchItemGenerator();
34   ~MockPrefetchItemGenerator();
35 
36   // Creates a new item using all set prefixes and an internal counter to set
37   // reasonable and unique values to all fields including the instance-unique
38   // offline ID.
39   PrefetchItem CreateItem(PrefetchItemState state);
40 
41   // Generates a unique offline ID within the context of this generator
42   // instance. Values will not be unique among different instances.
43   int64_t GenerateTestOfflineId();
44 
45   // Setters for all prefixes.
set_client_namespace(std::string client_namespace)46   void set_client_namespace(std::string client_namespace) {
47     client_namespace_ = client_namespace;
48   }
set_client_id_prefix(std::string client_id_prefix)49   void set_client_id_prefix(std::string client_id_prefix) {
50     client_id_prefix_ = client_id_prefix;
51   }
set_url_prefix(std::string url_prefix)52   void set_url_prefix(std::string url_prefix) { url_prefix_ = url_prefix; }
set_final_url_prefix(std::string final_url_prefix)53   void set_final_url_prefix(std::string final_url_prefix) {
54     final_url_prefix_ = final_url_prefix;
55   }
set_operation_name_prefix(std::string operation_name_prefix)56   void set_operation_name_prefix(std::string operation_name_prefix) {
57     operation_name_prefix_ = operation_name_prefix;
58   }
set_archive_body_name_prefix(std::string archive_body_name_prefix)59   void set_archive_body_name_prefix(std::string archive_body_name_prefix) {
60     archive_body_name_prefix_ = archive_body_name_prefix;
61   }
set_title_prefix(std::string title_prefix)62   void set_title_prefix(std::string title_prefix) {
63     title_prefix_ = title_prefix;
64   }
set_file_path_prefix(std::string file_path_prefix)65   void set_file_path_prefix(std::string file_path_prefix) {
66     file_path_prefix_ = file_path_prefix;
67   }
68 
69  private:
70   // These namespace name and prefixes must always be set.
71   std::string client_namespace_ = kClientNamespace;
72   std::string client_id_prefix_ = kClientIdPrefix;
73   std::string url_prefix_ = kUrlPrefix;
74 
75   // These prefixes, if custom set to the empty string, will cause related
76   // values not to be set.
77   std::string final_url_prefix_ = kFinalUrlPrefix;
78   std::string operation_name_prefix_ = kOperationNamePrefix;
79   std::string archive_body_name_prefix_ = kArchiveBodyNamePrefix;
80   std::string title_prefix_ = kTitlePrefix;
81   std::string file_path_prefix_ = kFilePathPrefix;
82 
83   // Test offline IDs start at an arbitrary, non-zero value to ease recognizing
84   // generated ID values among other integer values while debugging.
85   int64_t offline_id_counter_ = 1000;
86 };
87 
88 }  // namespace offline_pages
89 
90 #endif  // COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_MOCK_PREFETCH_ITEM_GENERATOR_H_
91