| Index: content/browser/memory/memory_monitor_mac_unittest.cc
|
| diff --git a/content/browser/memory/memory_monitor_mac_unittest.cc b/content/browser/memory/memory_monitor_mac_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9d040cdad27e9462889048fa485888b1a6136717
|
| --- /dev/null
|
| +++ b/content/browser/memory/memory_monitor_mac_unittest.cc
|
| @@ -0,0 +1,102 @@
|
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "content/browser/memory/memory_monitor_mac.h"
|
| +
|
| +#include "content/browser/memory/test_memory_monitor.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace content {
|
| +
|
| +namespace {
|
| +
|
| +// A delegate that allows mocking the various inputs to MemoryMonitorMac.
|
| +class TestMemoryMonitorMacDelegate : public TestMemoryMonitorDelegate {
|
| + public:
|
| + TestMemoryMonitorMacDelegate() {}
|
| +
|
| + void SetFreeMemoryKB(int free_kb) { mem_info_.free = free_kb; }
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(TestMemoryMonitorMacDelegate);
|
| +};
|
| +
|
| +class TestMemoryMonitorMac : public MemoryMonitorMac {};
|
| +
|
| +static const int kKBperMB = 1024;
|
| +static const int kPollInterval = 10; // seconds
|
| +
|
| +} // namespace
|
| +
|
| +class MemoryMonitorMacTest : public testing::Test {
|
| + public:
|
| + TestMemoryMonitorMacDelegate delegate_;
|
| + std::unique_ptr<MemoryMonitorMac> monitor_;
|
| +};
|
| +
|
| +TEST_F(MemoryMonitorMacTest, Create) {
|
| + delegate_.SetTotalMemoryKB(100000 * kKBperMB);
|
| + monitor_ = MemoryMonitorMac::Create(
|
| + &delegate_, base::TimeDelta::FromSeconds(kPollInterval));
|
| + EXPECT_EQ(0U, delegate_.calls());
|
| +}
|
| +
|
| +TEST_F(MemoryMonitorMacTest, GetFreeMemoryUntilCriticalMB) {
|
| + delegate_.SetTotalMemoryKB(1000 * kKBperMB);
|
| + delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_NORMAL);
|
| +
|
| + monitor_.reset(new MemoryMonitorMac(
|
| + &delegate_, base::TimeDelta::FromSeconds(kPollInterval)));
|
| + EXPECT_EQ(0u, delegate_.calls());
|
| +
|
| + delegate_.SetFreeMemoryKB(640 * kKBperMB);
|
| + EXPECT_EQ(640, monitor_->GetFreeMemoryUntilCriticalMB());
|
| + EXPECT_EQ(2U, delegate_.calls());
|
| + delegate_.ResetCalls();
|
| +
|
| + delegate_.SetFreeMemoryKB(640 * kKBperMB);
|
| + delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_WARN);
|
| + EXPECT_EQ(320, monitor_->GetFreeMemoryUntilCriticalMB());
|
| + EXPECT_EQ(2U, delegate_.calls());
|
| + delegate_.ResetCalls();
|
| + EXPECT_EQ(310, monitor_->GetFreeMemoryUntilCriticalMB());
|
| + EXPECT_EQ(2U, delegate_.calls());
|
| + delegate_.ResetCalls();
|
| + EXPECT_EQ(300, monitor_->GetFreeMemoryUntilCriticalMB());
|
| + EXPECT_EQ(2U, delegate_.calls());
|
| + delegate_.ResetCalls();
|
| +
|
| + delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_NORMAL);
|
| + EXPECT_EQ(310, monitor_->GetFreeMemoryUntilCriticalMB());
|
| + EXPECT_EQ(2U, delegate_.calls());
|
| + delegate_.ResetCalls();
|
| + EXPECT_EQ(320, monitor_->GetFreeMemoryUntilCriticalMB());
|
| + EXPECT_EQ(2U, delegate_.calls());
|
| + delegate_.ResetCalls();
|
| + EXPECT_EQ(330, monitor_->GetFreeMemoryUntilCriticalMB());
|
| + EXPECT_EQ(2U, delegate_.calls());
|
| + delegate_.ResetCalls();
|
| +
|
| + delegate_.SetFreeMemoryKB(64 * kKBperMB);
|
| + delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_CRITICAL);
|
| + EXPECT_EQ(0, monitor_->GetFreeMemoryUntilCriticalMB());
|
| + EXPECT_EQ(2U, delegate_.calls());
|
| + delegate_.ResetCalls();
|
| + EXPECT_EQ(0, monitor_->GetFreeMemoryUntilCriticalMB());
|
| + EXPECT_EQ(2U, delegate_.calls());
|
| + delegate_.ResetCalls();
|
| +
|
| + delegate_.SetMemoryPressure(DISPATCH_MEMORYPRESSURE_NORMAL);
|
| + EXPECT_EQ(10, monitor_->GetFreeMemoryUntilCriticalMB());
|
| + EXPECT_EQ(2U, delegate_.calls());
|
| + delegate_.ResetCalls();
|
| + EXPECT_EQ(20, monitor_->GetFreeMemoryUntilCriticalMB());
|
| + EXPECT_EQ(2U, delegate_.calls());
|
| + delegate_.ResetCalls();
|
| + EXPECT_EQ(30, monitor_->GetFreeMemoryUntilCriticalMB());
|
| + EXPECT_EQ(2U, delegate_.calls());
|
| + delegate_.ResetCalls();
|
| +}
|
| +
|
| +} // namespace content
|
|
|