| OLD | NEW |
| (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 static const int kPollInterval = 10; // seconds |
| 29 |
| 30 } // namespace |
| 31 |
| 32 class MemoryMonitorMacTest : public testing::Test { |
| 33 public: |
| 34 TestMemoryMonitorMacDelegate delegate_; |
| 35 std::unique_ptr<MemoryMonitorMac> monitor_; |
| 36 }; |
| 37 |
| 38 TEST_F(MemoryMonitorMacTest, Create) { |
| 39 delegate_.SetTotalMemoryKB(100000 * kKBperMB); |
| 40 monitor_ = MemoryMonitorMac::Create( |
| 41 &delegate_, base::TimeDelta::FromSeconds(kPollInterval)); |
| 42 EXPECT_EQ(0U, delegate_.calls()); |
| 43 } |
| 44 |
| 45 TEST_F(MemoryMonitorMacTest, GetFreeMemoryUntilCriticalMB) { |
| 46 delegate_.SetTotalMemoryKB(1000 * kKBperMB); |
| 47 delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_NORMAL); |
| 48 |
| 49 monitor_.reset(new MemoryMonitorMac( |
| 50 &delegate_, base::TimeDelta::FromSeconds(kPollInterval))); |
| 51 EXPECT_EQ(0u, delegate_.calls()); |
| 52 |
| 53 delegate_.SetFreeMemoryKB(640 * kKBperMB); |
| 54 EXPECT_EQ(640, monitor_->GetFreeMemoryUntilCriticalMB()); |
| 55 EXPECT_EQ(2U, delegate_.calls()); |
| 56 delegate_.ResetCalls(); |
| 57 |
| 58 delegate_.SetFreeMemoryKB(640 * kKBperMB); |
| 59 delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_WARN); |
| 60 EXPECT_EQ(320, monitor_->GetFreeMemoryUntilCriticalMB()); |
| 61 EXPECT_EQ(2U, delegate_.calls()); |
| 62 delegate_.ResetCalls(); |
| 63 EXPECT_EQ(310, monitor_->GetFreeMemoryUntilCriticalMB()); |
| 64 EXPECT_EQ(2U, delegate_.calls()); |
| 65 delegate_.ResetCalls(); |
| 66 EXPECT_EQ(300, monitor_->GetFreeMemoryUntilCriticalMB()); |
| 67 EXPECT_EQ(2U, delegate_.calls()); |
| 68 delegate_.ResetCalls(); |
| 69 |
| 70 delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_NORMAL); |
| 71 EXPECT_EQ(310, monitor_->GetFreeMemoryUntilCriticalMB()); |
| 72 EXPECT_EQ(2U, delegate_.calls()); |
| 73 delegate_.ResetCalls(); |
| 74 EXPECT_EQ(320, monitor_->GetFreeMemoryUntilCriticalMB()); |
| 75 EXPECT_EQ(2U, delegate_.calls()); |
| 76 delegate_.ResetCalls(); |
| 77 EXPECT_EQ(330, monitor_->GetFreeMemoryUntilCriticalMB()); |
| 78 EXPECT_EQ(2U, delegate_.calls()); |
| 79 delegate_.ResetCalls(); |
| 80 |
| 81 delegate_.SetFreeMemoryKB(64 * kKBperMB); |
| 82 delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_CRITICAL); |
| 83 EXPECT_EQ(0, monitor_->GetFreeMemoryUntilCriticalMB()); |
| 84 EXPECT_EQ(2U, delegate_.calls()); |
| 85 delegate_.ResetCalls(); |
| 86 EXPECT_EQ(0, monitor_->GetFreeMemoryUntilCriticalMB()); |
| 87 EXPECT_EQ(2U, delegate_.calls()); |
| 88 delegate_.ResetCalls(); |
| 89 |
| 90 delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_NORMAL); |
| 91 EXPECT_EQ(10, monitor_->GetFreeMemoryUntilCriticalMB()); |
| 92 EXPECT_EQ(2U, delegate_.calls()); |
| 93 delegate_.ResetCalls(); |
| 94 EXPECT_EQ(20, monitor_->GetFreeMemoryUntilCriticalMB()); |
| 95 EXPECT_EQ(2U, delegate_.calls()); |
| 96 delegate_.ResetCalls(); |
| 97 EXPECT_EQ(30, monitor_->GetFreeMemoryUntilCriticalMB()); |
| 98 EXPECT_EQ(2U, delegate_.calls()); |
| 99 delegate_.ResetCalls(); |
| 100 } |
| 101 |
| 102 } // namespace content |
| OLD | NEW |