OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef IOS_CHROME_TEST_APP_HISTOGRAM_TEST_UTIL_H_ |
| 6 #define IOS_CHROME_TEST_APP_HISTOGRAM_TEST_UTIL_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <memory> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/metrics/histogram.h" |
| 14 #include "base/metrics/histogram_base.h" |
| 15 |
| 16 // TODO(crbug.com/640141): factorize with base::HistogramTester and add |
| 17 // unittests. |
| 18 |
| 19 typedef void (^FailureBlock)(NSString*); |
| 20 |
| 21 namespace base { |
| 22 class HistogramSamples; |
| 23 } |
| 24 |
| 25 namespace chrome_test_util { |
| 26 |
| 27 struct Bucket; |
| 28 |
| 29 // HistogramTestUtil provides a simple interface for examining histograms, UMA |
| 30 // or otherwise. Earl Grey tests can use this interface to verify that histogram |
| 31 // data is getting logged as intended. |
| 32 class HistogramTester { |
| 33 public: |
| 34 using CountsMap = std::map<std::string, base::HistogramBase::Count>; |
| 35 |
| 36 // The constructor will call StatisticsRecorder::Initialize() for you. Also, |
| 37 // this takes a snapshot of all current histograms counts. |
| 38 HistogramTester(); |
| 39 ~HistogramTester(); |
| 40 |
| 41 // We know the exact number of samples in a bucket, and that no other bucket |
| 42 // should have samples. Measures the diff from the snapshot taken when this |
| 43 // object was constructed. |
| 44 // Returns true if the bucket contains |expected_count| samples and no other |
| 45 // buckets have samples. If not, call |failure_block| with a descriptive text |
| 46 // of the error. |
| 47 BOOL ExpectUniqueSample(const std::string& name, |
| 48 base::HistogramBase::Sample sample, |
| 49 base::HistogramBase::Count expected_count, |
| 50 FailureBlock failure_block) const; |
| 51 |
| 52 // We know the exact number of samples in a bucket, but other buckets may |
| 53 // have samples as well. Measures the diff from the snapshot taken when this |
| 54 // object was constructed. |
| 55 // Returns true if the bucket contains |expected_count| samples. If not, call |
| 56 // |failure_block| with a descriptive text of the error. |
| 57 BOOL ExpectBucketCount(const std::string& name, |
| 58 base::HistogramBase::Sample sample, |
| 59 base::HistogramBase::Count expected_count, |
| 60 FailureBlock failure_block) const; |
| 61 |
| 62 // We don't know the values of the samples, but we know how many there are. |
| 63 // This measures the diff from the snapshot taken when this object was |
| 64 // constructed. |
| 65 // Returns true if the histogram contains |count| samples. If not, call |
| 66 // |failure_block| with a descriptive text of the error. |
| 67 BOOL ExpectTotalCount(const std::string& name, |
| 68 base::HistogramBase::Count count, |
| 69 FailureBlock failure_block) const; |
| 70 |
| 71 // Returns a list of all of the buckets recorded since creation of this |
| 72 // object, as vector<Bucket>, where a Bucket represents the min boundary of |
| 73 // the bucket and the count of samples recorded to that bucket since creation. |
| 74 // If there is not histogram named |name|, return an empty vector. |
| 75 std::vector<Bucket> GetAllSamples(const std::string& name) const; |
| 76 |
| 77 // Returns a modified HistogramSamples containing only what has been logged |
| 78 // to the histogram since the creation of this object. Returns an empty vector |
| 79 // if the histogram is not found. |
| 80 std::unique_ptr<base::HistogramSamples> GetHistogramSamplesSinceCreation( |
| 81 const std::string& histogram_name) const; |
| 82 |
| 83 private: |
| 84 // Verifies and asserts that value in the |sample| bucket matches the |
| 85 // |expected_count|. The bucket's current value is determined from |samples| |
| 86 // and is modified based on the snapshot stored for histogram |name|. |
| 87 BOOL CheckBucketCount(const std::string& name, |
| 88 base::HistogramBase::Sample sample, |
| 89 base::Histogram::Count expected_count, |
| 90 const base::HistogramSamples& samples, |
| 91 FailureBlock failure_block) const; |
| 92 |
| 93 // Verifies that the total number of values recorded for the histogram |name| |
| 94 // is |expected_count|. This is checked against |samples| minus the snapshot |
| 95 // that was taken for |name|. |
| 96 BOOL CheckTotalCount(const std::string& name, |
| 97 base::Histogram::Count expected_count, |
| 98 const base::HistogramSamples& samples, |
| 99 FailureBlock failure_block) const; |
| 100 |
| 101 // Used to determine the histogram changes made during this instance's |
| 102 // lifecycle. This instance takes ownership of the samples, which are deleted |
| 103 // when the instance is destroyed. |
| 104 std::map<std::string, std::unique_ptr<base::HistogramSamples>> |
| 105 histograms_snapshot_; |
| 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(HistogramTester); |
| 108 }; |
| 109 |
| 110 struct Bucket { |
| 111 Bucket(base::HistogramBase::Sample min, base::HistogramBase::Count count) |
| 112 : min(min), count(count) {} |
| 113 |
| 114 bool operator==(const Bucket& other) const; |
| 115 |
| 116 base::HistogramBase::Sample min; |
| 117 base::HistogramBase::Count count; |
| 118 }; |
| 119 |
| 120 } // namespace chrome_test_util |
| 121 |
| 122 #endif // IOS_CHROME_TEST_APP_HISTOGRAM_TEST_UTIL_H_ |
OLD | NEW |