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

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

Powered by Google App Engine
This is Rietveld 408576698