OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef IOS_CHROME_BROWSER_BROWSER_STATE_TEST_CHROME_BROWSER_STATE_H_ | 5 #ifndef IOS_CHROME_BROWSER_BROWSER_STATE_TEST_CHROME_BROWSER_STATE_H_ |
6 #define IOS_CHROME_BROWSER_BROWSER_STATE_TEST_CHROME_BROWSER_STATE_H_ | 6 #define IOS_CHROME_BROWSER_BROWSER_STATE_TEST_CHROME_BROWSER_STATE_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "components/keyed_service/ios/browser_state_keyed_service_factory.h" | 13 #include "components/keyed_service/ios/browser_state_keyed_service_factory.h" |
14 #include "components/keyed_service/ios/refcounted_browser_state_keyed_service_fa
ctory.h" | 14 #include "components/keyed_service/ios/refcounted_browser_state_keyed_service_fa
ctory.h" |
15 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 15 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
16 #include "ios/chrome/browser/net/net_types.h" | 16 #include "ios/chrome/browser/net/net_types.h" |
17 | 17 |
18 namespace syncable_prefs { | 18 namespace sync_preferences { |
19 class PrefServiceSyncable; | 19 class PrefServiceSyncable; |
20 class TestingPrefServiceSyncable; | 20 class TestingPrefServiceSyncable; |
21 } | 21 } |
22 | 22 |
23 // This class is the implementation of ChromeBrowserState used for testing. | 23 // This class is the implementation of ChromeBrowserState used for testing. |
24 class TestChromeBrowserState : public ios::ChromeBrowserState { | 24 class TestChromeBrowserState : public ios::ChromeBrowserState { |
25 public: | 25 public: |
26 typedef std::vector< | 26 typedef std::vector< |
27 std::pair<BrowserStateKeyedServiceFactory*, | 27 std::pair<BrowserStateKeyedServiceFactory*, |
28 BrowserStateKeyedServiceFactory::TestingFactoryFunction>> | 28 BrowserStateKeyedServiceFactory::TestingFactoryFunction>> |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 // TestChromeBrowserState deletes the directory containing the files used by | 80 // TestChromeBrowserState deletes the directory containing the files used by |
81 // HistoryService, this only matters if you're recreating the HistoryService. | 81 // HistoryService, this only matters if you're recreating the HistoryService. |
82 bool CreateHistoryService(bool delete_file) WARN_UNUSED_RESULT; | 82 bool CreateHistoryService(bool delete_file) WARN_UNUSED_RESULT; |
83 | 83 |
84 // Shuts down and nulls out the reference to HistoryService. | 84 // Shuts down and nulls out the reference to HistoryService. |
85 void DestroyHistoryService(); | 85 void DestroyHistoryService(); |
86 | 86 |
87 // Returns the preferences as a TestingPrefServiceSyncable if possible or | 87 // Returns the preferences as a TestingPrefServiceSyncable if possible or |
88 // null. Returns null for off-the-record TestChromeBrowserState and also | 88 // null. Returns null for off-the-record TestChromeBrowserState and also |
89 // for TestChromeBrowserState initialized with a custom pref service. | 89 // for TestChromeBrowserState initialized with a custom pref service. |
90 syncable_prefs::TestingPrefServiceSyncable* GetTestingPrefService(); | 90 sync_preferences::TestingPrefServiceSyncable* GetTestingPrefService(); |
91 | 91 |
92 // Helper class that allows for parameterizing the building | 92 // Helper class that allows for parameterizing the building |
93 // of TestChromeBrowserStates. | 93 // of TestChromeBrowserStates. |
94 class Builder { | 94 class Builder { |
95 public: | 95 public: |
96 Builder(); | 96 Builder(); |
97 ~Builder(); | 97 ~Builder(); |
98 | 98 |
99 // Adds a testing factory to the TestChromeBrowserState. These testing | 99 // Adds a testing factory to the TestChromeBrowserState. These testing |
100 // factories are installed before the ProfileKeyedServices are created. | 100 // factories are installed before the ProfileKeyedServices are created. |
101 void AddTestingFactory( | 101 void AddTestingFactory( |
102 BrowserStateKeyedServiceFactory* service_factory, | 102 BrowserStateKeyedServiceFactory* service_factory, |
103 BrowserStateKeyedServiceFactory::TestingFactoryFunction cb); | 103 BrowserStateKeyedServiceFactory::TestingFactoryFunction cb); |
104 void AddTestingFactory( | 104 void AddTestingFactory( |
105 RefcountedBrowserStateKeyedServiceFactory* service_factory, | 105 RefcountedBrowserStateKeyedServiceFactory* service_factory, |
106 RefcountedBrowserStateKeyedServiceFactory::TestingFactoryFunction cb); | 106 RefcountedBrowserStateKeyedServiceFactory::TestingFactoryFunction cb); |
107 | 107 |
108 // Sets the path to the directory to be used to hold ChromeBrowserState | 108 // Sets the path to the directory to be used to hold ChromeBrowserState |
109 // data. | 109 // data. |
110 void SetPath(const base::FilePath& path); | 110 void SetPath(const base::FilePath& path); |
111 | 111 |
112 // Sets the PrefService to be used by the ChromeBrowserState. | 112 // Sets the PrefService to be used by the ChromeBrowserState. |
113 void SetPrefService( | 113 void SetPrefService( |
114 std::unique_ptr<syncable_prefs::PrefServiceSyncable> prefs); | 114 std::unique_ptr<sync_preferences::PrefServiceSyncable> prefs); |
115 | 115 |
116 // Creates the TestChromeBrowserState using previously-set settings. | 116 // Creates the TestChromeBrowserState using previously-set settings. |
117 std::unique_ptr<TestChromeBrowserState> Build(); | 117 std::unique_ptr<TestChromeBrowserState> Build(); |
118 | 118 |
119 private: | 119 private: |
120 // If true, Build() has been called. | 120 // If true, Build() has been called. |
121 bool build_called_; | 121 bool build_called_; |
122 | 122 |
123 // Various staging variables where values are held until Build() is invoked. | 123 // Various staging variables where values are held until Build() is invoked. |
124 base::FilePath state_path_; | 124 base::FilePath state_path_; |
125 std::unique_ptr<syncable_prefs::PrefServiceSyncable> pref_service_; | 125 std::unique_ptr<sync_preferences::PrefServiceSyncable> pref_service_; |
126 | 126 |
127 TestingFactories testing_factories_; | 127 TestingFactories testing_factories_; |
128 RefcountedTestingFactories refcounted_testing_factories_; | 128 RefcountedTestingFactories refcounted_testing_factories_; |
129 | 129 |
130 DISALLOW_COPY_AND_ASSIGN(Builder); | 130 DISALLOW_COPY_AND_ASSIGN(Builder); |
131 }; | 131 }; |
132 | 132 |
133 protected: | 133 protected: |
134 // Used to create the principal TestChromeBrowserState. | 134 // Used to create the principal TestChromeBrowserState. |
135 TestChromeBrowserState( | 135 TestChromeBrowserState( |
136 const base::FilePath& path, | 136 const base::FilePath& path, |
137 std::unique_ptr<syncable_prefs::PrefServiceSyncable> prefs, | 137 std::unique_ptr<sync_preferences::PrefServiceSyncable> prefs, |
138 const TestingFactories& testing_factories, | 138 const TestingFactories& testing_factories, |
139 const RefcountedTestingFactories& refcounted_testing_factories); | 139 const RefcountedTestingFactories& refcounted_testing_factories); |
140 | 140 |
141 private: | 141 private: |
142 friend class Builder; | 142 friend class Builder; |
143 | 143 |
144 // Used to create the incognito TestChromeBrowserState. | 144 // Used to create the incognito TestChromeBrowserState. |
145 explicit TestChromeBrowserState( | 145 explicit TestChromeBrowserState( |
146 TestChromeBrowserState* original_browser_state); | 146 TestChromeBrowserState* original_browser_state); |
147 | 147 |
148 // Initialization of the TestChromeBrowserState. This is a separate method | 148 // Initialization of the TestChromeBrowserState. This is a separate method |
149 // as it needs to be called after the bi-directional link between original | 149 // as it needs to be called after the bi-directional link between original |
150 // and off-the-record TestChromeBrowserState has been created. | 150 // and off-the-record TestChromeBrowserState has been created. |
151 void Init(); | 151 void Init(); |
152 | 152 |
153 // We use a temporary directory to store testing browser state data. | 153 // We use a temporary directory to store testing browser state data. |
154 // This must be declared before anything that may make use of the | 154 // This must be declared before anything that may make use of the |
155 // directory so as to ensure files are closed before cleanup. | 155 // directory so as to ensure files are closed before cleanup. |
156 base::ScopedTempDir temp_dir_; | 156 base::ScopedTempDir temp_dir_; |
157 | 157 |
158 // The path to this browser state. | 158 // The path to this browser state. |
159 base::FilePath state_path_; | 159 base::FilePath state_path_; |
160 | 160 |
161 // If non-null, |testing_prefs_| points to |prefs_|. It is there to avoid | 161 // If non-null, |testing_prefs_| points to |prefs_|. It is there to avoid |
162 // casting as |prefs_| may not be a TestingPrefServiceSyncable. | 162 // casting as |prefs_| may not be a TestingPrefServiceSyncable. |
163 std::unique_ptr<syncable_prefs::PrefServiceSyncable> prefs_; | 163 std::unique_ptr<sync_preferences::PrefServiceSyncable> prefs_; |
164 syncable_prefs::TestingPrefServiceSyncable* testing_prefs_; | 164 sync_preferences::TestingPrefServiceSyncable* testing_prefs_; |
165 | 165 |
166 // The incognito ChromeBrowserState instance that is associated with this | 166 // The incognito ChromeBrowserState instance that is associated with this |
167 // non-incognito ChromeBrowserState instance. | 167 // non-incognito ChromeBrowserState instance. |
168 std::unique_ptr<TestChromeBrowserState> otr_browser_state_; | 168 std::unique_ptr<TestChromeBrowserState> otr_browser_state_; |
169 TestChromeBrowserState* original_browser_state_; | 169 TestChromeBrowserState* original_browser_state_; |
170 | 170 |
171 DISALLOW_COPY_AND_ASSIGN(TestChromeBrowserState); | 171 DISALLOW_COPY_AND_ASSIGN(TestChromeBrowserState); |
172 }; | 172 }; |
173 | 173 |
174 #endif // IOS_CHROME_BROWSER_BROWSER_STATE_TEST_CHROME_BROWSER_STATE_H_ | 174 #endif // IOS_CHROME_BROWSER_BROWSER_STATE_TEST_CHROME_BROWSER_STATE_H_ |
OLD | NEW |