| Index: base/chromeos/memory_pressure_observer_chromeos_unittest.cc
|
| diff --git a/base/chromeos/memory_pressure_observer_chromeos_unittest.cc b/base/chromeos/memory_pressure_observer_chromeos_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..5348b675004d02df5a88a4d8871c4bdecea44569
|
| --- /dev/null
|
| +++ b/base/chromeos/memory_pressure_observer_chromeos_unittest.cc
|
| @@ -0,0 +1,141 @@
|
| +// Copyright 2014 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 "base/basictypes.h"
|
| +#include "base/chromeos/memory_pressure_observer_chromeos.h"
|
| +#include "base/memory/memory_pressure_listener.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace base {
|
| +
|
| +namespace {
|
| +
|
| +// The memory in percent value which gets returned to the observer.
|
| +// Do not read/modify value directly.
|
| +int s_memory_in_percent_override_ = 0;
|
| +
|
| +// True if the memory notifier got called.
|
| +// Do not read/modify value directly.
|
| +bool s_on_memory_pressure_called_ = false;
|
| +
|
| +// Call to set the memory in percent override.
|
| +void SetMemoryInPercentOverride(int override) {
|
| + s_memory_in_percent_override_ = override;
|
| +}
|
| +
|
| +// Returns the memory pressure in percent.
|
| +int GetMemoryInPercentOverride() {
|
| + return s_memory_in_percent_override_;
|
| +}
|
| +
|
| +// Processes OnMemoryPressure calls.
|
| +void OnMemoryPressure(MemoryPressureListener::MemoryPressureLevel level) {
|
| + s_on_memory_pressure_called_ = true;
|
| + if (s_memory_in_percent_override_ < 90)
|
| + EXPECT_EQ(level, MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
|
| + else
|
| + EXPECT_EQ(level, MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
|
| +}
|
| +
|
| +// Resets the indicator for memory pressure.
|
| +void ResetOnMemoryPressureCalled() {
|
| + s_on_memory_pressure_called_ = false;
|
| +}
|
| +
|
| +// Returns true when OnMemoryPressure was called (and resets it).
|
| +bool WasOnMemoryPressureCalled() {
|
| + bool b = s_on_memory_pressure_called_;
|
| + ResetOnMemoryPressureCalled();
|
| + return b;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +// This test tests the various transition states from memory pressure, looking
|
| +// for the correct behavior on event reposting as well as state updates.
|
| +TEST(MemoryPressureObserverChromeOSTest, CheckMemoryPressure) {
|
| + base::MessageLoopForUI message_loop;
|
| + scoped_ptr<MemoryPressureObserverChromeOS> observer(
|
| + new MemoryPressureObserverChromeOS);
|
| + // Disable any timers which are going on and set a special memory reporting
|
| + // function.
|
| + observer->StopObserving();
|
| + observer->SetGetUsedMemoryInPercentCallbackForUnittest(
|
| + GetMemoryInPercentOverride);
|
| + scoped_ptr<MemoryPressureListener> listener(
|
| + new MemoryPressureListener(base::Bind(&OnMemoryPressure)));
|
| + // Checking the memory pressure while 0% are used should not produce any
|
| + // events.
|
| + SetMemoryInPercentOverride(0);
|
| + ResetOnMemoryPressureCalled();
|
| +
|
| + observer->CheckMemoryPressure();
|
| + message_loop.RunUntilIdle();
|
| + EXPECT_FALSE(WasOnMemoryPressureCalled());
|
| + EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
|
| + observer->GetCurrentPressureLevel());
|
| +
|
| + // Setting the memory level to 80% should produce a moderate pressure level.
|
| + SetMemoryInPercentOverride(80);
|
| + observer->CheckMemoryPressure();
|
| + message_loop.RunUntilIdle();
|
| + EXPECT_TRUE(WasOnMemoryPressureCalled());
|
| + EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
|
| + observer->GetCurrentPressureLevel());
|
| +
|
| + // We need to check that the event gets reposted after a while.
|
| + int i = 0;
|
| + for (; i < 100; i++) {
|
| + observer->CheckMemoryPressure();
|
| + message_loop.RunUntilIdle();
|
| + EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
|
| + observer->GetCurrentPressureLevel());
|
| + if (WasOnMemoryPressureCalled())
|
| + break;
|
| + }
|
| + // Should be more then 5 and less then 100.
|
| + EXPECT_LE(5, i);
|
| + EXPECT_GE(99, i);
|
| +
|
| + // Setting the memory usage to 99% should produce critical levels.
|
| + SetMemoryInPercentOverride(99);
|
| + observer->CheckMemoryPressure();
|
| + message_loop.RunUntilIdle();
|
| + EXPECT_TRUE(WasOnMemoryPressureCalled());
|
| + EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
|
| + observer->GetCurrentPressureLevel());
|
| +
|
| + // Calling it again should immediately produce a second call.
|
| + observer->CheckMemoryPressure();
|
| + message_loop.RunUntilIdle();
|
| + EXPECT_TRUE(WasOnMemoryPressureCalled());
|
| + EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
|
| + observer->GetCurrentPressureLevel());
|
| +
|
| + // When lowering the pressure again we should not get an event, but the
|
| + // pressure should go back to moderate.
|
| + SetMemoryInPercentOverride(80);
|
| + observer->CheckMemoryPressure();
|
| + message_loop.RunUntilIdle();
|
| + EXPECT_FALSE(WasOnMemoryPressureCalled());
|
| + EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
|
| + observer->GetCurrentPressureLevel());
|
| +
|
| + // We should need exactly the same amount of calls as before, before the next
|
| + // call comes in.
|
| + int j = 0;
|
| + for (; j < 100; j++) {
|
| + observer->CheckMemoryPressure();
|
| + message_loop.RunUntilIdle();
|
| + EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
|
| + observer->GetCurrentPressureLevel());
|
| + if (WasOnMemoryPressureCalled())
|
| + break;
|
| + }
|
| + // We should have needed exactly the same amount of checks as before.
|
| + EXPECT_EQ(j, i);
|
| +}
|
| +
|
| +} // namespace base
|
|
|