[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>( |
| 455 | input_file, base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()}), |
| 456 | std::move(intercepting_pref_filter)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 457 | |
| 458 | ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, |
| 459 | pref_store->ReadPrefs()); |
| 460 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 461 | |
| 462 | // The store shouldn't be considered initialized until the interceptor |
| 463 | // returns. |
| 464 | EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 465 | EXPECT_FALSE(pref_store->IsInitializationComplete()); |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 466 | EXPECT_FALSE(pref_store->GetValue(kHomePage, nullptr)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 467 | |
| 468 | raw_intercepting_pref_filter_->ReleasePrefs(); |
| 469 | |
| 470 | EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 471 | EXPECT_TRUE(pref_store->IsInitializationComplete()); |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 472 | EXPECT_TRUE(pref_store->GetValue(kHomePage, nullptr)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 473 | |
| 474 | // The JSON file looks like this: |
| 475 | // { |
| 476 | // "homepage": "http://www.cnn.com", |
| 477 | // "some_directory": "/usr/local/", |
| 478 | // "tabs": { |
| 479 | // "new_windows_in_tabs": true, |
| 480 | // "max_tabs": 20 |
| 481 | // } |
| 482 | // } |
| 483 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 484 | RunBasicJsonPrefStoreTest(pref_store.get(), input_file, GetParam(), |
| 485 | &scoped_task_environment_); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 486 | } |
| 487 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 488 | TEST_P(JsonPrefStoreTest, ReadAsyncWithInterceptor) { |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 489 | base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 490 | ASSERT_LT(0, base::WriteFile(input_file, |
| 491 | kReadJson, arraysize(kReadJson) - 1)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 492 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 493 | std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 494 | new InterceptingPrefFilter()); |
| 495 | InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
| 496 | intercepting_pref_filter.get(); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 497 | auto pref_store = base::MakeRefCounted<JsonPrefStore>( |
| 498 | input_file, base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()}), |
| 499 | std::move(intercepting_pref_filter)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 500 | |
| 501 | MockPrefStoreObserver mock_observer; |
| 502 | pref_store->AddObserver(&mock_observer); |
| 503 | |
| 504 | // Ownership of the |mock_error_delegate| is handed to the |pref_store| below. |
| 505 | MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 506 | |
| 507 | { |
| 508 | pref_store->ReadPrefsAsync(mock_error_delegate); |
| 509 | |
| 510 | EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(0); |
| 511 | // EXPECT_CALL(*mock_error_delegate, |
| 512 | // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 513 | scoped_task_environment_.RunUntilIdle(); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 514 | |
| 515 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 516 | EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 517 | EXPECT_FALSE(pref_store->IsInitializationComplete()); |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 518 | EXPECT_FALSE(pref_store->GetValue(kHomePage, nullptr)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | { |
| 522 | EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 523 | // EXPECT_CALL(*mock_error_delegate, |
| 524 | // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
| 525 | |
| 526 | raw_intercepting_pref_filter_->ReleasePrefs(); |
| 527 | |
| 528 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 529 | EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 530 | EXPECT_TRUE(pref_store->IsInitializationComplete()); |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 531 | EXPECT_TRUE(pref_store->GetValue(kHomePage, nullptr)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | pref_store->RemoveObserver(&mock_observer); |
| 535 | |
| 536 | // The JSON file looks like this: |
| 537 | // { |
| 538 | // "homepage": "http://www.cnn.com", |
| 539 | // "some_directory": "/usr/local/", |
| 540 | // "tabs": { |
| 541 | // "new_windows_in_tabs": true, |
| 542 | // "max_tabs": 20 |
| 543 | // } |
| 544 | // } |
| 545 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 546 | RunBasicJsonPrefStoreTest(pref_store.get(), input_file, GetParam(), |
| 547 | &scoped_task_environment_); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 548 | } |
| 549 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame^] | 550 | TEST_P(JsonPrefStoreTest, WriteCountHistogramTestBasic) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 551 | base::HistogramTester histogram_tester; |
| 552 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 553 | SimpleTestClock* test_clock = new SimpleTestClock; |
| 554 | SetCurrentTimeInMinutes(0, test_clock); |
| 555 | JsonPrefStore::WriteCountHistogram histogram( |
| 556 | base::TimeDelta::FromSeconds(10), |
| 557 | base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 558 | std::unique_ptr<base::Clock>(test_clock)); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 559 | int32_t report_interval = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 560 | JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 561 | |
| 562 | histogram.RecordWriteOccured(); |
| 563 | |
| 564 | SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 565 | histogram.ReportOutstandingWrites(); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 566 | std::unique_ptr<HistogramSamples> samples = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 567 | histogram.GetHistogram()->SnapshotSamples(); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 568 | |
| 569 | std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
| 570 | histogram_tester.ExpectBucketCount(histogram_name, 1, 1); |
| 571 | histogram_tester.ExpectTotalCount(histogram_name, 1); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 572 | |
| 573 | ASSERT_EQ("Settings.JsonDataWriteCount.Local_State", |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 574 | std::string(histogram.GetHistogram()->histogram_name())); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 575 | ASSERT_TRUE(histogram.GetHistogram()->HasConstructionArguments(1, 30, 31)); |
| 576 | } |
| 577 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame^] | 578 | TEST_P(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 579 | base::HistogramTester histogram_tester; |
| 580 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 581 | SimpleTestClock* test_clock = new SimpleTestClock; |
| 582 | SetCurrentTimeInMinutes(0, test_clock); |
| 583 | JsonPrefStore::WriteCountHistogram histogram( |
| 584 | base::TimeDelta::FromSeconds(10), |
| 585 | base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 586 | std::unique_ptr<base::Clock>(test_clock)); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 587 | int32_t report_interval = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 588 | JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 589 | |
| 590 | histogram.RecordWriteOccured(); |
| 591 | SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
| 592 | histogram.RecordWriteOccured(); |
| 593 | SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
| 594 | histogram.RecordWriteOccured(); |
| 595 | |
| 596 | // Nothing should be recorded until the report period has elapsed. |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 597 | std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
| 598 | histogram_tester.ExpectTotalCount(histogram_name, 0); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 599 | |
| 600 | SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); |
| 601 | histogram.RecordWriteOccured(); |
| 602 | |
| 603 | // Now the report period has elapsed. |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 604 | histogram_tester.ExpectBucketCount(histogram_name, 3, 1); |
| 605 | histogram_tester.ExpectTotalCount(histogram_name, 1); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 606 | |
| 607 | // The last write won't be recorded because the second count period hasn't |
| 608 | // fully elapsed. |
| 609 | SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 610 | histogram.ReportOutstandingWrites(); |
| 611 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 612 | histogram_tester.ExpectBucketCount(histogram_name, 3, 1); |
| 613 | histogram_tester.ExpectTotalCount(histogram_name, 1); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 614 | } |
| 615 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame^] | 616 | TEST_P(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 617 | base::HistogramTester histogram_tester; |
| 618 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 619 | SimpleTestClock* test_clock = new SimpleTestClock; |
| 620 | SetCurrentTimeInMinutes(0, test_clock); |
| 621 | JsonPrefStore::WriteCountHistogram histogram( |
| 622 | base::TimeDelta::FromSeconds(10), |
| 623 | base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 624 | std::unique_ptr<base::Clock>(test_clock)); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 625 | int32_t report_interval = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 626 | JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 627 | |
| 628 | histogram.RecordWriteOccured(); |
| 629 | SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
| 630 | histogram.RecordWriteOccured(); |
| 631 | SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
| 632 | histogram.RecordWriteOccured(); |
| 633 | SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); |
| 634 | histogram.RecordWriteOccured(); |
| 635 | SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 636 | histogram.RecordWriteOccured(); |
| 637 | SetCurrentTimeInMinutes(2.1 * report_interval, test_clock); |
| 638 | histogram.RecordWriteOccured(); |
| 639 | SetCurrentTimeInMinutes(2.5 * report_interval, test_clock); |
| 640 | histogram.RecordWriteOccured(); |
| 641 | SetCurrentTimeInMinutes(2.7 * report_interval, test_clock); |
| 642 | histogram.RecordWriteOccured(); |
| 643 | SetCurrentTimeInMinutes(3.3 * report_interval, test_clock); |
| 644 | histogram.RecordWriteOccured(); |
| 645 | |
| 646 | // The last write won't be recorded because the second count period hasn't |
| 647 | // fully elapsed |
| 648 | SetCurrentTimeInMinutes(3.5 * report_interval, test_clock); |
| 649 | histogram.ReportOutstandingWrites(); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 650 | std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
| 651 | histogram_tester.ExpectBucketCount(histogram_name, 3, 2); |
| 652 | histogram_tester.ExpectBucketCount(histogram_name, 2, 1); |
| 653 | histogram_tester.ExpectTotalCount(histogram_name, 3); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 654 | } |
| 655 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame^] | 656 | TEST_P(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 657 | base::HistogramTester histogram_tester; |
| 658 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 659 | SimpleTestClock* test_clock = new SimpleTestClock; |
| 660 | SetCurrentTimeInMinutes(0, test_clock); |
| 661 | JsonPrefStore::WriteCountHistogram histogram( |
| 662 | base::TimeDelta::FromSeconds(10), |
| 663 | base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 664 | std::unique_ptr<base::Clock>(test_clock)); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 665 | int32_t report_interval = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 666 | JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 667 | |
| 668 | // 1 write in the first period. |
| 669 | histogram.RecordWriteOccured(); |
| 670 | |
| 671 | // No writes in the second and third periods. |
| 672 | |
| 673 | // 2 writes in the fourth period. |
| 674 | SetCurrentTimeInMinutes(3.1 * report_interval, test_clock); |
| 675 | histogram.RecordWriteOccured(); |
| 676 | SetCurrentTimeInMinutes(3.3 * report_interval, test_clock); |
| 677 | histogram.RecordWriteOccured(); |
| 678 | |
| 679 | // No writes in the fifth period. |
| 680 | |
| 681 | // 3 writes in the sixth period. |
| 682 | SetCurrentTimeInMinutes(5.1 * report_interval, test_clock); |
| 683 | histogram.RecordWriteOccured(); |
| 684 | SetCurrentTimeInMinutes(5.3 * report_interval, test_clock); |
| 685 | histogram.RecordWriteOccured(); |
| 686 | SetCurrentTimeInMinutes(5.5 * report_interval, test_clock); |
| 687 | histogram.RecordWriteOccured(); |
| 688 | |
| 689 | SetCurrentTimeInMinutes(6.1 * report_interval, test_clock); |
| 690 | histogram.ReportOutstandingWrites(); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 691 | std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
| 692 | histogram_tester.ExpectBucketCount(histogram_name, 0, 3); |
| 693 | histogram_tester.ExpectBucketCount(histogram_name, 1, 1); |
| 694 | histogram_tester.ExpectBucketCount(histogram_name, 2, 1); |
| 695 | histogram_tester.ExpectBucketCount(histogram_name, 3, 1); |
| 696 | histogram_tester.ExpectTotalCount(histogram_name, 6); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 697 | } |
| 698 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 699 | INSTANTIATE_TEST_CASE_P( |
| 700 | WithoutCallback, |
| 701 | JsonPrefStoreTest, |
| 702 | ::testing::Values(CommitPendingWriteMode::WITHOUT_CALLBACK)); |
| 703 | INSTANTIATE_TEST_CASE_P( |
| 704 | WithCallback, |
| 705 | JsonPrefStoreTest, |
| 706 | ::testing::Values(CommitPendingWriteMode::WITH_CALLBACK)); |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame^] | 707 | INSTANTIATE_TEST_CASE_P( |
| 708 | WithSynchronousCallback, |
| 709 | JsonPrefStoreTest, |
| 710 | ::testing::Values(CommitPendingWriteMode::WITH_SYNCHRONOUS_CALLBACK)); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 711 | |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 712 | class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 713 | public: |
| 714 | JsonPrefStoreLossyWriteTest() = default; |
| 715 | |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 716 | protected: |
| 717 | void SetUp() override { |
| 718 | JsonPrefStoreTest::SetUp(); |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 719 | test_file_ = temp_dir_.GetPath().AppendASCII("test.json"); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 720 | } |
| 721 | |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 722 | scoped_refptr<JsonPrefStore> CreatePrefStore() { |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 723 | return base::MakeRefCounted<JsonPrefStore>(test_file_); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | // Return the ImportantFileWriter for a given JsonPrefStore. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 727 | ImportantFileWriter* GetImportantFileWriter(JsonPrefStore* pref_store) { |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 728 | return &(pref_store->writer_); |
| 729 | } |
| 730 | |
| 731 | // Get the contents of kTestFile. Pumps the message loop before returning the |
| 732 | // result. |
| 733 | std::string GetTestFileContents() { |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 734 | scoped_task_environment_.RunUntilIdle(); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 735 | std::string file_contents; |
| 736 | ReadFileToString(test_file_, &file_contents); |
| 737 | return file_contents; |
| 738 | } |
| 739 | |
| 740 | private: |
| 741 | base::FilePath test_file_; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 742 | |
| 743 | DISALLOW_COPY_AND_ASSIGN(JsonPrefStoreLossyWriteTest); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 744 | }; |
| 745 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame^] | 746 | TEST_P(JsonPrefStoreLossyWriteTest, LossyWriteBasic) { |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 747 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 748 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 749 | |
| 750 | // Set a normal pref and check that it gets scheduled to be written. |
| 751 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 752 | pref_store->SetValue("normal", std::make_unique<base::Value>("normal"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 753 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 754 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 755 | file_writer->DoScheduledWrite(); |
| 756 | ASSERT_EQ("{\"normal\":\"normal\"}", GetTestFileContents()); |
| 757 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 758 | |
| 759 | // Set a lossy pref and check that it is not scheduled to be written. |
| 760 | // SetValue/RemoveValue. |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 761 | pref_store->SetValue("lossy", std::make_unique<base::Value>("lossy"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 762 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 763 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 764 | pref_store->RemoveValue("lossy", WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 765 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 766 | |
| 767 | // SetValueSilently/RemoveValueSilently. |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 768 | pref_store->SetValueSilently("lossy", std::make_unique<base::Value>("lossy"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 769 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 770 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 771 | pref_store->RemoveValueSilently("lossy", |
| 772 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 773 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 774 | |
| 775 | // ReportValueChanged. |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 776 | pref_store->SetValue("lossy", std::make_unique<base::Value>("lossy"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 777 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 778 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 779 | pref_store->ReportValueChanged("lossy", |
| 780 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 781 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 782 | |
| 783 | // Call CommitPendingWrite and check that the lossy pref and the normal pref |
| 784 | // are there with the last values set above. |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 785 | pref_store->CommitPendingWrite(base::OnceClosure()); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 786 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 787 | ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
| 788 | GetTestFileContents()); |
| 789 | } |
| 790 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame^] | 791 | TEST_P(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossyFirst) { |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 792 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 793 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 794 | |
| 795 | // Set a lossy pref and check that it is not scheduled to be written. |
| 796 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 797 | pref_store->SetValue("lossy", std::make_unique<base::Value>("lossy"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 798 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 799 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 800 | |
| 801 | // Set a normal pref and check that it is scheduled to be written. |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 802 | pref_store->SetValue("normal", std::make_unique<base::Value>("normal"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 803 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 804 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 805 | |
| 806 | // Call DoScheduledWrite and check both prefs get written. |
| 807 | file_writer->DoScheduledWrite(); |
| 808 | ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
| 809 | GetTestFileContents()); |
| 810 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 811 | } |
| 812 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame^] | 813 | TEST_P(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossySecond) { |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 814 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 815 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 816 | |
| 817 | // Set a normal pref and check that it is scheduled to be written. |
| 818 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 819 | pref_store->SetValue("normal", std::make_unique<base::Value>("normal"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 820 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 821 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 822 | |
| 823 | // Set a lossy pref and check that the write is still scheduled. |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 824 | pref_store->SetValue("lossy", std::make_unique<base::Value>("lossy"), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 825 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 826 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 827 | |
| 828 | // Call DoScheduledWrite and check both prefs get written. |
| 829 | file_writer->DoScheduledWrite(); |
| 830 | ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
| 831 | GetTestFileContents()); |
| 832 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 833 | } |
| 834 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame^] | 835 | TEST_P(JsonPrefStoreLossyWriteTest, ScheduleLossyWrite) { |
benwells | 2673059 | 2015-05-28 13:08:08 | [diff] [blame] | 836 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 837 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
benwells | 2673059 | 2015-05-28 13:08:08 | [diff] [blame] | 838 | |
| 839 | // 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] | 840 | pref_store->SetValue("lossy", std::make_unique<base::Value>("lossy"), |
benwells | 2673059 | 2015-05-28 13:08:08 | [diff] [blame] | 841 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 842 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 843 | |
| 844 | // Schedule pending lossy writes and check that it is scheduled. |
| 845 | pref_store->SchedulePendingLossyWrites(); |
| 846 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 847 | |
| 848 | // Call CommitPendingWrite and check that the lossy pref is there with the |
| 849 | // last value set above. |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 850 | pref_store->CommitPendingWrite(base::OnceClosure()); |
benwells | 2673059 | 2015-05-28 13:08:08 | [diff] [blame] | 851 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 852 | ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); |
| 853 | } |
| 854 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame^] | 855 | INSTANTIATE_TEST_CASE_P( |
| 856 | WithoutCallback, |
| 857 | JsonPrefStoreLossyWriteTest, |
| 858 | ::testing::Values(CommitPendingWriteMode::WITHOUT_CALLBACK)); |
| 859 | INSTANTIATE_TEST_CASE_P( |
| 860 | WithReply, |
| 861 | JsonPrefStoreLossyWriteTest, |
| 862 | ::testing::Values(CommitPendingWriteMode::WITH_CALLBACK)); |
| 863 | INSTANTIATE_TEST_CASE_P( |
| 864 | WithNotify, |
| 865 | JsonPrefStoreLossyWriteTest, |
| 866 | ::testing::Values(CommitPendingWriteMode::WITH_SYNCHRONOUS_CALLBACK)); |
| 867 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 868 | class SuccessfulWriteReplyObserver { |
| 869 | public: |
| 870 | SuccessfulWriteReplyObserver() = default; |
| 871 | |
| 872 | // Returns true if a successful write was observed via on_successful_write() |
| 873 | // and resets the observation state to false regardless. |
| 874 | bool GetAndResetObservationState() { |
| 875 | bool was_successful_write_observed = successful_write_reply_observed_; |
| 876 | successful_write_reply_observed_ = false; |
| 877 | return was_successful_write_observed; |
| 878 | } |
| 879 | |
| 880 | // Register OnWrite() to be called on the next write of |json_pref_store|. |
| 881 | void ObserveNextWriteCallback(JsonPrefStore* json_pref_store); |
| 882 | |
| 883 | void OnSuccessfulWrite() { |
| 884 | EXPECT_FALSE(successful_write_reply_observed_); |
| 885 | successful_write_reply_observed_ = true; |
| 886 | } |
| 887 | |
| 888 | private: |
| 889 | bool successful_write_reply_observed_ = false; |
| 890 | |
| 891 | DISALLOW_COPY_AND_ASSIGN(SuccessfulWriteReplyObserver); |
| 892 | }; |
| 893 | |
| 894 | void SuccessfulWriteReplyObserver::ObserveNextWriteCallback( |
| 895 | JsonPrefStore* json_pref_store) { |
| 896 | json_pref_store->RegisterOnNextSuccessfulWriteReply( |
| 897 | base::Bind(&SuccessfulWriteReplyObserver::OnSuccessfulWrite, |
| 898 | base::Unretained(this))); |
| 899 | } |
| 900 | |
| 901 | enum WriteCallbackObservationState { |
| 902 | NOT_CALLED, |
| 903 | CALLED_WITH_ERROR, |
| 904 | CALLED_WITH_SUCCESS, |
| 905 | }; |
| 906 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 907 | class WriteCallbacksObserver { |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 908 | public: |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 909 | WriteCallbacksObserver() = default; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 910 | |
| 911 | // Register OnWrite() to be called on the next write of |json_pref_store|. |
| 912 | void ObserveNextWriteCallback(JsonPrefStore* json_pref_store); |
| 913 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 914 | // Returns whether OnPreWrite() was called, and resets the observation state |
| 915 | // to false. |
| 916 | bool GetAndResetPreWriteObservationState(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 917 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 918 | // Returns the |WriteCallbackObservationState| which was observed, then resets |
| 919 | // it to |NOT_CALLED|. |
| 920 | WriteCallbackObservationState GetAndResetPostWriteObservationState(); |
| 921 | |
| 922 | JsonPrefStore::OnWriteCallbackPair GetCallbackPair() { |
| 923 | return std::make_pair( |
| 924 | base::Bind(&WriteCallbacksObserver::OnPreWrite, base::Unretained(this)), |
| 925 | base::Bind(&WriteCallbacksObserver::OnPostWrite, |
| 926 | base::Unretained(this))); |
| 927 | } |
| 928 | |
| 929 | void OnPreWrite() { |
| 930 | EXPECT_FALSE(pre_write_called_); |
| 931 | pre_write_called_ = true; |
| 932 | } |
| 933 | |
| 934 | void OnPostWrite(bool success) { |
| 935 | EXPECT_EQ(NOT_CALLED, post_write_observation_state_); |
| 936 | post_write_observation_state_ = |
| 937 | success ? CALLED_WITH_SUCCESS : CALLED_WITH_ERROR; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | private: |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 941 | bool pre_write_called_ = false; |
| 942 | WriteCallbackObservationState post_write_observation_state_ = NOT_CALLED; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 943 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 944 | DISALLOW_COPY_AND_ASSIGN(WriteCallbacksObserver); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 945 | }; |
| 946 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 947 | void WriteCallbacksObserver::ObserveNextWriteCallback(JsonPrefStore* writer) { |
| 948 | writer->RegisterOnNextWriteSynchronousCallbacks(GetCallbackPair()); |
| 949 | } |
| 950 | |
| 951 | bool WriteCallbacksObserver::GetAndResetPreWriteObservationState() { |
| 952 | bool observation_state = pre_write_called_; |
| 953 | pre_write_called_ = false; |
| 954 | return observation_state; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | WriteCallbackObservationState |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 958 | WriteCallbacksObserver::GetAndResetPostWriteObservationState() { |
| 959 | WriteCallbackObservationState state = post_write_observation_state_; |
| 960 | pre_write_called_ = false; |
| 961 | post_write_observation_state_ = NOT_CALLED; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 962 | return state; |
| 963 | } |
| 964 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame^] | 965 | class JsonPrefStoreCallbackTest : public testing::Test { |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 966 | public: |
| 967 | JsonPrefStoreCallbackTest() = default; |
| 968 | |
| 969 | protected: |
| 970 | void SetUp() override { |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame^] | 971 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
vabr | 5d919d6 | 2016-09-30 08:55:36 | [diff] [blame] | 972 | test_file_ = temp_dir_.GetPath().AppendASCII("test.json"); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | scoped_refptr<JsonPrefStore> CreatePrefStore() { |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 976 | return base::MakeRefCounted<JsonPrefStore>(test_file_); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | // Return the ImportantFileWriter for a given JsonPrefStore. |
| 980 | ImportantFileWriter* GetImportantFileWriter(JsonPrefStore* pref_store) { |
| 981 | return &(pref_store->writer_); |
| 982 | } |
| 983 | |
| 984 | void TriggerFakeWriteForCallback(JsonPrefStore* pref_store, bool success) { |
| 985 | JsonPrefStore::PostWriteCallback( |
| 986 | base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback, |
| 987 | pref_store->AsWeakPtr()), |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 988 | base::Bind(&WriteCallbacksObserver::OnPostWrite, |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 989 | base::Unretained(&write_callback_observer_)), |
| 990 | base::SequencedTaskRunnerHandle::Get(), success); |
| 991 | } |
| 992 | |
| 993 | SuccessfulWriteReplyObserver successful_write_reply_observer_; |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 994 | WriteCallbacksObserver write_callback_observer_; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 995 | |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame^] | 996 | protected: |
| 997 | base::test::ScopedTaskEnvironment scoped_task_environment_{ |
| 998 | base::test::ScopedTaskEnvironment::MainThreadType::DEFAULT, |
| 999 | base::test::ScopedTaskEnvironment::ExecutionMode::QUEUED}; |
| 1000 | |
| 1001 | base::ScopedTempDir temp_dir_; |
| 1002 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1003 | private: |
| 1004 | base::FilePath test_file_; |
| 1005 | |
| 1006 | DISALLOW_COPY_AND_ASSIGN(JsonPrefStoreCallbackTest); |
| 1007 | }; |
| 1008 | |
Francois Doray | 5f54795 | 2017-07-25 21:02:05 | [diff] [blame] | 1009 | TEST_F(JsonPrefStoreCallbackTest, TestSerializeDataCallbacks) { |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1010 | base::FilePath input_file = temp_dir_.GetPath().AppendASCII("write.json"); |
| 1011 | ASSERT_LT(0, |
| 1012 | base::WriteFile(input_file, kReadJson, arraysize(kReadJson) - 1)); |
| 1013 | |
| 1014 | std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
| 1015 | new InterceptingPrefFilter(write_callback_observer_.GetCallbackPair())); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1016 | auto pref_store = base::MakeRefCounted<JsonPrefStore>( |
| 1017 | input_file, base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()}), |
| 1018 | std::move(intercepting_pref_filter)); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1019 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
| 1020 | |
| 1021 | EXPECT_EQ(NOT_CALLED, |
| 1022 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 1023 | pref_store->SetValue("normal", std::make_unique<base::Value>("normal"), |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1024 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 1025 | file_writer->DoScheduledWrite(); |
| 1026 | |
| 1027 | // The observer should not be invoked right away. |
| 1028 | EXPECT_FALSE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
| 1029 | EXPECT_EQ(NOT_CALLED, |
| 1030 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
| 1031 | |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1032 | scoped_task_environment_.RunUntilIdle(); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1033 | |
| 1034 | EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
| 1035 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
| 1036 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
| 1037 | } |
| 1038 | |
| 1039 | TEST_F(JsonPrefStoreCallbackTest, TestPostWriteCallbacks) { |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1040 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
| 1041 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
| 1042 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1043 | // Test RegisterOnNextWriteSynchronousCallbacks after |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1044 | // RegisterOnNextSuccessfulWriteReply. |
| 1045 | successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); |
| 1046 | write_callback_observer_.ObserveNextWriteCallback(pref_store.get()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 1047 | file_writer->WriteNow(std::make_unique<std::string>("foo")); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1048 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1049 | EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1050 | EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
| 1051 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
| 1052 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1053 | |
| 1054 | // Test RegisterOnNextSuccessfulWriteReply after |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1055 | // RegisterOnNextWriteSynchronousCallbacks. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1056 | successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); |
| 1057 | write_callback_observer_.ObserveNextWriteCallback(pref_store.get()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 1058 | file_writer->WriteNow(std::make_unique<std::string>("foo")); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1059 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1060 | EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1061 | EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
| 1062 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
| 1063 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1064 | |
| 1065 | // Test RegisterOnNextSuccessfulWriteReply only. |
| 1066 | successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 1067 | file_writer->WriteNow(std::make_unique<std::string>("foo")); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1068 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1069 | EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1070 | EXPECT_FALSE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
| 1071 | EXPECT_EQ(NOT_CALLED, |
| 1072 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1073 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1074 | // Test RegisterOnNextWriteSynchronousCallbacks only. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1075 | write_callback_observer_.ObserveNextWriteCallback(pref_store.get()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 1076 | file_writer->WriteNow(std::make_unique<std::string>("foo")); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1077 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1078 | EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1079 | EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
| 1080 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
| 1081 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1082 | } |
| 1083 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1084 | TEST_F(JsonPrefStoreCallbackTest, TestPostWriteCallbacksWithFakeFailure) { |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1085 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
| 1086 | |
| 1087 | // Confirm that the observers are invoked. |
| 1088 | successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); |
| 1089 | TriggerFakeWriteForCallback(pref_store.get(), true); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1090 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1091 | EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); |
| 1092 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1093 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1094 | |
| 1095 | // Confirm that the observation states were reset. |
| 1096 | EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1097 | EXPECT_EQ(NOT_CALLED, |
| 1098 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1099 | |
| 1100 | // Confirm that re-installing the observers works for another write. |
| 1101 | successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); |
| 1102 | TriggerFakeWriteForCallback(pref_store.get(), true); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1103 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1104 | EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); |
| 1105 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1106 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1107 | |
| 1108 | // Confirm that the successful observer is not invoked by an unsuccessful |
| 1109 | // write, and that the synchronous observer is invoked. |
| 1110 | successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); |
| 1111 | TriggerFakeWriteForCallback(pref_store.get(), false); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1112 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1113 | EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); |
| 1114 | EXPECT_EQ(CALLED_WITH_ERROR, |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1115 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1116 | |
| 1117 | // Do a real write, and confirm that the successful observer was invoked after |
| 1118 | // being set by |PostWriteCallback| by the last TriggerFakeWriteCallback. |
| 1119 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 1120 | file_writer->WriteNow(std::make_unique<std::string>("foo")); |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1121 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1122 | EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1123 | EXPECT_EQ(NOT_CALLED, |
| 1124 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1125 | } |
| 1126 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1127 | TEST_F(JsonPrefStoreCallbackTest, TestPostWriteCallbacksDuringProfileDeath) { |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1128 | // Create a JsonPrefStore and attach observers to it, then delete it by making |
| 1129 | // it go out of scope to simulate profile switch or Chrome shutdown. |
| 1130 | { |
| 1131 | scoped_refptr<JsonPrefStore> soon_out_of_scope_pref_store = |
| 1132 | CreatePrefStore(); |
| 1133 | ImportantFileWriter* file_writer = |
| 1134 | GetImportantFileWriter(soon_out_of_scope_pref_store.get()); |
| 1135 | successful_write_reply_observer_.ObserveNextWriteCallback( |
| 1136 | soon_out_of_scope_pref_store.get()); |
| 1137 | write_callback_observer_.ObserveNextWriteCallback( |
| 1138 | soon_out_of_scope_pref_store.get()); |
Jinho Bang | 84b58bd | 2018-01-01 21:44:48 | [diff] [blame] | 1139 | file_writer->WriteNow(std::make_unique<std::string>("foo")); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1140 | } |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 1141 | scoped_task_environment_.RunUntilIdle(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1142 | EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1143 | EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1144 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 1145 | write_callback_observer_.GetAndResetPostWriteObservationState()); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 1146 | } |
| 1147 | |
vabr | 5d919d6 | 2016-09-30 08:55:36 | [diff] [blame] | 1148 | } // namespace base |