| 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 <stdint.h> |
| 8 |
| 7 #include <utility> | 9 #include <utility> |
| 8 | 10 |
| 9 #include "base/bind.h" | 11 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 16 #include "base/metrics/histogram_samples.h" | 19 #include "base/metrics/histogram_samples.h" |
| 17 #include "base/metrics/statistics_recorder.h" | 20 #include "base/metrics/statistics_recorder.h" |
| 18 #include "base/path_service.h" | 21 #include "base/path_service.h" |
| 19 #include "base/prefs/pref_filter.h" | 22 #include "base/prefs/pref_filter.h" |
| 20 #include "base/run_loop.h" | 23 #include "base/run_loop.h" |
| 21 #include "base/single_thread_task_runner.h" | 24 #include "base/single_thread_task_runner.h" |
| 22 #include "base/strings/string_number_conversions.h" | 25 #include "base/strings/string_number_conversions.h" |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); | 226 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
| 224 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 227 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
| 225 EXPECT_EQ(10, integer); | 228 EXPECT_EQ(10, integer); |
| 226 | 229 |
| 227 pref_store->SetValue( | 230 pref_store->SetValue( |
| 228 kLongIntPref, | 231 kLongIntPref, |
| 229 make_scoped_ptr(new StringValue(base::Int64ToString(214748364842LL))), | 232 make_scoped_ptr(new StringValue(base::Int64ToString(214748364842LL))), |
| 230 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 233 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 231 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); | 234 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); |
| 232 EXPECT_TRUE(actual->GetAsString(&string_value)); | 235 EXPECT_TRUE(actual->GetAsString(&string_value)); |
| 233 int64 value; | 236 int64_t value; |
| 234 base::StringToInt64(string_value, &value); | 237 base::StringToInt64(string_value, &value); |
| 235 EXPECT_EQ(214748364842LL, value); | 238 EXPECT_EQ(214748364842LL, value); |
| 236 | 239 |
| 237 // Serialize and compare to expected output. | 240 // Serialize and compare to expected output. |
| 238 ASSERT_TRUE(PathExists(golden_output_file)); | 241 ASSERT_TRUE(PathExists(golden_output_file)); |
| 239 pref_store->CommitPendingWrite(); | 242 pref_store->CommitPendingWrite(); |
| 240 RunLoop().RunUntilIdle(); | 243 RunLoop().RunUntilIdle(); |
| 241 EXPECT_TRUE(TextContentsEqual(golden_output_file, output_file)); | 244 EXPECT_TRUE(TextContentsEqual(golden_output_file, output_file)); |
| 242 ASSERT_TRUE(base::DeleteFile(output_file, false)); | 245 ASSERT_TRUE(base::DeleteFile(output_file, false)); |
| 243 } | 246 } |
| (...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 673 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 671 } | 674 } |
| 672 | 675 |
| 673 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) { | 676 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) { |
| 674 SimpleTestClock* test_clock = new SimpleTestClock; | 677 SimpleTestClock* test_clock = new SimpleTestClock; |
| 675 SetCurrentTimeInMinutes(0, test_clock); | 678 SetCurrentTimeInMinutes(0, test_clock); |
| 676 JsonPrefStore::WriteCountHistogram histogram( | 679 JsonPrefStore::WriteCountHistogram histogram( |
| 677 base::TimeDelta::FromSeconds(10), | 680 base::TimeDelta::FromSeconds(10), |
| 678 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | 681 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
| 679 scoped_ptr<base::Clock>(test_clock)); | 682 scoped_ptr<base::Clock>(test_clock)); |
| 680 int32 report_interval = | 683 int32_t report_interval = |
| 681 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | 684 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 682 | 685 |
| 683 histogram.RecordWriteOccured(); | 686 histogram.RecordWriteOccured(); |
| 684 | 687 |
| 685 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); | 688 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 686 histogram.ReportOutstandingWrites(); | 689 histogram.ReportOutstandingWrites(); |
| 687 scoped_ptr<HistogramSamples> samples = | 690 scoped_ptr<HistogramSamples> samples = |
| 688 histogram.GetHistogram()->SnapshotSamples(); | 691 histogram.GetHistogram()->SnapshotSamples(); |
| 689 ASSERT_EQ(1, samples->GetCount(1)); | 692 ASSERT_EQ(1, samples->GetCount(1)); |
| 690 ASSERT_EQ(1, samples->TotalCount()); | 693 ASSERT_EQ(1, samples->TotalCount()); |
| 691 | 694 |
| 692 ASSERT_EQ("Settings.JsonDataWriteCount.Local_State", | 695 ASSERT_EQ("Settings.JsonDataWriteCount.Local_State", |
| 693 histogram.GetHistogram()->histogram_name()); | 696 histogram.GetHistogram()->histogram_name()); |
| 694 ASSERT_TRUE(histogram.GetHistogram()->HasConstructionArguments(1, 30, 31)); | 697 ASSERT_TRUE(histogram.GetHistogram()->HasConstructionArguments(1, 30, 31)); |
| 695 } | 698 } |
| 696 | 699 |
| 697 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod) { | 700 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod) { |
| 698 SimpleTestClock* test_clock = new SimpleTestClock; | 701 SimpleTestClock* test_clock = new SimpleTestClock; |
| 699 SetCurrentTimeInMinutes(0, test_clock); | 702 SetCurrentTimeInMinutes(0, test_clock); |
| 700 JsonPrefStore::WriteCountHistogram histogram( | 703 JsonPrefStore::WriteCountHistogram histogram( |
| 701 base::TimeDelta::FromSeconds(10), | 704 base::TimeDelta::FromSeconds(10), |
| 702 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | 705 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
| 703 scoped_ptr<base::Clock>(test_clock)); | 706 scoped_ptr<base::Clock>(test_clock)); |
| 704 int32 report_interval = | 707 int32_t report_interval = |
| 705 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | 708 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 706 | 709 |
| 707 histogram.RecordWriteOccured(); | 710 histogram.RecordWriteOccured(); |
| 708 SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); | 711 SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
| 709 histogram.RecordWriteOccured(); | 712 histogram.RecordWriteOccured(); |
| 710 SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); | 713 SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
| 711 histogram.RecordWriteOccured(); | 714 histogram.RecordWriteOccured(); |
| 712 | 715 |
| 713 // Nothing should be recorded until the report period has elapsed. | 716 // Nothing should be recorded until the report period has elapsed. |
| 714 scoped_ptr<HistogramSamples> samples = | 717 scoped_ptr<HistogramSamples> samples = |
| (...skipping 18 matching lines...) Expand all Loading... |
| 733 ASSERT_EQ(1, samples->TotalCount()); | 736 ASSERT_EQ(1, samples->TotalCount()); |
| 734 } | 737 } |
| 735 | 738 |
| 736 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods) { | 739 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods) { |
| 737 SimpleTestClock* test_clock = new SimpleTestClock; | 740 SimpleTestClock* test_clock = new SimpleTestClock; |
| 738 SetCurrentTimeInMinutes(0, test_clock); | 741 SetCurrentTimeInMinutes(0, test_clock); |
| 739 JsonPrefStore::WriteCountHistogram histogram( | 742 JsonPrefStore::WriteCountHistogram histogram( |
| 740 base::TimeDelta::FromSeconds(10), | 743 base::TimeDelta::FromSeconds(10), |
| 741 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | 744 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
| 742 scoped_ptr<base::Clock>(test_clock)); | 745 scoped_ptr<base::Clock>(test_clock)); |
| 743 int32 report_interval = | 746 int32_t report_interval = |
| 744 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | 747 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 745 | 748 |
| 746 histogram.RecordWriteOccured(); | 749 histogram.RecordWriteOccured(); |
| 747 SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); | 750 SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
| 748 histogram.RecordWriteOccured(); | 751 histogram.RecordWriteOccured(); |
| 749 SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); | 752 SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
| 750 histogram.RecordWriteOccured(); | 753 histogram.RecordWriteOccured(); |
| 751 SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); | 754 SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); |
| 752 histogram.RecordWriteOccured(); | 755 histogram.RecordWriteOccured(); |
| 753 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); | 756 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 772 ASSERT_EQ(3, samples->TotalCount()); | 775 ASSERT_EQ(3, samples->TotalCount()); |
| 773 } | 776 } |
| 774 | 777 |
| 775 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) { | 778 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) { |
| 776 SimpleTestClock* test_clock = new SimpleTestClock; | 779 SimpleTestClock* test_clock = new SimpleTestClock; |
| 777 SetCurrentTimeInMinutes(0, test_clock); | 780 SetCurrentTimeInMinutes(0, test_clock); |
| 778 JsonPrefStore::WriteCountHistogram histogram( | 781 JsonPrefStore::WriteCountHistogram histogram( |
| 779 base::TimeDelta::FromSeconds(10), | 782 base::TimeDelta::FromSeconds(10), |
| 780 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | 783 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
| 781 scoped_ptr<base::Clock>(test_clock)); | 784 scoped_ptr<base::Clock>(test_clock)); |
| 782 int32 report_interval = | 785 int32_t report_interval = |
| 783 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | 786 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 784 | 787 |
| 785 // 1 write in the first period. | 788 // 1 write in the first period. |
| 786 histogram.RecordWriteOccured(); | 789 histogram.RecordWriteOccured(); |
| 787 | 790 |
| 788 // No writes in the second and third periods. | 791 // No writes in the second and third periods. |
| 789 | 792 |
| 790 // 2 writes in the fourth period. | 793 // 2 writes in the fourth period. |
| 791 SetCurrentTimeInMinutes(3.1 * report_interval, test_clock); | 794 SetCurrentTimeInMinutes(3.1 * report_interval, test_clock); |
| 792 histogram.RecordWriteOccured(); | 795 histogram.RecordWriteOccured(); |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 953 ASSERT_TRUE(file_writer->HasPendingWrite()); | 956 ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 954 | 957 |
| 955 // Call CommitPendingWrite and check that the lossy pref is there with the | 958 // Call CommitPendingWrite and check that the lossy pref is there with the |
| 956 // last value set above. | 959 // last value set above. |
| 957 pref_store->CommitPendingWrite(); | 960 pref_store->CommitPendingWrite(); |
| 958 ASSERT_FALSE(file_writer->HasPendingWrite()); | 961 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 959 ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); | 962 ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); |
| 960 } | 963 } |
| 961 | 964 |
| 962 } // namespace base | 965 } // namespace base |
| OLD | NEW |