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 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 // A struct used for conveniently setting values in the PrefStore | |
| 815 struct PrefDetails { | |
|
Mattias Nissler (ping if slow)
2015/05/08 07:04:25
This is still here, but I don't see an answer to m
raymes
2015/05/11 00:57:27
I removed this.
| |
| 816 std::string key; | |
| 817 scoped_ptr<base::Value> value; | |
| 818 uint32 flags; | |
| 819 }; | |
| 820 | |
| 821 static const base::FilePath::CharType kTestFile[]; | |
| 822 | |
| 823 static PrefDetails GetNormalPrefDetails() { | |
| 824 return {"normal", | |
| 825 scoped_ptr<base::Value>(new base::StringValue("normal")), | |
| 826 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS}; | |
| 827 } | |
| 828 | |
| 829 static PrefDetails GetLossyPrefDetails() { | |
| 830 return {"lossy", | |
| 831 scoped_ptr<base::Value>(new base::StringValue("lossy")), | |
| 832 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG}; | |
| 833 } | |
| 834 | |
| 835 void SetUp() override { | |
| 836 JsonPrefStoreTest::SetUp(); | |
| 837 base::DeleteFile(base::FilePath(kTestFile), false); | |
| 838 } | |
| 839 | |
| 840 void TearDown() override { | |
| 841 base::DeleteFile(base::FilePath(kTestFile), false); | |
| 842 JsonPrefStoreTest::TearDown(); | |
| 843 } | |
| 844 | |
| 845 // Creates a JsonPrefStore with the given |file_writer|. | |
| 846 scoped_refptr<JsonPrefStore> CreatePrefStore() { | |
| 847 return new JsonPrefStore(base::FilePath(kTestFile), | |
| 848 message_loop_.task_runner(), | |
| 849 scoped_ptr<PrefFilter>()); | |
| 850 } | |
| 851 | |
| 852 // Return the ImportantFileWriter for a given JsonPrefStore. | |
| 853 ImportantFileWriter* GetImportantFileWriter( | |
| 854 scoped_refptr<JsonPrefStore> pref_store) { | |
| 855 return &(pref_store->writer_); | |
| 856 } | |
| 857 | |
| 858 // Get the contents of kTestFile. Pumps the message loop before returning the | |
| 859 // result. | |
| 860 std::string GetTestFileContents() { | |
| 861 MessageLoop::current()->RunUntilIdle(); | |
| 862 std::string file_contents; | |
| 863 CHECK(ReadFileToString(base::FilePath(kTestFile), &file_contents)); | |
| 864 return file_contents; | |
| 865 } | |
| 866 }; | |
| 867 | |
| 868 // static | |
| 869 const base::FilePath::CharType JsonPrefStoreLossyWriteTest::kTestFile[] = | |
| 870 FILE_PATH_LITERAL("/tmp/Local_State"); | |
| 871 | |
| 872 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteBasic) { | |
| 873 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | |
| 874 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); | |
| 875 | |
| 876 PrefDetails normal = GetNormalPrefDetails(); | |
| 877 PrefDetails lossy = GetLossyPrefDetails(); | |
| 878 | |
| 879 // Set a normal pref and check that it gets scheduled to be written. | |
| 880 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 881 pref_store->SetValue(normal.key, normal.value->DeepCopy(), normal.flags); | |
| 882 ASSERT_TRUE(file_writer->HasPendingWrite()); | |
| 883 file_writer->DoScheduledWrite(); | |
| 884 ASSERT_EQ("{\"normal\":\"normal\"}", GetTestFileContents()); | |
| 885 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 886 | |
| 887 // Set a lossy pref and check that it is not scheduled to be written. | |
| 888 // SetValue/RemoveValue | |
|
Mattias Nissler (ping if slow)
2015/05/08 07:04:25
Missing period?
raymes
2015/05/11 00:57:27
Done.
| |
| 889 pref_store->SetValue(lossy.key, lossy.value->DeepCopy(), lossy.flags); | |
| 890 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 891 pref_store->RemoveValue(lossy.key, lossy.flags); | |
| 892 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 893 | |
| 894 // SetValueSilently/RemoveValueSilently. | |
| 895 pref_store->SetValueSilently(lossy.key, lossy.value->DeepCopy(), lossy.flags); | |
| 896 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 897 pref_store->RemoveValueSilently(lossy.key, lossy.flags); | |
| 898 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 899 | |
| 900 // ReportValueChanged. | |
| 901 pref_store->SetValue(lossy.key, lossy.value->DeepCopy(), lossy.flags); | |
| 902 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 903 pref_store->ReportValueChanged(lossy.key, lossy.flags); | |
| 904 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 905 | |
| 906 // Call CommitPendingWrite and check that the lossy pref and the normal pref | |
| 907 // are there with the last values set above. | |
| 908 pref_store->CommitPendingWrite(); | |
| 909 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 910 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | |
| 911 GetTestFileContents()); | |
| 912 } | |
| 913 | |
| 914 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixed) { | |
| 915 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | |
| 916 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); | |
| 917 | |
| 918 PrefDetails normal = GetNormalPrefDetails(); | |
| 919 PrefDetails lossy = GetLossyPrefDetails(); | |
| 920 | |
| 921 // Set a normal pref and check that it is scheduled to be written. | |
| 922 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 923 pref_store->SetValue(normal.key, normal.value->DeepCopy(), normal.flags); | |
| 924 ASSERT_TRUE(file_writer->HasPendingWrite()); | |
| 925 // Set a lossy pref and check that the write is still scheduled. | |
| 926 pref_store->SetValue(lossy.key, lossy.value->DeepCopy(), lossy.flags); | |
| 927 ASSERT_TRUE(file_writer->HasPendingWrite()); | |
| 928 // Call DoScheduledWrite and check both prefs get written. | |
| 929 file_writer->DoScheduledWrite(); | |
| 930 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | |
| 931 GetTestFileContents()); | |
| 932 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 933 | |
| 934 // Cleanup. | |
|
Mattias Nissler (ping if slow)
2015/05/08 07:04:25
Instead of doing a cleanup and starting fresh, why
raymes
2015/05/11 00:57:27
Done.
| |
| 935 pref_store->RemoveValue(normal.key, normal.flags); | |
| 936 pref_store->RemoveValue(lossy.key, lossy.flags); | |
| 937 pref_store->CommitPendingWrite(); | |
| 938 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 939 | |
| 940 // Set a lossy pref and check that it is not scheduled to be written. | |
| 941 pref_store->SetValue(lossy.key, lossy.value->DeepCopy(), lossy.flags); | |
| 942 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 943 // Set a normal pref and check that it is scheduled to be written. | |
| 944 pref_store->SetValue(normal.key, normal.value->DeepCopy(), normal.flags); | |
| 945 ASSERT_TRUE(file_writer->HasPendingWrite()); | |
| 946 // Call DoScheduledWrite and check both prefs get written. | |
| 947 file_writer->DoScheduledWrite(); | |
| 948 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | |
| 949 GetTestFileContents()); | |
| 950 ASSERT_FALSE(file_writer->HasPendingWrite()); | |
| 951 } | |
| 952 | |
| 812 } // namespace base | 953 } // namespace base |
| OLD | NEW |