| 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..0db6b2bbaf15c3113ec1db161427f8962be1e8b5
|
| --- /dev/null
|
| +++ b/components/memory_pressure/memory_pressure_stats_collector_unittest.cc
|
| @@ -0,0 +1,150 @@
|
| +// 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:
|
| + // Mutators.
|
| + void set_reporting_delegate(
|
| + scoped_ptr<ReportingDelegate> reporting_delegate) {
|
| + reporting_delegate_ = reporting_delegate.Pass();
|
| + }
|
| + void set_tick_clock(scoped_ptr<base::TickClock> tick_clock) {
|
| + tick_clock_ = tick_clock.Pass();
|
| + }
|
| +
|
| + // 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, base::TimeDelta*));
|
| + MOCK_METHOD2(ReportLevelChange,
|
| + void(MemoryPressureLevel, MemoryPressureLevel));
|
| +};
|
| +using MockReportingDelegate = testing::StrictMock<LenientMockReportingDelegate>;
|
| +
|
| +// Test fixture.
|
| +class MemoryPressureStatsCollectorTest : public testing::Test {
|
| + public:
|
| + void SetUp() override {
|
| + // Ownership is passed to the collector but we keep raw pointers to the
|
| + // injected dependencies.
|
| + reporting_delegate_ = new MockReportingDelegate();
|
| + tick_clock_ = new base::SimpleTestTickClock();
|
| + collector_.set_reporting_delegate(
|
| + scoped_ptr<MemoryPressureStatsCollector::ReportingDelegate>(
|
| + reporting_delegate_));
|
| + collector_.set_tick_clock(scoped_ptr<base::TickClock>(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(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());
|
| + 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 and make a report.
|
| + Tick(1);
|
| + EXPECT_CALL(*reporting_delegate_,
|
| + ReportCumulativeTime(
|
| + MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
|
| + testing::Pointee(base::TimeDelta::FromMilliseconds(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(1),
|
| + 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 the a different pressure level should increment the
|
| + // cumulative time and make several reports.
|
| + Tick(2);
|
| + EXPECT_CALL(*reporting_delegate_,
|
| + ReportCumulativeTime(
|
| + MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
|
| + testing::Pointee(base::TimeDelta::FromMilliseconds(1))));
|
| + EXPECT_CALL(*reporting_delegate_,
|
| + ReportCumulativeTime(
|
| + MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
|
| + testing::Pointee(base::TimeDelta::FromMilliseconds(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(1),
|
| + collector_.unreported_cumulative_time(0));
|
| + EXPECT_EQ(base::TimeDelta::FromMilliseconds(2),
|
| + collector_.unreported_cumulative_time(1));
|
| + EXPECT_TRUE(collector_.unreported_cumulative_time(2).is_zero());
|
| + testing::Mock::VerifyAndClearExpectations(reporting_delegate_);
|
| +}
|
| +
|
| +} // namespace memory_pressure
|
|
|