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

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: Rework UMA notifications. 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 MemoryPressureLevel last_pressure_level() const {
31 return last_pressure_level_;
32 }
33
34 base::TimeTicks last_update_time() const { return last_update_time_; }
35 };
36
37 namespace {
38
39 // A mock reporting delegate used in the unit test.
40 class LenientMockReportingDelegate
41 : public MemoryPressureStatsCollector::ReportingDelegate {
42 public:
43 using MemoryPressureLevel = MemoryPressureListener::MemoryPressureLevel;
44
45 // ReportingDelegate:
46 MOCK_METHOD2(ReportCumulativeTime, void(MemoryPressureLevel, int));
47 MOCK_METHOD2(ReportLevelChange,
48 void(MemoryPressureLevel, MemoryPressureLevel));
49 };
50 using MockReportingDelegate = testing::StrictMock<LenientMockReportingDelegate>;
51
52 // Test fixture.
53 class MemoryPressureStatsCollectorTest : public testing::Test {
54 public:
55 void SetUp() override {
56 // Ownership is passed to the collector but we keep raw pointers to the
57 // injected dependencies.
58 reporting_delegate_ = new MockReportingDelegate();
59 tick_clock_ = new base::SimpleTestTickClock();
60 collector_.set_reporting_delegate(
61 scoped_ptr<MemoryPressureStatsCollector::ReportingDelegate>(
62 reporting_delegate_));
63 collector_.set_tick_clock(scoped_ptr<base::TickClock>(tick_clock_));
64 }
65
66 void Tick(int ms) {
67 tick_clock_->Advance(base::TimeDelta::FromMilliseconds(ms));
68 }
69
70 TestMemoryPressureStatsCollector collector_;
71 MockReportingDelegate* reporting_delegate_;
72 base::SimpleTestTickClock* tick_clock_;
73 };
74
75 } // namespace
76
77 TEST_F(MemoryPressureStatsCollectorTest, EndToEnd) {
78 // Upon construction the last pressure level should be invalid.
79 EXPECT_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
80 collector_.last_pressure_level());
81 EXPECT_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
82 collector_.last_pressure_level());
83 EXPECT_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
84 collector_.last_pressure_level());
85
86 // And there should be no accumulated time at any pressure levels.
87 for (size_t i = 0; i < UMA_MEMORY_PRESSURE_LEVEL_COUNT; ++i)
88 EXPECT_TRUE(collector_.unreported_cumulative_time(i).is_zero());
89
90 // A first call should not invoke any reporting functions, but it should
91 // modify member variables.
92 Tick(500);
93 collector_.UpdateStatistics(
94 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
95 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
96 collector_.last_pressure_level());
97 EXPECT_EQ(tick_clock_->NowTicks(), collector_.last_update_time());
98 for (size_t i = 0; i < UMA_MEMORY_PRESSURE_LEVEL_COUNT; ++i)
99 EXPECT_TRUE(collector_.unreported_cumulative_time(i).is_zero());
100 testing::Mock::VerifyAndClearExpectations(reporting_delegate_);
101
102 // A subsequent call with the same pressure level should increment the
103 // cumulative time but not make a report, as less than one second of time
104 // has been accumulated.
105 Tick(500);
106 collector_.UpdateStatistics(
107 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
108 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
109 collector_.last_pressure_level());
110 EXPECT_EQ(tick_clock_->NowTicks(), collector_.last_update_time());
111 EXPECT_EQ(base::TimeDelta::FromMilliseconds(500),
112 collector_.unreported_cumulative_time(0));
113 EXPECT_TRUE(collector_.unreported_cumulative_time(1).is_zero());
114 EXPECT_TRUE(collector_.unreported_cumulative_time(2).is_zero());
115 testing::Mock::VerifyAndClearExpectations(reporting_delegate_);
116
117 // Yet another call and this time a report should be made, as one second
118 // of time has been accumulated. 500ms should remain unreported.
119 Tick(1000);
120 EXPECT_CALL(*reporting_delegate_,
121 ReportCumulativeTime(
122 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, 1));
123 collector_.UpdateStatistics(
124 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
125 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
126 collector_.last_pressure_level());
127 EXPECT_EQ(tick_clock_->NowTicks(), collector_.last_update_time());
128 EXPECT_EQ(base::TimeDelta::FromMilliseconds(500),
129 collector_.unreported_cumulative_time(0));
130 EXPECT_TRUE(collector_.unreported_cumulative_time(1).is_zero());
131 EXPECT_TRUE(collector_.unreported_cumulative_time(2).is_zero());
132 testing::Mock::VerifyAndClearExpectations(reporting_delegate_);
133
134 // A subsequent call with the a different pressure level should increment the
135 // cumulative time and make several reports.
136 Tick(2250);
137 EXPECT_CALL(*reporting_delegate_,
138 ReportCumulativeTime(
139 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, 2));
140 EXPECT_CALL(*reporting_delegate_,
141 ReportLevelChange(
142 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
143 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE));
144 collector_.UpdateStatistics(
145 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
146 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
147 collector_.last_pressure_level());
148 EXPECT_EQ(tick_clock_->NowTicks(), collector_.last_update_time());
149 EXPECT_EQ(base::TimeDelta::FromMilliseconds(500),
150 collector_.unreported_cumulative_time(0));
151 EXPECT_EQ(base::TimeDelta::FromMilliseconds(250),
152 collector_.unreported_cumulative_time(1));
153 EXPECT_TRUE(collector_.unreported_cumulative_time(2).is_zero());
154 testing::Mock::VerifyAndClearExpectations(reporting_delegate_);
155 }
156
157 } // namespace memory_pressure
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698