[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 5 | #include "components/prefs/json_pref_store.h" |
[email protected] | 00c8782 | 2012-11-27 19:09:17 | [diff] [blame] | 6 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 9 | #include <memory> |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 10 | #include <utility> |
| 11 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 12 | #include "base/bind.h" |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 14 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 15 | #include "base/files/scoped_temp_dir.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 16 | #include "base/location.h" |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 17 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 18 | #include "base/memory/ref_counted.h" |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 19 | #include "base/metrics/histogram_samples.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 20 | #include "base/path_service.h" |
[email protected] | f7b98b3 | 2013-02-05 08:14:15 | [diff] [blame] | 21 | #include "base/run_loop.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 22 | #include "base/single_thread_task_runner.h" |
[email protected] | dfa049e | 2013-02-07 02:57:22 | [diff] [blame] | 23 | #include "base/strings/string_number_conversions.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 24 | #include "base/strings/string_util.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 25 | #include "base/strings/utf_string_conversions.h" |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 26 | #include "base/synchronization/waitable_event.h" |
Devlin Cronin | 69228f4 | 2018-06-01 17:25:10 | [diff] [blame] | 27 | #include "base/test/metrics/histogram_tester.h" |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 28 | #include "base/test/scoped_task_environment.h" |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 29 | #include "base/test/simple_test_clock.h" |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 30 | #include "base/threading/sequenced_task_runner_handle.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 31 | #include "base/threading/thread.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 32 | #include "base/values.h" |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 33 | #include "components/prefs/persistent_pref_store_unittest.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 34 | #include "components/prefs/pref_filter.h" |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 35 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 36 | #include "testing/gtest/include/gtest/gtest.h" |
| 37 | |
[email protected] | 7e3ec42c | 2012-12-16 05:13:21 | [diff] [blame] | 38 | namespace base { |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 39 | namespace { |
| 40 | |
[email protected] | 5bfdcfd | 2012-11-22 22:08:24 | [diff] [blame] | 41 | const char kHomePage[] = "homepage"; |
| 42 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 43 | const char kReadJson[] = |
| 44 | "{\n" |
| 45 | " \"homepage\": \"http://www.cnn.com\",\n" |
| 46 | " \"some_directory\": \"/usr/local/\",\n" |
| 47 | " \"tabs\": {\n" |
| 48 | " \"new_windows_in_tabs\": true,\n" |
| 49 | " \"max_tabs\": 20\n" |
| 50 | " }\n" |
| 51 | "}"; |
| 52 | |
| 53 | const char kInvalidJson[] = "!@#$%^&"; |
| 54 | |
| 55 | // Expected output for tests using RunBasicJsonPrefStoreTest(). |
| 56 | const char kWriteGolden[] = |
| 57 | "{\"homepage\":\"http://www.cnn.com\"," |
| 58 | "\"long_int\":{\"pref\":\"214748364842\"}," |
| 59 | "\"some_directory\":\"/usr/sbin/\"," |
| 60 | "\"tabs\":{\"max_tabs\":10,\"new_windows_in_tabs\":false}}"; |
| 61 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 62 | // Set the time on the given SimpleTestClock to the given time in minutes. |
| 63 | void SetCurrentTimeInMinutes(double minutes, base::SimpleTestClock* clock) { |
| 64 | const int32_t kBaseTimeMins = 100; |
| 65 | clock->SetNow(base::Time::FromDoubleT((kBaseTimeMins + minutes) * 60)); |
| 66 | } |
| 67 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 68 | // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on |
| 69 | // to the |prefs| until explicitly asked to release them. |
| 70 | class InterceptingPrefFilter : public PrefFilter { |
| 71 | public: |
| 72 | InterceptingPrefFilter(); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 73 | InterceptingPrefFilter(OnWriteCallbackPair callback_pair); |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 74 | ~InterceptingPrefFilter() override; |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 75 | |
| 76 | // PrefFilter implementation: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 77 | void FilterOnLoad( |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 78 | const PostFilterOnLoadCallback& post_filter_on_load_callback, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 79 | std::unique_ptr<base::DictionaryValue> pref_store_contents) override; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 80 | void FilterUpdate(const std::string& path) override {} |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 81 | OnWriteCallbackPair FilterSerializeData( |
| 82 | base::DictionaryValue* pref_store_contents) override { |
| 83 | return on_write_callback_pair_; |
| 84 | } |
proberge | 45e34728 | 2017-08-16 21:24:05 | [diff] [blame] | 85 | void OnStoreDeletionFromDisk() override {} |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 86 | |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 87 | bool has_intercepted_prefs() const { return intercepted_prefs_ != nullptr; } |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 88 | |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 89 | // Finalize an intercepted read, handing |intercepted_prefs_| back to its |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 90 | // JsonPrefStore. |
| 91 | void ReleasePrefs(); |
| 92 | |
| 93 | private: |
| 94 | PostFilterOnLoadCallback post_filter_on_load_callback_; |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 95 | std::unique_ptr<base::DictionaryValue> intercepted_prefs_; |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 96 | OnWriteCallbackPair on_write_callback_pair_; |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 97 | |
| 98 | DISALLOW_COPY_AND_ASSIGN(InterceptingPrefFilter); |
| 99 | }; |
| 100 | |
| 101 | InterceptingPrefFilter::InterceptingPrefFilter() {} |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 102 | |
| 103 | InterceptingPrefFilter::InterceptingPrefFilter( |
| 104 | OnWriteCallbackPair callback_pair) { |
| 105 | on_write_callback_pair_ = callback_pair; |
| 106 | } |
| 107 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 108 | InterceptingPrefFilter::~InterceptingPrefFilter() {} |
| 109 | |
| 110 | void InterceptingPrefFilter::FilterOnLoad( |
| 111 | const PostFilterOnLoadCallback& post_filter_on_load_callback, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 112 | std::unique_ptr<base::DictionaryValue> pref_store_contents) { |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 113 | post_filter_on_load_callback_ = post_filter_on_load_callback; |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 114 | intercepted_prefs_ = std::move(pref_store_contents); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void InterceptingPrefFilter::ReleasePrefs() { |
| 118 | EXPECT_FALSE(post_filter_on_load_callback_.is_null()); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 119 | post_filter_on_load_callback_.Run(std::move(intercepted_prefs_), false); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 120 | post_filter_on_load_callback_.Reset(); |
| 121 | } |
| 122 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 123 | class MockPrefStoreObserver : public PrefStore::Observer { |
| 124 | public: |
| 125 | MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); |
| 126 | MOCK_METHOD1(OnInitializationCompleted, void (bool)); |
| 127 | }; |
| 128 | |
| 129 | class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { |
| 130 | public: |
| 131 | MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); |
| 132 | }; |
| 133 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 134 | enum class CommitPendingWriteMode { |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 135 | // Basic mode. |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 136 | WITHOUT_CALLBACK, |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 137 | // With reply callback. |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 138 | WITH_CALLBACK, |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 139 | // With synchronous notify callback (synchronous after the write -- shouldn't |
| 140 | // require pumping messages to observe). |
| 141 | WITH_SYNCHRONOUS_CALLBACK, |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 142 | }; |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 143 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 144 | base::test::ScopedTaskEnvironment::ExecutionMode GetExecutionMode( |
| 145 | CommitPendingWriteMode commit_mode) { |
| 146 | switch (commit_mode) { |
| 147 | case CommitPendingWriteMode::WITHOUT_CALLBACK: |
| 148 | FALLTHROUGH; |
| 149 | case CommitPendingWriteMode::WITH_CALLBACK: |
| 150 | return base::test::ScopedTaskEnvironment::ExecutionMode::QUEUED; |
| 151 | case CommitPendingWriteMode::WITH_SYNCHRONOUS_CALLBACK: |
| 152 | // Synchronous callbacks require async tasks to run on their own. |
| 153 | return base::test::ScopedTaskEnvironment::ExecutionMode::ASYNC; |
| 154 | } |
| 155 | } |
| 156 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 157 | void CommitPendingWrite( |
| 158 | JsonPrefStore* pref_store, |
| 159 | CommitPendingWriteMode commit_pending_write_mode, |
| 160 | base::test::ScopedTaskEnvironment* scoped_task_environment) { |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 161 | switch (commit_pending_write_mode) { |
| 162 | case CommitPendingWriteMode::WITHOUT_CALLBACK: { |
| 163 | pref_store->CommitPendingWrite(); |
| 164 | scoped_task_environment->RunUntilIdle(); |
| 165 | break; |
| 166 | } |
| 167 | case CommitPendingWriteMode::WITH_CALLBACK: { |
| 168 | TestCommitPendingWriteWithCallback(pref_store, scoped_task_environment); |
| 169 | break; |
| 170 | } |
| 171 | case CommitPendingWriteMode::WITH_SYNCHRONOUS_CALLBACK: { |
| 172 | base::WaitableEvent written; |
| 173 | pref_store->CommitPendingWrite( |
| 174 | base::OnceClosure(), |
| 175 | base::BindOnce(&base::WaitableEvent::Signal, Unretained(&written))); |
| 176 | written.Wait(); |
| 177 | break; |
| 178 | } |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | class JsonPrefStoreTest |
| 183 | : public testing::TestWithParam<CommitPendingWriteMode> { |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 184 | public: |
Francois Doray | 5f54795 | 2017-07-25 21:02:05 | [diff] [blame] | 185 | JsonPrefStoreTest() |
| 186 | : scoped_task_environment_( |
| 187 | base::test::ScopedTaskEnvironment::MainThreadType::DEFAULT, |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 188 | GetExecutionMode(GetParam())) {} |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 189 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 190 | protected: |
dcheng | 8aef3761 | 2014-12-23 02:56:47 | [diff] [blame] | 191 | void SetUp() override { |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 192 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 193 | } |
| 194 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 195 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 196 | |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 197 | // The path to temporary directory used to contain the test operations. |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 198 | base::ScopedTempDir temp_dir_; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 199 | |
| 200 | DISALLOW_COPY_AND_ASSIGN(JsonPrefStoreTest); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 201 | }; |
| 202 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 203 | } // namespace |
| 204 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 205 | // Test fallback behavior for a nonexistent file. |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 206 | TEST_P(JsonPrefStoreTest, NonExistentFile) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 207 | base::FilePath bogus_input_file = temp_dir_.GetPath().AppendASCII("read.txt"); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 208 | ASSERT_FALSE(PathExists(bogus_input_file)); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 209 | auto pref_store = base::MakeRefCounted<JsonPrefStore>(bogus_input_file); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 210 | EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 211 | pref_store->ReadPrefs()); |
| 212 | EXPECT_FALSE(pref_store->ReadOnly()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // Test fallback behavior for an invalid file. |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 216 | TEST_P(JsonPrefStoreTest, InvalidFile) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 217 | base::FilePath invalid_file = temp_dir_.GetPath().AppendASCII("invalid.json"); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 218 | ASSERT_LT(0, base::WriteFile(invalid_file, |
| 219 | kInvalidJson, arraysize(kInvalidJson) - 1)); |
| 220 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 221 | auto pref_store = base::MakeRefCounted<JsonPrefStore>(invalid_file); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 222 | EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 223 | pref_store->ReadPrefs()); |
| 224 | EXPECT_FALSE(pref_store->ReadOnly()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 225 | |
| 226 | // The file should have been moved aside. |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 227 | EXPECT_FALSE(PathExists(invalid_file)); |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 228 | base::FilePath moved_aside = temp_dir_.GetPath().AppendASCII("invalid.bad"); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 229 | EXPECT_TRUE(PathExists(moved_aside)); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 230 | |
| 231 | std::string moved_aside_contents; |
| 232 | ASSERT_TRUE(base::ReadFileToString(moved_aside, &moved_aside_contents)); |
| 233 | EXPECT_EQ(kInvalidJson, moved_aside_contents); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 234 | } |
| 235 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 236 | // This function is used to avoid code duplication while testing synchronous |
| 237 | // and asynchronous version of the JsonPrefStore loading. It validates that the |
| 238 | // given output file's contents matches kWriteGolden. |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 239 | void RunBasicJsonPrefStoreTest( |
| 240 | JsonPrefStore* pref_store, |
| 241 | const base::FilePath& output_file, |
| 242 | CommitPendingWriteMode commit_pending_write_mode, |
| 243 | base::test::ScopedTaskEnvironment* scoped_task_environment) { |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 244 | const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; |
| 245 | const char kMaxTabs[] = "tabs.max_tabs"; |
| 246 | const char kLongIntPref[] = "long_int.pref"; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 247 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 248 | std::string cnn("http://www.cnn.com"); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 249 | |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 250 | const Value* actual; |
[email protected] | 5bfdcfd | 2012-11-22 22:08:24 | [diff] [blame] | 251 | EXPECT_TRUE(pref_store->GetValue(kHomePage, &actual)); |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 252 | std::string string_value; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 253 | EXPECT_TRUE(actual->GetAsString(&string_value)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 254 | EXPECT_EQ(cnn, string_value); |
| 255 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 256 | const char kSomeDirectory[] = "some_directory"; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 257 | |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 258 | EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 259 | base::FilePath::StringType path; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 260 | EXPECT_TRUE(actual->GetAsString(&path)); |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 261 | EXPECT_EQ(base::FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); |
| 262 | base::FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 263 | |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 264 | pref_store->SetValue(kSomeDirectory, |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 265 | std::make_unique<Value>(some_path.value()), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 266 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 267 | EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 268 | EXPECT_TRUE(actual->GetAsString(&path)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 269 | EXPECT_EQ(some_path.value(), path); |
| 270 | |
| 271 | // Test reading some other data types from sub-dictionaries. |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 272 | EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 273 | bool boolean = false; |
| 274 | EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 275 | EXPECT_TRUE(boolean); |
| 276 | |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 277 | pref_store->SetValue(kNewWindowsInTabs, std::make_unique<Value>(false), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 278 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 279 | EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 280 | EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 281 | EXPECT_FALSE(boolean); |
| 282 | |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 283 | EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 284 | int integer = 0; |
| 285 | EXPECT_TRUE(actual->GetAsInteger(&integer)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 286 | EXPECT_EQ(20, integer); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 287 | pref_store->SetValue(kMaxTabs, std::make_unique<Value>(10), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 288 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 289 | EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 290 | EXPECT_TRUE(actual->GetAsInteger(&integer)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 291 | EXPECT_EQ(10, integer); |
| 292 | |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 293 | pref_store->SetValue( |
| 294 | kLongIntPref, |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 295 | std::make_unique<Value>(base::Int64ToString(214748364842LL)), |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 296 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 297 | EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 298 | EXPECT_TRUE(actual->GetAsString(&string_value)); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 299 | int64_t value; |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 300 | base::StringToInt64(string_value, &value); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 301 | EXPECT_EQ(214748364842LL, value); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 302 | |
| 303 | // Serialize and compare to expected output. |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 304 | CommitPendingWrite(pref_store, commit_pending_write_mode, |
| 305 | scoped_task_environment); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 306 | |
| 307 | std::string output_contents; |
| 308 | ASSERT_TRUE(base::ReadFileToString(output_file, &output_contents)); |
| 309 | EXPECT_EQ(kWriteGolden, output_contents); |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 310 | ASSERT_TRUE(base::DeleteFile(output_file, false)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 311 | } |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 312 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 313 | TEST_P(JsonPrefStoreTest, Basic) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 314 | base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 315 | ASSERT_LT(0, base::WriteFile(input_file, |
| 316 | kReadJson, arraysize(kReadJson) - 1)); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 317 | |
| 318 | // Test that the persistent value can be loaded. |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 319 | ASSERT_TRUE(PathExists(input_file)); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 320 | auto pref_store = base::MakeRefCounted<JsonPrefStore>(input_file); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 321 | ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 322 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 323 | EXPECT_TRUE(pref_store->IsInitializationComplete()); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 324 | |
| 325 | // The JSON file looks like this: |
| 326 | // { |
| 327 | // "homepage": "http://www.cnn.com", |
| 328 | // "some_directory": "/usr/local/", |
| 329 | // "tabs": { |
| 330 | // "new_windows_in_tabs": true, |
| 331 | // "max_tabs": 20 |
| 332 | // } |
| 333 | // } |
| 334 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 335 | RunBasicJsonPrefStoreTest(pref_store.get(), input_file, GetParam(), |
| 336 | &scoped_task_environment_); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 337 | } |
| 338 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 339 | TEST_P(JsonPrefStoreTest, BasicAsync) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 340 | base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 341 | ASSERT_LT(0, base::WriteFile(input_file, |
| 342 | kReadJson, arraysize(kReadJson) - 1)); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 343 | |
| 344 | // Test that the persistent value can be loaded. |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 345 | auto pref_store = base::MakeRefCounted<JsonPrefStore>(input_file); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 346 | |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 347 | { |
| 348 | MockPrefStoreObserver mock_observer; |
| 349 | pref_store->AddObserver(&mock_observer); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 350 | |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 351 | MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 352 | pref_store->ReadPrefsAsync(mock_error_delegate); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 353 | |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 354 | EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 355 | EXPECT_CALL(*mock_error_delegate, |
| 356 | OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 357 | scoped_task_environment_.RunUntilIdle(); |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 358 | pref_store->RemoveObserver(&mock_observer); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 359 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 360 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 361 | EXPECT_TRUE(pref_store->IsInitializationComplete()); |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 362 | } |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 363 | |
| 364 | // The JSON file looks like this: |
| 365 | // { |
| 366 | // "homepage": "http://www.cnn.com", |
| 367 | // "some_directory": "/usr/local/", |
| 368 | // "tabs": { |
| 369 | // "new_windows_in_tabs": true, |
| 370 | // "max_tabs": 20 |
| 371 | // } |
| 372 | // } |
| 373 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 374 | RunBasicJsonPrefStoreTest(pref_store.get(), input_file, GetParam(), |
| 375 | &scoped_task_environment_); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 376 | } |
| 377 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 378 | TEST_P(JsonPrefStoreTest, PreserveEmptyValues) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 379 | FilePath pref_file = temp_dir_.GetPath().AppendASCII("empty_values.json"); |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 380 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 381 | auto pref_store = base::MakeRefCounted<JsonPrefStore>(pref_file); |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 382 | |
| 383 | // Set some keys with empty values. |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 384 | pref_store->SetValue("list", std::make_unique<base::ListValue>(), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 385 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 386 | pref_store->SetValue("dict", std::make_unique<base::DictionaryValue>(), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 387 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 388 | |
| 389 | // Write to file. |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 390 | CommitPendingWrite(pref_store.get(), GetParam(), &scoped_task_environment_); |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 391 | |
| 392 | // Reload. |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 393 | pref_store = base::MakeRefCounted<JsonPrefStore>(pref_file); |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 394 | ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 395 | ASSERT_FALSE(pref_store->ReadOnly()); |
| 396 | |
| 397 | // Check values. |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 398 | const Value* result = nullptr; |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 399 | EXPECT_TRUE(pref_store->GetValue("list", &result)); |
| 400 | EXPECT_TRUE(ListValue().Equals(result)); |
| 401 | EXPECT_TRUE(pref_store->GetValue("dict", &result)); |
| 402 | EXPECT_TRUE(DictionaryValue().Equals(result)); |
| 403 | } |
| 404 | |
[email protected] | eeedaa69 | 2014-01-30 09:22:27 | [diff] [blame] | 405 | // This test is just documenting some potentially non-obvious behavior. It |
| 406 | // shouldn't be taken as normative. |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 407 | TEST_P(JsonPrefStoreTest, RemoveClearsEmptyParent) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 408 | FilePath pref_file = temp_dir_.GetPath().AppendASCII("empty_values.json"); |
[email protected] | eeedaa69 | 2014-01-30 09:22:27 | [diff] [blame] | 409 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 410 | auto pref_store = base::MakeRefCounted<JsonPrefStore>(pref_file); |
[email protected] | eeedaa69 | 2014-01-30 09:22:27 | [diff] [blame] | 411 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 412 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
[email protected] | eeedaa69 | 2014-01-30 09:22:27 | [diff] [blame] | 413 | dict->SetString("key", "value"); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 414 | pref_store->SetValue("dict", std::move(dict), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 415 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | eeedaa69 | 2014-01-30 09:22:27 | [diff] [blame] | 416 | |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 417 | pref_store->RemoveValue("dict.key", |
| 418 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | eeedaa69 | 2014-01-30 09:22:27 | [diff] [blame] | 419 | |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 420 | const base::Value* retrieved_dict = nullptr; |
[email protected] | eeedaa69 | 2014-01-30 09:22:27 | [diff] [blame] | 421 | bool has_dict = pref_store->GetValue("dict", &retrieved_dict); |
| 422 | EXPECT_FALSE(has_dict); |
| 423 | } |
| 424 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 425 | // Tests asynchronous reading of the file when there is no file. |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 426 | TEST_P(JsonPrefStoreTest, AsyncNonExistingFile) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 427 | base::FilePath bogus_input_file = temp_dir_.GetPath().AppendASCII("read.txt"); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 428 | ASSERT_FALSE(PathExists(bogus_input_file)); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 429 | auto pref_store = base::MakeRefCounted<JsonPrefStore>(bogus_input_file); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 430 | MockPrefStoreObserver mock_observer; |
| 431 | pref_store->AddObserver(&mock_observer); |
| 432 | |
| 433 | MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
| 434 | pref_store->ReadPrefsAsync(mock_error_delegate); |
| 435 | |
| 436 | EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 437 | EXPECT_CALL(*mock_error_delegate, |
| 438 | OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 439 | scoped_task_environment_.RunUntilIdle(); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 440 | pref_store->RemoveObserver(&mock_observer); |
| 441 | |
| 442 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 443 | } |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 444 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 445 | TEST_P(JsonPrefStoreTest, ReadWithInterceptor) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 446 | base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 447 | ASSERT_LT(0, base::WriteFile(input_file, |
| 448 | kReadJson, arraysize(kReadJson) - 1)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 449 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 450 | std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 451 | new InterceptingPrefFilter()); |
| 452 | InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
| 453 | intercepting_pref_filter.get(); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 454 | auto pref_store = base::MakeRefCounted<JsonPrefStore>( |
Gabriel Charette | 1f53ca3 | 2018-08-07 21:31:17 | [diff] [blame] | 455 | input_file, std::move(intercepting_pref_filter)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 456 | |
| 457 | ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, |
| 458 | pref_store->ReadPrefs()); |
| 459 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 460 | |
| 461 | // The store shouldn't be considered initialized until the interceptor |
| 462 | // returns. |
| 463 | EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 464 | EXPECT_FALSE(pref_store->IsInitializationComplete()); |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 465 | EXPECT_FALSE(pref_store->GetValue(kHomePage, nullptr)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 466 | |
| 467 | raw_intercepting_pref_filter_->ReleasePrefs(); |
| 468 | |
| 469 | EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 470 | EXPECT_TRUE(pref_store->IsInitializationComplete()); |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 471 | EXPECT_TRUE(pref_store->GetValue(kHomePage, nullptr)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 472 | |
| 473 | // The JSON file looks like this: |
| 474 | // { |
| 475 | // "homepage": "http://www.cnn.com", |
| 476 | // "some_directory": "/usr/local/", |
| 477 | // "tabs": { |
| 478 | // "new_windows_in_tabs": true, |
| 479 | // "max_tabs": 20 |
| 480 | // } |
| 481 | // } |
| 482 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 483 | RunBasicJsonPrefStoreTest(pref_store.get(), input_file, GetParam(), |
| 484 | &scoped_task_environment_); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 485 | } |
| 486 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 487 | TEST_P(JsonPrefStoreTest, ReadAsyncWithInterceptor) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 488 | base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 489 | ASSERT_LT(0, base::WriteFile(input_file, |
| 490 | kReadJson, arraysize(kReadJson) - 1)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 491 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 492 | std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 493 | new InterceptingPrefFilter()); |
| 494 | InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
| 495 | intercepting_pref_filter.get(); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 496 | auto pref_store = base::MakeRefCounted<JsonPrefStore>( |
Gabriel Charette | 1f53ca3 | 2018-08-07 21:31:17 | [diff] [blame] | 497 | input_file, std::move(intercepting_pref_filter)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 498 | |
| 499 | MockPrefStoreObserver mock_observer; |
| 500 | pref_store->AddObserver(&mock_observer); |
| 501 | |
| 502 | // Ownership of the |mock_error_delegate| is handed to the |pref_store| below. |
| 503 | MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 504 | |
| 505 | { |
| 506 | pref_store->ReadPrefsAsync(mock_error_delegate); |
| 507 | |
| 508 | EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(0); |
| 509 | // EXPECT_CALL(*mock_error_delegate, |
| 510 | // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 511 | scoped_task_environment_.RunUntilIdle(); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 512 | |
| 513 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 514 | EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 515 | EXPECT_FALSE(pref_store->IsInitializationComplete()); |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 516 | EXPECT_FALSE(pref_store->GetValue(kHomePage, nullptr)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | { |
| 520 | EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 521 | // EXPECT_CALL(*mock_error_delegate, |
| 522 | // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
| 523 | |
| 524 | raw_intercepting_pref_filter_->ReleasePrefs(); |
| 525 | |
| 526 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 527 | EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 528 | EXPECT_TRUE(pref_store->IsInitializationComplete()); |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 529 | EXPECT_TRUE(pref_store->GetValue(kHomePage, nullptr)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | pref_store->RemoveObserver(&mock_observer); |
| 533 | |
| 534 | // The JSON file looks like this: |
| 535 | // { |
| 536 | // "homepage": "http://www.cnn.com", |
| 537 | // "some_directory": "/usr/local/", |
| 538 | // "tabs": { |
| 539 | // "new_windows_in_tabs": true, |
| 540 | // "max_tabs": 20 |
| 541 | // } |
| 542 | // } |
| 543 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 544 | RunBasicJsonPrefStoreTest(pref_store.get(), input_file, GetParam(), |
| 545 | &scoped_task_environment_); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 546 | } |
| 547 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 548 | TEST_P(JsonPrefStoreTest, WriteCountHistogramTestBasic) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 549 | base::HistogramTester histogram_tester; |
| 550 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 551 | SimpleTestClock* test_clock = new SimpleTestClock; |
| 552 | SetCurrentTimeInMinutes(0, test_clock); |
| 553 | JsonPrefStore::WriteCountHistogram histogram( |
| 554 | base::TimeDelta::FromSeconds(10), |
| 555 | base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 556 | std::unique_ptr<base::Clock>(test_clock)); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 557 | int32_t report_interval = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 558 | JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 559 | |
| 560 | histogram.RecordWriteOccured(); |
| 561 | |
| 562 | SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 563 | histogram.ReportOutstandingWrites(); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 564 | std::unique_ptr<HistogramSamples> samples = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 565 | histogram.GetHistogram()->SnapshotSamples(); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 566 | |
| 567 | std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
| 568 | histogram_tester.ExpectBucketCount(histogram_name, 1, 1); |
| 569 | histogram_tester.ExpectTotalCount(histogram_name, 1); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 570 | |
| 571 | ASSERT_EQ("Settings.JsonDataWriteCount.Local_State", |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 572 | std::string(histogram.GetHistogram()->histogram_name())); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 573 | ASSERT_TRUE(histogram.GetHistogram()->HasConstructionArguments(1, 30, 31)); |
| 574 | } |
| 575 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 576 | TEST_P(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 577 | base::HistogramTester histogram_tester; |
| 578 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 579 | SimpleTestClock* test_clock = new SimpleTestClock; |
| 580 | SetCurrentTimeInMinutes(0, test_clock); |
| 581 | JsonPrefStore::WriteCountHistogram histogram( |
| 582 | base::TimeDelta::FromSeconds(10), |
| 583 | base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 584 | std::unique_ptr<base::Clock>(test_clock)); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 585 | int32_t report_interval = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 586 | JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 587 | |
| 588 | histogram.RecordWriteOccured(); |
| 589 | SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
| 590 | histogram.RecordWriteOccured(); |
| 591 | SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
| 592 | histogram.RecordWriteOccured(); |
| 593 | |
| 594 | // Nothing should be recorded until the report period has elapsed. |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 595 | std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
| 596 | histogram_tester.ExpectTotalCount(histogram_name, 0); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 597 | |
| 598 | SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); |
| 599 | histogram.RecordWriteOccured(); |
| 600 | |
| 601 | // Now the report period has elapsed. |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 602 | histogram_tester.ExpectBucketCount(histogram_name, 3, 1); |
| 603 | histogram_tester.ExpectTotalCount(histogram_name, 1); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 604 | |
| 605 | // The last write won't be recorded because the second count period hasn't |
| 606 | // fully elapsed. |
| 607 | SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 608 | histogram.ReportOutstandingWrites(); |
| 609 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 610 | histogram_tester.ExpectBucketCount(histogram_name, 3, 1); |
| 611 | histogram_tester.ExpectTotalCount(histogram_name, 1); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 612 | } |
| 613 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 614 | TEST_P(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 615 | base::HistogramTester histogram_tester; |
| 616 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 617 | SimpleTestClock* test_clock = new SimpleTestClock; |
| 618 | SetCurrentTimeInMinutes(0, test_clock); |
| 619 | JsonPrefStore::WriteCountHistogram histogram( |
| 620 | base::TimeDelta::FromSeconds(10), |
| 621 | base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 622 | std::unique_ptr<base::Clock>(test_clock)); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 623 | int32_t report_interval = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 624 | JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 625 | |
| 626 | histogram.RecordWriteOccured(); |
| 627 | SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
| 628 | histogram.RecordWriteOccured(); |
| 629 | SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
| 630 | histogram.RecordWriteOccured(); |
| 631 | SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); |
| 632 | histogram.RecordWriteOccured(); |
| 633 | SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 634 | histogram.RecordWriteOccured(); |
| 635 | SetCurrentTimeInMinutes(2.1 * report_interval, test_clock); |
| 636 | histogram.RecordWriteOccured(); |
| 637 | SetCurrentTimeInMinutes(2.5 * report_interval, test_clock); |
| 638 | histogram.RecordWriteOccured(); |
| 639 | SetCurrentTimeInMinutes(2.7 * report_interval, test_clock); |
| 640 | histogram.RecordWriteOccured(); |
| 641 | SetCurrentTimeInMinutes(3.3 * report_interval, test_clock); |
| 642 | histogram.RecordWriteOccured(); |
| 643 | |
| 644 | // The last write won't be recorded because the second count period hasn't |
| 645 | // fully elapsed |
| 646 | SetCurrentTimeInMinutes(3.5 * report_interval, test_clock); |
| 647 | histogram.ReportOutstandingWrites(); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 648 | std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
| 649 | histogram_tester.ExpectBucketCount(histogram_name, 3, 2); |
| 650 | histogram_tester.ExpectBucketCount(histogram_name, 2, 1); |
| 651 | histogram_tester.ExpectTotalCount(histogram_name, 3); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 652 | } |
| 653 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 654 | TEST_P(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 655 | base::HistogramTester histogram_tester; |
| 656 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 657 | SimpleTestClock* test_clock = new SimpleTestClock; |
| 658 | SetCurrentTimeInMinutes(0, test_clock); |
| 659 | JsonPrefStore::WriteCountHistogram histogram( |
| 660 | base::TimeDelta::FromSeconds(10), |
| 661 | base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 662 | std::unique_ptr<base::Clock>(test_clock)); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 663 | int32_t report_interval = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 664 | JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 665 | |
| 666 | // 1 write in the first period. |
| 667 | histogram.RecordWriteOccured(); |
| 668 | |
| 669 | // No writes in the second and third periods. |
| 670 | |
| 671 | // 2 writes in the fourth period. |
| 672 | SetCurrentTimeInMinutes(3.1 * report_interval, test_clock); |
| 673 | histogram.RecordWriteOccured(); |
| 674 | SetCurrentTimeInMinutes(3.3 * report_interval, test_clock); |
| 675 | histogram.RecordWriteOccured(); |
| 676 | |
| 677 | // No writes in the fifth period. |
| 678 | |
| 679 | // 3 writes in the sixth period. |
| 680 | SetCurrentTimeInMinutes(5.1 * report_interval, test_clock); |
| 681 | histogram.RecordWriteOccured(); |
| 682 | SetCurrentTimeInMinutes(5.3 * report_interval, test_clock); |
| 683 | histogram.RecordWriteOccured(); |
| 684 | SetCurrentTimeInMinutes(5.5 * report_interval, test_clock); |
| 685 | histogram.RecordWriteOccured(); |
| 686 | |
| 687 | SetCurrentTimeInMinutes(6.1 * report_interval, test_clock); |
| 688 | histogram.ReportOutstandingWrites(); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 689 | std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
| 690 | histogram_tester.ExpectBucketCount(histogram_name, 0, 3); |
| 691 | histogram_tester.ExpectBucketCount(histogram_name, 1, 1); |
| 692 | histogram_tester.ExpectBucketCount(histogram_name, 2, 1); |
| 693 | histogram_tester.ExpectBucketCount(histogram_name, 3, 1); |
| 694 | histogram_tester.ExpectTotalCount(histogram_name, 6); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 695 | } |
| 696 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 697 | INSTANTIATE_TEST_CASE_P( |
| 698 | WithoutCallback, |
| 699 | JsonPrefStoreTest, |
| 700 | ::testing::Values(CommitPendingWriteMode::WITHOUT_CALLBACK)); |
| 701 | INSTANTIATE_TEST_CASE_P( |
| 702 | WithCallback, |
| 703 | JsonPrefStoreTest, |
| 704 | ::testing::Values(CommitPendingWriteMode::WITH_CALLBACK)); |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 705 | INSTANTIATE_TEST_CASE_P( |
| 706 | WithSynchronousCallback, |
| 707 | JsonPrefStoreTest, |
| 708 | ::testing::Values(CommitPendingWriteMode::WITH_SYNCHRONOUS_CALLBACK)); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 709 | |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 710 | class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 711 | public: |
| 712 | JsonPrefStoreLossyWriteTest() = default; |
| 713 | |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 714 | protected: |
| 715 | void SetUp() override { |
| 716 | JsonPrefStoreTest::SetUp(); |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 717 | test_file_ = temp_dir_.GetPath().AppendASCII("test.json"); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 718 | } |
| 719 | |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 720 | scoped_refptr<JsonPrefStore> CreatePrefStore() { |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 721 | return base::MakeRefCounted<JsonPrefStore>(test_file_); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | // Return the ImportantFileWriter for a given JsonPrefStore. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 725 | ImportantFileWriter* GetImportantFileWriter(JsonPrefStore* pref_store) { |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 726 | return &(pref_store->writer_); |
| 727 | } |
| 728 | |
| 729 | // Get the contents of kTestFile. Pumps the message loop before returning the |
| 730 | // result. |
| 731 | std::string GetTestFileContents() { |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 732 | scoped_task_environment_.RunUntilIdle(); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 733 | std::string file_contents; |
| 734 | ReadFileToString(test_file_, &file_contents); |
| 735 | return file_contents; |
| 736 | } |
| 737 | |
| 738 | private: |
| 739 | base::FilePath test_file_; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 740 | |
| 741 | DISALLOW_COPY_AND_ASSIGN(JsonPrefStoreLossyWriteTest); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 742 | }; |
| 743 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 744 | TEST_P(JsonPrefStoreLossyWriteTest, LossyWriteBasic) { |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 745 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 746 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 747 | |
| 748 | // Set a normal pref and check that it gets scheduled to be written. |
| 749 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 750 | pref_store->SetValue("normal", std::make_unique<base::Value>("normal"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 751 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 752 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 753 | file_writer->DoScheduledWrite(); |
| 754 | ASSERT_EQ("{\"normal\":\"normal\"}", GetTestFileContents()); |
| 755 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 756 | |
| 757 | // Set a lossy pref and check that it is not scheduled to be written. |
| 758 | // SetValue/RemoveValue. |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 759 | pref_store->SetValue("lossy", std::make_unique<base::Value>("lossy"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 760 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 761 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 762 | pref_store->RemoveValue("lossy", WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 763 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 764 | |
| 765 | // SetValueSilently/RemoveValueSilently. |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 766 | pref_store->SetValueSilently("lossy", std::make_unique<base::Value>("lossy"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 767 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 768 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 769 | pref_store->RemoveValueSilently("lossy", |
| 770 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 771 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 772 | |
| 773 | // ReportValueChanged. |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 774 | pref_store->SetValue("lossy", std::make_unique<base::Value>("lossy"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 775 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 776 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 777 | pref_store->ReportValueChanged("lossy", |
| 778 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 779 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 780 | |
| 781 | // Call CommitPendingWrite and check that the lossy pref and the normal pref |
| 782 | // are there with the last values set above. |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 783 | pref_store->CommitPendingWrite(base::OnceClosure()); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 784 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 785 | ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
| 786 | GetTestFileContents()); |
| 787 | } |
| 788 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 789 | TEST_P(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossyFirst) { |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 790 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 791 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 792 | |
| 793 | // Set a lossy pref and check that it is not scheduled to be written. |
| 794 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 795 | pref_store->SetValue("lossy", std::make_unique<base::Value>("lossy"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 796 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 797 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 798 | |
| 799 | // Set a normal pref and check that it is scheduled to be written. |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 800 | pref_store->SetValue("normal", std::make_unique<base::Value>("normal"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 801 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 802 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 803 | |
| 804 | // Call DoScheduledWrite and check both prefs get written. |
| 805 | file_writer->DoScheduledWrite(); |
| 806 | ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
| 807 | GetTestFileContents()); |
| 808 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 809 | } |
| 810 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 811 | TEST_P(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossySecond) { |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 812 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 813 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 814 | |
| 815 | // Set a normal pref and check that it is scheduled to be written. |
| 816 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 817 | pref_store->SetValue("normal", std::make_unique<base::Value>("normal"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 818 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 819 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 820 | |
| 821 | // Set a lossy pref and check that the write is still scheduled. |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 822 | pref_store->SetValue("lossy", std::make_unique<base::Value>("lossy"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 823 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 824 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 825 | |
| 826 | // Call DoScheduledWrite and check both prefs get written. |
| 827 | file_writer->DoScheduledWrite(); |
| 828 | ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
| 829 | GetTestFileContents()); |
| 830 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 831 | } |
| 832 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 833 | TEST_P(JsonPrefStoreLossyWriteTest, ScheduleLossyWrite) { |
benwells | 2673059 | 2015-05-28 13:08:08 | [diff] [blame] | 834 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 835 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
benwells | 2673059 | 2015-05-28 13:08:08 | [diff] [blame] | 836 | |
| 837 | // Set a lossy pref and check that it is not scheduled to be written. |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 838 | pref_store->SetValue("lossy", std::make_unique<base::Value>("lossy"), |
benwells | 2673059 | 2015-05-28 13:08:08 | [diff] [blame] | 839 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 840 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 841 | |
| 842 | // Schedule pending lossy writes and check that it is scheduled. |
| 843 | pref_store->SchedulePendingLossyWrites(); |
| 844 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 845 | |
| 846 | // Call CommitPendingWrite and check that the lossy pref is there with the |
| 847 | // last value set above. |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 848 | pref_store->CommitPendingWrite(base::OnceClosure()); |
benwells | 2673059 | 2015-05-28 13:08:08 | [diff] [blame] | 849 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 850 | ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); |
| 851 | } |
| 852 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 853 | INSTANTIATE_TEST_CASE_P( |
| 854 | WithoutCallback, |
| 855 | JsonPrefStoreLossyWriteTest, |
| 856 | ::testing::Values(CommitPendingWriteMode::WITHOUT_CALLBACK)); |
| 857 | INSTANTIATE_TEST_CASE_P( |
| 858 | WithReply, |
| 859 | JsonPrefStoreLossyWriteTest, |
| 860 | ::testing::Values(CommitPendingWriteMode::WITH_CALLBACK)); |
| 861 | INSTANTIATE_TEST_CASE_P( |
| 862 | WithNotify, |
| 863 | JsonPrefStoreLossyWriteTest, |
| 864 | ::testing::Values(CommitPendingWriteMode::WITH_SYNCHRONOUS_CALLBACK)); |
| 865 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 866 | class SuccessfulWriteReplyObserver { |
| 867 | public: |
| 868 | SuccessfulWriteReplyObserver() = default; |
| 869 | |
| 870 | // Returns true if a successful write was observed via on_successful_write() |
| 871 | // and resets the observation state to false regardless. |
| 872 | bool GetAndResetObservationState() { |
| 873 | bool was_successful_write_observed = successful_write_reply_observed_; |
| 874 | successful_write_reply_observed_ = false; |
| 875 | return was_successful_write_observed; |
| 876 | } |
| 877 | |
| 878 | // Register OnWrite() to be called on the next write of |json_pref_store|. |
| 879 | void ObserveNextWriteCallback(JsonPrefStore* json_pref_store); |
| 880 | |
| 881 | void OnSuccessfulWrite() { |
| 882 | EXPECT_FALSE(successful_write_reply_observed_); |
| 883 | successful_write_reply_observed_ = true; |
| 884 | } |
| 885 | |
| 886 | private: |
| 887 | bool successful_write_reply_observed_ = false; |
| 888 | |
| 889 | DISALLOW_COPY_AND_ASSIGN(SuccessfulWriteReplyObserver); |
| 890 | }; |
| 891 | |
| 892 | void SuccessfulWriteReplyObserver::ObserveNextWriteCallback( |
| 893 | JsonPrefStore* json_pref_store) { |
| 894 | json_pref_store->RegisterOnNextSuccessfulWriteReply( |
| 895 | base::Bind(&SuccessfulWriteReplyObserver::OnSuccessfulWrite, |
| 896 | base::Unretained(this))); |
| 897 | } |
| 898 | |
| 899 | enum WriteCallbackObservationState { |
| 900 | NOT_CALLED, |
| 901 | CALLED_WITH_ERROR, |
| 902 | CALLED_WITH_SUCCESS, |
| 903 | }; |
| 904 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 905 | class WriteCallbacksObserver { |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 906 | public: |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 907 | WriteCallbacksObserver() = default; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 908 | |
| 909 | // Register OnWrite() to be called on the next write of |json_pref_store|. |
| 910 | void ObserveNextWriteCallback(JsonPrefStore* json_pref_store); |
| 911 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 912 | // Returns whether OnPreWrite() was called, and resets the observation state |
| 913 | // to false. |
| 914 | bool GetAndResetPreWriteObservationState(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 915 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 916 | // Returns the |WriteCallbackObservationState| which was observed, then resets |
| 917 | // it to |NOT_CALLED|. |
| 918 | WriteCallbackObservationState GetAndResetPostWriteObservationState(); |
| 919 | |
| 920 | JsonPrefStore::OnWriteCallbackPair GetCallbackPair() { |
| 921 | return std::make_pair( |
| 922 | base::Bind(&WriteCallbacksObserver::OnPreWrite, base::Unretained(this)), |
| 923 | base::Bind(&WriteCallbacksObserver::OnPostWrite, |
| 924 | base::Unretained(this))); |
| 925 | } |
| 926 | |
| 927 | void OnPreWrite() { |
| 928 | EXPECT_FALSE(pre_write_called_); |
| 929 | pre_write_called_ = true; |
| 930 | } |
| 931 | |
| 932 | void OnPostWrite(bool success) { |
| 933 | EXPECT_EQ(NOT_CALLED, post_write_observation_state_); |
| 934 | post_write_observation_state_ = |
| 935 | success ? CALLED_WITH_SUCCESS : CALLED_WITH_ERROR; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | private: |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 939 | bool pre_write_called_ = false; |
| 940 | WriteCallbackObservationState post_write_observation_state_ = NOT_CALLED; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 941 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 942 | DISALLOW_COPY_AND_ASSIGN(WriteCallbacksObserver); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 943 | }; |
| 944 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 945 | void WriteCallbacksObserver::ObserveNextWriteCallback(JsonPrefStore* writer) { |
| 946 | writer->RegisterOnNextWriteSynchronousCallbacks(GetCallbackPair()); |
| 947 | } |
| 948 | |
| 949 | bool WriteCallbacksObserver::GetAndResetPreWriteObservationState() { |
| 950 | bool observation_state = pre_write_called_; |
| 951 | pre_write_called_ = false; |
| 952 | return observation_state; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | WriteCallbackObservationState |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 956 | WriteCallbacksObserver::GetAndResetPostWriteObservationState() { |
| 957 | WriteCallbackObservationState state = post_write_observation_state_; |
| 958 | pre_write_called_ = false; |
| 959 | post_write_observation_state_ = NOT_CALLED; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 960 | return state; |
| 961 | } |
| 962 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 963 | class JsonPrefStoreCallbackTest : public testing::Test { |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 964 | public: |
| 965 | JsonPrefStoreCallbackTest() = default; |
| 966 | |
| 967 | protected: |
| 968 | void SetUp() override { |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 969 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
vabr | 5d919d6 | 2016-09-30 08:55:36 | [diff] [blame] | 970 | test_file_ = temp_dir_.GetPath().AppendASCII("test.json"); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | scoped_refptr<JsonPrefStore> CreatePrefStore() { |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 974 | return base::MakeRefCounted<JsonPrefStore>(test_file_); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | // Return the ImportantFileWriter for a given JsonPrefStore. |
| 978 | ImportantFileWriter* GetImportantFileWriter(JsonPrefStore* pref_store) { |
| 979 | return &(pref_store->writer_); |
| 980 | } |
| 981 | |
| 982 | void TriggerFakeWriteForCallback(JsonPrefStore* pref_store, bool success) { |
| 983 | JsonPrefStore::PostWriteCallback( |
| 984 | base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback, |
| 985 | pref_store->AsWeakPtr()), |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 986 | base::Bind(&WriteCallbacksObserver::OnPostWrite, |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 987 | base::Unretained(&write_callback_observer_)), |
| 988 | base::SequencedTaskRunnerHandle::Get(), success); |
| 989 | } |
| 990 | |
| 991 | SuccessfulWriteReplyObserver successful_write_reply_observer_; |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 992 | WriteCallbacksObserver write_callback_observer_; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 993 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 994 | protected: |
| 995 | base::test::ScopedTaskEnvironment scoped_task_environment_{ |
| 996 | base::test::ScopedTaskEnvironment::MainThreadType::DEFAULT, |
| 997 | base::test::ScopedTaskEnvironment::ExecutionMode::QUEUED}; |
| 998 | |
| 999 | base::ScopedTempDir temp_dir_; |
| 1000 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1001 | private: |
| 1002 | base::FilePath test_file_; |
| 1003 | |
| 1004 | DISALLOW_COPY_AND_ASSIGN(JsonPrefStoreCallbackTest); |
| 1005 | }; |
| 1006 | |
Francois Doray | 5f54795 | 2017-07-25 21:02:05 | [diff] [blame] | 1007 | TEST_F(JsonPrefStoreCallbackTest, TestSerializeDataCallbacks) { |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1008 | base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
| 1009 | ASSERT_LT(0, |
| 1010 | base::WriteFile(input_file, kReadJson, arraysize(kReadJson) - 1)); |
| 1011 | |
| 1012 | std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
| 1013 | new InterceptingPrefFilter(write_callback_observer_.GetCallbackPair())); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1014 | auto pref_store = base::MakeRefCounted<JsonPrefStore>( |
Gabriel Charette | 1f53ca3 | 2018-08-07 21:31:17 | [diff] [blame] | 1015 | input_file, std::move(intercepting_pref_filter)); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1016 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
| 1017 | |
| 1018 | EXPECT_EQ(NOT_CALLED, |
| 1019 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 1020 | pref_store->SetValue("normal", std::make_unique<base::Value>("normal"), |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1021 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 1022 | file_writer->DoScheduledWrite(); |
| 1023 | |
| 1024 | // The observer should not be invoked right away. |
| 1025 | EXPECT_FALSE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
| 1026 | EXPECT_EQ(NOT_CALLED, |
| 1027 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
| 1028 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1029 | scoped_task_environment_.RunUntilIdle(); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1030 | |
| 1031 | EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
| 1032 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
| 1033 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
| 1034 | } |
| 1035 | |
| 1036 | TEST_F(JsonPrefStoreCallbackTest, TestPostWriteCallbacks) { |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1037 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
| 1038 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
| 1039 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1040 | // Test RegisterOnNextWriteSynchronousCallbacks after |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1041 | // RegisterOnNextSuccessfulWriteReply. |
| 1042 | successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); |
| 1043 | write_callback_observer_.ObserveNextWriteCallback(pref_store.get()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 1044 | file_writer->WriteNow(std::make_unique<std::string>("foo")); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1045 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1046 | EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1047 | EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
| 1048 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
| 1049 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1050 | |
| 1051 | // Test RegisterOnNextSuccessfulWriteReply after |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1052 | // RegisterOnNextWriteSynchronousCallbacks. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1053 | successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); |
| 1054 | write_callback_observer_.ObserveNextWriteCallback(pref_store.get()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 1055 | file_writer->WriteNow(std::make_unique<std::string>("foo")); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1056 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1057 | EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1058 | EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
| 1059 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
| 1060 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1061 | |
| 1062 | // Test RegisterOnNextSuccessfulWriteReply only. |
| 1063 | successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 1064 | file_writer->WriteNow(std::make_unique<std::string>("foo")); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1065 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1066 | EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1067 | EXPECT_FALSE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
| 1068 | EXPECT_EQ(NOT_CALLED, |
| 1069 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1070 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1071 | // Test RegisterOnNextWriteSynchronousCallbacks only. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1072 | write_callback_observer_.ObserveNextWriteCallback(pref_store.get()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 1073 | file_writer->WriteNow(std::make_unique<std::string>("foo")); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1074 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1075 | EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1076 | EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
| 1077 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
| 1078 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1079 | } |
| 1080 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1081 | TEST_F(JsonPrefStoreCallbackTest, TestPostWriteCallbacksWithFakeFailure) { |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1082 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
| 1083 | |
| 1084 | // Confirm that the observers are invoked. |
| 1085 | successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); |
| 1086 | TriggerFakeWriteForCallback(pref_store.get(), true); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1087 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1088 | EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); |
| 1089 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1090 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1091 | |
| 1092 | // Confirm that the observation states were reset. |
| 1093 | EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1094 | EXPECT_EQ(NOT_CALLED, |
| 1095 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1096 | |
| 1097 | // Confirm that re-installing the observers works for another write. |
| 1098 | successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); |
| 1099 | TriggerFakeWriteForCallback(pref_store.get(), true); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1100 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1101 | EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); |
| 1102 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1103 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1104 | |
| 1105 | // Confirm that the successful observer is not invoked by an unsuccessful |
| 1106 | // write, and that the synchronous observer is invoked. |
| 1107 | successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); |
| 1108 | TriggerFakeWriteForCallback(pref_store.get(), false); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1109 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1110 | EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); |
| 1111 | EXPECT_EQ(CALLED_WITH_ERROR, |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1112 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1113 | |
| 1114 | // Do a real write, and confirm that the successful observer was invoked after |
| 1115 | // being set by |PostWriteCallback| by the last TriggerFakeWriteCallback. |
| 1116 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 1117 | file_writer->WriteNow(std::make_unique<std::string>("foo")); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1118 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1119 | EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1120 | EXPECT_EQ(NOT_CALLED, |
| 1121 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1122 | } |
| 1123 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1124 | TEST_F(JsonPrefStoreCallbackTest, TestPostWriteCallbacksDuringProfileDeath) { |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1125 | // Create a JsonPrefStore and attach observers to it, then delete it by making |
| 1126 | // it go out of scope to simulate profile switch or Chrome shutdown. |
| 1127 | { |
| 1128 | scoped_refptr<JsonPrefStore> soon_out_of_scope_pref_store = |
| 1129 | CreatePrefStore(); |
| 1130 | ImportantFileWriter* file_writer = |
| 1131 | GetImportantFileWriter(soon_out_of_scope_pref_store.get()); |
| 1132 | successful_write_reply_observer_.ObserveNextWriteCallback( |
| 1133 | soon_out_of_scope_pref_store.get()); |
| 1134 | write_callback_observer_.ObserveNextWriteCallback( |
| 1135 | soon_out_of_scope_pref_store.get()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 1136 | file_writer->WriteNow(std::make_unique<std::string>("foo")); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1137 | } |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1138 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1139 | EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1140 | EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1141 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1142 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1143 | } |
| 1144 | |
vabr | 5d919d6 | 2016-09-30 08:55:36 | [diff] [blame] | 1145 | } // namespace base |