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