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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/logging.h" | |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/metrics/histogram_persistence.h" | 14 #include "base/metrics/histogram_persistence.h" |
| 14 #include "base/metrics/sparse_histogram.h" | 15 #include "base/metrics/sparse_histogram.h" |
| 15 #include "base/metrics/statistics_recorder.h" | 16 #include "base/metrics/statistics_recorder.h" |
| 16 #include "base/values.h" | 17 #include "base/values.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace base { | 20 namespace base { |
| 20 | 21 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 return new Histogram(name, min, max, registered_ranges); | 55 return new Histogram(name, min, max, registered_ranges); |
| 55 } | 56 } |
| 56 | 57 |
| 57 void DeleteHistogram(HistogramBase* histogram) { | 58 void DeleteHistogram(HistogramBase* histogram) { |
| 58 delete histogram; | 59 delete histogram; |
| 59 } | 60 } |
| 60 | 61 |
| 61 StatisticsRecorder* statistics_recorder_; | 62 StatisticsRecorder* statistics_recorder_; |
| 62 }; | 63 }; |
| 63 | 64 |
| 65 // Class to make sure any manipulations we do to the min log level are | |
| 66 // contained (i.e., do not affect other unit tests). | |
| 67 class LogStateSaver { | |
|
Alexei Svitkine (slow)
2016/04/14 20:47:39
Put this in an anon namespace at the top of the fi
| |
| 68 public: | |
| 69 LogStateSaver() : old_min_log_level_(logging::GetMinLogLevel()) {} | |
| 70 | |
| 71 ~LogStateSaver() { | |
| 72 logging::SetMinLogLevel(old_min_log_level_); | |
| 73 logging::SetLogAssertHandler(nullptr); | |
| 74 } | |
| 75 | |
| 76 private: | |
| 77 int old_min_log_level_; | |
| 78 | |
| 79 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); | |
| 80 }; | |
| 81 | |
| 82 class StatisticsRecorderLoggingTestBase : public StatisticsRecorderTest { | |
| 83 protected: | |
| 84 void InitLogOnShutdown() { | |
| 85 DCHECK(statistics_recorder_); | |
| 86 statistics_recorder_->InitLogOnShutdownWithoutLock(); | |
| 87 } | |
| 88 | |
| 89 private: | |
| 90 LogStateSaver log_state_saver_; | |
|
Alexei Svitkine (slow)
2016/04/14 20:47:39
Is there any problem doing this unconditionally fo
| |
| 91 }; | |
| 92 | |
| 93 class StatisticsRecorderLoggingWarningOnStartupTest | |
|
Alexei Svitkine (slow)
2016/04/14 20:47:39
You don't need to create a separate test harness c
| |
| 94 : public StatisticsRecorderLoggingTestBase { | |
| 95 protected: | |
| 96 void SetUp() override { | |
| 97 logging::SetMinLogLevel(logging::LOG_WARNING); | |
| 98 StatisticsRecorderLoggingTestBase::SetUp(); | |
| 99 } | |
| 100 }; | |
| 101 | |
| 102 class StatisticsRecorderLoggingVerboseOnStartupTest | |
| 103 : public StatisticsRecorderLoggingTestBase { | |
| 104 protected: | |
| 105 void SetUp() override { | |
| 106 logging::SetMinLogLevel(logging::LOG_VERBOSE); | |
| 107 StatisticsRecorderTest::SetUp(); | |
| 108 } | |
| 109 | |
| 110 private: | |
| 111 LogStateSaver log_state_saver_; | |
| 112 }; | |
| 113 | |
| 114 | |
| 64 TEST_F(StatisticsRecorderTest, NotInitialized) { | 115 TEST_F(StatisticsRecorderTest, NotInitialized) { |
| 65 UninitializeStatisticsRecorder(); | 116 UninitializeStatisticsRecorder(); |
| 66 | 117 |
| 67 ASSERT_FALSE(StatisticsRecorder::IsActive()); | 118 ASSERT_FALSE(StatisticsRecorder::IsActive()); |
| 68 | 119 |
| 69 StatisticsRecorder::Histograms registered_histograms; | 120 StatisticsRecorder::Histograms registered_histograms; |
| 70 std::vector<const BucketRanges*> registered_ranges; | 121 std::vector<const BucketRanges*> registered_ranges; |
| 71 | 122 |
| 72 StatisticsRecorder::GetHistograms(®istered_histograms); | 123 StatisticsRecorder::GetHistograms(®istered_histograms); |
| 73 EXPECT_EQ(0u, registered_histograms.size()); | 124 EXPECT_EQ(0u, registered_histograms.size()); |
| (...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 506 | 557 |
| 507 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10, | 558 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10, |
| 508 HistogramBase::kNoFlags); | 559 HistogramBase::kNoFlags); |
| 509 EXPECT_TRUE(histogram); | 560 EXPECT_TRUE(histogram); |
| 510 histogram->Add(1); | 561 histogram->Add(1); |
| 511 | 562 |
| 512 EXPECT_TRUE(callback_wrapper.called); | 563 EXPECT_TRUE(callback_wrapper.called); |
| 513 EXPECT_EQ(callback_wrapper.last_histogram_value, 1); | 564 EXPECT_EQ(callback_wrapper.last_histogram_value, 1); |
| 514 } | 565 } |
| 515 | 566 |
| 567 TEST_F(StatisticsRecorderLoggingWarningOnStartupTest, | |
| 568 LogOnShutdownNotInitialized) { | |
| 569 EXPECT_FALSE(VLOG_IS_ON(1)); | |
| 570 EXPECT_FALSE(StatisticsRecorder::VLogInitializedForTesting()); | |
| 571 InitLogOnShutdown(); | |
| 572 EXPECT_FALSE(StatisticsRecorder::VLogInitializedForTesting()); | |
| 573 } | |
| 574 | |
| 575 TEST_F(StatisticsRecorderLoggingWarningOnStartupTest, | |
| 576 LogOnShutdownInitializedExplicitly) { | |
| 577 EXPECT_FALSE(VLOG_IS_ON(1)); | |
| 578 EXPECT_FALSE(StatisticsRecorder::VLogInitializedForTesting()); | |
| 579 logging::SetMinLogLevel(logging::LOG_VERBOSE); | |
| 580 EXPECT_TRUE(VLOG_IS_ON(1)); | |
| 581 InitLogOnShutdown(); | |
| 582 EXPECT_TRUE(StatisticsRecorder::VLogInitializedForTesting()); | |
| 583 } | |
| 584 | |
| 585 TEST_F(StatisticsRecorderLoggingVerboseOnStartupTest, | |
| 586 LogOnShutdownInitialized) { | |
| 587 EXPECT_TRUE(VLOG_IS_ON(1)); | |
| 588 EXPECT_TRUE(StatisticsRecorder::VLogInitializedForTesting()); | |
| 589 } | |
| 590 | |
| 516 } // namespace base | 591 } // namespace base |
| OLD | NEW |