[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" |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 19 | #include "base/message_loop/message_loop.h" |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 20 | #include "base/metrics/histogram_samples.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 21 | #include "base/path_service.h" |
[email protected] | f7b98b3 | 2013-02-05 08:14:15 | [diff] [blame] | 22 | #include "base/run_loop.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 23 | #include "base/single_thread_task_runner.h" |
[email protected] | dfa049e | 2013-02-07 02:57:22 | [diff] [blame] | 24 | #include "base/strings/string_number_conversions.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 25 | #include "base/strings/string_util.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 26 | #include "base/strings/utf_string_conversions.h" |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 27 | #include "base/test/histogram_tester.h" |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 28 | #include "base/test/simple_test_clock.h" |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 29 | #include "base/threading/sequenced_worker_pool.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 30 | #include "base/threading/thread.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 31 | #include "base/values.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 32 | #include "components/prefs/pref_filter.h" |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 33 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 34 | #include "testing/gtest/include/gtest/gtest.h" |
| 35 | |
[email protected] | 7e3ec42c | 2012-12-16 05:13:21 | [diff] [blame] | 36 | namespace base { |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 37 | namespace { |
| 38 | |
[email protected] | 5bfdcfd | 2012-11-22 22:08:24 | [diff] [blame] | 39 | const char kHomePage[] = "homepage"; |
| 40 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 41 | const char kReadJson[] = |
| 42 | "{\n" |
| 43 | " \"homepage\": \"http://www.cnn.com\",\n" |
| 44 | " \"some_directory\": \"/usr/local/\",\n" |
| 45 | " \"tabs\": {\n" |
| 46 | " \"new_windows_in_tabs\": true,\n" |
| 47 | " \"max_tabs\": 20\n" |
| 48 | " }\n" |
| 49 | "}"; |
| 50 | |
| 51 | const char kInvalidJson[] = "!@#$%^&"; |
| 52 | |
| 53 | // Expected output for tests using RunBasicJsonPrefStoreTest(). |
| 54 | const char kWriteGolden[] = |
| 55 | "{\"homepage\":\"http://www.cnn.com\"," |
| 56 | "\"long_int\":{\"pref\":\"214748364842\"}," |
| 57 | "\"some_directory\":\"/usr/sbin/\"," |
| 58 | "\"tabs\":{\"max_tabs\":10,\"new_windows_in_tabs\":false}}"; |
| 59 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 60 | // Set the time on the given SimpleTestClock to the given time in minutes. |
| 61 | void SetCurrentTimeInMinutes(double minutes, base::SimpleTestClock* clock) { |
| 62 | const int32_t kBaseTimeMins = 100; |
| 63 | clock->SetNow(base::Time::FromDoubleT((kBaseTimeMins + minutes) * 60)); |
| 64 | } |
| 65 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 66 | // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on |
| 67 | // to the |prefs| until explicitly asked to release them. |
| 68 | class InterceptingPrefFilter : public PrefFilter { |
| 69 | public: |
| 70 | InterceptingPrefFilter(); |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 71 | ~InterceptingPrefFilter() override; |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 72 | |
| 73 | // PrefFilter implementation: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 74 | void FilterOnLoad( |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 75 | const PostFilterOnLoadCallback& post_filter_on_load_callback, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 76 | std::unique_ptr<base::DictionaryValue> pref_store_contents) override; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 77 | void FilterUpdate(const std::string& path) override {} |
| 78 | void FilterSerializeData( |
mostynb | 9e096de | 2014-10-07 17:59:11 | [diff] [blame] | 79 | base::DictionaryValue* pref_store_contents) override {} |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 80 | |
| 81 | bool has_intercepted_prefs() const { return intercepted_prefs_ != NULL; } |
| 82 | |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 83 | // Finalize an intercepted read, handing |intercepted_prefs_| back to its |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 84 | // JsonPrefStore. |
| 85 | void ReleasePrefs(); |
| 86 | |
| 87 | private: |
| 88 | PostFilterOnLoadCallback post_filter_on_load_callback_; |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 89 | std::unique_ptr<base::DictionaryValue> intercepted_prefs_; |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 90 | |
| 91 | DISALLOW_COPY_AND_ASSIGN(InterceptingPrefFilter); |
| 92 | }; |
| 93 | |
| 94 | InterceptingPrefFilter::InterceptingPrefFilter() {} |
| 95 | InterceptingPrefFilter::~InterceptingPrefFilter() {} |
| 96 | |
| 97 | void InterceptingPrefFilter::FilterOnLoad( |
| 98 | const PostFilterOnLoadCallback& post_filter_on_load_callback, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 99 | std::unique_ptr<base::DictionaryValue> pref_store_contents) { |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 100 | post_filter_on_load_callback_ = post_filter_on_load_callback; |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 101 | intercepted_prefs_ = std::move(pref_store_contents); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void InterceptingPrefFilter::ReleasePrefs() { |
| 105 | EXPECT_FALSE(post_filter_on_load_callback_.is_null()); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 106 | post_filter_on_load_callback_.Run(std::move(intercepted_prefs_), false); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 107 | post_filter_on_load_callback_.Reset(); |
| 108 | } |
| 109 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 110 | class MockPrefStoreObserver : public PrefStore::Observer { |
| 111 | public: |
| 112 | MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); |
| 113 | MOCK_METHOD1(OnInitializationCompleted, void (bool)); |
| 114 | }; |
| 115 | |
| 116 | class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { |
| 117 | public: |
| 118 | MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); |
| 119 | }; |
| 120 | |
| 121 | } // namespace |
| 122 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 123 | class JsonPrefStoreTest : public testing::Test { |
| 124 | protected: |
dcheng | 8aef3761 | 2014-12-23 02:56:47 | [diff] [blame] | 125 | void SetUp() override { |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 126 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 127 | } |
| 128 | |
dcheng | 8aef3761 | 2014-12-23 02:56:47 | [diff] [blame] | 129 | void TearDown() override { |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 130 | // Make sure all pending tasks have been processed (e.g., deleting the |
| 131 | // JsonPrefStore may post write tasks). |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 132 | RunLoop().RunUntilIdle(); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 135 | // The path to temporary directory used to contain the test operations. |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 136 | base::ScopedTempDir temp_dir_; |
[email protected] | ea587b0 | 2010-05-21 15:01:35 | [diff] [blame] | 137 | // A message loop that we can use as the file thread message loop. |
| 138 | MessageLoop message_loop_; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | // Test fallback behavior for a nonexistent file. |
| 142 | TEST_F(JsonPrefStoreTest, NonExistentFile) { |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 143 | base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 144 | ASSERT_FALSE(PathExists(bogus_input_file)); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 145 | scoped_refptr<JsonPrefStore> pref_store = |
| 146 | new JsonPrefStore(bogus_input_file, message_loop_.task_runner(), |
| 147 | std::unique_ptr<PrefFilter>()); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 148 | EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 149 | pref_store->ReadPrefs()); |
| 150 | EXPECT_FALSE(pref_store->ReadOnly()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 151 | } |
| 152 | |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 153 | // Test fallback behavior for a nonexistent file and alternate file. |
| 154 | TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 155 | base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 156 | base::FilePath bogus_alternate_input_file = |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 157 | temp_dir_.path().AppendASCII("read_alternate.txt"); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 158 | ASSERT_FALSE(PathExists(bogus_input_file)); |
| 159 | ASSERT_FALSE(PathExists(bogus_alternate_input_file)); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 160 | scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 161 | bogus_input_file, bogus_alternate_input_file, message_loop_.task_runner(), |
| 162 | std::unique_ptr<PrefFilter>()); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 163 | EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 164 | pref_store->ReadPrefs()); |
| 165 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 166 | } |
| 167 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 168 | // Test fallback behavior for an invalid file. |
| 169 | TEST_F(JsonPrefStoreTest, InvalidFile) { |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 170 | base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 171 | ASSERT_LT(0, base::WriteFile(invalid_file, |
| 172 | kInvalidJson, arraysize(kInvalidJson) - 1)); |
| 173 | |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 174 | scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 175 | invalid_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 176 | EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 177 | pref_store->ReadPrefs()); |
| 178 | EXPECT_FALSE(pref_store->ReadOnly()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 179 | |
| 180 | // The file should have been moved aside. |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 181 | EXPECT_FALSE(PathExists(invalid_file)); |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 182 | base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 183 | EXPECT_TRUE(PathExists(moved_aside)); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 184 | |
| 185 | std::string moved_aside_contents; |
| 186 | ASSERT_TRUE(base::ReadFileToString(moved_aside, &moved_aside_contents)); |
| 187 | EXPECT_EQ(kInvalidJson, moved_aside_contents); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 188 | } |
| 189 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 190 | // This function is used to avoid code duplication while testing synchronous |
| 191 | // and asynchronous version of the JsonPrefStore loading. It validates that the |
| 192 | // given output file's contents matches kWriteGolden. |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 193 | void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store, |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 194 | const base::FilePath& output_file) { |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 195 | const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; |
| 196 | const char kMaxTabs[] = "tabs.max_tabs"; |
| 197 | const char kLongIntPref[] = "long_int.pref"; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 198 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 199 | std::string cnn("http://www.cnn.com"); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 200 | |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 201 | const Value* actual; |
[email protected] | 5bfdcfd | 2012-11-22 22:08:24 | [diff] [blame] | 202 | EXPECT_TRUE(pref_store->GetValue(kHomePage, &actual)); |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 203 | std::string string_value; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 204 | EXPECT_TRUE(actual->GetAsString(&string_value)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 205 | EXPECT_EQ(cnn, string_value); |
| 206 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 207 | const char kSomeDirectory[] = "some_directory"; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 208 | |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 209 | EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 210 | base::FilePath::StringType path; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 211 | EXPECT_TRUE(actual->GetAsString(&path)); |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 212 | EXPECT_EQ(base::FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); |
| 213 | base::FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 214 | |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 215 | pref_store->SetValue(kSomeDirectory, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 216 | base::WrapUnique(new StringValue(some_path.value())), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 217 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 218 | EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 219 | EXPECT_TRUE(actual->GetAsString(&path)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 220 | EXPECT_EQ(some_path.value(), path); |
| 221 | |
| 222 | // Test reading some other data types from sub-dictionaries. |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 223 | EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 224 | bool boolean = false; |
| 225 | EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 226 | EXPECT_TRUE(boolean); |
| 227 | |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 228 | pref_store->SetValue(kNewWindowsInTabs, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 229 | base::WrapUnique(new FundamentalValue(false)), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 230 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 231 | EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 232 | EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 233 | EXPECT_FALSE(boolean); |
| 234 | |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 235 | EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 236 | int integer = 0; |
| 237 | EXPECT_TRUE(actual->GetAsInteger(&integer)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 238 | EXPECT_EQ(20, integer); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 239 | pref_store->SetValue(kMaxTabs, base::WrapUnique(new FundamentalValue(10)), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 240 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 241 | EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 242 | EXPECT_TRUE(actual->GetAsInteger(&integer)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 243 | EXPECT_EQ(10, integer); |
| 244 | |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 245 | pref_store->SetValue( |
| 246 | kLongIntPref, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 247 | base::WrapUnique(new StringValue(base::Int64ToString(214748364842LL))), |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 248 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 249 | EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 250 | EXPECT_TRUE(actual->GetAsString(&string_value)); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 251 | int64_t value; |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 252 | base::StringToInt64(string_value, &value); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 253 | EXPECT_EQ(214748364842LL, value); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 254 | |
| 255 | // Serialize and compare to expected output. |
[email protected] | fbe17c8a | 2011-12-27 16:41:48 | [diff] [blame] | 256 | pref_store->CommitPendingWrite(); |
[email protected] | f7b98b3 | 2013-02-05 08:14:15 | [diff] [blame] | 257 | RunLoop().RunUntilIdle(); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 258 | |
| 259 | std::string output_contents; |
| 260 | ASSERT_TRUE(base::ReadFileToString(output_file, &output_contents)); |
| 261 | EXPECT_EQ(kWriteGolden, output_contents); |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 262 | ASSERT_TRUE(base::DeleteFile(output_file, false)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 263 | } |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 264 | |
| 265 | TEST_F(JsonPrefStoreTest, Basic) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 266 | base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 267 | ASSERT_LT(0, base::WriteFile(input_file, |
| 268 | kReadJson, arraysize(kReadJson) - 1)); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 269 | |
| 270 | // Test that the persistent value can be loaded. |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 271 | ASSERT_TRUE(PathExists(input_file)); |
[email protected] | 56cbcb3a | 2013-12-23 21:24:46 | [diff] [blame] | 272 | scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 273 | input_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 274 | ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 275 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 276 | EXPECT_TRUE(pref_store->IsInitializationComplete()); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 277 | |
| 278 | // The JSON file looks like this: |
| 279 | // { |
| 280 | // "homepage": "http://www.cnn.com", |
| 281 | // "some_directory": "/usr/local/", |
| 282 | // "tabs": { |
| 283 | // "new_windows_in_tabs": true, |
| 284 | // "max_tabs": 20 |
| 285 | // } |
| 286 | // } |
| 287 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 288 | RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | TEST_F(JsonPrefStoreTest, BasicAsync) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 292 | base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 293 | ASSERT_LT(0, base::WriteFile(input_file, |
| 294 | kReadJson, arraysize(kReadJson) - 1)); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 295 | |
| 296 | // Test that the persistent value can be loaded. |
[email protected] | 56cbcb3a | 2013-12-23 21:24:46 | [diff] [blame] | 297 | scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 298 | input_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 299 | |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 300 | { |
| 301 | MockPrefStoreObserver mock_observer; |
| 302 | pref_store->AddObserver(&mock_observer); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 303 | |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 304 | MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 305 | pref_store->ReadPrefsAsync(mock_error_delegate); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 306 | |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 307 | EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 308 | EXPECT_CALL(*mock_error_delegate, |
| 309 | OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 310 | RunLoop().RunUntilIdle(); |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 311 | pref_store->RemoveObserver(&mock_observer); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 312 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 313 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 314 | EXPECT_TRUE(pref_store->IsInitializationComplete()); |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 315 | } |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 316 | |
| 317 | // The JSON file looks like this: |
| 318 | // { |
| 319 | // "homepage": "http://www.cnn.com", |
| 320 | // "some_directory": "/usr/local/", |
| 321 | // "tabs": { |
| 322 | // "new_windows_in_tabs": true, |
| 323 | // "max_tabs": 20 |
| 324 | // } |
| 325 | // } |
| 326 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 327 | RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 328 | } |
| 329 | |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 330 | TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { |
| 331 | FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
| 332 | |
[email protected] | 56cbcb3a | 2013-12-23 21:24:46 | [diff] [blame] | 333 | scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 334 | pref_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 335 | |
| 336 | // Set some keys with empty values. |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 337 | pref_store->SetValue("list", base::WrapUnique(new base::ListValue), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 338 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 339 | pref_store->SetValue("dict", base::WrapUnique(new base::DictionaryValue), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 340 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 341 | |
| 342 | // Write to file. |
| 343 | pref_store->CommitPendingWrite(); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 344 | RunLoop().RunUntilIdle(); |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 345 | |
| 346 | // Reload. |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 347 | pref_store = new JsonPrefStore(pref_file, message_loop_.task_runner(), |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 348 | std::unique_ptr<PrefFilter>()); |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 349 | ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 350 | ASSERT_FALSE(pref_store->ReadOnly()); |
| 351 | |
| 352 | // Check values. |
| 353 | const Value* result = NULL; |
| 354 | EXPECT_TRUE(pref_store->GetValue("list", &result)); |
| 355 | EXPECT_TRUE(ListValue().Equals(result)); |
| 356 | EXPECT_TRUE(pref_store->GetValue("dict", &result)); |
| 357 | EXPECT_TRUE(DictionaryValue().Equals(result)); |
| 358 | } |
| 359 | |
[email protected] | eeedaa69 | 2014-01-30 09:22:27 | [diff] [blame] | 360 | // This test is just documenting some potentially non-obvious behavior. It |
| 361 | // shouldn't be taken as normative. |
| 362 | TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { |
| 363 | FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
| 364 | |
| 365 | scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 366 | pref_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
[email protected] | eeedaa69 | 2014-01-30 09:22:27 | [diff] [blame] | 367 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 368 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
[email protected] | eeedaa69 | 2014-01-30 09:22:27 | [diff] [blame] | 369 | dict->SetString("key", "value"); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 370 | pref_store->SetValue("dict", std::move(dict), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 371 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | eeedaa69 | 2014-01-30 09:22:27 | [diff] [blame] | 372 | |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 373 | pref_store->RemoveValue("dict.key", |
| 374 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | eeedaa69 | 2014-01-30 09:22:27 | [diff] [blame] | 375 | |
| 376 | const base::Value* retrieved_dict = NULL; |
| 377 | bool has_dict = pref_store->GetValue("dict", &retrieved_dict); |
| 378 | EXPECT_FALSE(has_dict); |
| 379 | } |
| 380 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 381 | // Tests asynchronous reading of the file when there is no file. |
| 382 | TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 383 | base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 384 | ASSERT_FALSE(PathExists(bogus_input_file)); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 385 | scoped_refptr<JsonPrefStore> pref_store = |
| 386 | new JsonPrefStore(bogus_input_file, message_loop_.task_runner(), |
| 387 | std::unique_ptr<PrefFilter>()); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 388 | MockPrefStoreObserver mock_observer; |
| 389 | pref_store->AddObserver(&mock_observer); |
| 390 | |
| 391 | MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
| 392 | pref_store->ReadPrefsAsync(mock_error_delegate); |
| 393 | |
| 394 | EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 395 | EXPECT_CALL(*mock_error_delegate, |
| 396 | OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 397 | RunLoop().RunUntilIdle(); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 398 | pref_store->RemoveObserver(&mock_observer); |
| 399 | |
| 400 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 401 | } |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 402 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 403 | TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 404 | base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 405 | ASSERT_LT(0, base::WriteFile(input_file, |
| 406 | kReadJson, arraysize(kReadJson) - 1)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 407 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 408 | std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 409 | new InterceptingPrefFilter()); |
| 410 | InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
| 411 | intercepting_pref_filter.get(); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 412 | scoped_refptr<JsonPrefStore> pref_store = |
| 413 | new JsonPrefStore(input_file, message_loop_.task_runner(), |
| 414 | std::move(intercepting_pref_filter)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 415 | |
| 416 | ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, |
| 417 | pref_store->ReadPrefs()); |
| 418 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 419 | |
| 420 | // The store shouldn't be considered initialized until the interceptor |
| 421 | // returns. |
| 422 | EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 423 | EXPECT_FALSE(pref_store->IsInitializationComplete()); |
| 424 | EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL)); |
| 425 | |
| 426 | raw_intercepting_pref_filter_->ReleasePrefs(); |
| 427 | |
| 428 | EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 429 | EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| 430 | EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL)); |
| 431 | |
| 432 | // The JSON file looks like this: |
| 433 | // { |
| 434 | // "homepage": "http://www.cnn.com", |
| 435 | // "some_directory": "/usr/local/", |
| 436 | // "tabs": { |
| 437 | // "new_windows_in_tabs": true, |
| 438 | // "max_tabs": 20 |
| 439 | // } |
| 440 | // } |
| 441 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 442 | RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | TEST_F(JsonPrefStoreTest, ReadAsyncWithInterceptor) { |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 446 | base::FilePath input_file = temp_dir_.path().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(); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 454 | scoped_refptr<JsonPrefStore> pref_store = |
| 455 | new JsonPrefStore(input_file, message_loop_.task_runner(), |
| 456 | std::move(intercepting_pref_filter)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 457 | |
| 458 | MockPrefStoreObserver mock_observer; |
| 459 | pref_store->AddObserver(&mock_observer); |
| 460 | |
| 461 | // Ownership of the |mock_error_delegate| is handed to the |pref_store| below. |
| 462 | MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 463 | |
| 464 | { |
| 465 | pref_store->ReadPrefsAsync(mock_error_delegate); |
| 466 | |
| 467 | EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(0); |
| 468 | // EXPECT_CALL(*mock_error_delegate, |
| 469 | // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
| 470 | RunLoop().RunUntilIdle(); |
| 471 | |
| 472 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 473 | EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 474 | EXPECT_FALSE(pref_store->IsInitializationComplete()); |
| 475 | EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL)); |
| 476 | } |
| 477 | |
| 478 | { |
| 479 | EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 480 | // EXPECT_CALL(*mock_error_delegate, |
| 481 | // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
| 482 | |
| 483 | raw_intercepting_pref_filter_->ReleasePrefs(); |
| 484 | |
| 485 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 486 | EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 487 | EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| 488 | EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL)); |
| 489 | } |
| 490 | |
| 491 | pref_store->RemoveObserver(&mock_observer); |
| 492 | |
| 493 | // The JSON file looks like this: |
| 494 | // { |
| 495 | // "homepage": "http://www.cnn.com", |
| 496 | // "some_directory": "/usr/local/", |
| 497 | // "tabs": { |
| 498 | // "new_windows_in_tabs": true, |
| 499 | // "max_tabs": 20 |
| 500 | // } |
| 501 | // } |
| 502 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 503 | RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 504 | } |
| 505 | |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 506 | TEST_F(JsonPrefStoreTest, AlternateFile) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 507 | base::FilePath alternate_input_file = |
| 508 | temp_dir_.path().AppendASCII("alternate.json"); |
| 509 | ASSERT_LT(0, base::WriteFile(alternate_input_file, |
| 510 | kReadJson, arraysize(kReadJson) - 1)); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 511 | |
| 512 | // Test that the alternate file is moved to the main file and read as-is from |
| 513 | // there. |
| 514 | base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 515 | ASSERT_FALSE(PathExists(input_file)); |
| 516 | ASSERT_TRUE(PathExists(alternate_input_file)); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 517 | scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 518 | input_file, alternate_input_file, message_loop_.task_runner(), |
| 519 | std::unique_ptr<PrefFilter>()); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 520 | |
| 521 | ASSERT_FALSE(PathExists(input_file)); |
| 522 | ASSERT_TRUE(PathExists(alternate_input_file)); |
| 523 | ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 524 | |
| 525 | ASSERT_TRUE(PathExists(input_file)); |
| 526 | ASSERT_FALSE(PathExists(alternate_input_file)); |
| 527 | |
| 528 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 529 | EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| 530 | |
| 531 | // The JSON file looks like this: |
| 532 | // { |
| 533 | // "homepage": "http://www.cnn.com", |
| 534 | // "some_directory": "/usr/local/", |
| 535 | // "tabs": { |
| 536 | // "new_windows_in_tabs": true, |
| 537 | // "max_tabs": 20 |
| 538 | // } |
| 539 | // } |
| 540 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 541 | RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | TEST_F(JsonPrefStoreTest, AlternateFileIgnoredWhenMainFileExists) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 545 | base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 546 | ASSERT_LT(0, base::WriteFile(input_file, |
| 547 | kReadJson, arraysize(kReadJson) - 1)); |
| 548 | |
| 549 | base::FilePath alternate_input_file = |
| 550 | temp_dir_.path().AppendASCII("alternate.json"); |
| 551 | ASSERT_LT(0, base::WriteFile(alternate_input_file, |
| 552 | kInvalidJson, arraysize(kInvalidJson) - 1)); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 553 | |
| 554 | // Test that the alternate file is ignored and that the read occurs from the |
| 555 | // existing main file. There is no attempt at even deleting the alternate |
| 556 | // file as this scenario should never happen in normal user-data-dirs. |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 557 | scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 558 | input_file, alternate_input_file, message_loop_.task_runner(), |
| 559 | std::unique_ptr<PrefFilter>()); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 560 | |
| 561 | ASSERT_TRUE(PathExists(input_file)); |
| 562 | ASSERT_TRUE(PathExists(alternate_input_file)); |
| 563 | ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 564 | |
| 565 | ASSERT_TRUE(PathExists(input_file)); |
| 566 | ASSERT_TRUE(PathExists(alternate_input_file)); |
| 567 | |
| 568 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 569 | EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| 570 | |
| 571 | // The JSON file looks like this: |
| 572 | // { |
| 573 | // "homepage": "http://www.cnn.com", |
| 574 | // "some_directory": "/usr/local/", |
| 575 | // "tabs": { |
| 576 | // "new_windows_in_tabs": true, |
| 577 | // "max_tabs": 20 |
| 578 | // } |
| 579 | // } |
| 580 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 581 | RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | TEST_F(JsonPrefStoreTest, AlternateFileDNE) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 585 | base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 586 | ASSERT_LT(0, base::WriteFile(input_file, |
| 587 | kReadJson, arraysize(kReadJson) - 1)); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 588 | |
| 589 | // Test that the basic read works fine when an alternate file is specified but |
| 590 | // does not exist. |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 591 | base::FilePath alternate_input_file = |
| 592 | temp_dir_.path().AppendASCII("alternate.json"); |
| 593 | ASSERT_TRUE(PathExists(input_file)); |
| 594 | ASSERT_FALSE(PathExists(alternate_input_file)); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 595 | scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 596 | input_file, alternate_input_file, message_loop_.task_runner(), |
| 597 | std::unique_ptr<PrefFilter>()); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 598 | |
| 599 | ASSERT_TRUE(PathExists(input_file)); |
| 600 | ASSERT_FALSE(PathExists(alternate_input_file)); |
| 601 | ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 602 | |
| 603 | ASSERT_TRUE(PathExists(input_file)); |
| 604 | ASSERT_FALSE(PathExists(alternate_input_file)); |
| 605 | |
| 606 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 607 | EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| 608 | |
| 609 | // The JSON file looks like this: |
| 610 | // { |
| 611 | // "homepage": "http://www.cnn.com", |
| 612 | // "some_directory": "/usr/local/", |
| 613 | // "tabs": { |
| 614 | // "new_windows_in_tabs": true, |
| 615 | // "max_tabs": 20 |
| 616 | // } |
| 617 | // } |
| 618 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 619 | RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | TEST_F(JsonPrefStoreTest, BasicAsyncWithAlternateFile) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 623 | base::FilePath alternate_input_file = |
| 624 | temp_dir_.path().AppendASCII("alternate.json"); |
| 625 | ASSERT_LT(0, base::WriteFile(alternate_input_file, |
| 626 | kReadJson, arraysize(kReadJson) - 1)); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 627 | |
| 628 | // Test that the alternate file is moved to the main file and read as-is from |
| 629 | // there even when the read is made asynchronously. |
| 630 | base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 631 | scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 632 | input_file, alternate_input_file, message_loop_.task_runner(), |
| 633 | std::unique_ptr<PrefFilter>()); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 634 | |
| 635 | ASSERT_FALSE(PathExists(input_file)); |
| 636 | ASSERT_TRUE(PathExists(alternate_input_file)); |
| 637 | |
| 638 | { |
| 639 | MockPrefStoreObserver mock_observer; |
| 640 | pref_store->AddObserver(&mock_observer); |
| 641 | |
| 642 | MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 643 | pref_store->ReadPrefsAsync(mock_error_delegate); |
| 644 | |
| 645 | EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 646 | EXPECT_CALL(*mock_error_delegate, |
| 647 | OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
| 648 | RunLoop().RunUntilIdle(); |
| 649 | pref_store->RemoveObserver(&mock_observer); |
| 650 | |
| 651 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 652 | EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| 653 | } |
| 654 | |
| 655 | ASSERT_TRUE(PathExists(input_file)); |
| 656 | ASSERT_FALSE(PathExists(alternate_input_file)); |
| 657 | |
| 658 | // The JSON file looks like this: |
| 659 | // { |
| 660 | // "homepage": "http://www.cnn.com", |
| 661 | // "some_directory": "/usr/local/", |
| 662 | // "tabs": { |
| 663 | // "new_windows_in_tabs": true, |
| 664 | // "max_tabs": 20 |
| 665 | // } |
| 666 | // } |
| 667 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 668 | RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 669 | } |
| 670 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 671 | TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 672 | base::HistogramTester histogram_tester; |
| 673 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 674 | SimpleTestClock* test_clock = new SimpleTestClock; |
| 675 | SetCurrentTimeInMinutes(0, test_clock); |
| 676 | JsonPrefStore::WriteCountHistogram histogram( |
| 677 | base::TimeDelta::FromSeconds(10), |
| 678 | base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 679 | std::unique_ptr<base::Clock>(test_clock)); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 680 | int32_t report_interval = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 681 | JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 682 | |
| 683 | histogram.RecordWriteOccured(); |
| 684 | |
| 685 | SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 686 | histogram.ReportOutstandingWrites(); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 687 | std::unique_ptr<HistogramSamples> samples = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 688 | histogram.GetHistogram()->SnapshotSamples(); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 689 | |
| 690 | std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
| 691 | histogram_tester.ExpectBucketCount(histogram_name, 1, 1); |
| 692 | histogram_tester.ExpectTotalCount(histogram_name, 1); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 693 | |
| 694 | ASSERT_EQ("Settings.JsonDataWriteCount.Local_State", |
| 695 | histogram.GetHistogram()->histogram_name()); |
| 696 | ASSERT_TRUE(histogram.GetHistogram()->HasConstructionArguments(1, 30, 31)); |
| 697 | } |
| 698 | |
| 699 | TEST_F(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 700 | base::HistogramTester histogram_tester; |
| 701 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 702 | SimpleTestClock* test_clock = new SimpleTestClock; |
| 703 | SetCurrentTimeInMinutes(0, test_clock); |
| 704 | JsonPrefStore::WriteCountHistogram histogram( |
| 705 | base::TimeDelta::FromSeconds(10), |
| 706 | base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 707 | std::unique_ptr<base::Clock>(test_clock)); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 708 | int32_t report_interval = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 709 | JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 710 | |
| 711 | histogram.RecordWriteOccured(); |
| 712 | SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
| 713 | histogram.RecordWriteOccured(); |
| 714 | SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
| 715 | histogram.RecordWriteOccured(); |
| 716 | |
| 717 | // Nothing should be recorded until the report period has elapsed. |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 718 | std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
| 719 | histogram_tester.ExpectTotalCount(histogram_name, 0); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 720 | |
| 721 | SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); |
| 722 | histogram.RecordWriteOccured(); |
| 723 | |
| 724 | // Now the report period has elapsed. |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 725 | histogram_tester.ExpectBucketCount(histogram_name, 3, 1); |
| 726 | histogram_tester.ExpectTotalCount(histogram_name, 1); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 727 | |
| 728 | // The last write won't be recorded because the second count period hasn't |
| 729 | // fully elapsed. |
| 730 | SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 731 | histogram.ReportOutstandingWrites(); |
| 732 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 733 | histogram_tester.ExpectBucketCount(histogram_name, 3, 1); |
| 734 | histogram_tester.ExpectTotalCount(histogram_name, 1); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | TEST_F(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 738 | base::HistogramTester histogram_tester; |
| 739 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 740 | SimpleTestClock* test_clock = new SimpleTestClock; |
| 741 | SetCurrentTimeInMinutes(0, test_clock); |
| 742 | JsonPrefStore::WriteCountHistogram histogram( |
| 743 | base::TimeDelta::FromSeconds(10), |
| 744 | base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 745 | std::unique_ptr<base::Clock>(test_clock)); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 746 | int32_t report_interval = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 747 | JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 748 | |
| 749 | histogram.RecordWriteOccured(); |
| 750 | SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
| 751 | histogram.RecordWriteOccured(); |
| 752 | SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
| 753 | histogram.RecordWriteOccured(); |
| 754 | SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); |
| 755 | histogram.RecordWriteOccured(); |
| 756 | SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 757 | histogram.RecordWriteOccured(); |
| 758 | SetCurrentTimeInMinutes(2.1 * report_interval, test_clock); |
| 759 | histogram.RecordWriteOccured(); |
| 760 | SetCurrentTimeInMinutes(2.5 * report_interval, test_clock); |
| 761 | histogram.RecordWriteOccured(); |
| 762 | SetCurrentTimeInMinutes(2.7 * report_interval, test_clock); |
| 763 | histogram.RecordWriteOccured(); |
| 764 | SetCurrentTimeInMinutes(3.3 * report_interval, test_clock); |
| 765 | histogram.RecordWriteOccured(); |
| 766 | |
| 767 | // The last write won't be recorded because the second count period hasn't |
| 768 | // fully elapsed |
| 769 | SetCurrentTimeInMinutes(3.5 * report_interval, test_clock); |
| 770 | histogram.ReportOutstandingWrites(); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 771 | std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
| 772 | histogram_tester.ExpectBucketCount(histogram_name, 3, 2); |
| 773 | histogram_tester.ExpectBucketCount(histogram_name, 2, 1); |
| 774 | histogram_tester.ExpectTotalCount(histogram_name, 3); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | TEST_F(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 778 | base::HistogramTester histogram_tester; |
| 779 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 780 | SimpleTestClock* test_clock = new SimpleTestClock; |
| 781 | SetCurrentTimeInMinutes(0, test_clock); |
| 782 | JsonPrefStore::WriteCountHistogram histogram( |
| 783 | base::TimeDelta::FromSeconds(10), |
| 784 | base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 785 | std::unique_ptr<base::Clock>(test_clock)); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 786 | int32_t report_interval = |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 787 | JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 788 | |
| 789 | // 1 write in the first period. |
| 790 | histogram.RecordWriteOccured(); |
| 791 | |
| 792 | // No writes in the second and third periods. |
| 793 | |
| 794 | // 2 writes in the fourth period. |
| 795 | SetCurrentTimeInMinutes(3.1 * report_interval, test_clock); |
| 796 | histogram.RecordWriteOccured(); |
| 797 | SetCurrentTimeInMinutes(3.3 * report_interval, test_clock); |
| 798 | histogram.RecordWriteOccured(); |
| 799 | |
| 800 | // No writes in the fifth period. |
| 801 | |
| 802 | // 3 writes in the sixth period. |
| 803 | SetCurrentTimeInMinutes(5.1 * report_interval, test_clock); |
| 804 | histogram.RecordWriteOccured(); |
| 805 | SetCurrentTimeInMinutes(5.3 * report_interval, test_clock); |
| 806 | histogram.RecordWriteOccured(); |
| 807 | SetCurrentTimeInMinutes(5.5 * report_interval, test_clock); |
| 808 | histogram.RecordWriteOccured(); |
| 809 | |
| 810 | SetCurrentTimeInMinutes(6.1 * report_interval, test_clock); |
| 811 | histogram.ReportOutstandingWrites(); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 812 | std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
| 813 | histogram_tester.ExpectBucketCount(histogram_name, 0, 3); |
| 814 | histogram_tester.ExpectBucketCount(histogram_name, 1, 1); |
| 815 | histogram_tester.ExpectBucketCount(histogram_name, 2, 1); |
| 816 | histogram_tester.ExpectBucketCount(histogram_name, 3, 1); |
| 817 | histogram_tester.ExpectTotalCount(histogram_name, 6); |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 818 | } |
| 819 | |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 820 | class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { |
| 821 | protected: |
| 822 | void SetUp() override { |
| 823 | JsonPrefStoreTest::SetUp(); |
| 824 | test_file_ = temp_dir_.path().AppendASCII("test.json"); |
| 825 | } |
| 826 | |
| 827 | // Creates a JsonPrefStore with the given |file_writer|. |
| 828 | scoped_refptr<JsonPrefStore> CreatePrefStore() { |
| 829 | return new JsonPrefStore(test_file_, message_loop_.task_runner(), |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 830 | std::unique_ptr<PrefFilter>()); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | // Return the ImportantFileWriter for a given JsonPrefStore. |
| 834 | ImportantFileWriter* GetImportantFileWriter( |
| 835 | scoped_refptr<JsonPrefStore> pref_store) { |
| 836 | return &(pref_store->writer_); |
| 837 | } |
| 838 | |
| 839 | // Get the contents of kTestFile. Pumps the message loop before returning the |
| 840 | // result. |
| 841 | std::string GetTestFileContents() { |
| 842 | RunLoop().RunUntilIdle(); |
| 843 | std::string file_contents; |
| 844 | ReadFileToString(test_file_, &file_contents); |
| 845 | return file_contents; |
| 846 | } |
| 847 | |
| 848 | private: |
| 849 | base::FilePath test_file_; |
| 850 | }; |
| 851 | |
| 852 | TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteBasic) { |
| 853 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
| 854 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); |
| 855 | |
| 856 | // Set a normal pref and check that it gets scheduled to be written. |
| 857 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 858 | pref_store->SetValue("normal", |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 859 | base::WrapUnique(new base::StringValue("normal")), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 860 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 861 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 862 | file_writer->DoScheduledWrite(); |
| 863 | ASSERT_EQ("{\"normal\":\"normal\"}", GetTestFileContents()); |
| 864 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 865 | |
| 866 | // Set a lossy pref and check that it is not scheduled to be written. |
| 867 | // SetValue/RemoveValue. |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 868 | pref_store->SetValue("lossy", |
| 869 | base::WrapUnique(new base::StringValue("lossy")), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 870 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 871 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 872 | pref_store->RemoveValue("lossy", WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 873 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 874 | |
| 875 | // SetValueSilently/RemoveValueSilently. |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 876 | pref_store->SetValueSilently("lossy", |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 877 | base::WrapUnique(new base::StringValue("lossy")), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 878 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 879 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 880 | pref_store->RemoveValueSilently("lossy", |
| 881 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 882 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 883 | |
| 884 | // ReportValueChanged. |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 885 | pref_store->SetValue("lossy", |
| 886 | base::WrapUnique(new base::StringValue("lossy")), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 887 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 888 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 889 | pref_store->ReportValueChanged("lossy", |
| 890 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 891 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 892 | |
| 893 | // Call CommitPendingWrite and check that the lossy pref and the normal pref |
| 894 | // are there with the last values set above. |
| 895 | pref_store->CommitPendingWrite(); |
| 896 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 897 | ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
| 898 | GetTestFileContents()); |
| 899 | } |
| 900 | |
| 901 | TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossyFirst) { |
| 902 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
| 903 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); |
| 904 | |
| 905 | // Set a lossy pref and check that it is not scheduled to be written. |
| 906 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 907 | pref_store->SetValue("lossy", |
| 908 | base::WrapUnique(new base::StringValue("lossy")), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 909 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 910 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 911 | |
| 912 | // Set a normal pref and check that it is scheduled to be written. |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 913 | pref_store->SetValue("normal", |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 914 | base::WrapUnique(new base::StringValue("normal")), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 915 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 916 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 917 | |
| 918 | // Call DoScheduledWrite and check both prefs get written. |
| 919 | file_writer->DoScheduledWrite(); |
| 920 | ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
| 921 | GetTestFileContents()); |
| 922 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 923 | } |
| 924 | |
| 925 | TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossySecond) { |
| 926 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
| 927 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); |
| 928 | |
| 929 | // Set a normal pref and check that it is scheduled to be written. |
| 930 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 931 | pref_store->SetValue("normal", |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 932 | base::WrapUnique(new base::StringValue("normal")), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 933 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 934 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 935 | |
| 936 | // Set a lossy pref and check that the write is still scheduled. |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 937 | pref_store->SetValue("lossy", |
| 938 | base::WrapUnique(new base::StringValue("lossy")), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 939 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 940 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 941 | |
| 942 | // Call DoScheduledWrite and check both prefs get written. |
| 943 | file_writer->DoScheduledWrite(); |
| 944 | ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
| 945 | GetTestFileContents()); |
| 946 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 947 | } |
| 948 | |
benwells | 2673059 | 2015-05-28 13:08:08 | [diff] [blame] | 949 | TEST_F(JsonPrefStoreLossyWriteTest, ScheduleLossyWrite) { |
| 950 | scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
| 951 | ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); |
| 952 | |
| 953 | // Set a lossy pref and check that it is not scheduled to be written. |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame^] | 954 | pref_store->SetValue("lossy", |
| 955 | base::WrapUnique(new base::StringValue("lossy")), |
benwells | 2673059 | 2015-05-28 13:08:08 | [diff] [blame] | 956 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
| 957 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 958 | |
| 959 | // Schedule pending lossy writes and check that it is scheduled. |
| 960 | pref_store->SchedulePendingLossyWrites(); |
| 961 | ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 962 | |
| 963 | // Call CommitPendingWrite and check that the lossy pref is there with the |
| 964 | // last value set above. |
| 965 | pref_store->CommitPendingWrite(); |
| 966 | ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 967 | ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); |
| 968 | } |
| 969 | |
[email protected] | 7e3ec42c | 2012-12-16 05:13:21 | [diff] [blame] | 970 | } // namespace base |