OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/prefs/json_pref_store.h" | 5 #include "components/prefs/json_pref_store.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 | 222 |
223 const char kSomeDirectory[] = "some_directory"; | 223 const char kSomeDirectory[] = "some_directory"; |
224 | 224 |
225 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); | 225 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
226 base::FilePath::StringType path; | 226 base::FilePath::StringType path; |
227 EXPECT_TRUE(actual->GetAsString(&path)); | 227 EXPECT_TRUE(actual->GetAsString(&path)); |
228 EXPECT_EQ(base::FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); | 228 EXPECT_EQ(base::FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); |
229 base::FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); | 229 base::FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); |
230 | 230 |
231 pref_store->SetValue(kSomeDirectory, | 231 pref_store->SetValue(kSomeDirectory, |
232 base::MakeUnique<StringValue>(some_path.value()), | 232 base::MakeUnique<Value>(some_path.value()), |
233 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 233 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
234 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); | 234 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
235 EXPECT_TRUE(actual->GetAsString(&path)); | 235 EXPECT_TRUE(actual->GetAsString(&path)); |
236 EXPECT_EQ(some_path.value(), path); | 236 EXPECT_EQ(some_path.value(), path); |
237 | 237 |
238 // Test reading some other data types from sub-dictionaries. | 238 // Test reading some other data types from sub-dictionaries. |
239 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); | 239 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
240 bool boolean = false; | 240 bool boolean = false; |
241 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 241 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
242 EXPECT_TRUE(boolean); | 242 EXPECT_TRUE(boolean); |
243 | 243 |
244 pref_store->SetValue(kNewWindowsInTabs, base::MakeUnique<Value>(false), | 244 pref_store->SetValue(kNewWindowsInTabs, base::MakeUnique<Value>(false), |
245 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 245 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
246 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); | 246 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
247 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 247 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
248 EXPECT_FALSE(boolean); | 248 EXPECT_FALSE(boolean); |
249 | 249 |
250 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); | 250 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
251 int integer = 0; | 251 int integer = 0; |
252 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 252 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
253 EXPECT_EQ(20, integer); | 253 EXPECT_EQ(20, integer); |
254 pref_store->SetValue(kMaxTabs, base::MakeUnique<Value>(10), | 254 pref_store->SetValue(kMaxTabs, base::MakeUnique<Value>(10), |
255 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 255 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
256 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); | 256 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
257 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 257 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
258 EXPECT_EQ(10, integer); | 258 EXPECT_EQ(10, integer); |
259 | 259 |
260 pref_store->SetValue(kLongIntPref, base::MakeUnique<StringValue>( | 260 pref_store->SetValue( |
261 base::Int64ToString(214748364842LL)), | 261 kLongIntPref, |
262 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 262 base::MakeUnique<Value>(base::Int64ToString(214748364842LL)), |
| 263 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
263 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); | 264 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); |
264 EXPECT_TRUE(actual->GetAsString(&string_value)); | 265 EXPECT_TRUE(actual->GetAsString(&string_value)); |
265 int64_t value; | 266 int64_t value; |
266 base::StringToInt64(string_value, &value); | 267 base::StringToInt64(string_value, &value); |
267 EXPECT_EQ(214748364842LL, value); | 268 EXPECT_EQ(214748364842LL, value); |
268 | 269 |
269 // Serialize and compare to expected output. | 270 // Serialize and compare to expected output. |
270 pref_store->CommitPendingWrite(); | 271 pref_store->CommitPendingWrite(); |
271 RunLoop().RunUntilIdle(); | 272 RunLoop().RunUntilIdle(); |
272 | 273 |
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
865 | 866 |
866 DISALLOW_COPY_AND_ASSIGN(JsonPrefStoreLossyWriteTest); | 867 DISALLOW_COPY_AND_ASSIGN(JsonPrefStoreLossyWriteTest); |
867 }; | 868 }; |
868 | 869 |
869 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteBasic) { | 870 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteBasic) { |
870 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | 871 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
871 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); | 872 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
872 | 873 |
873 // Set a normal pref and check that it gets scheduled to be written. | 874 // Set a normal pref and check that it gets scheduled to be written. |
874 ASSERT_FALSE(file_writer->HasPendingWrite()); | 875 ASSERT_FALSE(file_writer->HasPendingWrite()); |
875 pref_store->SetValue("normal", base::MakeUnique<base::StringValue>("normal"), | 876 pref_store->SetValue("normal", base::MakeUnique<base::Value>("normal"), |
876 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 877 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
877 ASSERT_TRUE(file_writer->HasPendingWrite()); | 878 ASSERT_TRUE(file_writer->HasPendingWrite()); |
878 file_writer->DoScheduledWrite(); | 879 file_writer->DoScheduledWrite(); |
879 ASSERT_EQ("{\"normal\":\"normal\"}", GetTestFileContents()); | 880 ASSERT_EQ("{\"normal\":\"normal\"}", GetTestFileContents()); |
880 ASSERT_FALSE(file_writer->HasPendingWrite()); | 881 ASSERT_FALSE(file_writer->HasPendingWrite()); |
881 | 882 |
882 // Set a lossy pref and check that it is not scheduled to be written. | 883 // Set a lossy pref and check that it is not scheduled to be written. |
883 // SetValue/RemoveValue. | 884 // SetValue/RemoveValue. |
884 pref_store->SetValue("lossy", base::MakeUnique<base::StringValue>("lossy"), | 885 pref_store->SetValue("lossy", base::MakeUnique<base::Value>("lossy"), |
885 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 886 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
886 ASSERT_FALSE(file_writer->HasPendingWrite()); | 887 ASSERT_FALSE(file_writer->HasPendingWrite()); |
887 pref_store->RemoveValue("lossy", WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 888 pref_store->RemoveValue("lossy", WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
888 ASSERT_FALSE(file_writer->HasPendingWrite()); | 889 ASSERT_FALSE(file_writer->HasPendingWrite()); |
889 | 890 |
890 // SetValueSilently/RemoveValueSilently. | 891 // SetValueSilently/RemoveValueSilently. |
891 pref_store->SetValueSilently("lossy", | 892 pref_store->SetValueSilently("lossy", base::MakeUnique<base::Value>("lossy"), |
892 base::MakeUnique<base::StringValue>("lossy"), | |
893 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 893 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
894 ASSERT_FALSE(file_writer->HasPendingWrite()); | 894 ASSERT_FALSE(file_writer->HasPendingWrite()); |
895 pref_store->RemoveValueSilently("lossy", | 895 pref_store->RemoveValueSilently("lossy", |
896 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 896 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
897 ASSERT_FALSE(file_writer->HasPendingWrite()); | 897 ASSERT_FALSE(file_writer->HasPendingWrite()); |
898 | 898 |
899 // ReportValueChanged. | 899 // ReportValueChanged. |
900 pref_store->SetValue("lossy", base::MakeUnique<base::StringValue>("lossy"), | 900 pref_store->SetValue("lossy", base::MakeUnique<base::Value>("lossy"), |
901 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 901 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
902 ASSERT_FALSE(file_writer->HasPendingWrite()); | 902 ASSERT_FALSE(file_writer->HasPendingWrite()); |
903 pref_store->ReportValueChanged("lossy", | 903 pref_store->ReportValueChanged("lossy", |
904 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 904 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
905 ASSERT_FALSE(file_writer->HasPendingWrite()); | 905 ASSERT_FALSE(file_writer->HasPendingWrite()); |
906 | 906 |
907 // Call CommitPendingWrite and check that the lossy pref and the normal pref | 907 // Call CommitPendingWrite and check that the lossy pref and the normal pref |
908 // are there with the last values set above. | 908 // are there with the last values set above. |
909 pref_store->CommitPendingWrite(); | 909 pref_store->CommitPendingWrite(); |
910 ASSERT_FALSE(file_writer->HasPendingWrite()); | 910 ASSERT_FALSE(file_writer->HasPendingWrite()); |
911 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | 911 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
912 GetTestFileContents()); | 912 GetTestFileContents()); |
913 } | 913 } |
914 | 914 |
915 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossyFirst) { | 915 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossyFirst) { |
916 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | 916 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
917 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); | 917 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
918 | 918 |
919 // Set a lossy pref and check that it is not scheduled to be written. | 919 // Set a lossy pref and check that it is not scheduled to be written. |
920 ASSERT_FALSE(file_writer->HasPendingWrite()); | 920 ASSERT_FALSE(file_writer->HasPendingWrite()); |
921 pref_store->SetValue("lossy", base::MakeUnique<base::StringValue>("lossy"), | 921 pref_store->SetValue("lossy", base::MakeUnique<base::Value>("lossy"), |
922 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 922 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
923 ASSERT_FALSE(file_writer->HasPendingWrite()); | 923 ASSERT_FALSE(file_writer->HasPendingWrite()); |
924 | 924 |
925 // Set a normal pref and check that it is scheduled to be written. | 925 // Set a normal pref and check that it is scheduled to be written. |
926 pref_store->SetValue("normal", base::MakeUnique<base::StringValue>("normal"), | 926 pref_store->SetValue("normal", base::MakeUnique<base::Value>("normal"), |
927 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 927 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
928 ASSERT_TRUE(file_writer->HasPendingWrite()); | 928 ASSERT_TRUE(file_writer->HasPendingWrite()); |
929 | 929 |
930 // Call DoScheduledWrite and check both prefs get written. | 930 // Call DoScheduledWrite and check both prefs get written. |
931 file_writer->DoScheduledWrite(); | 931 file_writer->DoScheduledWrite(); |
932 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | 932 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
933 GetTestFileContents()); | 933 GetTestFileContents()); |
934 ASSERT_FALSE(file_writer->HasPendingWrite()); | 934 ASSERT_FALSE(file_writer->HasPendingWrite()); |
935 } | 935 } |
936 | 936 |
937 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossySecond) { | 937 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossySecond) { |
938 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | 938 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
939 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); | 939 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
940 | 940 |
941 // Set a normal pref and check that it is scheduled to be written. | 941 // Set a normal pref and check that it is scheduled to be written. |
942 ASSERT_FALSE(file_writer->HasPendingWrite()); | 942 ASSERT_FALSE(file_writer->HasPendingWrite()); |
943 pref_store->SetValue("normal", base::MakeUnique<base::StringValue>("normal"), | 943 pref_store->SetValue("normal", base::MakeUnique<base::Value>("normal"), |
944 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 944 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
945 ASSERT_TRUE(file_writer->HasPendingWrite()); | 945 ASSERT_TRUE(file_writer->HasPendingWrite()); |
946 | 946 |
947 // Set a lossy pref and check that the write is still scheduled. | 947 // Set a lossy pref and check that the write is still scheduled. |
948 pref_store->SetValue("lossy", base::MakeUnique<base::StringValue>("lossy"), | 948 pref_store->SetValue("lossy", base::MakeUnique<base::Value>("lossy"), |
949 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 949 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
950 ASSERT_TRUE(file_writer->HasPendingWrite()); | 950 ASSERT_TRUE(file_writer->HasPendingWrite()); |
951 | 951 |
952 // Call DoScheduledWrite and check both prefs get written. | 952 // Call DoScheduledWrite and check both prefs get written. |
953 file_writer->DoScheduledWrite(); | 953 file_writer->DoScheduledWrite(); |
954 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | 954 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
955 GetTestFileContents()); | 955 GetTestFileContents()); |
956 ASSERT_FALSE(file_writer->HasPendingWrite()); | 956 ASSERT_FALSE(file_writer->HasPendingWrite()); |
957 } | 957 } |
958 | 958 |
959 TEST_F(JsonPrefStoreLossyWriteTest, ScheduleLossyWrite) { | 959 TEST_F(JsonPrefStoreLossyWriteTest, ScheduleLossyWrite) { |
960 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | 960 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
961 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); | 961 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
962 | 962 |
963 // Set a lossy pref and check that it is not scheduled to be written. | 963 // Set a lossy pref and check that it is not scheduled to be written. |
964 pref_store->SetValue("lossy", base::MakeUnique<base::StringValue>("lossy"), | 964 pref_store->SetValue("lossy", base::MakeUnique<base::Value>("lossy"), |
965 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 965 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
966 ASSERT_FALSE(file_writer->HasPendingWrite()); | 966 ASSERT_FALSE(file_writer->HasPendingWrite()); |
967 | 967 |
968 // Schedule pending lossy writes and check that it is scheduled. | 968 // Schedule pending lossy writes and check that it is scheduled. |
969 pref_store->SchedulePendingLossyWrites(); | 969 pref_store->SchedulePendingLossyWrites(); |
970 ASSERT_TRUE(file_writer->HasPendingWrite()); | 970 ASSERT_TRUE(file_writer->HasPendingWrite()); |
971 | 971 |
972 // Call CommitPendingWrite and check that the lossy pref is there with the | 972 // Call CommitPendingWrite and check that the lossy pref is there with the |
973 // last value set above. | 973 // last value set above. |
974 pref_store->CommitPendingWrite(); | 974 pref_store->CommitPendingWrite(); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1118 | 1118 |
1119 std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 1119 std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
1120 new InterceptingPrefFilter(write_callback_observer_.GetCallbackPair())); | 1120 new InterceptingPrefFilter(write_callback_observer_.GetCallbackPair())); |
1121 scoped_refptr<JsonPrefStore> pref_store = | 1121 scoped_refptr<JsonPrefStore> pref_store = |
1122 new JsonPrefStore(input_file, message_loop_.task_runner(), | 1122 new JsonPrefStore(input_file, message_loop_.task_runner(), |
1123 std::move(intercepting_pref_filter)); | 1123 std::move(intercepting_pref_filter)); |
1124 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); | 1124 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
1125 | 1125 |
1126 EXPECT_EQ(NOT_CALLED, | 1126 EXPECT_EQ(NOT_CALLED, |
1127 write_callback_observer_.GetAndResetPostWriteObservationState()); | 1127 write_callback_observer_.GetAndResetPostWriteObservationState()); |
1128 pref_store->SetValue("normal", base::MakeUnique<base::StringValue>("normal"), | 1128 pref_store->SetValue("normal", base::MakeUnique<base::Value>("normal"), |
1129 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 1129 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
1130 file_writer->DoScheduledWrite(); | 1130 file_writer->DoScheduledWrite(); |
1131 | 1131 |
1132 // The observer should not be invoked right away. | 1132 // The observer should not be invoked right away. |
1133 EXPECT_FALSE(write_callback_observer_.GetAndResetPreWriteObservationState()); | 1133 EXPECT_FALSE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
1134 EXPECT_EQ(NOT_CALLED, | 1134 EXPECT_EQ(NOT_CALLED, |
1135 write_callback_observer_.GetAndResetPostWriteObservationState()); | 1135 write_callback_observer_.GetAndResetPostWriteObservationState()); |
1136 | 1136 |
1137 RunLoop().RunUntilIdle(); | 1137 RunLoop().RunUntilIdle(); |
1138 | 1138 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1244 file_writer->WriteNow(MakeUnique<std::string>("foo")); | 1244 file_writer->WriteNow(MakeUnique<std::string>("foo")); |
1245 } | 1245 } |
1246 RunLoop().RunUntilIdle(); | 1246 RunLoop().RunUntilIdle(); |
1247 EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); | 1247 EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); |
1248 EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState()); | 1248 EXPECT_TRUE(write_callback_observer_.GetAndResetPreWriteObservationState()); |
1249 EXPECT_EQ(CALLED_WITH_SUCCESS, | 1249 EXPECT_EQ(CALLED_WITH_SUCCESS, |
1250 write_callback_observer_.GetAndResetPostWriteObservationState()); | 1250 write_callback_observer_.GetAndResetPostWriteObservationState()); |
1251 } | 1251 } |
1252 | 1252 |
1253 } // namespace base | 1253 } // namespace base |
OLD | NEW |