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/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/metrics/histogram_samples.h" |
| 14 #include "base/metrics/statistics_recorder.h" |
13 #include "base/path_service.h" | 15 #include "base/path_service.h" |
14 #include "base/prefs/pref_filter.h" | 16 #include "base/prefs/pref_filter.h" |
15 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
16 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
18 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 21 #include "base/test/simple_test_clock.h" |
19 #include "base/threading/sequenced_worker_pool.h" | 22 #include "base/threading/sequenced_worker_pool.h" |
20 #include "base/threading/thread.h" | 23 #include "base/threading/thread.h" |
21 #include "base/values.h" | 24 #include "base/values.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
24 | 27 |
25 namespace base { | 28 namespace base { |
26 namespace { | 29 namespace { |
27 | 30 |
28 const char kHomePage[] = "homepage"; | 31 const char kHomePage[] = "homepage"; |
29 | 32 |
| 33 // Set the time on the given SimpleTestClock to the given time in minutes. |
| 34 void SetCurrentTimeInMinutes(double minutes, base::SimpleTestClock* clock) { |
| 35 const int32_t kBaseTimeMins = 100; |
| 36 clock->SetNow(base::Time::FromDoubleT((kBaseTimeMins + minutes) * 60)); |
| 37 } |
| 38 |
30 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on | 39 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on |
31 // to the |prefs| until explicitly asked to release them. | 40 // to the |prefs| until explicitly asked to release them. |
32 class InterceptingPrefFilter : public PrefFilter { | 41 class InterceptingPrefFilter : public PrefFilter { |
33 public: | 42 public: |
34 InterceptingPrefFilter(); | 43 InterceptingPrefFilter(); |
35 ~InterceptingPrefFilter() override; | 44 ~InterceptingPrefFilter() override; |
36 | 45 |
37 // PrefFilter implementation: | 46 // PrefFilter implementation: |
38 void FilterOnLoad( | 47 void FilterOnLoad( |
39 const PostFilterOnLoadCallback& post_filter_on_load_callback, | 48 const PostFilterOnLoadCallback& post_filter_on_load_callback, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); | 109 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); |
101 message_loop_.Run(); | 110 message_loop_.Run(); |
102 } | 111 } |
103 | 112 |
104 // The path to temporary directory used to contain the test operations. | 113 // The path to temporary directory used to contain the test operations. |
105 base::ScopedTempDir temp_dir_; | 114 base::ScopedTempDir temp_dir_; |
106 // The path to the directory where the test data is stored. | 115 // The path to the directory where the test data is stored. |
107 base::FilePath data_dir_; | 116 base::FilePath data_dir_; |
108 // A message loop that we can use as the file thread message loop. | 117 // A message loop that we can use as the file thread message loop. |
109 MessageLoop message_loop_; | 118 MessageLoop message_loop_; |
| 119 |
| 120 private: |
| 121 // Ensure histograms are reset for each test. |
| 122 StatisticsRecorder statistics_recorder_; |
110 }; | 123 }; |
111 | 124 |
112 // Test fallback behavior for a nonexistent file. | 125 // Test fallback behavior for a nonexistent file. |
113 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 126 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
114 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 127 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
115 ASSERT_FALSE(PathExists(bogus_input_file)); | 128 ASSERT_FALSE(PathExists(bogus_input_file)); |
116 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 129 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
117 bogus_input_file, | 130 bogus_input_file, |
118 message_loop_.message_loop_proxy().get(), | 131 message_loop_.message_loop_proxy().get(), |
119 scoped_ptr<PrefFilter>()); | 132 scoped_ptr<PrefFilter>()); |
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 // "tabs": { | 677 // "tabs": { |
665 // "new_windows_in_tabs": true, | 678 // "new_windows_in_tabs": true, |
666 // "max_tabs": 20 | 679 // "max_tabs": 20 |
667 // } | 680 // } |
668 // } | 681 // } |
669 | 682 |
670 RunBasicJsonPrefStoreTest( | 683 RunBasicJsonPrefStoreTest( |
671 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 684 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
672 } | 685 } |
673 | 686 |
| 687 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) { |
| 688 SimpleTestClock* test_clock = new SimpleTestClock; |
| 689 SetCurrentTimeInMinutes(0, test_clock); |
| 690 JsonPrefStore::WriteCountHistogram histogram( |
| 691 base::TimeDelta::FromSeconds(10), |
| 692 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
| 693 scoped_ptr<base::Clock>(test_clock)); |
| 694 int32 report_interval = |
| 695 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 696 |
| 697 histogram.RecordWriteOccured(); |
| 698 |
| 699 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 700 histogram.ReportOutstandingWrites(); |
| 701 scoped_ptr<HistogramSamples> samples = |
| 702 histogram.GetHistogram()->SnapshotSamples(); |
| 703 ASSERT_EQ(1, samples->GetCount(1)); |
| 704 ASSERT_EQ(1, samples->TotalCount()); |
| 705 |
| 706 ASSERT_EQ("Settings.JsonDataWriteCount.Local_State", |
| 707 histogram.GetHistogram()->histogram_name()); |
| 708 ASSERT_TRUE(histogram.GetHistogram()->HasConstructionArguments(1, 30, 31)); |
| 709 } |
| 710 |
| 711 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod) { |
| 712 SimpleTestClock* test_clock = new SimpleTestClock; |
| 713 SetCurrentTimeInMinutes(0, test_clock); |
| 714 JsonPrefStore::WriteCountHistogram histogram( |
| 715 base::TimeDelta::FromSeconds(10), |
| 716 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
| 717 scoped_ptr<base::Clock>(test_clock)); |
| 718 int32 report_interval = |
| 719 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 720 |
| 721 histogram.RecordWriteOccured(); |
| 722 SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
| 723 histogram.RecordWriteOccured(); |
| 724 SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
| 725 histogram.RecordWriteOccured(); |
| 726 |
| 727 // Nothing should be recorded until the report period has elapsed. |
| 728 scoped_ptr<HistogramSamples> samples = |
| 729 histogram.GetHistogram()->SnapshotSamples(); |
| 730 ASSERT_EQ(0, samples->TotalCount()); |
| 731 |
| 732 SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); |
| 733 histogram.RecordWriteOccured(); |
| 734 |
| 735 // Now the report period has elapsed. |
| 736 samples = histogram.GetHistogram()->SnapshotSamples(); |
| 737 ASSERT_EQ(1, samples->GetCount(3)); |
| 738 ASSERT_EQ(1, samples->TotalCount()); |
| 739 |
| 740 // The last write won't be recorded because the second count period hasn't |
| 741 // fully elapsed. |
| 742 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 743 histogram.ReportOutstandingWrites(); |
| 744 |
| 745 samples = histogram.GetHistogram()->SnapshotSamples(); |
| 746 ASSERT_EQ(1, samples->GetCount(3)); |
| 747 ASSERT_EQ(1, samples->TotalCount()); |
| 748 } |
| 749 |
| 750 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods) { |
| 751 SimpleTestClock* test_clock = new SimpleTestClock; |
| 752 SetCurrentTimeInMinutes(0, test_clock); |
| 753 JsonPrefStore::WriteCountHistogram histogram( |
| 754 base::TimeDelta::FromSeconds(10), |
| 755 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
| 756 scoped_ptr<base::Clock>(test_clock)); |
| 757 int32 report_interval = |
| 758 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 759 |
| 760 histogram.RecordWriteOccured(); |
| 761 SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
| 762 histogram.RecordWriteOccured(); |
| 763 SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
| 764 histogram.RecordWriteOccured(); |
| 765 SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); |
| 766 histogram.RecordWriteOccured(); |
| 767 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 768 histogram.RecordWriteOccured(); |
| 769 SetCurrentTimeInMinutes(2.1 * report_interval, test_clock); |
| 770 histogram.RecordWriteOccured(); |
| 771 SetCurrentTimeInMinutes(2.5 * report_interval, test_clock); |
| 772 histogram.RecordWriteOccured(); |
| 773 SetCurrentTimeInMinutes(2.7 * report_interval, test_clock); |
| 774 histogram.RecordWriteOccured(); |
| 775 SetCurrentTimeInMinutes(3.3 * report_interval, test_clock); |
| 776 histogram.RecordWriteOccured(); |
| 777 |
| 778 // The last write won't be recorded because the second count period hasn't |
| 779 // fully elapsed |
| 780 SetCurrentTimeInMinutes(3.5 * report_interval, test_clock); |
| 781 histogram.ReportOutstandingWrites(); |
| 782 scoped_ptr<HistogramSamples> samples = |
| 783 histogram.GetHistogram()->SnapshotSamples(); |
| 784 ASSERT_EQ(2, samples->GetCount(3)); |
| 785 ASSERT_EQ(1, samples->GetCount(2)); |
| 786 ASSERT_EQ(3, samples->TotalCount()); |
| 787 } |
| 788 |
| 789 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) { |
| 790 SimpleTestClock* test_clock = new SimpleTestClock; |
| 791 SetCurrentTimeInMinutes(0, test_clock); |
| 792 JsonPrefStore::WriteCountHistogram histogram( |
| 793 base::TimeDelta::FromSeconds(10), |
| 794 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
| 795 scoped_ptr<base::Clock>(test_clock)); |
| 796 int32 report_interval = |
| 797 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 798 |
| 799 // 1 write in the first period. |
| 800 histogram.RecordWriteOccured(); |
| 801 |
| 802 // No writes in the second and third periods. |
| 803 |
| 804 // 2 writes in the fourth period. |
| 805 SetCurrentTimeInMinutes(3.1 * report_interval, test_clock); |
| 806 histogram.RecordWriteOccured(); |
| 807 SetCurrentTimeInMinutes(3.3 * report_interval, test_clock); |
| 808 histogram.RecordWriteOccured(); |
| 809 |
| 810 // No writes in the fifth period. |
| 811 |
| 812 // 3 writes in the sixth period. |
| 813 SetCurrentTimeInMinutes(5.1 * report_interval, test_clock); |
| 814 histogram.RecordWriteOccured(); |
| 815 SetCurrentTimeInMinutes(5.3 * report_interval, test_clock); |
| 816 histogram.RecordWriteOccured(); |
| 817 SetCurrentTimeInMinutes(5.5 * report_interval, test_clock); |
| 818 histogram.RecordWriteOccured(); |
| 819 |
| 820 SetCurrentTimeInMinutes(6.1 * report_interval, test_clock); |
| 821 histogram.ReportOutstandingWrites(); |
| 822 scoped_ptr<HistogramSamples> samples = |
| 823 histogram.GetHistogram()->SnapshotSamples(); |
| 824 ASSERT_EQ(3, samples->GetCount(0)); |
| 825 ASSERT_EQ(1, samples->GetCount(1)); |
| 826 ASSERT_EQ(1, samples->GetCount(2)); |
| 827 ASSERT_EQ(1, samples->GetCount(3)); |
| 828 ASSERT_EQ(6, samples->TotalCount()); |
| 829 } |
| 830 |
674 } // namespace base | 831 } // namespace base |
OLD | NEW |