Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: components/memory_pressure/memory_pressure_stats_collector_unittest.cc

Issue 1310043004: Create MemoryPressureStatsCollector. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 #include "components/memory_pressure/memory_pressure_stats_collector.h"
6
7 #include "base/test/simple_test_tick_clock.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace memory_pressure {
12
13 // This is outside of the anonymous namespace as it is a friend to the class
14 // under test.
15 class TestMemoryPressureStatsCollector : public MemoryPressureStatsCollector {
16 public:
17 // Mutators.
18 void set_reporting_delegate(
19 scoped_ptr<ReportingDelegate> reporting_delegate) {
20 reporting_delegate_ = reporting_delegate.Pass();
21 }
22 void set_tick_clock(scoped_ptr<base::TickClock> tick_clock) {
23 tick_clock_ = tick_clock.Pass();
24 }
25
26 // Accessors.
27 base::TimeDelta unreported_cumulative_time(int i) const {
28 return unreported_cumulative_time_[i];
29 }
30
31 MemoryPressureLevel last_pressure_level() const {
32 return last_pressure_level_;
33 }
34
35 base::TimeTicks last_update_time() const { return last_update_time_; }
36 };
37
38 namespace {
39
40 // A mock reporting delegate used in the unit test.
41 class LenientMockReportingDelegate
42 : public MemoryPressureStatsCollector::ReportingDelegate {
43 public:
44 using MemoryPressureLevel = MemoryPressureListener::MemoryPressureLevel;
45
46 // ReportingDelegate:
47 MOCK_METHOD2(ReportCumulativeTime,
48 void(MemoryPressureLevel, base::TimeDelta*));
49 MOCK_METHOD2(ReportLevelChange,
50 void(MemoryPressureLevel, MemoryPressureLevel));
51 };
52 using MockReportingDelegate = testing::StrictMock<LenientMockReportingDelegate>;
53
54 // Test fixture.
55 class MemoryPressureStatsCollectorTest : public testing::Test {
56 public:
57 void SetUp() override {
58 // Ownership is passed to the collector but we keep raw pointers to the
59 // injected dependencies.
60 reporting_delegate_ = new MockReportingDelegate();
61 tick_clock_ = new base::SimpleTestTickClock();
62 collector_.set_reporting_delegate(
63 scoped_ptr<MemoryPressureStatsCollector::ReportingDelegate>(
64 reporting_delegate_));
65 collector_.set_tick_clock(scoped_ptr<base::TickClock>(tick_clock_));
66 }
67
68 void Tick(int ms) {
69 tick_clock_->Advance(base::TimeDelta::FromMilliseconds(ms));
70 }
71
72 TestMemoryPressureStatsCollector collector_;
73 MockReportingDelegate* reporting_delegate_;
74 base::SimpleTestTickClock* tick_clock_;
75 };
76
77 } // namespace
78
79 TEST_F(MemoryPressureStatsCollectorTest, EndToEnd) {
80 // Upon construction the last pressure level should be invalid.
81 EXPECT_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
82 collector_.last_pressure_level());
83 EXPECT_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
84 collector_.last_pressure_level());
85 EXPECT_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
86 collector_.last_pressure_level());
87
88 // And there should be no accumulated time at any pressure levels.
89 for (size_t i = 0; i < UMA_MEMORY_PRESSURE_LEVEL_COUNT; ++i)
90 EXPECT_TRUE(collector_.unreported_cumulative_time(i).is_zero());
91
92 // A first call should not invoke any reporting functions, but it should
93 // modify member variables.
94 Tick(1);
95 collector_.UpdateStatistics(
96 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
97 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
98 collector_.last_pressure_level());
99 EXPECT_EQ(tick_clock_->NowTicks(), collector_.last_update_time());
100 for (size_t i = 0; i < UMA_MEMORY_PRESSURE_LEVEL_COUNT; ++i)
101 EXPECT_TRUE(collector_.unreported_cumulative_time(i).is_zero());
102 testing::Mock::VerifyAndClearExpectations(reporting_delegate_);
103
104 // A subsequent call with the same pressure level should increment the
105 // cumulative time and make a report.
106 Tick(1);
107 EXPECT_CALL(*reporting_delegate_,
108 ReportCumulativeTime(
109 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
110 testing::Pointee(base::TimeDelta::FromMilliseconds(1))));
111 collector_.UpdateStatistics(
112 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
113 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
114 collector_.last_pressure_level());
115 EXPECT_EQ(tick_clock_->NowTicks(), collector_.last_update_time());
116 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1),
117 collector_.unreported_cumulative_time(0));
118 EXPECT_TRUE(collector_.unreported_cumulative_time(1).is_zero());
119 EXPECT_TRUE(collector_.unreported_cumulative_time(2).is_zero());
120 testing::Mock::VerifyAndClearExpectations(reporting_delegate_);
121
122 // A subsequent call with the a different pressure level should increment the
123 // cumulative time and make several reports.
124 Tick(2);
125 EXPECT_CALL(*reporting_delegate_,
126 ReportCumulativeTime(
127 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
128 testing::Pointee(base::TimeDelta::FromMilliseconds(1))));
129 EXPECT_CALL(*reporting_delegate_,
130 ReportCumulativeTime(
131 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
132 testing::Pointee(base::TimeDelta::FromMilliseconds(2))));
133 EXPECT_CALL(*reporting_delegate_,
134 ReportLevelChange(
135 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
136 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE));
137 collector_.UpdateStatistics(
138 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
139 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
140 collector_.last_pressure_level());
141 EXPECT_EQ(tick_clock_->NowTicks(), collector_.last_update_time());
142 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1),
143 collector_.unreported_cumulative_time(0));
144 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2),
145 collector_.unreported_cumulative_time(1));
146 EXPECT_TRUE(collector_.unreported_cumulative_time(2).is_zero());
147 testing::Mock::VerifyAndClearExpectations(reporting_delegate_);
148 }
149
150 } // namespace memory_pressure
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698