Chromium Code Reviews| Index: components/subresource_filter/core/common/scoped_timers_unittest.cc |
| diff --git a/components/subresource_filter/core/common/scoped_timers_unittest.cc b/components/subresource_filter/core/common/scoped_timers_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c3dd0912b6f3ff60a4cad749075f495bb37a7b09 |
| --- /dev/null |
| +++ b/components/subresource_filter/core/common/scoped_timers_unittest.cc |
| @@ -0,0 +1,109 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/subresource_filter/core/common/scoped_timers.h" |
| + |
| +#include "base/test/histogram_tester.h" |
| +#include "base/time/time.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +class ExportFunctorForTests { |
| + public: |
| + int number_of_calls() const { return number_of_calls_; } |
| + void operator()(base::TimeDelta) { ++number_of_calls_; } |
| + |
| + private: |
| + int number_of_calls_ = 0; |
| +}; |
| + |
| +} // namespace |
| + |
| +namespace subresource_filter { |
| + |
| +TEST(ScopedTimersTest, ScopedTimerCallsFunctor) { |
| + ExportFunctorForTests export_functor; |
| + { |
| + SCOPED_TIMER(std::ref(export_functor)); |
| + EXPECT_EQ(0, export_functor.number_of_calls()); |
| + } |
| + EXPECT_EQ(1, export_functor.number_of_calls()); |
| +} |
| + |
| +TEST(ScopedTimersTest, ScopedThreadTimerCallsFunctor) { |
| + ExportFunctorForTests export_functor; |
| + { |
| + SCOPED_THREAD_TIMER(std::ref(export_functor)); |
| + EXPECT_EQ(0, export_functor.number_of_calls()); |
| + } |
| + EXPECT_EQ(1, export_functor.number_of_calls()); |
| +} |
| + |
| +TEST(ScopedTimersTest, ScopedTimersCopyFunctor) { |
| + ExportFunctorForTests export_functor; |
| + { |
| + SCOPED_TIMER(export_functor); |
| + SCOPED_THREAD_TIMER(export_functor); |
| + EXPECT_EQ(0, export_functor.number_of_calls()); |
| + } |
| + EXPECT_EQ(0, export_functor.number_of_calls()); |
| +} |
| + |
| +TEST(ScopedTimersTest, ScopedThreadTimerCallsStoredLambdaFunctor) { |
| + bool export_is_called = false; |
| + auto export_functor = [&export_is_called](base::TimeDelta) { |
| + export_is_called = true; |
| + }; |
| + |
| + { |
| + SCOPED_THREAD_TIMER(export_functor); |
| + EXPECT_FALSE(export_is_called); |
| + } |
| + EXPECT_TRUE(export_is_called); |
| +} |
| + |
| +TEST(ScopedTimersTest, ScopedTimerCallsStoredLambdaFunctor) { |
| + bool export_is_called = false; |
| + auto export_functor = [&export_is_called](base::TimeDelta) { |
| + export_is_called = true; |
| + }; |
| + |
| + { |
| + SCOPED_TIMER(export_functor); |
| + EXPECT_FALSE(export_is_called); |
| + } |
| + EXPECT_TRUE(export_is_called); |
| +} |
| + |
| +TEST(ScopedTimersTest, ScopedUmaHistogramMacros) { |
| + base::HistogramTester tester; |
| + { |
| + SCOPED_UMA_HISTOGRAM_THREAD_TIMER("ScopedTimers.ThreadTimer"); |
| + SCOPED_UMA_HISTOGRAM_MICRO_THREAD_TIMER("ScopedTimers.MicroThreadTimer"); |
| + SCOPED_UMA_HISTOGRAM_MICRO_TIMER("ScopedTimers.MicroTimer"); |
|
pkalinnikov
2016/11/28 13:58:36
Should I do something to suppress the following wa
|
| + |
| + tester.ExpectTotalCount("ScopedTimers.ThreadTimer", 0); |
| + tester.ExpectTotalCount("ScopedTimers.MicroThreadTimer", 0); |
| + tester.ExpectTotalCount("ScopedTimers.MicroTimer", 0); |
| + } |
| + |
| + tester.ExpectTotalCount("ScopedTimers.ThreadTimer", 1); |
| + tester.ExpectTotalCount("ScopedTimers.MicroThreadTimer", 1); |
| + tester.ExpectTotalCount("ScopedTimers.MicroTimer", 1); |
| +} |
| + |
| +TEST(ScopedTimersTest, UmaHistogramMicroTimesFromExportFunctor) { |
| + base::HistogramTester tester; |
| + auto export_functor = [](base::TimeDelta delta) { |
| + UMA_HISTOGRAM_MICRO_TIMES("ScopedTimers.MicroTimes", delta); |
| + }; |
| + { |
| + SCOPED_TIMER(export_functor); |
| + tester.ExpectTotalCount("ScopedTimers.MicroTimes", 0); |
| + } |
| + tester.ExpectTotalCount("ScopedTimers.MicroTimes", 1); |
| +} |
| + |
| +} // namespace subresource_filter |