| Index: components/memory_pressure/memory_pressure_stats_collector_unittest.cc
|
| diff --git a/components/memory_pressure/memory_pressure_stats_collector_unittest.cc b/components/memory_pressure/memory_pressure_stats_collector_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..899d3de80c4d3bd12cc40c5cc02772c5c6815ee3
|
| --- /dev/null
|
| +++ b/components/memory_pressure/memory_pressure_stats_collector_unittest.cc
|
| @@ -0,0 +1,143 @@
|
| +// Copyright 2015 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/memory_pressure/memory_pressure_stats_collector.h"
|
| +
|
| +#include "base/test/simple_test_tick_clock.h"
|
| +#include "testing/gmock/include/gmock/gmock.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace memory_pressure {
|
| +
|
| +// This is outside of the anonymous namespace as it is a friend to the class
|
| +// under test.
|
| +class TestMemoryPressureStatsCollector : public MemoryPressureStatsCollector {
|
| + public:
|
| + TestMemoryPressureStatsCollector(ReportingDelegate* reporting_delegate,
|
| + base::TickClock* tick_clock)
|
| + : MemoryPressureStatsCollector(reporting_delegate, tick_clock) {}
|
| +
|
| + // Accessors.
|
| + base::TimeDelta unreported_cumulative_time(int i) const {
|
| + return unreported_cumulative_time_[i];
|
| + }
|
| + MemoryPressureLevel last_pressure_level() const {
|
| + return last_pressure_level_;
|
| + }
|
| + base::TimeTicks last_update_time() const { return last_update_time_; }
|
| +};
|
| +
|
| +namespace {
|
| +
|
| +// A mock reporting delegate used in the unit test.
|
| +class LenientMockReportingDelegate
|
| + : public MemoryPressureStatsCollector::ReportingDelegate {
|
| + public:
|
| + using MemoryPressureLevel = MemoryPressureListener::MemoryPressureLevel;
|
| +
|
| + // ReportingDelegate:
|
| + MOCK_METHOD2(ReportCumulativeTime, void(MemoryPressureLevel, int));
|
| + MOCK_METHOD2(ReportLevelChange,
|
| + void(MemoryPressureLevel, MemoryPressureLevel));
|
| +};
|
| +using MockReportingDelegate = testing::StrictMock<LenientMockReportingDelegate>;
|
| +
|
| +// Test fixture.
|
| +class MemoryPressureStatsCollectorTest : public testing::Test {
|
| + public:
|
| + MemoryPressureStatsCollectorTest()
|
| + : collector_(&reporting_delegate_, &tick_clock_) {}
|
| +
|
| + void Tick(int ms) {
|
| + tick_clock_.Advance(base::TimeDelta::FromMilliseconds(ms));
|
| + }
|
| +
|
| + TestMemoryPressureStatsCollector collector_;
|
| + MockReportingDelegate reporting_delegate_;
|
| + base::SimpleTestTickClock tick_clock_;
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +TEST_F(MemoryPressureStatsCollectorTest, EndToEnd) {
|
| + // Upon construction the last pressure level should be invalid.
|
| + EXPECT_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
|
| + collector_.last_pressure_level());
|
| + EXPECT_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
|
| + collector_.last_pressure_level());
|
| + EXPECT_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
|
| + collector_.last_pressure_level());
|
| +
|
| + // And there should be no accumulated time at any pressure levels.
|
| + for (size_t i = 0; i < UMA_MEMORY_PRESSURE_LEVEL_COUNT; ++i)
|
| + EXPECT_TRUE(collector_.unreported_cumulative_time(i).is_zero());
|
| +
|
| + // A first call should not invoke any reporting functions, but it should
|
| + // modify member variables.
|
| + Tick(500);
|
| + collector_.UpdateStatistics(
|
| + MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
|
| + EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
|
| + collector_.last_pressure_level());
|
| + EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time());
|
| + for (size_t i = 0; i < UMA_MEMORY_PRESSURE_LEVEL_COUNT; ++i)
|
| + EXPECT_TRUE(collector_.unreported_cumulative_time(i).is_zero());
|
| + testing::Mock::VerifyAndClearExpectations(&reporting_delegate_);
|
| +
|
| + // A subsequent call with the same pressure level should increment the
|
| + // cumulative time but not make a report, as less than one second of time
|
| + // has been accumulated.
|
| + Tick(500);
|
| + collector_.UpdateStatistics(
|
| + MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
|
| + EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
|
| + collector_.last_pressure_level());
|
| + EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time());
|
| + EXPECT_EQ(base::TimeDelta::FromMilliseconds(500),
|
| + collector_.unreported_cumulative_time(0));
|
| + EXPECT_TRUE(collector_.unreported_cumulative_time(1).is_zero());
|
| + EXPECT_TRUE(collector_.unreported_cumulative_time(2).is_zero());
|
| + testing::Mock::VerifyAndClearExpectations(&reporting_delegate_);
|
| +
|
| + // Yet another call and this time a report should be made, as one second
|
| + // of time has been accumulated. 500ms should remain unreported.
|
| + Tick(1000);
|
| + EXPECT_CALL(reporting_delegate_,
|
| + ReportCumulativeTime(
|
| + MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, 1));
|
| + collector_.UpdateStatistics(
|
| + MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
|
| + EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
|
| + collector_.last_pressure_level());
|
| + EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time());
|
| + EXPECT_EQ(base::TimeDelta::FromMilliseconds(500),
|
| + collector_.unreported_cumulative_time(0));
|
| + EXPECT_TRUE(collector_.unreported_cumulative_time(1).is_zero());
|
| + EXPECT_TRUE(collector_.unreported_cumulative_time(2).is_zero());
|
| + testing::Mock::VerifyAndClearExpectations(&reporting_delegate_);
|
| +
|
| + // A subsequent call with a different pressure level should increment the
|
| + // cumulative time and make several reports.
|
| + Tick(2250);
|
| + EXPECT_CALL(reporting_delegate_,
|
| + ReportCumulativeTime(
|
| + MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, 2));
|
| + EXPECT_CALL(reporting_delegate_,
|
| + ReportLevelChange(
|
| + MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
|
| + MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE));
|
| + collector_.UpdateStatistics(
|
| + MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
|
| + EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
|
| + collector_.last_pressure_level());
|
| + EXPECT_EQ(tick_clock_.NowTicks(), collector_.last_update_time());
|
| + EXPECT_EQ(base::TimeDelta::FromMilliseconds(500),
|
| + collector_.unreported_cumulative_time(0));
|
| + EXPECT_EQ(base::TimeDelta::FromMilliseconds(250),
|
| + collector_.unreported_cumulative_time(1));
|
| + EXPECT_TRUE(collector_.unreported_cumulative_time(2).is_zero());
|
| + testing::Mock::VerifyAndClearExpectations(&reporting_delegate_);
|
| +}
|
| +
|
| +} // namespace memory_pressure
|
|
|