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 791 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 JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { | |
813 protected: | |
814 void SetUp() override { | |
815 JsonPrefStoreTest::SetUp(); | |
816 test_file_ = temp_dir_.path().AppendASCII("test.json"); | |
817 } | |
818 | |
819 // Creates a JsonPrefStore with the given |file_writer|. | |
820 scoped_refptr<JsonPrefStore> CreatePrefStore() { | |
821 return new JsonPrefStore(test_file_, message_loop_.task_runner(), | |
822 scoped_ptr<PrefFilter>()); | |
823 } | |
824 | |
825 // Return the ImportantFileWriter for a given JsonPrefStore. | |
826 ImportantFileWriter* GetImportantFileWriter( | |
827 scoped_refptr<JsonPrefStore> pref_store) { | |
828 return &(pref_store->writer_); | |
829 } | |
830 | |
831 // Get the contents of kTestFile. Pumps the message loop before returning the | |
832 // result. | |
833 std::string GetTestFileContents() { | |
834 MessageLoop::current()->RunUntilIdle(); | |
835 std::string file_contents; | |
836 ReadFileToString(test_file_, &file_contents); | |
837 return file_contents; | |
838 } | |
839 | |
840 private: | |
841 base::FilePath test_file_; | |
842 }; | |
843 | |
844 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteBasic) { | |
845 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | |
846 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); | |
847 | |
848 // Set a normal pref and check that it gets scheduled to be written. | |
849 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
850 pref_store->SetValue("normal", new base::StringValue("normal"), | |
851 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | |
852 ASSERT_TRUE(file_writer->HasPendingWrite()); | |
853 file_writer->DoScheduledWrite(); | |
854 ASSERT_EQ("{\"normal\":\"normal\"}", GetTestFileContents()); | |
855 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
856 | |
857 // Set a lossy pref and check that it is not scheduled to be written. | |
858 // SetValue/RemoveValue. | |
859 pref_store->SetValue("lossy", new base::StringValue("lossy"), | |
860 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | |
861 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
862 pref_store->RemoveValue("lossy", WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | |
863 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
864 | |
865 // SetValueSilently/RemoveValueSilently. | |
866 pref_store->SetValueSilently("lossy", new base::StringValue("lossy"), | |
867 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | |
868 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
869 pref_store->RemoveValueSilently("lossy", | |
870 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | |
871 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
872 | |
873 // ReportValueChanged. | |
874 pref_store->SetValue("lossy", new base::StringValue("lossy"), | |
875 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | |
876 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
877 pref_store->ReportValueChanged("lossy", | |
878 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | |
879 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
880 | |
881 // Call CommitPendingWrite and check that the lossy pref and the normal pref | |
882 // are there with the last values set above. | |
883 pref_store->CommitPendingWrite(); | |
884 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
885 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | |
886 GetTestFileContents()); | |
887 } | |
888 | |
889 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossyFirst) { | |
890 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | |
891 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); | |
892 | |
893 // Set a lossy pref and check that it is not scheduled to be written. | |
894 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
895 pref_store->SetValue("lossy", new base::StringValue("lossy"), | |
896 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | |
897 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
Mattias Nissler (ping if slow)
2015/05/11 07:17:10
suggestion: blank line before comment
raymes
2015/05/11 07:35:07
Done.
| |
898 // Set a normal pref and check that it is scheduled to be written. | |
899 pref_store->SetValue("normal", new base::StringValue("normal"), | |
900 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | |
901 ASSERT_TRUE(file_writer->HasPendingWrite()); | |
Mattias Nissler (ping if slow)
2015/05/11 07:17:10
ditto
raymes
2015/05/11 07:35:07
Done.
| |
902 // Call DoScheduledWrite and check both prefs get written. | |
903 file_writer->DoScheduledWrite(); | |
904 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | |
905 GetTestFileContents()); | |
906 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
907 } | |
908 | |
909 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossySecond) { | |
910 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | |
911 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); | |
912 | |
913 // Set a normal pref and check that it is scheduled to be written. | |
914 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
915 pref_store->SetValue("normal", new base::StringValue("normal"), | |
916 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | |
917 ASSERT_TRUE(file_writer->HasPendingWrite()); | |
Mattias Nissler (ping if slow)
2015/05/11 07:17:10
ditto
raymes
2015/05/11 07:35:07
Done.
| |
918 // Set a lossy pref and check that the write is still scheduled. | |
919 pref_store->SetValue("lossy", new base::StringValue("lossy"), | |
920 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | |
921 ASSERT_TRUE(file_writer->HasPendingWrite()); | |
Mattias Nissler (ping if slow)
2015/05/11 07:17:10
ditto
raymes
2015/05/11 07:35:07
Done.
| |
922 // Call DoScheduledWrite and check both prefs get written. | |
923 file_writer->DoScheduledWrite(); | |
924 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | |
925 GetTestFileContents()); | |
926 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
927 } | |
928 | |
812 } // namespace base | 929 } // namespace base |
OLD | NEW |