memory_pressure_listener_unittest.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include "base/memory/memory_pressure_listener.h"
  5. #include "base/bind.h"
  6. #include "base/run_loop.h"
  7. #include "base/test/task_environment.h"
  8. #include "testing/gmock/include/gmock/gmock.h"
  9. namespace base {
  10. using MemoryPressureLevel = MemoryPressureListener::MemoryPressureLevel;
  11. class MemoryPressureListenerTest : public testing::Test {
  12. public:
  13. MemoryPressureListenerTest()
  14. : task_environment_(test::TaskEnvironment::MainThreadType::UI) {}
  15. void SetUp() override {
  16. listener_ = std::make_unique<MemoryPressureListener>(
  17. FROM_HERE, BindRepeating(&MemoryPressureListenerTest::OnMemoryPressure,
  18. Unretained(this)));
  19. }
  20. void TearDown() override {
  21. listener_.reset();
  22. }
  23. protected:
  24. void ExpectNotification(
  25. void (*notification_function)(MemoryPressureLevel),
  26. MemoryPressureLevel level) {
  27. EXPECT_CALL(*this, OnMemoryPressure(level)).Times(1);
  28. notification_function(level);
  29. RunLoop().RunUntilIdle();
  30. }
  31. void ExpectNoNotification(
  32. void (*notification_function)(MemoryPressureLevel),
  33. MemoryPressureLevel level) {
  34. EXPECT_CALL(*this, OnMemoryPressure(testing::_)).Times(0);
  35. notification_function(level);
  36. RunLoop().RunUntilIdle();
  37. }
  38. private:
  39. MOCK_METHOD1(OnMemoryPressure,
  40. void(MemoryPressureListener::MemoryPressureLevel));
  41. test::TaskEnvironment task_environment_;
  42. std::unique_ptr<MemoryPressureListener> listener_;
  43. };
  44. TEST_F(MemoryPressureListenerTest, NotifyMemoryPressure) {
  45. // Memory pressure notifications are not suppressed by default.
  46. EXPECT_FALSE(MemoryPressureListener::AreNotificationsSuppressed());
  47. ExpectNotification(&MemoryPressureListener::NotifyMemoryPressure,
  48. MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE);
  49. ExpectNotification(&MemoryPressureListener::SimulatePressureNotification,
  50. MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE);
  51. // Enable suppressing memory pressure notifications.
  52. MemoryPressureListener::SetNotificationsSuppressed(true);
  53. EXPECT_TRUE(MemoryPressureListener::AreNotificationsSuppressed());
  54. ExpectNoNotification(&MemoryPressureListener::NotifyMemoryPressure,
  55. MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE);
  56. ExpectNotification(&MemoryPressureListener::SimulatePressureNotification,
  57. MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE);
  58. // Disable suppressing memory pressure notifications.
  59. MemoryPressureListener::SetNotificationsSuppressed(false);
  60. EXPECT_FALSE(MemoryPressureListener::AreNotificationsSuppressed());
  61. ExpectNotification(&MemoryPressureListener::NotifyMemoryPressure,
  62. MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL);
  63. ExpectNotification(&MemoryPressureListener::SimulatePressureNotification,
  64. MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL);
  65. }
  66. } // namespace base