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