Chromium Code Reviews| 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 "base/prefs/json_pref_store.h" | 5 #include "base/prefs/json_pref_store.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 206 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
| 207 EXPECT_TRUE(boolean); | 207 EXPECT_TRUE(boolean); |
| 208 | 208 |
| 209 pref_store->SetValue(kNewWindowsInTabs, new FundamentalValue(false), | 209 pref_store->SetValue(kNewWindowsInTabs, new FundamentalValue(false), |
| 210 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 210 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 211 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); | 211 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
| 212 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 212 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
| 213 EXPECT_FALSE(boolean); | 213 EXPECT_FALSE(boolean); |
| 214 | 214 |
| 215 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); | 215 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
| 216 int integer = 0; | 216 int integer; |
| 217 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 217 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
| 218 EXPECT_EQ(20, integer); | 218 EXPECT_EQ(20, integer); |
| 219 pref_store->SetValue(kMaxTabs, new FundamentalValue(10), | 219 pref_store->SetValue(kMaxTabs, new FundamentalValue(10), |
| 220 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 220 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 221 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); | 221 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
| 222 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 222 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
| 223 EXPECT_EQ(10, integer); | 223 EXPECT_EQ(10, integer); |
| 224 | 224 |
| 225 pref_store->SetValue(kLongIntPref, | 225 pref_store->SetValue(kLongIntPref, |
| 226 new StringValue(base::Int64ToString(214748364842LL)), | 226 new StringValue(base::Int64ToString(214748364842LL)), |
| (...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 802 histogram.ReportOutstandingWrites(); | 802 histogram.ReportOutstandingWrites(); |
| 803 scoped_ptr<HistogramSamples> samples = | 803 scoped_ptr<HistogramSamples> samples = |
| 804 histogram.GetHistogram()->SnapshotSamples(); | 804 histogram.GetHistogram()->SnapshotSamples(); |
| 805 ASSERT_EQ(3, samples->GetCount(0)); | 805 ASSERT_EQ(3, samples->GetCount(0)); |
| 806 ASSERT_EQ(1, samples->GetCount(1)); | 806 ASSERT_EQ(1, samples->GetCount(1)); |
| 807 ASSERT_EQ(1, samples->GetCount(2)); | 807 ASSERT_EQ(1, samples->GetCount(2)); |
| 808 ASSERT_EQ(1, samples->GetCount(3)); | 808 ASSERT_EQ(1, samples->GetCount(3)); |
| 809 ASSERT_EQ(6, samples->TotalCount()); | 809 ASSERT_EQ(6, samples->TotalCount()); |
| 810 } | 810 } |
| 811 | 811 |
| 812 class TestImportantFileWriter : public ImportantFileWriter { | |
| 813 public: | |
| 814 TestImportantFileWriter() {} | |
| 815 ~TestImportantFileWriter() override {} | |
| 816 | |
| 817 // ImportantFileWriter overrides. | |
| 818 bool HasPendingWrite() const override { return !!serializer_; } | |
| 819 | |
| 820 void WriteNow(scoped_ptr<std::string> data) override { NOTREACHED(); } | |
| 821 | |
| 822 void ScheduleWrite(DataSerializer* serializer) override { | |
| 823 serializer_ = serializer; | |
| 824 } | |
| 825 | |
| 826 void DoScheduledWrite() override { | |
| 827 CHECK(serializer_ && !has_last_write_); | |
| 828 if (!serializer_->SerializeData(&last_write_)) | |
| 829 NOTREACHED(); | |
| 830 serializer_ = nullptr; | |
| 831 has_last_write_ = true; | |
| 832 } | |
| 833 | |
| 834 void RegisterOnNextSuccessfulWriteCallback( | |
| 835 const base::Closure& on_next_successful_write) override { | |
| 836 NOTREACHED(); | |
| 837 } | |
| 838 | |
| 839 TimeDelta CommitInterval() const override { | |
| 840 return TimeDelta::FromSeconds(5); | |
| 841 } | |
| 842 | |
| 843 std::string GetAndClearLastWrite() { | |
| 844 std::string result = last_write_; | |
| 845 last_write_ = std::string(); | |
| 846 has_last_write_ = false; | |
| 847 return result; | |
| 848 } | |
| 849 | |
| 850 private: | |
| 851 DataSerializer* serializer_ = nullptr; | |
| 852 std::string last_write_; | |
| 853 bool has_last_write_ = false; | |
| 854 }; | |
| 855 | |
| 856 // This test subclass just contains some helper functions. | |
| 857 class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { | |
| 858 protected: | |
| 859 // A struct used for conveniently setting values in the PrefStore | |
| 860 struct PrefDetails { | |
|
Mattias Nissler (ping if slow)
2015/05/07 07:59:53
This is actually not providing any benefit that I
| |
| 861 std::string key; | |
| 862 scoped_ptr<base::Value> value; | |
| 863 uint32 flags; | |
| 864 }; | |
| 865 | |
| 866 static PrefDetails GetNormalPrefDetails() { | |
| 867 return {"normal", | |
| 868 scoped_ptr<base::Value>(new base::StringValue("normal")), | |
| 869 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS}; | |
| 870 } | |
| 871 | |
| 872 static PrefDetails GetLossyPrefDetails() { | |
| 873 return {"lossy", | |
| 874 scoped_ptr<base::Value>(new base::StringValue("lossy")), | |
| 875 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG}; | |
| 876 } | |
| 877 | |
| 878 // Creates a JsonPrefStore with the given |file_writer|. | |
| 879 scoped_refptr<JsonPrefStore> CreatePrefStore( | |
| 880 TestImportantFileWriter* file_writer) { | |
| 881 base::FilePath file(FILE_PATH_LITERAL("abc.txt")); | |
| 882 return new JsonPrefStore(file, file, message_loop_.task_runner(), | |
| 883 scoped_ptr<base::ImportantFileWriter>(file_writer), | |
| 884 scoped_ptr<PrefFilter>()); | |
| 885 } | |
| 886 }; | |
| 887 | |
| 888 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteBasic) { | |
| 889 TestImportantFileWriter* file_writer = new TestImportantFileWriter; | |
| 890 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(file_writer); | |
| 891 PrefDetails normal = GetNormalPrefDetails(); | |
| 892 PrefDetails lossy = GetLossyPrefDetails(); | |
| 893 | |
| 894 // Set a normal pref and check that it gets scheduled to be written. | |
| 895 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 896 pref_store->SetValue(normal.key, normal.value->DeepCopy(), normal.flags); | |
| 897 ASSERT_TRUE(file_writer->HasPendingWrite()); | |
| 898 file_writer->DoScheduledWrite(); | |
| 899 ASSERT_EQ("{\"normal\":\"normal\"}", file_writer->GetAndClearLastWrite()); | |
| 900 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
|
Mattias Nissler (ping if slow)
2015/05/07 07:59:53
Instead of mocking the file writer, you could just
| |
| 901 | |
| 902 // Set a lossy pref and check that it is not scheduled to be written. | |
| 903 // SetValue/RemoveValue | |
| 904 pref_store->SetValue(lossy.key, lossy.value->DeepCopy(), lossy.flags); | |
| 905 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 906 pref_store->RemoveValue(lossy.key, lossy.flags); | |
| 907 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
|
Mattias Nissler (ping if slow)
2015/05/07 07:59:53
Same here, could just spin the loop and check the
| |
| 908 | |
| 909 // SetValueSilently/RemoveValueSilently. | |
| 910 pref_store->SetValueSilently(lossy.key, lossy.value->DeepCopy(), lossy.flags); | |
| 911 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 912 pref_store->RemoveValueSilently(lossy.key, lossy.flags); | |
| 913 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 914 | |
| 915 // ReportValueChanged. | |
| 916 pref_store->SetValue(lossy.key, lossy.value->DeepCopy(), lossy.flags); | |
| 917 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 918 pref_store->ReportValueChanged(lossy.key, lossy.flags); | |
| 919 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 920 | |
| 921 // Call CommitPendingWrite and check that the lossy pref and the normal pref | |
| 922 // are there with the last values set above. | |
| 923 pref_store->CommitPendingWrite(); | |
| 924 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 925 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | |
| 926 file_writer->GetAndClearLastWrite()); | |
| 927 } | |
| 928 | |
| 929 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixed) { | |
| 930 TestImportantFileWriter* file_writer = new TestImportantFileWriter; | |
| 931 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(file_writer); | |
| 932 PrefDetails normal = GetNormalPrefDetails(); | |
| 933 PrefDetails lossy = GetLossyPrefDetails(); | |
| 934 | |
| 935 // Set a normal pref and check that it is scheduled to be written. | |
| 936 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 937 pref_store->SetValue(normal.key, normal.value->DeepCopy(), normal.flags); | |
| 938 ASSERT_TRUE(file_writer->HasPendingWrite()); | |
| 939 // Set a lossy pref and check that the write is still scheduled. | |
| 940 pref_store->SetValue(lossy.key, lossy.value->DeepCopy(), lossy.flags); | |
| 941 ASSERT_TRUE(file_writer->HasPendingWrite()); | |
| 942 // Call DoScheduledWrite and check both prefs get written. | |
| 943 file_writer->DoScheduledWrite(); | |
| 944 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | |
| 945 file_writer->GetAndClearLastWrite()); | |
| 946 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 947 | |
| 948 // Cleanup. | |
| 949 pref_store->RemoveValue(normal.key, normal.flags); | |
| 950 pref_store->RemoveValue(lossy.key, lossy.flags); | |
| 951 pref_store->CommitPendingWrite(); | |
| 952 file_writer->GetAndClearLastWrite(); | |
| 953 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 954 | |
| 955 // Set a lossy pref and check that it is not scheduled to be written. | |
| 956 pref_store->SetValue(lossy.key, lossy.value->DeepCopy(), lossy.flags); | |
| 957 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 958 // Set a normal pref and check that it is scheduled to be written. | |
| 959 pref_store->SetValue(normal.key, normal.value->DeepCopy(), normal.flags); | |
| 960 ASSERT_TRUE(file_writer->HasPendingWrite()); | |
| 961 // Call DoScheduledWrite and check both prefs get written. | |
| 962 file_writer->DoScheduledWrite(); | |
| 963 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | |
| 964 file_writer->GetAndClearLastWrite()); | |
| 965 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 966 } | |
| 967 | |
| 812 } // namespace base | 968 } // namespace base |
| OLD | NEW |