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

Side by Side Diff: content/browser/memory/memory_monitor_mac_unittest.cc

Issue 2360433002: Memory-monitor for Mac. (Closed)
Patch Set: Created 4 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 (c) 2016 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 "content/browser/memory/memory_monitor_mac.h"
6
7 #include "content/browser/memory/test_memory_monitor.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace content {
11
12 namespace {
13
14 // A delegate that allows mocking the various inputs to MemoryMonitorMac.
15 class TestMemoryMonitorMacDelegate : public TestMemoryMonitorDelegate {
16 public:
17 TestMemoryMonitorMacDelegate() {}
18
19 void SetFreeMemoryKB(int free_kb) { mem_info_.free = free_kb; }
20
21 private:
22 DISALLOW_COPY_AND_ASSIGN(TestMemoryMonitorMacDelegate);
23 };
24
25 class TestMemoryMonitorMac : public MemoryMonitorMac {};
26
27 static const int kKBperMB = 1024;
28
29 } // namespace
30
31 class MemoryMonitorMacTest : public testing::Test {
32 public:
33 TestMemoryMonitorMacDelegate delegate_;
34 std::unique_ptr<MemoryMonitorMac> monitor_;
35 };
36
37 TEST_F(MemoryMonitorMacTest, Create) {
38 delegate_.SetTotalMemoryKB(100000 * kKBperMB);
39 monitor_ = MemoryMonitorMac::Create(&delegate_);
40 EXPECT_EQ(0U, delegate_.calls());
41 }
42
43 TEST_F(MemoryMonitorMacTest, GetFreeMemoryUntilCriticalMB) {
44 delegate_.SetTotalMemoryKB(1000 * kKBperMB);
45 delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_NORMAL);
46
47 monitor_.reset(new MemoryMonitorMac(&delegate_));
48 EXPECT_EQ(0u, delegate_.calls());
49
50 delegate_.SetFreeMemoryKB(64 * kKBperMB);
51 EXPECT_EQ(64, monitor_->GetFreeMemoryUntilCriticalMB());
52 EXPECT_EQ(2U, delegate_.calls());
53 delegate_.ResetCalls();
54
55 delegate_.SetFreeMemoryKB(64 * kKBperMB);
56 delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_WARN);
57 EXPECT_EQ(32, monitor_->GetFreeMemoryUntilCriticalMB());
58 EXPECT_EQ(2U, delegate_.calls());
59 delegate_.ResetCalls();
60 EXPECT_EQ(31, monitor_->GetFreeMemoryUntilCriticalMB());
61 EXPECT_EQ(2U, delegate_.calls());
62 delegate_.ResetCalls();
63 EXPECT_EQ(30, monitor_->GetFreeMemoryUntilCriticalMB());
64 EXPECT_EQ(2U, delegate_.calls());
65 delegate_.ResetCalls();
66
67 delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_NORMAL);
68 EXPECT_EQ(31, monitor_->GetFreeMemoryUntilCriticalMB());
69 EXPECT_EQ(2U, delegate_.calls());
70 delegate_.ResetCalls();
71 EXPECT_EQ(32, monitor_->GetFreeMemoryUntilCriticalMB());
72 EXPECT_EQ(2U, delegate_.calls());
73 delegate_.ResetCalls();
74 EXPECT_EQ(33, monitor_->GetFreeMemoryUntilCriticalMB());
75 EXPECT_EQ(2U, delegate_.calls());
76 delegate_.ResetCalls();
77
78 delegate_.SetFreeMemoryKB(64 * kKBperMB);
79 delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_CRITICAL);
80 EXPECT_EQ(0, monitor_->GetFreeMemoryUntilCriticalMB());
81 EXPECT_EQ(2U, delegate_.calls());
82 delegate_.ResetCalls();
83 EXPECT_EQ(0, monitor_->GetFreeMemoryUntilCriticalMB());
84 EXPECT_EQ(2U, delegate_.calls());
85 delegate_.ResetCalls();
86
87 delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_NORMAL);
88 EXPECT_EQ(1, monitor_->GetFreeMemoryUntilCriticalMB());
89 EXPECT_EQ(2U, delegate_.calls());
90 delegate_.ResetCalls();
91 EXPECT_EQ(2, monitor_->GetFreeMemoryUntilCriticalMB());
92 EXPECT_EQ(2U, delegate_.calls());
93 delegate_.ResetCalls();
94 EXPECT_EQ(3, monitor_->GetFreeMemoryUntilCriticalMB());
95 EXPECT_EQ(2U, delegate_.calls());
96 delegate_.ResetCalls();
97 }
98
99 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698